Project

General

Profile

« Previous | Next » 

Revision 3237

Added by Jing Tao about 17 years ago

Add method to handle union operator.

View differences:

src/edu/ucsb/nceas/metacat/QueryGroup.java
49 49
    private Vector queryTerms = null;//this vector only holds query terms without same search value.
50 50
    private Vector queryGroupsChildren = null;
51 51
    private static Logger logMetacat = Logger.getLogger(QueryGroup.class);
52
    private static String UNION = "UNION";
52 53

  
53 54
    /**
54 55
     * construct a new QueryGroup
......
70 71
     * @param qgroup the query group to be added to the list of terms
71 72
     */
72 73
    public void addChild(QueryGroup qgroup) {
73
      //children.add((Object)qgroup);
74
      children.add((Object)qgroup);
74 75
      queryGroupsChildren.add(qgroup);
75 76
    }
76 77

  
......
80 81
     * @param qterm the query term to be added to the list of terms
81 82
     */
82 83
    public void addChild(QueryTerm qterm) {
83
      // children.add((Object)qterm);
84
      children.add((Object)qterm);
84 85
      handleNewQueryTerms(qterm);
85 86
      
86 87
    }
87 88

  
88
    /**
89
    /*
89 90
     * Retrieve an Enumeration of query terms for this QueryGroup
90 91
     */
91
    public Enumeration getChildren() {
92
    private Enumeration getChildren() {
92 93
      return children.elements();
93 94
    }
94 95

  
......
106 107

  
107 108
      boolean first = true;
108 109
      
109
      Enumeration en= getChildren();
110
      while (en.hasMoreElements()) {
111
        Object qobject = en.nextElement();
112
        if (qobject instanceof QueryGroup) {
113
            QueryGroup qg = (QueryGroup)qobject;
110
      if (!queryTermsWithSameValue.isEmpty())
111
      {
112
    	  printSQLStringWithSameSearchValue();
113
      }
114
      
115
      for (int i=0; i<queryGroupsChildren.size(); i++)
116
      {
117
      
118
        
119
            QueryGroup qg = (QueryGroup)queryGroupsChildren.elementAt(i);
114 120
        	String queryGroupSQL = qg.printSQL(useXMLIndex);
115 121
        	logMetacat.info("In QueryGroup.printSQL.. found a QueryGroup: " 
116
        			+ queryGroupSQL);
117
        	
122
        			+ queryGroupSQL);       	
118 123
        	if (first) {
119 124
        		first = false;
120 125
        	} else {
......
127 132
   		  	// count percerntage number
128 133
   		  	int count = qg.getPercentageSymbolCount();
129 134
   		  	countPercentageSearchItem = countPercentageSearchItem + count;
130
        } else if (qobject instanceof QueryTerm) {
131
           QueryTerm qt = (QueryTerm)qobject;
135
      }
136
      
137
      for (int i=0; i<queryTerms.size(); i++)
138
      {
139
           QueryTerm qt = (QueryTerm)queryTerms.elementAt(i);
132 140
           String termQueryString = qt.printSQL(useXMLIndex);
133 141
       	   logMetacat.info("In QueryGroup.printSQL.. found a QueryGroup: " 
134 142
        			+ termQueryString);
......
141 149
                   }
142 150
               }
143 151
               queryString.append(termQueryString);
144
           }
152
           
145 153
           // count percerntage number
146 154
           int count = qt.getPercentageSymbolCount();
147 155
           countPercentageSearchItem = countPercentageSearchItem + count;
148
        } else {
149
          System.err.println("qobject wrong type: fatal error");
150
        }
156
        } 
151 157
      }
152 158

  
153 159
      if(!queryString.toString().equals("")){
......
162 168
    }
163 169
    
164 170
    
171
    
172
    
165 173
    /*
166 174
     * If every query term in a queryGroup share a search value, we should
167 175
     * use a new query to replace the original query term query in order to
......
177 185
    	
178 186
    	if (queryTermsWithSameValue != null)
179 187
    	{
180
    		QueryTerm term1 = (QueryTerm)queryTermsWithSameValue.elementAt(0);
181
        	value = term1.getValue();
182
        	boolean first = true;
183
			sqlBuff.append("SELECT DISTINCT docid FROM xml_path_index WHERE UPPER(nodedata) LIKE '%");
184
			sqlBuff.append(value);
185
			sqlBuff.append("%' AND path IN (");
186
    		//gets every path in query term object
187
    		for (int i=0; i<queryTermsWithSameValue.size(); i++)
188
    		boolean firstVector = true;
189
    		for (int j=0; j<queryTermsWithSameValue.size(); j++)
188 190
    		{
189
    			QueryTerm term = (QueryTerm)queryTermsWithSameValue.elementAt(i);
190
    			value = term.getValue();
191
    			String path = term.getPathExpression();
192
    			if (path != null && !path.equals(""))
193
    			{
194
    				if (first)
195
    				{
196
    					first = false;
197
    					sqlBuff.append("'");
198
    					sqlBuff.append(path);
199
    					sqlBuff.append("'");
200
    					
201
    				}
202
    				else
203
    				{
204
    					sqlBuff.append(",'");
205
    					sqlBuff.append(path);
206
    					sqlBuff.append("'");
207
    				}
208
    				index++;
209
    			}
210
    		}
211
    		sqlBuff.append(")");
191
    			
192
	    		Vector queryTermVector = (Vector)queryTermsWithSameValue.elementAt(j);
193
	    		QueryTerm term1 = (QueryTerm)queryTermVector.elementAt(0);
194
	        	value = term1.getValue();
195
	        	boolean first = true;
196
	        	if (firstVector)
197
	        	{
198
				  firstVector = false;
199
	        	}
200
	        	else
201
	        	{
202
	        		sqlBuff.append(" "+operator+" ");
203
	        	}
204
	        	sqlBuff.append("SELECT DISTINCT docid FROM xml_path_index WHERE UPPER(nodedata) LIKE '%");
205
	        	sqlBuff.append(value);
206
				sqlBuff.append("%' AND path IN (");
207
	    		//gets every path in query term object
208
	    		for (int i=0; i<queryTermVector.size(); i++)
209
	    		{
210
	    			QueryTerm term = (QueryTerm)queryTermVector.elementAt(i);
211
	    			value = term.getValue();
212
	    			String path = term.getPathExpression();
213
	    			if (path != null && !path.equals(""))
214
	    			{
215
	    				if (first)
216
	    				{
217
	    					first = false;
218
	    					sqlBuff.append("'");
219
	    					sqlBuff.append(path);
220
	    					sqlBuff.append("'");
221
	    					
222
	    				}
223
	    				else
224
	    				{
225
	    					sqlBuff.append(",'");
226
	    					sqlBuff.append(path);
227
	    					sqlBuff.append("'");
228
	    				}
229
	    				index++;
230
	     				if (value != null && (value.equals("%") || value.equals("%%%")))
231
	                    {
232
	    				  countPercentageSearchItem++;
233
	                    }
234
    			     }
235
    		    }
236
    		    sqlBuff.append(")");
212 237
    	
238
    	    }
213 239
    	}
214 240
    	if (index >0)
215 241
    	{
......
250 276
     */
251 277
    private void handleNewQueryTerms(QueryTerm newTerm)
252 278
    {
253
    	if (newTerm != null)
279
    	// currently we only handle UNION group
280
    	if (newTerm != null )
254 281
    	{
255
    		for (int i=0; i<queryTerms.size(); i++)
256
    		{
257
    			QueryTerm term = (QueryTerm)queryTerms.elementAt(i);
258
    			if (term != null && term.hasSameSearchValue(newTerm))
259
    			{
260
    				// find a target which has same search value
261
    				Vector newSameValueVector = new Vector();
262
    				newSameValueVector.add(term);
263
    				newSameValueVector.addElement(newTerm);
264
    				queryTermsWithSameValue.add(newSameValueVector);
265
    				queryTerms.remove(i);
266
    				return;
267
    			}
268
    		}
269
    		// no same search value was found in queryTerms.
270
    		// then we need search queryTermsWithSameValue
271
    		for (int i=0; i<queryTermsWithSameValue.size(); i++)
272
    		{
273
    			Vector sameValueVec = (Vector)queryTermsWithSameValue.elementAt(i);
274
    			// we only compare the first query term
275
    			QueryTerm term = (QueryTerm)sameValueVec.elementAt(0);
276
    			if (term != null && term.hasSameSearchValue(newTerm))
277
    			{
278
    				sameValueVec.add(newTerm);
279
    				return;
280
    			}
281
    		}
282
    		//we only handle union operator now.
283
    		if (operator != null && operator.equalsIgnoreCase(UNION))
284
    	    {
285
	    		for (int i=0; i<queryTerms.size(); i++)
286
	    		{
287
	    			QueryTerm term = (QueryTerm)queryTerms.elementAt(i);
288
	    			if (term != null && term.hasSameSearchValue(newTerm))
289
	    			{
290
	    				// find a target which has same search value
291
	    				Vector newSameValueVector = new Vector();
292
	    				newSameValueVector.add(term);
293
	    				newSameValueVector.addElement(newTerm);
294
	    				queryTermsWithSameValue.add(newSameValueVector);
295
	    				queryTerms.remove(i);
296
	    				return;
297
	    			}
298
	    		}
299
	    		// no same search value was found in queryTerms.
300
	    		// then we need search queryTermsWithSameValue
301
	    		for (int i=0; i<queryTermsWithSameValue.size(); i++)
302
	    		{
303
	    			Vector sameValueVec = (Vector)queryTermsWithSameValue.elementAt(i);
304
	    			// we only compare the first query term
305
	    			QueryTerm term = (QueryTerm)sameValueVec.elementAt(0);
306
	    			if (term != null && term.hasSameSearchValue(newTerm))
307
	    			{
308
	    				sameValueVec.add(newTerm);
309
	    				return;
310
	    			}
311
	    		}
312
    	    }
282 313
    		// both queryTerms and queryTermsWithSameValue don't have the search value,
283 314
    		// add this newTerm to queryTerms
284 315
    		queryTerms.add(newTerm);

Also available in: Unified diff