Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that searches a relational DB for elements and 
4
 *             attributes that have free text matches a query string,
5
 *             or structured query matches to a path specified node in the 
6
 *             XML hierarchy.  It returns a result set consisting of the 
7
 *             document ID for each document that satisfies the query
8
 *  Copyright: 2000 Regents of the University of California and the
9
 *             National Center for Ecological Analysis and Synthesis
10
 *    Authors: Matt Jones
11
 *    Release: @release@
12
 *
13
 *   '$Author: bojilova $'
14
 *     '$Date: 2001-02-01 10:56:25 -0800 (Thu, 01 Feb 2001) $'
15
 * '$Revision: 692 $'
16
 *
17
 * This program is free software; you can redistribute it and/or modify
18
 * it under the terms of the GNU General Public License as published by
19
 * the Free Software Foundation; either version 2 of the License, or
20
 * (at your option) any later version.
21
 *
22
 * This program is distributed in the hope that it will be useful,
23
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25
 * GNU General Public License for more details.
26
 *
27
 * You should have received a copy of the GNU General Public License
28
 * along with this program; if not, write to the Free Software
29
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
30
 */
31

    
32
package edu.ucsb.nceas.metacat;
33

    
34
import java.io.*;
35
import java.util.Vector;
36
import java.net.URL;
37
import java.net.MalformedURLException;
38
import java.sql.*;
39
import java.util.Stack;
40
import java.util.Hashtable;
41
import java.util.Enumeration;
42

    
43
/** 
44
 * A Class that searches a relational DB for elements and 
45
 * attributes that have free text matches a query string,
46
 * or structured query matches to a path specified node in the 
47
 * XML hierarchy.  It returns a result set consisting of the 
48
 * document ID for each document that satisfies the query
49
 */
50
public class DBQuery {
51

    
52
  static final int ALL = 1;
53
  static final int WRITE = 2;
54
  static final int READ = 4;
55

    
56
  private Connection  conn = null;
57
  private String  parserName = null;
58
  private MetaCatUtil util = new MetaCatUtil();
59
  /**
60
   * the main routine used to test the DBQuery utility.
61
   * <p>
62
   * Usage: java DBQuery <xmlfile>
63
   *
64
   * @param xmlfile the filename of the xml file containing the query
65
   */
66
  static public void main(String[] args) {
67
     
68
     if (args.length < 1)
69
     {
70
        System.err.println("Wrong number of arguments!!!");
71
        System.err.println("USAGE: java DBQuery <xmlfile>");
72
        return;
73
     } else {
74
        try {
75
                    
76
          String xmlfile  = args[0];
77

    
78
          // Open a connection to the database
79
          MetaCatUtil   util = new MetaCatUtil();
80
          Connection dbconn = util.openDBConnection();
81

    
82
          // Execute the query
83
          DBQuery queryobj = new DBQuery(dbconn, util.getOption("saxparser"));
84
          FileReader xml = new FileReader(new File(xmlfile));
85
          Hashtable nodelist = null;
86
          nodelist = queryobj.findDocuments(xml, null, null);
87

    
88
          // Print the reulting document listing
89
          StringBuffer result = new StringBuffer();
90
          String document = null;
91
          String docid = null;
92
          result.append("<?xml version=\"1.0\"?>\n");
93
          result.append("<resultset>\n"); 
94
  // following line removed by Dan Higgins to avoid insertion of query XML inside returned XML doc
95
  //        result.append("  <query>" + xmlfile + "</query>\n");
96
          Enumeration doclist = nodelist.keys(); 
97
          while (doclist.hasMoreElements()) {
98
            docid = (String)doclist.nextElement();
99
            document = (String)nodelist.get(docid);
100
            result.append("  <document>\n    " + document + 
101
                          "\n  </document>\n");
102
          }
103
          result.append("</resultset>\n");
104

    
105
          System.out.println(result);
106

    
107
        } catch (Exception e) {
108
          System.err.println("Error in DBQuery.main");
109
          System.err.println(e.getMessage());
110
          e.printStackTrace(System.err);
111
        }
112
     }
113
  }
114
  
115
  /**
116
   * construct an instance of the DBQuery class 
117
   *
118
   * <p>Generally, one would call the findDocuments() routine after creating 
119
   * an instance to specify the search query</p>
120
   *
121
   * @param conn the JDBC connection that we use for the query
122
   * @param parserName the fully qualified name of a Java class implementing
123
   *                   the org.xml.sax.XMLReader interface
124
   */
125
  public DBQuery( Connection conn, String parserName ) 
126
                  throws IOException, 
127
                         SQLException, 
128
                         ClassNotFoundException {
129
    this.conn = conn;
130
    this.parserName = parserName;
131
  }
132
  
133
  public Hashtable findDocuments(Reader xmlquery, String user, String group)
134
  {
135
    return findDocuments(xmlquery, user, group, null);
136
  }
137
  
138
  /** 
139
   * routine to search the elements and attributes looking to match query
140
   *
141
   * @param xmlquery the xml serialization of the query (@see pathquery.dtd)
142
   * @param user the username of the user
143
   * @param group the group of the user
144
   * @param returndoc an array of document types to backtrack against.
145
   */
146
  public Hashtable findDocuments(Reader xmlquery, String user, String group,
147
                                 String[] returndoc)
148
  {
149
    //System.out.println("in finddocuments");
150
      Hashtable   docListResult = new Hashtable();
151
      PreparedStatement pstmt = null;
152
      String docid = null;
153
      String docname = null;
154
      String doctype = null;
155
// DOCTITLE attr cleared from the db
156
//      String doctitle = null;
157
      String createDate = null;
158
      String updateDate = null;
159
      String fieldname = null;
160
      String fielddata = null;
161
      String relation = null;
162
      Connection dbconn = null;
163
      int rev = 0;
164
      StringBuffer document = null; 
165
      Vector returndocVec = new Vector();
166
      
167
      if(returndoc != null)
168
      {//add the returndoc elements to a vector for easier manipulation
169
        for(int i=0; i<returndoc.length; i++)
170
        {
171
          returndocVec.add(new String((String)returndoc[i]));
172
        }
173
      }
174
      
175
      try {
176
        dbconn = util.openDBConnection();
177
        // Get the XML query and covert it into a SQL statment
178
        QuerySpecification qspec = new QuerySpecification(xmlquery, 
179
                                   parserName, 
180
                                   util.getOption("accNumSeparator"));
181
       // System.out.println(qspec.printSQL());
182
        pstmt = dbconn.prepareStatement( qspec.printSQL() );
183

    
184
        // Execute the SQL query using the JDBC connection
185
        pstmt.execute();
186
        ResultSet rs = pstmt.getResultSet();
187
        boolean tableHasRows = rs.next();
188
        while (tableHasRows) 
189
        {
190
          docid = rs.getString(1);
191
          if ( !hasPermission(dbconn, user, group, docid) ) {
192
            // Advance to the next record in the cursor
193
            tableHasRows = rs.next();
194
            continue;
195
          }
196
          docname = rs.getString(2);
197
          doctype = rs.getString(3);
198
// DOCTITLE attr cleared from the db
199
//          doctitle = rs.getString(4);
200
          createDate = rs.getString(4);
201
          updateDate = rs.getString(5);
202
          rev = rs.getInt(6);
203
          
204
          //System.out.println("vec.size = " + returndocVec.size());
205
          if(returndocVec.size() != 0 && !returndocVec.contains(doctype))
206
          { //there are returndocs to match (backtracking can now be performed). 
207
            //System.out.println("olddoctype: " + doctype);
208
            StringBuffer btBuf = new StringBuffer();
209
            btBuf.append("select object from xml_relation where ");
210
            btBuf.append("objdoctype in (");
211
            //build the doctype list for the backtracking sql statement
212
            for(int i=0; i<returndocVec.size(); i++)
213
            {
214
              btBuf.append("'").append((String)returndocVec.get(i)).append("'");
215
              if(i != (returndocVec.size() - 1))
216
              {
217
                btBuf.append(", ");
218
              } 
219
            }
220
            btBuf.append(") ");
221
            btBuf.append("and subject like '");
222
            //btBuf.append("metacat://").append(util.getOption("server"));
223
            //btBuf.append("?docid=").append(docid).append("'");
224
            btBuf.append("%docid=").append(docid).append("'");
225
            //System.out.println("sql: " + btBuf.toString());
226
            
227
            PreparedStatement npstmt = dbconn.prepareStatement(btBuf.toString());
228
            npstmt.execute();
229
            ResultSet btrs = npstmt.getResultSet();
230
            boolean hasBtRows = btrs.next();
231
            if(hasBtRows)
232
            { //there was a backtrackable document found
233
              DocumentImpl xmldoc = null;
234
              //System.out.println("document found is: " + btrs.getString(1));
235
              MetacatURL objURL = new MetacatURL(btrs.getString(1));
236
              try
237
              {
238
                xmldoc = new DocumentImpl(dbconn, objURL.getParam(0)[1]);
239
              }
240
              catch(Exception e)
241
              {
242
                System.out.println("Error getting document in " + 
243
                                   "DBQuery.findDocuments: " + e.getMessage());
244
              }
245
              
246
              docid   = xmldoc.getDocID();
247
              docname = xmldoc.getDocname();
248
              doctype = xmldoc.getDoctype();
249
// DOCTITLE attr cleared from the db
250
//              doctitle = xmldoc.getDocTitle();
251
              createDate = xmldoc.getCreateDate();
252
              updateDate = xmldoc.getUpdateDate();
253
              //System.out.println("docname: " + docname + " doctype: " + doctype + 
254
              //                   " doctitle: " + doctitle + " createdate: " +
255
              //                   createDate + " updatedate: " + updateDate);
256
            }
257
            npstmt.close();
258
            btrs.close();
259
          }
260
          
261
          document = new StringBuffer();
262
          //System.out.println("packagdoctype: " + util.getOption("packagedoctype"));
263
          //if(!doctype.equals(util.getOption("packagedoctype")))
264
          {
265
            String completeDocid = docid + util.getOption("accNumSeparator");
266
            completeDocid += rev;
267
            document.append("<docid>").append(completeDocid).append("</docid>");
268
            if (docname != null) {
269
              document.append("<docname>" + docname + "</docname>");
270
            }
271
            if (doctype != null) {
272
              document.append("<doctype>" + doctype + "</doctype>");
273
            }
274
// DOCTITLE attr cleared from the db
275
//            if (doctitle != null) {
276
//              document.append("<doctitle>" + doctitle + "</doctitle>");
277
//            }
278
            if(createDate != null) {
279
              document.append("<createdate>" + createDate + "</createdate>");
280
            }
281
            if(updateDate != null) {
282
              document.append("<updatedate>" + updateDate + "</updatedate>");
283
            }
284
            // Store the document id and the root node id
285
            docListResult.put(docid,(String)document.toString());
286
          }
287

    
288
          // Advance to the next record in the cursor
289
          tableHasRows = rs.next();
290
        }
291
        rs.close();
292
        //pstmt.close();
293
        
294
        if(qspec.containsExtendedSQL())
295
        {
296
          Vector extendedFields = new Vector(qspec.getReturnFieldList());
297
          Vector results = new Vector();
298
          Enumeration keylist = docListResult.keys();
299
          StringBuffer doclist = new StringBuffer();
300
          while(keylist.hasMoreElements())
301
          {
302
            doclist.append("'");
303
            doclist.append((String)keylist.nextElement());
304
            doclist.append("',");
305
          }
306
          doclist.deleteCharAt(doclist.length()-1); //remove the last comma
307
          pstmt.close();
308
          pstmt = dbconn.prepareStatement(qspec.printExtendedSQL(
309
                                        doclist.toString()));
310
          pstmt.execute();
311
          rs = pstmt.getResultSet();
312
          tableHasRows = rs.next();
313
          while(tableHasRows) 
314
          {
315
            docid = rs.getString(1);
316
            if ( !hasPermission(dbconn, user, group, docid) ) {
317
              // Advance to the next record in the cursor
318
              tableHasRows = rs.next();
319
              continue;
320
            }
321
            fieldname = rs.getString(2);
322
            fielddata = rs.getString(3);
323
            
324
            document = new StringBuffer();
325

    
326
            document.append("<param name=\"");
327
            document.append(fieldname);
328
            document.append("\">");
329
            document.append(fielddata);
330
            document.append("</param>");
331

    
332
            tableHasRows = rs.next();
333
            if(docListResult.containsKey(docid))
334
            {
335
              String removedelement = (String)docListResult.remove(docid);
336
              docListResult.put(docid, removedelement + document.toString());
337
            }
338
            else
339
            {
340
              docListResult.put(docid, document.toString()); 
341
            }
342
          }
343
          rs.close();
344
        }
345

    
346
        //this loop adds the relation data to the resultdoc
347
        //this code might be able to be added to the backtracking code above
348
        Enumeration docidkeys = docListResult.keys();
349
        while(docidkeys.hasMoreElements())
350
        {
351
          //String connstring = "metacat://"+util.getOption("server")+"?docid=";
352
          String connstring = "%docid=";
353
          String docidkey = (String)docidkeys.nextElement();
354
          //System.out.println("relationsql: " + qspec.printRelationSQL(
355
          //                                           connstring + docidkey));
356
          pstmt.close();
357
          pstmt = dbconn.prepareStatement(
358
                  qspec.printRelationSQL(connstring + docidkey));
359
          pstmt.execute();
360
          rs = pstmt.getResultSet();
361
          tableHasRows = rs.next();
362
          while(tableHasRows)
363
          {
364
            String sub = rs.getString(1);
365
            String rel = rs.getString(2);
366
            String obj = rs.getString(3);
367
            String subDT = rs.getString(4);
368
            String objDT = rs.getString(5);
369
            
370
            MetacatURL murl = new MetacatURL(sub);
371
            if(murl.getProtocol().equals("metacat"))
372
            {//we only want to process metacat urls here.
373
              String[] tempparam = murl.getParam(0);
374
              if(tempparam[0].equals("docid") && tempparam[1].equals(docidkey))
375
              {
376
                document = new StringBuffer();
377
                document.append("<relation>");
378
                document.append("<relationtype>").append(rel);
379
                document.append("</relationtype>");
380
                document.append("<relationdoc>").append(obj);
381
                document.append("</relationdoc>");
382
                document.append("<relationdoctype>").append(objDT);
383
                document.append("</relationdoctype>");
384
                document.append("</relation>");
385
                
386
                String removedelement = (String)docListResult.remove(docidkey);
387
                docListResult.put(docidkey, removedelement + document.toString());
388
                
389
              }
390
            }
391
            tableHasRows = rs.next();
392
          }
393
          rs.close();
394
          pstmt.close();
395
        }
396
        
397
      } catch (SQLException e) {
398
        System.err.println("SQL Error in DBQuery.findDocuments: " + 
399
                           e.getMessage());
400
      } catch (IOException ioe) {
401
        System.err.println("IO error in DBQuery.findDocuments:");
402
        System.err.println(ioe.getMessage());
403
      } catch (Exception ee) {
404
        System.out.println("Exception in DBQuery.findDocuments: " + 
405
                           ee.getMessage());
406
      }
407
      finally {
408
        try
409
        {
410
          dbconn.close();
411
        }
412
        catch(SQLException sqle)
413
        {
414
          System.out.println("error closing conn in DBQuery.findDocuments");
415
        }
416
      }
417
    //System.out.println("docListResult: ");
418
    //System.out.println(docListResult.toString());
419
    return docListResult;
420
  }
421
  
422
  /**
423
   * returns a string array of the contents of a particular node. 
424
   * If the node appears more than once, the contents are returned 
425
   * in the order in which they appearred in the document.
426
   * @param nodename the name or path of the particular node.
427
   * @param docid the docid of the document you want the node from.
428
   * @param conn a database connection-this allows this method to be static
429
   */
430
  public static Object[] getNodeContent(String nodename, String docid, 
431
                                        Connection conn)
432
  {
433
    StringBuffer query = new StringBuffer();
434
    Vector result = new Vector();
435
    PreparedStatement pstmt = null;
436
    query.append("select nodedata from xml_nodes where parentnodeid in ");
437
    query.append("(select nodeid from xml_index where path like '");
438
    query.append(nodename);
439
    query.append("' and docid like '").append(docid).append("')");
440
    try
441
    {
442
      pstmt = conn.prepareStatement(query.toString());
443

    
444
      // Execute the SQL query using the JDBC connection
445
      pstmt.execute();
446
      ResultSet rs = pstmt.getResultSet();
447
      boolean tableHasRows = rs.next();
448
      while (tableHasRows) 
449
      {
450
        result.add(rs.getString(1));
451
        System.out.println(rs.getString(1));
452
        tableHasRows = rs.next();
453
      }
454
    } 
455
    catch (SQLException e) 
456
    {
457
      System.err.println("Error in DBQuery.getNodeContent: " + e.getMessage());
458
    } finally {
459
      try
460
      {
461
        pstmt.close();
462
      }
463
      catch(SQLException sqle) {}
464
    }
465
    return result.toArray();
466
  }
467
  
468
  /**
469
   * format a structured query as an XML document that conforms
470
   * to the pathquery.dtd and is appropriate for submission to the DBQuery
471
   * structured query engine
472
   *
473
   * @param params The list of parameters that  should be included in the query
474
   */
475
  public static String createSQuery(Hashtable params)
476
  { 
477
    StringBuffer query = new StringBuffer();
478
    Enumeration elements;
479
    Enumeration keys;
480
    String doctype = null;
481
    String casesensitive = null;
482
    String searchmode = null;
483
    Object nextkey;
484
    Object nextelement;
485
    //add the xml headers
486
    query.append("<?xml version=\"1.0\"?>\n");
487
    query.append("<pathquery version=\"1.0\"><meta_file_id>");
488
    
489
    if(params.containsKey("meta_file_id"))
490
    {
491
      query.append( ((String[])params.get("meta_file_id"))[0]);
492
      query.append("</meta_file_id>");
493
    }
494
    else
495
    {
496
      query.append("unspecified</meta_file_id>");
497
    }
498
    
499
// DOCTITLE attr cleared from the db
500
//    query.append("<querytitle>");
501
//    if(params.containsKey("querytitle"))
502
//    {
503
//      query.append(((String[])params.get("querytitle"))[0]);
504
//      query.append("</querytitle>");
505
//    }
506
//    else
507
//    {
508
//      query.append("unspecified</querytitle>");
509
//    }
510
    
511
    if(params.containsKey("doctype"))
512
    {
513
      doctype = ((String[])params.get("doctype"))[0]; 
514
    }
515
    else
516
    {
517
      doctype = "ANY";  
518
    }
519
    
520
    if(params.containsKey("returnfield"))
521
    {
522
      String[] returnfield = ((String[])params.get("returnfield"));
523
      for(int i=0; i<returnfield.length; i++)
524
      {
525
        query.append("<returnfield>").append(returnfield[i]);
526
        query.append("</returnfield>");
527
      }
528
    }
529
    
530
    if(params.containsKey("owner"))
531
    {
532
      String[] owner = ((String[])params.get("owner"));
533
      for(int i=0; i<owner.length; i++)
534
      {
535
        query.append("<owner>").append(owner[i]);
536
        query.append("</owner>");
537
      }
538
    }
539
    
540
    if(params.containsKey("site"))
541
    {
542
      String[] site = ((String[])params.get("site"));
543
      for(int i=0; i<site.length; i++)
544
      {
545
        query.append("<site>").append(site[i]);
546
        query.append("</site>");
547
      }
548
    }
549
    
550
    //if you don't limit the query by doctype, then it just creates
551
    //an empty returndoctype tag.
552
    if (!doctype.equals("any") && 
553
        !doctype.equals("ANY") &&
554
        !doctype.equals("") ) 
555
    {
556
       query.append("<returndoctype>");
557
       query.append(doctype).append("</returndoctype>");
558
    }
559
    else
560
    { 
561
      query.append("<returndoctype></returndoctype>");
562
    }
563
    
564
    //allows the dynamic switching of boolean operators
565
    if(params.containsKey("operator"))
566
    {
567
      query.append("<querygroup operator=\"" + 
568
                ((String[])params.get("operator"))[0] + "\">");
569
    }
570
    else
571
    { //the default operator is UNION
572
      query.append("<querygroup operator=\"UNION\">"); 
573
    }
574
        
575
    if(params.containsKey("casesensitive"))
576
    {
577
      casesensitive = ((String[])params.get("casesensitive"))[0]; 
578
    }
579
    else
580
    {
581
      casesensitive = "false"; 
582
    }
583
    
584
    if(params.containsKey("searchmode"))
585
    {
586
      searchmode = ((String[])params.get("searchmode"))[0]; 
587
    }
588
    else
589
    {
590
      searchmode = "contains"; 
591
    }
592
        
593
    //anyfield is a special case because it does a 
594
    //free text search.  It does not have a <pathexpr>
595
    //tag.  This allows for a free text search within the structured
596
    //query.  This is useful if the INTERSECT operator is used.
597
    if(params.containsKey("anyfield"))
598
    {
599
       String[] anyfield = ((String[])params.get("anyfield"));
600
       //allow for more than one value for anyfield
601
       for(int i=0; i<anyfield.length; i++)
602
       {
603
         if(!anyfield[i].equals(""))
604
         {
605
           query.append("<queryterm casesensitive=\"" + casesensitive + 
606
                        "\" " + "searchmode=\"" + searchmode + "\"><value>" +
607
                        anyfield[i] +
608
                        "</value></queryterm>"); 
609
         }
610
       }
611
    }
612
        
613
    //this while loop finds the rest of the parameters
614
    //and attempts to query for the field specified
615
    //by the parameter.
616
    elements = params.elements();
617
    keys = params.keys();
618
    while(keys.hasMoreElements() && elements.hasMoreElements())
619
    {
620
      nextkey = keys.nextElement();
621
      nextelement = elements.nextElement();
622

    
623
      //make sure we aren't querying for any of these
624
      //parameters since the are already in the query
625
      //in one form or another.
626
      if(!nextkey.toString().equals("doctype") && 
627
         !nextkey.toString().equals("action")  &&
628
         !nextkey.toString().equals("qformat") && 
629
         !nextkey.toString().equals("anyfield") &&
630
         !nextkey.toString().equals("returnfield") &&
631
         !nextkey.toString().equals("owner") &&
632
         !nextkey.toString().equals("site") &&
633
         !nextkey.toString().equals("operator") )
634
      {
635
        //allow for more than value per field name
636
        for(int i=0; i<((String[])nextelement).length; i++)
637
        {
638
          if(!((String[])nextelement)[i].equals(""))
639
          {
640
            query.append("<queryterm casesensitive=\"" + casesensitive +"\" " + 
641
                         "searchmode=\"" + searchmode + "\">" +
642
                         "<value>" +
643
                         //add the query value
644
                         ((String[])nextelement)[i] +
645
                         "</value><pathexpr>" +
646
                         //add the path to query by 
647
                         nextkey.toString() + 
648
                         "</pathexpr></queryterm>");
649
          }
650
        }
651
      }
652
    }
653
    query.append("</querygroup></pathquery>");
654
    //append on the end of the xml and return the result as a string
655
    return query.toString();
656
  }
657
  
658
  /**
659
   * format a simple free-text value query as an XML document that conforms
660
   * to the pathquery.dtd and is appropriate for submission to the DBQuery
661
   * structured query engine
662
   *
663
   * @param value the text string to search for in the xml catalog
664
   * @param doctype the type of documents to include in the result set -- use
665
   *        "any" or "ANY" for unfiltered result sets
666
   */
667
   public static String createQuery(String value, String doctype) {
668
     StringBuffer xmlquery = new StringBuffer();
669
     xmlquery.append("<?xml version=\"1.0\"?>\n");
670
     xmlquery.append("<pathquery version=\"1.0\">");
671
     xmlquery.append("<meta_file_id>Unspecified</meta_file_id>");
672
// DOCTITLE attr cleared from the db
673
//     xmlquery.append("<querytitle>Unspecified</querytitle>");
674

    
675
     if (!doctype.equals("any") && !doctype.equals("ANY")) {
676
       xmlquery.append("<returndoctype>");
677
       xmlquery.append(doctype).append("</returndoctype>");
678
     }
679

    
680
     xmlquery.append("<querygroup operator=\"UNION\">");
681
     //chad added - 8/14
682
     //the if statement allows a query to gracefully handle a null 
683
     //query.  Without this if a nullpointerException is thrown.
684
     if(!value.equals(""))
685
     {
686
       xmlquery.append("<queryterm casesensitive=\"false\" ");
687
       xmlquery.append("searchmode=\"contains\">");
688
       xmlquery.append("<value>").append(value).append("</value>");
689
       xmlquery.append("</queryterm>");
690
     }
691
     xmlquery.append("</querygroup>");
692
     xmlquery.append("</pathquery>");
693

    
694
     
695
     return (xmlquery.toString());
696
   }
697

    
698
  /**
699
   * format a simple free-text value query as an XML document that conforms
700
   * to the pathquery.dtd and is appropriate for submission to the DBQuery
701
   * structured query engine
702
   *
703
   * @param value the text string to search for in the xml catalog
704
   */
705
   public static String createQuery(String value) {
706
     return createQuery(value, "any");
707
   }
708
   
709
  /** 
710
    * Check for "READ" permission on @docid for @user and/or @group 
711
    * from DB connection 
712
    */
713
  private boolean hasPermission ( Connection conn, String user,
714
                                  String group, String docid ) 
715
                  throws SQLException
716
  {
717
    // b' of the command line invocation
718
    if ( (user == null) && (group == null) ) {
719
      return true;
720
    }
721
    
722
    // Check for READ permission on @docid for @user and/or @group
723
    AccessControlList aclobj = new AccessControlList(conn);
724
    boolean hasPermission = aclobj.hasPermission("READ",user,docid);
725
    if ( !hasPermission && group != null ) {
726
      hasPermission = aclobj.hasPermission("READ",group,docid);
727
    }
728
    
729
    return hasPermission;
730
  }
731
   
732
}
(14-14/43)