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: leinfelder $'
13
 *     '$Date: 2010-12-27 14:47:01 -0800 (Mon, 27 Dec 2010) $'
14
 * '$Revision: 5760 $'
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.BufferedWriter;
34
import java.io.File;
35
import java.io.FileInputStream;
36
import java.io.FileOutputStream;
37
import java.io.IOException;
38
import java.io.InputStream;
39
import java.io.InputStreamReader;
40
import java.io.OutputStreamWriter;
41
import java.io.Reader;
42
import java.io.StringReader;
43
import java.io.StringWriter;
44
import java.io.Writer;
45
import java.sql.PreparedStatement;
46
import java.sql.ResultSet;
47
import java.sql.SQLException;
48
import java.util.Enumeration;
49
import java.util.Hashtable;
50
import java.util.Iterator;
51
import java.util.StringTokenizer;
52
import java.util.Vector;
53
import java.util.zip.ZipEntry;
54
import java.util.zip.ZipOutputStream;
55

    
56
import javax.servlet.ServletOutputStream;
57
import javax.servlet.http.HttpServletResponse;
58

    
59
import org.apache.log4j.Logger;
60

    
61
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlInterface;
62
import edu.ucsb.nceas.metacat.database.DBConnection;
63
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
64
import edu.ucsb.nceas.metacat.properties.PropertyService;
65
import edu.ucsb.nceas.metacat.util.AuthUtil;
66
import edu.ucsb.nceas.metacat.util.DocumentUtil;
67
import edu.ucsb.nceas.metacat.util.MetacatUtil;
68
import edu.ucsb.nceas.morpho.datapackage.Triple;
69
import edu.ucsb.nceas.morpho.datapackage.TripleCollection;
70
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
71

    
72

    
73
/**
74
 * A Class that searches a relational DB for elements and attributes that have
75
 * free text matches a query string, or structured query matches to a path
76
 * specified node in the XML hierarchy. It returns a result set consisting of
77
 * the document ID for each document that satisfies the query
78
 */
79
public class DBQuery
80
{
81

    
82
    static final int ALL = 1;
83

    
84
    static final int WRITE = 2;
85

    
86
    static final int READ = 4;
87
    
88
    private String qformat = "xml";
89

    
90
    //private Connection conn = null;
91
    private String parserName = null;
92

    
93
    private Logger logMetacat = Logger.getLogger(DBQuery.class);
94

    
95
    /** true if the metacat spatial option is installed **/
96
    private final boolean METACAT_SPATIAL = true;
97

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

    
120
    // Size of page for non paged query
121
    private static final int NONPAGESIZE = 99999999;
122
    /**
123
     * the main routine used to test the DBQuery utility.
124
     * <p>
125
     * Usage: java DBQuery <xmlfile>
126
     * NOTE: encoding should be provided for best results
127
     * @param xmlfile the filename of the xml file containing the query
128
     */
129
    static public void main(String[] args)
130
    {
131

    
132
        if (args.length < 1) {
133
            System.err.println("Wrong number of arguments!!!");
134
            System.err.println("USAGE: java DBQuery [-t] [-index] <xmlfile>");
135
            return;
136
        } else {
137
            try {
138

    
139
                int i = 0;
140
                boolean showRuntime = false;
141
                boolean useXMLIndex = false;
142
                if (args[i].equals("-t")) {
143
                    showRuntime = true;
144
                    i++;
145
                }
146
                if (args[i].equals("-index")) {
147
                    useXMLIndex = true;
148
                    i++;
149
                }
150
                String xmlfile = args[i];
151

    
152
                // Time the request if asked for
153
                double startTime = System.currentTimeMillis();
154

    
155
                // Open a connection to the database
156
                //Connection dbconn = util.openDBConnection();
157

    
158
                double connTime = System.currentTimeMillis();
159

    
160
                // Execute the query
161
                DBQuery queryobj = new DBQuery();
162
                Reader xml = new InputStreamReader(new FileInputStream(new File(xmlfile)));
163
                Hashtable nodelist = null;
164
                //nodelist = queryobj.findDocuments(xml, null, null, useXMLIndex);
165

    
166
                // Print the reulting document listing
167
                StringBuffer result = new StringBuffer();
168
                String document = null;
169
                String docid = null;
170
                result.append("<?xml version=\"1.0\"?>\n");
171
                result.append("<resultset>\n");
172

    
173
                if (!showRuntime) {
174
                    Enumeration doclist = nodelist.keys();
175
                    while (doclist.hasMoreElements()) {
176
                        docid = (String) doclist.nextElement();
177
                        document = (String) nodelist.get(docid);
178
                        result.append("  <document>\n    " + document
179
                                + "\n  </document>\n");
180
                    }
181

    
182
                    result.append("</resultset>\n");
183
                }
184
                // Time the request if asked for
185
                double stopTime = System.currentTimeMillis();
186
                double dbOpenTime = (connTime - startTime) / 1000;
187
                double readTime = (stopTime - connTime) / 1000;
188
                double executionTime = (stopTime - startTime) / 1000;
189
                if (showRuntime) {
190
                    System.out.print("  " + executionTime);
191
                    System.out.print("  " + dbOpenTime);
192
                    System.out.print("  " + readTime);
193
                    System.out.print("  " + nodelist.size());
194
                    System.out.println();
195
                }
196
                //System.out.println(result);
197
                //write into a file "result.txt"
198
                if (!showRuntime) {
199
                    File f = new File("./result.txt");
200
                    Writer fw = new OutputStreamWriter(new FileOutputStream(f));
201
                    BufferedWriter out = new BufferedWriter(fw);
202
                    out.write(result.toString());
203
                    out.flush();
204
                    out.close();
205
                    fw.close();
206
                }
207

    
208
            } catch (Exception e) {
209
                System.err.println("Error in DBQuery.main");
210
                System.err.println(e.getMessage());
211
                e.printStackTrace(System.err);
212
            }
213
        }
214
    }
215

    
216
    /**
217
     * construct an instance of the DBQuery class
218
     *
219
     * <p>
220
     * Generally, one would call the findDocuments() routine after creating an
221
     * instance to specify the search query
222
     * </p>
223
     *
224

    
225
     * @param parserName the fully qualified name of a Java class implementing
226
     *            the org.xml.sax.XMLReader interface
227
     */
228
    public DBQuery() throws PropertyNotFoundException
229
    {
230
        String parserName = PropertyService.getProperty("xml.saxparser");
231
        this.parserName = parserName;
232
    }
233

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

    
291
  /**
292
   * Method put the search result set into out printerwriter
293
   * @param resoponse the return response
294
   * @param out the output printer
295
   * @param params the paratermer hashtable
296
   * @param user the user name (it maybe different to the one in param)
297
   * @param groups the group array
298
   * @param sessionid  the sessionid
299
   */
300
  public void findDocuments(HttpServletResponse response,
301
                                       Writer out, Hashtable params,
302
                                       String user, String[] groups,
303
                                       String sessionid) throws PropertyNotFoundException
304
  {
305
    boolean useXMLIndex = (new Boolean(PropertyService.getProperty("database.usexmlindex")))
306
               .booleanValue();
307
    findDocuments(response, out, params, user, groups, sessionid, useXMLIndex);
308

    
309
  }
310

    
311

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

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

    
376

    
377

    
378
      if (qformat != null && qformat.equals(MetacatUtil.XMLFORMAT))
379
      {
380
        //xml format
381
        if(response != null)
382
        {
383
            response.setContentType("text/xml");
384
        }
385
        createResultDocument(xmlquery, qspec, out, user, groups, useXMLIndex, 
386
          pagesize, pagestart, sessionid, qformat);
387
      }//if
388
      else
389
      {
390
        //knb format, in this case we will get whole result and sent it out
391
        response.setContentType("text/html");
392
        Writer nonout = null;
393
        StringBuffer xml = createResultDocument(xmlquery, qspec, nonout, user,
394
                                                groups, useXMLIndex, pagesize, 
395
                                                pagestart, sessionid, qformat);
396
        
397
        //transfer the xml to html
398
        try
399
        {
400
         long startHTMLTransform = System.currentTimeMillis();
401
         DBTransform trans = new DBTransform();
402
         response.setContentType("text/html");
403

    
404
         // if the user is a moderator, then pass a param to the 
405
         // xsl specifying the fact
406
         if(AuthUtil.isModerator(user, groups)){
407
        	 params.put("isModerator", new String[] {"true"});
408
         }
409

    
410
         trans.transformXMLDocument(xml.toString(), "-//NCEAS//resultset//EN",
411
                                 "-//W3C//HTML//EN", qformat, out, params,
412
                                 sessionid);
413
         long transformRunTime = System.currentTimeMillis() - startHTMLTransform;
414
         
415
         transferWarnLimit = Long.parseLong(PropertyService.getProperty("dbquery.transformTimeWarnLimit"));
416
         
417
         if (transformRunTime > transferWarnLimit) {
418
         	logMetacat.warn("DBQuery.findDocuments - The time to transfrom resultset from xml to html format is "
419
                  		                             + transformRunTime);
420
         }
421
          MetacatUtil.writeDebugToFile("---------------------------------------------------------------------------------------------------------------Transfrom xml to html  "
422
                             + transformRunTime);
423
          MetacatUtil.writeDebugToDelimiteredFile(" " + transformRunTime, false);
424
        }
425
        catch(Exception e)
426
        {
427
         logMetacat.error("DBQuery.findDocuments - Error in MetaCatServlet.transformResultset:"
428
                                +e.getMessage());
429
         }
430

    
431
      }//else
432

    
433
  }
434
    
435
    
436
  
437
  /**
438
   * Transforms a hashtable of documents to an xml or html result and sent
439
   * the content to outputstream. Keep going untill hastable is empty. stop it.
440
   * add the QuerySpecification as parameter is for ecogrid. But it is duplicate
441
   * to xmlquery String
442
   * @param xmlquery
443
   * @param qspec
444
   * @param out
445
   * @param user
446
   * @param groups
447
   * @param useXMLIndex
448
   * @param sessionid
449
   * @return
450
   */
451
    public StringBuffer createResultDocument(String xmlquery,
452
                                              QuerySpecification qspec,
453
                                              Writer out,
454
                                              String user, String[] groups,
455
                                              boolean useXMLIndex)
456
    {
457
    	return createResultDocument(xmlquery,qspec,out, user,groups, useXMLIndex, 0, 0,"", qformat);
458
    }
459

    
460
  /*
461
   * Transforms a hashtable of documents to an xml or html result and sent
462
   * the content to outputstream. Keep going untill hastable is empty. stop it.
463
   * add the QuerySpecification as parameter is for ecogrid. But it is duplicate
464
   * to xmlquery String
465
   */
466
  public StringBuffer createResultDocument(String xmlquery,
467
                                            QuerySpecification qspec,
468
                                            Writer out,
469
                                            String user, String[] groups,
470
                                            boolean useXMLIndex, int pagesize,
471
                                            int pagestart, String sessionid, 
472
                                            String qformat)
473
  {
474
    DBConnection dbconn = null;
475
    int serialNumber = -1;
476
    StringBuffer resultset = new StringBuffer();
477

    
478
    //try to get the cached version first    
479
    // Hashtable sessionHash = MetaCatServlet.getSessionHash();
480
    // HttpSession sess = (HttpSession)sessionHash.get(sessionid);
481

    
482
    
483
    resultset.append("<?xml version=\"1.0\"?>\n");
484
    resultset.append("<resultset>\n");
485
    resultset.append("  <pagestart>" + pagestart + "</pagestart>\n");
486
    resultset.append("  <pagesize>" + pagesize + "</pagesize>\n");
487
    resultset.append("  <nextpage>" + (pagestart + 1) + "</nextpage>\n");
488
    resultset.append("  <previouspage>" + (pagestart - 1) + "</previouspage>\n");
489

    
490
    resultset.append("  <query>" + xmlquery + "</query>");
491
    //send out a new query
492
    if (out != null)
493
    {
494
    	try {
495
    	  out.write(resultset.toString());
496
		} catch (IOException e) {
497
			logMetacat.error(e.getMessage(), e);
498
		}
499
    }
500
    if (qspec != null)
501
    {
502
      try
503
      {
504

    
505
        //checkout the dbconnection
506
        dbconn = DBConnectionPool.getDBConnection("DBQuery.findDocuments");
507
        serialNumber = dbconn.getCheckOutSerialNumber();
508

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

    
567
    //default to returning the whole resultset
568
    return resultset;
569
  }//createResultDocuments
570

    
571
    /*
572
     * Find the doc list which match the query
573
     */
574
    private StringBuffer findResultDoclist(QuerySpecification qspec,
575
                                      Writer out,
576
                                      String user, String[]groups,
577
                                      DBConnection dbconn, boolean useXMLIndex,
578
                                      int pagesize, int pagestart, String sessionid, 
579
                                      Vector givenDocids, String qformat)
580
                                      throws Exception
581
    {
582
      StringBuffer resultsetBuffer = new StringBuffer();
583
      String query = null;
584
      int count = 0;
585
      int index = 0;
586
      ResultDocumentSet docListResult = new ResultDocumentSet();
587
      PreparedStatement pstmt = null;
588
      String docid = null;
589
      String docname = null;
590
      String doctype = null;
591
      String createDate = null;
592
      String updateDate = null;
593
      StringBuffer document = null;
594
      boolean lastpage = false;
595
      int rev = 0;
596
      double startTime = 0;
597
      int offset = 1;
598
      long startSelectionTime = System.currentTimeMillis();
599
      ResultSet rs = null;
600
           
601
   
602
      // this is a hack for offset. in postgresql 7, if the returned docid list is too long,
603
      //the extend query which base on the docid will be too long to be run. So we 
604
      // have to cut them into different parts. Page query don't need it somehow.
605
      if (out == null)
606
      {
607
        // for html page, we put everything into one page
608
        offset =
609
            (new Integer(PropertyService.getProperty("database.webResultsetSize"))).intValue();
610
      }
611
      else
612
      {
613
          offset =
614
              (new Integer(PropertyService.getProperty("database.appResultsetSize"))).intValue();
615
      }
616

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

    
683
      double queryExecuteTime = System.currentTimeMillis() / 1000;
684
      logMetacat.debug("DBQuery.findResultDoclist - Time to execute select docid query is "
685
                    + (queryExecuteTime - startTime));
686
      MetacatUtil.writeDebugToFile("\n\n\n\n\n\nExecute selection query  "
687
              + (queryExecuteTime - startTime));
688
      MetacatUtil.writeDebugToDelimiteredFile(""+(queryExecuteTime - startTime), false);
689

    
690
      boolean tableHasRows = rs.next();
691
      
692
      if(pagesize == 0)
693
      { //this makes sure we get all results if there is no paging
694
        pagesize = NONPAGESIZE;
695
        pagestart = NONPAGESIZE;
696
      } 
697
      
698
      int currentIndex = 0;
699
      while (tableHasRows)
700
      {
701
        logMetacat.debug("DBQuery.findResultDoclist - getting result: " + currentIndex);
702
        docid = rs.getString(1).trim();
703
        logMetacat.debug("DBQuery.findResultDoclist -  processing: " + docid);
704
        docname = rs.getString(2);
705
        doctype = rs.getString(3);
706
        logMetacat.debug("DBQuery.findResultDoclist - processing: " + doctype);
707
        createDate = rs.getString(4);
708
        updateDate = rs.getString(5);
709
        rev = rs.getInt(6);
710
        
711
         Vector returndocVec = qspec.getReturnDocList();
712
       if (returndocVec.size() == 0 || returndocVec.contains(doctype))
713
        {
714
          logMetacat.debug("DBQuery.findResultDoclist - NOT Back tracing now...");
715
           document = new StringBuffer();
716

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

    
811
     resultsetBuffer.append("\n<lastpage>" + lastpage + "</lastpage>\n");
812
     if (out != null)
813
     {
814
         out.write("\n<lastpage>" + lastpage + "</lastpage>\n");
815
     }
816
     
817
     // now we only cached none-paged query and user is public
818
     if (user != null && user.equalsIgnoreCase("public") 
819
    		 && pagesize == NONPAGESIZE && PropertyService.getProperty("database.queryCacheOn").equals("true"))
820
     {
821
       //System.out.println("the string stored into cache is "+ resultsetBuffer.toString());
822
  	   storeQueryResultIntoCache(selectionAndExtendedQuery, resultsetBuffer.toString());
823
     }
824
          
825
     return resultsetBuffer;
826
    }//findReturnDoclist
827

    
828

    
829
    /*
830
     * Send completed search hashtable(part of reulst)to output stream
831
     * and buffer into a buffer stream
832
     */
833
    private StringBuffer handleSubsetResult(QuerySpecification qspec,
834
                                           StringBuffer resultset,
835
                                           Writer out, ResultDocumentSet partOfDoclist,
836
                                           String user, String[]groups,
837
                                       DBConnection dbconn, boolean useXMLIndex,
838
                                       String qformat)
839
                                       throws Exception
840
   {
841
     double startReturnFieldTime = System.currentTimeMillis();
842
     // check if there is a record in xml_returnfield
843
     // and get the returnfield_id and usage count
844
     int usage_count = getXmlReturnfieldsTableId(qspec, dbconn);
845
     boolean enterRecords = false;
846

    
847
     // get value of database.xmlReturnfieldCount
848
     int count = (new Integer(PropertyService
849
                            .getProperty("database.xmlReturnfieldCount")))
850
                            .intValue();
851

    
852
     // set enterRecords to true if usage_count is more than the offset
853
     // specified in metacat.properties
854
     if(usage_count > count){
855
         enterRecords = true;
856
     }
857

    
858
     if(returnfield_id < 0){
859
         logMetacat.warn("DBQuery.handleSubsetResult - Error in getting returnfield id from"
860
                                  + "xml_returnfield table");
861
         enterRecords = false;
862
     }
863

    
864
     // get the hashtable containing the docids that already in the
865
     // xml_queryresult table
866
     logMetacat.info("DBQuery.handleSubsetResult - size of partOfDoclist before"
867
                             + " docidsInQueryresultTable(): "
868
                             + partOfDoclist.size());
869
     long startGetReturnValueFromQueryresultable = System.currentTimeMillis();
870
     Hashtable queryresultDocList = docidsInQueryresultTable(returnfield_id,
871
                                                        partOfDoclist, dbconn);
872

    
873
     // remove the keys in queryresultDocList from partOfDoclist
874
     Enumeration _keys = queryresultDocList.keys();
875
     while (_keys.hasMoreElements()){
876
         partOfDoclist.remove((String)_keys.nextElement());
877
     }
878
     
879
     long queryResultReturnValuetime = System.currentTimeMillis() - startGetReturnValueFromQueryresultable;
880
     long queryResultWarnLimit = 
881
    	 Long.parseLong(PropertyService.getProperty("dbquery.findQueryResultsTimeWarnLimit"));
882
     
883
     if (queryResultReturnValuetime > queryResultWarnLimit) {
884
    	 logMetacat.warn("DBQuery.handleSubsetResult - Time to get return fields from xml_queryresult table is (Part1 in return fields) " +
885
    		 queryResultReturnValuetime);
886
     }
887
     MetacatUtil.writeDebugToFile("-----------------------------------------Get fields from xml_queryresult(Part1 in return fields) " +
888
    		 queryResultReturnValuetime);
889
     MetacatUtil.writeDebugToDelimiteredFile(" " + queryResultReturnValuetime,false);
890
     
891
     long startExtendedQuery = System.currentTimeMillis();
892
     // backup the keys-elements in partOfDoclist to check later
893
     // if the doc entry is indexed yet
894
     Hashtable partOfDoclistBackup = new Hashtable();
895
     Iterator itt = partOfDoclist.getDocids();
896
     while (itt.hasNext()){
897
       Object key = itt.next();
898
         partOfDoclistBackup.put(key, partOfDoclist.get(key));
899
     }
900

    
901
     logMetacat.info("DBQuery.handleSubsetResult - size of partOfDoclist after"
902
                             + " docidsInQueryresultTable(): "
903
                             + partOfDoclist.size());
904

    
905
     //add return fields for the documents in partOfDoclist
906
     partOfDoclist = addReturnfield(partOfDoclist, qspec, user, groups,
907
                                        dbconn, useXMLIndex, qformat);
908
     long extendedQueryRunTime = startExtendedQuery - System.currentTimeMillis();
909
     long extendedQueryWarnLimit = 
910
    	 Long.parseLong(PropertyService.getProperty("dbquery.extendedQueryRunTimeWarnLimit"));
911
  
912
     if (extendedQueryRunTime > extendedQueryWarnLimit) {
913
    	 logMetacat.warn("DBQuery.handleSubsetResult - Get fields from index and node table (Part2 in return fields) "
914
        		                                          + extendedQueryRunTime);
915
     }
916
     MetacatUtil.writeDebugToFile("-----------------------------------------Get fields from extened query(Part2 in return fields) "
917
             + extendedQueryRunTime);
918
     MetacatUtil.writeDebugToDelimiteredFile(" "
919
             + extendedQueryRunTime, false);
920
     //add relationship part part docid list for the documents in partOfDocList
921
     //partOfDoclist = addRelationship(partOfDoclist, qspec, dbconn, useXMLIndex);
922

    
923
     long startStoreReturnField = System.currentTimeMillis();
924
     Iterator keys = partOfDoclist.getDocids();
925
     String key = null;
926
     String element = null;
927
     String query = null;
928
     int offset = (new Integer(PropertyService
929
                               .getProperty("database.queryresultStringLength")))
930
                               .intValue();
931
     while (keys.hasNext())
932
     {
933
         key = (String) keys.next();
934
         element = (String)partOfDoclist.get(key);
935
         
936
	 // check if the enterRecords is true, elements is not null, element's
937
         // length is less than the limit of table column and if the document
938
         // has been indexed already
939
         if(enterRecords && element != null
940
		&& element.length() < offset
941
		&& element.compareTo((String) partOfDoclistBackup.get(key)) != 0){
942
             query = "INSERT INTO xml_queryresult (returnfield_id, docid, "
943
                 + "queryresult_string) VALUES (?, ?, ?)";
944

    
945
             PreparedStatement pstmt = null;
946
             pstmt = dbconn.prepareStatement(query);
947
             pstmt.setInt(1, returnfield_id);
948
             pstmt.setString(2, key);
949
             pstmt.setString(3, element);
950
            
951
             dbconn.increaseUsageCount(1);
952
             try
953
             {
954
            	 pstmt.execute();
955
             }
956
             catch(Exception e)
957
             {
958
            	 logMetacat.warn("DBQuery.handleSubsetResult - couldn't insert the element to xml_queryresult table "+e.getLocalizedMessage());
959
             }
960
             finally
961
             {
962
                pstmt.close();
963
             }
964
         }
965
        
966
         // A string with element
967
         String xmlElement = "  <document>" + element + "</document>";
968

    
969
         //send single element to output
970
         if (out != null)
971
         {
972
             out.write(xmlElement);
973
         }
974
         resultset.append(xmlElement);
975
     }//while
976
     
977
     double storeReturnFieldTime = System.currentTimeMillis() - startStoreReturnField;
978
     long storeReturnFieldWarnLimit = 
979
    	 Long.parseLong(PropertyService.getProperty("dbquery.storeReturnFieldTimeWarnLimit"));
980

    
981
     if (storeReturnFieldTime > storeReturnFieldWarnLimit) {
982
    	 logMetacat.warn("DBQuery.handleSubsetResult - Time to store new return fields into xml_queryresult table (Part4 in return fields) "
983
                   + storeReturnFieldTime);
984
     }
985
     MetacatUtil.writeDebugToFile("-----------------------------------------Insert new record to xml_queryresult(Part4 in return fields) "
986
             + storeReturnFieldTime);
987
     MetacatUtil.writeDebugToDelimiteredFile(" " + storeReturnFieldTime, false);
988
     
989
     Enumeration keysE = queryresultDocList.keys();
990
     while (keysE.hasMoreElements())
991
     {
992
         key = (String) keysE.nextElement();
993
         element = (String)queryresultDocList.get(key);
994
         // A string with element
995
         String xmlElement = "  <document>" + element + "</document>";
996
         //send single element to output
997
         if (out != null)
998
         {
999
             out.write(xmlElement);
1000
         }
1001
         resultset.append(xmlElement);
1002
     }//while
1003
     double returnFieldTime = System.currentTimeMillis() - startReturnFieldTime;
1004
     long totalReturnFieldWarnLimit = 
1005
    	 Long.parseLong(PropertyService.getProperty("dbquery.totalReturnFieldTimeWarnLimit"));
1006

    
1007
     if (returnFieldTime > totalReturnFieldWarnLimit) {
1008
    	 logMetacat.warn("DBQuery.handleSubsetResult - Total time to get return fields is: "
1009
                           + returnFieldTime);
1010
     }
1011
     MetacatUtil.writeDebugToFile("DBQuery.handleSubsetResult - ---------------------------------------------------------------------------------------------------------------"+
1012
    		 "Total to get return fields  " + returnFieldTime);
1013
     MetacatUtil.writeDebugToDelimiteredFile("DBQuery.handleSubsetResult - "+ returnFieldTime, false);
1014
     return resultset;
1015
 }
1016

    
1017
   /**
1018
    * Get the docids already in xml_queryresult table and corresponding
1019
    * queryresultstring as a hashtable
1020
    */
1021
   private Hashtable docidsInQueryresultTable(int returnfield_id,
1022
                                              ResultDocumentSet partOfDoclist,
1023
                                              DBConnection dbconn){
1024

    
1025
         Hashtable returnValue = new Hashtable();
1026
         PreparedStatement pstmt = null;
1027
         ResultSet rs = null;
1028

    
1029
         // get partOfDoclist as string for the query
1030
         Iterator keylist = partOfDoclist.getDocids();
1031
         StringBuffer doclist = new StringBuffer();
1032
         while (keylist.hasNext())
1033
         {
1034
             doclist.append("'");
1035
             doclist.append((String) keylist.next());
1036
             doclist.append("',");
1037
         }//while
1038

    
1039

    
1040
         if (doclist.length() > 0)
1041
         {
1042
             doclist.deleteCharAt(doclist.length() - 1); //remove the last comma
1043

    
1044
             // the query to find out docids from xml_queryresult
1045
             String query = "select docid, queryresult_string from "
1046
                          + "xml_queryresult where returnfield_id = " +
1047
                          returnfield_id +" and docid in ("+ doclist + ")";
1048
             logMetacat.info("DBQuery.docidsInQueryresultTable - Query to get docids from xml_queryresult:"
1049
                                      + query);
1050

    
1051
             try {
1052
                 // prepare and execute the query
1053
                 pstmt = dbconn.prepareStatement(query);
1054
                 dbconn.increaseUsageCount(1);
1055
                 pstmt.execute();
1056
                 rs = pstmt.getResultSet();
1057
                 boolean tableHasRows = rs.next();
1058
                 while (tableHasRows) {
1059
                     // store the returned results in the returnValue hashtable
1060
                     String key = rs.getString(1);
1061
                     String element = rs.getString(2);
1062

    
1063
                     if(element != null){
1064
                         returnValue.put(key, element);
1065
                     } else {
1066
                         logMetacat.info("DBQuery.docidsInQueryresultTable - Null elment found ("
1067
                         + "DBQuery.docidsInQueryresultTable)");
1068
                     }
1069
                     tableHasRows = rs.next();
1070
                 }
1071
                 rs.close();
1072
                 pstmt.close();
1073
             } catch (Exception e){
1074
                 logMetacat.error("DBQuery.docidsInQueryresultTable - Error getting docids from "
1075
                                          + "queryresult: " + e.getMessage());
1076
              }
1077
         }
1078
         return returnValue;
1079
     }
1080

    
1081

    
1082
   /**
1083
    * Method to get id from xml_returnfield table
1084
    * for a given query specification
1085
    */
1086
   private int returnfield_id;
1087
   private int getXmlReturnfieldsTableId(QuerySpecification qspec,
1088
                                           DBConnection dbconn){
1089
       int id = -1;
1090
       int count = 1;
1091
       PreparedStatement pstmt = null;
1092
       ResultSet rs = null;
1093
       String returnfield = qspec.getSortedReturnFieldString();
1094

    
1095
       // query for finding the id from xml_returnfield
1096
       String query = "SELECT returnfield_id, usage_count FROM xml_returnfield "
1097
            + "WHERE returnfield_string LIKE ?";
1098
       logMetacat.info("DBQuery.getXmlReturnfieldsTableId - ReturnField Query:" + query);
1099

    
1100
       try {
1101
           // prepare and run the query
1102
           pstmt = dbconn.prepareStatement(query);
1103
           pstmt.setString(1,returnfield);
1104
           dbconn.increaseUsageCount(1);
1105
           pstmt.execute();
1106
           rs = pstmt.getResultSet();
1107
           boolean tableHasRows = rs.next();
1108

    
1109
           // if record found then increase the usage count
1110
           // else insert a new record and get the id of the new record
1111
           if(tableHasRows){
1112
               // get the id
1113
               id = rs.getInt(1);
1114
               count = rs.getInt(2) + 1;
1115
               rs.close();
1116
               pstmt.close();
1117

    
1118
               // increase the usage count
1119
               query = "UPDATE xml_returnfield SET usage_count ='" + count
1120
                   + "' WHERE returnfield_id ='"+ id +"'";
1121
               logMetacat.info("DBQuery.getXmlReturnfieldsTableId - ReturnField Table Update:"+ query);
1122

    
1123
               pstmt = dbconn.prepareStatement(query);
1124
               dbconn.increaseUsageCount(1);
1125
               pstmt.execute();
1126
               pstmt.close();
1127

    
1128
           } else {
1129
               rs.close();
1130
               pstmt.close();
1131

    
1132
               // insert a new record
1133
               query = "INSERT INTO xml_returnfield (returnfield_string, usage_count)"
1134
                   + "VALUES (?, '1')";
1135
               logMetacat.info("DBQuery.getXmlReturnfieldsTableId - ReturnField Table Insert:"+ query);
1136
               pstmt = dbconn.prepareStatement(query);
1137
               pstmt.setString(1, returnfield);
1138
               dbconn.increaseUsageCount(1);
1139
               pstmt.execute();
1140
               pstmt.close();
1141

    
1142
               // get the id of the new record
1143
               query = "SELECT returnfield_id FROM xml_returnfield "
1144
                   + "WHERE returnfield_string LIKE ?";
1145
               logMetacat.info("DBQuery.getXmlReturnfieldsTableId - ReturnField query after Insert:" + query);
1146
               pstmt = dbconn.prepareStatement(query);
1147
               pstmt.setString(1, returnfield);
1148

    
1149
               dbconn.increaseUsageCount(1);
1150
               pstmt.execute();
1151
               rs = pstmt.getResultSet();
1152
               if(rs.next()){
1153
                   id = rs.getInt(1);
1154
               } else {
1155
                   id = -1;
1156
               }
1157
               rs.close();
1158
               pstmt.close();
1159
           }
1160

    
1161
       } catch (Exception e){
1162
           logMetacat.error("DBQuery.getXmlReturnfieldsTableId - Error getting id from xml_returnfield in "
1163
                                     + "DBQuery.getXmlReturnfieldsTableId: "
1164
                                     + e.getMessage());
1165
           id = -1;
1166
       }
1167

    
1168
       returnfield_id = id;
1169
       return count;
1170
   }
1171

    
1172

    
1173
    /*
1174
     * A method to add return field to return doclist hash table
1175
     */
1176
    private ResultDocumentSet addReturnfield(ResultDocumentSet docListResult,
1177
                                      QuerySpecification qspec,
1178
                                      String user, String[]groups,
1179
                                      DBConnection dbconn, boolean useXMLIndex,
1180
                                      String qformat)
1181
                                      throws Exception
1182
    {
1183
      PreparedStatement pstmt = null;
1184
      ResultSet rs = null;
1185
      String docid = null;
1186
      String fieldname = null;
1187
      String fieldtype = null;
1188
      String fielddata = null;
1189
      String relation = null;
1190

    
1191
      if (qspec.containsExtendedSQL())
1192
      {
1193
        qspec.setUserName(user);
1194
        qspec.setGroup(groups);
1195
        Vector extendedFields = new Vector(qspec.getReturnFieldList());
1196
        Vector results = new Vector();
1197
        Iterator keylist = docListResult.getDocids();
1198
        StringBuffer doclist = new StringBuffer();
1199
        Vector parentidList = new Vector();
1200
        Hashtable returnFieldValue = new Hashtable();
1201
        while (keylist.hasNext())
1202
        {
1203
          String key = (String)keylist.next();
1204
          doclist.append("'");
1205
          doclist.append(key);
1206
          doclist.append("',");
1207
        }
1208
        if (doclist.length() > 0)
1209
        {
1210
          Hashtable controlPairs = new Hashtable();
1211
          doclist.deleteCharAt(doclist.length() - 1); //remove the last comma
1212
          boolean tableHasRows = false;
1213
        
1214

    
1215
           String extendedQuery =
1216
               qspec.printExtendedSQL(doclist.toString(), useXMLIndex);
1217
           logMetacat.info("DBQuery.addReturnfield - Extended query: " + extendedQuery);
1218

    
1219
           if(extendedQuery != null){
1220
//        	   long extendedQueryStart = System.currentTimeMillis();
1221
               pstmt = dbconn.prepareStatement(extendedQuery);
1222
               //increase dbconnection usage count
1223
               dbconn.increaseUsageCount(1);
1224
               pstmt.execute();
1225
               rs = pstmt.getResultSet();
1226
               tableHasRows = rs.next();
1227
               while (tableHasRows) {
1228
                   ReturnFieldValue returnValue = new ReturnFieldValue();
1229
                   docid = rs.getString(1).trim();
1230
                   fieldname = rs.getString(2);
1231
                   
1232
                   if(qformat.toLowerCase().trim().equals("xml"))
1233
                   {
1234
                       byte[] b = rs.getBytes(3);
1235
                       fielddata = new String(b, 0, b.length, MetaCatServlet.DEFAULT_ENCODING);
1236
                   }
1237
                   else
1238
                   {
1239
                       fielddata = rs.getString(3);
1240
                   }
1241
                   
1242
                   //System.out.println("raw fielddata: " + fielddata);
1243
                   fielddata = MetacatUtil.normalize(fielddata);
1244
                   //System.out.println("normalized fielddata: " + fielddata);
1245
                   String parentId = rs.getString(4);
1246
                   fieldtype = rs.getString(5);
1247
                   StringBuffer value = new StringBuffer();
1248

    
1249
                   //handle case when usexmlindex is true differently
1250
                   //at one point merging the nodedata (for large text elements) was 
1251
                   //deemed unnecessary - but now it is needed.  but not for attribute nodes
1252
                   if (useXMLIndex || !containsKey(parentidList, parentId)) {
1253
                	   //merge node data only for non-ATTRIBUTEs
1254
                	   if (fieldtype != null && !fieldtype.equals("ATTRIBUTE")) {
1255
	                	   //try merging the data
1256
	                	   ReturnFieldValue existingRFV =
1257
	                		   getArrayValue(parentidList, parentId);
1258
	                	   if (existingRFV != null && !existingRFV.getFieldType().equals("ATTRIBUTE")) {
1259
	                		   fielddata = existingRFV.getFieldValue() + fielddata;
1260
	                	   }
1261
                	   }
1262
                	   //System.out.println("fieldname: " + fieldname + " fielddata: " + fielddata);
1263

    
1264
                       value.append("<param name=\"");
1265
                       value.append(fieldname);
1266
                       value.append("\">");
1267
                       value.append(fielddata);
1268
                       value.append("</param>");
1269
                       //set returnvalue
1270
                       returnValue.setDocid(docid);
1271
                       returnValue.setFieldValue(fielddata);
1272
                       returnValue.setFieldType(fieldtype);
1273
                       returnValue.setXMLFieldValue(value.toString());
1274
                       // Store it in hastable
1275
                       putInArray(parentidList, parentId, returnValue);
1276
                   }
1277
                   else {
1278
                       
1279
                       // need to merge nodedata if they have same parent id and
1280
                       // node type is text
1281
                       fielddata = (String) ( (ReturnFieldValue)
1282
                                             getArrayValue(
1283
                           parentidList, parentId)).getFieldValue()
1284
                           + fielddata;
1285
                       //System.out.println("fieldname: " + fieldname + " fielddata: " + fielddata);
1286
                       value.append("<param name=\"");
1287
                       value.append(fieldname);
1288
                       value.append("\">");
1289
                       value.append(fielddata);
1290
                       value.append("</param>");
1291
                       returnValue.setDocid(docid);
1292
                       returnValue.setFieldValue(fielddata);
1293
                       returnValue.setFieldType(fieldtype);
1294
                       returnValue.setXMLFieldValue(value.toString());
1295
                       // remove the old return value from paretnidList
1296
                       parentidList.remove(parentId);
1297
                       // store the new return value in parentidlit
1298
                       putInArray(parentidList, parentId, returnValue);
1299
                   }
1300
                   tableHasRows = rs.next();
1301
               } //while
1302
               rs.close();
1303
               pstmt.close();
1304

    
1305
               // put the merger node data info into doclistReult
1306
               Enumeration xmlFieldValue = (getElements(parentidList)).
1307
                   elements();
1308
               while (xmlFieldValue.hasMoreElements()) {
1309
                   ReturnFieldValue object =
1310
                       (ReturnFieldValue) xmlFieldValue.nextElement();
1311
                   docid = object.getDocid();
1312
                   if (docListResult.containsDocid(docid)) {
1313
                       String removedelement = (String) docListResult.
1314
                           remove(docid);
1315
                       docListResult.
1316
                           addResultDocument(new ResultDocument(docid,
1317
                               removedelement + object.getXMLFieldValue()));
1318
                   }
1319
                   else {
1320
                       docListResult.addResultDocument(
1321
                         new ResultDocument(docid, object.getXMLFieldValue()));
1322
                   }
1323
               } //while
1324
//               double docListResultEnd = System.currentTimeMillis() / 1000;
1325
//               logMetacat.warn(
1326
//                   "Time to prepare ResultDocumentSet after"
1327
//                   + " execute extended query: "
1328
//                   + (docListResultEnd - extendedQueryEnd));
1329
           }
1330
       }//if doclist lenght is great than zero
1331
     }//if has extended query
1332

    
1333
      return docListResult;
1334
    }//addReturnfield
1335

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

    
1410

    
1411
    /*
1412
     * A method to search if Vector contains a particular key string
1413
     */
1414
    private boolean containsKey(Vector parentidList, String parentId)
1415
    {
1416

    
1417
        Vector tempVector = null;
1418

    
1419
        for (int count = 0; count < parentidList.size(); count++) {
1420
            tempVector = (Vector) parentidList.get(count);
1421
            if (parentId.compareTo((String) tempVector.get(0)) == 0) { return true; }
1422
        }
1423
        return false;
1424
    }
1425
    
1426
    /*
1427
     * A method to put key and value in Vector
1428
     */
1429
    private void putInArray(Vector parentidList, String key,
1430
            ReturnFieldValue value)
1431
    {
1432

    
1433
        Vector tempVector = null;
1434
        //only filter if the field type is NOT an attribute (say, for text)
1435
        String fieldType = value.getFieldType();
1436
        if (fieldType != null && !fieldType.equals("ATTRIBUTE")) {
1437
        
1438
	        for (int count = 0; count < parentidList.size(); count++) {
1439
	            tempVector = (Vector) parentidList.get(count);
1440
	
1441
	            if (key.compareTo((String) tempVector.get(0)) == 0) {
1442
	                tempVector.remove(1);
1443
	                tempVector.add(1, value);
1444
	                return;
1445
	            }
1446
	        }
1447
        }
1448

    
1449
        tempVector = new Vector();
1450
        tempVector.add(0, key);
1451
        tempVector.add(1, value);
1452
        parentidList.add(tempVector);
1453
        return;
1454
    }
1455

    
1456
    /*
1457
     * A method to get value in Vector given a key
1458
     */
1459
    private ReturnFieldValue getArrayValue(Vector parentidList, String key)
1460
    {
1461

    
1462
        Vector tempVector = null;
1463

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

    
1467
            if (key.compareTo((String) tempVector.get(0)) == 0) { return (ReturnFieldValue) tempVector
1468
                    .get(1); }
1469
        }
1470
        return null;
1471
    }
1472

    
1473
    /*
1474
     * A method to get enumeration of all values in Vector
1475
     */
1476
    private Vector getElements(Vector parentidList)
1477
    {
1478
        Vector enumVector = new Vector();
1479
        Vector tempVector = null;
1480

    
1481
        for (int count = 0; count < parentidList.size(); count++) {
1482
            tempVector = (Vector) parentidList.get(count);
1483

    
1484
            enumVector.add(tempVector.get(1));
1485
        }
1486
        return enumVector;
1487
    }
1488

    
1489
  
1490

    
1491
    /*
1492
     * A method to create a query to get owner's docid list
1493
     */
1494
    private String getOwnerQuery(String owner)
1495
    {
1496
        if (owner != null) {
1497
            owner = owner.toLowerCase();
1498
        }
1499
        StringBuffer self = new StringBuffer();
1500

    
1501
        self.append("SELECT docid,docname,doctype,");
1502
        self.append("date_created, date_updated, rev ");
1503
        self.append("FROM xml_documents WHERE docid IN (");
1504
        self.append("(");
1505
        self.append("SELECT DISTINCT docid FROM xml_nodes WHERE \n");
1506
        self.append("nodedata LIKE '%%%' ");
1507
        self.append(") \n");
1508
        self.append(") ");
1509
        self.append(" AND (");
1510
        self.append(" lower(user_owner) = '" + owner + "'");
1511
        self.append(") ");
1512
        return self.toString();
1513
    }
1514

    
1515
    /**
1516
     * format a structured query as an XML document that conforms to the
1517
     * pathquery.dtd and is appropriate for submission to the DBQuery
1518
     * structured query engine
1519
     *
1520
     * @param params The list of parameters that should be included in the
1521
     *            query
1522
     */
1523
    public static String createSQuery(Hashtable params) throws PropertyNotFoundException
1524
    {
1525
        StringBuffer query = new StringBuffer();
1526
        Enumeration elements;
1527
        Enumeration keys;
1528
        String filterDoctype = null;
1529
        String casesensitive = null;
1530
        String searchmode = null;
1531
        Object nextkey;
1532
        Object nextelement;
1533
        //add the xml headers
1534
        query.append("<?xml version=\"1.0\"?>\n");
1535
        query.append("<pathquery version=\"1.2\">\n");
1536

    
1537

    
1538

    
1539
        if (params.containsKey("meta_file_id")) {
1540
            query.append("<meta_file_id>");
1541
            query.append(((String[]) params.get("meta_file_id"))[0]);
1542
            query.append("</meta_file_id>");
1543
        }
1544

    
1545
        if (params.containsKey("returndoctype")) {
1546
            String[] returnDoctypes = ((String[]) params.get("returndoctype"));
1547
            for (int i = 0; i < returnDoctypes.length; i++) {
1548
                String doctype = (String) returnDoctypes[i];
1549

    
1550
                if (!doctype.equals("any") && !doctype.equals("ANY")
1551
                        && !doctype.equals("")) {
1552
                    query.append("<returndoctype>").append(doctype);
1553
                    query.append("</returndoctype>");
1554
                }
1555
            }
1556
        }
1557

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

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

    
1574
        if (params.containsKey("owner")) {
1575
            String[] owner = ((String[]) params.get("owner"));
1576
            for (int i = 0; i < owner.length; i++) {
1577
                query.append("<owner>").append(owner[i]);
1578
                query.append("</owner>");
1579
            }
1580
        }
1581

    
1582
        if (params.containsKey("site")) {
1583
            String[] site = ((String[]) params.get("site"));
1584
            for (int i = 0; i < site.length; i++) {
1585
                query.append("<site>").append(site[i]);
1586
                query.append("</site>");
1587
            }
1588
        }
1589

    
1590
        //allows the dynamic switching of boolean operators
1591
        if (params.containsKey("operator")) {
1592
            query.append("<querygroup operator=\""
1593
                    + ((String[]) params.get("operator"))[0] + "\">");
1594
        } else { //the default operator is UNION
1595
            query.append("<querygroup operator=\"UNION\">");
1596
        }
1597

    
1598
        if (params.containsKey("casesensitive")) {
1599
            casesensitive = ((String[]) params.get("casesensitive"))[0];
1600
        } else {
1601
            casesensitive = "false";
1602
        }
1603

    
1604
        if (params.containsKey("searchmode")) {
1605
            searchmode = ((String[]) params.get("searchmode"))[0];
1606
        } else {
1607
            searchmode = "contains";
1608
        }
1609

    
1610
        //anyfield is a special case because it does a
1611
        //free text search. It does not have a <pathexpr>
1612
        //tag. This allows for a free text search within the structured
1613
        //query. This is useful if the INTERSECT operator is used.
1614
        if (params.containsKey("anyfield")) {
1615
            String[] anyfield = ((String[]) params.get("anyfield"));
1616
            //allow for more than one value for anyfield
1617
            for (int i = 0; i < anyfield.length; i++) {
1618
                if (anyfield[i] != null && !anyfield[i].equals("")) {
1619
                    query.append("<queryterm casesensitive=\"" + casesensitive
1620
                            + "\" " + "searchmode=\"" + searchmode
1621
                            + "\"><value>" + anyfield[i]
1622
                            + "</value></queryterm>");
1623
                }
1624
            }
1625
        }
1626

    
1627
        //this while loop finds the rest of the parameters
1628
        //and attempts to query for the field specified
1629
        //by the parameter.
1630
        elements = params.elements();
1631
        keys = params.keys();
1632
        while (keys.hasMoreElements() && elements.hasMoreElements()) {
1633
            nextkey = keys.nextElement();
1634
            nextelement = elements.nextElement();
1635

    
1636
            //make sure we aren't querying for any of these
1637
            //parameters since the are already in the query
1638
            //in one form or another.
1639
            Vector ignoredParams = new Vector();
1640
            ignoredParams.add("returndoctype");
1641
            ignoredParams.add("filterdoctype");
1642
            ignoredParams.add("action");
1643
            ignoredParams.add("qformat");
1644
            ignoredParams.add("anyfield");
1645
            ignoredParams.add("returnfield");
1646
            ignoredParams.add("owner");
1647
            ignoredParams.add("site");
1648
            ignoredParams.add("operator");
1649
            ignoredParams.add("sessionid");
1650
            ignoredParams.add("pagesize");
1651
            ignoredParams.add("pagestart");
1652
            ignoredParams.add("searchmode");
1653

    
1654
            // Also ignore parameters listed in the properties file
1655
            // so that they can be passed through to stylesheets
1656
            String paramsToIgnore = PropertyService
1657
                    .getProperty("database.queryignoredparams");
1658
            StringTokenizer st = new StringTokenizer(paramsToIgnore, ",");
1659
            while (st.hasMoreTokens()) {
1660
                ignoredParams.add(st.nextToken());
1661
            }
1662
            if (!ignoredParams.contains(nextkey.toString())) {
1663
                //allow for more than value per field name
1664
                for (int i = 0; i < ((String[]) nextelement).length; i++) {
1665
                    if (!((String[]) nextelement)[i].equals("")) {
1666
                        query.append("<queryterm casesensitive=\""
1667
                                + casesensitive + "\" " + "searchmode=\""
1668
                                + searchmode + "\">" + "<value>" +
1669
                                //add the query value
1670
                                ((String[]) nextelement)[i]
1671
                                + "</value><pathexpr>" +
1672
                                //add the path to query by
1673
                                nextkey.toString() + "</pathexpr></queryterm>");
1674
                    }
1675
                }
1676
            }
1677
        }
1678
        query.append("</querygroup></pathquery>");
1679
        //append on the end of the xml and return the result as a string
1680
        return query.toString();
1681
    }
1682

    
1683
    /**
1684
     * format a simple free-text value query as an XML document that conforms
1685
     * to the pathquery.dtd and is appropriate for submission to the DBQuery
1686
     * structured query engine
1687
     *
1688
     * @param value the text string to search for in the xml catalog
1689
     * @param doctype the type of documents to include in the result set -- use
1690
     *            "any" or "ANY" for unfiltered result sets
1691
     */
1692
    public static String createQuery(String value, String doctype)
1693
    {
1694
        StringBuffer xmlquery = new StringBuffer();
1695
        xmlquery.append("<?xml version=\"1.0\"?>\n");
1696
        xmlquery.append("<pathquery version=\"1.0\">");
1697

    
1698
        if (!doctype.equals("any") && !doctype.equals("ANY")) {
1699
            xmlquery.append("<returndoctype>");
1700
            xmlquery.append(doctype).append("</returndoctype>");
1701
        }
1702

    
1703
        xmlquery.append("<querygroup operator=\"UNION\">");
1704
        //chad added - 8/14
1705
        //the if statement allows a query to gracefully handle a null
1706
        //query. Without this if a nullpointerException is thrown.
1707
        if (!value.equals("")) {
1708
            xmlquery.append("<queryterm casesensitive=\"false\" ");
1709
            xmlquery.append("searchmode=\"contains\">");
1710
            xmlquery.append("<value>").append(value).append("</value>");
1711
            xmlquery.append("</queryterm>");
1712
        }
1713
        xmlquery.append("</querygroup>");
1714
        xmlquery.append("</pathquery>");
1715

    
1716
        return (xmlquery.toString());
1717
    }
1718

    
1719
    /**
1720
     * format a simple free-text value query as an XML document that conforms
1721
     * to the pathquery.dtd and is appropriate for submission to the DBQuery
1722
     * structured query engine
1723
     *
1724
     * @param value the text string to search for in the xml catalog
1725
     */
1726
    public static String createQuery(String value)
1727
    {
1728
        return createQuery(value, "any");
1729
    }
1730

    
1731
    /**
1732
     * Check for "READ" permission on @docid for @user and/or @group from DB
1733
     * connection
1734
     */
1735
    private boolean hasPermission(String user, String[] groups, String docid)
1736
            throws SQLException, Exception
1737
    {
1738
        // Check for READ permission on @docid for @user and/or @groups
1739
        PermissionController controller = new PermissionController(docid);
1740
        return controller.hasPermission(user, groups,
1741
                AccessControlInterface.READSTRING);
1742
    }
1743

    
1744
    /**
1745
     * Get all docIds list for a data packadge
1746
     *
1747
     * @param dataPackageDocid, the string in docId field of xml_relation table
1748
     */
1749
    private Vector getCurrentDocidListForDataPackage(String dataPackageDocid)
1750
    {
1751
        DBConnection dbConn = null;
1752
        int serialNumber = -1;
1753
        Vector docIdList = new Vector();//return value
1754
        PreparedStatement pStmt = null;
1755
        ResultSet rs = null;
1756
        String docIdInSubjectField = null;
1757
        String docIdInObjectField = null;
1758

    
1759
        // Check the parameter
1760
        if (dataPackageDocid == null || dataPackageDocid.equals("")) { return docIdList; }//if
1761

    
1762
        //the query stirng
1763
        String query = "SELECT subject, object from xml_relation where docId = ?";
1764
        try {
1765
            dbConn = DBConnectionPool
1766
                    .getDBConnection("DBQuery.getCurrentDocidListForDataPackage");
1767
            serialNumber = dbConn.getCheckOutSerialNumber();
1768
            pStmt = dbConn.prepareStatement(query);
1769
            //bind the value to query
1770
            pStmt.setString(1, dataPackageDocid);
1771

    
1772
            //excute the query
1773
            pStmt.execute();
1774
            //get the result set
1775
            rs = pStmt.getResultSet();
1776
            //process the result
1777
            while (rs.next()) {
1778
                //In order to get the whole docIds in a data packadge,
1779
                //we need to put the docIds of subject and object field in
1780
                // xml_relation
1781
                //into the return vector
1782
                docIdInSubjectField = rs.getString(1);//the result docId in
1783
                                                      // subject field
1784
                docIdInObjectField = rs.getString(2);//the result docId in
1785
                                                     // object field
1786

    
1787
                //don't put the duplicate docId into the vector
1788
                if (!docIdList.contains(docIdInSubjectField)) {
1789
                    docIdList.add(docIdInSubjectField);
1790
                }
1791

    
1792
                //don't put the duplicate docId into the vector
1793
                if (!docIdList.contains(docIdInObjectField)) {
1794
                    docIdList.add(docIdInObjectField);
1795
                }
1796
            }//while
1797
            //close the pStmt
1798
            pStmt.close();
1799
        }//try
1800
        catch (SQLException e) {
1801
            logMetacat.error("DBQuery.getCurrentDocidListForDataPackage - Error in getDocidListForDataPackage: "
1802
                    + e.getMessage());
1803
        }//catch
1804
        finally {
1805
            try {
1806
                pStmt.close();
1807
            }//try
1808
            catch (SQLException ee) {
1809
                logMetacat.error("DBQuery.getCurrentDocidListForDataPackage - SQL Error: "
1810
                                + ee.getMessage());
1811
            }//catch
1812
            finally {
1813
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1814
            }//fianlly
1815
        }//finally
1816
        return docIdList;
1817
    }//getCurrentDocidListForDataPackadge()
1818

    
1819
    /**
1820
     * Get all docIds list for a data packadge
1821
     *
1822
     * @param dataPackageDocid, the string in docId field of xml_relation table
1823
     */
1824
    private Vector getOldVersionDocidListForDataPackage(String dataPackageDocidWithRev)
1825
    {
1826

    
1827
        Vector docIdList = new Vector();//return value
1828
        Vector tripleList = null;
1829
        String xml = null;
1830

    
1831
        // Check the parameter
1832
        if (dataPackageDocidWithRev == null || dataPackageDocidWithRev.equals("")) { return docIdList; }//if
1833

    
1834
        try {
1835
            //initial a documentImpl object
1836
            DocumentImpl packageDocument = new DocumentImpl(dataPackageDocidWithRev);
1837
            //transfer to documentImpl object to string
1838
            xml = packageDocument.toString();
1839

    
1840
            //create a tripcollection object
1841
            TripleCollection tripleForPackage = new TripleCollection(
1842
                    new StringReader(xml));
1843
            //get the vetor of triples
1844
            tripleList = tripleForPackage.getCollection();
1845

    
1846
            for (int i = 0; i < tripleList.size(); i++) {
1847
                //put subject docid into docIdlist without duplicate
1848
                if (!docIdList.contains(((Triple) tripleList.elementAt(i))
1849
                        .getSubject())) {
1850
                    //put subject docid into docIdlist
1851
                    docIdList.add(((Triple) tripleList.get(i)).getSubject());
1852
                }
1853
                //put object docid into docIdlist without duplicate
1854
                if (!docIdList.contains(((Triple) tripleList.elementAt(i))
1855
                        .getObject())) {
1856
                    docIdList.add(((Triple) (tripleList.get(i))).getObject());
1857
                }
1858
            }//for
1859
        }//try
1860
        catch (Exception e) {
1861
            logMetacat.error("DBQuery.getCurrentDocidListForDataPackage - General error: "
1862
                    + e.getMessage());
1863
        }//catch
1864

    
1865
        // return result
1866
        return docIdList;
1867
    }//getDocidListForPackageInXMLRevisions()
1868

    
1869
    /**
1870
     * Check if the docId is a data packadge id. If the id is a data packadage
1871
     * id, it should be store in the docId fields in xml_relation table. So we
1872
     * can use a query to get the entries which the docId equals the given
1873
     * value. If the result is null. The docId is not a packadge id. Otherwise,
1874
     * it is.
1875
     *
1876
     * @param docId, the id need to be checked
1877
     */
1878
    private boolean isDataPackageId(String docId)
1879
    {
1880
        boolean result = false;
1881
        PreparedStatement pStmt = null;
1882
        ResultSet rs = null;
1883
        String query = "SELECT docId from xml_relation where docId = ?";
1884
        DBConnection dbConn = null;
1885
        int serialNumber = -1;
1886
        try {
1887
            dbConn = DBConnectionPool
1888
                    .getDBConnection("DBQuery.isDataPackageId");
1889
            serialNumber = dbConn.getCheckOutSerialNumber();
1890
            pStmt = dbConn.prepareStatement(query);
1891
            //bind the value to query
1892
            pStmt.setString(1, docId);
1893
            //execute the query
1894
            pStmt.execute();
1895
            rs = pStmt.getResultSet();
1896
            //process the result
1897
            if (rs.next()) //There are some records for the id in docId fields
1898
            {
1899
                result = true;//It is a data packadge id
1900
            }
1901
            pStmt.close();
1902
        }//try
1903
        catch (SQLException e) {
1904
            logMetacat.error("DBQuery.isDataPackageId - SQL Error: "
1905
                    + e.getMessage());
1906
        } finally {
1907
            try {
1908
                pStmt.close();
1909
            }//try
1910
            catch (SQLException ee) {
1911
                logMetacat.error("DBQuery.isDataPackageId - SQL Error in isDataPackageId: "
1912
                        + ee.getMessage());
1913
            }//catch
1914
            finally {
1915
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1916
            }//finally
1917
        }//finally
1918
        return result;
1919
    }//isDataPackageId()
1920

    
1921
    /**
1922
     * Check if the user has the permission to export data package
1923
     *
1924
     * @param conn, the connection
1925
     * @param docId, the id need to be checked
1926
     * @param user, the name of user
1927
     * @param groups, the user's group
1928
     */
1929
    private boolean hasPermissionToExportPackage(String docId, String user,
1930
            String[] groups) throws Exception
1931
    {
1932
        //DocumentImpl doc=new DocumentImpl(conn,docId);
1933
        return DocumentImpl.hasReadPermission(user, groups, docId);
1934
    }
1935

    
1936
    /**
1937
     * Get the current Rev for a docid in xml_documents table
1938
     *
1939
     * @param docId, the id need to get version numb If the return value is -5,
1940
     *            means no value in rev field for this docid
1941
     */
1942
    private int getCurrentRevFromXMLDoumentsTable(String docId)
1943
            throws SQLException
1944
    {
1945
        int rev = -5;
1946
        PreparedStatement pStmt = null;
1947
        ResultSet rs = null;
1948
        String query = "SELECT rev from xml_documents where docId = ?";
1949
        DBConnection dbConn = null;
1950
        int serialNumber = -1;
1951
        try {
1952
            dbConn = DBConnectionPool
1953
                    .getDBConnection("DBQuery.getCurrentRevFromXMLDocumentsTable");
1954
            serialNumber = dbConn.getCheckOutSerialNumber();
1955
            pStmt = dbConn.prepareStatement(query);
1956
            //bind the value to query
1957
            pStmt.setString(1, docId);
1958
            //execute the query
1959
            pStmt.execute();
1960
            rs = pStmt.getResultSet();
1961
            //process the result
1962
            if (rs.next()) //There are some records for rev
1963
            {
1964
                rev = rs.getInt(1);
1965
                ;//It is the version for given docid
1966
            } else {
1967
                rev = -5;
1968
            }
1969

    
1970
        }//try
1971
        catch (SQLException e) {
1972
            logMetacat.error("DBQuery.getCurrentRevFromXMLDoumentsTable - SQL Error: "
1973
                            + e.getMessage());
1974
            throw e;
1975
        }//catch
1976
        finally {
1977
            try {
1978
                pStmt.close();
1979
            }//try
1980
            catch (SQLException ee) {
1981
                logMetacat.error(
1982
                        "DBQuery.getCurrentRevFromXMLDoumentsTable - SQL Error: "
1983
                                + ee.getMessage());
1984
            }//catch
1985
            finally {
1986
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1987
            }//finally
1988
        }//finally
1989
        return rev;
1990
    }//getCurrentRevFromXMLDoumentsTable
1991

    
1992
    /**
1993
     * put a doc into a zip output stream
1994
     *
1995
     * @param docImpl, docmentImpl object which will be sent to zip output
1996
     *            stream
1997
     * @param zipOut, zip output stream which the docImpl will be put
1998
     * @param packageZipEntry, the zip entry name for whole package
1999
     */
2000
    private void addDocToZipOutputStream(DocumentImpl docImpl,
2001
            ZipOutputStream zipOut, String packageZipEntry)
2002
            throws ClassNotFoundException, IOException, SQLException,
2003
            McdbException, Exception
2004
    {
2005
        byte[] byteString = null;
2006
        ZipEntry zEntry = null;
2007

    
2008
        byteString = docImpl.getBytes();
2009
        //use docId as the zip entry's name
2010
        zEntry = new ZipEntry(packageZipEntry + "/metadata/"
2011
                + docImpl.getDocID());
2012
        zEntry.setSize(byteString.length);
2013
        zipOut.putNextEntry(zEntry);
2014
        zipOut.write(byteString, 0, byteString.length);
2015
        zipOut.closeEntry();
2016

    
2017
    }//addDocToZipOutputStream()
2018

    
2019
    /**
2020
     * Transfer a docid vetor to a documentImpl vector. The documentImpl vetor
2021
     * only inlcudes current version. If a DocumentImple object couldn't find
2022
     * for a docid, then the String of this docid was added to vetor rather
2023
     * than DocumentImple object.
2024
     *
2025
     * @param docIdList, a vetor hold a docid list for a data package. In
2026
     *            docid, there is not version number in it.
2027
     */
2028

    
2029
    private Vector getCurrentAllDocumentImpl(Vector docIdList)
2030
            throws McdbException, Exception
2031
    {
2032
        //Connection dbConn=null;
2033
        Vector documentImplList = new Vector();
2034
        int rev = 0;
2035

    
2036
        // Check the parameter
2037
        if (docIdList.isEmpty()) { return documentImplList; }//if
2038

    
2039
        //for every docid in vector
2040
        for (int i = 0; i < docIdList.size(); i++) {
2041
            try {
2042
                //get newest version for this docId
2043
                rev = getCurrentRevFromXMLDoumentsTable((String) docIdList
2044
                        .elementAt(i));
2045

    
2046
                // There is no record for this docId in xml_documents table
2047
                if (rev == -5) {
2048
                    // Rather than put DocumentImple object, put a String
2049
                    // Object(docid)
2050
                    // into the documentImplList
2051
                    documentImplList.add((String) docIdList.elementAt(i));
2052
                    // Skip other code
2053
                    continue;
2054
                }
2055

    
2056
                String docidPlusVersion = ((String) docIdList.elementAt(i))
2057
                        + PropertyService.getProperty("document.accNumSeparator") + rev;
2058

    
2059
                //create new documentImpl object
2060
                DocumentImpl documentImplObject = new DocumentImpl(
2061
                        docidPlusVersion);
2062
                //add them to vector
2063
                documentImplList.add(documentImplObject);
2064
            }//try
2065
            catch (Exception e) {
2066
                logMetacat.error("DBQuery.getCurrentAllDocumentImpl - General error: "
2067
                        + e.getMessage());
2068
                // continue the for loop
2069
                continue;
2070
            }
2071
        }//for
2072
        return documentImplList;
2073
    }
2074

    
2075
    /**
2076
     * Transfer a docid vetor to a documentImpl vector. If a DocumentImple
2077
     * object couldn't find for a docid, then the String of this docid was
2078
     * added to vetor rather than DocumentImple object.
2079
     *
2080
     * @param docIdList, a vetor hold a docid list for a data package. In
2081
     *            docid, t here is version number in it.
2082
     */
2083
    private Vector getOldVersionAllDocumentImpl(Vector docIdList)
2084
    {
2085
        //Connection dbConn=null;
2086
        Vector documentImplList = new Vector();
2087
        String siteCode = null;
2088
        String uniqueId = null;
2089
        int rev = 0;
2090

    
2091
        // Check the parameter
2092
        if (docIdList.isEmpty()) { return documentImplList; }//if
2093

    
2094
        //for every docid in vector
2095
        for (int i = 0; i < docIdList.size(); i++) {
2096

    
2097
            String docidPlusVersion = (String) (docIdList.elementAt(i));
2098

    
2099
            try {
2100
                //create new documentImpl object
2101
                DocumentImpl documentImplObject = new DocumentImpl(
2102
                        docidPlusVersion);
2103
                //add them to vector
2104
                documentImplList.add(documentImplObject);
2105
            }//try
2106
            catch (McdbDocNotFoundException notFoundE) {
2107
                logMetacat.error("DBQuery.getOldVersionAllDocument - Error finding doc " 
2108
                		+ docidPlusVersion + " : " + notFoundE.getMessage());
2109
                // Rather than add a DocumentImple object into vetor, a String
2110
                // object
2111
                // - the doicd was added to the vector
2112
                documentImplList.add(docidPlusVersion);
2113
                // Continue the for loop
2114
                continue;
2115
            }//catch
2116
            catch (Exception e) {
2117
                logMetacat.error(
2118
                        "DBQuery.getOldVersionAllDocument - General error: "
2119
                                + e.getMessage());
2120
                // Continue the for loop
2121
                continue;
2122
            }//catch
2123

    
2124
        }//for
2125
        return documentImplList;
2126
    }//getOldVersionAllDocumentImple
2127

    
2128
    /**
2129
     * put a data file into a zip output stream
2130
     *
2131
     * @param docImpl, docmentImpl object which will be sent to zip output
2132
     *            stream
2133
     * @param zipOut, the zip output stream which the docImpl will be put
2134
     * @param packageZipEntry, the zip entry name for whole package
2135
     */
2136
    private void addDataFileToZipOutputStream(DocumentImpl docImpl,
2137
            ZipOutputStream zipOut, String packageZipEntry)
2138
            throws ClassNotFoundException, IOException, SQLException,
2139
            McdbException, Exception
2140
    {
2141
        byte[] byteString = null;
2142
        ZipEntry zEntry = null;
2143
        // this is data file; add file to zip
2144
        String filePath = PropertyService.getProperty("application.datafilepath");
2145
        if (!filePath.endsWith("/")) {
2146
            filePath += "/";
2147
        }
2148
        String fileName = filePath + docImpl.getDocID();
2149
        zEntry = new ZipEntry(packageZipEntry + "/data/" + docImpl.getDocID());
2150
        zipOut.putNextEntry(zEntry);
2151
        FileInputStream fin = null;
2152
        try {
2153
            fin = new FileInputStream(fileName);
2154
            byte[] buf = new byte[4 * 1024]; // 4K buffer
2155
            int b = fin.read(buf);
2156
            while (b != -1) {
2157
                zipOut.write(buf, 0, b);
2158
                b = fin.read(buf);
2159
            }//while
2160
            zipOut.closeEntry();
2161
        }//try
2162
        catch (IOException ioe) {
2163
            logMetacat.error("DBQuery.addDataFileToZipOutputStream - I/O error: "
2164
                    + ioe.getMessage());
2165
        }//catch
2166
    }//addDataFileToZipOutputStream()
2167

    
2168
    /**
2169
     * create a html summary for data package and put it into zip output stream
2170
     *
2171
     * @param docImplList, the documentImpl ojbects in data package
2172
     * @param zipOut, the zip output stream which the html should be put
2173
     * @param packageZipEntry, the zip entry name for whole package
2174
     */
2175
    private void addHtmlSummaryToZipOutputStream(Vector docImplList,
2176
            ZipOutputStream zipOut, String packageZipEntry) throws Exception
2177
    {
2178
        StringBuffer htmlDoc = new StringBuffer();
2179
        ZipEntry zEntry = null;
2180
        byte[] byteString = null;
2181
        InputStream source;
2182
        DBTransform xmlToHtml;
2183

    
2184
        //create a DBTransform ojbect
2185
        xmlToHtml = new DBTransform();
2186
        //head of html
2187
        htmlDoc.append("<html><head></head><body>");
2188
        for (int i = 0; i < docImplList.size(); i++) {
2189
            // If this String object, this means it is missed data file
2190
            if ((((docImplList.elementAt(i)).getClass()).toString())
2191
                    .equals("class java.lang.String")) {
2192

    
2193
                htmlDoc.append("<a href=\"");
2194
                String dataFileid = (String) docImplList.elementAt(i);
2195
                htmlDoc.append("./data/").append(dataFileid).append("\">");
2196
                htmlDoc.append("Data File: ");
2197
                htmlDoc.append(dataFileid).append("</a><br>");
2198
                htmlDoc.append("<br><hr><br>");
2199

    
2200
            }//if
2201
            else if ((((DocumentImpl) docImplList.elementAt(i)).getDoctype())
2202
                    .compareTo("BIN") != 0) { //this is an xml file so we can
2203
                                              // transform it.
2204
                //transform each file individually then concatenate all of the
2205
                //transformations together.
2206

    
2207
                //for metadata xml title
2208
                htmlDoc.append("<h2>");
2209
                htmlDoc.append(((DocumentImpl) docImplList.elementAt(i))
2210
                        .getDocID());
2211
                //htmlDoc.append(".");
2212
                //htmlDoc.append(((DocumentImpl)docImplList.elementAt(i)).getRev());
2213
                htmlDoc.append("</h2>");
2214
                //do the actual transform
2215
                StringWriter docString = new StringWriter();
2216
                xmlToHtml.transformXMLDocument(((DocumentImpl) docImplList
2217
                        .elementAt(i)).toString(), "-//NCEAS//eml-generic//EN",
2218
                        "-//W3C//HTML//EN", "html", docString, null, null);
2219
                htmlDoc.append(docString.toString());
2220
                htmlDoc.append("<br><br><hr><br><br>");
2221
            }//if
2222
            else { //this is a data file so we should link to it in the html
2223
                htmlDoc.append("<a href=\"");
2224
                String dataFileid = ((DocumentImpl) docImplList.elementAt(i))
2225
                        .getDocID();
2226
                htmlDoc.append("./data/").append(dataFileid).append("\">");
2227
                htmlDoc.append("Data File: ");
2228
                htmlDoc.append(dataFileid).append("</a><br>");
2229
                htmlDoc.append("<br><hr><br>");
2230
            }//else
2231
        }//for
2232
        htmlDoc.append("</body></html>");
2233
        // use standard encoding even though the different docs might have use different encodings,
2234
        // the String objects in java should be correct and able to be encoded as the same Metacat default
2235
        byteString = htmlDoc.toString().getBytes(MetaCatServlet.DEFAULT_ENCODING);
2236
        zEntry = new ZipEntry(packageZipEntry + "/metadata.html");
2237
        zEntry.setSize(byteString.length);
2238
        zipOut.putNextEntry(zEntry);
2239
        zipOut.write(byteString, 0, byteString.length);
2240
        zipOut.closeEntry();
2241
        //dbConn.close();
2242

    
2243
    }//addHtmlSummaryToZipOutputStream
2244

    
2245
    /**
2246
     * put a data packadge into a zip output stream
2247
     *
2248
     * @param docId, which the user want to put into zip output stream,it has version
2249
     * @param out, a servletoutput stream which the zip output stream will be
2250
     *            put
2251
     * @param user, the username of the user
2252
     * @param groups, the group of the user
2253
     */
2254
    public ZipOutputStream getZippedPackage(String docIdString,
2255
            ServletOutputStream out, String user, String[] groups,
2256
            String passWord) throws ClassNotFoundException, IOException,
2257
            SQLException, McdbException, NumberFormatException, Exception
2258
    {
2259
        ZipOutputStream zOut = null;
2260
        String elementDocid = null;
2261
        DocumentImpl docImpls = null;
2262
        //Connection dbConn = null;
2263
        Vector docIdList = new Vector();
2264
        Vector documentImplList = new Vector();
2265
        Vector htmlDocumentImplList = new Vector();
2266
        String packageId = null;
2267
        String rootName = "package";//the package zip entry name
2268

    
2269
        String docId = null;
2270
        int version = -5;
2271
        // Docid without revision
2272
        docId = DocumentUtil.getDocIdFromString(docIdString);
2273
        // revision number
2274
        version = DocumentUtil.getVersionFromString(docIdString);
2275

    
2276
        //check if the reqused docId is a data package id
2277
        if (!isDataPackageId(docId)) {
2278

    
2279
            /*
2280
             * Exception e = new Exception("The request the doc id "
2281
             * +docIdString+ " is not a data package id");
2282
             */
2283

    
2284
            //CB 1/6/03: if the requested docid is not a datapackage, we just
2285
            // zip
2286
            //up the single document and return the zip file.
2287
            if (!hasPermissionToExportPackage(docId, user, groups)) {
2288

    
2289
                Exception e = new Exception("User " + user
2290
                        + " does not have permission"
2291
                        + " to export the data package " + docIdString);
2292
                throw e;
2293
            }
2294

    
2295
            docImpls = new DocumentImpl(docIdString);
2296
            //checking if the user has the permission to read the documents
2297
            if (DocumentImpl.hasReadPermission(user, groups, docImpls
2298
                    .getDocID())) {
2299
                zOut = new ZipOutputStream(out);
2300
                //if the docImpls is metadata
2301
                if ((docImpls.getDoctype()).compareTo("BIN") != 0) {
2302
                    //add metadata into zip output stream
2303
                    addDocToZipOutputStream(docImpls, zOut, rootName);
2304
                }//if
2305
                else {
2306
                    //it is data file
2307
                    addDataFileToZipOutputStream(docImpls, zOut, rootName);
2308
                    htmlDocumentImplList.add(docImpls);
2309
                }//else
2310
            }//if
2311

    
2312
            zOut.finish(); //terminate the zip file
2313
            return zOut;
2314
        }
2315
        // Check the permission of user
2316
        else if (!hasPermissionToExportPackage(docId, user, groups)) {
2317

    
2318
            Exception e = new Exception("User " + user
2319
                    + " does not have permission"
2320
                    + " to export the data package " + docIdString);
2321
            throw e;
2322
        } else //it is a packadge id
2323
        {
2324
            //store the package id
2325
            packageId = docId;
2326
            //get current version in database
2327
            int currentVersion = getCurrentRevFromXMLDoumentsTable(packageId);
2328
            //If it is for current version (-1 means user didn't specify
2329
            // revision)
2330
            if ((version == -1) || version == currentVersion) {
2331
                //get current version number
2332
                version = currentVersion;
2333
                //get package zip entry name
2334
                //it should be docId.revsion.package
2335
                rootName = packageId + PropertyService.getProperty("document.accNumSeparator")
2336
                        + version + PropertyService.getProperty("document.accNumSeparator")
2337
                        + "package";
2338
                //get the whole id list for data packadge
2339
                docIdList = getCurrentDocidListForDataPackage(packageId);
2340
                //get the whole documentImple object
2341
                documentImplList = getCurrentAllDocumentImpl(docIdList);
2342

    
2343
            }//if
2344
            else if (version > currentVersion || version < -1) {
2345
                throw new Exception("The user specified docid: " + docId + "."
2346
                        + version + " doesn't exist");
2347
            }//else if
2348
            else //for an old version
2349
            {
2350

    
2351
                rootName = docIdString
2352
                        + PropertyService.getProperty("document.accNumSeparator") + "package";
2353
                //get the whole id list for data packadge
2354
                docIdList = getOldVersionDocidListForDataPackage(docIdString);
2355

    
2356
                //get the whole documentImple object
2357
                documentImplList = getOldVersionAllDocumentImpl(docIdList);
2358
            }//else
2359

    
2360
            // Make sure documentImplist is not empty
2361
            if (documentImplList.isEmpty()) { throw new Exception(
2362
                    "Couldn't find component for data package: " + packageId); }//if
2363

    
2364
            zOut = new ZipOutputStream(out);
2365
            //put every element into zip output stream
2366
            for (int i = 0; i < documentImplList.size(); i++) {
2367
                // if the object in the vetor is String, this means we couldn't
2368
                // find
2369
                // the document locally, we need find it remote
2370
                if ((((documentImplList.elementAt(i)).getClass()).toString())
2371
                        .equals("class java.lang.String")) {
2372
                    // Get String object from vetor
2373
                    String documentId = (String) documentImplList.elementAt(i);
2374
                    logMetacat.info("DBQuery.getZippedPackage - docid: " + documentId);
2375
                    // Get doicd without revision
2376
                    String docidWithoutRevision = 
2377
                    	DocumentUtil.getDocIdFromString(documentId);
2378
                    logMetacat.info("DBQuery.getZippedPackage - docidWithoutRevsion: "
2379
                            + docidWithoutRevision);
2380
                    // Get revision
2381
                    String revision = 
2382
                    	DocumentUtil.getRevisionStringFromString(documentId);
2383
                    logMetacat.info("DBQuery.getZippedPackage - revision from docIdentifier: "
2384
                            + revision);
2385
                    // Zip entry string
2386
                    String zipEntryPath = rootName + "/data/";
2387
                    // Create a RemoteDocument object
2388
                    RemoteDocument remoteDoc = new RemoteDocument(
2389
                            docidWithoutRevision, revision, user, passWord,
2390
                            zipEntryPath);
2391
                    // Here we only read data file from remote metacat
2392
                    String docType = remoteDoc.getDocType();
2393
                    if (docType != null) {
2394
                        if (docType.equals("BIN")) {
2395
                            // Put remote document to zip output
2396
                            remoteDoc.readDocumentFromRemoteServerByZip(zOut);
2397
                            // Add String object to htmlDocumentImplList
2398
                            String elementInHtmlList = remoteDoc
2399
                                    .getDocIdWithoutRevsion()
2400
                                    + PropertyService.getProperty("document.accNumSeparator")
2401
                                    + remoteDoc.getRevision();
2402
                            htmlDocumentImplList.add(elementInHtmlList);
2403
                        }//if
2404
                    }//if
2405

    
2406
                }//if
2407
                else {
2408
                    //create a docmentImpls object (represent xml doc) base on
2409
                    // the docId
2410
                    docImpls = (DocumentImpl) documentImplList.elementAt(i);
2411
                    //checking if the user has the permission to read the
2412
                    // documents
2413
                    if (DocumentImpl.hasReadPermission(user, groups, docImpls
2414
                            .getDocID())) {
2415
                        //if the docImpls is metadata
2416
                        if ((docImpls.getDoctype()).compareTo("BIN") != 0) {
2417
                            //add metadata into zip output stream
2418
                            addDocToZipOutputStream(docImpls, zOut, rootName);
2419
                            //add the documentImpl into the vetor which will
2420
                            // be used in html
2421
                            htmlDocumentImplList.add(docImpls);
2422

    
2423
                        }//if
2424
                        else {
2425
                            //it is data file
2426
                            addDataFileToZipOutputStream(docImpls, zOut,
2427
                                    rootName);
2428
                            htmlDocumentImplList.add(docImpls);
2429
                        }//else
2430
                    }//if
2431
                }//else
2432
            }//for
2433

    
2434
            //add html summary file
2435
            addHtmlSummaryToZipOutputStream(htmlDocumentImplList, zOut,
2436
                    rootName);
2437
            zOut.finish(); //terminate the zip file
2438
            //dbConn.close();
2439
            return zOut;
2440
        }//else
2441
    }//getZippedPackage()
2442

    
2443
    private class ReturnFieldValue
2444
    {
2445

    
2446
        private String docid = null; //return field value for this docid
2447

    
2448
        private String fieldValue = null;
2449

    
2450
        private String xmlFieldValue = null; //return field value in xml
2451
                                             // format
2452
        private String fieldType = null; //ATTRIBUTE, TEXT...
2453

    
2454
        public void setDocid(String myDocid)
2455
        {
2456
            docid = myDocid;
2457
        }
2458

    
2459
        public String getDocid()
2460
        {
2461
            return docid;
2462
        }
2463

    
2464
        public void setFieldValue(String myValue)
2465
        {
2466
            fieldValue = myValue;
2467
        }
2468

    
2469
        public String getFieldValue()
2470
        {
2471
            return fieldValue;
2472
        }
2473

    
2474
        public void setXMLFieldValue(String xml)
2475
        {
2476
            xmlFieldValue = xml;
2477
        }
2478

    
2479
        public String getXMLFieldValue()
2480
        {
2481
            return xmlFieldValue;
2482
        }
2483
        
2484
        public void setFieldType(String myType)
2485
        {
2486
            fieldType = myType;
2487
        }
2488

    
2489
        public String getFieldType()
2490
        {
2491
            return fieldType;
2492
        }
2493

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