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: berkley $'
13
 *     '$Date: 2010-08-11 12:26:22 -0700 (Wed, 11 Aug 2010) $'
14
 * '$Revision: 5491 $'
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.metacat.accesscontrol.AccessControlInterface;
52
import edu.ucsb.nceas.metacat.database.DBConnection;
53
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
54
import edu.ucsb.nceas.metacat.properties.PropertyService;
55
import edu.ucsb.nceas.metacat.util.AuthUtil;
56
import edu.ucsb.nceas.metacat.util.DocumentUtil;
57
import edu.ucsb.nceas.metacat.util.MetacatUtil;
58
import edu.ucsb.nceas.morpho.datapackage.Triple;
59
import edu.ucsb.nceas.morpho.datapackage.TripleCollection;
60
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
61

    
62

    
63
/**
64
 * A Class that searches a relational DB for elements and attributes that have
65
 * free text matches a query string, or structured query matches to a path
66
 * specified node in the XML hierarchy. It returns a result set consisting of
67
 * the document ID for each document that satisfies the query
68
 */
69
public class DBQuery
70
{
71

    
72
    static final int ALL = 1;
73

    
74
    static final int WRITE = 2;
75

    
76
    static final int READ = 4;
77
    
78
    private String qformat = "xml";
79

    
80
    //private Connection conn = null;
81
    private String parserName = null;
82

    
83
    private Logger logMetacat = Logger.getLogger(DBQuery.class);
84

    
85
    /** true if the metacat spatial option is installed **/
86
    private final boolean METACAT_SPATIAL = true;
87

    
88
    /** useful if you just want to grab a list of docids. Since the docids can be very long,
89
         it is a vector of vector  **/
90
    Vector docidOverride = new Vector();
91
    
92
    // a hash table serves as query reuslt cache. Key of hashtable
93
    // is a query string and value is result xml string
94
    private static Hashtable queryResultCache = new Hashtable();
95
    
96
    // Capacity of the query result cache
97
    private static final int QUERYRESULTCACHESIZE;
98
    static {
99
    	int qryRsltCacheSize = 0;
100
    	try {
101
    		qryRsltCacheSize = Integer.parseInt(PropertyService.getProperty("database.queryresultCacheSize"));
102
    	} catch (PropertyNotFoundException pnfe) {
103
    		System.err.println("Could not get QUERYRESULTCACHESIZE property in static block: "
104
					+ pnfe.getMessage());
105
    	}
106
    	QUERYRESULTCACHESIZE = qryRsltCacheSize;
107
    }
108
    
109

    
110
    // Size of page for non paged query
111
    private static final int NONPAGESIZE = 99999999;
112
    /**
113
     * the main routine used to test the DBQuery utility.
114
     * <p>
115
     * Usage: java DBQuery <xmlfile>
116
     *
117
     * @param xmlfile the filename of the xml file containing the query
118
     */
119
    static public void main(String[] args)
120
    {
121

    
122
        if (args.length < 1) {
123
            System.err.println("Wrong number of arguments!!!");
124
            System.err.println("USAGE: java DBQuery [-t] [-index] <xmlfile>");
125
            return;
126
        } else {
127
            try {
128

    
129
                int i = 0;
130
                boolean showRuntime = false;
131
                boolean useXMLIndex = false;
132
                if (args[i].equals("-t")) {
133
                    showRuntime = true;
134
                    i++;
135
                }
136
                if (args[i].equals("-index")) {
137
                    useXMLIndex = true;
138
                    i++;
139
                }
140
                String xmlfile = args[i];
141

    
142
                // Time the request if asked for
143
                double startTime = System.currentTimeMillis();
144

    
145
                // Open a connection to the database
146
                //Connection dbconn = util.openDBConnection();
147

    
148
                double connTime = System.currentTimeMillis();
149

    
150
                // Execute the query
151
                DBQuery queryobj = new DBQuery();
152
                FileReader xml = new FileReader(new File(xmlfile));
153
                Hashtable nodelist = null;
154
                //nodelist = queryobj.findDocuments(xml, null, null, useXMLIndex);
155

    
156
                // Print the reulting document listing
157
                StringBuffer result = new StringBuffer();
158
                String document = null;
159
                String docid = null;
160
                result.append("<?xml version=\"1.0\"?>\n");
161
                result.append("<resultset>\n");
162

    
163
                if (!showRuntime) {
164
                    Enumeration doclist = nodelist.keys();
165
                    while (doclist.hasMoreElements()) {
166
                        docid = (String) doclist.nextElement();
167
                        document = (String) nodelist.get(docid);
168
                        result.append("  <document>\n    " + document
169
                                + "\n  </document>\n");
170
                    }
171

    
172
                    result.append("</resultset>\n");
173
                }
174
                // Time the request if asked for
175
                double stopTime = System.currentTimeMillis();
176
                double dbOpenTime = (connTime - startTime) / 1000;
177
                double readTime = (stopTime - connTime) / 1000;
178
                double executionTime = (stopTime - startTime) / 1000;
179
                if (showRuntime) {
180
                    System.out.print("  " + executionTime);
181
                    System.out.print("  " + dbOpenTime);
182
                    System.out.print("  " + readTime);
183
                    System.out.print("  " + nodelist.size());
184
                    System.out.println();
185
                }
186
                //System.out.println(result);
187
                //write into a file "result.txt"
188
                if (!showRuntime) {
189
                    File f = new File("./result.txt");
190
                    FileWriter fw = new FileWriter(f);
191
                    BufferedWriter out = new BufferedWriter(fw);
192
                    out.write(result.toString());
193
                    out.flush();
194
                    out.close();
195
                    fw.close();
196
                }
197

    
198
            } catch (Exception e) {
199
                System.err.println("Error in DBQuery.main");
200
                System.err.println(e.getMessage());
201
                e.printStackTrace(System.err);
202
            }
203
        }
204
    }
205

    
206
    /**
207
     * construct an instance of the DBQuery class
208
     *
209
     * <p>
210
     * Generally, one would call the findDocuments() routine after creating an
211
     * instance to specify the search query
212
     * </p>
213
     *
214

    
215
     * @param parserName the fully qualified name of a Java class implementing
216
     *            the org.xml.sax.XMLReader interface
217
     */
218
    public DBQuery() throws PropertyNotFoundException
219
    {
220
        String parserName = PropertyService.getProperty("xml.saxparser");
221
        this.parserName = parserName;
222
    }
223

    
224
    /**
225
     * 
226
     * Construct an instance of DBQuery Class
227
     * BUT accept a docid Vector that will supersede
228
     * the query.printSQL() method
229
     *
230
     * If a docid Vector is passed in,
231
     * the docids will be used to create a simple IN query 
232
     * without the multiple subselects of the printSQL() method
233
     *
234
     * Using this constructor, we just check for 
235
     * a docidOverride Vector in the findResultDoclist() method
236
     *
237
     * @param docids List of docids to display in the resultset
238
     */
239
    public DBQuery(Vector docids) throws PropertyNotFoundException
240
    {
241
    	// since the query will be too long to be handled, so we divided the 
242
    	// docids vector into couple vectors.
243
    	int size = (new Integer(PropertyService.getProperty("database.appResultsetSize"))).intValue();
244
    	logMetacat.info("DBQuery.DBQuery - The size of select doicds is "+docids.size());
245
    	logMetacat.info("DBQuery.DBQuery - The application result size in metacat.properties is "+size);
246
    	Vector subset = new Vector();
247
    	if (docids != null && docids.size() > size)
248
    	{
249
    		int index = 0;
250
    		for (int i=0; i< docids.size(); i++)
251
    		{
252
    			
253
    			if (index < size)
254
    			{  	
255
    				subset.add(docids.elementAt(i));
256
    				index ++;
257
    			}
258
    			else
259
    			{
260
    				docidOverride.add(subset);
261
    				subset = new Vector();
262
    				subset.add(docids.elementAt(i));
263
    			    index = 1;
264
    			}
265
    		}
266
    		if (!subset.isEmpty())
267
    		{
268
    			docidOverride.add(subset);
269
    		}
270
    		
271
    	}
272
    	else
273
    	{
274
    		this.docidOverride.add(docids);
275
    	}
276
        
277
        String parserName = PropertyService.getProperty("xml.saxparser");
278
        this.parserName = parserName;
279
    }
280

    
281
  /**
282
   * Method put the search result set into out printerwriter
283
   * @param resoponse the return response
284
   * @param out the output printer
285
   * @param params the paratermer hashtable
286
   * @param user the user name (it maybe different to the one in param)
287
   * @param groups the group array
288
   * @param sessionid  the sessionid
289
   */
290
  public void findDocuments(HttpServletResponse response,
291
                                       PrintWriter out, Hashtable params,
292
                                       String user, String[] groups,
293
                                       String sessionid) throws PropertyNotFoundException
294
  {
295
    boolean useXMLIndex = (new Boolean(PropertyService.getProperty("database.usexmlindex")))
296
               .booleanValue();
297
    findDocuments(response, out, params, user, groups, sessionid, useXMLIndex);
298

    
299
  }
300

    
301

    
302
    /**
303
     * Method put the search result set into out printerwriter
304
     * @param resoponse the return response
305
     * @param out the output printer
306
     * @param params the paratermer hashtable
307
     * @param user the user name (it maybe different to the one in param)
308
     * @param groups the group array
309
     * @param sessionid  the sessionid
310
     */
311
    public void findDocuments(HttpServletResponse response,
312
                                         PrintWriter out, Hashtable params,
313
                                         String user, String[] groups,
314
                                         String sessionid, boolean useXMLIndex)
315
    {
316
      int pagesize = 0;
317
      int pagestart = 0;
318
      long transferWarnLimit = 0; 
319
      
320
      if(params.containsKey("pagesize") && params.containsKey("pagestart"))
321
      {
322
        String pagesizeStr = ((String[])params.get("pagesize"))[0];
323
        String pagestartStr = ((String[])params.get("pagestart"))[0];
324
        if(pagesizeStr != null && pagestartStr != null)
325
        {
326
          pagesize = (new Integer(pagesizeStr)).intValue();
327
          pagestart = (new Integer(pagestartStr)).intValue();
328
        }
329
      }
330
      
331
      String xmlquery = null;
332
      String qformat = null;
333
      // get query and qformat
334
      try {
335
    	xmlquery = ((String[])params.get("query"))[0];
336

    
337
        logMetacat.info("DBQuery.findDocuments - SESSIONID: " + sessionid);
338
        logMetacat.info("DBQuery.findDocuments - xmlquery: " + xmlquery);
339
        qformat = ((String[])params.get("qformat"))[0];
340
        logMetacat.info("DBQuery.findDocuments - qformat: " + qformat);
341
      }
342
      catch (Exception ee)
343
      {
344
        logMetacat.error("DBQuery.findDocuments - Couldn't retrieve xmlquery or qformat value from "
345
                  +"params hashtable in DBQuery.findDocuments: "
346
                  + ee.getMessage()); 
347
      }
348
      // Get the XML query and covert it into a SQL statment
349
      QuerySpecification qspec = null;
350
      if ( xmlquery != null)
351
      {
352
         xmlquery = transformQuery(xmlquery);
353
         try
354
         {
355
           qspec = new QuerySpecification(xmlquery,
356
                                          parserName,
357
                                          PropertyService.getProperty("document.accNumSeparator"));
358
         }
359
         catch (Exception ee)
360
         {
361
           logMetacat.error("DBQuery.findDocuments - error generating QuerySpecification object: "
362
                                    + ee.getMessage());
363
         }
364
      }
365

    
366

    
367

    
368
      if (qformat != null && qformat.equals(MetacatUtil.XMLFORMAT))
369
      {
370
        //xml format
371
        if(response != null)
372
        {
373
            response.setContentType("text/xml");
374
        }
375
        createResultDocument(xmlquery, qspec, out, user, groups, useXMLIndex, 
376
          pagesize, pagestart, sessionid, qformat);
377
      }//if
378
      else
379
      {
380
        //knb format, in this case we will get whole result and sent it out
381
        response.setContentType("text/html");
382
        PrintWriter nonout = null;
383
        StringBuffer xml = createResultDocument(xmlquery, qspec, nonout, user,
384
                                                groups, useXMLIndex, pagesize, 
385
                                                pagestart, sessionid, qformat);
386
        
387
        //transfer the xml to html
388
        try
389
        {
390
         long startHTMLTransform = System.currentTimeMillis();
391
         DBTransform trans = new DBTransform();
392
         response.setContentType("text/html");
393

    
394
         // if the user is a moderator, then pass a param to the 
395
         // xsl specifying the fact
396
         if(AuthUtil.isModerator(user, groups)){
397
        	 params.put("isModerator", new String[] {"true"});
398
         }
399

    
400
         trans.transformXMLDocument(xml.toString(), "-//NCEAS//resultset//EN",
401
                                 "-//W3C//HTML//EN", qformat, out, params,
402
                                 sessionid);
403
         long transformRunTime = System.currentTimeMillis() - startHTMLTransform;
404
         
405
         transferWarnLimit = Long.parseLong(PropertyService.getProperty("dbquery.transformTimeWarnLimit"));
406
         
407
         if (transformRunTime > transferWarnLimit) {
408
         	logMetacat.warn("DBQuery.findDocuments - The time to transfrom resultset from xml to html format is "
409
                  		                             + transformRunTime);
410
         }
411
          MetacatUtil.writeDebugToFile("---------------------------------------------------------------------------------------------------------------Transfrom xml to html  "
412
                             + transformRunTime);
413
          MetacatUtil.writeDebugToDelimiteredFile(" " + transformRunTime, false);
414
        }
415
        catch(Exception e)
416
        {
417
         logMetacat.error("DBQuery.findDocuments - Error in MetaCatServlet.transformResultset:"
418
                                +e.getMessage());
419
         }
420

    
421
      }//else
422

    
423
  }
424
    
425
    
426
  
427
  /**
428
   * Transforms a hashtable of documents to an xml or html result and sent
429
   * the content to outputstream. Keep going untill hastable is empty. stop it.
430
   * add the QuerySpecification as parameter is for ecogrid. But it is duplicate
431
   * to xmlquery String
432
   * @param xmlquery
433
   * @param qspec
434
   * @param out
435
   * @param user
436
   * @param groups
437
   * @param useXMLIndex
438
   * @param sessionid
439
   * @return
440
   */
441
    public StringBuffer createResultDocument(String xmlquery,
442
                                              QuerySpecification qspec,
443
                                              PrintWriter out,
444
                                              String user, String[] groups,
445
                                              boolean useXMLIndex)
446
    {
447
    	return createResultDocument(xmlquery,qspec,out, user,groups, useXMLIndex, 0, 0,"", qformat);
448
    }
449

    
450
  /*
451
   * Transforms a hashtable of documents to an xml or html result and sent
452
   * the content to outputstream. Keep going untill hastable is empty. stop it.
453
   * add the QuerySpecification as parameter is for ecogrid. But it is duplicate
454
   * to xmlquery String
455
   */
456
  public StringBuffer createResultDocument(String xmlquery,
457
                                            QuerySpecification qspec,
458
                                            PrintWriter out,
459
                                            String user, String[] groups,
460
                                            boolean useXMLIndex, int pagesize,
461
                                            int pagestart, String sessionid, 
462
                                            String qformat)
463
  {
464
    DBConnection dbconn = null;
465
    int serialNumber = -1;
466
    StringBuffer resultset = new StringBuffer();
467

    
468
    //try to get the cached version first    
469
    // Hashtable sessionHash = MetaCatServlet.getSessionHash();
470
    // HttpSession sess = (HttpSession)sessionHash.get(sessionid);
471

    
472
    
473
    resultset.append("<?xml version=\"1.0\"?>\n");
474
    resultset.append("<resultset>\n");
475
    resultset.append("  <pagestart>" + pagestart + "</pagestart>\n");
476
    resultset.append("  <pagesize>" + pagesize + "</pagesize>\n");
477
    resultset.append("  <nextpage>" + (pagestart + 1) + "</nextpage>\n");
478
    resultset.append("  <previouspage>" + (pagestart - 1) + "</previouspage>\n");
479

    
480
    resultset.append("  <query>" + xmlquery + "</query>");
481
    //send out a new query
482
    if (out != null)
483
    {
484
      out.println(resultset.toString());
485
    }
486
    if (qspec != null)
487
    {
488
      try
489
      {
490

    
491
        //checkout the dbconnection
492
        dbconn = DBConnectionPool.getDBConnection("DBQuery.findDocuments");
493
        serialNumber = dbconn.getCheckOutSerialNumber();
494

    
495
        //print out the search result
496
        // search the doc list
497
        Vector givenDocids = new Vector();
498
        StringBuffer resultContent = new StringBuffer();
499
        if (docidOverride == null || docidOverride.size() == 0)
500
        {
501
        	logMetacat.debug("DBQuery.createResultDocument - Not in map query");
502
        	resultContent = findResultDoclist(qspec, out, user, groups,
503
                    dbconn, useXMLIndex, pagesize, pagestart, 
504
                    sessionid, givenDocids, qformat);
505
        }
506
        else
507
        {
508
        	logMetacat.debug("DBQuery.createResultDocument - In map query");
509
        	// since docid can be too long to be handled. We divide it into several parts
510
        	for (int i= 0; i<docidOverride.size(); i++)
511
        	{
512
        	   logMetacat.debug("DBQuery.createResultDocument - in loop===== "+i);
513
        		givenDocids = (Vector)docidOverride.elementAt(i);
514
        		StringBuffer subset = findResultDoclist(qspec, out, user, groups,
515
                        dbconn, useXMLIndex, pagesize, pagestart, 
516
                        sessionid, givenDocids, qformat);
517
        		resultContent.append(subset);
518
        	}
519
        }
520
           
521
        resultset.append(resultContent);
522
      } //try
523
      catch (IOException ioe)
524
      {
525
        logMetacat.error("DBQuery.createResultDocument - IO error: " + ioe.getMessage());
526
      }
527
      catch (SQLException e)
528
      {
529
        logMetacat.error("DBQuery.createResultDocument - SQL Error: " + e.getMessage());
530
      }
531
      catch (Exception ee)
532
      {
533
        logMetacat.error("DBQuery.createResultDocument - General exception: "
534
                                 + ee.getMessage());
535
        ee.printStackTrace();
536
      }
537
      finally
538
      {
539
        DBConnectionPool.returnDBConnection(dbconn, serialNumber);
540
      } //finally
541
    }//if
542
    String closeRestultset = "</resultset>";
543
    resultset.append(closeRestultset);
544
    if (out != null)
545
    {
546
      out.println(closeRestultset);
547
    }
548

    
549
    //default to returning the whole resultset
550
    return resultset;
551
  }//createResultDocuments
552

    
553
    /*
554
     * Find the doc list which match the query
555
     */
556
    private StringBuffer findResultDoclist(QuerySpecification qspec,
557
                                      PrintWriter out,
558
                                      String user, String[]groups,
559
                                      DBConnection dbconn, boolean useXMLIndex,
560
                                      int pagesize, int pagestart, String sessionid, 
561
                                      Vector givenDocids, String qformat)
562
                                      throws Exception
563
    {
564
      StringBuffer resultsetBuffer = new StringBuffer();
565
      String query = null;
566
      int count = 0;
567
      int index = 0;
568
      ResultDocumentSet docListResult = new ResultDocumentSet();
569
      PreparedStatement pstmt = null;
570
      String docid = null;
571
      String docname = null;
572
      String doctype = null;
573
      String createDate = null;
574
      String updateDate = null;
575
      StringBuffer document = null;
576
      boolean lastpage = false;
577
      int rev = 0;
578
      double startTime = 0;
579
      int offset = 1;
580
      long startSelectionTime = System.currentTimeMillis();
581
      ResultSet rs = null;
582
           
583
   
584
      // this is a hack for offset. in postgresql 7, if the returned docid list is too long,
585
      //the extend query which base on the docid will be too long to be run. So we 
586
      // have to cut them into different parts. Page query don't need it somehow.
587
      if (out == null)
588
      {
589
        // for html page, we put everything into one page
590
        offset =
591
            (new Integer(PropertyService.getProperty("database.webResultsetSize"))).intValue();
592
      }
593
      else
594
      {
595
          offset =
596
              (new Integer(PropertyService.getProperty("database.appResultsetSize"))).intValue();
597
      }
598

    
599
      /*
600
       * Check the docidOverride Vector
601
       * if defined, we bypass the qspec.printSQL() method
602
       * and contruct a simpler query based on a 
603
       * list of docids rather than a bunch of subselects
604
       */
605
      if ( givenDocids == null || givenDocids.size() == 0 ) {
606
          query = qspec.printSQL(useXMLIndex);
607
      } else {
608
          logMetacat.info("DBQuery.findResultDoclist - docid override " + givenDocids.size());
609
          StringBuffer queryBuffer = new StringBuffer( "SELECT docid,docname,doctype,date_created, date_updated, rev " );
610
          queryBuffer.append( " FROM xml_documents WHERE docid IN (" );
611
          for (int i = 0; i < givenDocids.size(); i++) {  
612
              queryBuffer.append("'");
613
              queryBuffer.append( (String)givenDocids.elementAt(i) );
614
              queryBuffer.append("',");
615
          }
616
          // empty string hack 
617
          queryBuffer.append( "'') " );
618
          query = queryBuffer.toString();
619
      } 
620
      String ownerQuery = getOwnerQuery(user);
621
      //logMetacat.debug("query: " + query);
622
      logMetacat.debug("DBQuery.findResultDoclist - owner query: " + ownerQuery);
623
      // if query is not the owner query, we need to check the permission
624
      // otherwise we don't need (owner has all permission by default)
625
      if (!query.equals(ownerQuery))
626
      {
627
        // set user name and group
628
        qspec.setUserName(user);
629
        qspec.setGroup(groups);
630
        // Get access query
631
        String accessQuery = qspec.getAccessQuery();
632
        if(!query.endsWith("WHERE")){
633
            query = query + accessQuery;
634
        } else {
635
            query = query + accessQuery.substring(4, accessQuery.length());
636
        }
637
        
638
      }
639
      logMetacat.debug("DBQuery.findResultDoclist - final selection query: " + query);
640
      String selectionAndExtendedQuery = null;
641
      // we only get cache for public
642
      if (user != null && user.equalsIgnoreCase("public") 
643
     		 && pagesize == 0 && PropertyService.getProperty("database.queryCacheOn").equals("true"))
644
      {
645
    	  selectionAndExtendedQuery = query +qspec.getReturnDocList()+qspec.getReturnFieldList();
646
   	      String cachedResult = getResultXMLFromCache(selectionAndExtendedQuery);
647
   	      logMetacat.debug("DBQuery.findResultDoclist - The key of query cache is " + selectionAndExtendedQuery);
648
   	      //System.out.println("==========the string from cache is "+cachedResult);
649
   	      if (cachedResult != null)
650
   	      {
651
   	    	logMetacat.info("DBQuery.findResultDoclist - result from cache !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
652
   	    	 if (out != null)
653
   	         {
654
   	             out.println(cachedResult);
655
   	         }
656
   	    	 resultsetBuffer.append(cachedResult);
657
   	    	 return resultsetBuffer;
658
   	      }
659
      }
660
      
661
      startTime = System.currentTimeMillis() / 1000;
662
      pstmt = dbconn.prepareStatement(query);
663
      rs = pstmt.executeQuery();
664

    
665
      double queryExecuteTime = System.currentTimeMillis() / 1000;
666
      logMetacat.debug("DBQuery.findResultDoclist - Time to execute select docid query is "
667
                    + (queryExecuteTime - startTime));
668
      MetacatUtil.writeDebugToFile("\n\n\n\n\n\nExecute selection query  "
669
              + (queryExecuteTime - startTime));
670
      MetacatUtil.writeDebugToDelimiteredFile(""+(queryExecuteTime - startTime), false);
671

    
672
      boolean tableHasRows = rs.next();
673
      
674
      if(pagesize == 0)
675
      { //this makes sure we get all results if there is no paging
676
        pagesize = NONPAGESIZE;
677
        pagestart = NONPAGESIZE;
678
      } 
679
      
680
      int currentIndex = 0;
681
      while (tableHasRows)
682
      {
683
        logMetacat.debug("DBQuery.findResultDoclist - getting result: " + currentIndex);
684
        docid = rs.getString(1).trim();
685
        logMetacat.debug("DBQuery.findResultDoclist -  processing: " + docid);
686
        docname = rs.getString(2);
687
        doctype = rs.getString(3);
688
        logMetacat.debug("DBQuery.findResultDoclist - processing: " + doctype);
689
        createDate = rs.getString(4);
690
        updateDate = rs.getString(5);
691
        rev = rs.getInt(6);
692
        
693
         Vector returndocVec = qspec.getReturnDocList();
694
       if (returndocVec.size() == 0 || returndocVec.contains(doctype))
695
        {
696
          logMetacat.debug("DBQuery.findResultDoclist - NOT Back tracing now...");
697
           document = new StringBuffer();
698

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

    
793
     resultsetBuffer.append("\n<lastpage>" + lastpage + "</lastpage>\n");
794
     if (out != null)
795
     {
796
         out.println("\n<lastpage>" + lastpage + "</lastpage>\n");
797
     }
798
     
799
     // now we only cached none-paged query and user is public
800
     if (user != null && user.equalsIgnoreCase("public") 
801
    		 && pagesize == NONPAGESIZE && PropertyService.getProperty("database.queryCacheOn").equals("true"))
802
     {
803
       //System.out.println("the string stored into cache is "+ resultsetBuffer.toString());
804
  	   storeQueryResultIntoCache(selectionAndExtendedQuery, resultsetBuffer.toString());
805
     }
806
          
807
     return resultsetBuffer;
808
    }//findReturnDoclist
809

    
810

    
811
    /*
812
     * Send completed search hashtable(part of reulst)to output stream
813
     * and buffer into a buffer stream
814
     */
815
    private StringBuffer handleSubsetResult(QuerySpecification qspec,
816
                                           StringBuffer resultset,
817
                                           PrintWriter out, ResultDocumentSet partOfDoclist,
818
                                           String user, String[]groups,
819
                                       DBConnection dbconn, boolean useXMLIndex,
820
                                       String qformat)
821
                                       throws Exception
822
   {
823
     double startReturnFieldTime = System.currentTimeMillis();
824
     // check if there is a record in xml_returnfield
825
     // and get the returnfield_id and usage count
826
     int usage_count = getXmlReturnfieldsTableId(qspec, dbconn);
827
     boolean enterRecords = false;
828

    
829
     // get value of database.xmlReturnfieldCount
830
     int count = (new Integer(PropertyService
831
                            .getProperty("database.xmlReturnfieldCount")))
832
                            .intValue();
833

    
834
     // set enterRecords to true if usage_count is more than the offset
835
     // specified in metacat.properties
836
     if(usage_count > count){
837
         enterRecords = true;
838
     }
839

    
840
     if(returnfield_id < 0){
841
         logMetacat.warn("DBQuery.handleSubsetResult - Error in getting returnfield id from"
842
                                  + "xml_returnfield table");
843
         enterRecords = false;
844
     }
845

    
846
     // get the hashtable containing the docids that already in the
847
     // xml_queryresult table
848
     logMetacat.info("DBQuery.handleSubsetResult - size of partOfDoclist before"
849
                             + " docidsInQueryresultTable(): "
850
                             + partOfDoclist.size());
851
     long startGetReturnValueFromQueryresultable = System.currentTimeMillis();
852
     Hashtable queryresultDocList = docidsInQueryresultTable(returnfield_id,
853
                                                        partOfDoclist, dbconn);
854

    
855
     // remove the keys in queryresultDocList from partOfDoclist
856
     Enumeration _keys = queryresultDocList.keys();
857
     while (_keys.hasMoreElements()){
858
         partOfDoclist.remove((String)_keys.nextElement());
859
     }
860
     
861
     long queryResultReturnValuetime = System.currentTimeMillis() - startGetReturnValueFromQueryresultable;
862
     long queryResultWarnLimit = 
863
    	 Long.parseLong(PropertyService.getProperty("dbquery.findQueryResultsTimeWarnLimit"));
864
     
865
     if (queryResultReturnValuetime > queryResultWarnLimit) {
866
    	 logMetacat.warn("DBQuery.handleSubsetResult - Time to get return fields from xml_queryresult table is (Part1 in return fields) " +
867
    		 queryResultReturnValuetime);
868
     }
869
     MetacatUtil.writeDebugToFile("-----------------------------------------Get fields from xml_queryresult(Part1 in return fields) " +
870
    		 queryResultReturnValuetime);
871
     MetacatUtil.writeDebugToDelimiteredFile(" " + queryResultReturnValuetime,false);
872
     
873
     long startExtendedQuery = System.currentTimeMillis();
874
     // backup the keys-elements in partOfDoclist to check later
875
     // if the doc entry is indexed yet
876
     Hashtable partOfDoclistBackup = new Hashtable();
877
     Iterator itt = partOfDoclist.getDocids();
878
     while (itt.hasNext()){
879
       Object key = itt.next();
880
         partOfDoclistBackup.put(key, partOfDoclist.get(key));
881
     }
882

    
883
     logMetacat.info("DBQuery.handleSubsetResult - size of partOfDoclist after"
884
                             + " docidsInQueryresultTable(): "
885
                             + partOfDoclist.size());
886

    
887
     //add return fields for the documents in partOfDoclist
888
     partOfDoclist = addReturnfield(partOfDoclist, qspec, user, groups,
889
                                        dbconn, useXMLIndex, qformat);
890
     long extendedQueryRunTime = startExtendedQuery - System.currentTimeMillis();
891
     long extendedQueryWarnLimit = 
892
    	 Long.parseLong(PropertyService.getProperty("dbquery.extendedQueryRunTimeWarnLimit"));
893
  
894
     if (extendedQueryRunTime > extendedQueryWarnLimit) {
895
    	 logMetacat.warn("DBQuery.handleSubsetResult - Get fields from index and node table (Part2 in return fields) "
896
        		                                          + extendedQueryRunTime);
897
     }
898
     MetacatUtil.writeDebugToFile("-----------------------------------------Get fields from extened query(Part2 in return fields) "
899
             + extendedQueryRunTime);
900
     MetacatUtil.writeDebugToDelimiteredFile(" "
901
             + extendedQueryRunTime, false);
902
     //add relationship part part docid list for the documents in partOfDocList
903
     //partOfDoclist = addRelationship(partOfDoclist, qspec, dbconn, useXMLIndex);
904

    
905
     long startStoreReturnField = System.currentTimeMillis();
906
     Iterator keys = partOfDoclist.getDocids();
907
     String key = null;
908
     String element = null;
909
     String query = null;
910
     int offset = (new Integer(PropertyService
911
                               .getProperty("database.queryresultStringLength")))
912
                               .intValue();
913
     while (keys.hasNext())
914
     {
915
         key = (String) keys.next();
916
         element = (String)partOfDoclist.get(key);
917
         
918
	 // check if the enterRecords is true, elements is not null, element's
919
         // length is less than the limit of table column and if the document
920
         // has been indexed already
921
         if(enterRecords && element != null
922
		&& element.length() < offset
923
		&& element.compareTo((String) partOfDoclistBackup.get(key)) != 0){
924
             query = "INSERT INTO xml_queryresult (returnfield_id, docid, "
925
                 + "queryresult_string) VALUES (?, ?, ?)";
926

    
927
             PreparedStatement pstmt = null;
928
             pstmt = dbconn.prepareStatement(query);
929
             pstmt.setInt(1, returnfield_id);
930
             pstmt.setString(2, key);
931
             pstmt.setString(3, element);
932
            
933
             dbconn.increaseUsageCount(1);
934
             try
935
             {
936
            	 pstmt.execute();
937
             }
938
             catch(Exception e)
939
             {
940
            	 logMetacat.warn("DBQuery.handleSubsetResult - couldn't insert the element to xml_queryresult table "+e.getLocalizedMessage());
941
             }
942
             finally
943
             {
944
                pstmt.close();
945
             }
946
         }
947
        
948
         // A string with element
949
         String xmlElement = "  <document>" + element + "</document>";
950

    
951
         //send single element to output
952
         if (out != null)
953
         {
954
             out.println(xmlElement);
955
         }
956
         resultset.append(xmlElement);
957
     }//while
958
     
959
     double storeReturnFieldTime = System.currentTimeMillis() - startStoreReturnField;
960
     long storeReturnFieldWarnLimit = 
961
    	 Long.parseLong(PropertyService.getProperty("dbquery.storeReturnFieldTimeWarnLimit"));
962

    
963
     if (storeReturnFieldTime > storeReturnFieldWarnLimit) {
964
    	 logMetacat.warn("DBQuery.handleSubsetResult - Time to store new return fields into xml_queryresult table (Part4 in return fields) "
965
                   + storeReturnFieldTime);
966
     }
967
     MetacatUtil.writeDebugToFile("-----------------------------------------Insert new record to xml_queryresult(Part4 in return fields) "
968
             + storeReturnFieldTime);
969
     MetacatUtil.writeDebugToDelimiteredFile(" " + storeReturnFieldTime, false);
970
     
971
     Enumeration keysE = queryresultDocList.keys();
972
     while (keysE.hasMoreElements())
973
     {
974
         key = (String) keysE.nextElement();
975
         element = (String)queryresultDocList.get(key);
976
         // A string with element
977
         String xmlElement = "  <document>" + element + "</document>";
978
         //send single element to output
979
         if (out != null)
980
         {
981
             out.println(xmlElement);
982
         }
983
         resultset.append(xmlElement);
984
     }//while
985
     double returnFieldTime = System.currentTimeMillis() - startReturnFieldTime;
986
     long totalReturnFieldWarnLimit = 
987
    	 Long.parseLong(PropertyService.getProperty("dbquery.totalReturnFieldTimeWarnLimit"));
988

    
989
     if (returnFieldTime > totalReturnFieldWarnLimit) {
990
    	 logMetacat.warn("DBQuery.handleSubsetResult - Total time to get return fields is: "
991
                           + returnFieldTime);
992
     }
993
     MetacatUtil.writeDebugToFile("DBQuery.handleSubsetResult - ---------------------------------------------------------------------------------------------------------------"+
994
    		 "Total to get return fields  " + returnFieldTime);
995
     MetacatUtil.writeDebugToDelimiteredFile("DBQuery.handleSubsetResult - "+ returnFieldTime, false);
996
     return resultset;
997
 }
998

    
999
   /**
1000
    * Get the docids already in xml_queryresult table and corresponding
1001
    * queryresultstring as a hashtable
1002
    */
1003
   private Hashtable docidsInQueryresultTable(int returnfield_id,
1004
                                              ResultDocumentSet partOfDoclist,
1005
                                              DBConnection dbconn){
1006

    
1007
         Hashtable returnValue = new Hashtable();
1008
         PreparedStatement pstmt = null;
1009
         ResultSet rs = null;
1010

    
1011
         // get partOfDoclist as string for the query
1012
         Iterator keylist = partOfDoclist.getDocids();
1013
         StringBuffer doclist = new StringBuffer();
1014
         while (keylist.hasNext())
1015
         {
1016
             doclist.append("'");
1017
             doclist.append((String) keylist.next());
1018
             doclist.append("',");
1019
         }//while
1020

    
1021

    
1022
         if (doclist.length() > 0)
1023
         {
1024
             doclist.deleteCharAt(doclist.length() - 1); //remove the last comma
1025

    
1026
             // the query to find out docids from xml_queryresult
1027
             String query = "select docid, queryresult_string from "
1028
                          + "xml_queryresult where returnfield_id = " +
1029
                          returnfield_id +" and docid in ("+ doclist + ")";
1030
             logMetacat.info("DBQuery.docidsInQueryresultTable - Query to get docids from xml_queryresult:"
1031
                                      + query);
1032

    
1033
             try {
1034
                 // prepare and execute the query
1035
                 pstmt = dbconn.prepareStatement(query);
1036
                 dbconn.increaseUsageCount(1);
1037
                 pstmt.execute();
1038
                 rs = pstmt.getResultSet();
1039
                 boolean tableHasRows = rs.next();
1040
                 while (tableHasRows) {
1041
                     // store the returned results in the returnValue hashtable
1042
                     String key = rs.getString(1);
1043
                     String element = rs.getString(2);
1044

    
1045
                     if(element != null){
1046
                         returnValue.put(key, element);
1047
                     } else {
1048
                         logMetacat.info("DBQuery.docidsInQueryresultTable - Null elment found ("
1049
                         + "DBQuery.docidsInQueryresultTable)");
1050
                     }
1051
                     tableHasRows = rs.next();
1052
                 }
1053
                 rs.close();
1054
                 pstmt.close();
1055
             } catch (Exception e){
1056
                 logMetacat.error("DBQuery.docidsInQueryresultTable - Error getting docids from "
1057
                                          + "queryresult: " + e.getMessage());
1058
              }
1059
         }
1060
         return returnValue;
1061
     }
1062

    
1063

    
1064
   /**
1065
    * Method to get id from xml_returnfield table
1066
    * for a given query specification
1067
    */
1068
   private int returnfield_id;
1069
   private int getXmlReturnfieldsTableId(QuerySpecification qspec,
1070
                                           DBConnection dbconn){
1071
       int id = -1;
1072
       int count = 1;
1073
       PreparedStatement pstmt = null;
1074
       ResultSet rs = null;
1075
       String returnfield = qspec.getSortedReturnFieldString();
1076

    
1077
       // query for finding the id from xml_returnfield
1078
       String query = "SELECT returnfield_id, usage_count FROM xml_returnfield "
1079
            + "WHERE returnfield_string LIKE ?";
1080
       logMetacat.info("DBQuery.getXmlReturnfieldsTableId - ReturnField Query:" + query);
1081

    
1082
       try {
1083
           // prepare and run the query
1084
           pstmt = dbconn.prepareStatement(query);
1085
           pstmt.setString(1,returnfield);
1086
           dbconn.increaseUsageCount(1);
1087
           pstmt.execute();
1088
           rs = pstmt.getResultSet();
1089
           boolean tableHasRows = rs.next();
1090

    
1091
           // if record found then increase the usage count
1092
           // else insert a new record and get the id of the new record
1093
           if(tableHasRows){
1094
               // get the id
1095
               id = rs.getInt(1);
1096
               count = rs.getInt(2) + 1;
1097
               rs.close();
1098
               pstmt.close();
1099

    
1100
               // increase the usage count
1101
               query = "UPDATE xml_returnfield SET usage_count ='" + count
1102
                   + "' WHERE returnfield_id ='"+ id +"'";
1103
               logMetacat.info("DBQuery.getXmlReturnfieldsTableId - ReturnField Table Update:"+ query);
1104

    
1105
               pstmt = dbconn.prepareStatement(query);
1106
               dbconn.increaseUsageCount(1);
1107
               pstmt.execute();
1108
               pstmt.close();
1109

    
1110
           } else {
1111
               rs.close();
1112
               pstmt.close();
1113

    
1114
               // insert a new record
1115
               query = "INSERT INTO xml_returnfield (returnfield_string, usage_count)"
1116
                   + "VALUES (?, '1')";
1117
               logMetacat.info("DBQuery.getXmlReturnfieldsTableId - ReturnField Table Insert:"+ query);
1118
               pstmt = dbconn.prepareStatement(query);
1119
               pstmt.setString(1, returnfield);
1120
               dbconn.increaseUsageCount(1);
1121
               pstmt.execute();
1122
               pstmt.close();
1123

    
1124
               // get the id of the new record
1125
               query = "SELECT returnfield_id FROM xml_returnfield "
1126
                   + "WHERE returnfield_string LIKE ?";
1127
               logMetacat.info("DBQuery.getXmlReturnfieldsTableId - ReturnField query after Insert:" + query);
1128
               pstmt = dbconn.prepareStatement(query);
1129
               pstmt.setString(1, returnfield);
1130

    
1131
               dbconn.increaseUsageCount(1);
1132
               pstmt.execute();
1133
               rs = pstmt.getResultSet();
1134
               if(rs.next()){
1135
                   id = rs.getInt(1);
1136
               } else {
1137
                   id = -1;
1138
               }
1139
               rs.close();
1140
               pstmt.close();
1141
           }
1142

    
1143
       } catch (Exception e){
1144
           logMetacat.error("DBQuery.getXmlReturnfieldsTableId - Error getting id from xml_returnfield in "
1145
                                     + "DBQuery.getXmlReturnfieldsTableId: "
1146
                                     + e.getMessage());
1147
           id = -1;
1148
       }
1149

    
1150
       returnfield_id = id;
1151
       return count;
1152
   }
1153

    
1154

    
1155
    /*
1156
     * A method to add return field to return doclist hash table
1157
     */
1158
    private ResultDocumentSet addReturnfield(ResultDocumentSet docListResult,
1159
                                      QuerySpecification qspec,
1160
                                      String user, String[]groups,
1161
                                      DBConnection dbconn, boolean useXMLIndex,
1162
                                      String qformat)
1163
                                      throws Exception
1164
    {
1165
      PreparedStatement pstmt = null;
1166
      ResultSet rs = null;
1167
      String docid = null;
1168
      String fieldname = null;
1169
      String fieldtype = null;
1170
      String fielddata = null;
1171
      String relation = null;
1172

    
1173
      if (qspec.containsExtendedSQL())
1174
      {
1175
        qspec.setUserName(user);
1176
        qspec.setGroup(groups);
1177
        Vector extendedFields = new Vector(qspec.getReturnFieldList());
1178
        Vector results = new Vector();
1179
        Iterator keylist = docListResult.getDocids();
1180
        StringBuffer doclist = new StringBuffer();
1181
        Vector parentidList = new Vector();
1182
        Hashtable returnFieldValue = new Hashtable();
1183
        while (keylist.hasNext())
1184
        {
1185
          String key = (String)keylist.next();
1186
          doclist.append("'");
1187
          doclist.append(key);
1188
          doclist.append("',");
1189
        }
1190
        if (doclist.length() > 0)
1191
        {
1192
          Hashtable controlPairs = new Hashtable();
1193
          doclist.deleteCharAt(doclist.length() - 1); //remove the last comma
1194
          boolean tableHasRows = false;
1195
        
1196

    
1197
           String extendedQuery =
1198
               qspec.printExtendedSQL(doclist.toString(), useXMLIndex);
1199
           logMetacat.info("DBQuery.addReturnfield - Extended query: " + extendedQuery);
1200

    
1201
           if(extendedQuery != null){
1202
//        	   long extendedQueryStart = System.currentTimeMillis();
1203
               pstmt = dbconn.prepareStatement(extendedQuery);
1204
               //increase dbconnection usage count
1205
               dbconn.increaseUsageCount(1);
1206
               pstmt.execute();
1207
               rs = pstmt.getResultSet();
1208
               tableHasRows = rs.next();
1209
               while (tableHasRows) {
1210
                   ReturnFieldValue returnValue = new ReturnFieldValue();
1211
                   docid = rs.getString(1).trim();
1212
                   fieldname = rs.getString(2);
1213
                   
1214
                   if(qformat.toLowerCase().trim().equals("xml"))
1215
                   {
1216
                       byte[] b = rs.getBytes(3);
1217
                       fielddata = new String(b, 0, b.length);
1218
                   }
1219
                   else
1220
                   {
1221
                       fielddata = rs.getString(3);
1222
                   }
1223
                   
1224
                   //System.out.println("raw fielddata: " + fielddata);
1225
                   fielddata = MetacatUtil.normalize(fielddata);
1226
                   //System.out.println("normalized fielddata: " + fielddata);
1227
                   String parentId = rs.getString(4);
1228
                   fieldtype = rs.getString(5);
1229
                   StringBuffer value = new StringBuffer();
1230

    
1231
                   //handle case when usexmlindex is true differently
1232
                   //at one point merging the nodedata (for large text elements) was 
1233
                   //deemed unnecessary - but now it is needed.  but not for attribute nodes
1234
                   if (useXMLIndex || !containsKey(parentidList, parentId)) {
1235
                	   //merge node data only for non-ATTRIBUTEs
1236
                	   if (fieldtype != null && !fieldtype.equals("ATTRIBUTE")) {
1237
	                	   //try merging the data
1238
	                	   ReturnFieldValue existingRFV =
1239
	                		   getArrayValue(parentidList, parentId);
1240
	                	   if (existingRFV != null && !existingRFV.getFieldType().equals("ATTRIBUTE")) {
1241
	                		   fielddata = existingRFV.getFieldValue() + fielddata;
1242
	                	   }
1243
                	   }
1244
                	   //System.out.println("fieldname: " + fieldname + " fielddata: " + fielddata);
1245

    
1246
                       value.append("<param name=\"");
1247
                       value.append(fieldname);
1248
                       value.append("\">");
1249
                       value.append(fielddata);
1250
                       value.append("</param>");
1251
                       //set returnvalue
1252
                       returnValue.setDocid(docid);
1253
                       returnValue.setFieldValue(fielddata);
1254
                       returnValue.setFieldType(fieldtype);
1255
                       returnValue.setXMLFieldValue(value.toString());
1256
                       // Store it in hastable
1257
                       putInArray(parentidList, parentId, returnValue);
1258
                   }
1259
                   else {
1260
                       
1261
                       // need to merge nodedata if they have same parent id and
1262
                       // node type is text
1263
                       fielddata = (String) ( (ReturnFieldValue)
1264
                                             getArrayValue(
1265
                           parentidList, parentId)).getFieldValue()
1266
                           + fielddata;
1267
                       //System.out.println("fieldname: " + fieldname + " fielddata: " + fielddata);
1268
                       value.append("<param name=\"");
1269
                       value.append(fieldname);
1270
                       value.append("\">");
1271
                       value.append(fielddata);
1272
                       value.append("</param>");
1273
                       returnValue.setDocid(docid);
1274
                       returnValue.setFieldValue(fielddata);
1275
                       returnValue.setFieldType(fieldtype);
1276
                       returnValue.setXMLFieldValue(value.toString());
1277
                       // remove the old return value from paretnidList
1278
                       parentidList.remove(parentId);
1279
                       // store the new return value in parentidlit
1280
                       putInArray(parentidList, parentId, returnValue);
1281
                   }
1282
                   tableHasRows = rs.next();
1283
               } //while
1284
               rs.close();
1285
               pstmt.close();
1286

    
1287
               // put the merger node data info into doclistReult
1288
               Enumeration xmlFieldValue = (getElements(parentidList)).
1289
                   elements();
1290
               while (xmlFieldValue.hasMoreElements()) {
1291
                   ReturnFieldValue object =
1292
                       (ReturnFieldValue) xmlFieldValue.nextElement();
1293
                   docid = object.getDocid();
1294
                   if (docListResult.containsDocid(docid)) {
1295
                       String removedelement = (String) docListResult.
1296
                           remove(docid);
1297
                       docListResult.
1298
                           addResultDocument(new ResultDocument(docid,
1299
                               removedelement + object.getXMLFieldValue()));
1300
                   }
1301
                   else {
1302
                       docListResult.addResultDocument(
1303
                         new ResultDocument(docid, object.getXMLFieldValue()));
1304
                   }
1305
               } //while
1306
//               double docListResultEnd = System.currentTimeMillis() / 1000;
1307
//               logMetacat.warn(
1308
//                   "Time to prepare ResultDocumentSet after"
1309
//                   + " execute extended query: "
1310
//                   + (docListResultEnd - extendedQueryEnd));
1311
           }
1312
       }//if doclist lenght is great than zero
1313
     }//if has extended query
1314

    
1315
      return docListResult;
1316
    }//addReturnfield
1317

    
1318
  
1319
  /**
1320
   * removes the <?xml version="1.0"?> tag from the beginning.  This takes a
1321
   * string as a param instead of a hashtable.
1322
   *
1323
   * @param xmlquery a string representing a query.
1324
   */
1325
   private  String transformQuery(String xmlquery)
1326
   {
1327
     xmlquery = xmlquery.trim();
1328
     int index = xmlquery.indexOf("?>");
1329
     if (index != -1)
1330
     {
1331
       return xmlquery.substring(index + 2, xmlquery.length());
1332
     }
1333
     else
1334
     {
1335
       return xmlquery;
1336
     }
1337
   }
1338
   
1339
   /*
1340
    * Method to store query string and result xml string into query result
1341
    * cache. If the size alreay reache the limitation, the cache will be
1342
    * cleared first, then store them.
1343
    */
1344
   private void storeQueryResultIntoCache(String query, String resultXML)
1345
   {
1346
	   synchronized (queryResultCache)
1347
	   {
1348
		   if (queryResultCache.size() >= QUERYRESULTCACHESIZE)
1349
		   {
1350
			   queryResultCache.clear();
1351
		   }
1352
		   queryResultCache.put(query, resultXML);
1353
		   
1354
	   }
1355
   }
1356
   
1357
   /*
1358
    * Method to get result xml string from query result cache. 
1359
    * Note: the returned string can be null.
1360
    */
1361
   private String getResultXMLFromCache(String query)
1362
   {
1363
	   String resultSet = null;
1364
	   synchronized (queryResultCache)
1365
	   {
1366
          try
1367
          {
1368
        	 logMetacat.info("DBQuery.getResultXMLFromCache - Get query from cache");
1369
		     resultSet = (String)queryResultCache.get(query);
1370
		   
1371
          }
1372
          catch (Exception e)
1373
          {
1374
        	  resultSet = null;
1375
          }
1376
		   
1377
	   }
1378
	   return resultSet;
1379
   }
1380
   
1381
   /**
1382
    * Method to clear the query result cache.
1383
    */
1384
   public static void clearQueryResultCache()
1385
   {
1386
	   synchronized (queryResultCache)
1387
	   {
1388
		   queryResultCache.clear();
1389
	   }
1390
   }
1391

    
1392

    
1393
    /*
1394
     * A method to search if Vector contains a particular key string
1395
     */
1396
    private boolean containsKey(Vector parentidList, String parentId)
1397
    {
1398

    
1399
        Vector tempVector = null;
1400

    
1401
        for (int count = 0; count < parentidList.size(); count++) {
1402
            tempVector = (Vector) parentidList.get(count);
1403
            if (parentId.compareTo((String) tempVector.get(0)) == 0) { return true; }
1404
        }
1405
        return false;
1406
    }
1407
    
1408
    /*
1409
     * A method to put key and value in Vector
1410
     */
1411
    private void putInArray(Vector parentidList, String key,
1412
            ReturnFieldValue value)
1413
    {
1414

    
1415
        Vector tempVector = null;
1416
        //only filter if the field type is NOT an attribute (say, for text)
1417
        String fieldType = value.getFieldType();
1418
        if (fieldType != null && !fieldType.equals("ATTRIBUTE")) {
1419
        
1420
	        for (int count = 0; count < parentidList.size(); count++) {
1421
	            tempVector = (Vector) parentidList.get(count);
1422
	
1423
	            if (key.compareTo((String) tempVector.get(0)) == 0) {
1424
	                tempVector.remove(1);
1425
	                tempVector.add(1, value);
1426
	                return;
1427
	            }
1428
	        }
1429
        }
1430

    
1431
        tempVector = new Vector();
1432
        tempVector.add(0, key);
1433
        tempVector.add(1, value);
1434
        parentidList.add(tempVector);
1435
        return;
1436
    }
1437

    
1438
    /*
1439
     * A method to get value in Vector given a key
1440
     */
1441
    private ReturnFieldValue getArrayValue(Vector parentidList, String key)
1442
    {
1443

    
1444
        Vector tempVector = null;
1445

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

    
1449
            if (key.compareTo((String) tempVector.get(0)) == 0) { return (ReturnFieldValue) tempVector
1450
                    .get(1); }
1451
        }
1452
        return null;
1453
    }
1454

    
1455
    /*
1456
     * A method to get enumeration of all values in Vector
1457
     */
1458
    private Vector getElements(Vector parentidList)
1459
    {
1460
        Vector enumVector = new Vector();
1461
        Vector tempVector = null;
1462

    
1463
        for (int count = 0; count < parentidList.size(); count++) {
1464
            tempVector = (Vector) parentidList.get(count);
1465

    
1466
            enumVector.add(tempVector.get(1));
1467
        }
1468
        return enumVector;
1469
    }
1470

    
1471
  
1472

    
1473
    /*
1474
     * A method to create a query to get owner's docid list
1475
     */
1476
    private String getOwnerQuery(String owner)
1477
    {
1478
        if (owner != null) {
1479
            owner = owner.toLowerCase();
1480
        }
1481
        StringBuffer self = new StringBuffer();
1482

    
1483
        self.append("SELECT docid,docname,doctype,");
1484
        self.append("date_created, date_updated, rev ");
1485
        self.append("FROM xml_documents WHERE docid IN (");
1486
        self.append("(");
1487
        self.append("SELECT DISTINCT docid FROM xml_nodes WHERE \n");
1488
        self.append("nodedata LIKE '%%%' ");
1489
        self.append(") \n");
1490
        self.append(") ");
1491
        self.append(" AND (");
1492
        self.append(" lower(user_owner) = '" + owner + "'");
1493
        self.append(") ");
1494
        return self.toString();
1495
    }
1496

    
1497
    /**
1498
     * format a structured query as an XML document that conforms to the
1499
     * pathquery.dtd and is appropriate for submission to the DBQuery
1500
     * structured query engine
1501
     *
1502
     * @param params The list of parameters that should be included in the
1503
     *            query
1504
     */
1505
    public static String createSQuery(Hashtable params) throws PropertyNotFoundException
1506
    {
1507
        StringBuffer query = new StringBuffer();
1508
        Enumeration elements;
1509
        Enumeration keys;
1510
        String filterDoctype = null;
1511
        String casesensitive = null;
1512
        String searchmode = null;
1513
        Object nextkey;
1514
        Object nextelement;
1515
        //add the xml headers
1516
        query.append("<?xml version=\"1.0\"?>\n");
1517
        query.append("<pathquery version=\"1.2\">\n");
1518

    
1519

    
1520

    
1521
        if (params.containsKey("meta_file_id")) {
1522
            query.append("<meta_file_id>");
1523
            query.append(((String[]) params.get("meta_file_id"))[0]);
1524
            query.append("</meta_file_id>");
1525
        }
1526

    
1527
        if (params.containsKey("returndoctype")) {
1528
            String[] returnDoctypes = ((String[]) params.get("returndoctype"));
1529
            for (int i = 0; i < returnDoctypes.length; i++) {
1530
                String doctype = (String) returnDoctypes[i];
1531

    
1532
                if (!doctype.equals("any") && !doctype.equals("ANY")
1533
                        && !doctype.equals("")) {
1534
                    query.append("<returndoctype>").append(doctype);
1535
                    query.append("</returndoctype>");
1536
                }
1537
            }
1538
        }
1539

    
1540
        if (params.containsKey("filterdoctype")) {
1541
            String[] filterDoctypes = ((String[]) params.get("filterdoctype"));
1542
            for (int i = 0; i < filterDoctypes.length; i++) {
1543
                query.append("<filterdoctype>").append(filterDoctypes[i]);
1544
                query.append("</filterdoctype>");
1545
            }
1546
        }
1547

    
1548
        if (params.containsKey("returnfield")) {
1549
            String[] returnfield = ((String[]) params.get("returnfield"));
1550
            for (int i = 0; i < returnfield.length; i++) {
1551
                query.append("<returnfield>").append(returnfield[i]);
1552
                query.append("</returnfield>");
1553
            }
1554
        }
1555

    
1556
        if (params.containsKey("owner")) {
1557
            String[] owner = ((String[]) params.get("owner"));
1558
            for (int i = 0; i < owner.length; i++) {
1559
                query.append("<owner>").append(owner[i]);
1560
                query.append("</owner>");
1561
            }
1562
        }
1563

    
1564
        if (params.containsKey("site")) {
1565
            String[] site = ((String[]) params.get("site"));
1566
            for (int i = 0; i < site.length; i++) {
1567
                query.append("<site>").append(site[i]);
1568
                query.append("</site>");
1569
            }
1570
        }
1571

    
1572
        //allows the dynamic switching of boolean operators
1573
        if (params.containsKey("operator")) {
1574
            query.append("<querygroup operator=\""
1575
                    + ((String[]) params.get("operator"))[0] + "\">");
1576
        } else { //the default operator is UNION
1577
            query.append("<querygroup operator=\"UNION\">");
1578
        }
1579

    
1580
        if (params.containsKey("casesensitive")) {
1581
            casesensitive = ((String[]) params.get("casesensitive"))[0];
1582
        } else {
1583
            casesensitive = "false";
1584
        }
1585

    
1586
        if (params.containsKey("searchmode")) {
1587
            searchmode = ((String[]) params.get("searchmode"))[0];
1588
        } else {
1589
            searchmode = "contains";
1590
        }
1591

    
1592
        //anyfield is a special case because it does a
1593
        //free text search. It does not have a <pathexpr>
1594
        //tag. This allows for a free text search within the structured
1595
        //query. This is useful if the INTERSECT operator is used.
1596
        if (params.containsKey("anyfield")) {
1597
            String[] anyfield = ((String[]) params.get("anyfield"));
1598
            //allow for more than one value for anyfield
1599
            for (int i = 0; i < anyfield.length; i++) {
1600
                if (anyfield[i] != null && !anyfield[i].equals("")) {
1601
                    query.append("<queryterm casesensitive=\"" + casesensitive
1602
                            + "\" " + "searchmode=\"" + searchmode
1603
                            + "\"><value>" + anyfield[i]
1604
                            + "</value></queryterm>");
1605
                }
1606
            }
1607
        }
1608

    
1609
        //this while loop finds the rest of the parameters
1610
        //and attempts to query for the field specified
1611
        //by the parameter.
1612
        elements = params.elements();
1613
        keys = params.keys();
1614
        while (keys.hasMoreElements() && elements.hasMoreElements()) {
1615
            nextkey = keys.nextElement();
1616
            nextelement = elements.nextElement();
1617

    
1618
            //make sure we aren't querying for any of these
1619
            //parameters since the are already in the query
1620
            //in one form or another.
1621
            Vector ignoredParams = new Vector();
1622
            ignoredParams.add("returndoctype");
1623
            ignoredParams.add("filterdoctype");
1624
            ignoredParams.add("action");
1625
            ignoredParams.add("qformat");
1626
            ignoredParams.add("anyfield");
1627
            ignoredParams.add("returnfield");
1628
            ignoredParams.add("owner");
1629
            ignoredParams.add("site");
1630
            ignoredParams.add("operator");
1631
            ignoredParams.add("sessionid");
1632
            ignoredParams.add("pagesize");
1633
            ignoredParams.add("pagestart");
1634
            ignoredParams.add("searchmode");
1635

    
1636
            // Also ignore parameters listed in the properties file
1637
            // so that they can be passed through to stylesheets
1638
            String paramsToIgnore = PropertyService
1639
                    .getProperty("database.queryignoredparams");
1640
            StringTokenizer st = new StringTokenizer(paramsToIgnore, ",");
1641
            while (st.hasMoreTokens()) {
1642
                ignoredParams.add(st.nextToken());
1643
            }
1644
            if (!ignoredParams.contains(nextkey.toString())) {
1645
                //allow for more than value per field name
1646
                for (int i = 0; i < ((String[]) nextelement).length; i++) {
1647
                    if (!((String[]) nextelement)[i].equals("")) {
1648
                        query.append("<queryterm casesensitive=\""
1649
                                + casesensitive + "\" " + "searchmode=\""
1650
                                + searchmode + "\">" + "<value>" +
1651
                                //add the query value
1652
                                ((String[]) nextelement)[i]
1653
                                + "</value><pathexpr>" +
1654
                                //add the path to query by
1655
                                nextkey.toString() + "</pathexpr></queryterm>");
1656
                    }
1657
                }
1658
            }
1659
        }
1660
        query.append("</querygroup></pathquery>");
1661
        //append on the end of the xml and return the result as a string
1662
        return query.toString();
1663
    }
1664

    
1665
    /**
1666
     * format a simple free-text value query as an XML document that conforms
1667
     * to the pathquery.dtd and is appropriate for submission to the DBQuery
1668
     * structured query engine
1669
     *
1670
     * @param value the text string to search for in the xml catalog
1671
     * @param doctype the type of documents to include in the result set -- use
1672
     *            "any" or "ANY" for unfiltered result sets
1673
     */
1674
    public static String createQuery(String value, String doctype)
1675
    {
1676
        StringBuffer xmlquery = new StringBuffer();
1677
        xmlquery.append("<?xml version=\"1.0\"?>\n");
1678
        xmlquery.append("<pathquery version=\"1.0\">");
1679

    
1680
        if (!doctype.equals("any") && !doctype.equals("ANY")) {
1681
            xmlquery.append("<returndoctype>");
1682
            xmlquery.append(doctype).append("</returndoctype>");
1683
        }
1684

    
1685
        xmlquery.append("<querygroup operator=\"UNION\">");
1686
        //chad added - 8/14
1687
        //the if statement allows a query to gracefully handle a null
1688
        //query. Without this if a nullpointerException is thrown.
1689
        if (!value.equals("")) {
1690
            xmlquery.append("<queryterm casesensitive=\"false\" ");
1691
            xmlquery.append("searchmode=\"contains\">");
1692
            xmlquery.append("<value>").append(value).append("</value>");
1693
            xmlquery.append("</queryterm>");
1694
        }
1695
        xmlquery.append("</querygroup>");
1696
        xmlquery.append("</pathquery>");
1697

    
1698
        return (xmlquery.toString());
1699
    }
1700

    
1701
    /**
1702
     * format a simple free-text value query as an XML document that conforms
1703
     * to the pathquery.dtd and is appropriate for submission to the DBQuery
1704
     * structured query engine
1705
     *
1706
     * @param value the text string to search for in the xml catalog
1707
     */
1708
    public static String createQuery(String value)
1709
    {
1710
        return createQuery(value, "any");
1711
    }
1712

    
1713
    /**
1714
     * Check for "READ" permission on @docid for @user and/or @group from DB
1715
     * connection
1716
     */
1717
    private boolean hasPermission(String user, String[] groups, String docid)
1718
            throws SQLException, Exception
1719
    {
1720
        // Check for READ permission on @docid for @user and/or @groups
1721
        PermissionController controller = new PermissionController(docid);
1722
        return controller.hasPermission(user, groups,
1723
                AccessControlInterface.READSTRING);
1724
    }
1725

    
1726
    /**
1727
     * Get all docIds list for a data packadge
1728
     *
1729
     * @param dataPackageDocid, the string in docId field of xml_relation table
1730
     */
1731
    private Vector getCurrentDocidListForDataPackage(String dataPackageDocid)
1732
    {
1733
        DBConnection dbConn = null;
1734
        int serialNumber = -1;
1735
        Vector docIdList = new Vector();//return value
1736
        PreparedStatement pStmt = null;
1737
        ResultSet rs = null;
1738
        String docIdInSubjectField = null;
1739
        String docIdInObjectField = null;
1740

    
1741
        // Check the parameter
1742
        if (dataPackageDocid == null || dataPackageDocid.equals("")) { return docIdList; }//if
1743

    
1744
        //the query stirng
1745
        String query = "SELECT subject, object from xml_relation where docId = ?";
1746
        try {
1747
            dbConn = DBConnectionPool
1748
                    .getDBConnection("DBQuery.getCurrentDocidListForDataPackage");
1749
            serialNumber = dbConn.getCheckOutSerialNumber();
1750
            pStmt = dbConn.prepareStatement(query);
1751
            //bind the value to query
1752
            pStmt.setString(1, dataPackageDocid);
1753

    
1754
            //excute the query
1755
            pStmt.execute();
1756
            //get the result set
1757
            rs = pStmt.getResultSet();
1758
            //process the result
1759
            while (rs.next()) {
1760
                //In order to get the whole docIds in a data packadge,
1761
                //we need to put the docIds of subject and object field in
1762
                // xml_relation
1763
                //into the return vector
1764
                docIdInSubjectField = rs.getString(1);//the result docId in
1765
                                                      // subject field
1766
                docIdInObjectField = rs.getString(2);//the result docId in
1767
                                                     // object field
1768

    
1769
                //don't put the duplicate docId into the vector
1770
                if (!docIdList.contains(docIdInSubjectField)) {
1771
                    docIdList.add(docIdInSubjectField);
1772
                }
1773

    
1774
                //don't put the duplicate docId into the vector
1775
                if (!docIdList.contains(docIdInObjectField)) {
1776
                    docIdList.add(docIdInObjectField);
1777
                }
1778
            }//while
1779
            //close the pStmt
1780
            pStmt.close();
1781
        }//try
1782
        catch (SQLException e) {
1783
            logMetacat.error("DBQuery.getCurrentDocidListForDataPackage - Error in getDocidListForDataPackage: "
1784
                    + e.getMessage());
1785
        }//catch
1786
        finally {
1787
            try {
1788
                pStmt.close();
1789
            }//try
1790
            catch (SQLException ee) {
1791
                logMetacat.error("DBQuery.getCurrentDocidListForDataPackage - SQL Error: "
1792
                                + ee.getMessage());
1793
            }//catch
1794
            finally {
1795
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1796
            }//fianlly
1797
        }//finally
1798
        return docIdList;
1799
    }//getCurrentDocidListForDataPackadge()
1800

    
1801
    /**
1802
     * Get all docIds list for a data packadge
1803
     *
1804
     * @param dataPackageDocid, the string in docId field of xml_relation table
1805
     */
1806
    private Vector getOldVersionDocidListForDataPackage(String dataPackageDocidWithRev)
1807
    {
1808

    
1809
        Vector docIdList = new Vector();//return value
1810
        Vector tripleList = null;
1811
        String xml = null;
1812

    
1813
        // Check the parameter
1814
        if (dataPackageDocidWithRev == null || dataPackageDocidWithRev.equals("")) { return docIdList; }//if
1815

    
1816
        try {
1817
            //initial a documentImpl object
1818
            DocumentImpl packageDocument = new DocumentImpl(dataPackageDocidWithRev);
1819
            //transfer to documentImpl object to string
1820
            xml = packageDocument.toString();
1821

    
1822
            //create a tripcollection object
1823
            TripleCollection tripleForPackage = new TripleCollection(
1824
                    new StringReader(xml));
1825
            //get the vetor of triples
1826
            tripleList = tripleForPackage.getCollection();
1827

    
1828
            for (int i = 0; i < tripleList.size(); i++) {
1829
                //put subject docid into docIdlist without duplicate
1830
                if (!docIdList.contains(((Triple) tripleList.elementAt(i))
1831
                        .getSubject())) {
1832
                    //put subject docid into docIdlist
1833
                    docIdList.add(((Triple) tripleList.get(i)).getSubject());
1834
                }
1835
                //put object docid into docIdlist without duplicate
1836
                if (!docIdList.contains(((Triple) tripleList.elementAt(i))
1837
                        .getObject())) {
1838
                    docIdList.add(((Triple) (tripleList.get(i))).getObject());
1839
                }
1840
            }//for
1841
        }//try
1842
        catch (Exception e) {
1843
            logMetacat.error("DBQuery.getCurrentDocidListForDataPackage - General error: "
1844
                    + e.getMessage());
1845
        }//catch
1846

    
1847
        // return result
1848
        return docIdList;
1849
    }//getDocidListForPackageInXMLRevisions()
1850

    
1851
    /**
1852
     * Check if the docId is a data packadge id. If the id is a data packadage
1853
     * id, it should be store in the docId fields in xml_relation table. So we
1854
     * can use a query to get the entries which the docId equals the given
1855
     * value. If the result is null. The docId is not a packadge id. Otherwise,
1856
     * it is.
1857
     *
1858
     * @param docId, the id need to be checked
1859
     */
1860
    private boolean isDataPackageId(String docId)
1861
    {
1862
        boolean result = false;
1863
        PreparedStatement pStmt = null;
1864
        ResultSet rs = null;
1865
        String query = "SELECT docId from xml_relation where docId = ?";
1866
        DBConnection dbConn = null;
1867
        int serialNumber = -1;
1868
        try {
1869
            dbConn = DBConnectionPool
1870
                    .getDBConnection("DBQuery.isDataPackageId");
1871
            serialNumber = dbConn.getCheckOutSerialNumber();
1872
            pStmt = dbConn.prepareStatement(query);
1873
            //bind the value to query
1874
            pStmt.setString(1, docId);
1875
            //execute the query
1876
            pStmt.execute();
1877
            rs = pStmt.getResultSet();
1878
            //process the result
1879
            if (rs.next()) //There are some records for the id in docId fields
1880
            {
1881
                result = true;//It is a data packadge id
1882
            }
1883
            pStmt.close();
1884
        }//try
1885
        catch (SQLException e) {
1886
            logMetacat.error("DBQuery.isDataPackageId - SQL Error: "
1887
                    + e.getMessage());
1888
        } finally {
1889
            try {
1890
                pStmt.close();
1891
            }//try
1892
            catch (SQLException ee) {
1893
                logMetacat.error("DBQuery.isDataPackageId - SQL Error in isDataPackageId: "
1894
                        + ee.getMessage());
1895
            }//catch
1896
            finally {
1897
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1898
            }//finally
1899
        }//finally
1900
        return result;
1901
    }//isDataPackageId()
1902

    
1903
    /**
1904
     * Check if the user has the permission to export data package
1905
     *
1906
     * @param conn, the connection
1907
     * @param docId, the id need to be checked
1908
     * @param user, the name of user
1909
     * @param groups, the user's group
1910
     */
1911
    private boolean hasPermissionToExportPackage(String docId, String user,
1912
            String[] groups) throws Exception
1913
    {
1914
        //DocumentImpl doc=new DocumentImpl(conn,docId);
1915
        return DocumentImpl.hasReadPermission(user, groups, docId);
1916
    }
1917

    
1918
    /**
1919
     * Get the current Rev for a docid in xml_documents table
1920
     *
1921
     * @param docId, the id need to get version numb If the return value is -5,
1922
     *            means no value in rev field for this docid
1923
     */
1924
    private int getCurrentRevFromXMLDoumentsTable(String docId)
1925
            throws SQLException
1926
    {
1927
        int rev = -5;
1928
        PreparedStatement pStmt = null;
1929
        ResultSet rs = null;
1930
        String query = "SELECT rev from xml_documents where docId = ?";
1931
        DBConnection dbConn = null;
1932
        int serialNumber = -1;
1933
        try {
1934
            dbConn = DBConnectionPool
1935
                    .getDBConnection("DBQuery.getCurrentRevFromXMLDocumentsTable");
1936
            serialNumber = dbConn.getCheckOutSerialNumber();
1937
            pStmt = dbConn.prepareStatement(query);
1938
            //bind the value to query
1939
            pStmt.setString(1, docId);
1940
            //execute the query
1941
            pStmt.execute();
1942
            rs = pStmt.getResultSet();
1943
            //process the result
1944
            if (rs.next()) //There are some records for rev
1945
            {
1946
                rev = rs.getInt(1);
1947
                ;//It is the version for given docid
1948
            } else {
1949
                rev = -5;
1950
            }
1951

    
1952
        }//try
1953
        catch (SQLException e) {
1954
            logMetacat.error("DBQuery.getCurrentRevFromXMLDoumentsTable - SQL Error: "
1955
                            + e.getMessage());
1956
            throw e;
1957
        }//catch
1958
        finally {
1959
            try {
1960
                pStmt.close();
1961
            }//try
1962
            catch (SQLException ee) {
1963
                logMetacat.error(
1964
                        "DBQuery.getCurrentRevFromXMLDoumentsTable - SQL Error: "
1965
                                + ee.getMessage());
1966
            }//catch
1967
            finally {
1968
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1969
            }//finally
1970
        }//finally
1971
        return rev;
1972
    }//getCurrentRevFromXMLDoumentsTable
1973

    
1974
    /**
1975
     * put a doc into a zip output stream
1976
     *
1977
     * @param docImpl, docmentImpl object which will be sent to zip output
1978
     *            stream
1979
     * @param zipOut, zip output stream which the docImpl will be put
1980
     * @param packageZipEntry, the zip entry name for whole package
1981
     */
1982
    private void addDocToZipOutputStream(DocumentImpl docImpl,
1983
            ZipOutputStream zipOut, String packageZipEntry)
1984
            throws ClassNotFoundException, IOException, SQLException,
1985
            McdbException, Exception
1986
    {
1987
        byte[] byteString = null;
1988
        ZipEntry zEntry = null;
1989

    
1990
        byteString = docImpl.toString().getBytes();
1991
        //use docId as the zip entry's name
1992
        zEntry = new ZipEntry(packageZipEntry + "/metadata/"
1993
                + docImpl.getDocID());
1994
        zEntry.setSize(byteString.length);
1995
        zipOut.putNextEntry(zEntry);
1996
        zipOut.write(byteString, 0, byteString.length);
1997
        zipOut.closeEntry();
1998

    
1999
    }//addDocToZipOutputStream()
2000

    
2001
    /**
2002
     * Transfer a docid vetor to a documentImpl vector. The documentImpl vetor
2003
     * only inlcudes current version. If a DocumentImple object couldn't find
2004
     * for a docid, then the String of this docid was added to vetor rather
2005
     * than DocumentImple object.
2006
     *
2007
     * @param docIdList, a vetor hold a docid list for a data package. In
2008
     *            docid, there is not version number in it.
2009
     */
2010

    
2011
    private Vector getCurrentAllDocumentImpl(Vector docIdList)
2012
            throws McdbException, Exception
2013
    {
2014
        //Connection dbConn=null;
2015
        Vector documentImplList = new Vector();
2016
        int rev = 0;
2017

    
2018
        // Check the parameter
2019
        if (docIdList.isEmpty()) { return documentImplList; }//if
2020

    
2021
        //for every docid in vector
2022
        for (int i = 0; i < docIdList.size(); i++) {
2023
            try {
2024
                //get newest version for this docId
2025
                rev = getCurrentRevFromXMLDoumentsTable((String) docIdList
2026
                        .elementAt(i));
2027

    
2028
                // There is no record for this docId in xml_documents table
2029
                if (rev == -5) {
2030
                    // Rather than put DocumentImple object, put a String
2031
                    // Object(docid)
2032
                    // into the documentImplList
2033
                    documentImplList.add((String) docIdList.elementAt(i));
2034
                    // Skip other code
2035
                    continue;
2036
                }
2037

    
2038
                String docidPlusVersion = ((String) docIdList.elementAt(i))
2039
                        + PropertyService.getProperty("document.accNumSeparator") + rev;
2040

    
2041
                //create new documentImpl object
2042
                DocumentImpl documentImplObject = new DocumentImpl(
2043
                        docidPlusVersion);
2044
                //add them to vector
2045
                documentImplList.add(documentImplObject);
2046
            }//try
2047
            catch (Exception e) {
2048
                logMetacat.error("DBQuery.getCurrentAllDocumentImpl - General error: "
2049
                        + e.getMessage());
2050
                // continue the for loop
2051
                continue;
2052
            }
2053
        }//for
2054
        return documentImplList;
2055
    }
2056

    
2057
    /**
2058
     * Transfer a docid vetor to a documentImpl vector. If a DocumentImple
2059
     * object couldn't find for a docid, then the String of this docid was
2060
     * added to vetor rather than DocumentImple object.
2061
     *
2062
     * @param docIdList, a vetor hold a docid list for a data package. In
2063
     *            docid, t here is version number in it.
2064
     */
2065
    private Vector getOldVersionAllDocumentImpl(Vector docIdList)
2066
    {
2067
        //Connection dbConn=null;
2068
        Vector documentImplList = new Vector();
2069
        String siteCode = null;
2070
        String uniqueId = null;
2071
        int rev = 0;
2072

    
2073
        // Check the parameter
2074
        if (docIdList.isEmpty()) { return documentImplList; }//if
2075

    
2076
        //for every docid in vector
2077
        for (int i = 0; i < docIdList.size(); i++) {
2078

    
2079
            String docidPlusVersion = (String) (docIdList.elementAt(i));
2080

    
2081
            try {
2082
                //create new documentImpl object
2083
                DocumentImpl documentImplObject = new DocumentImpl(
2084
                        docidPlusVersion);
2085
                //add them to vector
2086
                documentImplList.add(documentImplObject);
2087
            }//try
2088
            catch (McdbDocNotFoundException notFoundE) {
2089
                logMetacat.error("DBQuery.getOldVersionAllDocument - Error finding doc " 
2090
                		+ docidPlusVersion + " : " + notFoundE.getMessage());
2091
                // Rather than add a DocumentImple object into vetor, a String
2092
                // object
2093
                // - the doicd was added to the vector
2094
                documentImplList.add(docidPlusVersion);
2095
                // Continue the for loop
2096
                continue;
2097
            }//catch
2098
            catch (Exception e) {
2099
                logMetacat.error(
2100
                        "DBQuery.getOldVersionAllDocument - General error: "
2101
                                + e.getMessage());
2102
                // Continue the for loop
2103
                continue;
2104
            }//catch
2105

    
2106
        }//for
2107
        return documentImplList;
2108
    }//getOldVersionAllDocumentImple
2109

    
2110
    /**
2111
     * put a data file into a zip output stream
2112
     *
2113
     * @param docImpl, docmentImpl object which will be sent to zip output
2114
     *            stream
2115
     * @param zipOut, the zip output stream which the docImpl will be put
2116
     * @param packageZipEntry, the zip entry name for whole package
2117
     */
2118
    private void addDataFileToZipOutputStream(DocumentImpl docImpl,
2119
            ZipOutputStream zipOut, String packageZipEntry)
2120
            throws ClassNotFoundException, IOException, SQLException,
2121
            McdbException, Exception
2122
    {
2123
        byte[] byteString = null;
2124
        ZipEntry zEntry = null;
2125
        // this is data file; add file to zip
2126
        String filePath = PropertyService.getProperty("application.datafilepath");
2127
        if (!filePath.endsWith("/")) {
2128
            filePath += "/";
2129
        }
2130
        String fileName = filePath + docImpl.getDocID();
2131
        zEntry = new ZipEntry(packageZipEntry + "/data/" + docImpl.getDocID());
2132
        zipOut.putNextEntry(zEntry);
2133
        FileInputStream fin = null;
2134
        try {
2135
            fin = new FileInputStream(fileName);
2136
            byte[] buf = new byte[4 * 1024]; // 4K buffer
2137
            int b = fin.read(buf);
2138
            while (b != -1) {
2139
                zipOut.write(buf, 0, b);
2140
                b = fin.read(buf);
2141
            }//while
2142
            zipOut.closeEntry();
2143
        }//try
2144
        catch (IOException ioe) {
2145
            logMetacat.error("DBQuery.addDataFileToZipOutputStream - I/O error: "
2146
                    + ioe.getMessage());
2147
        }//catch
2148
    }//addDataFileToZipOutputStream()
2149

    
2150
    /**
2151
     * create a html summary for data package and put it into zip output stream
2152
     *
2153
     * @param docImplList, the documentImpl ojbects in data package
2154
     * @param zipOut, the zip output stream which the html should be put
2155
     * @param packageZipEntry, the zip entry name for whole package
2156
     */
2157
    private void addHtmlSummaryToZipOutputStream(Vector docImplList,
2158
            ZipOutputStream zipOut, String packageZipEntry) throws Exception
2159
    {
2160
        StringBuffer htmlDoc = new StringBuffer();
2161
        ZipEntry zEntry = null;
2162
        byte[] byteString = null;
2163
        InputStream source;
2164
        DBTransform xmlToHtml;
2165

    
2166
        //create a DBTransform ojbect
2167
        xmlToHtml = new DBTransform();
2168
        //head of html
2169
        htmlDoc.append("<html><head></head><body>");
2170
        for (int i = 0; i < docImplList.size(); i++) {
2171
            // If this String object, this means it is missed data file
2172
            if ((((docImplList.elementAt(i)).getClass()).toString())
2173
                    .equals("class java.lang.String")) {
2174

    
2175
                htmlDoc.append("<a href=\"");
2176
                String dataFileid = (String) docImplList.elementAt(i);
2177
                htmlDoc.append("./data/").append(dataFileid).append("\">");
2178
                htmlDoc.append("Data File: ");
2179
                htmlDoc.append(dataFileid).append("</a><br>");
2180
                htmlDoc.append("<br><hr><br>");
2181

    
2182
            }//if
2183
            else if ((((DocumentImpl) docImplList.elementAt(i)).getDoctype())
2184
                    .compareTo("BIN") != 0) { //this is an xml file so we can
2185
                                              // transform it.
2186
                //transform each file individually then concatenate all of the
2187
                //transformations together.
2188

    
2189
                //for metadata xml title
2190
                htmlDoc.append("<h2>");
2191
                htmlDoc.append(((DocumentImpl) docImplList.elementAt(i))
2192
                        .getDocID());
2193
                //htmlDoc.append(".");
2194
                //htmlDoc.append(((DocumentImpl)docImplList.elementAt(i)).getRev());
2195
                htmlDoc.append("</h2>");
2196
                //do the actual transform
2197
                StringWriter docString = new StringWriter();
2198
                xmlToHtml.transformXMLDocument(((DocumentImpl) docImplList
2199
                        .elementAt(i)).toString(), "-//NCEAS//eml-generic//EN",
2200
                        "-//W3C//HTML//EN", "html", docString, null, null);
2201
                htmlDoc.append(docString.toString());
2202
                htmlDoc.append("<br><br><hr><br><br>");
2203
            }//if
2204
            else { //this is a data file so we should link to it in the html
2205
                htmlDoc.append("<a href=\"");
2206
                String dataFileid = ((DocumentImpl) docImplList.elementAt(i))
2207
                        .getDocID();
2208
                htmlDoc.append("./data/").append(dataFileid).append("\">");
2209
                htmlDoc.append("Data File: ");
2210
                htmlDoc.append(dataFileid).append("</a><br>");
2211
                htmlDoc.append("<br><hr><br>");
2212
            }//else
2213
        }//for
2214
        htmlDoc.append("</body></html>");
2215
        byteString = htmlDoc.toString().getBytes();
2216
        zEntry = new ZipEntry(packageZipEntry + "/metadata.html");
2217
        zEntry.setSize(byteString.length);
2218
        zipOut.putNextEntry(zEntry);
2219
        zipOut.write(byteString, 0, byteString.length);
2220
        zipOut.closeEntry();
2221
        //dbConn.close();
2222

    
2223
    }//addHtmlSummaryToZipOutputStream
2224

    
2225
    /**
2226
     * put a data packadge into a zip output stream
2227
     *
2228
     * @param docId, which the user want to put into zip output stream,it has version
2229
     * @param out, a servletoutput stream which the zip output stream will be
2230
     *            put
2231
     * @param user, the username of the user
2232
     * @param groups, the group of the user
2233
     */
2234
    public ZipOutputStream getZippedPackage(String docIdString,
2235
            ServletOutputStream out, String user, String[] groups,
2236
            String passWord) throws ClassNotFoundException, IOException,
2237
            SQLException, McdbException, NumberFormatException, Exception
2238
    {
2239
        ZipOutputStream zOut = null;
2240
        String elementDocid = null;
2241
        DocumentImpl docImpls = null;
2242
        //Connection dbConn = null;
2243
        Vector docIdList = new Vector();
2244
        Vector documentImplList = new Vector();
2245
        Vector htmlDocumentImplList = new Vector();
2246
        String packageId = null;
2247
        String rootName = "package";//the package zip entry name
2248

    
2249
        String docId = null;
2250
        int version = -5;
2251
        // Docid without revision
2252
        docId = DocumentUtil.getDocIdFromString(docIdString);
2253
        // revision number
2254
        version = DocumentUtil.getVersionFromString(docIdString);
2255

    
2256
        //check if the reqused docId is a data package id
2257
        if (!isDataPackageId(docId)) {
2258

    
2259
            /*
2260
             * Exception e = new Exception("The request the doc id "
2261
             * +docIdString+ " is not a data package id");
2262
             */
2263

    
2264
            //CB 1/6/03: if the requested docid is not a datapackage, we just
2265
            // zip
2266
            //up the single document and return the zip file.
2267
            if (!hasPermissionToExportPackage(docId, user, groups)) {
2268

    
2269
                Exception e = new Exception("User " + user
2270
                        + " does not have permission"
2271
                        + " to export the data package " + docIdString);
2272
                throw e;
2273
            }
2274

    
2275
            docImpls = new DocumentImpl(docIdString);
2276
            //checking if the user has the permission to read the documents
2277
            if (DocumentImpl.hasReadPermission(user, groups, docImpls
2278
                    .getDocID())) {
2279
                zOut = new ZipOutputStream(out);
2280
                //if the docImpls is metadata
2281
                if ((docImpls.getDoctype()).compareTo("BIN") != 0) {
2282
                    //add metadata into zip output stream
2283
                    addDocToZipOutputStream(docImpls, zOut, rootName);
2284
                }//if
2285
                else {
2286
                    //it is data file
2287
                    addDataFileToZipOutputStream(docImpls, zOut, rootName);
2288
                    htmlDocumentImplList.add(docImpls);
2289
                }//else
2290
            }//if
2291

    
2292
            zOut.finish(); //terminate the zip file
2293
            return zOut;
2294
        }
2295
        // Check the permission of user
2296
        else if (!hasPermissionToExportPackage(docId, user, groups)) {
2297

    
2298
            Exception e = new Exception("User " + user
2299
                    + " does not have permission"
2300
                    + " to export the data package " + docIdString);
2301
            throw e;
2302
        } else //it is a packadge id
2303
        {
2304
            //store the package id
2305
            packageId = docId;
2306
            //get current version in database
2307
            int currentVersion = getCurrentRevFromXMLDoumentsTable(packageId);
2308
            //If it is for current version (-1 means user didn't specify
2309
            // revision)
2310
            if ((version == -1) || version == currentVersion) {
2311
                //get current version number
2312
                version = currentVersion;
2313
                //get package zip entry name
2314
                //it should be docId.revsion.package
2315
                rootName = packageId + PropertyService.getProperty("document.accNumSeparator")
2316
                        + version + PropertyService.getProperty("document.accNumSeparator")
2317
                        + "package";
2318
                //get the whole id list for data packadge
2319
                docIdList = getCurrentDocidListForDataPackage(packageId);
2320
                //get the whole documentImple object
2321
                documentImplList = getCurrentAllDocumentImpl(docIdList);
2322

    
2323
            }//if
2324
            else if (version > currentVersion || version < -1) {
2325
                throw new Exception("The user specified docid: " + docId + "."
2326
                        + version + " doesn't exist");
2327
            }//else if
2328
            else //for an old version
2329
            {
2330

    
2331
                rootName = docIdString
2332
                        + PropertyService.getProperty("document.accNumSeparator") + "package";
2333
                //get the whole id list for data packadge
2334
                docIdList = getOldVersionDocidListForDataPackage(docIdString);
2335

    
2336
                //get the whole documentImple object
2337
                documentImplList = getOldVersionAllDocumentImpl(docIdList);
2338
            }//else
2339

    
2340
            // Make sure documentImplist is not empty
2341
            if (documentImplList.isEmpty()) { throw new Exception(
2342
                    "Couldn't find component for data package: " + packageId); }//if
2343

    
2344
            zOut = new ZipOutputStream(out);
2345
            //put every element into zip output stream
2346
            for (int i = 0; i < documentImplList.size(); i++) {
2347
                // if the object in the vetor is String, this means we couldn't
2348
                // find
2349
                // the document locally, we need find it remote
2350
                if ((((documentImplList.elementAt(i)).getClass()).toString())
2351
                        .equals("class java.lang.String")) {
2352
                    // Get String object from vetor
2353
                    String documentId = (String) documentImplList.elementAt(i);
2354
                    logMetacat.info("DBQuery.getZippedPackage - docid: " + documentId);
2355
                    // Get doicd without revision
2356
                    String docidWithoutRevision = 
2357
                    	DocumentUtil.getDocIdFromString(documentId);
2358
                    logMetacat.info("DBQuery.getZippedPackage - docidWithoutRevsion: "
2359
                            + docidWithoutRevision);
2360
                    // Get revision
2361
                    String revision = 
2362
                    	DocumentUtil.getRevisionStringFromString(documentId);
2363
                    logMetacat.info("DBQuery.getZippedPackage - revision from docIdentifier: "
2364
                            + revision);
2365
                    // Zip entry string
2366
                    String zipEntryPath = rootName + "/data/";
2367
                    // Create a RemoteDocument object
2368
                    RemoteDocument remoteDoc = new RemoteDocument(
2369
                            docidWithoutRevision, revision, user, passWord,
2370
                            zipEntryPath);
2371
                    // Here we only read data file from remote metacat
2372
                    String docType = remoteDoc.getDocType();
2373
                    if (docType != null) {
2374
                        if (docType.equals("BIN")) {
2375
                            // Put remote document to zip output
2376
                            remoteDoc.readDocumentFromRemoteServerByZip(zOut);
2377
                            // Add String object to htmlDocumentImplList
2378
                            String elementInHtmlList = remoteDoc
2379
                                    .getDocIdWithoutRevsion()
2380
                                    + PropertyService.getProperty("document.accNumSeparator")
2381
                                    + remoteDoc.getRevision();
2382
                            htmlDocumentImplList.add(elementInHtmlList);
2383
                        }//if
2384
                    }//if
2385

    
2386
                }//if
2387
                else {
2388
                    //create a docmentImpls object (represent xml doc) base on
2389
                    // the docId
2390
                    docImpls = (DocumentImpl) documentImplList.elementAt(i);
2391
                    //checking if the user has the permission to read the
2392
                    // documents
2393
                    if (DocumentImpl.hasReadPermission(user, groups, docImpls
2394
                            .getDocID())) {
2395
                        //if the docImpls is metadata
2396
                        if ((docImpls.getDoctype()).compareTo("BIN") != 0) {
2397
                            //add metadata into zip output stream
2398
                            addDocToZipOutputStream(docImpls, zOut, rootName);
2399
                            //add the documentImpl into the vetor which will
2400
                            // be used in html
2401
                            htmlDocumentImplList.add(docImpls);
2402

    
2403
                        }//if
2404
                        else {
2405
                            //it is data file
2406
                            addDataFileToZipOutputStream(docImpls, zOut,
2407
                                    rootName);
2408
                            htmlDocumentImplList.add(docImpls);
2409
                        }//else
2410
                    }//if
2411
                }//else
2412
            }//for
2413

    
2414
            //add html summary file
2415
            addHtmlSummaryToZipOutputStream(htmlDocumentImplList, zOut,
2416
                    rootName);
2417
            zOut.finish(); //terminate the zip file
2418
            //dbConn.close();
2419
            return zOut;
2420
        }//else
2421
    }//getZippedPackage()
2422

    
2423
    private class ReturnFieldValue
2424
    {
2425

    
2426
        private String docid = null; //return field value for this docid
2427

    
2428
        private String fieldValue = null;
2429

    
2430
        private String xmlFieldValue = null; //return field value in xml
2431
                                             // format
2432
        private String fieldType = null; //ATTRIBUTE, TEXT...
2433

    
2434
        public void setDocid(String myDocid)
2435
        {
2436
            docid = myDocid;
2437
        }
2438

    
2439
        public String getDocid()
2440
        {
2441
            return docid;
2442
        }
2443

    
2444
        public void setFieldValue(String myValue)
2445
        {
2446
            fieldValue = myValue;
2447
        }
2448

    
2449
        public String getFieldValue()
2450
        {
2451
            return fieldValue;
2452
        }
2453

    
2454
        public void setXMLFieldValue(String xml)
2455
        {
2456
            xmlFieldValue = xml;
2457
        }
2458

    
2459
        public String getXMLFieldValue()
2460
        {
2461
            return xmlFieldValue;
2462
        }
2463
        
2464
        public void setFieldType(String myType)
2465
        {
2466
            fieldType = myType;
2467
        }
2468

    
2469
        public String getFieldType()
2470
        {
2471
            return fieldType;
2472
        }
2473

    
2474
    }
2475
    
2476
    /**
2477
     * a class to store one result document consisting of a docid and a document
2478
     */
2479
    private class ResultDocument
2480
    {
2481
      public String docid;
2482
      public String document;
2483
      
2484
      public ResultDocument(String docid, String document)
2485
      {
2486
        this.docid = docid;
2487
        this.document = document;
2488
      }
2489
    }
2490
    
2491
    /**
2492
     * a private class to handle a set of resultDocuments
2493
     */
2494
    private class ResultDocumentSet
2495
    {
2496
      private Vector docids;
2497
      private Vector documents;
2498
      
2499
      public ResultDocumentSet()
2500
      {
2501
        docids = new Vector();
2502
        documents = new Vector();
2503
      }
2504
      
2505
      /**
2506
       * adds a result document to the set
2507
       */
2508
      public void addResultDocument(ResultDocument rd)
2509
      {
2510
        if(rd.docid == null)
2511
          return;
2512
        if(rd.document == null)
2513
          rd.document = "";
2514
       
2515
           docids.addElement(rd.docid);
2516
           documents.addElement(rd.document);
2517
        
2518
      }
2519
      
2520
      /**
2521
       * gets an iterator of docids
2522
       */
2523
      public Iterator getDocids()
2524
      {
2525
        return docids.iterator();
2526
      }
2527
      
2528
      /**
2529
       * gets an iterator of documents
2530
       */
2531
      public Iterator getDocuments()
2532
      {
2533
        return documents.iterator();
2534
      }
2535
      
2536
      /**
2537
       * returns the size of the set
2538
       */
2539
      public int size()
2540
      {
2541
        return docids.size();
2542
      }
2543
      
2544
      /**
2545
       * tests to see if this set contains the given docid
2546
       */
2547
      private boolean containsDocid(String docid)
2548
      {
2549
        for(int i=0; i<docids.size(); i++)
2550
        {
2551
          String docid0 = (String)docids.elementAt(i);
2552
          if(docid0.trim().equals(docid.trim()))
2553
          {
2554
            return true;
2555
          }
2556
        }
2557
        return false;
2558
      }
2559
      
2560
      /**
2561
       * removes the element with the given docid
2562
       */
2563
      public String remove(String docid)
2564
      {
2565
        for(int i=0; i<docids.size(); i++)
2566
        {
2567
          String docid0 = (String)docids.elementAt(i);
2568
          if(docid0.trim().equals(docid.trim()))
2569
          {
2570
            String returnDoc = (String)documents.elementAt(i);
2571
            documents.remove(i);
2572
            docids.remove(i);
2573
            return returnDoc;
2574
          }
2575
        }
2576
        return null;
2577
      }
2578
      
2579
      /**
2580
       * add a result document
2581
       */
2582
      public void put(ResultDocument rd)
2583
      {
2584
        addResultDocument(rd);
2585
      }
2586
      
2587
      /**
2588
       * add a result document by components
2589
       */
2590
      public void put(String docid, String document)
2591
      {
2592
        addResultDocument(new ResultDocument(docid, document));
2593
      }
2594
      
2595
      /**
2596
       * get the document part of the result document by docid
2597
       */
2598
      public Object get(String docid)
2599
      {
2600
        for(int i=0; i<docids.size(); i++)
2601
        {
2602
          String docid0 = (String)docids.elementAt(i);
2603
          if(docid0.trim().equals(docid.trim()))
2604
          {
2605
            return documents.elementAt(i);
2606
          }
2607
        }
2608
        return null;
2609
      }
2610
      
2611
      /**
2612
       * get the document part of the result document by an object
2613
       */
2614
      public Object get(Object o)
2615
      {
2616
        return get((String)o);
2617
      }
2618
      
2619
      /**
2620
       * get an entire result document by index number
2621
       */
2622
      public ResultDocument get(int index)
2623
      {
2624
        return new ResultDocument((String)docids.elementAt(index), 
2625
          (String)documents.elementAt(index));
2626
      }
2627
      
2628
      /**
2629
       * return a string representation of this object
2630
       */
2631
      public String toString()
2632
      {
2633
        String s = "";
2634
        for(int i=0; i<docids.size(); i++)
2635
        {
2636
          s += (String)docids.elementAt(i) + "\n";
2637
        }
2638
        return s;
2639
      }
2640
      /*
2641
       * Set a new document value for a given docid
2642
       */
2643
      public void set(String docid, String document)
2644
      {
2645
    	   for(int i=0; i<docids.size(); i++)
2646
           {
2647
             String docid0 = (String)docids.elementAt(i);
2648
             if(docid0.trim().equals(docid.trim()))
2649
             {
2650
                 documents.set(i, document);
2651
             }
2652
           }
2653
           
2654
      }
2655
    }
2656
}
(17-17/63)