📄 owfequeryitem.java
字号:
/* * Copyright (c) 2005, John Mettraux, OpenWFE.org * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * . Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * . Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * . Neither the name of the "OpenWFE" nor the names of its contributors may be * used to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * $Id: OwfeQueryItem.java,v 1.2 2005/05/17 16:41:06 jmettraux Exp $ *///// OwfeQueryItem.java//// john.mettraux@openwfe.org//// generated with // jtmpl 1.1.00 16.08.2003 John Mettraux (jmettraux@openwfe.org)//package openwfe.org.xdbc.nquery;import openwfe.org.query.QueryException;import openwfe.org.query.QueryConfiguration;import openwfe.org.query.sets.Set;import openwfe.org.query.item.Query;import openwfe.org.sql.SqlUtils;/** * A query item focussed on how OpenWFE workitem fields (attributes) are * structered (in XML) and thus capable of querying like : "please return * me workitems with a field named "X" that has a value of "Y"... * * <p><font size=2>CVS Info : * <br>$Author: jmettraux $ * <br>$Id: OwfeQueryItem.java,v 1.2 2005/05/17 16:41:06 jmettraux Exp $ </font> * * @author john.mettraux@openwfe.org */public class OwfeQueryItem extends AtomicQueryItem{ private final static org.apache.log4j.Logger log = org.apache.log4j.Logger .getLogger(OwfeQueryItem.class.getName()); // // CONSTANTS & co //public final static String STRING_TYPE = "string"; public final static String ID = "id"; public final static String PARENT_ID = "parentElementId"; public final static String KEY = "key"; public final static String VALUE = "value"; //public final static String XML_ELT_NAME = "owfe-query"; //public final static String XML_ATT_KEY = "key"; //public final static String XML_ATT_VALUE = "value"; //public final static String XML_ATT_KEY_TYPE = "key-type"; //public final static String XML_ATT_VALUE_TYPE = "value-type"; // // FIELDS private String key = null; private String keyType = null; private String value = null; private String valueType = null; // // CONSTRUCTORS public void init (final QueryConfiguration config, final Query parent, final Object arg) throws QueryException { final org.jdom.Element elt = (org.jdom.Element)arg; this.key = elt.getAttributeValue("key"); this.keyType = elt.getAttributeValue("key-type"); this.value = elt.getAttributeValue("value"); this.valueType = elt.getAttributeValue("value-type"); setValueOperator(elt.getAttributeValue("op")); } /* public OwfeQueryItem (String key, String value) { this.key = key; this.value = value; } public OwfeQueryItem (String key, String value, int valueOperator) { this.key = key; this.value = value; this.valueOperator = valueOperator; } public OwfeQueryItem (String key, String valueType, String value) { this.key = key; this.valueType = valueType; this.value = value; } public OwfeQueryItem (String keyType, String key, String valueType, String value) { this.keyType = keyType; this.key = key; this.valueType = valueType; this.value = value; } */ // // METHODS from QueryItem public String getQueryString () { // // is never used return null; } /* public org.jdom.Element encode () { org.jdom.Element result = new org.jdom.Element(XML_ELT_NAME); result.setAttribute(XML_ATT_KEY, this.key); result.setAttribute(XML_ATT_VALUE, this.value); if (this.keyType != null) result.setAttribute(XML_ATT_KEY_TYPE, this.keyType); if (this.valueType != null) result.setAttribute(XML_ATT_VALUE_TYPE, this.valueType); result.setAttribute(XML_ATT_VALUE_OPERATOR, encodeValueOperator()); return result; } */ //public boolean matches (org.jdom.Element elt) public boolean validate (final Object o) throws QueryException { final org.jdom.Element elt = (org.jdom.Element)o; if (elt.getName().equals("entry")) { org.jdom.Element eKey = getFirstChild(elt.getChild("key")); org.jdom.Element eValue = getFirstChild(elt.getChild("value")); /* log.debug("\n targetKey '"+this.keyType+"' --> '"+this.key+"'"); log.debug("\n targetValue '"+this.valueType+"' --> '"+this.value+"'"); log.debug("\n eKey '"+eKey.getName()+"' --> '"+eKey.getTextTrim()+"'"); log.debug("\n eValue '"+eValue.getName()+"' --> '"+eValue.getTextTrim()+"'"); */ boolean result = eKey.getTextTrim().matches(this.key); if (this.keyType != null) result = result && (eKey.getName().equals(this.keyType)); if (this.valueType != null) result = result && (eValue.getName().equals(this.valueType)); if (getValueOperator() == LIKE) result = result && eValue.getTextTrim().matches(this.value); else result = result && compare(eValue.getTextTrim(), this.value); if (result) return true; } return childrenMatches(elt); } private org.jdom.Element getFirstChild (org.jdom.Element elt) { if (elt.getChildren().size() > 0) return (org.jdom.Element)elt.getChildren().get(0); return null; } //public Set query (String clientIdLike, java.sql.Connection con) public Set query (final Object queryArg) throws QueryException { final Object[] os = (Object[])queryArg; final String clientIdLike = (String)os[0]; final java.sql.Connection con = (java.sql.Connection)os[1]; final IdSet result = new IdSet(); // // querying for value first, // it might make the query quicker as there are // less chances to hit a good value, // key names are more homogeneous log.debug("querying for value"); java.util.Set valueResult = buildResultSet (clientIdLike, con, VALUE, this.valueType, this.value); if (valueResult == null) return result; // nothing found log.debug("querying for key"); java.util.Set keyResult = buildResultSet (clientIdLike, con, KEY, this.keyType, this.key); if (keyResult == null) return result; // nothing found java.util.Iterator it = keyResult.iterator(); while (it.hasNext()) { String sid = (String)it.next(); if (valueResult.contains(sid)) { String[] ss = splitSid(sid); //log.debug("docid : "+ss[0]); //log.debug("clientid : "+ss[2]); result.add(Long.parseLong(ss[0]), ss[2]); // adding the docId } } return result; } private String[] splitSid (String sid) { String[] result = new String[3]; int i = sid.indexOf("|"); result[0] = sid.substring(0, i); sid = sid.substring(i+1); i = sid.indexOf("|"); result[1] = sid.substring(0, i); sid = sid.substring(i+1); result[2] = sid; /* log.debug ("\nsid[0] : "+result[0]+ "\nsid[1] : "+result[1]+ "\nsid[2] : "+result[2]); */ return result; } // // METHODS protected java.util.Set buildResultSet (String clientIdLike, java.sql.Connection con, String keyOrValue, String type, String value) throws QueryException { // // FIRST STAGE StringBuffer sb = new StringBuffer(); sb.append("SELECT DISTINCT "); sb.append(DOCUMENT_ID); sb.append(", "); sb.append(CLIENT_ID); sb.append(", "); sb.append(PARENT_ID); sb.append(" FROM "); sb.append(ELEMENTS); sb.append(", "); sb.append(DOCUMENTS); sb.append(" WHERE "); sb.append(ELEMENTS); sb.append("."); sb.append(DOCUMENT_ID); sb.append(" = "); sb.append(DOCUMENTS); sb.append("."); sb.append("id"); if (type != null) { sb.append(" AND "); sb.append(ELTNAME); sb.append(" = '"); sb.append(type); sb.append("'"); } sb.append(" AND "); sb.append(ELTVALUE); if (keyOrValue.equals("key")) sb.append(" LIKE "); else sb.append(getValueOperator()); sb.append(SqlUtils.prepareString(value)); if (clientIdLike != null) { sb.append(" AND "); sb.append(CLIENT_ID); sb.append(" LIKE "); sb.append(SqlUtils.prepareString(clientIdLike)); } //sb.append(";"); String query = sb.toString(); log.debug(" ~~~ 1st stage query >\n"+query+"\n<"); // // execute 1st stage query java.util.Map results = new java.util.HashMap(); java.sql.Statement st = null; java.sql.ResultSet rs = null; try { st = con.createStatement(); rs = st.executeQuery(query); while (rs.next()) { Long docId = new Long(rs.getLong(1)); String clientId = rs.getString(2); Long parentId = new Long(rs.getLong(3)); Object[] os = new Object[3]; os[0] = docId; os[1] = clientId; //log.debug(" ~~~~ putting "+docId+" --> "+parentId); results.put(parentId, os); } } catch (final java.sql.SQLException se) { throw new QueryException ("1st stage of query failed", se); } finally { try { rs.close(); st.close(); } catch (Exception e) { // ignore } } // // SECOND STAGE if (results.size() < 1) return null; sb = new StringBuffer(); sb.append("SELECT "); sb.append(ID); sb.append(", "); sb.append(PARENT_ID); sb.append(" FROM "); sb.append(ELEMENTS); sb.append(" WHERE "); sb.append(ELTNAME); sb.append(" = '"); sb.append(keyOrValue); sb.append("' AND "); sb.append("( "); java.util.Iterator it = results.keySet().iterator(); while (it.hasNext()) { sb.append(ID); sb.append(" = "); sb.append(it.next()); if (it.hasNext()) sb.append(" OR "); } sb.append(" )"); //sb.append(";"); query = sb.toString(); log.debug(" ~~~ 2nd stage query >\n"+query+"\n<"); // // execute 2nd stage query try { st = con.createStatement(); rs = st.executeQuery(query); while (rs.next()) { Long id = new Long(rs.getLong(1)); Long parentId = new Long(rs.getLong(2)); Object[] os = (Object[])results.get(id); os[2] = parentId; } } catch (final java.sql.SQLException se) { throw new QueryException ("2nd stage of query failed", se); } finally { try { rs.close(); st.close(); } catch (Exception e) { // ignore } } // // build result set java.util.Set result = new java.util.HashSet(results.size()); it = results.values().iterator(); while (it.hasNext()) { Object[] os = (Object[])it.next(); if (os[2] == null) continue; result.add (os[0].toString() + "|" + os[2].toString() + "|" + os[1].toString()); } return result; } // // STATIC METHODS /* public static QueryItem decode (org.jdom.Element elt) { String key = elt.getAttributeValue(XML_ATT_KEY); String value = elt.getAttributeValue(XML_ATT_VALUE); String keyType = elt.getAttributeValue(XML_ATT_KEY_TYPE); String valueType = elt.getAttributeValue(XML_ATT_VALUE_TYPE); String valueOperator = elt.getAttributeValue(XML_ATT_VALUE_OPERATOR); OwfeQueryItem oqi = new OwfeQueryItem(keyType, key, valueType, value); oqi.setValueOperator(decodeValueOperator(valueOperator)); return oqi; } */}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -