Project

General

Profile

« Previous | Next » 

Revision 372

Added by berkley almost 24 years ago

Made changes to createSQuery to allow for multiple parameters of the same name. Also changed the param list to include only "Hashtable params" without a "String doctype" since the doctype is already contained in the params.

View differences:

src/edu/ucsb/nceas/metacat/DBQuery.java
176 176
   * structured query engine
177 177
   *
178 178
   * @param params The list of parameters that  should be included in the query
179
   * @param doctype the type of documents to include in the result set -- use
180
   *        "any" or "ANY" for unfiltered result sets
181 179
   */
182
  public static String createSQuery(Hashtable params, String doctype)
180
  public static String createSQuery(Hashtable params)
183 181
  { 
184 182
    StringBuffer query = new StringBuffer();
185 183
    Enumeration elements;
186 184
    Enumeration keys;
185
    String doctype = null;
186
    String casesensitive = null;
187
    String searchmode = null;
187 188
    Object nextkey;
188 189
    Object nextelement;
189 190
    //add the xml headers
......
210 211
    {
211 212
    	query.append("unspecified</querytitle>");
212 213
    }
214
    
215
    if(params.containsKey("doctype"))
216
    {
217
      doctype = ((String[])params.get("doctype"))[0]; 
218
    }
219
    else
220
    {
221
      doctype = "ANY";  
222
    }
223
    
213 224
    //if you don't limit the query by doctype, then it just creates
214 225
    //an empty returndoctype tag.
215 226
    if (!doctype.equals("any") && 
......
235 246
      query.append("<querygroup operator=\"UNION\">"); 
236 247
    }
237 248
    		
249
    if(params.containsKey("casesensitive"))
250
    {
251
      casesensitive = ((String[])params.get("casesensitive"))[0]; 
252
    }
253
    else
254
    {
255
      casesensitive = "false"; 
256
    }
257
    
258
    if(params.containsKey("searchmode"))
259
    {
260
      searchmode = ((String[])params.get("searchmode"))[0]; 
261
    }
262
    else
263
    {
264
      searchmode = "contains"; 
265
    }
238 266
    		
239 267
    //anyfield is a special case because it does a 
240 268
    //free text search.  It does not have a <pathexpr>
......
242 270
    //query.  This is useful if the INTERSECT operator is used.
243 271
    if(params.containsKey("anyfield"))
244 272
    {
245
    	 if(!((String[])params.get("anyfield"))[0].equals(""))
273
       String[] anyfield = ((String[])params.get("anyfield"));
274
       //allow for more than one value for anyfield
275
       for(int i=0; i<anyfield.length; i++)
246 276
       {
247
         query.append("<queryterm casesensitive=\"false\" " +
248
    			            "searchmode=\"contains\"><value>" +
249
    			            ((String[])params.get("anyfield"))[0] +
250
    			            "</value></queryterm>");
277
         if(!anyfield[i].equals(""))
278
         {
279
           query.append("<queryterm casesensitive=\"" + casesensitive + 
280
                        "\" " + "searchmode=\"" + searchmode + "\"><value>" +
281
    			              anyfield[i] +
282
    			              "</value></queryterm>"); 
283
         }
251 284
       }
252 285
    }
253 286
    		
......
260 293
    {
261 294
      nextkey = keys.nextElement();
262 295
    	nextelement = elements.nextElement();
263
    		
296

  
264 297
    	//make sure we aren't querying for any of these
265 298
    	//parameters since the are already in the query
266 299
      //in one form or another.
......
268 301
    		 !nextkey.toString().equals("action")  &&
269 302
    		 !nextkey.toString().equals("qformat") && 
270 303
    		 !nextkey.toString().equals("anyfield") &&
271
    		 !nextkey.toString().equals("operator") &&
272
    		 !(((String[])nextelement)[0].equals("")) )
304
    		 !nextkey.toString().equals("operator") )
273 305
    	{
274
        query.append("<queryterm casesensitive=\"false\" " + 
275
    				         "searchmode=\"contains\">" +
276
    		             "<value>" +
277
                     //add the query value
278
    		             ((String[])nextelement)[0] +
279
    		             "</value><pathexpr>" +
280
    		             //add the path to query by 
281
                     nextkey.toString() + 
282
                     "</pathexpr></queryterm>");
306
        //allow for more than value per field name
307
        for(int i=0; i<((String[])nextelement).length; i++)
308
        {
309
          if(!((String[])nextelement)[i].equals(""))
310
          {
311
            query.append("<queryterm casesensitive=\"" + casesensitive +"\" " + 
312
    				             "searchmode=\"" + searchmode + "\">" +
313
    		                 "<value>" +
314
                         //add the query value
315
    		                 ((String[])nextelement)[i] +
316
    		                 "</value><pathexpr>" +
317
    		                 //add the path to query by 
318
                         nextkey.toString() + 
319
                         "</pathexpr></queryterm>");
320
          }
321
        }
283 322
    	}
284 323
    }
285 324
    query.append("</querygroup></pathquery>");
......
340 379

  
341 380
/**
342 381
 * '$Log$
382
 * 'Revision 1.13  2000/08/14 21:26:12  berkley
383
 * 'Added createSQuery() to handle structured queries of an arbitrary number of parameters.  Also modified createQuery() to handle a null query in a graceful manner.
384
 * '
343 385
 * 'Revision 1.12  2000/08/14 20:53:33  jones
344 386
 * 'Added "release" keyword to all metacat source files so that the release
345 387
 * 'number will be evident in software distributions.

Also available in: Unified diff