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
 *
12
 *   '$Author: tao $'
13
 *     '$Date: 2007-05-11 12:07:40 -0700 (Fri, 11 May 2007) $'
14
 * '$Revision: 3277 $'
15
 *
16
 * This program is free software; you can redistribute it and/or modify
17
 * it under the terms of the GNU General Public License as published by
18
 * the Free Software Foundation; either version 2 of the License, or
19
 * (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU General Public License
27
 * along with this program; if not, write to the Free Software
28
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29
 */
30

    
31
package edu.ucsb.nceas.metacat;
32

    
33
import java.io.*;
34
import java.util.zip.*;
35
import java.sql.PreparedStatement;
36
import java.sql.ResultSet;
37
import java.sql.SQLException;
38
import java.util.*;
39

    
40
import javax.servlet.ServletOutputStream;
41
import javax.servlet.http.HttpServletResponse;
42
import javax.servlet.http.HttpSession;
43

    
44
import org.apache.log4j.Logger;
45

    
46
import org.w3c.dom.*;
47
import javax.xml.parsers.DocumentBuilderFactory;
48
import org.xml.sax.InputSource;
49
import org.w3c.dom.ls.*;
50

    
51
import edu.ucsb.nceas.morpho.datapackage.Triple;
52
import edu.ucsb.nceas.morpho.datapackage.TripleCollection;
53

    
54

    
55
/**
56
 * A Class that searches a relational DB for elements and attributes that have
57
 * free text matches a query string, or structured query matches to a path
58
 * specified node in the XML hierarchy. It returns a result set consisting of
59
 * the document ID for each document that satisfies the query
60
 */
61
public class DBQuery
62
{
63

    
64
    static final int ALL = 1;
65

    
66
    static final int WRITE = 2;
67

    
68
    static final int READ = 4;
69

    
70
    //private Connection conn = null;
71
    private String parserName = null;
72

    
73
    private MetaCatUtil util = new MetaCatUtil();
74

    
75
    private Logger logMetacat = Logger.getLogger(DBQuery.class);
76

    
77
    /** true if the metacat spatial option is installed **/
78
    private final boolean METACAT_SPATIAL = true;
79

    
80
    /** useful if you just want to grab a list of docids **/
81
    Vector docidOverride = new Vector();
82

    
83
    /**
84
     * the main routine used to test the DBQuery utility.
85
     * <p>
86
     * Usage: java DBQuery <xmlfile>
87
     *
88
     * @param xmlfile the filename of the xml file containing the query
89
     */
90
    static public void main(String[] args)
91
    {
92

    
93
        if (args.length < 1) {
94
            System.err.println("Wrong number of arguments!!!");
95
            System.err.println("USAGE: java DBQuery [-t] [-index] <xmlfile>");
96
            return;
97
        } else {
98
            try {
99

    
100
                int i = 0;
101
                boolean showRuntime = false;
102
                boolean useXMLIndex = false;
103
                if (args[i].equals("-t")) {
104
                    showRuntime = true;
105
                    i++;
106
                }
107
                if (args[i].equals("-index")) {
108
                    useXMLIndex = true;
109
                    i++;
110
                }
111
                String xmlfile = args[i];
112

    
113
                // Time the request if asked for
114
                double startTime = System.currentTimeMillis();
115

    
116
                // Open a connection to the database
117
                MetaCatUtil util = new MetaCatUtil();
118
                //Connection dbconn = util.openDBConnection();
119

    
120
                double connTime = System.currentTimeMillis();
121

    
122
                // Execute the query
123
                DBQuery queryobj = new DBQuery();
124
                FileReader xml = new FileReader(new File(xmlfile));
125
                Hashtable nodelist = null;
126
                //nodelist = queryobj.findDocuments(xml, null, null, useXMLIndex);
127

    
128
                // Print the reulting document listing
129
                StringBuffer result = new StringBuffer();
130
                String document = null;
131
                String docid = null;
132
                result.append("<?xml version=\"1.0\"?>\n");
133
                result.append("<resultset>\n");
134

    
135
                if (!showRuntime) {
136
                    Enumeration doclist = nodelist.keys();
137
                    while (doclist.hasMoreElements()) {
138
                        docid = (String) doclist.nextElement();
139
                        document = (String) nodelist.get(docid);
140
                        result.append("  <document>\n    " + document
141
                                + "\n  </document>\n");
142
                    }
143

    
144
                    result.append("</resultset>\n");
145
                }
146
                // Time the request if asked for
147
                double stopTime = System.currentTimeMillis();
148
                double dbOpenTime = (connTime - startTime) / 1000;
149
                double readTime = (stopTime - connTime) / 1000;
150
                double executionTime = (stopTime - startTime) / 1000;
151
                if (showRuntime) {
152
                    System.out.print("  " + executionTime);
153
                    System.out.print("  " + dbOpenTime);
154
                    System.out.print("  " + readTime);
155
                    System.out.print("  " + nodelist.size());
156
                    System.out.println();
157
                }
158
                //System.out.println(result);
159
                //write into a file "result.txt"
160
                if (!showRuntime) {
161
                    File f = new File("./result.txt");
162
                    FileWriter fw = new FileWriter(f);
163
                    BufferedWriter out = new BufferedWriter(fw);
164
                    out.write(result.toString());
165
                    out.flush();
166
                    out.close();
167
                    fw.close();
168
                }
169

    
170
            } catch (Exception e) {
171
                System.err.println("Error in DBQuery.main");
172
                System.err.println(e.getMessage());
173
                e.printStackTrace(System.err);
174
            }
175
        }
176
    }
177

    
178
    /**
179
     * construct an instance of the DBQuery class
180
     *
181
     * <p>
182
     * Generally, one would call the findDocuments() routine after creating an
183
     * instance to specify the search query
184
     * </p>
185
     *
186

    
187
     * @param parserName the fully qualified name of a Java class implementing
188
     *            the org.xml.sax.XMLReader interface
189
     */
190
    public DBQuery()
191
    {
192
        String parserName = MetaCatUtil.getOption("saxparser");
193
        this.parserName = parserName;
194
    }
195

    
196
    /**
197
     * 
198
     * Construct an instance of DBQuery Class
199
     * BUT accept a docid Vector that will supersede
200
     * the query.printSQL() method
201
     *
202
     * If a docid Vector is passed in,
203
     * the docids will be used to create a simple IN query 
204
     * without the multiple subselects of the printSQL() method
205
     *
206
     * Using this constructor, we just check for 
207
     * a docidOverride Vector in the findResultDoclist() method
208
     *
209
     * @param docids List of docids to display in the resultset
210
     */
211
    public DBQuery(Vector docids)
212
    {
213
        this.docidOverride = docids;
214
        String parserName = MetaCatUtil.getOption("saxparser");
215
        this.parserName = parserName;
216
    }
217

    
218
  /**
219
   * Method put the search result set into out printerwriter
220
   * @param resoponse the return response
221
   * @param out the output printer
222
   * @param params the paratermer hashtable
223
   * @param user the user name (it maybe different to the one in param)
224
   * @param groups the group array
225
   * @param sessionid  the sessionid
226
   */
227
  public void findDocuments(HttpServletResponse response,
228
                                       PrintWriter out, Hashtable params,
229
                                       String user, String[] groups,
230
                                       String sessionid)
231
  {
232
    boolean useXMLIndex = (new Boolean(MetaCatUtil.getOption("usexmlindex")))
233
               .booleanValue();
234
    findDocuments(response, out, params, user, groups, sessionid, useXMLIndex);
235

    
236
  }
237

    
238

    
239
    /**
240
     * Method put the search result set into out printerwriter
241
     * @param resoponse the return response
242
     * @param out the output printer
243
     * @param params the paratermer hashtable
244
     * @param user the user name (it maybe different to the one in param)
245
     * @param groups the group array
246
     * @param sessionid  the sessionid
247
     */
248
    public void findDocuments(HttpServletResponse response,
249
                                         PrintWriter out, Hashtable params,
250
                                         String user, String[] groups,
251
                                         String sessionid, boolean useXMLIndex)
252
    {
253
      int pagesize = 0;
254
      int pagestart = 0;
255
      
256
      if(params.containsKey("pagesize") && params.containsKey("pagestart"))
257
      {
258
        String pagesizeStr = ((String[])params.get("pagesize"))[0];
259
        String pagestartStr = ((String[])params.get("pagestart"))[0];
260
        if(pagesizeStr != null && pagestartStr != null)
261
        {
262
          pagesize = (new Integer(pagesizeStr)).intValue();
263
          pagestart = (new Integer(pagestartStr)).intValue();
264
        }
265
      }
266
      
267
      // get query and qformat
268
      String xmlquery = ((String[])params.get("query"))[0];
269

    
270
      logMetacat.info("SESSIONID: " + sessionid);
271
      logMetacat.info("xmlquery: " + xmlquery);
272
      String qformat = ((String[])params.get("qformat"))[0];
273
      logMetacat.info("qformat: " + qformat);
274
      // Get the XML query and covert it into a SQL statment
275
      QuerySpecification qspec = null;
276
      if ( xmlquery != null)
277
      {
278
         xmlquery = transformQuery(xmlquery);
279
         try
280
         {
281
           qspec = new QuerySpecification(xmlquery,
282
                                          parserName,
283
                                          MetaCatUtil.getOption("accNumSeparator"));
284
         }
285
         catch (Exception ee)
286
         {
287
           logMetacat.error("error generating QuerySpecification object"
288
                                    +" in DBQuery.findDocuments"
289
                                    + ee.getMessage());
290
         }
291
      }
292

    
293

    
294

    
295
      if (qformat != null && qformat.equals(MetaCatServlet.XMLFORMAT))
296
      {
297
        //xml format
298
        response.setContentType("text/xml");
299
        createResultDocument(xmlquery, qspec, out, user, groups, useXMLIndex, 
300
          pagesize, pagestart, sessionid);
301
      }//if
302
      else
303
      {
304
        //knb format, in this case we will get whole result and sent it out
305
        response.setContentType("text/html");
306
        PrintWriter nonout = null;
307
        StringBuffer xml = createResultDocument(xmlquery, qspec, nonout, user,
308
                                                groups, useXMLIndex, pagesize, 
309
                                                pagestart, sessionid);
310
        
311
        //transfer the xml to html
312
        try
313
        {
314
         double startHTMLTransform = System.currentTimeMillis()/1000;
315
         DBTransform trans = new DBTransform();
316
         response.setContentType("text/html");
317

    
318
         // if the user is a moderator, then pass a param to the 
319
         // xsl specifying the fact
320
         if(MetaCatUtil.isModerator(user, groups)){
321
        	 params.put("isModerator", new String[] {"true"});
322
         }
323

    
324
         trans.transformXMLDocument(xml.toString(), "-//NCEAS//resultset//EN",
325
                                 "-//W3C//HTML//EN", qformat, out, params,
326
                                 sessionid);
327
         double endHTMLTransform = System.currentTimeMillis()/1000;
328
          logMetacat.warn("The time to transfrom resultset from xml to html format is "
329
                  		                             +(endHTMLTransform -startHTMLTransform));
330
          MetaCatUtil.writeDebugToFile("---------------------------------------------------------------------------------------------------------------Transfrom xml to html  "
331
                             +(endHTMLTransform -startHTMLTransform));
332
          MetaCatUtil.writeDebugToDelimiteredFile(" "+(endHTMLTransform -startHTMLTransform), false);
333
        }
334
        catch(Exception e)
335
        {
336
         logMetacat.error("Error in MetaCatServlet.transformResultset:"
337
                                +e.getMessage());
338
         }
339

    
340
      }//else
341

    
342
  }
343
  
344
  /**
345
   * Transforms a hashtable of documents to an xml or html result and sent
346
   * the content to outputstream. Keep going untill hastable is empty. stop it.
347
   * add the QuerySpecification as parameter is for ecogrid. But it is duplicate
348
   * to xmlquery String
349
   * @param xmlquery
350
   * @param qspec
351
   * @param out
352
   * @param user
353
   * @param groups
354
   * @param useXMLIndex
355
   * @param sessionid
356
   * @return
357
   */
358
    public StringBuffer createResultDocument(String xmlquery,
359
                                              QuerySpecification qspec,
360
                                              PrintWriter out,
361
                                              String user, String[] groups,
362
                                              boolean useXMLIndex)
363
    {
364
    	return createResultDocument(xmlquery,qspec,out, user,groups, useXMLIndex, 0, 0,"");
365
    }
366

    
367
  /*
368
   * Transforms a hashtable of documents to an xml or html result and sent
369
   * the content to outputstream. Keep going untill hastable is empty. stop it.
370
   * add the QuerySpecification as parameter is for ecogrid. But it is duplicate
371
   * to xmlquery String
372
   */
373
  public StringBuffer createResultDocument(String xmlquery,
374
                                            QuerySpecification qspec,
375
                                            PrintWriter out,
376
                                            String user, String[] groups,
377
                                            boolean useXMLIndex, int pagesize,
378
                                            int pagestart, String sessionid)
379
  {
380
    DBConnection dbconn = null;
381
    int serialNumber = -1;
382
    StringBuffer resultset = new StringBuffer();
383

    
384
    //try to get the cached version first    
385
    Hashtable sessionHash = MetaCatServlet.getSessionHash();
386
    HttpSession sess = (HttpSession)sessionHash.get(sessionid);
387

    
388
    QuerySpecification cachedQuerySpec = null;
389
    if (sess != null)
390
    {
391
    	cachedQuerySpec = (QuerySpecification)sess.getAttribute("query");
392
    }
393
    
394
    resultset.append("<?xml version=\"1.0\"?>\n");
395
    resultset.append("<resultset>\n");
396
    resultset.append("  <pagestart>" + pagestart + "</pagestart>\n");
397
    resultset.append("  <pagesize>" + pagesize + "</pagesize>\n");
398
    resultset.append("  <nextpage>" + (pagestart + 1) + "</nextpage>\n");
399
    resultset.append("  <previouspage>" + (pagestart - 1) + "</previouspage>\n");
400

    
401
    resultset.append("  <query>" + xmlquery + "</query>");
402
    //send out a new query
403
    if (out != null)
404
    {
405
      out.println(resultset.toString());
406
    }
407
    if (qspec != null)
408
    {
409
      try
410
      {
411

    
412
        //checkout the dbconnection
413
        dbconn = DBConnectionPool.getDBConnection("DBQuery.findDocuments");
414
        serialNumber = dbconn.getCheckOutSerialNumber();
415

    
416
        //print out the search result
417
        // search the doc list
418
        resultset = findResultDoclist(qspec, resultset, out, user, groups,
419
                                      dbconn, useXMLIndex, pagesize, pagestart, 
420
                                      sessionid);
421
      } //try
422
      catch (IOException ioe)
423
      {
424
        logMetacat.error("IO error in DBQuery.findDocuments:");
425
        logMetacat.error(ioe.getMessage());
426

    
427
      }
428
      catch (SQLException e)
429
      {
430
        logMetacat.error("SQL Error in DBQuery.findDocuments: "
431
                                 + e.getMessage());
432
      }
433
      catch (Exception ee)
434
      {
435
        logMetacat.error("Exception in DBQuery.findDocuments: "
436
                                 + ee.getMessage());
437
        ee.printStackTrace();
438
      }
439
      finally
440
      {
441
        DBConnectionPool.returnDBConnection(dbconn, serialNumber);
442
      } //finally
443
    }//if
444
    String closeRestultset = "</resultset>";
445
    resultset.append(closeRestultset);
446
    if (out != null)
447
    {
448
      out.println(closeRestultset);
449
    }
450

    
451
    //default to returning the whole resultset
452
    return resultset;
453
  }//createResultDocuments
454

    
455
    /*
456
     * Find the doc list which match the query
457
     */
458
    private StringBuffer findResultDoclist(QuerySpecification qspec,
459
                                      StringBuffer resultsetBuffer,
460
                                      PrintWriter out,
461
                                      String user, String[]groups,
462
                                      DBConnection dbconn, boolean useXMLIndex,
463
                                      int pagesize, int pagestart, String sessionid)
464
                                      throws Exception
465
    {
466
      String query = null;
467
      int count = 0;
468
      int index = 0;
469
      ResultDocumentSet docListResult = new ResultDocumentSet();
470
      PreparedStatement pstmt = null;
471
      String docid = null;
472
      String docname = null;
473
      String doctype = null;
474
      String createDate = null;
475
      String updateDate = null;
476
      StringBuffer document = null;
477
      boolean lastpage = false;
478
      int rev = 0;
479
      double startTime = 0;
480
      int offset = 1;
481
      double startSelectionTime = System.currentTimeMillis()/1000;
482
      ResultSet rs = null;
483
        
484
      offset = 1;
485
      // this is a hack for offset
486
      if (out == null)
487
      {
488
        // for html page, we put everything into one page
489
        offset =
490
            (new Integer(MetaCatUtil.getOption("web_resultsetsize"))).intValue();
491
      }
492
      else
493
      {
494
          offset =
495
              (new Integer(MetaCatUtil.getOption("app_resultsetsize"))).intValue();
496
      }
497

    
498
      /*
499
       * Check the docidOverride Vector
500
       * if defined, we bypass the qspec.printSQL() method
501
       * and contruct a simpler query based on a 
502
       * list of docids rather than a bunch of subselects
503
       */
504
      if ( this.docidOverride.size() == 0 ) {
505
          query = qspec.printSQL(useXMLIndex);
506
      } else {
507
          logMetacat.info("*** docid override " + this.docidOverride.size());
508
          StringBuffer queryBuffer = new StringBuffer( "SELECT docid,docname,doctype,date_created, date_updated, rev " );
509
          queryBuffer.append( " FROM xml_documents WHERE docid IN (" );
510
          for (int i = 0; i < docidOverride.size(); i++) {  
511
              queryBuffer.append("'");
512
              queryBuffer.append( (String)docidOverride.elementAt(i) );
513
              queryBuffer.append("',");
514
          }
515
          // empty string hack 
516
          queryBuffer.append( "'') " );
517
          query = queryBuffer.toString();
518
      } 
519
      String ownerQuery = getOwnerQuery(user);
520
      logMetacat.info("\n\n\n query: " + query);
521
      logMetacat.info("\n\n\n owner query: "+ownerQuery);
522
      // if query is not the owner query, we need to check the permission
523
      // otherwise we don't need (owner has all permission by default)
524
      if (!query.equals(ownerQuery))
525
      {
526
        // set user name and group
527
        qspec.setUserName(user);
528
        qspec.setGroup(groups);
529
        // Get access query
530
        String accessQuery = qspec.getAccessQuery();
531
        if(!query.endsWith("WHERE")){
532
            query = query + accessQuery;
533
        } else {
534
            query = query + accessQuery.substring(4, accessQuery.length());
535
        }
536
        logMetacat.info("\n\n\n final query: " + query);
537
      }
538

    
539
      startTime = System.currentTimeMillis() / 1000;
540
      pstmt = dbconn.prepareStatement(query);
541
      rs = pstmt.executeQuery();
542

    
543
      double queryExecuteTime = System.currentTimeMillis() / 1000;
544
      logMetacat.warn("Time to execute select docid query is "
545
                    + (queryExecuteTime - startTime));
546
      MetaCatUtil.writeDebugToFile("\n\n\n\n\n\nExecute selection query  "
547
              + (queryExecuteTime - startTime));
548
      MetaCatUtil.writeDebugToDelimiteredFile(""+(queryExecuteTime - startTime), false);
549

    
550
      boolean tableHasRows = rs.next();
551
      
552
      if(pagesize == 0)
553
      { //this makes sure we get all results if there is no paging
554
        pagesize = 99999;
555
        pagestart = 99999;
556
      } 
557
      
558
      int currentIndex = 0;
559
      while (tableHasRows)
560
      {
561
        logMetacat.info("############getting result: " + currentIndex);
562
        docid = rs.getString(1).trim();
563
        logMetacat.info("############processing: " + docid);
564
        docname = rs.getString(2);
565
        doctype = rs.getString(3);
566
        logMetacat.info("############processing: " + doctype);
567
        createDate = rs.getString(4);
568
        updateDate = rs.getString(5);
569
        rev = rs.getInt(6);
570
        
571
        // if there are returndocs to match, backtracking can be performed
572
        // otherwise, just return the document that was hit
573
        Vector returndocVec = qspec.getReturnDocList();
574
        if (returndocVec.size() != 0 && !returndocVec.contains(doctype)
575
             && !qspec.isPercentageSearch())
576
         {
577
           logMetacat.info("Back tracing now...");
578
           String sep = MetaCatUtil.getOption("accNumSeparator");
579
           StringBuffer btBuf = new StringBuffer();
580
           btBuf.append("select docid from xml_relation where ");
581

    
582
           //build the doctype list for the backtracking sql statement
583
           btBuf.append("packagetype in (");
584
           for (int i = 0; i < returndocVec.size(); i++)
585
           {
586
             btBuf.append("'").append((String) returndocVec.get(i)).append("'");
587
             if (i != (returndocVec.size() - 1))
588
             {
589
                btBuf.append(", ");
590
              }
591
            }
592
            btBuf.append(") ");
593
            btBuf.append("and (subject like '");
594
            btBuf.append(docid).append("'");
595
            btBuf.append("or object like '");
596
            btBuf.append(docid).append("')");
597

    
598
            PreparedStatement npstmt = dbconn.prepareStatement(btBuf.toString());
599
            //should incease usage count
600
            dbconn.increaseUsageCount(1);
601
            npstmt.execute();
602
            ResultSet btrs = npstmt.getResultSet();
603
            boolean hasBtRows = btrs.next();
604
            //Hashtable list = new Hashtable();
605
            while (hasBtRows)
606
            {
607
               //there was a backtrackable document found
608
               DocumentImpl xmldoc = null;
609
               String packageDocid = btrs.getString(1);
610
               logMetacat.info("Getting document for docid: "
611
                                         + packageDocid);
612
                try
613
                {
614
                    //  THIS CONSTRUCTOR BUILDS THE WHOLE XML doc not
615
                    // needed here
616
                    // xmldoc = new DocumentImpl(dbconn, packageDocid);
617
                    //  thus use the following to get the doc info only
618
                    //  xmldoc = new DocumentImpl(dbconn);
619
                    String accNumber = packageDocid + MetaCatUtil.getOption("accNumSeparator") +
620
                    DBUtil.getLatestRevisionInDocumentTable(packageDocid);
621
                    xmldoc = new DocumentImpl(accNumber, false);
622
                    if (xmldoc == null)
623
                    {
624
                       logMetacat.info("Document was null for: "
625
                                                + packageDocid);
626
                    }
627
                }
628
                catch (Exception e)
629
                {
630
                    System.out.println("Error getting document in "
631
                                       + "DBQuery.findDocuments: "
632
                                       + e.getMessage());
633
                }
634

    
635
                String docid_org = xmldoc.getDocID();
636
                if (docid_org == null)
637
                {
638
                   logMetacat.info("Docid_org was null.");
639
                   hasBtRows = btrs.next();
640
                   continue;
641
                }
642
                docid = docid_org.trim();
643
                /*if (list.containsKey(docid))
644
                {
645
                        	logMetacat.info("DocumentResultSet already has docid "+docid+" and skip it");
646
                            hasBtRows = btrs.next();
647
                            continue;
648
                 }*/
649
                docname = xmldoc.getDocname();
650
                doctype = xmldoc.getDoctype();
651
                createDate = xmldoc.getCreateDate();
652
                updateDate = xmldoc.getUpdateDate();
653
                rev = xmldoc.getRev();
654
                document = new StringBuffer();
655

    
656
                String completeDocid = docid
657
                                + MetaCatUtil.getOption("accNumSeparator");
658
                completeDocid += rev;
659
                document.append("<docid>").append(completeDocid);
660
                document.append("</docid>");
661
                if (docname != null)
662
                {
663
                  document.append("<docname>" + docname + "</docname>");
664
                }
665
                if (doctype != null)
666
                {
667
                  document.append("<doctype>" + doctype + "</doctype>");
668
                }
669
                if (createDate != null)
670
                {
671
                 document.append("<createdate>" + createDate + "</createdate>");
672
                }
673
                if (updateDate != null)
674
                {
675
                  document.append("<updatedate>" + updateDate+ "</updatedate>");
676
                }
677
                // Store the document id and the root node id
678
                docListResult.addResultDocument(
679
                  new ResultDocument(docid, (String) document.toString()));
680
                currentIndex++;
681
                //list.put(docid, docid);
682
                logMetacat.info("$$$$$$$real result: " + docid);
683
                count++;
684

    
685
                // Get the next package document linked to our hit
686
                hasBtRows = btrs.next();
687
              }//while
688
              npstmt.close();
689
              btrs.close();
690
        }
691
        else if (returndocVec.size() == 0 || returndocVec.contains(doctype))
692
        {
693
          logMetacat.info("NOT Back tracing now...");
694
           document = new StringBuffer();
695

    
696
           String completeDocid = docid
697
                            + MetaCatUtil.getOption("accNumSeparator");
698
           completeDocid += rev;
699
           document.append("<docid>").append(completeDocid).append("</docid>");
700
           if (docname != null)
701
           {
702
               document.append("<docname>" + docname + "</docname>");
703
           }
704
           if (doctype != null)
705
           {
706
              document.append("<doctype>" + doctype + "</doctype>");
707
           }
708
           if (createDate != null)
709
           {
710
               document.append("<createdate>" + createDate + "</createdate>");
711
           }
712
           if (updateDate != null)
713
           {
714
             document.append("<updatedate>" + updateDate + "</updatedate>");
715
           }
716
           // Store the document id and the root node id
717
           
718
           docListResult.addResultDocument(
719
             new ResultDocument(docid, (String) document.toString()));
720
           logMetacat.info("$$$$$$$real result: " + docid);
721
           currentIndex++;
722
           count++;
723
        }//else
724
        
725
        // when doclist reached the offset number, send out doc list and empty
726
        // the hash table
727
        if (count == offset && pagesize == 0)
728
        { //if pagesize is not 0, do this later.
729
          //reset count
730
          //logMetacat.warn("############doing subset cache");
731
          count = 0;
732
          handleSubsetResult(qspec, resultsetBuffer, out, docListResult,
733
                              user, groups,dbconn, useXMLIndex);
734
          //reset docListResult
735
          docListResult = new ResultDocumentSet();
736
        }
737
       
738
       logMetacat.info("currentIndex: " + currentIndex);
739
       logMetacat.info("page comparator: " + (pagesize * pagestart) + pagesize);
740
       if(currentIndex >= ((pagesize * pagestart) + pagesize))
741
       {
742
         ResultDocumentSet pagedResultsHash = new ResultDocumentSet();
743
         for(int i=pagesize*pagestart; i<docListResult.size(); i++)
744
         {
745
           pagedResultsHash.put(docListResult.get(i));
746
         }
747
         
748
         docListResult = pagedResultsHash;
749
         break;
750
       }
751
       // Advance to the next record in the cursor
752
       tableHasRows = rs.next();
753
       if(!tableHasRows)
754
       {
755
         ResultDocumentSet pagedResultsHash = new ResultDocumentSet();
756
         //get the last page of information then break
757
         if(pagesize != 99999)
758
         {
759
           for(int i=pagesize*pagestart; i<docListResult.size(); i++)
760
           {
761
             pagedResultsHash.put(docListResult.get(i));
762
           }
763
           docListResult = pagedResultsHash;
764
         }
765
         
766
         lastpage = true;
767
         break;
768
       }
769
     }//while
770
     
771
     rs.close();
772
     pstmt.close();
773
     double docListTime = System.currentTimeMillis() / 1000;
774
     logMetacat.warn("======Total time to get docid list is: "
775
                          + (docListTime - startSelectionTime ));
776
     MetaCatUtil.writeDebugToFile("---------------------------------------------------------------------------------------------------------------Total selection: "
777
             + (docListTime - startSelectionTime ));
778
     MetaCatUtil.writeDebugToDelimiteredFile(" "+ (docListTime - startSelectionTime ), false);
779
     //if docListResult is not empty, it need to be sent.
780
     if (docListResult.size() != 0)
781
     {
782
       handleSubsetResult(qspec,resultsetBuffer, out, docListResult,
783
                              user, groups,dbconn, useXMLIndex);
784
     }
785

    
786
     resultsetBuffer.append("\n<lastpage>" + lastpage + "</lastpage>\n");
787
     if (out != null)
788
     {
789
         out.println("\n<lastpage>" + lastpage + "</lastpage>\n");
790
     }
791
          
792
     return resultsetBuffer;
793
    }//findReturnDoclist
794

    
795

    
796
    /*
797
     * Send completed search hashtable(part of reulst)to output stream
798
     * and buffer into a buffer stream
799
     */
800
    private StringBuffer handleSubsetResult(QuerySpecification qspec,
801
                                           StringBuffer resultset,
802
                                           PrintWriter out, ResultDocumentSet partOfDoclist,
803
                                           String user, String[]groups,
804
                                       DBConnection dbconn, boolean useXMLIndex)
805
                                       throws Exception
806
   {
807
     double startReturnField = System.currentTimeMillis()/1000;
808
     // check if there is a record in xml_returnfield
809
     // and get the returnfield_id and usage count
810
     int usage_count = getXmlReturnfieldsTableId(qspec, dbconn);
811
     boolean enterRecords = false;
812

    
813
     // get value of xml_returnfield_count
814
     int count = (new Integer(MetaCatUtil
815
                            .getOption("xml_returnfield_count")))
816
                            .intValue();
817

    
818
     // set enterRecords to true if usage_count is more than the offset
819
     // specified in metacat.properties
820
     if(usage_count > count){
821
         enterRecords = true;
822
     }
823

    
824
     if(returnfield_id < 0){
825
         logMetacat.warn("Error in getting returnfield id from"
826
                                  + "xml_returnfield table");
827
         enterRecords = false;
828
     }
829

    
830
     // get the hashtable containing the docids that already in the
831
     // xml_queryresult table
832
     logMetacat.info("size of partOfDoclist before"
833
                             + " docidsInQueryresultTable(): "
834
                             + partOfDoclist.size());
835
     double startGetReturnValueFromQueryresultable = System.currentTimeMillis()/1000;
836
     Hashtable queryresultDocList = docidsInQueryresultTable(returnfield_id,
837
                                                        partOfDoclist, dbconn);
838

    
839
     // remove the keys in queryresultDocList from partOfDoclist
840
     Enumeration _keys = queryresultDocList.keys();
841
     while (_keys.hasMoreElements()){
842
         partOfDoclist.remove((String)_keys.nextElement());
843
     }
844
     double endGetReturnValueFromQueryresultable = System.currentTimeMillis()/1000;
845
     logMetacat.warn("Time to get return fields from xml_queryresult table is (Part1 in return fields) " +
846
          		               (endGetReturnValueFromQueryresultable-startGetReturnValueFromQueryresultable));
847
     MetaCatUtil.writeDebugToFile("-----------------------------------------Get fields from xml_queryresult(Part1 in return fields) " +
848
               (endGetReturnValueFromQueryresultable-startGetReturnValueFromQueryresultable));
849
     MetaCatUtil.writeDebugToDelimiteredFile(" " +
850
             (endGetReturnValueFromQueryresultable-startGetReturnValueFromQueryresultable),false);
851
     // backup the keys-elements in partOfDoclist to check later
852
     // if the doc entry is indexed yet
853
     Hashtable partOfDoclistBackup = new Hashtable();
854
     Iterator itt = partOfDoclist.getDocids();
855
     while (itt.hasNext()){
856
       Object key = itt.next();
857
         partOfDoclistBackup.put(key, partOfDoclist.get(key));
858
     }
859

    
860
     logMetacat.info("size of partOfDoclist after"
861
                             + " docidsInQueryresultTable(): "
862
                             + partOfDoclist.size());
863

    
864
     //add return fields for the documents in partOfDoclist
865
     partOfDoclist = addReturnfield(partOfDoclist, qspec, user, groups,
866
                                        dbconn, useXMLIndex);
867
     double endExtendedQuery = System.currentTimeMillis()/1000;
868
     logMetacat.warn("Get fields from index and node table (Part2 in return fields) "
869
        		                                          + (endExtendedQuery - endGetReturnValueFromQueryresultable));
870
     MetaCatUtil.writeDebugToFile("-----------------------------------------Get fields from extened query(Part2 in return fields) "
871
             + (endExtendedQuery - endGetReturnValueFromQueryresultable));
872
     MetaCatUtil.writeDebugToDelimiteredFile(" "
873
             + (endExtendedQuery - endGetReturnValueFromQueryresultable), false);
874
     //add relationship part part docid list for the documents in partOfDocList
875
     partOfDoclist = addRelationship(partOfDoclist, qspec, dbconn, useXMLIndex);
876

    
877
     double startStoreReturnField = System.currentTimeMillis()/1000;
878
     Iterator keys = partOfDoclist.getDocids();
879
     String key = null;
880
     String element = null;
881
     String query = null;
882
     int offset = (new Integer(MetaCatUtil
883
                               .getOption("queryresult_string_length")))
884
                               .intValue();
885
     while (keys.hasNext())
886
     {
887
         key = (String) keys.next();
888
         element = (String)partOfDoclist.get(key);
889

    
890
	 // check if the enterRecords is true, elements is not null, element's
891
         // length is less than the limit of table column and if the document
892
         // has been indexed already
893
         if(enterRecords && element != null
894
		&& element.length() < offset
895
		&& element.compareTo((String) partOfDoclistBackup.get(key)) != 0){
896
             query = "INSERT INTO xml_queryresult (returnfield_id, docid, "
897
                 + "queryresult_string) VALUES (?, ?, ?)";
898

    
899
             PreparedStatement pstmt = null;
900
             pstmt = dbconn.prepareStatement(query);
901
             pstmt.setInt(1, returnfield_id);
902
             pstmt.setString(2, key);
903
             pstmt.setString(3, element);
904

    
905
             dbconn.increaseUsageCount(1);
906
             pstmt.execute();
907
             pstmt.close();
908
         }
909
        
910
         // A string with element
911
         String xmlElement = "  <document>" + element + "</document>";
912

    
913
         //send single element to output
914
         if (out != null)
915
         {
916
             out.println(xmlElement);
917
         }
918
         resultset.append(xmlElement);
919
     }//while
920
     
921
     double endStoreReturnField = System.currentTimeMillis()/1000;
922
     logMetacat.warn("Time to store new return fields into xml_queryresult table (Part4 in return fields) "
923
                   + (endStoreReturnField -startStoreReturnField));
924
     MetaCatUtil.writeDebugToFile("-----------------------------------------Insert new record to xml_queryresult(Part4 in return fields) "
925
             + (endStoreReturnField -startStoreReturnField));
926
     MetaCatUtil.writeDebugToDelimiteredFile(" "
927
             + (endStoreReturnField -startStoreReturnField), false);
928
     
929
     Enumeration keysE = queryresultDocList.keys();
930
     while (keysE.hasMoreElements())
931
     {
932
         key = (String) keysE.nextElement();
933
         element = (String)queryresultDocList.get(key);
934
         // A string with element
935
         String xmlElement = "  <document>" + element + "</document>";
936
         //send single element to output
937
         if (out != null)
938
         {
939
             out.println(xmlElement);
940
         }
941
         resultset.append(xmlElement);
942
     }//while
943
     double returnFieldTime = System.currentTimeMillis() / 1000;
944
     logMetacat.warn("======Total time to get return fields is: "
945
                           + (returnFieldTime - startReturnField));
946
     MetaCatUtil.writeDebugToFile("---------------------------------------------------------------------------------------------------------------"+
947
    		 "Total to get return fields  "
948
                                   + (returnFieldTime - startReturnField));
949
     MetaCatUtil.writeDebugToDelimiteredFile(" "+ (returnFieldTime - startReturnField), false);
950
     return resultset;
951
 }
952

    
953
   /**
954
    * Get the docids already in xml_queryresult table and corresponding
955
    * queryresultstring as a hashtable
956
    */
957
   private Hashtable docidsInQueryresultTable(int returnfield_id,
958
                                              ResultDocumentSet partOfDoclist,
959
                                              DBConnection dbconn){
960

    
961
         Hashtable returnValue = new Hashtable();
962
         PreparedStatement pstmt = null;
963
         ResultSet rs = null;
964

    
965
         // get partOfDoclist as string for the query
966
         Iterator keylist = partOfDoclist.getDocids();
967
         StringBuffer doclist = new StringBuffer();
968
         while (keylist.hasNext())
969
         {
970
             doclist.append("'");
971
             doclist.append((String) keylist.next());
972
             doclist.append("',");
973
         }//while
974

    
975

    
976
         if (doclist.length() > 0)
977
         {
978
             doclist.deleteCharAt(doclist.length() - 1); //remove the last comma
979

    
980
             // the query to find out docids from xml_queryresult
981
             String query = "select docid, queryresult_string from "
982
                          + "xml_queryresult where returnfield_id = " +
983
                          returnfield_id +" and docid in ("+ doclist + ")";
984
             logMetacat.info("Query to get docids from xml_queryresult:"
985
                                      + query);
986

    
987
             try {
988
                 // prepare and execute the query
989
                 pstmt = dbconn.prepareStatement(query);
990
                 dbconn.increaseUsageCount(1);
991
                 pstmt.execute();
992
                 rs = pstmt.getResultSet();
993
                 boolean tableHasRows = rs.next();
994
                 while (tableHasRows) {
995
                     // store the returned results in the returnValue hashtable
996
                     String key = rs.getString(1);
997
                     String element = rs.getString(2);
998

    
999
                     if(element != null){
1000
                         returnValue.put(key, element);
1001
                     } else {
1002
                         logMetacat.info("Null elment found ("
1003
                         + "DBQuery.docidsInQueryresultTable)");
1004
                     }
1005
                     tableHasRows = rs.next();
1006
                 }
1007
                 rs.close();
1008
                 pstmt.close();
1009
             } catch (Exception e){
1010
                 logMetacat.error("Error getting docids from "
1011
                                          + "queryresult in "
1012
                                          + "DBQuery.docidsInQueryresultTable: "
1013
                                          + e.getMessage());
1014
              }
1015
         }
1016
         return returnValue;
1017
     }
1018

    
1019

    
1020
   /**
1021
    * Method to get id from xml_returnfield table
1022
    * for a given query specification
1023
    */
1024
   private int returnfield_id;
1025
   private int getXmlReturnfieldsTableId(QuerySpecification qspec,
1026
                                           DBConnection dbconn){
1027
       int id = -1;
1028
       int count = 1;
1029
       PreparedStatement pstmt = null;
1030
       ResultSet rs = null;
1031
       String returnfield = qspec.getSortedReturnFieldString();
1032

    
1033
       // query for finding the id from xml_returnfield
1034
       String query = "SELECT returnfield_id, usage_count FROM xml_returnfield "
1035
            + "WHERE returnfield_string LIKE ?";
1036
       logMetacat.info("ReturnField Query:" + query);
1037

    
1038
       try {
1039
           // prepare and run the query
1040
           pstmt = dbconn.prepareStatement(query);
1041
           pstmt.setString(1,returnfield);
1042
           dbconn.increaseUsageCount(1);
1043
           pstmt.execute();
1044
           rs = pstmt.getResultSet();
1045
           boolean tableHasRows = rs.next();
1046

    
1047
           // if record found then increase the usage count
1048
           // else insert a new record and get the id of the new record
1049
           if(tableHasRows){
1050
               // get the id
1051
               id = rs.getInt(1);
1052
               count = rs.getInt(2) + 1;
1053
               rs.close();
1054
               pstmt.close();
1055

    
1056
               // increase the usage count
1057
               query = "UPDATE xml_returnfield SET usage_count ='" + count
1058
                   + "' WHERE returnfield_id ='"+ id +"'";
1059
               logMetacat.info("ReturnField Table Update:"+ query);
1060

    
1061
               pstmt = dbconn.prepareStatement(query);
1062
               dbconn.increaseUsageCount(1);
1063
               pstmt.execute();
1064
               pstmt.close();
1065

    
1066
           } else {
1067
               rs.close();
1068
               pstmt.close();
1069

    
1070
               // insert a new record
1071
               query = "INSERT INTO xml_returnfield (returnfield_string, usage_count)"
1072
                   + "VALUES (?, '1')";
1073
               logMetacat.info("ReturnField Table Insert:"+ query);
1074
               pstmt = dbconn.prepareStatement(query);
1075
               pstmt.setString(1, returnfield);
1076
               dbconn.increaseUsageCount(1);
1077
               pstmt.execute();
1078
               pstmt.close();
1079

    
1080
               // get the id of the new record
1081
               query = "SELECT returnfield_id FROM xml_returnfield "
1082
                   + "WHERE returnfield_string LIKE ?";
1083
               logMetacat.info("ReturnField query after Insert:" + query);
1084
               pstmt = dbconn.prepareStatement(query);
1085
               pstmt.setString(1, returnfield);
1086

    
1087
               dbconn.increaseUsageCount(1);
1088
               pstmt.execute();
1089
               rs = pstmt.getResultSet();
1090
               if(rs.next()){
1091
                   id = rs.getInt(1);
1092
               } else {
1093
                   id = -1;
1094
               }
1095
               rs.close();
1096
               pstmt.close();
1097
           }
1098

    
1099
       } catch (Exception e){
1100
           logMetacat.error("Error getting id from xml_returnfield in "
1101
                                     + "DBQuery.getXmlReturnfieldsTableId: "
1102
                                     + e.getMessage());
1103
           id = -1;
1104
       }
1105

    
1106
       returnfield_id = id;
1107
       return count;
1108
   }
1109

    
1110

    
1111
    /*
1112
     * A method to add return field to return doclist hash table
1113
     */
1114
    private ResultDocumentSet addReturnfield(ResultDocumentSet docListResult,
1115
                                      QuerySpecification qspec,
1116
                                      String user, String[]groups,
1117
                                      DBConnection dbconn, boolean useXMLIndex )
1118
                                      throws Exception
1119
    {
1120
      PreparedStatement pstmt = null;
1121
      ResultSet rs = null;
1122
      String docid = null;
1123
      String fieldname = null;
1124
      String fielddata = null;
1125
      String relation = null;
1126

    
1127
      if (qspec.containsExtendedSQL())
1128
      {
1129
        qspec.setUserName(user);
1130
        qspec.setGroup(groups);
1131
        Vector extendedFields = new Vector(qspec.getReturnFieldList());
1132
        Vector results = new Vector();
1133
        Iterator keylist = docListResult.getDocids();
1134
        StringBuffer doclist = new StringBuffer();
1135
        Vector parentidList = new Vector();
1136
        Hashtable returnFieldValue = new Hashtable();
1137
        while (keylist.hasNext())
1138
        {
1139
          doclist.append("'");
1140
          doclist.append((String) keylist.next());
1141
          doclist.append("',");
1142
        }
1143
        if (doclist.length() > 0)
1144
        {
1145
          Hashtable controlPairs = new Hashtable();
1146
          doclist.deleteCharAt(doclist.length() - 1); //remove the last comma
1147
          boolean tableHasRows = false;
1148
          // check if user has permission to see the return field data
1149
          /*String accessControlSQL =
1150
                 qspec.printAccessControlSQLForReturnField(doclist.toString());
1151
          pstmt = dbconn.prepareStatement(accessControlSQL);
1152
          //increase dbconnection usage count
1153
          dbconn.increaseUsageCount(1);
1154
          pstmt.execute();
1155
          rs = pstmt.getResultSet();
1156
          tableHasRows = rs.next();
1157
          while (tableHasRows)
1158
          {
1159
            long startNodeId = rs.getLong(1);
1160
            long endNodeId = rs.getLong(2);
1161
            controlPairs.put(new Long(startNodeId), new Long(endNodeId));
1162
            tableHasRows = rs.next();
1163
          }*/
1164

    
1165
           /*double extendedAccessQueryEnd = System.currentTimeMillis() / 1000;
1166
           logMetacat.info( "Time for execute access extended query: "
1167
                          + (extendedAccessQueryEnd - extendedQueryStart));*/
1168

    
1169
           String extendedQuery =
1170
               qspec.printExtendedSQL(doclist.toString(), useXMLIndex);
1171
           logMetacat.info("Extended query: " + extendedQuery);
1172

    
1173
           if(extendedQuery != null){
1174
        	   double extendedQueryStart = System.currentTimeMillis() / 1000;
1175
               pstmt = dbconn.prepareStatement(extendedQuery);
1176
               //increase dbconnection usage count
1177
               dbconn.increaseUsageCount(1);
1178
               pstmt.execute();
1179
               rs = pstmt.getResultSet();
1180
               double extendedQueryEnd = System.currentTimeMillis() / 1000;
1181
               logMetacat.warn(
1182
                   "Time to execute extended query: "
1183
                   + (extendedQueryEnd - extendedQueryStart));
1184
               MetaCatUtil.writeDebugToFile(
1185
                       "Execute extended query "
1186
                       + (extendedQueryEnd - extendedQueryStart));
1187
               MetaCatUtil.writeDebugToDelimiteredFile(" "+ (extendedQueryEnd - extendedQueryStart), false);
1188
               tableHasRows = rs.next();
1189
               while (tableHasRows) {
1190
                   ReturnFieldValue returnValue = new ReturnFieldValue();
1191
                   docid = rs.getString(1).trim();
1192
                   fieldname = rs.getString(2);
1193
                   fielddata = rs.getString(3);
1194
                   fielddata = MetaCatUtil.normalize(fielddata);
1195
                   String parentId = rs.getString(4);
1196
                   StringBuffer value = new StringBuffer();
1197

    
1198
                   // if xml_index is used, there would be just one record per nodeid
1199
                   // as xml_index just keeps one entry for each path
1200
                   if (useXMLIndex || !containsKey(parentidList, parentId)) {
1201
                       // don't need to merger nodedata
1202
                       value.append("<param name=\"");
1203
                       value.append(fieldname);
1204
                       value.append("\">");
1205
                       value.append(fielddata);
1206
                       value.append("</param>");
1207
                       //set returnvalue
1208
                       returnValue.setDocid(docid);
1209
                       returnValue.setFieldValue(fielddata);
1210
                       returnValue.setXMLFieldValue(value.toString());
1211
                       // Store it in hastable
1212
                       putInArray(parentidList, parentId, returnValue);
1213
                   }
1214
                   else {
1215
                       // need to merge nodedata if they have same parent id and
1216
                       // node type is text
1217
                       fielddata = (String) ( (ReturnFieldValue)
1218
                                             getArrayValue(
1219
                           parentidList, parentId)).getFieldValue()
1220
                           + fielddata;
1221
                       value.append("<param name=\"");
1222
                       value.append(fieldname);
1223
                       value.append("\">");
1224
                       value.append(fielddata);
1225
                       value.append("</param>");
1226
                       returnValue.setDocid(docid);
1227
                       returnValue.setFieldValue(fielddata);
1228
                       returnValue.setXMLFieldValue(value.toString());
1229
                       // remove the old return value from paretnidList
1230
                       parentidList.remove(parentId);
1231
                       // store the new return value in parentidlit
1232
                       putInArray(parentidList, parentId, returnValue);
1233
                   }
1234
                   tableHasRows = rs.next();
1235
               } //while
1236
               rs.close();
1237
               pstmt.close();
1238

    
1239
               // put the merger node data info into doclistReult
1240
               Enumeration xmlFieldValue = (getElements(parentidList)).
1241
                   elements();
1242
               while (xmlFieldValue.hasMoreElements()) {
1243
                   ReturnFieldValue object =
1244
                       (ReturnFieldValue) xmlFieldValue.nextElement();
1245
                   docid = object.getDocid();
1246
                   if (docListResult.containsDocid(docid)) {
1247
                       String removedelement = (String) docListResult.
1248
                           remove(docid);
1249
                       docListResult.
1250
                           addResultDocument(new ResultDocument(docid,
1251
                               removedelement + object.getXMLFieldValue()));
1252
                   }
1253
                   else {
1254
                       docListResult.addResultDocument(
1255
                         new ResultDocument(docid, object.getXMLFieldValue()));
1256
                   }
1257
               } //while
1258
               double docListResultEnd = System.currentTimeMillis() / 1000;
1259
               logMetacat.warn(
1260
                   "Time to prepare ResultDocumentSet after"
1261
                   + " execute extended query: "
1262
                   + (docListResultEnd - extendedQueryEnd));
1263
           }
1264

    
1265
           // get attribures return
1266
           double startGetAttribute = System.currentTimeMillis()/1000;
1267
           docListResult = getAttributeValueForReturn(qspec,
1268
                           docListResult, doclist.toString(), useXMLIndex);
1269
           double endGetAttribute = System.currentTimeMillis()/1000;
1270
           logMetacat.warn(
1271
                   "Time to get attribute return value after"
1272
                   + " execute extended query: "
1273
                   + (endGetAttribute - startGetAttribute));
1274
           MetaCatUtil.writeDebugToFile(
1275
                   "Get attribute return field "
1276
                   + (endGetAttribute - startGetAttribute));
1277
           MetaCatUtil.writeDebugToDelimiteredFile(" "+ (endGetAttribute - startGetAttribute), false);
1278
           
1279
           
1280
       }//if doclist lenght is great than zero
1281

    
1282
     }//if has extended query
1283

    
1284
      return docListResult;
1285
    }//addReturnfield
1286

    
1287
    /*
1288
    * A method to add relationship to return doclist hash table
1289
    */
1290
   private ResultDocumentSet addRelationship(ResultDocumentSet docListResult,
1291
                                     QuerySpecification qspec,
1292
                                     DBConnection dbconn, boolean useXMLIndex )
1293
                                     throws Exception
1294
  {
1295
    PreparedStatement pstmt = null;
1296
    ResultSet rs = null;
1297
    StringBuffer document = null;
1298
    double startRelation = System.currentTimeMillis() / 1000;
1299
    Iterator docidkeys = docListResult.getDocids();
1300
    while (docidkeys.hasNext())
1301
    {
1302
      //String connstring =
1303
      // "metacat://"+util.getOption("server")+"?docid=";
1304
      String connstring = "%docid=";
1305
      String docidkey;
1306
      synchronized(docListResult)
1307
      {
1308
        docidkey = (String) docidkeys.next();
1309
      }
1310
      pstmt = dbconn.prepareStatement(QuerySpecification
1311
                      .printRelationSQL(docidkey));
1312
      pstmt.execute();
1313
      rs = pstmt.getResultSet();
1314
      boolean tableHasRows = rs.next();
1315
      while (tableHasRows)
1316
      {
1317
        String sub = rs.getString(1);
1318
        String rel = rs.getString(2);
1319
        String obj = rs.getString(3);
1320
        String subDT = rs.getString(4);
1321
        String objDT = rs.getString(5);
1322

    
1323
        document = new StringBuffer();
1324
        document.append("<triple>");
1325
        document.append("<subject>").append(MetaCatUtil.normalize(sub));
1326
        document.append("</subject>");
1327
        if (subDT != null)
1328
        {
1329
          document.append("<subjectdoctype>").append(subDT);
1330
          document.append("</subjectdoctype>");
1331
        }
1332
        document.append("<relationship>").append(MetaCatUtil.normalize(rel));
1333
        document.append("</relationship>");
1334
        document.append("<object>").append(MetaCatUtil.normalize(obj));
1335
        document.append("</object>");
1336
        if (objDT != null)
1337
        {
1338
          document.append("<objectdoctype>").append(objDT);
1339
          document.append("</objectdoctype>");
1340
        }
1341
        document.append("</triple>");
1342

    
1343
        String removedelement = (String) docListResult.get(docidkey);
1344
        docListResult.set(docidkey, removedelement+ document.toString());
1345
        tableHasRows = rs.next();
1346
      }//while
1347
      rs.close();
1348
      pstmt.close();
1349
      
1350
    }//while
1351
    double endRelation = System.currentTimeMillis() / 1000;
1352
    logMetacat.warn("Time to add relationship to return fields (part 3 in return fields): "
1353
                             + (endRelation - startRelation));
1354
    MetaCatUtil.writeDebugToFile("-----------------------------------------Add relationship to return field(part3 in return fields): "
1355
            + (endRelation - startRelation));
1356
    MetaCatUtil.writeDebugToDelimiteredFile(" "+ (endRelation - startRelation), false);
1357

    
1358
    return docListResult;
1359
  }//addRelation
1360

    
1361
  /**
1362
   * removes the <?xml version="1.0"?> tag from the beginning.  This takes a
1363
   * string as a param instead of a hashtable.
1364
   *
1365
   * @param xmlquery a string representing a query.
1366
   */
1367
   private  String transformQuery(String xmlquery)
1368
   {
1369
     xmlquery = xmlquery.trim();
1370
     int index = xmlquery.indexOf("?>");
1371
     if (index != -1)
1372
     {
1373
       return xmlquery.substring(index + 2, xmlquery.length());
1374
     }
1375
     else
1376
     {
1377
       return xmlquery;
1378
     }
1379
   }
1380

    
1381

    
1382
    /*
1383
     * A method to search if Vector contains a particular key string
1384
     */
1385
    private boolean containsKey(Vector parentidList, String parentId)
1386
    {
1387

    
1388
        Vector tempVector = null;
1389

    
1390
        for (int count = 0; count < parentidList.size(); count++) {
1391
            tempVector = (Vector) parentidList.get(count);
1392
            if (parentId.compareTo((String) tempVector.get(0)) == 0) { return true; }
1393
        }
1394
        return false;
1395
    }
1396

    
1397
    /*
1398
     * A method to put key and value in Vector
1399
     */
1400
    private void putInArray(Vector parentidList, String key,
1401
            ReturnFieldValue value)
1402
    {
1403

    
1404
        Vector tempVector = null;
1405

    
1406
        for (int count = 0; count < parentidList.size(); count++) {
1407
            tempVector = (Vector) parentidList.get(count);
1408

    
1409
            if (key.compareTo((String) tempVector.get(0)) == 0) {
1410
                tempVector.remove(1);
1411
                tempVector.add(1, value);
1412
                return;
1413
            }
1414
        }
1415

    
1416
        tempVector = new Vector();
1417
        tempVector.add(0, key);
1418
        tempVector.add(1, value);
1419
        parentidList.add(tempVector);
1420
        return;
1421
    }
1422

    
1423
    /*
1424
     * A method to get value in Vector given a key
1425
     */
1426
    private ReturnFieldValue getArrayValue(Vector parentidList, String key)
1427
    {
1428

    
1429
        Vector tempVector = null;
1430

    
1431
        for (int count = 0; count < parentidList.size(); count++) {
1432
            tempVector = (Vector) parentidList.get(count);
1433

    
1434
            if (key.compareTo((String) tempVector.get(0)) == 0) { return (ReturnFieldValue) tempVector
1435
                    .get(1); }
1436
        }
1437
        return null;
1438
    }
1439

    
1440
    /*
1441
     * A method to get enumeration of all values in Vector
1442
     */
1443
    private Vector getElements(Vector parentidList)
1444
    {
1445
        Vector enumVector = new Vector();
1446
        Vector tempVector = null;
1447

    
1448
        for (int count = 0; count < parentidList.size(); count++) {
1449
            tempVector = (Vector) parentidList.get(count);
1450

    
1451
            enumVector.add(tempVector.get(1));
1452
        }
1453
        return enumVector;
1454
    }
1455

    
1456
    /*
1457
     * A method to return search result after running a query which return
1458
     * field have attribue
1459
     */
1460
    private ResultDocumentSet getAttributeValueForReturn(QuerySpecification squery,
1461
            ResultDocumentSet docInformationList, String docList, boolean useXMLIndex)
1462
    {
1463
        StringBuffer XML = null;
1464
        String sql = null;
1465
        DBConnection dbconn = null;
1466
        PreparedStatement pstmt = null;
1467
        ResultSet rs = null;
1468
        int serialNumber = -1;
1469
        boolean tableHasRows = false;
1470

    
1471
        //check the parameter
1472
        if (squery == null || docList == null || docList.length() < 0) { return docInformationList; }
1473

    
1474
        // if has attribute as return field
1475
        if (squery.containsAttributeReturnField()) {
1476
            sql = squery.printAttributeQuery(docList, useXMLIndex);
1477
            try {
1478
                dbconn = DBConnectionPool
1479
                        .getDBConnection("DBQuery.getAttributeValue");
1480
                serialNumber = dbconn.getCheckOutSerialNumber();
1481
                pstmt = dbconn.prepareStatement(sql);
1482
                pstmt.execute();
1483
                rs = pstmt.getResultSet();
1484
                tableHasRows = rs.next();
1485
                while (tableHasRows) {
1486
                    String docid = rs.getString(1).trim();
1487
                    String fieldname = rs.getString(2);
1488
                    String fielddata = rs.getString(3);
1489
                    String attirbuteName = rs.getString(4);
1490
                    XML = new StringBuffer();
1491

    
1492
                    XML.append("<param name=\"");
1493
                    XML.append(fieldname);
1494
                    XML.append("/");
1495
                    XML.append(QuerySpecification.ATTRIBUTESYMBOL);
1496
                    XML.append(attirbuteName);
1497
                    XML.append("\">");
1498
                    XML.append(fielddata);
1499
                    XML.append("</param>");
1500
                    tableHasRows = rs.next();
1501

    
1502
                    if (docInformationList.containsDocid(docid)) {
1503
                        String removedelement = (String) docInformationList
1504
                                .remove(docid);
1505
                        docInformationList.put(docid, removedelement
1506
                                + XML.toString());
1507
                    } else {
1508
                        docInformationList.put(docid, XML.toString());
1509
                    }
1510
                }//while
1511
                rs.close();
1512
                pstmt.close();
1513
            } catch (Exception se) {
1514
                logMetacat.error(
1515
                        "Error in DBQuery.getAttributeValue1: "
1516
                                + se.getMessage());
1517
            } finally {
1518
                try {
1519
                    pstmt.close();
1520
                }//try
1521
                catch (SQLException sqlE) {
1522
                    logMetacat.error(
1523
                            "Error in DBQuery.getAttributeValue2: "
1524
                                    + sqlE.getMessage());
1525
                }//catch
1526
                finally {
1527
                    DBConnectionPool.returnDBConnection(dbconn, serialNumber);
1528
                }//finally
1529
            }//finally
1530
        }//if
1531
        return docInformationList;
1532

    
1533
    }
1534

    
1535
    /*
1536
     * A method to create a query to get owner's docid list
1537
     */
1538
    private String getOwnerQuery(String owner)
1539
    {
1540
        if (owner != null) {
1541
            owner = owner.toLowerCase();
1542
        }
1543
        StringBuffer self = new StringBuffer();
1544

    
1545
        self.append("SELECT docid,docname,doctype,");
1546
        self.append("date_created, date_updated, rev ");
1547
        self.append("FROM xml_documents WHERE docid IN (");
1548
        self.append("(");
1549
        self.append("SELECT DISTINCT docid FROM xml_nodes WHERE \n");
1550
        self.append("nodedata LIKE '%%%' ");
1551
        self.append(") \n");
1552
        self.append(") ");
1553
        self.append(" AND (");
1554
        self.append(" lower(user_owner) = '" + owner + "'");
1555
        self.append(") ");
1556
        return self.toString();
1557
    }
1558

    
1559
    /**
1560
     * format a structured query as an XML document that conforms to the
1561
     * pathquery.dtd and is appropriate for submission to the DBQuery
1562
     * structured query engine
1563
     *
1564
     * @param params The list of parameters that should be included in the
1565
     *            query
1566
     */
1567
    public static String createSQuery(Hashtable params)
1568
    {
1569
        StringBuffer query = new StringBuffer();
1570
        Enumeration elements;
1571
        Enumeration keys;
1572
        String filterDoctype = null;
1573
        String casesensitive = null;
1574
        String searchmode = null;
1575
        Object nextkey;
1576
        Object nextelement;
1577
        //add the xml headers
1578
        query.append("<?xml version=\"1.0\"?>\n");
1579
        query.append("<pathquery version=\"1.2\">\n");
1580

    
1581

    
1582

    
1583
        if (params.containsKey("meta_file_id")) {
1584
            query.append("<meta_file_id>");
1585
            query.append(((String[]) params.get("meta_file_id"))[0]);
1586
            query.append("</meta_file_id>");
1587
        }
1588

    
1589
        if (params.containsKey("returndoctype")) {
1590
            String[] returnDoctypes = ((String[]) params.get("returndoctype"));
1591
            for (int i = 0; i < returnDoctypes.length; i++) {
1592
                String doctype = (String) returnDoctypes[i];
1593

    
1594
                if (!doctype.equals("any") && !doctype.equals("ANY")
1595
                        && !doctype.equals("")) {
1596
                    query.append("<returndoctype>").append(doctype);
1597
                    query.append("</returndoctype>");
1598
                }
1599
            }
1600
        }
1601

    
1602
        if (params.containsKey("filterdoctype")) {
1603
            String[] filterDoctypes = ((String[]) params.get("filterdoctype"));
1604
            for (int i = 0; i < filterDoctypes.length; i++) {
1605
                query.append("<filterdoctype>").append(filterDoctypes[i]);
1606
                query.append("</filterdoctype>");
1607
            }
1608
        }
1609

    
1610
        if (params.containsKey("returnfield")) {
1611
            String[] returnfield = ((String[]) params.get("returnfield"));
1612
            for (int i = 0; i < returnfield.length; i++) {
1613
                query.append("<returnfield>").append(returnfield[i]);
1614
                query.append("</returnfield>");
1615
            }
1616
        }
1617

    
1618
        if (params.containsKey("owner")) {
1619
            String[] owner = ((String[]) params.get("owner"));
1620
            for (int i = 0; i < owner.length; i++) {
1621
                query.append("<owner>").append(owner[i]);
1622
                query.append("</owner>");
1623
            }
1624
        }
1625

    
1626
        if (params.containsKey("site")) {
1627
            String[] site = ((String[]) params.get("site"));
1628
            for (int i = 0; i < site.length; i++) {
1629
                query.append("<site>").append(site[i]);
1630
                query.append("</site>");
1631
            }
1632
        }
1633

    
1634
        //allows the dynamic switching of boolean operators
1635
        if (params.containsKey("operator")) {
1636
            query.append("<querygroup operator=\""
1637
                    + ((String[]) params.get("operator"))[0] + "\">");
1638
        } else { //the default operator is UNION
1639
            query.append("<querygroup operator=\"UNION\">");
1640
        }
1641

    
1642
        if (params.containsKey("casesensitive")) {
1643
            casesensitive = ((String[]) params.get("casesensitive"))[0];
1644
        } else {
1645
            casesensitive = "false";
1646
        }
1647

    
1648
        if (params.containsKey("searchmode")) {
1649
            searchmode = ((String[]) params.get("searchmode"))[0];
1650
        } else {
1651
            searchmode = "contains";
1652
        }
1653

    
1654
        //anyfield is a special case because it does a
1655
        //free text search. It does not have a <pathexpr>
1656
        //tag. This allows for a free text search within the structured
1657
        //query. This is useful if the INTERSECT operator is used.
1658
        if (params.containsKey("anyfield")) {
1659
            String[] anyfield = ((String[]) params.get("anyfield"));
1660
            //allow for more than one value for anyfield
1661
            for (int i = 0; i < anyfield.length; i++) {
1662
                if (!anyfield[i].equals("")) {
1663
                    query.append("<queryterm casesensitive=\"" + casesensitive
1664
                            + "\" " + "searchmode=\"" + searchmode
1665
                            + "\"><value>" + anyfield[i]
1666
                            + "</value></queryterm>");
1667
                }
1668
            }
1669
        }
1670

    
1671
        //this while loop finds the rest of the parameters
1672
        //and attempts to query for the field specified
1673
        //by the parameter.
1674
        elements = params.elements();
1675
        keys = params.keys();
1676
        while (keys.hasMoreElements() && elements.hasMoreElements()) {
1677
            nextkey = keys.nextElement();
1678
            nextelement = elements.nextElement();
1679

    
1680
            //make sure we aren't querying for any of these
1681
            //parameters since the are already in the query
1682
            //in one form or another.
1683
            Vector ignoredParams = new Vector();
1684
            ignoredParams.add("returndoctype");
1685
            ignoredParams.add("filterdoctype");
1686
            ignoredParams.add("action");
1687
            ignoredParams.add("qformat");
1688
            ignoredParams.add("anyfield");
1689
            ignoredParams.add("returnfield");
1690
            ignoredParams.add("owner");
1691
            ignoredParams.add("site");
1692
            ignoredParams.add("operator");
1693
            ignoredParams.add("sessionid");
1694
            ignoredParams.add("pagesize");
1695
            ignoredParams.add("pagestart");
1696

    
1697
            // Also ignore parameters listed in the properties file
1698
            // so that they can be passed through to stylesheets
1699
            String paramsToIgnore = MetaCatUtil
1700
                    .getOption("query.ignored.params");
1701
            StringTokenizer st = new StringTokenizer(paramsToIgnore, ",");
1702
            while (st.hasMoreTokens()) {
1703
                ignoredParams.add(st.nextToken());
1704
            }
1705
            if (!ignoredParams.contains(nextkey.toString())) {
1706
                //allow for more than value per field name
1707
                for (int i = 0; i < ((String[]) nextelement).length; i++) {
1708
                    if (!((String[]) nextelement)[i].equals("")) {
1709
                        query.append("<queryterm casesensitive=\""
1710
                                + casesensitive + "\" " + "searchmode=\""
1711
                                + searchmode + "\">" + "<value>" +
1712
                                //add the query value
1713
                                ((String[]) nextelement)[i]
1714
                                + "</value><pathexpr>" +
1715
                                //add the path to query by
1716
                                nextkey.toString() + "</pathexpr></queryterm>");
1717
                    }
1718
                }
1719
            }
1720
        }
1721
        query.append("</querygroup></pathquery>");
1722
        //append on the end of the xml and return the result as a string
1723
        return query.toString();
1724
    }
1725

    
1726
    /**
1727
     * format a simple free-text value query as an XML document that conforms
1728
     * to the pathquery.dtd and is appropriate for submission to the DBQuery
1729
     * structured query engine
1730
     *
1731
     * @param value the text string to search for in the xml catalog
1732
     * @param doctype the type of documents to include in the result set -- use
1733
     *            "any" or "ANY" for unfiltered result sets
1734
     */
1735
    public static String createQuery(String value, String doctype)
1736
    {
1737
        StringBuffer xmlquery = new StringBuffer();
1738
        xmlquery.append("<?xml version=\"1.0\"?>\n");
1739
        xmlquery.append("<pathquery version=\"1.0\">");
1740

    
1741
        if (!doctype.equals("any") && !doctype.equals("ANY")) {
1742
            xmlquery.append("<returndoctype>");
1743
            xmlquery.append(doctype).append("</returndoctype>");
1744
        }
1745

    
1746
        xmlquery.append("<querygroup operator=\"UNION\">");
1747
        //chad added - 8/14
1748
        //the if statement allows a query to gracefully handle a null
1749
        //query. Without this if a nullpointerException is thrown.
1750
        if (!value.equals("")) {
1751
            xmlquery.append("<queryterm casesensitive=\"false\" ");
1752
            xmlquery.append("searchmode=\"contains\">");
1753
            xmlquery.append("<value>").append(value).append("</value>");
1754
            xmlquery.append("</queryterm>");
1755
        }
1756
        xmlquery.append("</querygroup>");
1757
        xmlquery.append("</pathquery>");
1758

    
1759
        return (xmlquery.toString());
1760
    }
1761

    
1762
    /**
1763
     * format a simple free-text value query as an XML document that conforms
1764
     * to the pathquery.dtd and is appropriate for submission to the DBQuery
1765
     * structured query engine
1766
     *
1767
     * @param value the text string to search for in the xml catalog
1768
     */
1769
    public static String createQuery(String value)
1770
    {
1771
        return createQuery(value, "any");
1772
    }
1773

    
1774
    /**
1775
     * Check for "READ" permission on @docid for @user and/or @group from DB
1776
     * connection
1777
     */
1778
    private boolean hasPermission(String user, String[] groups, String docid)
1779
            throws SQLException, Exception
1780
    {
1781
        // Check for READ permission on @docid for @user and/or @groups
1782
        PermissionController controller = new PermissionController(docid);
1783
        return controller.hasPermission(user, groups,
1784
                AccessControlInterface.READSTRING);
1785
    }
1786

    
1787
    /**
1788
     * Get all docIds list for a data packadge
1789
     *
1790
     * @param dataPackageDocid, the string in docId field of xml_relation table
1791
     */
1792
    private Vector getCurrentDocidListForDataPackage(String dataPackageDocid)
1793
    {
1794
        DBConnection dbConn = null;
1795
        int serialNumber = -1;
1796
        Vector docIdList = new Vector();//return value
1797
        PreparedStatement pStmt = null;
1798
        ResultSet rs = null;
1799
        String docIdInSubjectField = null;
1800
        String docIdInObjectField = null;
1801

    
1802
        // Check the parameter
1803
        if (dataPackageDocid == null || dataPackageDocid.equals("")) { return docIdList; }//if
1804

    
1805
        //the query stirng
1806
        String query = "SELECT subject, object from xml_relation where docId = ?";
1807
        try {
1808
            dbConn = DBConnectionPool
1809
                    .getDBConnection("DBQuery.getCurrentDocidListForDataPackage");
1810
            serialNumber = dbConn.getCheckOutSerialNumber();
1811
            pStmt = dbConn.prepareStatement(query);
1812
            //bind the value to query
1813
            pStmt.setString(1, dataPackageDocid);
1814

    
1815
            //excute the query
1816
            pStmt.execute();
1817
            //get the result set
1818
            rs = pStmt.getResultSet();
1819
            //process the result
1820
            while (rs.next()) {
1821
                //In order to get the whole docIds in a data packadge,
1822
                //we need to put the docIds of subject and object field in
1823
                // xml_relation
1824
                //into the return vector
1825
                docIdInSubjectField = rs.getString(1);//the result docId in
1826
                                                      // subject field
1827
                docIdInObjectField = rs.getString(2);//the result docId in
1828
                                                     // object field
1829

    
1830
                //don't put the duplicate docId into the vector
1831
                if (!docIdList.contains(docIdInSubjectField)) {
1832
                    docIdList.add(docIdInSubjectField);
1833
                }
1834

    
1835
                //don't put the duplicate docId into the vector
1836
                if (!docIdList.contains(docIdInObjectField)) {
1837
                    docIdList.add(docIdInObjectField);
1838
                }
1839
            }//while
1840
            //close the pStmt
1841
            pStmt.close();
1842
        }//try
1843
        catch (SQLException e) {
1844
            logMetacat.error("Error in getDocidListForDataPackage: "
1845
                    + e.getMessage());
1846
        }//catch
1847
        finally {
1848
            try {
1849
                pStmt.close();
1850
            }//try
1851
            catch (SQLException ee) {
1852
                logMetacat.error(
1853
                        "Error in getDocidListForDataPackage: "
1854
                                + ee.getMessage());
1855
            }//catch
1856
            finally {
1857
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1858
            }//fianlly
1859
        }//finally
1860
        return docIdList;
1861
    }//getCurrentDocidListForDataPackadge()
1862

    
1863
    /**
1864
     * Get all docIds list for a data packadge
1865
     *
1866
     * @param dataPackageDocid, the string in docId field of xml_relation table
1867
     */
1868
    private Vector getOldVersionDocidListForDataPackage(String dataPackageDocidWithRev)
1869
    {
1870

    
1871
        Vector docIdList = new Vector();//return value
1872
        Vector tripleList = null;
1873
        String xml = null;
1874

    
1875
        // Check the parameter
1876
        if (dataPackageDocidWithRev == null || dataPackageDocidWithRev.equals("")) { return docIdList; }//if
1877

    
1878
        try {
1879
            //initial a documentImpl object
1880
            DocumentImpl packageDocument = new DocumentImpl(dataPackageDocidWithRev);
1881
            //transfer to documentImpl object to string
1882
            xml = packageDocument.toString();
1883

    
1884
            //create a tripcollection object
1885
            TripleCollection tripleForPackage = new TripleCollection(
1886
                    new StringReader(xml));
1887
            //get the vetor of triples
1888
            tripleList = tripleForPackage.getCollection();
1889

    
1890
            for (int i = 0; i < tripleList.size(); i++) {
1891
                //put subject docid into docIdlist without duplicate
1892
                if (!docIdList.contains(((Triple) tripleList.elementAt(i))
1893
                        .getSubject())) {
1894
                    //put subject docid into docIdlist
1895
                    docIdList.add(((Triple) tripleList.get(i)).getSubject());
1896
                }
1897
                //put object docid into docIdlist without duplicate
1898
                if (!docIdList.contains(((Triple) tripleList.elementAt(i))
1899
                        .getObject())) {
1900
                    docIdList.add(((Triple) (tripleList.get(i))).getObject());
1901
                }
1902
            }//for
1903
        }//try
1904
        catch (Exception e) {
1905
            logMetacat.error("Error in getOldVersionAllDocumentImpl: "
1906
                    + e.getMessage());
1907
        }//catch
1908

    
1909
        // return result
1910
        return docIdList;
1911
    }//getDocidListForPackageInXMLRevisions()
1912

    
1913
    /**
1914
     * Check if the docId is a data packadge id. If the id is a data packadage
1915
     * id, it should be store in the docId fields in xml_relation table. So we
1916
     * can use a query to get the entries which the docId equals the given
1917
     * value. If the result is null. The docId is not a packadge id. Otherwise,
1918
     * it is.
1919
     *
1920
     * @param docId, the id need to be checked
1921
     */
1922
    private boolean isDataPackageId(String docId)
1923
    {
1924
        boolean result = false;
1925
        PreparedStatement pStmt = null;
1926
        ResultSet rs = null;
1927
        String query = "SELECT docId from xml_relation where docId = ?";
1928
        DBConnection dbConn = null;
1929
        int serialNumber = -1;
1930
        try {
1931
            dbConn = DBConnectionPool
1932
                    .getDBConnection("DBQuery.isDataPackageId");
1933
            serialNumber = dbConn.getCheckOutSerialNumber();
1934
            pStmt = dbConn.prepareStatement(query);
1935
            //bind the value to query
1936
            pStmt.setString(1, docId);
1937
            //execute the query
1938
            pStmt.execute();
1939
            rs = pStmt.getResultSet();
1940
            //process the result
1941
            if (rs.next()) //There are some records for the id in docId fields
1942
            {
1943
                result = true;//It is a data packadge id
1944
            }
1945
            pStmt.close();
1946
        }//try
1947
        catch (SQLException e) {
1948
            logMetacat.error("Error in isDataPackageId: "
1949
                    + e.getMessage());
1950
        } finally {
1951
            try {
1952
                pStmt.close();
1953
            }//try
1954
            catch (SQLException ee) {
1955
                logMetacat.error("Error in isDataPackageId: "
1956
                        + ee.getMessage());
1957
            }//catch
1958
            finally {
1959
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1960
            }//finally
1961
        }//finally
1962
        return result;
1963
    }//isDataPackageId()
1964

    
1965
    /**
1966
     * Check if the user has the permission to export data package
1967
     *
1968
     * @param conn, the connection
1969
     * @param docId, the id need to be checked
1970
     * @param user, the name of user
1971
     * @param groups, the user's group
1972
     */
1973
    private boolean hasPermissionToExportPackage(String docId, String user,
1974
            String[] groups) throws Exception
1975
    {
1976
        //DocumentImpl doc=new DocumentImpl(conn,docId);
1977
        return DocumentImpl.hasReadPermission(user, groups, docId);
1978
    }
1979

    
1980
    /**
1981
     * Get the current Rev for a docid in xml_documents table
1982
     *
1983
     * @param docId, the id need to get version numb If the return value is -5,
1984
     *            means no value in rev field for this docid
1985
     */
1986
    private int getCurrentRevFromXMLDoumentsTable(String docId)
1987
            throws SQLException
1988
    {
1989
        int rev = -5;
1990
        PreparedStatement pStmt = null;
1991
        ResultSet rs = null;
1992
        String query = "SELECT rev from xml_documents where docId = ?";
1993
        DBConnection dbConn = null;
1994
        int serialNumber = -1;
1995
        try {
1996
            dbConn = DBConnectionPool
1997
                    .getDBConnection("DBQuery.getCurrentRevFromXMLDocumentsTable");
1998
            serialNumber = dbConn.getCheckOutSerialNumber();
1999
            pStmt = dbConn.prepareStatement(query);
2000
            //bind the value to query
2001
            pStmt.setString(1, docId);
2002
            //execute the query
2003
            pStmt.execute();
2004
            rs = pStmt.getResultSet();
2005
            //process the result
2006
            if (rs.next()) //There are some records for rev
2007
            {
2008
                rev = rs.getInt(1);
2009
                ;//It is the version for given docid
2010
            } else {
2011
                rev = -5;
2012
            }
2013

    
2014
        }//try
2015
        catch (SQLException e) {
2016
            logMetacat.error(
2017
                    "Error in getCurrentRevFromXMLDoumentsTable: "
2018
                            + e.getMessage());
2019
            throw e;
2020
        }//catch
2021
        finally {
2022
            try {
2023
                pStmt.close();
2024
            }//try
2025
            catch (SQLException ee) {
2026
                logMetacat.error(
2027
                        "Error in getCurrentRevFromXMLDoumentsTable: "
2028
                                + ee.getMessage());
2029
            }//catch
2030
            finally {
2031
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
2032
            }//finally
2033
        }//finally
2034
        return rev;
2035
    }//getCurrentRevFromXMLDoumentsTable
2036

    
2037
    /**
2038
     * put a doc into a zip output stream
2039
     *
2040
     * @param docImpl, docmentImpl object which will be sent to zip output
2041
     *            stream
2042
     * @param zipOut, zip output stream which the docImpl will be put
2043
     * @param packageZipEntry, the zip entry name for whole package
2044
     */
2045
    private void addDocToZipOutputStream(DocumentImpl docImpl,
2046
            ZipOutputStream zipOut, String packageZipEntry)
2047
            throws ClassNotFoundException, IOException, SQLException,
2048
            McdbException, Exception
2049
    {
2050
        byte[] byteString = null;
2051
        ZipEntry zEntry = null;
2052

    
2053
        byteString = docImpl.toString().getBytes();
2054
        //use docId as the zip entry's name
2055
        zEntry = new ZipEntry(packageZipEntry + "/metadata/"
2056
                + docImpl.getDocID());
2057
        zEntry.setSize(byteString.length);
2058
        zipOut.putNextEntry(zEntry);
2059
        zipOut.write(byteString, 0, byteString.length);
2060
        zipOut.closeEntry();
2061

    
2062
    }//addDocToZipOutputStream()
2063

    
2064
    /**
2065
     * Transfer a docid vetor to a documentImpl vector. The documentImpl vetor
2066
     * only inlcudes current version. If a DocumentImple object couldn't find
2067
     * for a docid, then the String of this docid was added to vetor rather
2068
     * than DocumentImple object.
2069
     *
2070
     * @param docIdList, a vetor hold a docid list for a data package. In
2071
     *            docid, there is not version number in it.
2072
     */
2073

    
2074
    private Vector getCurrentAllDocumentImpl(Vector docIdList)
2075
            throws McdbException, Exception
2076
    {
2077
        //Connection dbConn=null;
2078
        Vector documentImplList = new Vector();
2079
        int rev = 0;
2080

    
2081
        // Check the parameter
2082
        if (docIdList.isEmpty()) { return documentImplList; }//if
2083

    
2084
        //for every docid in vector
2085
        for (int i = 0; i < docIdList.size(); i++) {
2086
            try {
2087
                //get newest version for this docId
2088
                rev = getCurrentRevFromXMLDoumentsTable((String) docIdList
2089
                        .elementAt(i));
2090

    
2091
                // There is no record for this docId in xml_documents table
2092
                if (rev == -5) {
2093
                    // Rather than put DocumentImple object, put a String
2094
                    // Object(docid)
2095
                    // into the documentImplList
2096
                    documentImplList.add((String) docIdList.elementAt(i));
2097
                    // Skip other code
2098
                    continue;
2099
                }
2100

    
2101
                String docidPlusVersion = ((String) docIdList.elementAt(i))
2102
                        + MetaCatUtil.getOption("accNumSeparator") + rev;
2103

    
2104
                //create new documentImpl object
2105
                DocumentImpl documentImplObject = new DocumentImpl(
2106
                        docidPlusVersion);
2107
                //add them to vector
2108
                documentImplList.add(documentImplObject);
2109
            }//try
2110
            catch (Exception e) {
2111
                logMetacat.error("Error in getCurrentAllDocumentImpl: "
2112
                        + e.getMessage());
2113
                // continue the for loop
2114
                continue;
2115
            }
2116
        }//for
2117
        return documentImplList;
2118
    }
2119

    
2120
    /**
2121
     * Transfer a docid vetor to a documentImpl vector. If a DocumentImple
2122
     * object couldn't find for a docid, then the String of this docid was
2123
     * added to vetor rather than DocumentImple object.
2124
     *
2125
     * @param docIdList, a vetor hold a docid list for a data package. In
2126
     *            docid, t here is version number in it.
2127
     */
2128
    private Vector getOldVersionAllDocumentImpl(Vector docIdList)
2129
    {
2130
        //Connection dbConn=null;
2131
        Vector documentImplList = new Vector();
2132
        String siteCode = null;
2133
        String uniqueId = null;
2134
        int rev = 0;
2135

    
2136
        // Check the parameter
2137
        if (docIdList.isEmpty()) { return documentImplList; }//if
2138

    
2139
        //for every docid in vector
2140
        for (int i = 0; i < docIdList.size(); i++) {
2141

    
2142
            String docidPlusVersion = (String) (docIdList.elementAt(i));
2143

    
2144
            try {
2145
                //create new documentImpl object
2146
                DocumentImpl documentImplObject = new DocumentImpl(
2147
                        docidPlusVersion);
2148
                //add them to vector
2149
                documentImplList.add(documentImplObject);
2150
            }//try
2151
            catch (McdbDocNotFoundException notFoundE) {
2152
                logMetacat.error(
2153
                        "Error in DBQuery.getOldVersionAllDocument" + "Imple"
2154
                                + notFoundE.getMessage());
2155
                // Rather than add a DocumentImple object into vetor, a String
2156
                // object
2157
                // - the doicd was added to the vector
2158
                documentImplList.add(docidPlusVersion);
2159
                // Continue the for loop
2160
                continue;
2161
            }//catch
2162
            catch (Exception e) {
2163
                logMetacat.error(
2164
                        "Error in DBQuery.getOldVersionAllDocument" + "Imple"
2165
                                + e.getMessage());
2166
                // Continue the for loop
2167
                continue;
2168
            }//catch
2169

    
2170
        }//for
2171
        return documentImplList;
2172
    }//getOldVersionAllDocumentImple
2173

    
2174
    /**
2175
     * put a data file into a zip output stream
2176
     *
2177
     * @param docImpl, docmentImpl object which will be sent to zip output
2178
     *            stream
2179
     * @param zipOut, the zip output stream which the docImpl will be put
2180
     * @param packageZipEntry, the zip entry name for whole package
2181
     */
2182
    private void addDataFileToZipOutputStream(DocumentImpl docImpl,
2183
            ZipOutputStream zipOut, String packageZipEntry)
2184
            throws ClassNotFoundException, IOException, SQLException,
2185
            McdbException, Exception
2186
    {
2187
        byte[] byteString = null;
2188
        ZipEntry zEntry = null;
2189
        // this is data file; add file to zip
2190
        String filePath = MetaCatUtil.getOption("datafilepath");
2191
        if (!filePath.endsWith("/")) {
2192
            filePath += "/";
2193
        }
2194
        String fileName = filePath + docImpl.getDocID();
2195
        zEntry = new ZipEntry(packageZipEntry + "/data/" + docImpl.getDocID());
2196
        zipOut.putNextEntry(zEntry);
2197
        FileInputStream fin = null;
2198
        try {
2199
            fin = new FileInputStream(fileName);
2200
            byte[] buf = new byte[4 * 1024]; // 4K buffer
2201
            int b = fin.read(buf);
2202
            while (b != -1) {
2203
                zipOut.write(buf, 0, b);
2204
                b = fin.read(buf);
2205
            }//while
2206
            zipOut.closeEntry();
2207
        }//try
2208
        catch (IOException ioe) {
2209
            logMetacat.error("There is an exception: "
2210
                    + ioe.getMessage());
2211
        }//catch
2212
    }//addDataFileToZipOutputStream()
2213

    
2214
    /**
2215
     * create a html summary for data package and put it into zip output stream
2216
     *
2217
     * @param docImplList, the documentImpl ojbects in data package
2218
     * @param zipOut, the zip output stream which the html should be put
2219
     * @param packageZipEntry, the zip entry name for whole package
2220
     */
2221
    private void addHtmlSummaryToZipOutputStream(Vector docImplList,
2222
            ZipOutputStream zipOut, String packageZipEntry) throws Exception
2223
    {
2224
        StringBuffer htmlDoc = new StringBuffer();
2225
        ZipEntry zEntry = null;
2226
        byte[] byteString = null;
2227
        InputStream source;
2228
        DBTransform xmlToHtml;
2229

    
2230
        //create a DBTransform ojbect
2231
        xmlToHtml = new DBTransform();
2232
        //head of html
2233
        htmlDoc.append("<html><head></head><body>");
2234
        for (int i = 0; i < docImplList.size(); i++) {
2235
            // If this String object, this means it is missed data file
2236
            if ((((docImplList.elementAt(i)).getClass()).toString())
2237
                    .equals("class java.lang.String")) {
2238

    
2239
                htmlDoc.append("<a href=\"");
2240
                String dataFileid = (String) docImplList.elementAt(i);
2241
                htmlDoc.append("./data/").append(dataFileid).append("\">");
2242
                htmlDoc.append("Data File: ");
2243
                htmlDoc.append(dataFileid).append("</a><br>");
2244
                htmlDoc.append("<br><hr><br>");
2245

    
2246
            }//if
2247
            else if ((((DocumentImpl) docImplList.elementAt(i)).getDoctype())
2248
                    .compareTo("BIN") != 0) { //this is an xml file so we can
2249
                                              // transform it.
2250
                //transform each file individually then concatenate all of the
2251
                //transformations together.
2252

    
2253
                //for metadata xml title
2254
                htmlDoc.append("<h2>");
2255
                htmlDoc.append(((DocumentImpl) docImplList.elementAt(i))
2256
                        .getDocID());
2257
                //htmlDoc.append(".");
2258
                //htmlDoc.append(((DocumentImpl)docImplList.elementAt(i)).getRev());
2259
                htmlDoc.append("</h2>");
2260
                //do the actual transform
2261
                StringWriter docString = new StringWriter();
2262
                xmlToHtml.transformXMLDocument(((DocumentImpl) docImplList
2263
                        .elementAt(i)).toString(), "-//NCEAS//eml-generic//EN",
2264
                        "-//W3C//HTML//EN", "html", docString);
2265
                htmlDoc.append(docString.toString());
2266
                htmlDoc.append("<br><br><hr><br><br>");
2267
            }//if
2268
            else { //this is a data file so we should link to it in the html
2269
                htmlDoc.append("<a href=\"");
2270
                String dataFileid = ((DocumentImpl) docImplList.elementAt(i))
2271
                        .getDocID();
2272
                htmlDoc.append("./data/").append(dataFileid).append("\">");
2273
                htmlDoc.append("Data File: ");
2274
                htmlDoc.append(dataFileid).append("</a><br>");
2275
                htmlDoc.append("<br><hr><br>");
2276
            }//else
2277
        }//for
2278
        htmlDoc.append("</body></html>");
2279
        byteString = htmlDoc.toString().getBytes();
2280
        zEntry = new ZipEntry(packageZipEntry + "/metadata.html");
2281
        zEntry.setSize(byteString.length);
2282
        zipOut.putNextEntry(zEntry);
2283
        zipOut.write(byteString, 0, byteString.length);
2284
        zipOut.closeEntry();
2285
        //dbConn.close();
2286

    
2287
    }//addHtmlSummaryToZipOutputStream
2288

    
2289
    /**
2290
     * put a data packadge into a zip output stream
2291
     *
2292
     * @param docId, which the user want to put into zip output stream,it has version
2293
     * @param out, a servletoutput stream which the zip output stream will be
2294
     *            put
2295
     * @param user, the username of the user
2296
     * @param groups, the group of the user
2297
     */
2298
    public ZipOutputStream getZippedPackage(String docIdString,
2299
            ServletOutputStream out, String user, String[] groups,
2300
            String passWord) throws ClassNotFoundException, IOException,
2301
            SQLException, McdbException, NumberFormatException, Exception
2302
    {
2303
        ZipOutputStream zOut = null;
2304
        String elementDocid = null;
2305
        DocumentImpl docImpls = null;
2306
        //Connection dbConn = null;
2307
        Vector docIdList = new Vector();
2308
        Vector documentImplList = new Vector();
2309
        Vector htmlDocumentImplList = new Vector();
2310
        String packageId = null;
2311
        String rootName = "package";//the package zip entry name
2312

    
2313
        String docId = null;
2314
        int version = -5;
2315
        // Docid without revision
2316
        docId = MetaCatUtil.getDocIdFromString(docIdString);
2317
        // revision number
2318
        version = MetaCatUtil.getVersionFromString(docIdString);
2319

    
2320
        //check if the reqused docId is a data package id
2321
        if (!isDataPackageId(docId)) {
2322

    
2323
            /*
2324
             * Exception e = new Exception("The request the doc id "
2325
             * +docIdString+ " is not a data package id");
2326
             */
2327

    
2328
            //CB 1/6/03: if the requested docid is not a datapackage, we just
2329
            // zip
2330
            //up the single document and return the zip file.
2331
            if (!hasPermissionToExportPackage(docId, user, groups)) {
2332

    
2333
                Exception e = new Exception("User " + user
2334
                        + " does not have permission"
2335
                        + " to export the data package " + docIdString);
2336
                throw e;
2337
            }
2338

    
2339
            docImpls = new DocumentImpl(docIdString);
2340
            //checking if the user has the permission to read the documents
2341
            if (DocumentImpl.hasReadPermission(user, groups, docImpls
2342
                    .getDocID())) {
2343
                zOut = new ZipOutputStream(out);
2344
                //if the docImpls is metadata
2345
                if ((docImpls.getDoctype()).compareTo("BIN") != 0) {
2346
                    //add metadata into zip output stream
2347
                    addDocToZipOutputStream(docImpls, zOut, rootName);
2348
                }//if
2349
                else {
2350
                    //it is data file
2351
                    addDataFileToZipOutputStream(docImpls, zOut, rootName);
2352
                    htmlDocumentImplList.add(docImpls);
2353
                }//else
2354
            }//if
2355

    
2356
            zOut.finish(); //terminate the zip file
2357
            return zOut;
2358
        }
2359
        // Check the permission of user
2360
        else if (!hasPermissionToExportPackage(docId, user, groups)) {
2361

    
2362
            Exception e = new Exception("User " + user
2363
                    + " does not have permission"
2364
                    + " to export the data package " + docIdString);
2365
            throw e;
2366
        } else //it is a packadge id
2367
        {
2368
            //store the package id
2369
            packageId = docId;
2370
            //get current version in database
2371
            int currentVersion = getCurrentRevFromXMLDoumentsTable(packageId);
2372
            //If it is for current version (-1 means user didn't specify
2373
            // revision)
2374
            if ((version == -1) || version == currentVersion) {
2375
                //get current version number
2376
                version = currentVersion;
2377
                //get package zip entry name
2378
                //it should be docId.revsion.package
2379
                rootName = packageId + MetaCatUtil.getOption("accNumSeparator")
2380
                        + version + MetaCatUtil.getOption("accNumSeparator")
2381
                        + "package";
2382
                //get the whole id list for data packadge
2383
                docIdList = getCurrentDocidListForDataPackage(packageId);
2384
                //get the whole documentImple object
2385
                documentImplList = getCurrentAllDocumentImpl(docIdList);
2386

    
2387
            }//if
2388
            else if (version > currentVersion || version < -1) {
2389
                throw new Exception("The user specified docid: " + docId + "."
2390
                        + version + " doesn't exist");
2391
            }//else if
2392
            else //for an old version
2393
            {
2394

    
2395
                rootName = docIdString
2396
                        + MetaCatUtil.getOption("accNumSeparator") + "package";
2397
                //get the whole id list for data packadge
2398
                docIdList = getOldVersionDocidListForDataPackage(docIdString);
2399

    
2400
                //get the whole documentImple object
2401
                documentImplList = getOldVersionAllDocumentImpl(docIdList);
2402
            }//else
2403

    
2404
            // Make sure documentImplist is not empty
2405
            if (documentImplList.isEmpty()) { throw new Exception(
2406
                    "Couldn't find component for data package: " + packageId); }//if
2407

    
2408
            zOut = new ZipOutputStream(out);
2409
            //put every element into zip output stream
2410
            for (int i = 0; i < documentImplList.size(); i++) {
2411
                // if the object in the vetor is String, this means we couldn't
2412
                // find
2413
                // the document locally, we need find it remote
2414
                if ((((documentImplList.elementAt(i)).getClass()).toString())
2415
                        .equals("class java.lang.String")) {
2416
                    // Get String object from vetor
2417
                    String documentId = (String) documentImplList.elementAt(i);
2418
                    logMetacat.info("docid: " + documentId);
2419
                    // Get doicd without revision
2420
                    String docidWithoutRevision = MetaCatUtil
2421
                            .getDocIdFromString(documentId);
2422
                    logMetacat.info("docidWithoutRevsion: "
2423
                            + docidWithoutRevision);
2424
                    // Get revision
2425
                    String revision = MetaCatUtil
2426
                            .getRevisionStringFromString(documentId);
2427
                    logMetacat.info("revsion from docIdentifier: "
2428
                            + revision);
2429
                    // Zip entry string
2430
                    String zipEntryPath = rootName + "/data/";
2431
                    // Create a RemoteDocument object
2432
                    RemoteDocument remoteDoc = new RemoteDocument(
2433
                            docidWithoutRevision, revision, user, passWord,
2434
                            zipEntryPath);
2435
                    // Here we only read data file from remote metacat
2436
                    String docType = remoteDoc.getDocType();
2437
                    if (docType != null) {
2438
                        if (docType.equals("BIN")) {
2439
                            // Put remote document to zip output
2440
                            remoteDoc.readDocumentFromRemoteServerByZip(zOut);
2441
                            // Add String object to htmlDocumentImplList
2442
                            String elementInHtmlList = remoteDoc
2443
                                    .getDocIdWithoutRevsion()
2444
                                    + MetaCatUtil.getOption("accNumSeparator")
2445
                                    + remoteDoc.getRevision();
2446
                            htmlDocumentImplList.add(elementInHtmlList);
2447
                        }//if
2448
                    }//if
2449

    
2450
                }//if
2451
                else {
2452
                    //create a docmentImpls object (represent xml doc) base on
2453
                    // the docId
2454
                    docImpls = (DocumentImpl) documentImplList.elementAt(i);
2455
                    //checking if the user has the permission to read the
2456
                    // documents
2457
                    if (DocumentImpl.hasReadPermission(user, groups, docImpls
2458
                            .getDocID())) {
2459
                        //if the docImpls is metadata
2460
                        if ((docImpls.getDoctype()).compareTo("BIN") != 0) {
2461
                            //add metadata into zip output stream
2462
                            addDocToZipOutputStream(docImpls, zOut, rootName);
2463
                            //add the documentImpl into the vetor which will
2464
                            // be used in html
2465
                            htmlDocumentImplList.add(docImpls);
2466

    
2467
                        }//if
2468
                        else {
2469
                            //it is data file
2470
                            addDataFileToZipOutputStream(docImpls, zOut,
2471
                                    rootName);
2472
                            htmlDocumentImplList.add(docImpls);
2473
                        }//else
2474
                    }//if
2475
                }//else
2476
            }//for
2477

    
2478
            //add html summary file
2479
            addHtmlSummaryToZipOutputStream(htmlDocumentImplList, zOut,
2480
                    rootName);
2481
            zOut.finish(); //terminate the zip file
2482
            //dbConn.close();
2483
            return zOut;
2484
        }//else
2485
    }//getZippedPackage()
2486

    
2487
    private class ReturnFieldValue
2488
    {
2489

    
2490
        private String docid = null; //return field value for this docid
2491

    
2492
        private String fieldValue = null;
2493

    
2494
        private String xmlFieldValue = null; //return field value in xml
2495
                                             // format
2496

    
2497
        public void setDocid(String myDocid)
2498
        {
2499
            docid = myDocid;
2500
        }
2501

    
2502
        public String getDocid()
2503
        {
2504
            return docid;
2505
        }
2506

    
2507
        public void setFieldValue(String myValue)
2508
        {
2509
            fieldValue = myValue;
2510
        }
2511

    
2512
        public String getFieldValue()
2513
        {
2514
            return fieldValue;
2515
        }
2516

    
2517
        public void setXMLFieldValue(String xml)
2518
        {
2519
            xmlFieldValue = xml;
2520
        }
2521

    
2522
        public String getXMLFieldValue()
2523
        {
2524
            return xmlFieldValue;
2525
        }
2526

    
2527
    }
2528
    
2529
    /**
2530
     * a class to store one result document consisting of a docid and a document
2531
     */
2532
    private class ResultDocument
2533
    {
2534
      public String docid;
2535
      public String document;
2536
      
2537
      public ResultDocument(String docid, String document)
2538
      {
2539
        this.docid = docid;
2540
        this.document = document;
2541
      }
2542
    }
2543
    
2544
    /**
2545
     * a private class to handle a set of resultDocuments
2546
     */
2547
    private class ResultDocumentSet
2548
    {
2549
      private Vector docids;
2550
      private Vector documents;
2551
      
2552
      public ResultDocumentSet()
2553
      {
2554
        docids = new Vector();
2555
        documents = new Vector();
2556
      }
2557
      
2558
      /**
2559
       * adds a result document to the set
2560
       */
2561
      public void addResultDocument(ResultDocument rd)
2562
      {
2563
        if(rd.docid == null)
2564
          return;
2565
        if(rd.document == null)
2566
          rd.document = "";
2567
        if (!containsDocid(rd.docid))
2568
        {
2569
           docids.addElement(rd.docid);
2570
           documents.addElement(rd.document);
2571
        }
2572
      }
2573
      
2574
      /**
2575
       * gets an iterator of docids
2576
       */
2577
      public Iterator getDocids()
2578
      {
2579
        return docids.iterator();
2580
      }
2581
      
2582
      /**
2583
       * gets an iterator of documents
2584
       */
2585
      public Iterator getDocuments()
2586
      {
2587
        return documents.iterator();
2588
      }
2589
      
2590
      /**
2591
       * returns the size of the set
2592
       */
2593
      public int size()
2594
      {
2595
        return docids.size();
2596
      }
2597
      
2598
      /**
2599
       * tests to see if this set contains the given docid
2600
       */
2601
      public boolean containsDocid(String docid)
2602
      {
2603
        for(int i=0; i<docids.size(); i++)
2604
        {
2605
          String docid0 = (String)docids.elementAt(i);
2606
          if(docid0.trim().equals(docid.trim()))
2607
          {
2608
            return true;
2609
          }
2610
        }
2611
        return false;
2612
      }
2613
      
2614
      /**
2615
       * removes the element with the given docid
2616
       */
2617
      public String remove(String docid)
2618
      {
2619
        for(int i=0; i<docids.size(); i++)
2620
        {
2621
          String docid0 = (String)docids.elementAt(i);
2622
          if(docid0.trim().equals(docid.trim()))
2623
          {
2624
            String returnDoc = (String)documents.elementAt(i);
2625
            documents.remove(i);
2626
            docids.remove(i);
2627
            return returnDoc;
2628
          }
2629
        }
2630
        return null;
2631
      }
2632
      
2633
      /**
2634
       * add a result document
2635
       */
2636
      public void put(ResultDocument rd)
2637
      {
2638
        addResultDocument(rd);
2639
      }
2640
      
2641
      /**
2642
       * add a result document by components
2643
       */
2644
      public void put(String docid, String document)
2645
      {
2646
        addResultDocument(new ResultDocument(docid, document));
2647
      }
2648
      
2649
      /**
2650
       * get the document part of the result document by docid
2651
       */
2652
      public Object get(String docid)
2653
      {
2654
        for(int i=0; i<docids.size(); i++)
2655
        {
2656
          String docid0 = (String)docids.elementAt(i);
2657
          if(docid0.trim().equals(docid.trim()))
2658
          {
2659
            return documents.elementAt(i);
2660
          }
2661
        }
2662
        return null;
2663
      }
2664
      
2665
      /**
2666
       * get the document part of the result document by an object
2667
       */
2668
      public Object get(Object o)
2669
      {
2670
        return get((String)o);
2671
      }
2672
      
2673
      /**
2674
       * get an entire result document by index number
2675
       */
2676
      public ResultDocument get(int index)
2677
      {
2678
        return new ResultDocument((String)docids.elementAt(index), 
2679
          (String)documents.elementAt(index));
2680
      }
2681
      
2682
      /**
2683
       * return a string representation of this object
2684
       */
2685
      public String toString()
2686
      {
2687
        String s = "";
2688
        for(int i=0; i<docids.size(); i++)
2689
        {
2690
          s += (String)docids.elementAt(i) + "\n";
2691
        }
2692
        return s;
2693
      }
2694
      /*
2695
       * Set a new document value for a given docid
2696
       */
2697
      public void set(String docid, String document)
2698
      {
2699
    	   for(int i=0; i<docids.size(); i++)
2700
           {
2701
             String docid0 = (String)docids.elementAt(i);
2702
             if(docid0.trim().equals(docid.trim()))
2703
             {
2704
                 documents.set(i, document);
2705
             }
2706
           }
2707
           
2708
      }
2709
    }
2710
}
(21-21/66)