Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that represents a structured query, and can be
4
 *             constructed from an XML serialization conforming to
5
 *             pathquery.dtd. The printSQL() method can be used to print
6
 *             a SQL serialization of the query.
7
 *  Copyright: 2000 Regents of the University of California and the
8
 *             National Center for Ecological Analysis and Synthesis
9
 *    Authors: Matt Jones
10
 *
11
 *   '$Author: tao $'
12
 *     '$Date: 2007-09-12 15:37:33 -0700 (Wed, 12 Sep 2007) $'
13
 * '$Revision: 3430 $'
14
 *
15
 * This program is free software; you can redistribute it and/or modify
16
 * it under the terms of the GNU General Public License as published by
17
 * the Free Software Foundation; either version 2 of the License, or
18
 * (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU General Public License
26
 * along with this program; if not, write to the Free Software
27
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28
 */
29

    
30
package edu.ucsb.nceas.metacat;
31

    
32
import edu.ucsb.nceas.dbadapter.*;
33

    
34
import java.io.*;
35
import java.util.Hashtable;
36
import java.util.Stack;
37
import java.util.Vector;
38
import java.util.Enumeration;
39

    
40
import org.apache.log4j.Logger;
41

    
42
 /** a utility class that represents a group of terms in a query */
43
public  class QueryGroup {
44
    private String operator = null;  // indicates how query terms are combined
45
    private Vector children = null;  // the list of query terms and groups
46
    private int countPercentageSearchItem = 0;
47
    private Vector queryTermsWithSameValue = null;//this two dimension vectors.
48
                                                  //will hold query terms which has same search value.
49
    private Vector queryTermsInPathIndex = null; //this vector holds query terms without same value
50
                                                                                 // and search path is in path index.
51
    private Vector queryTerms = null;//this vector only holds query terms without same search value.
52
                                                             // and search path is NOT in path index.
53
    private Vector queryGroupsChildren = null;
54
    private static Logger logMetacat = Logger.getLogger(QueryGroup.class);
55
    private static String UNION = "UNION";
56

    
57
    /**
58
     * construct a new QueryGroup
59
     *
60
     * @param operator the boolean conector used to connect query terms
61
     *                    in this query group
62
     */
63
    public QueryGroup(String operator) {
64
      this.operator = operator;
65
      children = new Vector();
66
      queryTermsWithSameValue = new Vector();
67
      queryTermsInPathIndex = new Vector(); 
68
      queryTerms = new Vector();
69
      queryGroupsChildren = new Vector();
70
    }
71

    
72
    /**
73
     * Add a child QueryGroup to this QueryGroup
74
     *
75
     * @param qgroup the query group to be added to the list of terms
76
     */
77
    public void addChild(QueryGroup qgroup) {
78
      children.add((Object)qgroup);
79
      queryGroupsChildren.add(qgroup);
80
    }
81

    
82
    /**
83
     * Add a child QueryTerm to this QueryGroup
84
     *
85
     * @param qterm the query term to be added to the list of terms
86
     */
87
    public void addChild(QueryTerm qterm) {
88
      children.add((Object)qterm);
89
      handleNewQueryTerms(qterm);
90
      
91
    }
92

    
93
    /*
94
     * Retrieve an Enumeration of query terms for this QueryGroup
95
     */
96
    private Enumeration getChildren() {
97
      return children.elements();
98
    }
99

    
100
    public int getPercentageSymbolCount()
101
    {
102
      return countPercentageSearchItem;
103
    }
104

    
105
    /**
106
     * create a SQL serialization of the query that this instance represents
107
     */
108
    public String printSQL(boolean useXMLIndex) {
109
      StringBuffer self = new StringBuffer();
110
      StringBuffer queryString = new StringBuffer();
111

    
112
      boolean first = true;
113
      
114
      if (!queryTermsWithSameValue.isEmpty() || !queryTermsInPathIndex.isEmpty())
115
      {
116
    	  String pathIndexQueryString = printSQLStringInPathIndex();
117
    	  queryString.append(pathIndexQueryString);
118
    	  if (queryString != null)
119
    	  {
120
    		  first = false;
121
    	  }
122
      }
123
      
124
      for (int i=0; i<queryGroupsChildren.size(); i++)
125
      {
126
      
127
        
128
            QueryGroup qg = (QueryGroup)queryGroupsChildren.elementAt(i);
129
        	String queryGroupSQL = qg.printSQL(useXMLIndex);
130
        	logMetacat.info("In QueryGroup.printSQL.. found a QueryGroup: " 
131
        			+ queryGroupSQL);       	
132
        	if (first) {
133
        		first = false;
134
        	} else {
135
        		if(!queryString.toString().equals("") && queryGroupSQL != null &&!queryGroupSQL.equals("")){
136
                    queryString.append(" " + operator + " ");
137
        		}
138
        	}
139
   		  	queryString.append(queryGroupSQL);
140
   		  	
141
   		  	// count percerntage number
142
   		  	int count = qg.getPercentageSymbolCount();
143
   		  	countPercentageSearchItem = countPercentageSearchItem + count;
144
      }
145
      
146
      for (int i=0; i<queryTerms.size(); i++)
147
      {
148
           QueryTerm qt = (QueryTerm)queryTerms.elementAt(i);
149
           String termQueryString = qt.printSQL(useXMLIndex);
150
       	   logMetacat.info("In QueryGroup.printSQL.. found a QueryGroup: " 
151
        			+ termQueryString);
152
           if(!(qt.getSearchMode().equals("contains") && qt.getValue().equals("%"))){
153
        	   if (first) {
154
                   first = false;
155
               } else {
156
                   if(!queryString.toString().equals("")){
157
                       queryString.append(" " + operator + " ");
158
                   }
159
               }
160
               queryString.append(termQueryString);
161
           
162
           // count percerntage number
163
           int count = qt.getPercentageSymbolCount();
164
           countPercentageSearchItem = countPercentageSearchItem + count;
165
        } 
166
      }
167

    
168
      if(!queryString.toString().equals("")){
169
          self.append("(");
170
          self.append(queryString.toString());
171
          self.append(")");
172
      }
173
      
174
      logMetacat.info("In QueryGroup.printSQL.. final query returned is: " 
175
			+ self.toString());
176
      return self.toString();
177
    }
178
    
179
    
180
    
181
    
182
    /*
183
     * If every query term in a queryGroup share a search value and search path
184
     * is in xml_path_index, we should use a new query to replace the original query term query in order to
185
     * improve performance. Also if even the term doesn't share any value with other term
186
     * we still use "OR" to replace UNION action (we only handle union operator in the query group).
187
     * 
188
     */
189
    private String printSQLStringInPathIndex()
190
    {
191
    	String sql ="";
192
    	String value ="";
193
    	StringBuffer sqlBuff = new StringBuffer();
194
    	Vector pathVector = new Vector();
195
    	int index =0;
196
    	if (queryTermsWithSameValue != null && queryTermsInPathIndex != null)
197
    	{
198
    		
199
    		sqlBuff.append("SELECT DISTINCT docid FROM xml_path_index WHERE ");
200
    		if (!queryTermsWithSameValue.isEmpty())
201
    		{
202
    			boolean firstVector = true;
203
	    		for (int j=0; j<queryTermsWithSameValue.size(); j++)
204
	    		{
205
	    	   		Vector queryTermVector = (Vector)queryTermsWithSameValue.elementAt(j);
206
		    		QueryTerm term1 = (QueryTerm)queryTermVector.elementAt(0);
207
		        	value = term1.getValue();
208
		        	boolean first = true;
209
		        	if (firstVector)
210
		        	{
211
					  firstVector = false;
212
		        	}
213
		        	else
214
		        	{
215
		        		sqlBuff.append(" "+"OR"+" ");
216
		        	}
217
		        	sqlBuff.append(" (UPPER(nodedata) LIKE '%");
218
		        	if (value != null)
219
		        	{
220
		        	    sqlBuff.append(value.toUpperCase());
221
		        	}
222
		        	else
223
		        	{
224
		        		sqlBuff.append(value);
225
		        	}
226
					sqlBuff.append("%' AND path IN (");
227
		    		//gets every path in query term object
228
		    		for (int i=0; i<queryTermVector.size(); i++)
229
		    		{
230
		    			QueryTerm term = (QueryTerm)queryTermVector.elementAt(i);
231
		    			value = term.getValue();
232
		    			String path = term.getPathExpression();
233
		    			if (path != null && !path.equals(""))
234
		    			{
235
		    				if (first)
236
		    				{
237
		    					first = false;
238
		    					sqlBuff.append("'");
239
		    					sqlBuff.append(path);
240
		    					sqlBuff.append("'");
241
		    					
242
		    				}
243
		    				else
244
		    				{
245
		    					sqlBuff.append(",'");
246
		    					sqlBuff.append(path);
247
		    					sqlBuff.append("'");
248
		    				}
249
		    				index++;
250
		     				if (value != null && (value.equals("%") || value.equals("%%%")))
251
		                    {
252
		    				  countPercentageSearchItem++;
253
		                    }
254
	    			     }
255
	    		    }
256
	    		    sqlBuff.append("))");
257
	    	
258
	    	    }
259
	    	}
260
    		if (!queryTermsInPathIndex.isEmpty())
261
    		{
262
    			for (int j=0; j<queryTermsInPathIndex.size(); j++)
263
    			{
264
    				QueryTerm term = (QueryTerm)queryTermsInPathIndex.elementAt(j);
265
    				if (term != null)
266
    				{
267
	    				term.setInUnionGroup(true);
268
		    			 if (index > 0)
269
		    			 {
270
		    				 sqlBuff.append(" "+"OR"+" ");
271
		    			 }
272
		    			 sqlBuff.append("(");
273
	    				 sqlBuff.append(term.printSQL(true));
274
	    				 sqlBuff.append(")");
275
	    				 index++;
276
	    			}
277
    			}
278
    		}
279
    	}
280
    	if (index >0)
281
    	{
282
    		sql = sqlBuff.toString();
283
    	}
284
    	return sql;
285
    }
286

    
287
    /**
288
     * create a String description of the query that this instance represents.
289
     * This should become a way to get the XML serialization of the query.
290
     */
291
    public String toString() {
292
      StringBuffer self = new StringBuffer();
293

    
294
      self.append("  (Query group operator=" + operator + "\n");
295
      Enumeration en= getChildren();
296
      while (en.hasMoreElements()) {
297
        Object qobject = en.nextElement();
298
        self.append(qobject);
299
      }
300
      self.append("  )\n");
301
      return self.toString();
302
    }
303
    
304
    /*
305
     * When a new QueryTerm come, first we need to compare it to
306
     * the queryTerm vector, which contains queryTerm that doesn't
307
     * have same search value to any other queryTerm. Here is algorithm.
308
     * 1) If new QueryTerm find a QueryTerm in queryTerms which has same search value,
309
     *    them create a new vector which contain both QueryTerms and add the new vector
310
     *    to two-dimention vector queryTermsWithSameValue, and remove the QueryTerm which
311
     *    was in queryTerm.
312
     * 2) If new QueryTerm couldn't find a QueryTerm in queryTerms which has same search value,
313
     *    then search queryTermsWithSameValue, to see if this vector already has the search value.
314
     *    2.1) if has the search value, add the new QueryTerm to the queryTermsWithSameValue.
315
     *    2.2) if hasn't, add the new QueryTerm to queryTerms vector.
316
     */
317
    private void handleNewQueryTerms(QueryTerm newTerm)
318
    {
319
    	// currently we only handle UNION group
320
    	if (newTerm != null )
321
    	{
322
    		//System.out.println("new term is not null branch in handle new query term");
323
    		//we only handle union operator now.
324
    		if (operator != null && operator.equalsIgnoreCase(UNION) && 
325
    				MetaCatUtil.pathsForIndexing.contains(newTerm.getPathExpression()))
326
    	    {
327
    			//System.out.println("in only union branch in handle new query term");
328
	    		for (int i=0; i<queryTermsInPathIndex.size(); i++)
329
	    		{
330
	    			QueryTerm term = (QueryTerm)queryTermsInPathIndex.elementAt(i);
331
	    			if (term != null && term.hasSameSearchValue(newTerm))
332
	    			{
333
	    				//System.out.println("1Move a query term and add a new query term into search value in handle new query term");
334
	    				// find a target which has same search value
335
	    				Vector newSameValueVector = new Vector();
336
	    				newSameValueVector.add(term);
337
	    				newSameValueVector.addElement(newTerm);
338
	    				queryTermsWithSameValue.add(newSameValueVector);
339
	    				queryTermsInPathIndex.remove(i);
340
	    				return;
341
	    			}
342
	    		}
343
	    		// no same search value was found in queryTerms.
344
	    		// then we need search queryTermsWithSameValue
345
	    		for (int i=0; i<queryTermsWithSameValue.size(); i++)
346
	    		{
347
	    			Vector sameValueVec = (Vector)queryTermsWithSameValue.elementAt(i);
348
	    			// we only compare the first query term
349
	    			QueryTerm term = (QueryTerm)sameValueVec.elementAt(0);
350
	    			if (term != null && term.hasSameSearchValue(newTerm))
351
	    			{
352
	    				//System.out.println("2add a new query term into search value in handle new query term");
353
	    				sameValueVec.add(newTerm);
354
	    				return;
355
	    			}
356
	    		}
357
	    		//nothing found, but the search path is still in xml_path_index,
358
	    		// save it into queryTermsInPathIndex vector
359
	    		queryTermsInPathIndex.add(newTerm);
360
	    		return;
361
    	    }
362
    		
363
    		// add this newTerm to queryTerms since we couldn't find it in xml_path_index
364
    		queryTerms.add(newTerm);
365
    	}
366
    	
367
    }
368
  }
(52-52/66)