Project

General

Profile

« Previous | Next » 

Revision 350

Added by berkley over 24 years ago

Added createSQuery() to handle structured queries of an arbitrary number of parameters. Also modified createQuery() to handle a null query in a graceful manner.

View differences:

src/edu/ucsb/nceas/metacat/DBQuery.java
180 180
   *        "any" or "ANY" for unfiltered result sets
181 181
   */
182 182
  public static String createSQuery(Hashtable params, String doctype)
183
  {
184
    StringBuffer query=null;
183
  { 
184
    StringBuffer query = new StringBuffer();
185 185
    Enumeration elements;
186 186
    Enumeration keys;
187 187
    Object nextkey;
188 188
    Object nextelement;
189
    query.append("<?xml version=\"1.0\"?>");
190

  
189
    //add the xml headers
190
    query.append("<?xml version=\"1.0\"?>\n");
191 191
    query.append("<pathquery version=\"1.0\"><meta_file_id>");
192
    
192 193
    if(params.containsKey("meta_file_id"))
193 194
    {
194 195
      query.append( ((String[])params.get("meta_file_id"))[0]);
......
198 199
    {
199 200
      query.append("unspecified</meta_file_id>");
200 201
    }
202
    
201 203
    query.append("<querytitle>");
202 204
    if(params.containsKey("querytitle"))
203 205
    {
......
208 210
    {
209 211
    	query.append("unspecified</querytitle>");
210 212
    }
213
    //if you don't limit the query by doctype, then it just creates
214
    //an empty returndoctype tag.
211 215
    if (!doctype.equals("any") && 
212 216
        !doctype.equals("ANY") &&
213 217
        !doctype.equals("") ) 
......
215 219
       query.append("<returndoctype>");
216 220
       query.append(doctype).append("</returndoctype>");
217 221
    }
218
    query.append("<querygroup operator=\"" + 
222
    else
223
    { 
224
      query.append("<returndoctype></returndoctype>");
225
    }
226
    
227
    //allows the dynamic switching of boolean operators
228
    if(params.containsKey("operator"))
229
    {
230
      query.append("<querygroup operator=\"" + 
219 231
    		        ((String[])params.get("operator"))[0] + "\">");
232
    }
233
    else
234
    { //the default operator is UNION
235
      query.append("<querygroup operator=\"UNION\">"); 
236
    }
220 237
    		
221 238
    		
222 239
    //anyfield is a special case because it does a 
223 240
    //free text search.  It does not have a <pathexpr>
224
    //tag.
225
    if(params.containsKey("anyfield") && 
226
    	 !((String[])params.get("anyfield"))[0].equals(""))
241
    //tag.  This allows for a free text search within the structured
242
    //query.  This is useful if the INTERSECT operator is used.
243
    if(params.containsKey("anyfield"))
227 244
    {
228
    	query.append("<queryterm casesensitive=\"false\" " +
229
    			         "searchmode=\"contains\"><value>" +
230
    			         ((String[])params.get("anyfield"))[0] +
231
    			         "</value></queryterm>");
245
    	 if(!((String[])params.get("anyfield"))[0].equals(""))
246
       {
247
         query.append("<queryterm casesensitive=\"false\" " +
248
    			            "searchmode=\"contains\"><value>" +
249
    			            ((String[])params.get("anyfield"))[0] +
250
    			            "</value></queryterm>");
251
       }
232 252
    }
233 253
    		
234 254
    //this while loop finds the rest of the parameters
......
254 274
        query.append("<queryterm casesensitive=\"false\" " + 
255 275
    				         "searchmode=\"contains\">" +
256 276
    		             "<value>" +
257
        //add the query value
277
                     //add the query value
258 278
    		             ((String[])nextelement)[0] +
259 279
    		             "</value><pathexpr>" +
260
    		//add the path to query by 
280
    		             //add the path to query by 
261 281
                     nextkey.toString() + 
262 282
                     "</pathexpr></queryterm>");
263 283
    	}
264 284
    }
265 285
    query.append("</querygroup></pathquery>");
286
    //append on the end of the xml and return the result as a string
266 287
    return query.toString();
267 288
  }
268 289
  
......
288 309
     }
289 310

  
290 311
     xmlquery.append("<querygroup operator=\"UNION\">");
291
     xmlquery.append("<queryterm casesensitive=\"false\" ");
292
     xmlquery.append("searchmode=\"contains\">");
293
     xmlquery.append("<value>").append(value).append("</value>");
294
     xmlquery.append("</queryterm>");
312
     //chad added - 8/14
313
     //the if statement allows a query to gracefully handle a null 
314
     //query.  Without this if a nullpointerException is thrown.
315
     if(!value.equals(""))
316
     {
317
       xmlquery.append("<queryterm casesensitive=\"false\" ");
318
       xmlquery.append("searchmode=\"contains\">");
319
       xmlquery.append("<value>").append(value).append("</value>");
320
       xmlquery.append("</queryterm>");
321
     }
295 322
     xmlquery.append("</querygroup>");
296 323
     xmlquery.append("</pathquery>");
297 324

  
......
313 340

  
314 341
/**
315 342
 * '$Log$
343
 * 'Revision 1.12  2000/08/14 20:53:33  jones
344
 * 'Added "release" keyword to all metacat source files so that the release
345
 * 'number will be evident in software distributions.
346
 * '
316 347
 * 'Revision 1.11  2000/08/11 18:26:07  berkley
317 348
 * 'added createSQuery
318 349
 * '

Also available in: Unified diff