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: 2011-11-09 17:45:24 -0800 (Wed, 09 Nov 2011) $'
14
 * '$Revision: 6629 $'
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.sql.Timestamp;
49
import java.util.ArrayList;
50
import java.util.Date;
51
import java.util.Enumeration;
52
import java.util.Hashtable;
53
import java.util.Iterator;
54
import java.util.List;
55
import java.util.StringTokenizer;
56
import java.util.Vector;
57
import java.util.zip.ZipEntry;
58
import java.util.zip.ZipOutputStream;
59

    
60
import javax.servlet.ServletOutputStream;
61
import javax.servlet.http.HttpServletResponse;
62

    
63
import org.apache.log4j.Logger;
64

    
65
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlInterface;
66
import edu.ucsb.nceas.metacat.database.DBConnection;
67
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
68
import edu.ucsb.nceas.metacat.properties.PropertyService;
69
import edu.ucsb.nceas.metacat.util.AuthUtil;
70
import edu.ucsb.nceas.metacat.util.DocumentUtil;
71
import edu.ucsb.nceas.metacat.util.MetacatUtil;
72
import edu.ucsb.nceas.morpho.datapackage.Triple;
73
import edu.ucsb.nceas.morpho.datapackage.TripleCollection;
74
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
75

    
76

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

    
86
    static final int ALL = 1;
87

    
88
    static final int WRITE = 2;
89

    
90
    static final int READ = 4;
91
    
92
    private String qformat = "xml";
93
    
94
    // are we combining the query with docid list and, if so, using INTERSECT or UNION?
95
    private String operator = null;
96

    
97
    //private Connection conn = null;
98
    private String parserName = null;
99

    
100
    private Logger logMetacat = Logger.getLogger(DBQuery.class);
101

    
102
    /** true if the metacat spatial option is installed **/
103
    private final boolean METACAT_SPATIAL = true;
104

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

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

    
139
        if (args.length < 1) {
140
            System.err.println("Wrong number of arguments!!!");
141
            System.err.println("USAGE: java DBQuery [-t] [-index] <xmlfile>");
142
            return;
143
        } else {
144
            try {
145

    
146
                int i = 0;
147
                boolean showRuntime = false;
148
                boolean useXMLIndex = false;
149
                if (args[i].equals("-t")) {
150
                    showRuntime = true;
151
                    i++;
152
                }
153
                if (args[i].equals("-index")) {
154
                    useXMLIndex = true;
155
                    i++;
156
                }
157
                String xmlfile = args[i];
158

    
159
                // Time the request if asked for
160
                double startTime = System.currentTimeMillis();
161

    
162
                // Open a connection to the database
163
                //Connection dbconn = util.openDBConnection();
164

    
165
                double connTime = System.currentTimeMillis();
166

    
167
                // Execute the query
168
                DBQuery queryobj = new DBQuery();
169
                Reader xml = new InputStreamReader(new FileInputStream(new File(xmlfile)));
170
                Hashtable nodelist = null;
171
                //nodelist = queryobj.findDocuments(xml, null, null, useXMLIndex);
172

    
173
                // Print the reulting document listing
174
                StringBuffer result = new StringBuffer();
175
                String document = null;
176
                String docid = null;
177
                result.append("<?xml version=\"1.0\"?>\n");
178
                result.append("<resultset>\n");
179

    
180
                if (!showRuntime) {
181
                    Enumeration doclist = nodelist.keys();
182
                    while (doclist.hasMoreElements()) {
183
                        docid = (String) doclist.nextElement();
184
                        document = (String) nodelist.get(docid);
185
                        result.append("  <document>\n    " + document
186
                                + "\n  </document>\n");
187
                    }
188

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

    
215
            } catch (Exception e) {
216
                System.err.println("Error in DBQuery.main");
217
                System.err.println(e.getMessage());
218
                e.printStackTrace(System.err);
219
            }
220
        }
221
    }
222

    
223
    /**
224
     * construct an instance of the DBQuery class
225
     *
226
     * <p>
227
     * Generally, one would call the findDocuments() routine after creating an
228
     * instance to specify the search query
229
     * </p>
230
     *
231

    
232
     * @param parserName the fully qualified name of a Java class implementing
233
     *            the org.xml.sax.XMLReader interface
234
     */
235
    public DBQuery() throws PropertyNotFoundException
236
    {
237
        String parserName = PropertyService.getProperty("xml.saxparser");
238
        this.parserName = parserName;
239
    }
240

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

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

    
316
  }
317

    
318

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

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

    
383

    
384

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

    
411
         // if the user is a moderator, then pass a param to the 
412
         // xsl specifying the fact
413
         if(AuthUtil.isModerator(user, groups)){
414
        	 params.put("isModerator", new String[] {"true"});
415
         }
416

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

    
438
      }//else
439

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

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

    
485
    //try to get the cached version first    
486
    // Hashtable sessionHash = MetaCatServlet.getSessionHash();
487
    // HttpSession sess = (HttpSession)sessionHash.get(sessionid);
488

    
489
    
490
    resultset.append("<?xml version=\"1.0\"?>\n");
491
    resultset.append("<resultset>\n");
492
    resultset.append("  <pagestart>" + pagestart + "</pagestart>\n");
493
    resultset.append("  <pagesize>" + pagesize + "</pagesize>\n");
494
    resultset.append("  <nextpage>" + (pagestart + 1) + "</nextpage>\n");
495
    resultset.append("  <previouspage>" + (pagestart - 1) + "</previouspage>\n");
496

    
497
    resultset.append("  <query>" + xmlquery + "</query>");
498
    //send out a new query
499
    if (out != null)
500
    {
501
    	try {
502
    	  out.write(resultset.toString());
503
		} catch (IOException e) {
504
			logMetacat.error(e.getMessage(), e);
505
		}
506
    }
507
    if (qspec != null)
508
    {
509
      try
510
      {
511

    
512
        //checkout the dbconnection
513
        dbconn = DBConnectionPool.getDBConnection("DBQuery.findDocuments");
514
        serialNumber = dbconn.getCheckOutSerialNumber();
515

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

    
574
    //default to returning the whole resultset
575
    return resultset;
576
  }//createResultDocuments
577

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

    
627
      /*
628
       * Check the docidOverride Vector
629
       * if defined, we bypass the qspec.printSQL() method
630
       * and contruct a simpler query based on a 
631
       * list of docids rather than a bunch of subselects
632
       */
633
      // keep track of the values we add as prepared statement question marks (?)
634
	  List<Object> docidValues = new ArrayList<Object>();
635
      if ( givenDocids == null || givenDocids.size() == 0 ) {
636
          query = qspec.printSQL(useXMLIndex, docidValues);
637
          parameterValues.addAll(docidValues);
638
      } else {
639
    	  // condition for the docids
640
    	  List<Object> docidConditionValues = new ArrayList<Object>();
641
    	  StringBuffer docidCondition = new StringBuffer();
642
    	  docidCondition.append( " docid IN (" );
643
          for (int i = 0; i < givenDocids.size(); i++) {  
644
        	  docidCondition.append("?");
645
        	  if (i < givenDocids.size()-1) {
646
        		  docidCondition.append(",");
647
        	  }
648
        	  docidConditionValues.add((String)givenDocids.elementAt(i));
649
          }
650
          docidCondition.append( ") " );
651
		  
652
    	  // include the docids, either exclusively, or in conjuction with the query
653
    	  if (operator == null) {
654
    		  query = "SELECT docid, docname, doctype, date_created, date_updated, rev FROM xml_documents WHERE";
655
              query = query + docidCondition.toString();
656
              parameterValues.addAll(docidConditionValues);
657
    	  } else {
658
    		  // start with the keyword query, but add conditions
659
              query = qspec.printSQL(useXMLIndex, docidValues);
660
              parameterValues.addAll(docidValues);
661
              String myOperator = "";
662
              if (!query.endsWith("WHERE")) {
663
	              if (operator.equalsIgnoreCase(QueryGroup.UNION)) {
664
	            	  myOperator =  " OR ";
665
	              }
666
	              else {
667
	            	  myOperator =  " AND ";
668
	              }
669
              }
670
              query = query + myOperator + docidCondition.toString();
671
              parameterValues.addAll(docidConditionValues);
672

    
673
    	  }
674
      } 
675
      // we don't actually use this query for anything
676
      List<Object> ownerValues = new ArrayList<Object>();
677
      String ownerQuery = getOwnerQuery(user, ownerValues);
678
      //logMetacat.debug("query: " + query);
679
      logMetacat.debug("DBQuery.findResultDoclist - owner query: " + ownerQuery);
680
      // if query is not the owner query, we need to check the permission
681
      // otherwise we don't need (owner has all permission by default)
682
      if (!query.equals(ownerQuery))
683
      {
684
        // set user name and group
685
        qspec.setUserName(user);
686
        qspec.setGroup(groups);
687
        // Get access query
688
        String accessQuery = qspec.getAccessQuery();
689
        if(!query.endsWith("WHERE")){
690
            query = query + accessQuery;
691
        } else {
692
            query = query + accessQuery.substring(4, accessQuery.length());
693
        }
694
        
695
      }
696
      logMetacat.debug("DBQuery.findResultDoclist - final selection query: " + query);
697
      String selectionAndExtendedQuery = null;
698
      // we only get cache for public
699
      if (user != null && user.equalsIgnoreCase("public") 
700
     		 && pagesize == 0 && PropertyService.getProperty("database.queryCacheOn").equals("true"))
701
      {
702
    	  selectionAndExtendedQuery = query +qspec.getReturnDocList()+qspec.getReturnFieldList();
703
   	      String cachedResult = getResultXMLFromCache(selectionAndExtendedQuery);
704
   	      logMetacat.debug("DBQuery.findResultDoclist - The key of query cache is " + selectionAndExtendedQuery);
705
   	      //System.out.println("==========the string from cache is "+cachedResult);
706
   	      if (cachedResult != null)
707
   	      {
708
   	    	logMetacat.info("DBQuery.findResultDoclist - result from cache !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
709
   	    	 if (out != null)
710
   	         {
711
   	             out.write(cachedResult);
712
   	         }
713
   	    	 resultsetBuffer.append(cachedResult);
714
   	    	 return resultsetBuffer;
715
   	      }
716
      }
717
      
718
      startTime = System.currentTimeMillis() / 1000;
719
      pstmt = dbconn.prepareStatement(query);
720
      
721
      // set all the values we have collected 
722
      pstmt = setPreparedStatementValues(parameterValues, pstmt);
723
      
724
      logMetacat.debug("Prepared statement after setting parameter values: " + pstmt.toString());
725
      rs = pstmt.executeQuery();
726

    
727
      double queryExecuteTime = System.currentTimeMillis() / 1000;
728
      logMetacat.debug("DBQuery.findResultDoclist - Time to execute select docid query is "
729
                    + (queryExecuteTime - startTime));
730
      MetacatUtil.writeDebugToFile("\n\n\n\n\n\nExecute selection query  "
731
              + (queryExecuteTime - startTime));
732
      MetacatUtil.writeDebugToDelimiteredFile(""+(queryExecuteTime - startTime), false);
733

    
734
      boolean tableHasRows = rs.next();
735
      
736
      if(pagesize == 0)
737
      { //this makes sure we get all results if there is no paging
738
        pagesize = NONPAGESIZE;
739
        pagestart = NONPAGESIZE;
740
      } 
741
      
742
      int currentIndex = 0;
743
      while (tableHasRows)
744
      {
745
        logMetacat.debug("DBQuery.findResultDoclist - getting result: " + currentIndex);
746
        docid = rs.getString(1).trim();
747
        logMetacat.debug("DBQuery.findResultDoclist -  processing: " + docid);
748
        docname = rs.getString(2);
749
        doctype = rs.getString(3);
750
        logMetacat.debug("DBQuery.findResultDoclist - processing: " + doctype);
751
        createDate = rs.getString(4);
752
        updateDate = rs.getString(5);
753
        rev = rs.getInt(6);
754
        
755
         Vector returndocVec = qspec.getReturnDocList();
756
       if (returndocVec.size() == 0 || returndocVec.contains(doctype))
757
        {
758
          logMetacat.debug("DBQuery.findResultDoclist - NOT Back tracing now...");
759
           document = new StringBuffer();
760

    
761
           String completeDocid = docid
762
                            + PropertyService.getProperty("document.accNumSeparator");
763
           completeDocid += rev;
764
           document.append("<docid>").append(completeDocid).append("</docid>");
765
           if (docname != null)
766
           {
767
               document.append("<docname>" + docname + "</docname>");
768
           }
769
           if (doctype != null)
770
           {
771
              document.append("<doctype>" + doctype + "</doctype>");
772
           }
773
           if (createDate != null)
774
           {
775
               document.append("<createdate>" + createDate + "</createdate>");
776
           }
777
           if (updateDate != null)
778
           {
779
             document.append("<updatedate>" + updateDate + "</updatedate>");
780
           }
781
           // Store the document id and the root node id
782
           
783
           docListResult.addResultDocument(
784
             new ResultDocument(docid, (String) document.toString()));
785
           logMetacat.info("DBQuery.findResultDoclist - real result: " + docid);
786
           currentIndex++;
787
           count++;
788
        }//else
789
        
790
        // when doclist reached the offset number, send out doc list and empty
791
        // the hash table
792
        if (count == offset && pagesize == NONPAGESIZE)
793
        { //if pagesize is not 0, do this later.
794
          //reset count
795
          //logMetacat.warn("############doing subset cache");
796
          count = 0;
797
          handleSubsetResult(qspec, resultsetBuffer, out, docListResult,
798
                              user, groups,dbconn, useXMLIndex, qformat);
799
          //reset docListResult
800
          docListResult = new ResultDocumentSet();
801
        }
802
       
803
       logMetacat.debug("DBQuery.findResultDoclist - currentIndex: " + currentIndex);
804
       logMetacat.debug("DBQuery.findResultDoclist - page comparator: " + (pagesize * pagestart) + pagesize);
805
       if(currentIndex >= ((pagesize * pagestart) + pagesize))
806
       {
807
         ResultDocumentSet pagedResultsHash = new ResultDocumentSet();
808
         for(int i=pagesize*pagestart; i<docListResult.size(); i++)
809
         {
810
           pagedResultsHash.put(docListResult.get(i));
811
         }
812
         
813
         docListResult = pagedResultsHash;
814
         break;
815
       }
816
       // Advance to the next record in the cursor
817
       tableHasRows = rs.next();
818
       if(!tableHasRows)
819
       {
820
         ResultDocumentSet pagedResultsHash = new ResultDocumentSet();
821
         //get the last page of information then break
822
         if(pagesize != NONPAGESIZE)
823
         {
824
           for(int i=pagesize*pagestart; i<docListResult.size(); i++)
825
           {
826
             pagedResultsHash.put(docListResult.get(i));
827
           }
828
           docListResult = pagedResultsHash;
829
         }
830
         
831
         lastpage = true;
832
         break;
833
       }
834
     }//while
835
     
836
     rs.close();
837
     pstmt.close();
838
     long docListTime = System.currentTimeMillis() - startSelectionTime;
839
     long docListWarnLimit = Long.parseLong(PropertyService.getProperty("dbquery.findDocListTimeWarnLimit"));
840
     if (docListTime > docListWarnLimit) {
841
    	 logMetacat.warn("DBQuery.findResultDoclist - Total time to get docid list is: "
842
                          + docListTime);
843
     }
844
     MetacatUtil.writeDebugToFile("---------------------------------------------------------------------------------------------------------------Total selection: "
845
             + docListTime);
846
     MetacatUtil.writeDebugToDelimiteredFile(" "+ docListTime, false);
847
     //if docListResult is not empty, it need to be sent.
848
     if (docListResult.size() != 0)
849
     {
850
      
851
       handleSubsetResult(qspec,resultsetBuffer, out, docListResult,
852
                              user, groups,dbconn, useXMLIndex, qformat);
853
     }
854

    
855
     resultsetBuffer.append("\n<lastpage>" + lastpage + "</lastpage>\n");
856
     if (out != null)
857
     {
858
         out.write("\n<lastpage>" + lastpage + "</lastpage>\n");
859
     }
860
     
861
     // now we only cached none-paged query and user is public
862
     if (user != null && user.equalsIgnoreCase("public") 
863
    		 && pagesize == NONPAGESIZE && PropertyService.getProperty("database.queryCacheOn").equals("true"))
864
     {
865
       //System.out.println("the string stored into cache is "+ resultsetBuffer.toString());
866
  	   storeQueryResultIntoCache(selectionAndExtendedQuery, resultsetBuffer.toString());
867
     }
868
          
869
     return resultsetBuffer;
870
    }//findReturnDoclist
871

    
872

    
873
    /*
874
     * Send completed search hashtable(part of reulst)to output stream
875
     * and buffer into a buffer stream
876
     */
877
    private StringBuffer handleSubsetResult(QuerySpecification qspec,
878
                                           StringBuffer resultset,
879
                                           Writer out, ResultDocumentSet partOfDoclist,
880
                                           String user, String[]groups,
881
                                       DBConnection dbconn, boolean useXMLIndex,
882
                                       String qformat)
883
                                       throws Exception
884
   {
885
     double startReturnFieldTime = System.currentTimeMillis();
886
     // check if there is a record in xml_returnfield
887
     // and get the returnfield_id and usage count
888
     int usage_count = getXmlReturnfieldsTableId(qspec, dbconn);
889
     boolean enterRecords = false;
890

    
891
     // get value of database.xmlReturnfieldCount
892
     int count = (new Integer(PropertyService
893
                            .getProperty("database.xmlReturnfieldCount")))
894
                            .intValue();
895

    
896
     // set enterRecords to true if usage_count is more than the offset
897
     // specified in metacat.properties
898
     if(usage_count > count){
899
         enterRecords = true;
900
     }
901

    
902
     if(returnfield_id < 0){
903
         logMetacat.warn("DBQuery.handleSubsetResult - Error in getting returnfield id from"
904
                                  + "xml_returnfield table");
905
         enterRecords = false;
906
     }
907

    
908
     // get the hashtable containing the docids that already in the
909
     // xml_queryresult table
910
     logMetacat.info("DBQuery.handleSubsetResult - size of partOfDoclist before"
911
                             + " docidsInQueryresultTable(): "
912
                             + partOfDoclist.size());
913
     long startGetReturnValueFromQueryresultable = System.currentTimeMillis();
914
     Hashtable queryresultDocList = docidsInQueryresultTable(returnfield_id,
915
                                                        partOfDoclist, dbconn);
916

    
917
     // remove the keys in queryresultDocList from partOfDoclist
918
     Enumeration _keys = queryresultDocList.keys();
919
     while (_keys.hasMoreElements()){
920
         partOfDoclist.remove((String)_keys.nextElement());
921
     }
922
     
923
     long queryResultReturnValuetime = System.currentTimeMillis() - startGetReturnValueFromQueryresultable;
924
     long queryResultWarnLimit = 
925
    	 Long.parseLong(PropertyService.getProperty("dbquery.findQueryResultsTimeWarnLimit"));
926
     
927
     if (queryResultReturnValuetime > queryResultWarnLimit) {
928
    	 logMetacat.warn("DBQuery.handleSubsetResult - Time to get return fields from xml_queryresult table is (Part1 in return fields) " +
929
    		 queryResultReturnValuetime);
930
     }
931
     MetacatUtil.writeDebugToFile("-----------------------------------------Get fields from xml_queryresult(Part1 in return fields) " +
932
    		 queryResultReturnValuetime);
933
     MetacatUtil.writeDebugToDelimiteredFile(" " + queryResultReturnValuetime,false);
934
     
935
     long startExtendedQuery = System.currentTimeMillis();
936
     // backup the keys-elements in partOfDoclist to check later
937
     // if the doc entry is indexed yet
938
     Hashtable partOfDoclistBackup = new Hashtable();
939
     Iterator itt = partOfDoclist.getDocids();
940
     while (itt.hasNext()){
941
       Object key = itt.next();
942
         partOfDoclistBackup.put(key, partOfDoclist.get(key));
943
     }
944

    
945
     logMetacat.info("DBQuery.handleSubsetResult - size of partOfDoclist after"
946
                             + " docidsInQueryresultTable(): "
947
                             + partOfDoclist.size());
948

    
949
     //add return fields for the documents in partOfDoclist
950
     partOfDoclist = addReturnfield(partOfDoclist, qspec, user, groups,
951
                                        dbconn, useXMLIndex, qformat);
952
     long extendedQueryRunTime = startExtendedQuery - System.currentTimeMillis();
953
     long extendedQueryWarnLimit = 
954
    	 Long.parseLong(PropertyService.getProperty("dbquery.extendedQueryRunTimeWarnLimit"));
955
  
956
     if (extendedQueryRunTime > extendedQueryWarnLimit) {
957
    	 logMetacat.warn("DBQuery.handleSubsetResult - Get fields from index and node table (Part2 in return fields) "
958
        		                                          + extendedQueryRunTime);
959
     }
960
     MetacatUtil.writeDebugToFile("-----------------------------------------Get fields from extened query(Part2 in return fields) "
961
             + extendedQueryRunTime);
962
     MetacatUtil.writeDebugToDelimiteredFile(" "
963
             + extendedQueryRunTime, false);
964
     //add relationship part part docid list for the documents in partOfDocList
965
     //partOfDoclist = addRelationship(partOfDoclist, qspec, dbconn, useXMLIndex);
966

    
967
     long startStoreReturnField = System.currentTimeMillis();
968
     Iterator keys = partOfDoclist.getDocids();
969
     String key = null;
970
     String element = null;
971
     String query = null;
972
     int offset = (new Integer(PropertyService
973
                               .getProperty("database.queryresultStringLength")))
974
                               .intValue();
975
     while (keys.hasNext())
976
     {
977
         key = (String) keys.next();
978
         element = (String)partOfDoclist.get(key);
979
         
980
	 // check if the enterRecords is true, elements is not null, element's
981
         // length is less than the limit of table column and if the document
982
         // has been indexed already
983
         if(enterRecords && element != null
984
		&& element.length() < offset
985
		&& element.compareTo((String) partOfDoclistBackup.get(key)) != 0){
986
             query = "INSERT INTO xml_queryresult (returnfield_id, docid, "
987
                 + "queryresult_string) VALUES (?, ?, ?)";
988

    
989
             PreparedStatement pstmt = null;
990
             pstmt = dbconn.prepareStatement(query);
991
             pstmt.setInt(1, returnfield_id);
992
             pstmt.setString(2, key);
993
             pstmt.setString(3, element);
994
            
995
             dbconn.increaseUsageCount(1);
996
             try
997
             {
998
            	 pstmt.execute();
999
             }
1000
             catch(Exception e)
1001
             {
1002
            	 logMetacat.warn("DBQuery.handleSubsetResult - couldn't insert the element to xml_queryresult table "+e.getLocalizedMessage());
1003
             }
1004
             finally
1005
             {
1006
                pstmt.close();
1007
             }
1008
         }
1009
        
1010
         // A string with element
1011
         String xmlElement = "  <document>" + element + "</document>";
1012

    
1013
         //send single element to output
1014
         if (out != null)
1015
         {
1016
             out.write(xmlElement);
1017
         }
1018
         resultset.append(xmlElement);
1019
     }//while
1020
     
1021
     double storeReturnFieldTime = System.currentTimeMillis() - startStoreReturnField;
1022
     long storeReturnFieldWarnLimit = 
1023
    	 Long.parseLong(PropertyService.getProperty("dbquery.storeReturnFieldTimeWarnLimit"));
1024

    
1025
     if (storeReturnFieldTime > storeReturnFieldWarnLimit) {
1026
    	 logMetacat.warn("DBQuery.handleSubsetResult - Time to store new return fields into xml_queryresult table (Part4 in return fields) "
1027
                   + storeReturnFieldTime);
1028
     }
1029
     MetacatUtil.writeDebugToFile("-----------------------------------------Insert new record to xml_queryresult(Part4 in return fields) "
1030
             + storeReturnFieldTime);
1031
     MetacatUtil.writeDebugToDelimiteredFile(" " + storeReturnFieldTime, false);
1032
     
1033
     Enumeration keysE = queryresultDocList.keys();
1034
     while (keysE.hasMoreElements())
1035
     {
1036
         key = (String) keysE.nextElement();
1037
         element = (String)queryresultDocList.get(key);
1038
         // A string with element
1039
         String xmlElement = "  <document>" + element + "</document>";
1040
         //send single element to output
1041
         if (out != null)
1042
         {
1043
             out.write(xmlElement);
1044
         }
1045
         resultset.append(xmlElement);
1046
     }//while
1047
     double returnFieldTime = System.currentTimeMillis() - startReturnFieldTime;
1048
     long totalReturnFieldWarnLimit = 
1049
    	 Long.parseLong(PropertyService.getProperty("dbquery.totalReturnFieldTimeWarnLimit"));
1050

    
1051
     if (returnFieldTime > totalReturnFieldWarnLimit) {
1052
    	 logMetacat.warn("DBQuery.handleSubsetResult - Total time to get return fields is: "
1053
                           + returnFieldTime);
1054
     }
1055
     MetacatUtil.writeDebugToFile("DBQuery.handleSubsetResult - ---------------------------------------------------------------------------------------------------------------"+
1056
    		 "Total to get return fields  " + returnFieldTime);
1057
     MetacatUtil.writeDebugToDelimiteredFile("DBQuery.handleSubsetResult - "+ returnFieldTime, false);
1058
     return resultset;
1059
 }
1060

    
1061
   /**
1062
    * Get the docids already in xml_queryresult table and corresponding
1063
    * queryresultstring as a hashtable
1064
    */
1065
   private Hashtable docidsInQueryresultTable(int returnfield_id,
1066
                                              ResultDocumentSet partOfDoclist,
1067
                                              DBConnection dbconn){
1068

    
1069
         Hashtable returnValue = new Hashtable();
1070
         PreparedStatement pstmt = null;
1071
         ResultSet rs = null;
1072
         
1073
         // keep track of parameter values
1074
         List<Object> parameterValues = new ArrayList<Object>();
1075

    
1076
         // get partOfDoclist as string for the query
1077
         Iterator keylist = partOfDoclist.getDocids();
1078
         StringBuffer doclist = new StringBuffer();
1079
         while (keylist.hasNext())
1080
         {
1081
             doclist.append("?,");
1082
             parameterValues.add((String) keylist.next());
1083
         }//while
1084

    
1085
         if (doclist.length() > 0)
1086
         {
1087
             doclist.deleteCharAt(doclist.length() - 1); //remove the last comma
1088

    
1089
             // the query to find out docids from xml_queryresult
1090
             String query = "select docid, queryresult_string from "
1091
                          + "xml_queryresult where returnfield_id = " +
1092
                          returnfield_id +" and docid in ("+ doclist + ")";
1093
             logMetacat.info("DBQuery.docidsInQueryresultTable - Query to get docids from xml_queryresult:"
1094
                                      + query);
1095

    
1096
             try {
1097
                 // prepare and execute the query
1098
                 pstmt = dbconn.prepareStatement(query);
1099
                 // bind parameter values
1100
                 pstmt = setPreparedStatementValues(parameterValues, pstmt);
1101
                 
1102
                 dbconn.increaseUsageCount(1);
1103
                 pstmt.execute();
1104
                 rs = pstmt.getResultSet();
1105
                 boolean tableHasRows = rs.next();
1106
                 while (tableHasRows) {
1107
                     // store the returned results in the returnValue hashtable
1108
                     String key = rs.getString(1);
1109
                     String element = rs.getString(2);
1110

    
1111
                     if(element != null){
1112
                         returnValue.put(key, element);
1113
                     } else {
1114
                         logMetacat.info("DBQuery.docidsInQueryresultTable - Null elment found ("
1115
                         + "DBQuery.docidsInQueryresultTable)");
1116
                     }
1117
                     tableHasRows = rs.next();
1118
                 }
1119
                 rs.close();
1120
                 pstmt.close();
1121
             } catch (Exception e){
1122
                 logMetacat.error("DBQuery.docidsInQueryresultTable - Error getting docids from "
1123
                                          + "queryresult: " + e.getMessage());
1124
              }
1125
         }
1126
         return returnValue;
1127
     }
1128

    
1129

    
1130
   /**
1131
    * Method to get id from xml_returnfield table
1132
    * for a given query specification
1133
    */
1134
   private int returnfield_id;
1135
   private int getXmlReturnfieldsTableId(QuerySpecification qspec,
1136
                                           DBConnection dbconn){
1137
       int id = -1;
1138
       int count = 1;
1139
       PreparedStatement pstmt = null;
1140
       ResultSet rs = null;
1141
       String returnfield = qspec.getSortedReturnFieldString();
1142

    
1143
       // query for finding the id from xml_returnfield
1144
       String query = "SELECT returnfield_id, usage_count FROM xml_returnfield "
1145
            + "WHERE returnfield_string LIKE ?";
1146
       logMetacat.info("DBQuery.getXmlReturnfieldsTableId - ReturnField Query:" + query);
1147

    
1148
       try {
1149
           // prepare and run the query
1150
           pstmt = dbconn.prepareStatement(query);
1151
           pstmt.setString(1,returnfield);
1152
           dbconn.increaseUsageCount(1);
1153
           pstmt.execute();
1154
           rs = pstmt.getResultSet();
1155
           boolean tableHasRows = rs.next();
1156

    
1157
           // if record found then increase the usage count
1158
           // else insert a new record and get the id of the new record
1159
           if(tableHasRows){
1160
               // get the id
1161
               id = rs.getInt(1);
1162
               count = rs.getInt(2) + 1;
1163
               rs.close();
1164
               pstmt.close();
1165

    
1166
               // increase the usage count
1167
               query = "UPDATE xml_returnfield SET usage_count = ?"
1168
                   + " WHERE returnfield_id = ?";
1169
               logMetacat.info("DBQuery.getXmlReturnfieldsTableId - ReturnField Table Update:"+ query);
1170

    
1171
               pstmt = dbconn.prepareStatement(query);
1172
               pstmt.setInt(1, count);
1173
               pstmt.setInt(2, id);
1174
               dbconn.increaseUsageCount(1);
1175
               pstmt.execute();
1176
               pstmt.close();
1177

    
1178
           } else {
1179
               rs.close();
1180
               pstmt.close();
1181

    
1182
               // insert a new record
1183
               query = "INSERT INTO xml_returnfield (returnfield_string, usage_count)"
1184
                   + "VALUES (?, '1')";
1185
               logMetacat.info("DBQuery.getXmlReturnfieldsTableId - ReturnField Table Insert:"+ query);
1186
               pstmt = dbconn.prepareStatement(query);
1187
               pstmt.setString(1, returnfield);
1188
               dbconn.increaseUsageCount(1);
1189
               pstmt.execute();
1190
               pstmt.close();
1191

    
1192
               // get the id of the new record
1193
               query = "SELECT returnfield_id FROM xml_returnfield "
1194
                   + "WHERE returnfield_string LIKE ?";
1195
               logMetacat.info("DBQuery.getXmlReturnfieldsTableId - ReturnField query after Insert:" + query);
1196
               pstmt = dbconn.prepareStatement(query);
1197
               pstmt.setString(1, returnfield);
1198

    
1199
               dbconn.increaseUsageCount(1);
1200
               pstmt.execute();
1201
               rs = pstmt.getResultSet();
1202
               if(rs.next()){
1203
                   id = rs.getInt(1);
1204
               } else {
1205
                   id = -1;
1206
               }
1207
               rs.close();
1208
               pstmt.close();
1209
           }
1210

    
1211
       } catch (Exception e){
1212
           logMetacat.error("DBQuery.getXmlReturnfieldsTableId - Error getting id from xml_returnfield in "
1213
                                     + "DBQuery.getXmlReturnfieldsTableId: "
1214
                                     + e.getMessage());
1215
           id = -1;
1216
       }
1217

    
1218
       returnfield_id = id;
1219
       return count;
1220
   }
1221

    
1222

    
1223
    /*
1224
     * A method to add return field to return doclist hash table
1225
     */
1226
    private ResultDocumentSet addReturnfield(ResultDocumentSet docListResult,
1227
                                      QuerySpecification qspec,
1228
                                      String user, String[]groups,
1229
                                      DBConnection dbconn, boolean useXMLIndex,
1230
                                      String qformat)
1231
                                      throws Exception
1232
    {
1233
      PreparedStatement pstmt = null;
1234
      ResultSet rs = null;
1235
      String docid = null;
1236
      String fieldname = null;
1237
      String fieldtype = null;
1238
      String fielddata = null;
1239
      String relation = null;
1240
      // keep track of parameter values
1241
      List<Object> parameterValues = new ArrayList<Object>();
1242

    
1243
      if (qspec.containsExtendedSQL())
1244
      {
1245
        qspec.setUserName(user);
1246
        qspec.setGroup(groups);
1247
        Vector extendedFields = new Vector(qspec.getReturnFieldList());
1248
        Vector results = new Vector();
1249
        Iterator keylist = docListResult.getDocids();
1250
        StringBuffer doclist = new StringBuffer();
1251
        List<Object> doclistValues = new ArrayList<Object>();
1252
        Vector parentidList = new Vector();
1253
        Hashtable returnFieldValue = new Hashtable();
1254
        while (keylist.hasNext())
1255
        {
1256
          String key = (String)keylist.next();
1257
          doclist.append("?,");
1258
          doclistValues.add(key);
1259
        }
1260
        if (doclist.length() > 0)
1261
        {
1262
          Hashtable controlPairs = new Hashtable();
1263
          doclist.deleteCharAt(doclist.length() - 1); //remove the last comma
1264
          boolean tableHasRows = false;
1265
        
1266

    
1267
          
1268
           String extendedQuery =
1269
               qspec.printExtendedSQL(doclist.toString(), useXMLIndex, parameterValues);
1270
           // add them after, since the doclist clause is at the end of the generated queries
1271
           parameterValues.addAll(doclistValues);
1272
           logMetacat.info("DBQuery.addReturnfield - Extended query: " + extendedQuery);
1273

    
1274
           if(extendedQuery != null){
1275
//        	   long extendedQueryStart = System.currentTimeMillis();
1276
               pstmt = dbconn.prepareStatement(extendedQuery);
1277
               // set the parameter values
1278
               pstmt = DBQuery.setPreparedStatementValues(parameterValues, pstmt);
1279
               //increase dbconnection usage count
1280
               dbconn.increaseUsageCount(1);
1281
               pstmt.execute();
1282
               rs = pstmt.getResultSet();
1283
               tableHasRows = rs.next();
1284
               while (tableHasRows) {
1285
                   ReturnFieldValue returnValue = new ReturnFieldValue();
1286
                   docid = rs.getString(1).trim();
1287
                   fieldname = rs.getString(2);
1288
                   
1289
                   if(qformat.toLowerCase().trim().equals("xml"))
1290
                   {
1291
                       byte[] b = rs.getBytes(3);
1292
                       fielddata = new String(b, 0, b.length, MetaCatServlet.DEFAULT_ENCODING);
1293
                   }
1294
                   else
1295
                   {
1296
                       fielddata = rs.getString(3);
1297
                   }
1298
                   
1299
                   //System.out.println("raw fielddata: " + fielddata);
1300
                   fielddata = MetacatUtil.normalize(fielddata);
1301
                   //System.out.println("normalized fielddata: " + fielddata);
1302
                   String parentId = rs.getString(4);
1303
                   fieldtype = rs.getString(5);
1304
                   StringBuffer value = new StringBuffer();
1305

    
1306
                   //handle case when usexmlindex is true differently
1307
                   //at one point merging the nodedata (for large text elements) was 
1308
                   //deemed unnecessary - but now it is needed.  but not for attribute nodes
1309
                   if (useXMLIndex || !containsKey(parentidList, parentId)) {
1310
                	   //merge node data only for non-ATTRIBUTEs
1311
                	   if (fieldtype != null && !fieldtype.equals("ATTRIBUTE")) {
1312
	                	   //try merging the data
1313
	                	   ReturnFieldValue existingRFV =
1314
	                		   getArrayValue(parentidList, parentId);
1315
	                	   if (existingRFV != null && !existingRFV.getFieldType().equals("ATTRIBUTE")) {
1316
	                		   fielddata = existingRFV.getFieldValue() + fielddata;
1317
	                	   }
1318
                	   }
1319
                	   //System.out.println("fieldname: " + fieldname + " fielddata: " + fielddata);
1320

    
1321
                       value.append("<param name=\"");
1322
                       value.append(fieldname);
1323
                       value.append("\">");
1324
                       value.append(fielddata);
1325
                       value.append("</param>");
1326
                       //set returnvalue
1327
                       returnValue.setDocid(docid);
1328
                       returnValue.setFieldValue(fielddata);
1329
                       returnValue.setFieldType(fieldtype);
1330
                       returnValue.setXMLFieldValue(value.toString());
1331
                       // Store it in hastable
1332
                       putInArray(parentidList, parentId, returnValue);
1333
                   }
1334
                   else {
1335
                       
1336
                       // need to merge nodedata if they have same parent id and
1337
                       // node type is text
1338
                       fielddata = (String) ( (ReturnFieldValue)
1339
                                             getArrayValue(
1340
                           parentidList, parentId)).getFieldValue()
1341
                           + fielddata;
1342
                       //System.out.println("fieldname: " + fieldname + " fielddata: " + fielddata);
1343
                       value.append("<param name=\"");
1344
                       value.append(fieldname);
1345
                       value.append("\">");
1346
                       value.append(fielddata);
1347
                       value.append("</param>");
1348
                       returnValue.setDocid(docid);
1349
                       returnValue.setFieldValue(fielddata);
1350
                       returnValue.setFieldType(fieldtype);
1351
                       returnValue.setXMLFieldValue(value.toString());
1352
                       // remove the old return value from paretnidList
1353
                       parentidList.remove(parentId);
1354
                       // store the new return value in parentidlit
1355
                       putInArray(parentidList, parentId, returnValue);
1356
                   }
1357
                   tableHasRows = rs.next();
1358
               } //while
1359
               rs.close();
1360
               pstmt.close();
1361

    
1362
               // put the merger node data info into doclistReult
1363
               Enumeration xmlFieldValue = (getElements(parentidList)).
1364
                   elements();
1365
               while (xmlFieldValue.hasMoreElements()) {
1366
                   ReturnFieldValue object =
1367
                       (ReturnFieldValue) xmlFieldValue.nextElement();
1368
                   docid = object.getDocid();
1369
                   if (docListResult.containsDocid(docid)) {
1370
                       String removedelement = (String) docListResult.
1371
                           remove(docid);
1372
                       docListResult.
1373
                           addResultDocument(new ResultDocument(docid,
1374
                               removedelement + object.getXMLFieldValue()));
1375
                   }
1376
                   else {
1377
                       docListResult.addResultDocument(
1378
                         new ResultDocument(docid, object.getXMLFieldValue()));
1379
                   }
1380
               } //while
1381
//               double docListResultEnd = System.currentTimeMillis() / 1000;
1382
//               logMetacat.warn(
1383
//                   "Time to prepare ResultDocumentSet after"
1384
//                   + " execute extended query: "
1385
//                   + (docListResultEnd - extendedQueryEnd));
1386
           }
1387
       }//if doclist lenght is great than zero
1388
     }//if has extended query
1389

    
1390
      return docListResult;
1391
    }//addReturnfield
1392

    
1393
  
1394
  /**
1395
   * removes the <?xml version="1.0"?> tag from the beginning.  This takes a
1396
   * string as a param instead of a hashtable.
1397
   *
1398
   * @param xmlquery a string representing a query.
1399
   */
1400
   private  String transformQuery(String xmlquery)
1401
   {
1402
     xmlquery = xmlquery.trim();
1403
     int index = xmlquery.indexOf("?>");
1404
     if (index != -1)
1405
     {
1406
       return xmlquery.substring(index + 2, xmlquery.length());
1407
     }
1408
     else
1409
     {
1410
       return xmlquery;
1411
     }
1412
   }
1413
   
1414
   /*
1415
    * Method to store query string and result xml string into query result
1416
    * cache. If the size alreay reache the limitation, the cache will be
1417
    * cleared first, then store them.
1418
    */
1419
   private void storeQueryResultIntoCache(String query, String resultXML)
1420
   {
1421
	   synchronized (queryResultCache)
1422
	   {
1423
		   if (queryResultCache.size() >= QUERYRESULTCACHESIZE)
1424
		   {
1425
			   queryResultCache.clear();
1426
		   }
1427
		   queryResultCache.put(query, resultXML);
1428
		   
1429
	   }
1430
   }
1431
   
1432
   /*
1433
    * Method to get result xml string from query result cache. 
1434
    * Note: the returned string can be null.
1435
    */
1436
   private String getResultXMLFromCache(String query)
1437
   {
1438
	   String resultSet = null;
1439
	   synchronized (queryResultCache)
1440
	   {
1441
          try
1442
          {
1443
        	 logMetacat.info("DBQuery.getResultXMLFromCache - Get query from cache");
1444
		     resultSet = (String)queryResultCache.get(query);
1445
		   
1446
          }
1447
          catch (Exception e)
1448
          {
1449
        	  resultSet = null;
1450
          }
1451
		   
1452
	   }
1453
	   return resultSet;
1454
   }
1455
   
1456
   /**
1457
    * Method to clear the query result cache.
1458
    */
1459
   public static void clearQueryResultCache()
1460
   {
1461
	   synchronized (queryResultCache)
1462
	   {
1463
		   queryResultCache.clear();
1464
	   }
1465
   }
1466
   
1467
   /**
1468
    * Set the parameter values in the prepared statement using instrospection
1469
    * of the given value objects
1470
    * @param parameterValues
1471
    * @param pstmt
1472
    * @return
1473
    * @throws SQLException
1474
    */
1475
   public static PreparedStatement setPreparedStatementValues(List<Object> parameterValues, PreparedStatement pstmt) throws SQLException {
1476
	   // set all the values we have collected 
1477
      int parameterIndex = 1;
1478
      for (Object parameterValue: parameterValues) {
1479
    	  if (parameterValue instanceof String) {
1480
    		  pstmt.setString(parameterIndex, (String) parameterValue);
1481
    	  }
1482
    	  else if (parameterValue instanceof Integer) {
1483
    		  pstmt.setInt(parameterIndex, (Integer) parameterValue);
1484
    	  }
1485
    	  else if (parameterValue instanceof Float) {
1486
    		  pstmt.setFloat(parameterIndex, (Float) parameterValue);
1487
    	  }
1488
    	  else if (parameterValue instanceof Double) {
1489
    		  pstmt.setDouble(parameterIndex, (Double) parameterValue);
1490
    	  }
1491
    	  else if (parameterValue instanceof Date) {
1492
    		  pstmt.setTimestamp(parameterIndex, new Timestamp(((Date) parameterValue).getTime()));
1493
    	  }
1494
    	  else {
1495
    		  pstmt.setObject(parameterIndex, parameterValue);
1496
    	  }
1497
    	  parameterIndex++;
1498
      }
1499
      return pstmt;
1500
   }
1501

    
1502

    
1503
    /*
1504
     * A method to search if Vector contains a particular key string
1505
     */
1506
    private boolean containsKey(Vector parentidList, String parentId)
1507
    {
1508

    
1509
        Vector tempVector = null;
1510

    
1511
        for (int count = 0; count < parentidList.size(); count++) {
1512
            tempVector = (Vector) parentidList.get(count);
1513
            if (parentId.compareTo((String) tempVector.get(0)) == 0) { return true; }
1514
        }
1515
        return false;
1516
    }
1517
    
1518
    /*
1519
     * A method to put key and value in Vector
1520
     */
1521
    private void putInArray(Vector parentidList, String key,
1522
            ReturnFieldValue value)
1523
    {
1524

    
1525
        Vector tempVector = null;
1526
        //only filter if the field type is NOT an attribute (say, for text)
1527
        String fieldType = value.getFieldType();
1528
        if (fieldType != null && !fieldType.equals("ATTRIBUTE")) {
1529
        
1530
	        for (int count = 0; count < parentidList.size(); count++) {
1531
	            tempVector = (Vector) parentidList.get(count);
1532
	
1533
	            if (key.compareTo((String) tempVector.get(0)) == 0) {
1534
	                tempVector.remove(1);
1535
	                tempVector.add(1, value);
1536
	                return;
1537
	            }
1538
	        }
1539
        }
1540

    
1541
        tempVector = new Vector();
1542
        tempVector.add(0, key);
1543
        tempVector.add(1, value);
1544
        parentidList.add(tempVector);
1545
        return;
1546
    }
1547

    
1548
    /*
1549
     * A method to get value in Vector given a key
1550
     */
1551
    private ReturnFieldValue getArrayValue(Vector parentidList, String key)
1552
    {
1553

    
1554
        Vector tempVector = null;
1555

    
1556
        for (int count = 0; count < parentidList.size(); count++) {
1557
            tempVector = (Vector) parentidList.get(count);
1558

    
1559
            if (key.compareTo((String) tempVector.get(0)) == 0) { return (ReturnFieldValue) tempVector
1560
                    .get(1); }
1561
        }
1562
        return null;
1563
    }
1564

    
1565
    /*
1566
     * A method to get enumeration of all values in Vector
1567
     */
1568
    private Vector getElements(Vector parentidList)
1569
    {
1570
        Vector enumVector = new Vector();
1571
        Vector tempVector = null;
1572

    
1573
        for (int count = 0; count < parentidList.size(); count++) {
1574
            tempVector = (Vector) parentidList.get(count);
1575

    
1576
            enumVector.add(tempVector.get(1));
1577
        }
1578
        return enumVector;
1579
    }
1580

    
1581
  
1582

    
1583
    /*
1584
     * A method to create a query to get owner's docid list
1585
     */
1586
    private String getOwnerQuery(String owner, List<Object> parameterValues)
1587
    {
1588
        if (owner != null) {
1589
            owner = owner.toLowerCase();
1590
        }
1591
        StringBuffer self = new StringBuffer();
1592

    
1593
        self.append("SELECT docid,docname,doctype,");
1594
        self.append("date_created, date_updated, rev ");
1595
        self.append("FROM xml_documents WHERE docid IN (");
1596
        self.append("(");
1597
        self.append("SELECT DISTINCT docid FROM xml_nodes WHERE \n");
1598
        self.append("nodedata LIKE '%%%' ");
1599
        self.append(") \n");
1600
        self.append(") ");
1601
        self.append(" AND (");
1602
        self.append(" lower(user_owner) = ?");
1603
        self.append(") ");
1604
        parameterValues.add(owner);
1605
        return self.toString();
1606
    }
1607

    
1608
    /**
1609
     * format a structured query as an XML document that conforms to the
1610
     * pathquery.dtd and is appropriate for submission to the DBQuery
1611
     * structured query engine
1612
     *
1613
     * @param params The list of parameters that should be included in the
1614
     *            query
1615
     */
1616
    public static String createSQuery(Hashtable params) throws PropertyNotFoundException
1617
    {
1618
        StringBuffer query = new StringBuffer();
1619
        Enumeration elements;
1620
        Enumeration keys;
1621
        String filterDoctype = null;
1622
        String casesensitive = null;
1623
        String searchmode = null;
1624
        Object nextkey;
1625
        Object nextelement;
1626
        //add the xml headers
1627
        query.append("<?xml version=\"1.0\"?>\n");
1628
        query.append("<pathquery version=\"1.2\">\n");
1629

    
1630

    
1631

    
1632
        if (params.containsKey("meta_file_id")) {
1633
            query.append("<meta_file_id>");
1634
            query.append(((String[]) params.get("meta_file_id"))[0]);
1635
            query.append("</meta_file_id>");
1636
        }
1637

    
1638
        if (params.containsKey("returndoctype")) {
1639
            String[] returnDoctypes = ((String[]) params.get("returndoctype"));
1640
            for (int i = 0; i < returnDoctypes.length; i++) {
1641
                String doctype = (String) returnDoctypes[i];
1642

    
1643
                if (!doctype.equals("any") && !doctype.equals("ANY")
1644
                        && !doctype.equals("")) {
1645
                    query.append("<returndoctype>").append(doctype);
1646
                    query.append("</returndoctype>");
1647
                }
1648
            }
1649
        }
1650

    
1651
        if (params.containsKey("filterdoctype")) {
1652
            String[] filterDoctypes = ((String[]) params.get("filterdoctype"));
1653
            for (int i = 0; i < filterDoctypes.length; i++) {
1654
                query.append("<filterdoctype>").append(filterDoctypes[i]);
1655
                query.append("</filterdoctype>");
1656
            }
1657
        }
1658

    
1659
        if (params.containsKey("returnfield")) {
1660
            String[] returnfield = ((String[]) params.get("returnfield"));
1661
            for (int i = 0; i < returnfield.length; i++) {
1662
                query.append("<returnfield>").append(returnfield[i]);
1663
                query.append("</returnfield>");
1664
            }
1665
        }
1666

    
1667
        if (params.containsKey("owner")) {
1668
            String[] owner = ((String[]) params.get("owner"));
1669
            for (int i = 0; i < owner.length; i++) {
1670
                query.append("<owner>").append(owner[i]);
1671
                query.append("</owner>");
1672
            }
1673
        }
1674

    
1675
        if (params.containsKey("site")) {
1676
            String[] site = ((String[]) params.get("site"));
1677
            for (int i = 0; i < site.length; i++) {
1678
                query.append("<site>").append(site[i]);
1679
                query.append("</site>");
1680
            }
1681
        }
1682

    
1683
        //allows the dynamic switching of boolean operators
1684
        if (params.containsKey("operator")) {
1685
            query.append("<querygroup operator=\""
1686
                    + ((String[]) params.get("operator"))[0] + "\">");
1687
        } else { //the default operator is UNION
1688
            query.append("<querygroup operator=\"UNION\">");
1689
        }
1690

    
1691
        if (params.containsKey("casesensitive")) {
1692
            casesensitive = ((String[]) params.get("casesensitive"))[0];
1693
        } else {
1694
            casesensitive = "false";
1695
        }
1696

    
1697
        if (params.containsKey("searchmode")) {
1698
            searchmode = ((String[]) params.get("searchmode"))[0];
1699
        } else {
1700
            searchmode = "contains";
1701
        }
1702

    
1703
        //anyfield is a special case because it does a
1704
        //free text search. It does not have a <pathexpr>
1705
        //tag. This allows for a free text search within the structured
1706
        //query. This is useful if the INTERSECT operator is used.
1707
        if (params.containsKey("anyfield")) {
1708
            String[] anyfield = ((String[]) params.get("anyfield"));
1709
            //allow for more than one value for anyfield
1710
            for (int i = 0; i < anyfield.length; i++) {
1711
                if (anyfield[i] != null && !anyfield[i].equals("")) {
1712
                    query.append("<queryterm casesensitive=\"" + casesensitive
1713
                            + "\" " + "searchmode=\"" + searchmode
1714
                            + "\"><value>" + anyfield[i]
1715
                            + "</value></queryterm>");
1716
                }
1717
            }
1718
        }
1719

    
1720
        //this while loop finds the rest of the parameters
1721
        //and attempts to query for the field specified
1722
        //by the parameter.
1723
        elements = params.elements();
1724
        keys = params.keys();
1725
        while (keys.hasMoreElements() && elements.hasMoreElements()) {
1726
            nextkey = keys.nextElement();
1727
            nextelement = elements.nextElement();
1728

    
1729
            //make sure we aren't querying for any of these
1730
            //parameters since the are already in the query
1731
            //in one form or another.
1732
            Vector ignoredParams = new Vector();
1733
            ignoredParams.add("returndoctype");
1734
            ignoredParams.add("filterdoctype");
1735
            ignoredParams.add("action");
1736
            ignoredParams.add("qformat");
1737
            ignoredParams.add("anyfield");
1738
            ignoredParams.add("returnfield");
1739
            ignoredParams.add("owner");
1740
            ignoredParams.add("site");
1741
            ignoredParams.add("operator");
1742
            ignoredParams.add("sessionid");
1743
            ignoredParams.add("pagesize");
1744
            ignoredParams.add("pagestart");
1745
            ignoredParams.add("searchmode");
1746

    
1747
            // Also ignore parameters listed in the properties file
1748
            // so that they can be passed through to stylesheets
1749
            String paramsToIgnore = PropertyService
1750
                    .getProperty("database.queryignoredparams");
1751
            StringTokenizer st = new StringTokenizer(paramsToIgnore, ",");
1752
            while (st.hasMoreTokens()) {
1753
                ignoredParams.add(st.nextToken());
1754
            }
1755
            if (!ignoredParams.contains(nextkey.toString())) {
1756
                //allow for more than value per field name
1757
                for (int i = 0; i < ((String[]) nextelement).length; i++) {
1758
                    if (!((String[]) nextelement)[i].equals("")) {
1759
                        query.append("<queryterm casesensitive=\""
1760
                                + casesensitive + "\" " + "searchmode=\""
1761
                                + searchmode + "\">" + "<value>" +
1762
                                //add the query value
1763
                                ((String[]) nextelement)[i]
1764
                                + "</value><pathexpr>" +
1765
                                //add the path to query by
1766
                                nextkey.toString() + "</pathexpr></queryterm>");
1767
                    }
1768
                }
1769
            }
1770
        }
1771
        query.append("</querygroup></pathquery>");
1772
        //append on the end of the xml and return the result as a string
1773
        return query.toString();
1774
    }
1775

    
1776
    /**
1777
     * format a simple free-text value query as an XML document that conforms
1778
     * to the pathquery.dtd and is appropriate for submission to the DBQuery
1779
     * structured query engine
1780
     *
1781
     * @param value the text string to search for in the xml catalog
1782
     * @param doctype the type of documents to include in the result set -- use
1783
     *            "any" or "ANY" for unfiltered result sets
1784
     */
1785
    public static String createQuery(String value, String doctype)
1786
    {
1787
        StringBuffer xmlquery = new StringBuffer();
1788
        xmlquery.append("<?xml version=\"1.0\"?>\n");
1789
        xmlquery.append("<pathquery version=\"1.0\">");
1790

    
1791
        if (!doctype.equals("any") && !doctype.equals("ANY")) {
1792
            xmlquery.append("<returndoctype>");
1793
            xmlquery.append(doctype).append("</returndoctype>");
1794
        }
1795

    
1796
        xmlquery.append("<querygroup operator=\"UNION\">");
1797
        //chad added - 8/14
1798
        //the if statement allows a query to gracefully handle a null
1799
        //query. Without this if a nullpointerException is thrown.
1800
        if (!value.equals("")) {
1801
            xmlquery.append("<queryterm casesensitive=\"false\" ");
1802
            xmlquery.append("searchmode=\"contains\">");
1803
            xmlquery.append("<value>").append(value).append("</value>");
1804
            xmlquery.append("</queryterm>");
1805
        }
1806
        xmlquery.append("</querygroup>");
1807
        xmlquery.append("</pathquery>");
1808

    
1809
        return (xmlquery.toString());
1810
    }
1811

    
1812
    /**
1813
     * format a simple free-text value query as an XML document that conforms
1814
     * to the pathquery.dtd and is appropriate for submission to the DBQuery
1815
     * structured query engine
1816
     *
1817
     * @param value the text string to search for in the xml catalog
1818
     */
1819
    public static String createQuery(String value)
1820
    {
1821
        return createQuery(value, "any");
1822
    }
1823

    
1824
    /**
1825
     * Check for "READ" permission on @docid for @user and/or @group from DB
1826
     * connection
1827
     */
1828
    private boolean hasPermission(String user, String[] groups, String docid)
1829
            throws SQLException, Exception
1830
    {
1831
        // Check for READ permission on @docid for @user and/or @groups
1832
        PermissionController controller = new PermissionController(docid);
1833
        return controller.hasPermission(user, groups,
1834
                AccessControlInterface.READSTRING);
1835
    }
1836

    
1837
    /**
1838
     * Get all docIds list for a data packadge
1839
     *
1840
     * @param dataPackageDocid, the string in docId field of xml_relation table
1841
     */
1842
    private Vector getCurrentDocidListForDataPackage(String dataPackageDocid)
1843
    {
1844
        DBConnection dbConn = null;
1845
        int serialNumber = -1;
1846
        Vector docIdList = new Vector();//return value
1847
        PreparedStatement pStmt = null;
1848
        ResultSet rs = null;
1849
        String docIdInSubjectField = null;
1850
        String docIdInObjectField = null;
1851

    
1852
        // Check the parameter
1853
        if (dataPackageDocid == null || dataPackageDocid.equals("")) { return docIdList; }//if
1854

    
1855
        //the query stirng
1856
        String query = "SELECT subject, object from xml_relation where docId = ?";
1857
        try {
1858
            dbConn = DBConnectionPool
1859
                    .getDBConnection("DBQuery.getCurrentDocidListForDataPackage");
1860
            serialNumber = dbConn.getCheckOutSerialNumber();
1861
            pStmt = dbConn.prepareStatement(query);
1862
            //bind the value to query
1863
            pStmt.setString(1, dataPackageDocid);
1864

    
1865
            //excute the query
1866
            pStmt.execute();
1867
            //get the result set
1868
            rs = pStmt.getResultSet();
1869
            //process the result
1870
            while (rs.next()) {
1871
                //In order to get the whole docIds in a data packadge,
1872
                //we need to put the docIds of subject and object field in
1873
                // xml_relation
1874
                //into the return vector
1875
                docIdInSubjectField = rs.getString(1);//the result docId in
1876
                                                      // subject field
1877
                docIdInObjectField = rs.getString(2);//the result docId in
1878
                                                     // object field
1879

    
1880
                //don't put the duplicate docId into the vector
1881
                if (!docIdList.contains(docIdInSubjectField)) {
1882
                    docIdList.add(docIdInSubjectField);
1883
                }
1884

    
1885
                //don't put the duplicate docId into the vector
1886
                if (!docIdList.contains(docIdInObjectField)) {
1887
                    docIdList.add(docIdInObjectField);
1888
                }
1889
            }//while
1890
            //close the pStmt
1891
            pStmt.close();
1892
        }//try
1893
        catch (SQLException e) {
1894
            logMetacat.error("DBQuery.getCurrentDocidListForDataPackage - Error in getDocidListForDataPackage: "
1895
                    + e.getMessage());
1896
        }//catch
1897
        finally {
1898
            try {
1899
                pStmt.close();
1900
            }//try
1901
            catch (SQLException ee) {
1902
                logMetacat.error("DBQuery.getCurrentDocidListForDataPackage - SQL Error: "
1903
                                + ee.getMessage());
1904
            }//catch
1905
            finally {
1906
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1907
            }//fianlly
1908
        }//finally
1909
        return docIdList;
1910
    }//getCurrentDocidListForDataPackadge()
1911

    
1912
    /**
1913
     * Get all docIds list for a data packadge
1914
     *
1915
     * @param dataPackageDocid, the string in docId field of xml_relation table
1916
     */
1917
    private Vector getOldVersionDocidListForDataPackage(String dataPackageDocidWithRev)
1918
    {
1919

    
1920
        Vector docIdList = new Vector();//return value
1921
        Vector tripleList = null;
1922
        String xml = null;
1923

    
1924
        // Check the parameter
1925
        if (dataPackageDocidWithRev == null || dataPackageDocidWithRev.equals("")) { return docIdList; }//if
1926

    
1927
        try {
1928
            //initial a documentImpl object
1929
            DocumentImpl packageDocument = new DocumentImpl(dataPackageDocidWithRev);
1930
            //transfer to documentImpl object to string
1931
            xml = packageDocument.toString();
1932

    
1933
            //create a tripcollection object
1934
            TripleCollection tripleForPackage = new TripleCollection(
1935
                    new StringReader(xml));
1936
            //get the vetor of triples
1937
            tripleList = tripleForPackage.getCollection();
1938

    
1939
            for (int i = 0; i < tripleList.size(); i++) {
1940
                //put subject docid into docIdlist without duplicate
1941
                if (!docIdList.contains(((Triple) tripleList.elementAt(i))
1942
                        .getSubject())) {
1943
                    //put subject docid into docIdlist
1944
                    docIdList.add(((Triple) tripleList.get(i)).getSubject());
1945
                }
1946
                //put object docid into docIdlist without duplicate
1947
                if (!docIdList.contains(((Triple) tripleList.elementAt(i))
1948
                        .getObject())) {
1949
                    docIdList.add(((Triple) (tripleList.get(i))).getObject());
1950
                }
1951
            }//for
1952
        }//try
1953
        catch (Exception e) {
1954
            logMetacat.error("DBQuery.getCurrentDocidListForDataPackage - General error: "
1955
                    + e.getMessage());
1956
        }//catch
1957

    
1958
        // return result
1959
        return docIdList;
1960
    }//getDocidListForPackageInXMLRevisions()
1961

    
1962
    /**
1963
     * Check if the docId is a data packadge id. If the id is a data packadage
1964
     * id, it should be store in the docId fields in xml_relation table. So we
1965
     * can use a query to get the entries which the docId equals the given
1966
     * value. If the result is null. The docId is not a packadge id. Otherwise,
1967
     * it is.
1968
     *
1969
     * @param docId, the id need to be checked
1970
     */
1971
    private boolean isDataPackageId(String docId)
1972
    {
1973
        boolean result = false;
1974
        PreparedStatement pStmt = null;
1975
        ResultSet rs = null;
1976
        String query = "SELECT docId from xml_relation where docId = ?";
1977
        DBConnection dbConn = null;
1978
        int serialNumber = -1;
1979
        try {
1980
            dbConn = DBConnectionPool
1981
                    .getDBConnection("DBQuery.isDataPackageId");
1982
            serialNumber = dbConn.getCheckOutSerialNumber();
1983
            pStmt = dbConn.prepareStatement(query);
1984
            //bind the value to query
1985
            pStmt.setString(1, docId);
1986
            //execute the query
1987
            pStmt.execute();
1988
            rs = pStmt.getResultSet();
1989
            //process the result
1990
            if (rs.next()) //There are some records for the id in docId fields
1991
            {
1992
                result = true;//It is a data packadge id
1993
            }
1994
            pStmt.close();
1995
        }//try
1996
        catch (SQLException e) {
1997
            logMetacat.error("DBQuery.isDataPackageId - SQL Error: "
1998
                    + e.getMessage());
1999
        } finally {
2000
            try {
2001
                pStmt.close();
2002
            }//try
2003
            catch (SQLException ee) {
2004
                logMetacat.error("DBQuery.isDataPackageId - SQL Error in isDataPackageId: "
2005
                        + ee.getMessage());
2006
            }//catch
2007
            finally {
2008
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
2009
            }//finally
2010
        }//finally
2011
        return result;
2012
    }//isDataPackageId()
2013

    
2014
    public String getOperator() {
2015
		return operator;
2016
	}
2017

    
2018
    /**
2019
     * Specifies if and how docid overrides should be included in the general query
2020
     * @param operator null, UNION, or INTERSECT (see QueryGroup)
2021
     */
2022
	public void setOperator(String operator) {
2023
		this.operator = operator;
2024
	}
2025

    
2026
	/**
2027
     * Check if the user has the permission to export data package
2028
     *
2029
     * @param conn, the connection
2030
     * @param docId, the id need to be checked
2031
     * @param user, the name of user
2032
     * @param groups, the user's group
2033
     */
2034
    private boolean hasPermissionToExportPackage(String docId, String user,
2035
            String[] groups) throws Exception
2036
    {
2037
        //DocumentImpl doc=new DocumentImpl(conn,docId);
2038
        return DocumentImpl.hasReadPermission(user, groups, docId);
2039
    }
2040

    
2041
    /**
2042
     * Get the current Rev for a docid in xml_documents table
2043
     *
2044
     * @param docId, the id need to get version numb If the return value is -5,
2045
     *            means no value in rev field for this docid
2046
     */
2047
    private int getCurrentRevFromXMLDoumentsTable(String docId)
2048
            throws SQLException
2049
    {
2050
        int rev = -5;
2051
        PreparedStatement pStmt = null;
2052
        ResultSet rs = null;
2053
        String query = "SELECT rev from xml_documents where docId = ?";
2054
        DBConnection dbConn = null;
2055
        int serialNumber = -1;
2056
        try {
2057
            dbConn = DBConnectionPool
2058
                    .getDBConnection("DBQuery.getCurrentRevFromXMLDocumentsTable");
2059
            serialNumber = dbConn.getCheckOutSerialNumber();
2060
            pStmt = dbConn.prepareStatement(query);
2061
            //bind the value to query
2062
            pStmt.setString(1, docId);
2063
            //execute the query
2064
            pStmt.execute();
2065
            rs = pStmt.getResultSet();
2066
            //process the result
2067
            if (rs.next()) //There are some records for rev
2068
            {
2069
                rev = rs.getInt(1);
2070
                ;//It is the version for given docid
2071
            } else {
2072
                rev = -5;
2073
            }
2074

    
2075
        }//try
2076
        catch (SQLException e) {
2077
            logMetacat.error("DBQuery.getCurrentRevFromXMLDoumentsTable - SQL Error: "
2078
                            + e.getMessage());
2079
            throw e;
2080
        }//catch
2081
        finally {
2082
            try {
2083
                pStmt.close();
2084
            }//try
2085
            catch (SQLException ee) {
2086
                logMetacat.error(
2087
                        "DBQuery.getCurrentRevFromXMLDoumentsTable - SQL Error: "
2088
                                + ee.getMessage());
2089
            }//catch
2090
            finally {
2091
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
2092
            }//finally
2093
        }//finally
2094
        return rev;
2095
    }//getCurrentRevFromXMLDoumentsTable
2096

    
2097
    /**
2098
     * put a doc into a zip output stream
2099
     *
2100
     * @param docImpl, docmentImpl object which will be sent to zip output
2101
     *            stream
2102
     * @param zipOut, zip output stream which the docImpl will be put
2103
     * @param packageZipEntry, the zip entry name for whole package
2104
     */
2105
    private void addDocToZipOutputStream(DocumentImpl docImpl,
2106
            ZipOutputStream zipOut, String packageZipEntry)
2107
            throws ClassNotFoundException, IOException, SQLException,
2108
            McdbException, Exception
2109
    {
2110
        byte[] byteString = null;
2111
        ZipEntry zEntry = null;
2112

    
2113
        byteString = docImpl.getBytes();
2114
        //use docId as the zip entry's name
2115
        zEntry = new ZipEntry(packageZipEntry + "/metadata/"
2116
                + docImpl.getDocID());
2117
        zEntry.setSize(byteString.length);
2118
        zipOut.putNextEntry(zEntry);
2119
        zipOut.write(byteString, 0, byteString.length);
2120
        zipOut.closeEntry();
2121

    
2122
    }//addDocToZipOutputStream()
2123

    
2124
    /**
2125
     * Transfer a docid vetor to a documentImpl vector. The documentImpl vetor
2126
     * only inlcudes current version. If a DocumentImple object couldn't find
2127
     * for a docid, then the String of this docid was added to vetor rather
2128
     * than DocumentImple object.
2129
     *
2130
     * @param docIdList, a vetor hold a docid list for a data package. In
2131
     *            docid, there is not version number in it.
2132
     */
2133

    
2134
    private Vector getCurrentAllDocumentImpl(Vector docIdList)
2135
            throws McdbException, Exception
2136
    {
2137
        //Connection dbConn=null;
2138
        Vector documentImplList = new Vector();
2139
        int rev = 0;
2140

    
2141
        // Check the parameter
2142
        if (docIdList.isEmpty()) { return documentImplList; }//if
2143

    
2144
        //for every docid in vector
2145
        for (int i = 0; i < docIdList.size(); i++) {
2146
            try {
2147
                //get newest version for this docId
2148
                rev = getCurrentRevFromXMLDoumentsTable((String) docIdList
2149
                        .elementAt(i));
2150

    
2151
                // There is no record for this docId in xml_documents table
2152
                if (rev == -5) {
2153
                    // Rather than put DocumentImple object, put a String
2154
                    // Object(docid)
2155
                    // into the documentImplList
2156
                    documentImplList.add((String) docIdList.elementAt(i));
2157
                    // Skip other code
2158
                    continue;
2159
                }
2160

    
2161
                String docidPlusVersion = ((String) docIdList.elementAt(i))
2162
                        + PropertyService.getProperty("document.accNumSeparator") + rev;
2163

    
2164
                //create new documentImpl object
2165
                DocumentImpl documentImplObject = new DocumentImpl(
2166
                        docidPlusVersion);
2167
                //add them to vector
2168
                documentImplList.add(documentImplObject);
2169
            }//try
2170
            catch (Exception e) {
2171
                logMetacat.error("DBQuery.getCurrentAllDocumentImpl - General error: "
2172
                        + e.getMessage());
2173
                // continue the for loop
2174
                continue;
2175
            }
2176
        }//for
2177
        return documentImplList;
2178
    }
2179

    
2180
    /**
2181
     * Transfer a docid vetor to a documentImpl vector. If a DocumentImple
2182
     * object couldn't find for a docid, then the String of this docid was
2183
     * added to vetor rather than DocumentImple object.
2184
     *
2185
     * @param docIdList, a vetor hold a docid list for a data package. In
2186
     *            docid, t here is version number in it.
2187
     */
2188
    private Vector getOldVersionAllDocumentImpl(Vector docIdList)
2189
    {
2190
        //Connection dbConn=null;
2191
        Vector documentImplList = new Vector();
2192
        String siteCode = null;
2193
        String uniqueId = null;
2194
        int rev = 0;
2195

    
2196
        // Check the parameter
2197
        if (docIdList.isEmpty()) { return documentImplList; }//if
2198

    
2199
        //for every docid in vector
2200
        for (int i = 0; i < docIdList.size(); i++) {
2201

    
2202
            String docidPlusVersion = (String) (docIdList.elementAt(i));
2203

    
2204
            try {
2205
                //create new documentImpl object
2206
                DocumentImpl documentImplObject = new DocumentImpl(
2207
                        docidPlusVersion);
2208
                //add them to vector
2209
                documentImplList.add(documentImplObject);
2210
            }//try
2211
            catch (McdbDocNotFoundException notFoundE) {
2212
                logMetacat.error("DBQuery.getOldVersionAllDocument - Error finding doc " 
2213
                		+ docidPlusVersion + " : " + notFoundE.getMessage());
2214
                // Rather than add a DocumentImple object into vetor, a String
2215
                // object
2216
                // - the doicd was added to the vector
2217
                documentImplList.add(docidPlusVersion);
2218
                // Continue the for loop
2219
                continue;
2220
            }//catch
2221
            catch (Exception e) {
2222
                logMetacat.error(
2223
                        "DBQuery.getOldVersionAllDocument - General error: "
2224
                                + e.getMessage());
2225
                // Continue the for loop
2226
                continue;
2227
            }//catch
2228

    
2229
        }//for
2230
        return documentImplList;
2231
    }//getOldVersionAllDocumentImple
2232

    
2233
    /**
2234
     * put a data file into a zip output stream
2235
     *
2236
     * @param docImpl, docmentImpl object which will be sent to zip output
2237
     *            stream
2238
     * @param zipOut, the zip output stream which the docImpl will be put
2239
     * @param packageZipEntry, the zip entry name for whole package
2240
     */
2241
    private void addDataFileToZipOutputStream(DocumentImpl docImpl,
2242
            ZipOutputStream zipOut, String packageZipEntry)
2243
            throws ClassNotFoundException, IOException, SQLException,
2244
            McdbException, Exception
2245
    {
2246
        byte[] byteString = null;
2247
        ZipEntry zEntry = null;
2248
        // this is data file; add file to zip
2249
        String filePath = PropertyService.getProperty("application.datafilepath");
2250
        if (!filePath.endsWith("/")) {
2251
            filePath += "/";
2252
        }
2253
        String fileName = filePath + docImpl.getDocID();
2254
        zEntry = new ZipEntry(packageZipEntry + "/data/" + docImpl.getDocID());
2255
        zipOut.putNextEntry(zEntry);
2256
        FileInputStream fin = null;
2257
        try {
2258
            fin = new FileInputStream(fileName);
2259
            byte[] buf = new byte[4 * 1024]; // 4K buffer
2260
            int b = fin.read(buf);
2261
            while (b != -1) {
2262
                zipOut.write(buf, 0, b);
2263
                b = fin.read(buf);
2264
            }//while
2265
            zipOut.closeEntry();
2266
        }//try
2267
        catch (IOException ioe) {
2268
            logMetacat.error("DBQuery.addDataFileToZipOutputStream - I/O error: "
2269
                    + ioe.getMessage());
2270
        }//catch
2271
    }//addDataFileToZipOutputStream()
2272

    
2273
    /**
2274
     * create a html summary for data package and put it into zip output stream
2275
     *
2276
     * @param docImplList, the documentImpl ojbects in data package
2277
     * @param zipOut, the zip output stream which the html should be put
2278
     * @param packageZipEntry, the zip entry name for whole package
2279
     */
2280
    private void addHtmlSummaryToZipOutputStream(Vector docImplList,
2281
            ZipOutputStream zipOut, String packageZipEntry) throws Exception
2282
    {
2283
        StringBuffer htmlDoc = new StringBuffer();
2284
        ZipEntry zEntry = null;
2285
        byte[] byteString = null;
2286
        InputStream source;
2287
        DBTransform xmlToHtml;
2288

    
2289
        //create a DBTransform ojbect
2290
        xmlToHtml = new DBTransform();
2291
        //head of html
2292
        htmlDoc.append("<html><head></head><body>");
2293
        for (int i = 0; i < docImplList.size(); i++) {
2294
            // If this String object, this means it is missed data file
2295
            if ((((docImplList.elementAt(i)).getClass()).toString())
2296
                    .equals("class java.lang.String")) {
2297

    
2298
                htmlDoc.append("<a href=\"");
2299
                String dataFileid = (String) docImplList.elementAt(i);
2300
                htmlDoc.append("./data/").append(dataFileid).append("\">");
2301
                htmlDoc.append("Data File: ");
2302
                htmlDoc.append(dataFileid).append("</a><br>");
2303
                htmlDoc.append("<br><hr><br>");
2304

    
2305
            }//if
2306
            else if ((((DocumentImpl) docImplList.elementAt(i)).getDoctype())
2307
                    .compareTo("BIN") != 0) { //this is an xml file so we can
2308
                                              // transform it.
2309
                //transform each file individually then concatenate all of the
2310
                //transformations together.
2311

    
2312
                //for metadata xml title
2313
                htmlDoc.append("<h2>");
2314
                htmlDoc.append(((DocumentImpl) docImplList.elementAt(i))
2315
                        .getDocID());
2316
                //htmlDoc.append(".");
2317
                //htmlDoc.append(((DocumentImpl)docImplList.elementAt(i)).getRev());
2318
                htmlDoc.append("</h2>");
2319
                //do the actual transform
2320
                StringWriter docString = new StringWriter();
2321
                xmlToHtml.transformXMLDocument(((DocumentImpl) docImplList
2322
                        .elementAt(i)).toString(), "-//NCEAS//eml-generic//EN",
2323
                        "-//W3C//HTML//EN", "html", docString, null, null);
2324
                htmlDoc.append(docString.toString());
2325
                htmlDoc.append("<br><br><hr><br><br>");
2326
            }//if
2327
            else { //this is a data file so we should link to it in the html
2328
                htmlDoc.append("<a href=\"");
2329
                String dataFileid = ((DocumentImpl) docImplList.elementAt(i))
2330
                        .getDocID();
2331
                htmlDoc.append("./data/").append(dataFileid).append("\">");
2332
                htmlDoc.append("Data File: ");
2333
                htmlDoc.append(dataFileid).append("</a><br>");
2334
                htmlDoc.append("<br><hr><br>");
2335
            }//else
2336
        }//for
2337
        htmlDoc.append("</body></html>");
2338
        // use standard encoding even though the different docs might have use different encodings,
2339
        // the String objects in java should be correct and able to be encoded as the same Metacat default
2340
        byteString = htmlDoc.toString().getBytes(MetaCatServlet.DEFAULT_ENCODING);
2341
        zEntry = new ZipEntry(packageZipEntry + "/metadata.html");
2342
        zEntry.setSize(byteString.length);
2343
        zipOut.putNextEntry(zEntry);
2344
        zipOut.write(byteString, 0, byteString.length);
2345
        zipOut.closeEntry();
2346
        //dbConn.close();
2347

    
2348
    }//addHtmlSummaryToZipOutputStream
2349

    
2350
    /**
2351
     * put a data packadge into a zip output stream
2352
     *
2353
     * @param docId, which the user want to put into zip output stream,it has version
2354
     * @param out, a servletoutput stream which the zip output stream will be
2355
     *            put
2356
     * @param user, the username of the user
2357
     * @param groups, the group of the user
2358
     */
2359
    public ZipOutputStream getZippedPackage(String docIdString,
2360
            ServletOutputStream out, String user, String[] groups,
2361
            String passWord) throws ClassNotFoundException, IOException,
2362
            SQLException, McdbException, NumberFormatException, Exception
2363
    {
2364
        ZipOutputStream zOut = null;
2365
        String elementDocid = null;
2366
        DocumentImpl docImpls = null;
2367
        //Connection dbConn = null;
2368
        Vector docIdList = new Vector();
2369
        Vector documentImplList = new Vector();
2370
        Vector htmlDocumentImplList = new Vector();
2371
        String packageId = null;
2372
        String rootName = "package";//the package zip entry name
2373

    
2374
        String docId = null;
2375
        int version = -5;
2376
        // Docid without revision
2377
        docId = DocumentUtil.getDocIdFromString(docIdString);
2378
        // revision number
2379
        version = DocumentUtil.getVersionFromString(docIdString);
2380

    
2381
        //check if the reqused docId is a data package id
2382
        if (!isDataPackageId(docId)) {
2383

    
2384
            /*
2385
             * Exception e = new Exception("The request the doc id "
2386
             * +docIdString+ " is not a data package id");
2387
             */
2388

    
2389
            //CB 1/6/03: if the requested docid is not a datapackage, we just
2390
            // zip
2391
            //up the single document and return the zip file.
2392
            if (!hasPermissionToExportPackage(docId, user, groups)) {
2393

    
2394
                Exception e = new Exception("User " + user
2395
                        + " does not have permission"
2396
                        + " to export the data package " + docIdString);
2397
                throw e;
2398
            }
2399

    
2400
            docImpls = new DocumentImpl(docIdString);
2401
            //checking if the user has the permission to read the documents
2402
            if (DocumentImpl.hasReadPermission(user, groups, docImpls
2403
                    .getDocID())) {
2404
                zOut = new ZipOutputStream(out);
2405
                //if the docImpls is metadata
2406
                if ((docImpls.getDoctype()).compareTo("BIN") != 0) {
2407
                    //add metadata into zip output stream
2408
                    addDocToZipOutputStream(docImpls, zOut, rootName);
2409
                }//if
2410
                else {
2411
                    //it is data file
2412
                    addDataFileToZipOutputStream(docImpls, zOut, rootName);
2413
                    htmlDocumentImplList.add(docImpls);
2414
                }//else
2415
            }//if
2416

    
2417
            zOut.finish(); //terminate the zip file
2418
            return zOut;
2419
        }
2420
        // Check the permission of user
2421
        else if (!hasPermissionToExportPackage(docId, user, groups)) {
2422

    
2423
            Exception e = new Exception("User " + user
2424
                    + " does not have permission"
2425
                    + " to export the data package " + docIdString);
2426
            throw e;
2427
        } else //it is a packadge id
2428
        {
2429
            //store the package id
2430
            packageId = docId;
2431
            //get current version in database
2432
            int currentVersion = getCurrentRevFromXMLDoumentsTable(packageId);
2433
            //If it is for current version (-1 means user didn't specify
2434
            // revision)
2435
            if ((version == -1) || version == currentVersion) {
2436
                //get current version number
2437
                version = currentVersion;
2438
                //get package zip entry name
2439
                //it should be docId.revsion.package
2440
                rootName = packageId + PropertyService.getProperty("document.accNumSeparator")
2441
                        + version + PropertyService.getProperty("document.accNumSeparator")
2442
                        + "package";
2443
                //get the whole id list for data packadge
2444
                docIdList = getCurrentDocidListForDataPackage(packageId);
2445
                //get the whole documentImple object
2446
                documentImplList = getCurrentAllDocumentImpl(docIdList);
2447

    
2448
            }//if
2449
            else if (version > currentVersion || version < -1) {
2450
                throw new Exception("The user specified docid: " + docId + "."
2451
                        + version + " doesn't exist");
2452
            }//else if
2453
            else //for an old version
2454
            {
2455

    
2456
                rootName = docIdString
2457
                        + PropertyService.getProperty("document.accNumSeparator") + "package";
2458
                //get the whole id list for data packadge
2459
                docIdList = getOldVersionDocidListForDataPackage(docIdString);
2460

    
2461
                //get the whole documentImple object
2462
                documentImplList = getOldVersionAllDocumentImpl(docIdList);
2463
            }//else
2464

    
2465
            // Make sure documentImplist is not empty
2466
            if (documentImplList.isEmpty()) { throw new Exception(
2467
                    "Couldn't find component for data package: " + packageId); }//if
2468

    
2469
            zOut = new ZipOutputStream(out);
2470
            //put every element into zip output stream
2471
            for (int i = 0; i < documentImplList.size(); i++) {
2472
                // if the object in the vetor is String, this means we couldn't
2473
                // find
2474
                // the document locally, we need find it remote
2475
                if ((((documentImplList.elementAt(i)).getClass()).toString())
2476
                        .equals("class java.lang.String")) {
2477
                    // Get String object from vetor
2478
                    String documentId = (String) documentImplList.elementAt(i);
2479
                    logMetacat.info("DBQuery.getZippedPackage - docid: " + documentId);
2480
                    // Get doicd without revision
2481
                    String docidWithoutRevision = 
2482
                    	DocumentUtil.getDocIdFromString(documentId);
2483
                    logMetacat.info("DBQuery.getZippedPackage - docidWithoutRevsion: "
2484
                            + docidWithoutRevision);
2485
                    // Get revision
2486
                    String revision = 
2487
                    	DocumentUtil.getRevisionStringFromString(documentId);
2488
                    logMetacat.info("DBQuery.getZippedPackage - revision from docIdentifier: "
2489
                            + revision);
2490
                    // Zip entry string
2491
                    String zipEntryPath = rootName + "/data/";
2492
                    // Create a RemoteDocument object
2493
                    RemoteDocument remoteDoc = new RemoteDocument(
2494
                            docidWithoutRevision, revision, user, passWord,
2495
                            zipEntryPath);
2496
                    // Here we only read data file from remote metacat
2497
                    String docType = remoteDoc.getDocType();
2498
                    if (docType != null) {
2499
                        if (docType.equals("BIN")) {
2500
                            // Put remote document to zip output
2501
                            remoteDoc.readDocumentFromRemoteServerByZip(zOut);
2502
                            // Add String object to htmlDocumentImplList
2503
                            String elementInHtmlList = remoteDoc
2504
                                    .getDocIdWithoutRevsion()
2505
                                    + PropertyService.getProperty("document.accNumSeparator")
2506
                                    + remoteDoc.getRevision();
2507
                            htmlDocumentImplList.add(elementInHtmlList);
2508
                        }//if
2509
                    }//if
2510

    
2511
                }//if
2512
                else {
2513
                    //create a docmentImpls object (represent xml doc) base on
2514
                    // the docId
2515
                    docImpls = (DocumentImpl) documentImplList.elementAt(i);
2516
                    //checking if the user has the permission to read the
2517
                    // documents
2518
                    if (DocumentImpl.hasReadPermission(user, groups, docImpls
2519
                            .getDocID())) {
2520
                        //if the docImpls is metadata
2521
                        if ((docImpls.getDoctype()).compareTo("BIN") != 0) {
2522
                            //add metadata into zip output stream
2523
                            addDocToZipOutputStream(docImpls, zOut, rootName);
2524
                            //add the documentImpl into the vetor which will
2525
                            // be used in html
2526
                            htmlDocumentImplList.add(docImpls);
2527

    
2528
                        }//if
2529
                        else {
2530
                            //it is data file
2531
                            addDataFileToZipOutputStream(docImpls, zOut,
2532
                                    rootName);
2533
                            htmlDocumentImplList.add(docImpls);
2534
                        }//else
2535
                    }//if
2536
                }//else
2537
            }//for
2538

    
2539
            //add html summary file
2540
            addHtmlSummaryToZipOutputStream(htmlDocumentImplList, zOut,
2541
                    rootName);
2542
            zOut.finish(); //terminate the zip file
2543
            //dbConn.close();
2544
            return zOut;
2545
        }//else
2546
    }//getZippedPackage()
2547

    
2548
    private class ReturnFieldValue
2549
    {
2550

    
2551
        private String docid = null; //return field value for this docid
2552

    
2553
        private String fieldValue = null;
2554

    
2555
        private String xmlFieldValue = null; //return field value in xml
2556
                                             // format
2557
        private String fieldType = null; //ATTRIBUTE, TEXT...
2558

    
2559
        public void setDocid(String myDocid)
2560
        {
2561
            docid = myDocid;
2562
        }
2563

    
2564
        public String getDocid()
2565
        {
2566
            return docid;
2567
        }
2568

    
2569
        public void setFieldValue(String myValue)
2570
        {
2571
            fieldValue = myValue;
2572
        }
2573

    
2574
        public String getFieldValue()
2575
        {
2576
            return fieldValue;
2577
        }
2578

    
2579
        public void setXMLFieldValue(String xml)
2580
        {
2581
            xmlFieldValue = xml;
2582
        }
2583

    
2584
        public String getXMLFieldValue()
2585
        {
2586
            return xmlFieldValue;
2587
        }
2588
        
2589
        public void setFieldType(String myType)
2590
        {
2591
            fieldType = myType;
2592
        }
2593

    
2594
        public String getFieldType()
2595
        {
2596
            return fieldType;
2597
        }
2598

    
2599
    }
2600
    
2601
    /**
2602
     * a class to store one result document consisting of a docid and a document
2603
     */
2604
    private class ResultDocument
2605
    {
2606
      public String docid;
2607
      public String document;
2608
      
2609
      public ResultDocument(String docid, String document)
2610
      {
2611
        this.docid = docid;
2612
        this.document = document;
2613
      }
2614
    }
2615
    
2616
    /**
2617
     * a private class to handle a set of resultDocuments
2618
     */
2619
    private class ResultDocumentSet
2620
    {
2621
      private Vector docids;
2622
      private Vector documents;
2623
      
2624
      public ResultDocumentSet()
2625
      {
2626
        docids = new Vector();
2627
        documents = new Vector();
2628
      }
2629
      
2630
      /**
2631
       * adds a result document to the set
2632
       */
2633
      public void addResultDocument(ResultDocument rd)
2634
      {
2635
        if(rd.docid == null)
2636
          return;
2637
        if(rd.document == null)
2638
          rd.document = "";
2639
       
2640
           docids.addElement(rd.docid);
2641
           documents.addElement(rd.document);
2642
        
2643
      }
2644
      
2645
      /**
2646
       * gets an iterator of docids
2647
       */
2648
      public Iterator getDocids()
2649
      {
2650
        return docids.iterator();
2651
      }
2652
      
2653
      /**
2654
       * gets an iterator of documents
2655
       */
2656
      public Iterator getDocuments()
2657
      {
2658
        return documents.iterator();
2659
      }
2660
      
2661
      /**
2662
       * returns the size of the set
2663
       */
2664
      public int size()
2665
      {
2666
        return docids.size();
2667
      }
2668
      
2669
      /**
2670
       * tests to see if this set contains the given docid
2671
       */
2672
      private boolean containsDocid(String docid)
2673
      {
2674
        for(int i=0; i<docids.size(); i++)
2675
        {
2676
          String docid0 = (String)docids.elementAt(i);
2677
          if(docid0.trim().equals(docid.trim()))
2678
          {
2679
            return true;
2680
          }
2681
        }
2682
        return false;
2683
      }
2684
      
2685
      /**
2686
       * removes the element with the given docid
2687
       */
2688
      public String remove(String docid)
2689
      {
2690
        for(int i=0; i<docids.size(); i++)
2691
        {
2692
          String docid0 = (String)docids.elementAt(i);
2693
          if(docid0.trim().equals(docid.trim()))
2694
          {
2695
            String returnDoc = (String)documents.elementAt(i);
2696
            documents.remove(i);
2697
            docids.remove(i);
2698
            return returnDoc;
2699
          }
2700
        }
2701
        return null;
2702
      }
2703
      
2704
      /**
2705
       * add a result document
2706
       */
2707
      public void put(ResultDocument rd)
2708
      {
2709
        addResultDocument(rd);
2710
      }
2711
      
2712
      /**
2713
       * add a result document by components
2714
       */
2715
      public void put(String docid, String document)
2716
      {
2717
        addResultDocument(new ResultDocument(docid, document));
2718
      }
2719
      
2720
      /**
2721
       * get the document part of the result document by docid
2722
       */
2723
      public Object get(String docid)
2724
      {
2725
        for(int i=0; i<docids.size(); i++)
2726
        {
2727
          String docid0 = (String)docids.elementAt(i);
2728
          if(docid0.trim().equals(docid.trim()))
2729
          {
2730
            return documents.elementAt(i);
2731
          }
2732
        }
2733
        return null;
2734
      }
2735
      
2736
      /**
2737
       * get the document part of the result document by an object
2738
       */
2739
      public Object get(Object o)
2740
      {
2741
        return get((String)o);
2742
      }
2743
      
2744
      /**
2745
       * get an entire result document by index number
2746
       */
2747
      public ResultDocument get(int index)
2748
      {
2749
        return new ResultDocument((String)docids.elementAt(index), 
2750
          (String)documents.elementAt(index));
2751
      }
2752
      
2753
      /**
2754
       * return a string representation of this object
2755
       */
2756
      public String toString()
2757
      {
2758
        String s = "";
2759
        for(int i=0; i<docids.size(); i++)
2760
        {
2761
          s += (String)docids.elementAt(i) + "\n";
2762
        }
2763
        return s;
2764
      }
2765
      /*
2766
       * Set a new document value for a given docid
2767
       */
2768
      public void set(String docid, String document)
2769
      {
2770
    	   for(int i=0; i<docids.size(); i++)
2771
           {
2772
             String docid0 = (String)docids.elementAt(i);
2773
             if(docid0.trim().equals(docid.trim()))
2774
             {
2775
                 documents.set(i, document);
2776
             }
2777
           }
2778
           
2779
      }
2780
    }
2781
}
(17-17/65)