Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that searches a relational DB for elements and
4
 *             attributes that have free text matches a query string,
5
 *             or structured query matches to a path specified node in the
6
 *             XML hierarchy.  It returns a result set consisting of the
7
 *             document ID for each document that satisfies the query
8
 *  Copyright: 2000 Regents of the University of California and the
9
 *             National Center for Ecological Analysis and Synthesis
10
 *    Authors: Matt Jones
11
 *
12
 *   '$Author: tao $'
13
 *     '$Date: 2013-08-26 16:30:00 -0700 (Mon, 26 Aug 2013) $'
14
 * '$Revision: 8164 $'
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.commons.lang.StringEscapeUtils;
64
import org.apache.log4j.Logger;
65
import org.dataone.service.exceptions.NotImplemented;
66

    
67
import edu.ucsb.nceas.metacat.common.query.EnabledQueryEngines;
68
import edu.ucsb.nceas.metacat.database.DBConnection;
69
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
70
import edu.ucsb.nceas.metacat.properties.PropertyService;
71
import edu.ucsb.nceas.metacat.shared.HandlerException;
72
import edu.ucsb.nceas.metacat.util.AuthUtil;
73
import edu.ucsb.nceas.metacat.util.DocumentUtil;
74
import edu.ucsb.nceas.metacat.util.MetacatUtil;
75
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
76

    
77
import edu.ucsb.nceas.utilities.access.AccessControlInterface;
78
import edu.ucsb.nceas.utilities.triple.Triple;
79
import edu.ucsb.nceas.utilities.triple.TripleCollection;
80

    
81

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

    
91
    public static final String XPATHQUERYOFFINFO = "The Metacat Path Query Engine is turned off. If you want to turn it on, please contact the administrator.";
92
    static final int ALL = 1;
93

    
94
    static final int WRITE = 2;
95

    
96
    static final int READ = 4;
97
    
98
    private String qformat = "xml";
99
    
100
    // are we combining the query with docid list and, if so, using INTERSECT or UNION?
101
    private String operator = null;
102

    
103
    //private Connection conn = null;
104
    private String parserName = null;
105

    
106
    private Logger logMetacat = Logger.getLogger(DBQuery.class);
107

    
108
    /** true if the metacat spatial option is installed **/
109
    private final boolean METACAT_SPATIAL = true;
110

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

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

    
145
        if (args.length < 1) {
146
            System.err.println("Wrong number of arguments!!!");
147
            System.err.println("USAGE: java DBQuery [-t] [-index] <xmlfile>");
148
            return;
149
        } else {
150
            try {
151

    
152
                int i = 0;
153
                boolean showRuntime = false;
154
                boolean useXMLIndex = false;
155
                if (args[i].equals("-t")) {
156
                    showRuntime = true;
157
                    i++;
158
                }
159
                if (args[i].equals("-index")) {
160
                    useXMLIndex = true;
161
                    i++;
162
                }
163
                String xmlfile = args[i];
164

    
165
                // Time the request if asked for
166
                double startTime = System.currentTimeMillis();
167

    
168
                // Open a connection to the database
169
                //Connection dbconn = util.openDBConnection();
170

    
171
                double connTime = System.currentTimeMillis();
172

    
173
                // Execute the query
174
                DBQuery queryobj = new DBQuery();
175
                Reader xml = new InputStreamReader(new FileInputStream(new File(xmlfile)));
176
                Hashtable nodelist = null;
177
                //nodelist = queryobj.findDocuments(xml, null, null, useXMLIndex);
178

    
179
                // Print the reulting document listing
180
                StringBuffer result = new StringBuffer();
181
                String document = null;
182
                String docid = null;
183
                result.append("<?xml version=\"1.0\"?>\n");
184
                result.append("<resultset>\n");
185

    
186
                if (!showRuntime) {
187
                    Enumeration doclist = nodelist.keys();
188
                    while (doclist.hasMoreElements()) {
189
                        docid = (String) doclist.nextElement();
190
                        document = (String) nodelist.get(docid);
191
                        result.append("  <document>\n    " + document
192
                                + "\n  </document>\n");
193
                    }
194

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

    
221
            } catch (Exception e) {
222
                System.err.println("Error in DBQuery.main");
223
                System.err.println(e.getMessage());
224
                e.printStackTrace(System.err);
225
            }
226
        }
227
    }
228

    
229
    /**
230
     * construct an instance of the DBQuery class
231
     *
232
     * <p>
233
     * Generally, one would call the findDocuments() routine after creating an
234
     * instance to specify the search query
235
     * </p>
236
     *
237

    
238
     * @param parserName the fully qualified name of a Java class implementing
239
     *            the org.xml.sax.XMLReader interface
240
     */
241
    public DBQuery() throws PropertyNotFoundException
242
    {
243
        String parserName = PropertyService.getProperty("xml.saxparser");
244
        this.parserName = parserName;
245
    }
246

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

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

    
323
  }
324

    
325

    
326
    /**
327
     * Method put the search result set into out printerwriter
328
     * @param resoponse the return response
329
     * @param out the output printer
330
     * @param params the paratermer hashtable
331
     * @param user the user name (it maybe different to the one in param)
332
     * @param groups the group array
333
     * @param sessionid  the sessionid
334
     */
335
    private void findDocuments(HttpServletResponse response,
336
                                         Writer out, Hashtable params,
337
                                         String user, String[] groups,
338
                                         String sessionid, boolean useXMLIndex) {
339
      if(!EnabledQueryEngines.getInstance().isEnabled(EnabledQueryEngines.PATHQUERYENGINE)) {
340
          try {
341
              String output = "";
342
              output += "<?xml version=\"1.0\"?>";
343
              output += "<error>";
344
              output += XPATHQUERYOFFINFO;
345
              output += "</error>";
346
              out.write(output);
347
              out.close();
348
          } catch (IOException e) {
349
              logMetacat.warn("DBQuery.findDocuments - metacat can't write the message that the pathquery is off to the client since :"+e.getMessage());
350
          } 
351
          return;
352
      }
353
      int pagesize = 0;
354
      int pagestart = 0;
355
      long transferWarnLimit = 0; 
356
      
357
      if(params.containsKey("pagesize") && params.containsKey("pagestart"))
358
      {
359
        String pagesizeStr = ((String[])params.get("pagesize"))[0];
360
        String pagestartStr = ((String[])params.get("pagestart"))[0];
361
        if(pagesizeStr != null && pagestartStr != null)
362
        {
363
          pagesize = (new Integer(pagesizeStr)).intValue();
364
          pagestart = (new Integer(pagestartStr)).intValue();
365
        }
366
      }
367
      
368
      String xmlquery = null;
369
      String qformat = null;
370
      // get query and qformat
371
      try {
372
    	xmlquery = ((String[])params.get("query"))[0];
373

    
374
        logMetacat.info("DBQuery.findDocuments - SESSIONID: " + sessionid);
375
        logMetacat.info("DBQuery.findDocuments - xmlquery: " + xmlquery);
376
        qformat = ((String[])params.get("qformat"))[0];
377
        logMetacat.info("DBQuery.findDocuments - qformat: " + qformat);
378
      }
379
      catch (Exception ee)
380
      {
381
        logMetacat.error("DBQuery.findDocuments - Couldn't retrieve xmlquery or qformat value from "
382
                  +"params hashtable in DBQuery.findDocuments: "
383
                  + ee.getMessage()); 
384
      }
385
      // Get the XML query and covert it into a SQL statment
386
      QuerySpecification qspec = null;
387
      if ( xmlquery != null)
388
      {
389
         xmlquery = transformQuery(xmlquery);
390
         try
391
         {
392
           qspec = new QuerySpecification(xmlquery,
393
                                          parserName,
394
                                          PropertyService.getProperty("document.accNumSeparator"));
395
         }
396
         catch (Exception ee)
397
         {
398
           logMetacat.error("DBQuery.findDocuments - error generating QuerySpecification object: "
399
                                    + ee.getMessage());
400
         }
401
      }
402

    
403

    
404

    
405
      if (qformat != null && qformat.equals(MetacatUtil.XMLFORMAT))
406
      {
407
        //xml format
408
        if(response != null)
409
        {
410
            response.setContentType("text/xml");
411
        }
412
        createResultDocument(xmlquery, qspec, out, user, groups, useXMLIndex, 
413
          pagesize, pagestart, sessionid, qformat, false);
414
      }//if
415
      else
416
      {
417
        //knb format, in this case we will get whole result and sent it out
418
        response.setContentType("text/html");
419
        Writer nonout = null;
420
        StringBuffer xml = createResultDocument(xmlquery, qspec, nonout, user,
421
                                                groups, useXMLIndex, pagesize, 
422
                                                pagestart, sessionid, qformat, false);
423
        
424
        //transfer the xml to html
425
        try
426
        {
427
         long startHTMLTransform = System.currentTimeMillis();
428
         DBTransform trans = new DBTransform();
429
         response.setContentType("text/html");
430

    
431
         // if the user is a moderator, then pass a param to the 
432
         // xsl specifying the fact
433
         if(AuthUtil.isModerator(user, groups)){
434
        	 params.put("isModerator", new String[] {"true"});
435
         }
436

    
437
         trans.transformXMLDocument(xml.toString(), "-//NCEAS//resultset//EN",
438
                                 "-//W3C//HTML//EN", qformat, out, params,
439
                                 sessionid);
440
         long transformRunTime = System.currentTimeMillis() - startHTMLTransform;
441
         
442
         transferWarnLimit = Long.parseLong(PropertyService.getProperty("dbquery.transformTimeWarnLimit"));
443
         
444
         if (transformRunTime > transferWarnLimit) {
445
         	logMetacat.warn("DBQuery.findDocuments - The time to transfrom resultset from xml to html format is "
446
                  		                             + transformRunTime);
447
         }
448
          MetacatUtil.writeDebugToFile("---------------------------------------------------------------------------------------------------------------Transfrom xml to html  "
449
                             + transformRunTime);
450
          MetacatUtil.writeDebugToDelimiteredFile(" " + transformRunTime, false);
451
        }
452
        catch(Exception e)
453
        {
454
         logMetacat.error("DBQuery.findDocuments - Error in MetaCatServlet.transformResultset:"
455
                                +e.getMessage());
456
         }
457

    
458
      }//else
459

    
460
  }
461
    
462
  /**
463
   * Transforms a hashtable of documents to an xml or html result and sent
464
   * the content to outputstream. Keep going untill hastable is empty. stop it.
465
   * add the QuerySpecification as parameter is for ecogrid. But it is duplicate
466
   * to xmlquery String
467
   * @param xmlquery
468
   * @param qspec
469
   * @param out
470
   * @param user
471
   * @param groups
472
   * @param useXMLIndex
473
   * @param sessionid
474
   * @return
475
   */
476
    public StringBuffer createResultDocument(String xmlquery,
477
                                              QuerySpecification qspec,
478
                                              Writer out,
479
                                              String user, String[] groups,
480
                                              boolean useXMLIndex)
481
    {
482
    	return createResultDocument(xmlquery,qspec,out, user,groups, useXMLIndex, 0, 0,"", qformat, false);
483
    }
484
    
485
    /**
486
     * 
487
     * @param xmlquery
488
     * @param user
489
     * @param groups
490
     * @param useXMLIndex
491
     * @return
492
     * @throws IOException 
493
     * @throws PropertyNotFoundException 
494
     */
495
	public String performPathquery(String xmlquery, String user,
496
			String[] groups) throws PropertyNotFoundException, IOException {
497
		
498
		// get the XML query and convert it to query specification
499
		xmlquery = transformQuery(xmlquery);
500
		QuerySpecification qspec = new QuerySpecification(xmlquery, parserName, PropertyService.getProperty("document.accNumSeparator"));
501
		
502
		// force it to output the results to the string buffer, not outputstream
503
		Writer nonout = null;
504
		boolean useXMLIndex = (new Boolean(PropertyService.getProperty("database.usexmlindex"))).booleanValue();
505
		StringBuffer xml = createResultDocument(xmlquery, qspec, nonout, user, groups, useXMLIndex, 0, 0, "", qformat, true);
506

    
507
		return xml.toString();
508

    
509
	}
510

    
511
  /*
512
   * Transforms a hashtable of documents to an xml or html result and sent
513
   * the content to outputstream. Keep going untill hastable is empty. stop it.
514
   * add the QuerySpecification as parameter is for ecogrid. But it is duplicate
515
   * to xmlquery String
516
   */
517
  public StringBuffer createResultDocument(String xmlquery,
518
                                            QuerySpecification qspec,
519
                                            Writer out,
520
                                            String user, String[] groups,
521
                                            boolean useXMLIndex, int pagesize,
522
                                            int pagestart, String sessionid, 
523
                                            String qformat, boolean includeGuid)
524
  {
525
    DBConnection dbconn = null;
526
    int serialNumber = -1;
527
    StringBuffer resultset = new StringBuffer();
528

    
529
    //try to get the cached version first    
530
    // Hashtable sessionHash = MetaCatServlet.getSessionHash();
531
    // HttpSession sess = (HttpSession)sessionHash.get(sessionid);
532

    
533
    
534
    resultset.append("<?xml version=\"1.0\"?>\n");
535
    resultset.append("<resultset>\n");
536
    resultset.append("  <pagestart>" + pagestart + "</pagestart>\n");
537
    resultset.append("  <pagesize>" + pagesize + "</pagesize>\n");
538
    resultset.append("  <nextpage>" + (pagestart + 1) + "</nextpage>\n");
539
    resultset.append("  <previouspage>" + (pagestart - 1) + "</previouspage>\n");
540

    
541
    resultset.append("  <query><![CDATA[" + xmlquery + "]]></query>");
542
    //send out a new query
543
    if (out != null)
544
    {
545
    	try {
546
    	  out.write(resultset.toString());
547
		} catch (IOException e) {
548
			logMetacat.error(e.getMessage(), e);
549
		}
550
    }
551
    if (qspec != null)
552
    {
553
      try
554
      {
555

    
556
        //checkout the dbconnection
557
        dbconn = DBConnectionPool.getDBConnection("DBQuery.findDocuments");
558
        serialNumber = dbconn.getCheckOutSerialNumber();
559

    
560
        //print out the search result
561
        // search the doc list
562
        Vector givenDocids = new Vector();
563
        StringBuffer resultContent = new StringBuffer();
564
        if (docidOverride == null || docidOverride.size() == 0)
565
        {
566
        	logMetacat.debug("DBQuery.createResultDocument - Not in map query");
567
        	resultContent = findResultDoclist(qspec, out, user, groups,
568
                    dbconn, useXMLIndex, pagesize, pagestart, 
569
                    sessionid, givenDocids, qformat, includeGuid);
570
        }
571
        else
572
        {
573
        	logMetacat.debug("DBQuery.createResultDocument - In map query");
574
        	// since docid can be too long to be handled. We divide it into several parts
575
        	for (int i= 0; i<docidOverride.size(); i++)
576
        	{
577
        	   logMetacat.debug("DBQuery.createResultDocument - in loop===== "+i);
578
        		givenDocids = (Vector)docidOverride.elementAt(i);
579
        		StringBuffer subset = findResultDoclist(qspec, out, user, groups,
580
                        dbconn, useXMLIndex, pagesize, pagestart, 
581
                        sessionid, givenDocids, qformat, includeGuid);
582
        		resultContent.append(subset);
583
        	}
584
        }
585
           
586
        resultset.append(resultContent);
587
      } //try
588
      catch (IOException ioe)
589
      {
590
        logMetacat.error("DBQuery.createResultDocument - IO error: " + ioe.getMessage());
591
      }
592
      catch (SQLException e)
593
      {
594
        logMetacat.error("DBQuery.createResultDocument - SQL Error: " + e.getMessage());
595
      }
596
      catch (Exception ee)
597
      {
598
        logMetacat.error("DBQuery.createResultDocument - General exception: "
599
                                 + ee.getMessage());
600
        ee.printStackTrace();
601
      }
602
      finally
603
      {
604
        DBConnectionPool.returnDBConnection(dbconn, serialNumber);
605
      } //finally
606
    }//if
607
    String closeRestultset = "</resultset>";
608
    resultset.append(closeRestultset);
609
    if (out != null)
610
    {
611
      try {
612
		out.write(closeRestultset);
613
		} catch (IOException e) {
614
			logMetacat.error(e.getMessage(), e);
615
		}
616
    }
617

    
618
    //default to returning the whole resultset
619
    return resultset;
620
  }//createResultDocuments
621

    
622
    /*
623
     * Find the doc list which match the query
624
     */
625
    private StringBuffer findResultDoclist(QuerySpecification qspec,
626
                                      Writer out,
627
                                      String user, String[]groups,
628
                                      DBConnection dbconn, boolean useXMLIndex,
629
                                      int pagesize, int pagestart, String sessionid, 
630
                                      Vector givenDocids, String qformat, boolean includeGuid)
631
                                      throws Exception
632
    {
633
    	// keep track of the values we add as prepared statement question marks (?)
634
  	  List<Object> parameterValues = new ArrayList<Object>();
635
  	  
636
      StringBuffer resultsetBuffer = new StringBuffer();
637
      String query = null;
638
      int count = 0;
639
      int index = 0;
640
      ResultDocumentSet docListResult = new ResultDocumentSet();
641
      PreparedStatement pstmt = null;
642
      String docid = null;
643
      String guid = null;
644
      String docname = null;
645
      String doctype = null;
646
      String createDate = null;
647
      String updateDate = null;
648
      StringBuffer document = null;
649
      boolean lastpage = false;
650
      int rev = 0;
651
      double startTime = 0;
652
      int offset = 1;
653
      long startSelectionTime = System.currentTimeMillis();
654
      ResultSet rs = null;
655
           
656
   
657
      // this is a hack for offset. in postgresql 7, if the returned docid list is too long,
658
      //the extend query which base on the docid will be too long to be run. So we 
659
      // have to cut them into different parts. Page query don't need it somehow.
660
      if (out == null)
661
      {
662
        // for html page, we put everything into one page
663
        offset =
664
            (new Integer(PropertyService.getProperty("database.webResultsetSize"))).intValue();
665
      }
666
      else
667
      {
668
          offset =
669
              (new Integer(PropertyService.getProperty("database.appResultsetSize"))).intValue();
670
      }
671

    
672
      /*
673
       * Check the docidOverride Vector
674
       * if defined, we bypass the qspec.printSQL() method
675
       * and contruct a simpler query based on a 
676
       * list of docids rather than a bunch of subselects
677
       */
678
      // keep track of the values we add as prepared statement question marks (?)
679
	  List<Object> docidValues = new ArrayList<Object>();
680
      if ( givenDocids == null || givenDocids.size() == 0 ) {
681
          query = qspec.printSQL(useXMLIndex, docidValues);
682
          parameterValues.addAll(docidValues);
683
      } else {
684
    	  // condition for the docids
685
    	  List<Object> docidConditionValues = new ArrayList<Object>();
686
    	  StringBuffer docidCondition = new StringBuffer();
687
    	  docidCondition.append( " xml_documents.docid IN (" );
688
          for (int i = 0; i < givenDocids.size(); i++) {  
689
        	  docidCondition.append("?");
690
        	  if (i < givenDocids.size()-1) {
691
        		  docidCondition.append(",");
692
        	  }
693
        	  docidConditionValues.add((String)givenDocids.elementAt(i));
694
          }
695
          docidCondition.append( ") " );
696
		  
697
    	  // include the docids, either exclusively, or in conjuction with the query
698
    	  if (operator == null) {
699
    		  query = "SELECT xml_documents.docid, identifier.guid, docname, doctype, date_created, date_updated, xml_documents.rev " +
700
    		  		"FROM xml_documents, identifier " +
701
    		  		"WHERE xml_documents.docid = identifier.docid AND xml_documents.rev = identifier.rev AND ";
702
              query = query + docidCondition.toString();
703
              parameterValues.addAll(docidConditionValues);
704
    	  } else {
705
    		  // start with the keyword query, but add conditions
706
              query = qspec.printSQL(useXMLIndex, docidValues);
707
              parameterValues.addAll(docidValues);
708
              String myOperator = "";
709
              if (!query.endsWith("WHERE") && !query.endsWith("OR") && !query.endsWith("AND")) {
710
	              if (operator.equalsIgnoreCase(QueryGroup.UNION)) {
711
	            	  myOperator =  " OR ";
712
	              }
713
	              else {
714
	            	  myOperator =  " AND ";
715
	              }
716
              }
717
              query = query + myOperator + docidCondition.toString();
718
              parameterValues.addAll(docidConditionValues);
719

    
720
    	  }
721
      } 
722
      // we don't actually use this query for anything
723
      List<Object> ownerValues = new ArrayList<Object>();
724
      String ownerQuery = getOwnerQuery(user, ownerValues);
725
      //logMetacat.debug("query: " + query);
726
      logMetacat.debug("DBQuery.findResultDoclist - owner query: " + ownerQuery);
727
      // if query is not the owner query, we need to check the permission
728
      // otherwise we don't need (owner has all permission by default)
729
      if (!query.equals(ownerQuery))
730
      {
731
        // set user name and group
732
        qspec.setUserName(user);
733
        qspec.setGroup(groups);
734
        // Get access query
735
        String accessQuery = qspec.getAccessQuery();
736
        if(!query.endsWith("AND")){
737
            query = query + accessQuery;
738
        } else {
739
            query = query + accessQuery.substring(4, accessQuery.length());
740
        }
741
        
742
      }
743
      logMetacat.debug("DBQuery.findResultDoclist - final selection query: " + query);
744
      
745
    
746
      pstmt = dbconn.prepareStatement(query);     
747
      // set all the values we have collected 
748
      pstmt = setPreparedStatementValues(parameterValues, pstmt);
749
      
750
      String queryCacheKey = null;
751
      // we only get cache for public
752
      if (user != null && user.equalsIgnoreCase("public") 
753
         && pagesize == 0 && PropertyService.getProperty("database.queryCacheOn").equals("true"))
754
      {
755
          queryCacheKey = pstmt.toString() +qspec.getReturnDocList()+qspec.getReturnFieldList();
756
          String cachedResult = getResultXMLFromCache(queryCacheKey);
757
          logMetacat.debug("=======DBQuery.findResultDoclist - The key of query cache is " + queryCacheKey);
758
          //System.out.println("==========the string from cache is "+cachedResult);
759
          if (cachedResult != null)
760
          {
761
          logMetacat.info("DBQuery.findResultDoclist - result from cache !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
762
           if (out != null)
763
             {
764
                 out.write(cachedResult);
765
             }
766
           resultsetBuffer.append(cachedResult);
767
           pstmt.close();
768
           return resultsetBuffer;
769
          }
770
      }
771
      
772
      startTime = System.currentTimeMillis() / 1000;
773
      logMetacat.debug("Prepared statement after setting parameter values: " + pstmt.toString());
774
      rs = pstmt.executeQuery();
775

    
776
      double queryExecuteTime = System.currentTimeMillis() / 1000;
777
      logMetacat.debug("DBQuery.findResultDoclist - Time to execute select docid query is "
778
                    + (queryExecuteTime - startTime));
779
      MetacatUtil.writeDebugToFile("\n\n\n\n\n\nExecute selection query  "
780
              + (queryExecuteTime - startTime));
781
      MetacatUtil.writeDebugToDelimiteredFile(""+(queryExecuteTime - startTime), false);
782

    
783
      boolean tableHasRows = rs.next();
784
      
785
      if(pagesize == 0)
786
      { //this makes sure we get all results if there is no paging
787
        pagesize = NONPAGESIZE;
788
        pagestart = NONPAGESIZE;
789
      } 
790
      
791
      int currentIndex = 0;
792
      while (tableHasRows)
793
      {
794
        logMetacat.debug("DBQuery.findResultDoclist - getting result: " + currentIndex);
795
        docid = rs.getString(1).trim();
796
        logMetacat.debug("DBQuery.findResultDoclist -  docid: " + docid);
797
        guid = rs.getString(2).trim();
798
        logMetacat.debug("DBQuery.findResultDoclist -  guid: " + guid);
799
        docname = rs.getString(3);
800
        doctype = rs.getString(4);
801
        logMetacat.debug("DBQuery.findResultDoclist - doctype: " + doctype);
802
        createDate = rs.getString(5);
803
        updateDate = rs.getString(6);
804
        rev = rs.getInt(7);
805
        
806
         Vector returndocVec = qspec.getReturnDocList();
807
       if (returndocVec.size() == 0 || returndocVec.contains(doctype))
808
        {
809
          logMetacat.debug("DBQuery.findResultDoclist - NOT Back tracing now...");
810
           document = new StringBuffer();
811

    
812
           String completeDocid = docid
813
                            + PropertyService.getProperty("document.accNumSeparator");
814
           completeDocid += rev;
815
           document.append("<docid>").append(completeDocid).append("</docid>");
816
           if (includeGuid) {
817
        	   document.append("<guid>").append(guid).append("</guid>");
818
           }
819
           if (docname != null)
820
           {
821
               document.append("<docname>" + docname + "</docname>");
822
           }
823
           if (doctype != null)
824
           {
825
              document.append("<doctype>" + doctype + "</doctype>");
826
           }
827
           if (createDate != null)
828
           {
829
               document.append("<createdate>" + createDate + "</createdate>");
830
           }
831
           if (updateDate != null)
832
           {
833
             document.append("<updatedate>" + updateDate + "</updatedate>");
834
           }
835
           // Store the document id and the root node id
836
           
837
           docListResult.addResultDocument(
838
             new ResultDocument(docid, (String) document.toString()));
839
           logMetacat.info("DBQuery.findResultDoclist - real result: " + docid);
840
           currentIndex++;
841
           count++;
842
        }//else
843
        
844
        // when doclist reached the offset number, send out doc list and empty
845
        // the hash table
846
        if (count == offset && pagesize == NONPAGESIZE)
847
        { //if pagesize is not 0, do this later.
848
          //reset count
849
          //logMetacat.warn("############doing subset cache");
850
          count = 0;
851
          handleSubsetResult(qspec, resultsetBuffer, out, docListResult,
852
                              user, groups,dbconn, useXMLIndex, qformat);
853
          //reset docListResult
854
          docListResult = new ResultDocumentSet();
855
        }
856
       
857
       logMetacat.debug("DBQuery.findResultDoclist - currentIndex: " + currentIndex);
858
       logMetacat.debug("DBQuery.findResultDoclist - page comparator: " + (pagesize * pagestart) + pagesize);
859
       if(currentIndex >= ((pagesize * pagestart) + pagesize))
860
       {
861
         ResultDocumentSet pagedResultsHash = new ResultDocumentSet();
862
         for(int i=pagesize*pagestart; i<docListResult.size(); i++)
863
         {
864
           pagedResultsHash.put(docListResult.get(i));
865
         }
866
         
867
         docListResult = pagedResultsHash;
868
         break;
869
       }
870
       // Advance to the next record in the cursor
871
       tableHasRows = rs.next();
872
       if(!tableHasRows)
873
       {
874
         ResultDocumentSet pagedResultsHash = new ResultDocumentSet();
875
         //get the last page of information then break
876
         if(pagesize != NONPAGESIZE)
877
         {
878
           for(int i=pagesize*pagestart; i<docListResult.size(); i++)
879
           {
880
             pagedResultsHash.put(docListResult.get(i));
881
           }
882
           docListResult = pagedResultsHash;
883
         }
884
         
885
         lastpage = true;
886
         break;
887
       }
888
     }//while
889
     
890
     rs.close();
891
     pstmt.close();
892
     long docListTime = System.currentTimeMillis() - startSelectionTime;
893
     long docListWarnLimit = Long.parseLong(PropertyService.getProperty("dbquery.findDocListTimeWarnLimit"));
894
     if (docListTime > docListWarnLimit) {
895
    	 logMetacat.warn("DBQuery.findResultDoclist - Total time to get docid list is: "
896
                          + docListTime);
897
     }
898
     MetacatUtil.writeDebugToFile("---------------------------------------------------------------------------------------------------------------Total selection: "
899
             + docListTime);
900
     MetacatUtil.writeDebugToDelimiteredFile(" "+ docListTime, false);
901
     //if docListResult is not empty, it need to be sent.
902
     if (docListResult.size() != 0)
903
     {
904
      
905
       handleSubsetResult(qspec,resultsetBuffer, out, docListResult,
906
                              user, groups,dbconn, useXMLIndex, qformat);
907
     }
908

    
909
     resultsetBuffer.append("\n<lastpage>" + lastpage + "</lastpage>\n");
910
     if (out != null)
911
     {
912
         out.write("\n<lastpage>" + lastpage + "</lastpage>\n");
913
     }
914
     
915
     // now we only cached none-paged query and user is public
916
     if (user != null && user.equalsIgnoreCase("public") 
917
    		 && pagesize == NONPAGESIZE && PropertyService.getProperty("database.queryCacheOn").equals("true"))
918
     {
919
       //System.out.println("the string stored into cache is "+ resultsetBuffer.toString());
920
  	   storeQueryResultIntoCache(queryCacheKey, resultsetBuffer.toString());
921
     }
922
          
923
     return resultsetBuffer;
924
    }//findReturnDoclist
925

    
926

    
927
    /*
928
     * Send completed search hashtable(part of reulst)to output stream
929
     * and buffer into a buffer stream
930
     */
931
    private StringBuffer handleSubsetResult(QuerySpecification qspec,
932
                                           StringBuffer resultset,
933
                                           Writer out, ResultDocumentSet partOfDoclist,
934
                                           String user, String[]groups,
935
                                       DBConnection dbconn, boolean useXMLIndex,
936
                                       String qformat)
937
                                       throws Exception
938
   {
939
     double startReturnFieldTime = System.currentTimeMillis();
940
     // check if there is a record in xml_returnfield
941
     // and get the returnfield_id and usage count
942
     int usage_count = getXmlReturnfieldsTableId(qspec, dbconn);
943
     boolean enterRecords = false;
944

    
945
     // get value of database.xmlReturnfieldCount
946
     int count = (new Integer(PropertyService
947
                            .getProperty("database.xmlReturnfieldCount")))
948
                            .intValue();
949

    
950
     // set enterRecords to true if usage_count is more than the offset
951
     // specified in metacat.properties
952
     if(usage_count > count){
953
         enterRecords = true;
954
     }
955

    
956
     if(returnfield_id < 0){
957
         logMetacat.warn("DBQuery.handleSubsetResult - Error in getting returnfield id from"
958
                                  + "xml_returnfield table");
959
         enterRecords = false;
960
     }
961

    
962
     // get the hashtable containing the docids that already in the
963
     // xml_queryresult table
964
     logMetacat.info("DBQuery.handleSubsetResult - size of partOfDoclist before"
965
                             + " docidsInQueryresultTable(): "
966
                             + partOfDoclist.size());
967
     long startGetReturnValueFromQueryresultable = System.currentTimeMillis();
968
     Hashtable queryresultDocList = docidsInQueryresultTable(returnfield_id,
969
                                                        partOfDoclist, dbconn);
970

    
971
     // remove the keys in queryresultDocList from partOfDoclist
972
     Enumeration _keys = queryresultDocList.keys();
973
     while (_keys.hasMoreElements()){
974
         partOfDoclist.remove((String)_keys.nextElement());
975
     }
976
     
977
     long queryResultReturnValuetime = System.currentTimeMillis() - startGetReturnValueFromQueryresultable;
978
     long queryResultWarnLimit = 
979
    	 Long.parseLong(PropertyService.getProperty("dbquery.findQueryResultsTimeWarnLimit"));
980
     
981
     if (queryResultReturnValuetime > queryResultWarnLimit) {
982
    	 logMetacat.warn("DBQuery.handleSubsetResult - Time to get return fields from xml_queryresult table is (Part1 in return fields) " +
983
    		 queryResultReturnValuetime);
984
     }
985
     MetacatUtil.writeDebugToFile("-----------------------------------------Get fields from xml_queryresult(Part1 in return fields) " +
986
    		 queryResultReturnValuetime);
987
     MetacatUtil.writeDebugToDelimiteredFile(" " + queryResultReturnValuetime,false);
988
     
989
     long startExtendedQuery = System.currentTimeMillis();
990
     // backup the keys-elements in partOfDoclist to check later
991
     // if the doc entry is indexed yet
992
     Hashtable partOfDoclistBackup = new Hashtable();
993
     Iterator itt = partOfDoclist.getDocids();
994
     while (itt.hasNext()){
995
       Object key = itt.next();
996
         partOfDoclistBackup.put(key, partOfDoclist.get(key));
997
     }
998

    
999
     logMetacat.info("DBQuery.handleSubsetResult - size of partOfDoclist after"
1000
                             + " docidsInQueryresultTable(): "
1001
                             + partOfDoclist.size());
1002

    
1003
     //add return fields for the documents in partOfDoclist
1004
     partOfDoclist = addReturnfield(partOfDoclist, qspec, user, groups,
1005
                                        dbconn, useXMLIndex, qformat);
1006
     long extendedQueryRunTime = startExtendedQuery - System.currentTimeMillis();
1007
     long extendedQueryWarnLimit = 
1008
    	 Long.parseLong(PropertyService.getProperty("dbquery.extendedQueryRunTimeWarnLimit"));
1009
  
1010
     if (extendedQueryRunTime > extendedQueryWarnLimit) {
1011
    	 logMetacat.warn("DBQuery.handleSubsetResult - Get fields from index and node table (Part2 in return fields) "
1012
        		                                          + extendedQueryRunTime);
1013
     }
1014
     MetacatUtil.writeDebugToFile("-----------------------------------------Get fields from extened query(Part2 in return fields) "
1015
             + extendedQueryRunTime);
1016
     MetacatUtil.writeDebugToDelimiteredFile(" "
1017
             + extendedQueryRunTime, false);
1018
     //add relationship part part docid list for the documents in partOfDocList
1019
     //partOfDoclist = addRelationship(partOfDoclist, qspec, dbconn, useXMLIndex);
1020

    
1021
     long startStoreReturnField = System.currentTimeMillis();
1022
     Iterator keys = partOfDoclist.getDocids();
1023
     String key = null;
1024
     String element = null;
1025
     String query = null;
1026
     int offset = (new Integer(PropertyService
1027
                               .getProperty("database.queryresultStringLength")))
1028
                               .intValue();
1029
     while (keys.hasNext())
1030
     {
1031
         key = (String) keys.next();
1032
         element = (String)partOfDoclist.get(key);
1033
         
1034
	 // check if the enterRecords is true, elements is not null, element's
1035
         // length is less than the limit of table column and if the document
1036
         // has been indexed already
1037
         if(enterRecords && element != null
1038
		&& element.length() < offset
1039
		&& element.compareTo((String) partOfDoclistBackup.get(key)) != 0){
1040
             query = "INSERT INTO xml_queryresult (returnfield_id, docid, "
1041
                 + "queryresult_string) VALUES (?, ?, ?)";
1042

    
1043
             PreparedStatement pstmt = null;
1044
             pstmt = dbconn.prepareStatement(query);
1045
             pstmt.setInt(1, returnfield_id);
1046
             pstmt.setString(2, key);
1047
             pstmt.setString(3, element);
1048
            
1049
             dbconn.increaseUsageCount(1);
1050
             try
1051
             {
1052
            	 pstmt.execute();
1053
             }
1054
             catch(Exception e)
1055
             {
1056
            	 logMetacat.warn("DBQuery.handleSubsetResult - couldn't insert the element to xml_queryresult table "+e.getLocalizedMessage());
1057
             }
1058
             finally
1059
             {
1060
                pstmt.close();
1061
             }
1062
         }
1063
        
1064
         // A string with element
1065
         String xmlElement = "  <document>" + element + "</document>";
1066

    
1067
         //send single element to output
1068
         if (out != null)
1069
         {
1070
             out.write(xmlElement);
1071
         }
1072
         resultset.append(xmlElement);
1073
     }//while
1074
     
1075
     double storeReturnFieldTime = System.currentTimeMillis() - startStoreReturnField;
1076
     long storeReturnFieldWarnLimit = 
1077
    	 Long.parseLong(PropertyService.getProperty("dbquery.storeReturnFieldTimeWarnLimit"));
1078

    
1079
     if (storeReturnFieldTime > storeReturnFieldWarnLimit) {
1080
    	 logMetacat.warn("DBQuery.handleSubsetResult - Time to store new return fields into xml_queryresult table (Part4 in return fields) "
1081
                   + storeReturnFieldTime);
1082
     }
1083
     MetacatUtil.writeDebugToFile("-----------------------------------------Insert new record to xml_queryresult(Part4 in return fields) "
1084
             + storeReturnFieldTime);
1085
     MetacatUtil.writeDebugToDelimiteredFile(" " + storeReturnFieldTime, false);
1086
     
1087
     Enumeration keysE = queryresultDocList.keys();
1088
     while (keysE.hasMoreElements())
1089
     {
1090
         key = (String) keysE.nextElement();
1091
         element = (String)queryresultDocList.get(key);
1092
         // A string with element
1093
         String xmlElement = "  <document>" + element + "</document>";
1094
         //send single element to output
1095
         if (out != null)
1096
         {
1097
             out.write(xmlElement);
1098
         }
1099
         resultset.append(xmlElement);
1100
     }//while
1101
     double returnFieldTime = System.currentTimeMillis() - startReturnFieldTime;
1102
     long totalReturnFieldWarnLimit = 
1103
    	 Long.parseLong(PropertyService.getProperty("dbquery.totalReturnFieldTimeWarnLimit"));
1104

    
1105
     if (returnFieldTime > totalReturnFieldWarnLimit) {
1106
    	 logMetacat.warn("DBQuery.handleSubsetResult - Total time to get return fields is: "
1107
                           + returnFieldTime);
1108
     }
1109
     MetacatUtil.writeDebugToFile("DBQuery.handleSubsetResult - ---------------------------------------------------------------------------------------------------------------"+
1110
    		 "Total to get return fields  " + returnFieldTime);
1111
     MetacatUtil.writeDebugToDelimiteredFile("DBQuery.handleSubsetResult - "+ returnFieldTime, false);
1112
     return resultset;
1113
 }
1114

    
1115
   /**
1116
    * Get the docids already in xml_queryresult table and corresponding
1117
    * queryresultstring as a hashtable
1118
    */
1119
   private Hashtable docidsInQueryresultTable(int returnfield_id,
1120
                                              ResultDocumentSet partOfDoclist,
1121
                                              DBConnection dbconn){
1122

    
1123
         Hashtable returnValue = new Hashtable();
1124
         PreparedStatement pstmt = null;
1125
         ResultSet rs = null;
1126
         
1127
         // keep track of parameter values
1128
         List<Object> parameterValues = new ArrayList<Object>();
1129

    
1130
         // get partOfDoclist as string for the query
1131
         Iterator keylist = partOfDoclist.getDocids();
1132
         StringBuffer doclist = new StringBuffer();
1133
         while (keylist.hasNext())
1134
         {
1135
             doclist.append("?,");
1136
             parameterValues.add((String) keylist.next());
1137
         }//while
1138

    
1139
         if (doclist.length() > 0)
1140
         {
1141
             doclist.deleteCharAt(doclist.length() - 1); //remove the last comma
1142

    
1143
             // the query to find out docids from xml_queryresult
1144
             String query = "select docid, queryresult_string from "
1145
                          + "xml_queryresult where returnfield_id = " +
1146
                          returnfield_id +" and docid in ("+ doclist + ")";
1147
             logMetacat.info("DBQuery.docidsInQueryresultTable - Query to get docids from xml_queryresult:"
1148
                                      + query);
1149

    
1150
             try {
1151
                 // prepare and execute the query
1152
                 pstmt = dbconn.prepareStatement(query);
1153
                 // bind parameter values
1154
                 pstmt = setPreparedStatementValues(parameterValues, pstmt);
1155
                 
1156
                 dbconn.increaseUsageCount(1);
1157
                 pstmt.execute();
1158
                 rs = pstmt.getResultSet();
1159
                 boolean tableHasRows = rs.next();
1160
                 while (tableHasRows) {
1161
                     // store the returned results in the returnValue hashtable
1162
                     String key = rs.getString(1);
1163
                     String element = rs.getString(2);
1164

    
1165
                     if(element != null){
1166
                         returnValue.put(key, element);
1167
                     } else {
1168
                         logMetacat.info("DBQuery.docidsInQueryresultTable - Null elment found ("
1169
                         + "DBQuery.docidsInQueryresultTable)");
1170
                     }
1171
                     tableHasRows = rs.next();
1172
                 }
1173
                 rs.close();
1174
                 pstmt.close();
1175
             } catch (Exception e){
1176
                 logMetacat.error("DBQuery.docidsInQueryresultTable - Error getting docids from "
1177
                                          + "queryresult: " + e.getMessage());
1178
              }
1179
         }
1180
         return returnValue;
1181
     }
1182

    
1183

    
1184
   /**
1185
    * Method to get id from xml_returnfield table
1186
    * for a given query specification
1187
    */
1188
   private int returnfield_id;
1189
   private int getXmlReturnfieldsTableId(QuerySpecification qspec,
1190
                                           DBConnection dbconn){
1191
       int id = -1;
1192
       int count = 1;
1193
       PreparedStatement pstmt = null;
1194
       ResultSet rs = null;
1195
       String returnfield = qspec.getSortedReturnFieldString();
1196

    
1197
       // query for finding the id from xml_returnfield
1198
       String query = "SELECT returnfield_id, usage_count FROM xml_returnfield "
1199
            + "WHERE returnfield_string LIKE ?";
1200
       logMetacat.info("DBQuery.getXmlReturnfieldsTableId - ReturnField Query:" + query);
1201

    
1202
       try {
1203
           // prepare and run the query
1204
           pstmt = dbconn.prepareStatement(query);
1205
           pstmt.setString(1,returnfield);
1206
           dbconn.increaseUsageCount(1);
1207
           pstmt.execute();
1208
           rs = pstmt.getResultSet();
1209
           boolean tableHasRows = rs.next();
1210

    
1211
           // if record found then increase the usage count
1212
           // else insert a new record and get the id of the new record
1213
           if(tableHasRows){
1214
               // get the id
1215
               id = rs.getInt(1);
1216
               count = rs.getInt(2) + 1;
1217
               rs.close();
1218
               pstmt.close();
1219

    
1220
               // increase the usage count
1221
               query = "UPDATE xml_returnfield SET usage_count = ?"
1222
                   + " WHERE returnfield_id = ?";
1223
               logMetacat.info("DBQuery.getXmlReturnfieldsTableId - ReturnField Table Update:"+ query);
1224

    
1225
               pstmt = dbconn.prepareStatement(query);
1226
               pstmt.setInt(1, count);
1227
               pstmt.setInt(2, id);
1228
               dbconn.increaseUsageCount(1);
1229
               pstmt.execute();
1230
               pstmt.close();
1231

    
1232
           } else {
1233
               rs.close();
1234
               pstmt.close();
1235

    
1236
               // insert a new record
1237
               query = "INSERT INTO xml_returnfield (returnfield_string, usage_count)"
1238
                   + "VALUES (?, '1')";
1239
               logMetacat.info("DBQuery.getXmlReturnfieldsTableId - ReturnField Table Insert:"+ query);
1240
               pstmt = dbconn.prepareStatement(query);
1241
               pstmt.setString(1, returnfield);
1242
               dbconn.increaseUsageCount(1);
1243
               pstmt.execute();
1244
               pstmt.close();
1245

    
1246
               // get the id of the new record
1247
               query = "SELECT returnfield_id FROM xml_returnfield "
1248
                   + "WHERE returnfield_string LIKE ?";
1249
               logMetacat.info("DBQuery.getXmlReturnfieldsTableId - ReturnField query after Insert:" + query);
1250
               pstmt = dbconn.prepareStatement(query);
1251
               pstmt.setString(1, returnfield);
1252

    
1253
               dbconn.increaseUsageCount(1);
1254
               pstmt.execute();
1255
               rs = pstmt.getResultSet();
1256
               if(rs.next()){
1257
                   id = rs.getInt(1);
1258
               } else {
1259
                   id = -1;
1260
               }
1261
               rs.close();
1262
               pstmt.close();
1263
           }
1264

    
1265
       } catch (Exception e){
1266
           logMetacat.error("DBQuery.getXmlReturnfieldsTableId - Error getting id from xml_returnfield in "
1267
                                     + "DBQuery.getXmlReturnfieldsTableId: "
1268
                                     + e.getMessage());
1269
           id = -1;
1270
       }
1271

    
1272
       returnfield_id = id;
1273
       return count;
1274
   }
1275

    
1276

    
1277
    /*
1278
     * A method to add return field to return doclist hash table
1279
     */
1280
    private ResultDocumentSet addReturnfield(ResultDocumentSet docListResult,
1281
                                      QuerySpecification qspec,
1282
                                      String user, String[]groups,
1283
                                      DBConnection dbconn, boolean useXMLIndex,
1284
                                      String qformat)
1285
                                      throws Exception
1286
    {
1287
      PreparedStatement pstmt = null;
1288
      ResultSet rs = null;
1289
      String docid = null;
1290
      String fieldname = null;
1291
      String fieldtype = null;
1292
      String fielddata = null;
1293
      String relation = null;
1294
      // keep track of parameter values
1295
      List<Object> parameterValues = new ArrayList<Object>();
1296

    
1297
      if (qspec.containsExtendedSQL())
1298
      {
1299
        qspec.setUserName(user);
1300
        qspec.setGroup(groups);
1301
        Vector extendedFields = new Vector(qspec.getReturnFieldList());
1302
        Vector results = new Vector();
1303
        Iterator keylist = docListResult.getDocids();
1304
        StringBuffer doclist = new StringBuffer();
1305
        List<Object> doclistValues = new ArrayList<Object>();
1306
        Vector parentidList = new Vector();
1307
        Hashtable returnFieldValue = new Hashtable();
1308
        while (keylist.hasNext())
1309
        {
1310
          String key = (String)keylist.next();
1311
          doclist.append("?,");
1312
          doclistValues.add(key);
1313
        }
1314
        if (doclist.length() > 0)
1315
        {
1316
          Hashtable controlPairs = new Hashtable();
1317
          doclist.deleteCharAt(doclist.length() - 1); //remove the last comma
1318
          boolean tableHasRows = false;
1319
        
1320

    
1321
          
1322
           String extendedQuery =
1323
               qspec.printExtendedSQL(doclist.toString(), useXMLIndex, parameterValues, doclistValues);
1324
           // DO not add doclist values -- they are included in the query
1325
           //parameterValues.addAll(doclistValues);
1326
           logMetacat.info("DBQuery.addReturnfield - Extended query: " + extendedQuery);
1327

    
1328
           if(extendedQuery != null){
1329
//        	   long extendedQueryStart = System.currentTimeMillis();
1330
               pstmt = dbconn.prepareStatement(extendedQuery);
1331
               // set the parameter values
1332
               pstmt = DBQuery.setPreparedStatementValues(parameterValues, pstmt);
1333
               //increase dbconnection usage count
1334
               dbconn.increaseUsageCount(1);
1335
               pstmt.execute();
1336
               rs = pstmt.getResultSet();
1337
               tableHasRows = rs.next();
1338
               while (tableHasRows) {
1339
                   ReturnFieldValue returnValue = new ReturnFieldValue();
1340
                   docid = rs.getString(1).trim();
1341
                   fieldname = rs.getString(2);
1342
                   
1343
                   if(qformat.toLowerCase().trim().equals("xml"))
1344
                   {
1345
                       byte[] b = rs.getBytes(3);
1346
                       fielddata = new String(b, 0, b.length, MetaCatServlet.DEFAULT_ENCODING);
1347
                   }
1348
                   else
1349
                   {
1350
                       fielddata = rs.getString(3);
1351
                   }
1352
                   
1353
                   //System.out.println("raw fielddata: " + fielddata);
1354
                   fielddata = MetacatUtil.normalize(fielddata);
1355
                   //System.out.println("normalized fielddata: " + fielddata);
1356
                   String parentId = rs.getString(4);
1357
                   fieldtype = rs.getString(5);
1358
                   StringBuffer value = new StringBuffer();
1359

    
1360
                   //handle case when usexmlindex is true differently
1361
                   //at one point merging the nodedata (for large text elements) was 
1362
                   //deemed unnecessary - but now it is needed.  but not for attribute nodes
1363
                   if (useXMLIndex || !containsKey(parentidList, parentId)) {
1364
                	   //merge node data only for non-ATTRIBUTEs
1365
                	   if (fieldtype != null && !fieldtype.equals("ATTRIBUTE")) {
1366
	                	   //try merging the data
1367
	                	   ReturnFieldValue existingRFV =
1368
	                		   getArrayValue(parentidList, parentId);
1369
	                	   if (existingRFV != null && !existingRFV.getFieldType().equals("ATTRIBUTE")) {
1370
	                		   fielddata = existingRFV.getFieldValue() + fielddata;
1371
	                	   }
1372
                	   }
1373
                	   //System.out.println("fieldname: " + fieldname + " fielddata: " + fielddata);
1374

    
1375
                       value.append("<param name=\"");
1376
                       value.append(fieldname);
1377
                       value.append("\">");
1378
                       value.append(fielddata);
1379
                       value.append("</param>");
1380
                       //set returnvalue
1381
                       returnValue.setDocid(docid);
1382
                       returnValue.setFieldValue(fielddata);
1383
                       returnValue.setFieldType(fieldtype);
1384
                       returnValue.setXMLFieldValue(value.toString());
1385
                       // Store it in hastable
1386
                       putInArray(parentidList, parentId, returnValue);
1387
                   }
1388
                   else {
1389
                       
1390
                       // need to merge nodedata if they have same parent id and
1391
                       // node type is text
1392
                       fielddata = (String) ( (ReturnFieldValue)
1393
                                             getArrayValue(
1394
                           parentidList, parentId)).getFieldValue()
1395
                           + fielddata;
1396
                       //System.out.println("fieldname: " + fieldname + " fielddata: " + fielddata);
1397
                       value.append("<param name=\"");
1398
                       value.append(fieldname);
1399
                       value.append("\">");
1400
                       value.append(fielddata);
1401
                       value.append("</param>");
1402
                       returnValue.setDocid(docid);
1403
                       returnValue.setFieldValue(fielddata);
1404
                       returnValue.setFieldType(fieldtype);
1405
                       returnValue.setXMLFieldValue(value.toString());
1406
                       // remove the old return value from paretnidList
1407
                       parentidList.remove(parentId);
1408
                       // store the new return value in parentidlit
1409
                       putInArray(parentidList, parentId, returnValue);
1410
                   }
1411
                   tableHasRows = rs.next();
1412
               } //while
1413
               rs.close();
1414
               pstmt.close();
1415

    
1416
               // put the merger node data info into doclistReult
1417
               Enumeration xmlFieldValue = (getElements(parentidList)).
1418
                   elements();
1419
               while (xmlFieldValue.hasMoreElements()) {
1420
                   ReturnFieldValue object =
1421
                       (ReturnFieldValue) xmlFieldValue.nextElement();
1422
                   docid = object.getDocid();
1423
                   if (docListResult.containsDocid(docid)) {
1424
                       String removedelement = (String) docListResult.
1425
                           remove(docid);
1426
                       docListResult.
1427
                           addResultDocument(new ResultDocument(docid,
1428
                               removedelement + object.getXMLFieldValue()));
1429
                   }
1430
                   else {
1431
                       docListResult.addResultDocument(
1432
                         new ResultDocument(docid, object.getXMLFieldValue()));
1433
                   }
1434
               } //while
1435
//               double docListResultEnd = System.currentTimeMillis() / 1000;
1436
//               logMetacat.warn(
1437
//                   "Time to prepare ResultDocumentSet after"
1438
//                   + " execute extended query: "
1439
//                   + (docListResultEnd - extendedQueryEnd));
1440
           }
1441
       }//if doclist lenght is great than zero
1442
     }//if has extended query
1443

    
1444
      return docListResult;
1445
    }//addReturnfield
1446

    
1447
  
1448
  /**
1449
   * removes the <?xml version="1.0"?> tag from the beginning.  This takes a
1450
   * string as a param instead of a hashtable.
1451
   *
1452
   * @param xmlquery a string representing a query.
1453
   */
1454
   private  String transformQuery(String xmlquery)
1455
   {
1456
     xmlquery = xmlquery.trim();
1457
     int index = xmlquery.indexOf("?>");
1458
     if (index != -1)
1459
     {
1460
       return xmlquery.substring(index + 2, xmlquery.length());
1461
     }
1462
     else
1463
     {
1464
       return xmlquery;
1465
     }
1466
   }
1467
   
1468
   /*
1469
    * Method to store query string and result xml string into query result
1470
    * cache. If the size alreay reache the limitation, the cache will be
1471
    * cleared first, then store them.
1472
    */
1473
   private void storeQueryResultIntoCache(String query, String resultXML)
1474
   {
1475
	   synchronized (queryResultCache)
1476
	   {
1477
		   if (queryResultCache.size() >= QUERYRESULTCACHESIZE)
1478
		   {
1479
			   queryResultCache.clear();
1480
		   }
1481
		   queryResultCache.put(query, resultXML);
1482
		   
1483
	   }
1484
   }
1485
   
1486
   /*
1487
    * Method to get result xml string from query result cache. 
1488
    * Note: the returned string can be null.
1489
    */
1490
   private String getResultXMLFromCache(String query)
1491
   {
1492
	   String resultSet = null;
1493
	   synchronized (queryResultCache)
1494
	   {
1495
          try
1496
          {
1497
        	 logMetacat.info("DBQuery.getResultXMLFromCache - Get query from cache");
1498
		     resultSet = (String)queryResultCache.get(query);
1499
		   
1500
          }
1501
          catch (Exception e)
1502
          {
1503
        	  resultSet = null;
1504
          }
1505
		   
1506
	   }
1507
	   return resultSet;
1508
   }
1509
   
1510
   /**
1511
    * Method to clear the query result cache.
1512
    */
1513
   public static void clearQueryResultCache()
1514
   {
1515
	   synchronized (queryResultCache)
1516
	   {
1517
		   queryResultCache.clear();
1518
	   }
1519
   }
1520
   
1521
   /**
1522
    * Set the parameter values in the prepared statement using instrospection
1523
    * of the given value objects
1524
    * @param parameterValues
1525
    * @param pstmt
1526
    * @return
1527
    * @throws SQLException
1528
    */
1529
   public static PreparedStatement setPreparedStatementValues(List<Object> parameterValues, PreparedStatement pstmt) throws SQLException {
1530
	   // set all the values we have collected 
1531
      int parameterIndex = 1;
1532
      for (Object parameterValue: parameterValues) {
1533
    	  if (parameterValue instanceof String) {
1534
    		  pstmt.setString(parameterIndex, (String) parameterValue);
1535
    	  }
1536
    	  else if (parameterValue instanceof Integer) {
1537
    		  pstmt.setInt(parameterIndex, (Integer) parameterValue);
1538
    	  }
1539
    	  else if (parameterValue instanceof Float) {
1540
    		  pstmt.setFloat(parameterIndex, (Float) parameterValue);
1541
    	  }
1542
    	  else if (parameterValue instanceof Double) {
1543
    		  pstmt.setDouble(parameterIndex, (Double) parameterValue);
1544
    	  }
1545
    	  else if (parameterValue instanceof Date) {
1546
    		  pstmt.setTimestamp(parameterIndex, new Timestamp(((Date) parameterValue).getTime()));
1547
    	  }
1548
    	  else {
1549
    		  pstmt.setObject(parameterIndex, parameterValue);
1550
    	  }
1551
    	  parameterIndex++;
1552
      }
1553
      return pstmt;
1554
   }
1555

    
1556

    
1557
    /*
1558
     * A method to search if Vector contains a particular key string
1559
     */
1560
    private boolean containsKey(Vector parentidList, String parentId)
1561
    {
1562

    
1563
        Vector tempVector = null;
1564

    
1565
        for (int count = 0; count < parentidList.size(); count++) {
1566
            tempVector = (Vector) parentidList.get(count);
1567
            if (parentId.compareTo((String) tempVector.get(0)) == 0) { return true; }
1568
        }
1569
        return false;
1570
    }
1571
    
1572
    /*
1573
     * A method to put key and value in Vector
1574
     */
1575
    private void putInArray(Vector parentidList, String key,
1576
            ReturnFieldValue value)
1577
    {
1578

    
1579
        Vector tempVector = null;
1580
        //only filter if the field type is NOT an attribute (say, for text)
1581
        String fieldType = value.getFieldType();
1582
        if (fieldType != null && !fieldType.equals("ATTRIBUTE")) {
1583
        
1584
	        for (int count = 0; count < parentidList.size(); count++) {
1585
	            tempVector = (Vector) parentidList.get(count);
1586
	
1587
	            if (key.compareTo((String) tempVector.get(0)) == 0) {
1588
	                tempVector.remove(1);
1589
	                tempVector.add(1, value);
1590
	                return;
1591
	            }
1592
	        }
1593
        }
1594

    
1595
        tempVector = new Vector();
1596
        tempVector.add(0, key);
1597
        tempVector.add(1, value);
1598
        parentidList.add(tempVector);
1599
        return;
1600
    }
1601

    
1602
    /*
1603
     * A method to get value in Vector given a key
1604
     */
1605
    private ReturnFieldValue getArrayValue(Vector parentidList, String key)
1606
    {
1607

    
1608
        Vector tempVector = null;
1609

    
1610
        for (int count = 0; count < parentidList.size(); count++) {
1611
            tempVector = (Vector) parentidList.get(count);
1612

    
1613
            if (key.compareTo((String) tempVector.get(0)) == 0) { return (ReturnFieldValue) tempVector
1614
                    .get(1); }
1615
        }
1616
        return null;
1617
    }
1618

    
1619
    /*
1620
     * A method to get enumeration of all values in Vector
1621
     */
1622
    private Vector getElements(Vector parentidList)
1623
    {
1624
        Vector enumVector = new Vector();
1625
        Vector tempVector = null;
1626

    
1627
        for (int count = 0; count < parentidList.size(); count++) {
1628
            tempVector = (Vector) parentidList.get(count);
1629

    
1630
            enumVector.add(tempVector.get(1));
1631
        }
1632
        return enumVector;
1633
    }
1634

    
1635
  
1636

    
1637
    /*
1638
     * A method to create a query to get owner's docid list
1639
     */
1640
    private String getOwnerQuery(String owner, List<Object> parameterValues)
1641
    {
1642
        if (owner != null) {
1643
            owner = owner.toLowerCase();
1644
        }
1645
        StringBuffer self = new StringBuffer();
1646

    
1647
        self.append("SELECT docid,docname,doctype,");
1648
        self.append("date_created, date_updated, rev ");
1649
        self.append("FROM xml_documents WHERE docid IN (");
1650
        self.append("(");
1651
        self.append("SELECT DISTINCT docid FROM xml_nodes WHERE \n");
1652
        self.append("nodedata LIKE '%%%' ");
1653
        self.append(") \n");
1654
        self.append(") ");
1655
        self.append(" AND (");
1656
        self.append(" lower(user_owner) = ?");
1657
        self.append(") ");
1658
        parameterValues.add(owner);
1659
        return self.toString();
1660
    }
1661

    
1662
    /**
1663
     * format a structured query as an XML document that conforms to the
1664
     * pathquery.dtd and is appropriate for submission to the DBQuery
1665
     * structured query engine
1666
     *
1667
     * @param params The list of parameters that should be included in the
1668
     *            query
1669
     */
1670
    public static String createSQuery(Hashtable params) throws PropertyNotFoundException
1671
    {
1672
        StringBuffer query = new StringBuffer();
1673
        Enumeration elements;
1674
        Enumeration keys;
1675
        String filterDoctype = null;
1676
        String casesensitive = null;
1677
        String searchmode = null;
1678
        Object nextkey;
1679
        Object nextelement;
1680
        //add the xml headers
1681
        query.append("<?xml version=\"1.0\"?>\n");
1682
        query.append("<pathquery version=\"1.2\">\n");
1683

    
1684

    
1685

    
1686
        if (params.containsKey("meta_file_id")) {
1687
            query.append("<meta_file_id>");
1688
            query.append(((String[]) params.get("meta_file_id"))[0]);
1689
            query.append("</meta_file_id>");
1690
        }
1691

    
1692
        if (params.containsKey("returndoctype")) {
1693
            String[] returnDoctypes = ((String[]) params.get("returndoctype"));
1694
            for (int i = 0; i < returnDoctypes.length; i++) {
1695
                String doctype = (String) returnDoctypes[i];
1696

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

    
1705
        if (params.containsKey("filterdoctype")) {
1706
            String[] filterDoctypes = ((String[]) params.get("filterdoctype"));
1707
            for (int i = 0; i < filterDoctypes.length; i++) {
1708
                query.append("<filterdoctype>").append(filterDoctypes[i]);
1709
                query.append("</filterdoctype>");
1710
            }
1711
        }
1712

    
1713
        if (params.containsKey("returnfield")) {
1714
            String[] returnfield = ((String[]) params.get("returnfield"));
1715
            for (int i = 0; i < returnfield.length; i++) {
1716
                query.append("<returnfield>").append(returnfield[i]);
1717
                query.append("</returnfield>");
1718
            }
1719
        }
1720

    
1721
        if (params.containsKey("owner")) {
1722
            String[] owner = ((String[]) params.get("owner"));
1723
            for (int i = 0; i < owner.length; i++) {
1724
                query.append("<owner>").append(owner[i]);
1725
                query.append("</owner>");
1726
            }
1727
        }
1728

    
1729
        if (params.containsKey("site")) {
1730
            String[] site = ((String[]) params.get("site"));
1731
            for (int i = 0; i < site.length; i++) {
1732
                query.append("<site>").append(site[i]);
1733
                query.append("</site>");
1734
            }
1735
        }
1736

    
1737
        //allows the dynamic switching of boolean operators
1738
        if (params.containsKey("operator")) {
1739
            query.append("<querygroup operator=\""
1740
                    + ((String[]) params.get("operator"))[0] + "\">");
1741
        } else { //the default operator is UNION
1742
            query.append("<querygroup operator=\"UNION\">");
1743
        }
1744

    
1745
        if (params.containsKey("casesensitive")) {
1746
            casesensitive = ((String[]) params.get("casesensitive"))[0];
1747
        } else {
1748
            casesensitive = "false";
1749
        }
1750

    
1751
        if (params.containsKey("searchmode")) {
1752
            searchmode = ((String[]) params.get("searchmode"))[0];
1753
        } else {
1754
            searchmode = "contains";
1755
        }
1756

    
1757
        //anyfield is a special case because it does a
1758
        //free text search. It does not have a <pathexpr>
1759
        //tag. This allows for a free text search within the structured
1760
        //query. This is useful if the INTERSECT operator is used.
1761
        if (params.containsKey("anyfield")) {
1762
            String[] anyfield = ((String[]) params.get("anyfield"));
1763
            //allow for more than one value for anyfield
1764
            for (int i = 0; i < anyfield.length; i++) {
1765
                if (anyfield[i] != null && !anyfield[i].equals("")) {
1766
                    query.append("<queryterm casesensitive=\"" + casesensitive
1767
                            + "\" " + "searchmode=\"" + searchmode
1768
                            + "\"><value>" 
1769
                            + StringEscapeUtils.escapeXml(anyfield[i])
1770
                            + "</value></queryterm>");
1771
                }
1772
            }
1773
        }
1774

    
1775
        //this while loop finds the rest of the parameters
1776
        //and attempts to query for the field specified
1777
        //by the parameter.
1778
        elements = params.elements();
1779
        keys = params.keys();
1780
        while (keys.hasMoreElements() && elements.hasMoreElements()) {
1781
            nextkey = keys.nextElement();
1782
            nextelement = elements.nextElement();
1783

    
1784
            //make sure we aren't querying for any of these
1785
            //parameters since the are already in the query
1786
            //in one form or another.
1787
            Vector ignoredParams = new Vector();
1788
            ignoredParams.add("returndoctype");
1789
            ignoredParams.add("filterdoctype");
1790
            ignoredParams.add("action");
1791
            ignoredParams.add("qformat");
1792
            ignoredParams.add("anyfield");
1793
            ignoredParams.add("returnfield");
1794
            ignoredParams.add("owner");
1795
            ignoredParams.add("site");
1796
            ignoredParams.add("operator");
1797
            ignoredParams.add("sessionid");
1798
            ignoredParams.add("pagesize");
1799
            ignoredParams.add("pagestart");
1800
            ignoredParams.add("searchmode");
1801

    
1802
            // Also ignore parameters listed in the properties file
1803
            // so that they can be passed through to stylesheets
1804
            String paramsToIgnore = PropertyService
1805
                    .getProperty("database.queryignoredparams");
1806
            StringTokenizer st = new StringTokenizer(paramsToIgnore, ",");
1807
            while (st.hasMoreTokens()) {
1808
                ignoredParams.add(st.nextToken());
1809
            }
1810
            if (!ignoredParams.contains(nextkey.toString())) {
1811
                //allow for more than value per field name
1812
                for (int i = 0; i < ((String[]) nextelement).length; i++) {
1813
                    if (!((String[]) nextelement)[i].equals("")) {
1814
                        query.append("<queryterm casesensitive=\""
1815
                                + casesensitive + "\" " + "searchmode=\""
1816
                                + searchmode + "\">" + "<value>" +
1817
                                //add the query value
1818
                                StringEscapeUtils.escapeXml(((String[]) nextelement)[i])
1819
                                + "</value><pathexpr>" +
1820
                                //add the path to query by
1821
                                nextkey.toString() + "</pathexpr></queryterm>");
1822
                    }
1823
                }
1824
            }
1825
        }
1826
        query.append("</querygroup></pathquery>");
1827
        //append on the end of the xml and return the result as a string
1828
        return query.toString();
1829
    }
1830

    
1831
    /**
1832
     * format a simple free-text value query as an XML document that conforms
1833
     * to the pathquery.dtd and is appropriate for submission to the DBQuery
1834
     * structured query engine
1835
     *
1836
     * @param value the text string to search for in the xml catalog
1837
     * @param doctype the type of documents to include in the result set -- use
1838
     *            "any" or "ANY" for unfiltered result sets
1839
     */
1840
    public static String createQuery(String value, String doctype)
1841
    {
1842
        StringBuffer xmlquery = new StringBuffer();
1843
        xmlquery.append("<?xml version=\"1.0\"?>\n");
1844
        xmlquery.append("<pathquery version=\"1.0\">");
1845

    
1846
        if (!doctype.equals("any") && !doctype.equals("ANY")) {
1847
            xmlquery.append("<returndoctype>");
1848
            xmlquery.append(doctype).append("</returndoctype>");
1849
        }
1850

    
1851
        xmlquery.append("<querygroup operator=\"UNION\">");
1852
        //chad added - 8/14
1853
        //the if statement allows a query to gracefully handle a null
1854
        //query. Without this if a nullpointerException is thrown.
1855
        if (!value.equals("")) {
1856
            xmlquery.append("<queryterm casesensitive=\"false\" ");
1857
            xmlquery.append("searchmode=\"contains\">");
1858
            xmlquery.append("<value>").append(value).append("</value>");
1859
            xmlquery.append("</queryterm>");
1860
        }
1861
        xmlquery.append("</querygroup>");
1862
        xmlquery.append("</pathquery>");
1863

    
1864
        return (xmlquery.toString());
1865
    }
1866

    
1867
    /**
1868
     * format a simple free-text value query as an XML document that conforms
1869
     * to the pathquery.dtd and is appropriate for submission to the DBQuery
1870
     * structured query engine
1871
     *
1872
     * @param value the text string to search for in the xml catalog
1873
     */
1874
    public static String createQuery(String value)
1875
    {
1876
        return createQuery(value, "any");
1877
    }
1878

    
1879
    /**
1880
     * Check for "READ" permission on @docid for @user and/or @group from DB
1881
     * connection
1882
     */
1883
    private boolean hasPermission(String user, String[] groups, String docid)
1884
            throws SQLException, Exception
1885
    {
1886
        // Check for READ permission on @docid for @user and/or @groups
1887
        PermissionController controller = new PermissionController(docid);
1888
        return controller.hasPermission(user, groups,
1889
                AccessControlInterface.READSTRING);
1890
    }
1891

    
1892
    /**
1893
     * Get all docIds list for a data packadge
1894
     *
1895
     * @param dataPackageDocid, the string in docId field of xml_relation table
1896
     */
1897
    private Vector getCurrentDocidListForDataPackage(String dataPackageDocid)
1898
    {
1899
        DBConnection dbConn = null;
1900
        int serialNumber = -1;
1901
        Vector docIdList = new Vector();//return value
1902
        PreparedStatement pStmt = null;
1903
        ResultSet rs = null;
1904
        String docIdInSubjectField = null;
1905
        String docIdInObjectField = null;
1906

    
1907
        // Check the parameter
1908
        if (dataPackageDocid == null || dataPackageDocid.equals("")) { return docIdList; }//if
1909

    
1910
        //the query stirng
1911
        String query = "SELECT subject, object from xml_relation where docId = ?";
1912
        try {
1913
            dbConn = DBConnectionPool
1914
                    .getDBConnection("DBQuery.getCurrentDocidListForDataPackage");
1915
            serialNumber = dbConn.getCheckOutSerialNumber();
1916
            pStmt = dbConn.prepareStatement(query);
1917
            //bind the value to query
1918
            pStmt.setString(1, dataPackageDocid);
1919

    
1920
            //excute the query
1921
            pStmt.execute();
1922
            //get the result set
1923
            rs = pStmt.getResultSet();
1924
            //process the result
1925
            while (rs.next()) {
1926
                //In order to get the whole docIds in a data packadge,
1927
                //we need to put the docIds of subject and object field in
1928
                // xml_relation
1929
                //into the return vector
1930
                docIdInSubjectField = rs.getString(1);//the result docId in
1931
                                                      // subject field
1932
                docIdInObjectField = rs.getString(2);//the result docId in
1933
                                                     // object field
1934

    
1935
                //don't put the duplicate docId into the vector
1936
                if (!docIdList.contains(docIdInSubjectField)) {
1937
                    docIdList.add(docIdInSubjectField);
1938
                }
1939

    
1940
                //don't put the duplicate docId into the vector
1941
                if (!docIdList.contains(docIdInObjectField)) {
1942
                    docIdList.add(docIdInObjectField);
1943
                }
1944
            }//while
1945
            //close the pStmt
1946
            pStmt.close();
1947
        }//try
1948
        catch (SQLException e) {
1949
            logMetacat.error("DBQuery.getCurrentDocidListForDataPackage - Error in getDocidListForDataPackage: "
1950
                    + e.getMessage());
1951
        }//catch
1952
        finally {
1953
            try {
1954
                pStmt.close();
1955
            }//try
1956
            catch (SQLException ee) {
1957
                logMetacat.error("DBQuery.getCurrentDocidListForDataPackage - SQL Error: "
1958
                                + ee.getMessage());
1959
            }//catch
1960
            finally {
1961
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1962
            }//fianlly
1963
        }//finally
1964
        return docIdList;
1965
    }//getCurrentDocidListForDataPackadge()
1966

    
1967
    /**
1968
     * Get all docIds list for a data packadge
1969
     *
1970
     * @param dataPackageDocid, the string in docId field of xml_relation table
1971
     */
1972
    private Vector getOldVersionDocidListForDataPackage(String dataPackageDocidWithRev)
1973
    {
1974

    
1975
        Vector docIdList = new Vector();//return value
1976
        Vector tripleList = null;
1977
        String xml = null;
1978

    
1979
        // Check the parameter
1980
        if (dataPackageDocidWithRev == null || dataPackageDocidWithRev.equals("")) { return docIdList; }//if
1981

    
1982
        try {
1983
            //initial a documentImpl object
1984
            DocumentImpl packageDocument = new DocumentImpl(dataPackageDocidWithRev);
1985
            //transfer to documentImpl object to string
1986
            xml = packageDocument.toString();
1987

    
1988
            //create a tripcollection object
1989
            TripleCollection tripleForPackage = new TripleCollection(
1990
                    new StringReader(xml));
1991
            //get the vetor of triples
1992
            tripleList = tripleForPackage.getCollection();
1993

    
1994
            for (int i = 0; i < tripleList.size(); i++) {
1995
                //put subject docid into docIdlist without duplicate
1996
                if (!docIdList.contains(((Triple) tripleList.elementAt(i))
1997
                        .getSubject())) {
1998
                    //put subject docid into docIdlist
1999
                    docIdList.add(((Triple) tripleList.get(i)).getSubject());
2000
                }
2001
                //put object docid into docIdlist without duplicate
2002
                if (!docIdList.contains(((Triple) tripleList.elementAt(i))
2003
                        .getObject())) {
2004
                    docIdList.add(((Triple) (tripleList.get(i))).getObject());
2005
                }
2006
            }//for
2007
        }//try
2008
        catch (Exception e) {
2009
            logMetacat.error("DBQuery.getCurrentDocidListForDataPackage - General error: "
2010
                    + e.getMessage());
2011
        }//catch
2012

    
2013
        // return result
2014
        return docIdList;
2015
    }//getDocidListForPackageInXMLRevisions()
2016

    
2017
    /**
2018
     * Check if the docId is a data packadge id. If the id is a data packadage
2019
     * id, it should be store in the docId fields in xml_relation table. So we
2020
     * can use a query to get the entries which the docId equals the given
2021
     * value. If the result is null. The docId is not a packadge id. Otherwise,
2022
     * it is.
2023
     *
2024
     * @param docId, the id need to be checked
2025
     */
2026
    private boolean isDataPackageId(String docId)
2027
    {
2028
        boolean result = false;
2029
        PreparedStatement pStmt = null;
2030
        ResultSet rs = null;
2031
        String query = "SELECT docId from xml_relation where docId = ?";
2032
        DBConnection dbConn = null;
2033
        int serialNumber = -1;
2034
        try {
2035
            dbConn = DBConnectionPool
2036
                    .getDBConnection("DBQuery.isDataPackageId");
2037
            serialNumber = dbConn.getCheckOutSerialNumber();
2038
            pStmt = dbConn.prepareStatement(query);
2039
            //bind the value to query
2040
            pStmt.setString(1, docId);
2041
            //execute the query
2042
            pStmt.execute();
2043
            rs = pStmt.getResultSet();
2044
            //process the result
2045
            if (rs.next()) //There are some records for the id in docId fields
2046
            {
2047
                result = true;//It is a data packadge id
2048
            }
2049
            pStmt.close();
2050
        }//try
2051
        catch (SQLException e) {
2052
            logMetacat.error("DBQuery.isDataPackageId - SQL Error: "
2053
                    + e.getMessage());
2054
        } finally {
2055
            try {
2056
                pStmt.close();
2057
            }//try
2058
            catch (SQLException ee) {
2059
                logMetacat.error("DBQuery.isDataPackageId - SQL Error in isDataPackageId: "
2060
                        + ee.getMessage());
2061
            }//catch
2062
            finally {
2063
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
2064
            }//finally
2065
        }//finally
2066
        return result;
2067
    }//isDataPackageId()
2068

    
2069
    public String getOperator() {
2070
		return operator;
2071
	}
2072

    
2073
    /**
2074
     * Specifies if and how docid overrides should be included in the general query
2075
     * @param operator null, UNION, or INTERSECT (see QueryGroup)
2076
     */
2077
	public void setOperator(String operator) {
2078
		this.operator = operator;
2079
	}
2080

    
2081
	public String getQformat() {
2082
		return qformat;
2083
	}
2084

    
2085
	public void setQformat(String qformat) {
2086
		this.qformat = qformat;
2087
	}
2088

    
2089
	/**
2090
     * Check if the user has the permission to export data package
2091
     *
2092
     * @param conn, the connection
2093
     * @param docId, the id need to be checked
2094
     * @param user, the name of user
2095
     * @param groups, the user's group
2096
     */
2097
    private boolean hasPermissionToExportPackage(String docId, String user,
2098
            String[] groups) throws Exception
2099
    {
2100
        //DocumentImpl doc=new DocumentImpl(conn,docId);
2101
        return DocumentImpl.hasReadPermission(user, groups, docId);
2102
    }
2103

    
2104
    /**
2105
     * Get the current Rev for a docid in xml_documents table
2106
     *
2107
     * @param docId, the id need to get version numb If the return value is -5,
2108
     *            means no value in rev field for this docid
2109
     */
2110
    private int getCurrentRevFromXMLDoumentsTable(String docId)
2111
            throws SQLException
2112
    {
2113
        int rev = -5;
2114
        PreparedStatement pStmt = null;
2115
        ResultSet rs = null;
2116
        String query = "SELECT rev from xml_documents where docId = ?";
2117
        DBConnection dbConn = null;
2118
        int serialNumber = -1;
2119
        try {
2120
            dbConn = DBConnectionPool
2121
                    .getDBConnection("DBQuery.getCurrentRevFromXMLDocumentsTable");
2122
            serialNumber = dbConn.getCheckOutSerialNumber();
2123
            pStmt = dbConn.prepareStatement(query);
2124
            //bind the value to query
2125
            pStmt.setString(1, docId);
2126
            //execute the query
2127
            pStmt.execute();
2128
            rs = pStmt.getResultSet();
2129
            //process the result
2130
            if (rs.next()) //There are some records for rev
2131
            {
2132
                rev = rs.getInt(1);
2133
                ;//It is the version for given docid
2134
            } else {
2135
                rev = -5;
2136
            }
2137

    
2138
        }//try
2139
        catch (SQLException e) {
2140
            logMetacat.error("DBQuery.getCurrentRevFromXMLDoumentsTable - SQL Error: "
2141
                            + e.getMessage());
2142
            throw e;
2143
        }//catch
2144
        finally {
2145
            try {
2146
                pStmt.close();
2147
            }//try
2148
            catch (SQLException ee) {
2149
                logMetacat.error(
2150
                        "DBQuery.getCurrentRevFromXMLDoumentsTable - SQL Error: "
2151
                                + ee.getMessage());
2152
            }//catch
2153
            finally {
2154
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
2155
            }//finally
2156
        }//finally
2157
        return rev;
2158
    }//getCurrentRevFromXMLDoumentsTable
2159

    
2160
    /**
2161
     * put a doc into a zip output stream
2162
     *
2163
     * @param docImpl, docmentImpl object which will be sent to zip output
2164
     *            stream
2165
     * @param zipOut, zip output stream which the docImpl will be put
2166
     * @param packageZipEntry, the zip entry name for whole package
2167
     */
2168
    private void addDocToZipOutputStream(DocumentImpl docImpl,
2169
            ZipOutputStream zipOut, String packageZipEntry)
2170
            throws ClassNotFoundException, IOException, SQLException,
2171
            McdbException, Exception
2172
    {
2173
        byte[] byteString = null;
2174
        ZipEntry zEntry = null;
2175

    
2176
        byteString = docImpl.getBytes();
2177
        //use docId as the zip entry's name
2178
        String fullDocId = docImpl.getDocID() + PropertyService.getProperty("document.accNumSeparator") + docImpl.getRev();
2179
		zEntry = new ZipEntry(packageZipEntry + "/metadata/" + fullDocId );
2180
        zEntry.setSize(byteString.length);
2181
        zipOut.putNextEntry(zEntry);
2182
        zipOut.write(byteString, 0, byteString.length);
2183
        zipOut.closeEntry();
2184

    
2185
    }//addDocToZipOutputStream()
2186

    
2187
    /**
2188
     * Transfer a docid vetor to a documentImpl vector. The documentImpl vetor
2189
     * only inlcudes current version. If a DocumentImple object couldn't find
2190
     * for a docid, then the String of this docid was added to vetor rather
2191
     * than DocumentImple object.
2192
     *
2193
     * @param docIdList, a vetor hold a docid list for a data package. In
2194
     *            docid, there is not version number in it.
2195
     */
2196

    
2197
    private Vector getCurrentAllDocumentImpl(Vector docIdList)
2198
            throws McdbException, Exception
2199
    {
2200
        //Connection dbConn=null;
2201
        Vector documentImplList = new Vector();
2202
        int rev = 0;
2203

    
2204
        // Check the parameter
2205
        if (docIdList.isEmpty()) { return documentImplList; }//if
2206

    
2207
        //for every docid in vector
2208
        for (int i = 0; i < docIdList.size(); i++) {
2209
            try {
2210
				//get newest version for this docId
2211
                String smartDocid = DocumentUtil.getSmartDocId((String) docIdList.elementAt(i));
2212
                rev = getCurrentRevFromXMLDoumentsTable(smartDocid);
2213

    
2214
                // There is no record for this docId in xml_documents table
2215
                if (rev == -5) {
2216
                    // Rather than put DocumentImple object, put a String
2217
                    // Object(docid)
2218
                    // into the documentImplList
2219
                    documentImplList.add((String) docIdList.elementAt(i));
2220
                    // Skip other code
2221
                    continue;
2222
                }
2223

    
2224
                String docidPlusVersion = smartDocid
2225
                        + PropertyService.getProperty("document.accNumSeparator") + rev;
2226

    
2227
                //create new documentImpl object
2228
                DocumentImpl documentImplObject = new DocumentImpl(
2229
                        docidPlusVersion);
2230
                //add them to vector
2231
                documentImplList.add(documentImplObject);
2232
            }//try
2233
            catch (Exception e) {
2234
                logMetacat.error("DBQuery.getCurrentAllDocumentImpl - General error: "
2235
                        + e.getMessage());
2236
                // continue the for loop
2237
                continue;
2238
            }
2239
        }//for
2240
        return documentImplList;
2241
    }
2242

    
2243
    /**
2244
     * Transfer a docid vetor to a documentImpl vector. If a DocumentImple
2245
     * object couldn't find for a docid, then the String of this docid was
2246
     * added to vetor rather than DocumentImple object.
2247
     *
2248
     * @param docIdList, a vetor hold a docid list for a data package. In
2249
     *            docid, t here is version number in it.
2250
     */
2251
    private Vector getOldVersionAllDocumentImpl(Vector docIdList)
2252
    {
2253
        //Connection dbConn=null;
2254
        Vector documentImplList = new Vector();
2255
        String siteCode = null;
2256
        String uniqueId = null;
2257
        int rev = 0;
2258

    
2259
        // Check the parameter
2260
        if (docIdList.isEmpty()) { return documentImplList; }//if
2261

    
2262
        //for every docid in vector
2263
        for (int i = 0; i < docIdList.size(); i++) {
2264

    
2265
            String docidPlusVersion = (String) (docIdList.elementAt(i));
2266

    
2267
            try {
2268
                //create new documentImpl object
2269
                DocumentImpl documentImplObject = new DocumentImpl(
2270
                        docidPlusVersion);
2271
                //add them to vector
2272
                documentImplList.add(documentImplObject);
2273
            }//try
2274
            catch (McdbDocNotFoundException notFoundE) {
2275
                logMetacat.error("DBQuery.getOldVersionAllDocument - Error finding doc " 
2276
                		+ docidPlusVersion + " : " + notFoundE.getMessage());
2277
                // Rather than add a DocumentImple object into vetor, a String
2278
                // object
2279
                // - the doicd was added to the vector
2280
                documentImplList.add(docidPlusVersion);
2281
                // Continue the for loop
2282
                continue;
2283
            }//catch
2284
            catch (Exception e) {
2285
                logMetacat.error(
2286
                        "DBQuery.getOldVersionAllDocument - General error: "
2287
                                + e.getMessage());
2288
                // Continue the for loop
2289
                continue;
2290
            }//catch
2291

    
2292
        }//for
2293
        return documentImplList;
2294
    }//getOldVersionAllDocumentImple
2295

    
2296
    /**
2297
     * put a data file into a zip output stream
2298
     *
2299
     * @param docImpl, docmentImpl object which will be sent to zip output
2300
     *            stream
2301
     * @param zipOut, the zip output stream which the docImpl will be put
2302
     * @param packageZipEntry, the zip entry name for whole package
2303
     */
2304
    private void addDataFileToZipOutputStream(DocumentImpl docImpl,
2305
            ZipOutputStream zipOut, String packageZipEntry)
2306
            throws ClassNotFoundException, IOException, SQLException,
2307
            McdbException, Exception
2308
    {
2309
        byte[] byteString = null;
2310
        ZipEntry zEntry = null;
2311
        // this is data file; add file to zip
2312
        String filePath = PropertyService.getProperty("application.datafilepath");
2313
        if (!filePath.endsWith("/")) {
2314
            filePath += "/";
2315
        }
2316
        String fileName = docImpl.getDocID() + PropertyService.getProperty("document.accNumSeparator") + docImpl.getRev();
2317
        String entityName = docImpl.getDocname();
2318
        filePath = filePath + fileName;
2319
        zEntry = new ZipEntry(packageZipEntry + "/data/" + fileName + "-" + entityName);
2320
        zipOut.putNextEntry(zEntry);
2321
        FileInputStream fin = null;
2322
        try {
2323
            fin = new FileInputStream(filePath);
2324
            byte[] buf = new byte[4 * 1024]; // 4K buffer
2325
            int b = fin.read(buf);
2326
            while (b != -1) {
2327
                zipOut.write(buf, 0, b);
2328
                b = fin.read(buf);
2329
            }//while
2330
            zipOut.closeEntry();
2331
        }//try
2332
        catch (IOException ioe) {
2333
            logMetacat.error("DBQuery.addDataFileToZipOutputStream - I/O error: "
2334
                    + ioe.getMessage());
2335
        }//catch
2336
    }//addDataFileToZipOutputStream()
2337

    
2338
    /**
2339
     * create a html summary for data package and put it into zip output stream
2340
     *
2341
     * @param docImplList, the documentImpl ojbects in data package
2342
     * @param zipOut, the zip output stream which the html should be put
2343
     * @param packageZipEntry, the zip entry name for whole package
2344
     */
2345
    private void addHtmlSummaryToZipOutputStream(Vector docImplList,
2346
            ZipOutputStream zipOut, String packageZipEntry) throws Exception
2347
    {
2348
        StringBuffer htmlDoc = new StringBuffer();
2349
        ZipEntry zEntry = null;
2350
        byte[] byteString = null;
2351
        InputStream source;
2352
        DBTransform xmlToHtml;
2353

    
2354
        //create a DBTransform ojbect
2355
        xmlToHtml = new DBTransform();
2356
        //head of html
2357
        htmlDoc.append("<html><head></head><body>");
2358
        for (int i = 0; i < docImplList.size(); i++) {
2359
            // If this String object, this means it is missed data file
2360
            if ((((docImplList.elementAt(i)).getClass()).toString())
2361
                    .equals("class java.lang.String")) {
2362

    
2363
                htmlDoc.append("<a href=\"");
2364
                String dataFileid = (String) docImplList.elementAt(i);
2365
                htmlDoc.append("./data/").append(dataFileid).append("\">");
2366
                htmlDoc.append("Data File: ");
2367
                htmlDoc.append(dataFileid).append("</a><br>");
2368
                htmlDoc.append("<br><hr><br>");
2369

    
2370
            }//if
2371
            else if ((((DocumentImpl) docImplList.elementAt(i)).getDoctype())
2372
                    .compareTo("BIN") != 0) { //this is an xml file so we can
2373
                                              // transform it.
2374
                //transform each file individually then concatenate all of the
2375
                //transformations together.
2376

    
2377
                //for metadata xml title
2378
                htmlDoc.append("<h2>");
2379
                htmlDoc.append(((DocumentImpl) docImplList.elementAt(i))
2380
                        .getDocID());
2381
                //htmlDoc.append(".");
2382
                //htmlDoc.append(((DocumentImpl)docImplList.elementAt(i)).getRev());
2383
                htmlDoc.append("</h2>");
2384
                //do the actual transform
2385
                Writer docString = new StringWriter();
2386
                xmlToHtml.transformXMLDocument(
2387
                		((DocumentImpl) docImplList.elementAt(i)).toString(), 
2388
                		((DocumentImpl) docImplList.elementAt(i)).getDoctype(), //"-//NCEAS//eml-generic//EN",
2389
                        "-//W3C//HTML//EN", 
2390
                        qformat,
2391
                        docString, 
2392
                        null, 
2393
                        null);
2394
                htmlDoc.append(docString.toString());
2395
                htmlDoc.append("<br><br><hr><br><br>");
2396
            }//if
2397
            else { //this is a data file so we should link to it in the html
2398
                htmlDoc.append("<a href=\"");
2399
                String dataFileid = ((DocumentImpl) docImplList.elementAt(i))
2400
                        .getDocID();
2401
                htmlDoc.append("./data/").append(dataFileid).append("\">");
2402
                htmlDoc.append("Data File: ");
2403
                htmlDoc.append(dataFileid).append("</a><br>");
2404
                htmlDoc.append("<br><hr><br>");
2405
            }//else
2406
        }//for
2407
        htmlDoc.append("</body></html>");
2408
        // use standard encoding even though the different docs might have use different encodings,
2409
        // the String objects in java should be correct and able to be encoded as the same Metacat default
2410
        byteString = htmlDoc.toString().getBytes(MetaCatServlet.DEFAULT_ENCODING);
2411
        zEntry = new ZipEntry(packageZipEntry + "/metadata.html");
2412
        zEntry.setSize(byteString.length);
2413
        zipOut.putNextEntry(zEntry);
2414
        zipOut.write(byteString, 0, byteString.length);
2415
        zipOut.closeEntry();
2416
        //dbConn.close();
2417

    
2418
    }//addHtmlSummaryToZipOutputStream
2419

    
2420
    /**
2421
     * put a data packadge into a zip output stream
2422
     *
2423
     * @param docId, which the user want to put into zip output stream,it has version
2424
     * @param out, a servletoutput stream which the zip output stream will be
2425
     *            put
2426
     * @param user, the username of the user
2427
     * @param groups, the group of the user
2428
     */
2429
    public ZipOutputStream getZippedPackage(String docIdString,
2430
            ServletOutputStream out, String user, String[] groups,
2431
            String passWord) throws ClassNotFoundException, IOException,
2432
            SQLException, McdbException, NumberFormatException, Exception
2433
    {
2434
        ZipOutputStream zOut = null;
2435
        String elementDocid = null;
2436
        DocumentImpl docImpls = null;
2437
        //Connection dbConn = null;
2438
        Vector docIdList = new Vector();
2439
        Vector documentImplList = new Vector();
2440
        Vector htmlDocumentImplList = new Vector();
2441
        String packageId = null;
2442
        String rootName = "package";//the package zip entry name
2443

    
2444
        String docId = null;
2445
        int version = -5;
2446
        // Docid without revision
2447
        docId = DocumentUtil.getDocIdFromString(docIdString);
2448
        // revision number
2449
        version = DocumentUtil.getVersionFromString(docIdString);
2450

    
2451
        //check if the reqused docId is a data package id
2452
        if (!isDataPackageId(docId)) {
2453

    
2454
            /*
2455
             * Exception e = new Exception("The request the doc id "
2456
             * +docIdString+ " is not a data package id");
2457
             */
2458

    
2459
            //CB 1/6/03: if the requested docid is not a datapackage, we just
2460
            // zip
2461
            //up the single document and return the zip file.
2462
            if (!hasPermissionToExportPackage(docId, user, groups)) {
2463

    
2464
                Exception e = new Exception("User " + user
2465
                        + " does not have permission"
2466
                        + " to export the data package " + docIdString);
2467
                throw e;
2468
            }
2469

    
2470
            docImpls = new DocumentImpl(docIdString);
2471
            //checking if the user has the permission to read the documents
2472
            if (DocumentImpl.hasReadPermission(user, groups, docImpls
2473
                    .getDocID())) {
2474
                zOut = new ZipOutputStream(out);
2475
                //if the docImpls is metadata
2476
                if ((docImpls.getDoctype()).compareTo("BIN") != 0) {
2477
                    //add metadata into zip output stream
2478
                    addDocToZipOutputStream(docImpls, zOut, rootName);
2479
                }//if
2480
                else {
2481
                    //it is data file
2482
                    addDataFileToZipOutputStream(docImpls, zOut, rootName);
2483
                    htmlDocumentImplList.add(docImpls);
2484
                }//else
2485
            }//if
2486

    
2487
            zOut.finish(); //terminate the zip file
2488
            return zOut;
2489
        }
2490
        // Check the permission of user
2491
        else if (!hasPermissionToExportPackage(docIdString, user, groups)) {
2492

    
2493
            Exception e = new Exception("User " + user
2494
                    + " does not have permission"
2495
                    + " to export the data package " + docIdString);
2496
            throw e;
2497
        } else //it is a packadge id
2498
        {
2499
            //store the package id
2500
            packageId = docId;
2501
            //get current version in database
2502
            int currentVersion = getCurrentRevFromXMLDoumentsTable(packageId);
2503
            //If it is for current version (-1 means user didn't specify
2504
            // revision)
2505
            if ((version == -1) || version == currentVersion) {
2506
                //get current version number
2507
                version = currentVersion;
2508
                //get package zip entry name
2509
                //it should be docId.revsion.package
2510
                rootName = packageId + PropertyService.getProperty("document.accNumSeparator")
2511
                        + version + PropertyService.getProperty("document.accNumSeparator")
2512
                        + "package";
2513
                //get the whole id list for data packadge
2514
                docIdList = getCurrentDocidListForDataPackage(packageId);
2515
                //get the whole documentImple object
2516
                documentImplList = getCurrentAllDocumentImpl(docIdList);
2517

    
2518
            }//if
2519
            else if (version > currentVersion || version < -1) {
2520
                throw new Exception("The user specified docid: " + docId + "."
2521
                        + version + " doesn't exist");
2522
            }//else if
2523
            else //for an old version
2524
            {
2525

    
2526
                rootName = docIdString
2527
                        + PropertyService.getProperty("document.accNumSeparator") + "package";
2528
                //get the whole id list for data packadge
2529
                docIdList = getOldVersionDocidListForDataPackage(docIdString);
2530

    
2531
                //get the whole documentImple object
2532
                documentImplList = getOldVersionAllDocumentImpl(docIdList);
2533
            }//else
2534

    
2535
            // Make sure documentImplist is not empty
2536
            if (documentImplList.isEmpty()) { throw new Exception(
2537
                    "Couldn't find component for data package: " + packageId); }//if
2538

    
2539
            zOut = new ZipOutputStream(out);
2540
            //put every element into zip output stream
2541
            for (int i = 0; i < documentImplList.size(); i++) {
2542
                // if the object in the vetor is String, this means we couldn't
2543
                // find
2544
                // the document locally, we need find it remote
2545
                if ((((documentImplList.elementAt(i)).getClass()).toString())
2546
                        .equals("class java.lang.String")) {
2547
                    // Get String object from vetor
2548
                    String documentId = (String) documentImplList.elementAt(i);
2549
                    logMetacat.info("DBQuery.getZippedPackage - docid: " + documentId);
2550
                    // Get doicd without revision
2551
                    String docidWithoutRevision = 
2552
                    	DocumentUtil.getDocIdFromString(documentId);
2553
                    logMetacat.info("DBQuery.getZippedPackage - docidWithoutRevsion: "
2554
                            + docidWithoutRevision);
2555
                    // Get revision
2556
                    String revision = 
2557
                    	DocumentUtil.getRevisionStringFromString(documentId);
2558
                    logMetacat.info("DBQuery.getZippedPackage - revision from docIdentifier: "
2559
                            + revision);
2560
                    // Zip entry string
2561
                    String zipEntryPath = rootName + "/data/";
2562
                    // Create a RemoteDocument object
2563
                    RemoteDocument remoteDoc = new RemoteDocument(
2564
                            docidWithoutRevision, revision, user, passWord,
2565
                            zipEntryPath);
2566
                    // Here we only read data file from remote metacat
2567
                    String docType = remoteDoc.getDocType();
2568
                    if (docType != null) {
2569
                        if (docType.equals("BIN")) {
2570
                            // Put remote document to zip output
2571
                            remoteDoc.readDocumentFromRemoteServerByZip(zOut);
2572
                            // Add String object to htmlDocumentImplList
2573
                            String elementInHtmlList = remoteDoc
2574
                                    .getDocIdWithoutRevsion()
2575
                                    + PropertyService.getProperty("document.accNumSeparator")
2576
                                    + remoteDoc.getRevision();
2577
                            htmlDocumentImplList.add(elementInHtmlList);
2578
                        }//if
2579
                    }//if
2580

    
2581
                }//if
2582
                else {
2583
                    //create a docmentImpls object (represent xml doc) base on
2584
                    // the docId
2585
                    docImpls = (DocumentImpl) documentImplList.elementAt(i);
2586
                    //checking if the user has the permission to read the
2587
                    // documents
2588
                    
2589
                    String fullDocId = docImpls.getDocID() + PropertyService.getProperty("document.accNumSeparator") + docImpls.getRev();
2590
					if (DocumentImpl.hasReadPermission(user, groups, fullDocId )) {
2591
                        //if the docImpls is metadata
2592
                        if ((docImpls.getDoctype()).compareTo("BIN") != 0) {
2593
                            //add metadata into zip output stream
2594
                            addDocToZipOutputStream(docImpls, zOut, rootName);
2595
                            //add the documentImpl into the vetor which will
2596
                            // be used in html
2597
                            htmlDocumentImplList.add(docImpls);
2598

    
2599
                        }//if
2600
                        else {
2601
                            //it is data file
2602
                            addDataFileToZipOutputStream(docImpls, zOut,
2603
                                    rootName);
2604
                            htmlDocumentImplList.add(docImpls);
2605
                        }//else
2606
                    }//if
2607
                }//else
2608
            }//for
2609

    
2610
            //add html summary file
2611
            addHtmlSummaryToZipOutputStream(htmlDocumentImplList, zOut,
2612
                    rootName);
2613
            zOut.finish(); //terminate the zip file
2614
            //dbConn.close();
2615
            return zOut;
2616
        }//else
2617
    }//getZippedPackage()
2618

    
2619
    private class ReturnFieldValue
2620
    {
2621

    
2622
        private String docid = null; //return field value for this docid
2623

    
2624
        private String fieldValue = null;
2625

    
2626
        private String xmlFieldValue = null; //return field value in xml
2627
                                             // format
2628
        private String fieldType = null; //ATTRIBUTE, TEXT...
2629

    
2630
        public void setDocid(String myDocid)
2631
        {
2632
            docid = myDocid;
2633
        }
2634

    
2635
        public String getDocid()
2636
        {
2637
            return docid;
2638
        }
2639

    
2640
        public void setFieldValue(String myValue)
2641
        {
2642
            fieldValue = myValue;
2643
        }
2644

    
2645
        public String getFieldValue()
2646
        {
2647
            return fieldValue;
2648
        }
2649

    
2650
        public void setXMLFieldValue(String xml)
2651
        {
2652
            xmlFieldValue = xml;
2653
        }
2654

    
2655
        public String getXMLFieldValue()
2656
        {
2657
            return xmlFieldValue;
2658
        }
2659
        
2660
        public void setFieldType(String myType)
2661
        {
2662
            fieldType = myType;
2663
        }
2664

    
2665
        public String getFieldType()
2666
        {
2667
            return fieldType;
2668
        }
2669

    
2670
    }
2671
    
2672
    /**
2673
     * a class to store one result document consisting of a docid and a document
2674
     */
2675
    private class ResultDocument
2676
    {
2677
      public String docid;
2678
      public String document;
2679
      
2680
      public ResultDocument(String docid, String document)
2681
      {
2682
        this.docid = docid;
2683
        this.document = document;
2684
      }
2685
    }
2686
    
2687
    /**
2688
     * a private class to handle a set of resultDocuments
2689
     */
2690
    private class ResultDocumentSet
2691
    {
2692
      private Vector docids;
2693
      private Vector documents;
2694
      
2695
      public ResultDocumentSet()
2696
      {
2697
        docids = new Vector();
2698
        documents = new Vector();
2699
      }
2700
      
2701
      /**
2702
       * adds a result document to the set
2703
       */
2704
      public void addResultDocument(ResultDocument rd)
2705
      {
2706
        if(rd.docid == null)
2707
          return;
2708
        if(rd.document == null)
2709
          rd.document = "";
2710
       
2711
           docids.addElement(rd.docid);
2712
           documents.addElement(rd.document);
2713
        
2714
      }
2715
      
2716
      /**
2717
       * gets an iterator of docids
2718
       */
2719
      public Iterator getDocids()
2720
      {
2721
        return docids.iterator();
2722
      }
2723
      
2724
      /**
2725
       * gets an iterator of documents
2726
       */
2727
      public Iterator getDocuments()
2728
      {
2729
        return documents.iterator();
2730
      }
2731
      
2732
      /**
2733
       * returns the size of the set
2734
       */
2735
      public int size()
2736
      {
2737
        return docids.size();
2738
      }
2739
      
2740
      /**
2741
       * tests to see if this set contains the given docid
2742
       */
2743
      private boolean containsDocid(String docid)
2744
      {
2745
        for(int i=0; i<docids.size(); i++)
2746
        {
2747
          String docid0 = (String)docids.elementAt(i);
2748
          if(docid0.trim().equals(docid.trim()))
2749
          {
2750
            return true;
2751
          }
2752
        }
2753
        return false;
2754
      }
2755
      
2756
      /**
2757
       * removes the element with the given docid
2758
       */
2759
      public String remove(String docid)
2760
      {
2761
        for(int i=0; i<docids.size(); i++)
2762
        {
2763
          String docid0 = (String)docids.elementAt(i);
2764
          if(docid0.trim().equals(docid.trim()))
2765
          {
2766
            String returnDoc = (String)documents.elementAt(i);
2767
            documents.remove(i);
2768
            docids.remove(i);
2769
            return returnDoc;
2770
          }
2771
        }
2772
        return null;
2773
      }
2774
      
2775
      /**
2776
       * add a result document
2777
       */
2778
      public void put(ResultDocument rd)
2779
      {
2780
        addResultDocument(rd);
2781
      }
2782
      
2783
      /**
2784
       * add a result document by components
2785
       */
2786
      public void put(String docid, String document)
2787
      {
2788
        addResultDocument(new ResultDocument(docid, document));
2789
      }
2790
      
2791
      /**
2792
       * get the document part of the result document by docid
2793
       */
2794
      public Object get(String docid)
2795
      {
2796
        for(int i=0; i<docids.size(); i++)
2797
        {
2798
          String docid0 = (String)docids.elementAt(i);
2799
          if(docid0.trim().equals(docid.trim()))
2800
          {
2801
            return documents.elementAt(i);
2802
          }
2803
        }
2804
        return null;
2805
      }
2806
      
2807
      /**
2808
       * get the document part of the result document by an object
2809
       */
2810
      public Object get(Object o)
2811
      {
2812
        return get((String)o);
2813
      }
2814
      
2815
      /**
2816
       * get an entire result document by index number
2817
       */
2818
      public ResultDocument get(int index)
2819
      {
2820
        return new ResultDocument((String)docids.elementAt(index), 
2821
          (String)documents.elementAt(index));
2822
      }
2823
      
2824
      /**
2825
       * return a string representation of this object
2826
       */
2827
      public String toString()
2828
      {
2829
        String s = "";
2830
        for(int i=0; i<docids.size(); i++)
2831
        {
2832
          s += (String)docids.elementAt(i) + "\n";
2833
        }
2834
        return s;
2835
      }
2836
      /*
2837
       * Set a new document value for a given docid
2838
       */
2839
      public void set(String docid, String document)
2840
      {
2841
    	   for(int i=0; i<docids.size(); i++)
2842
           {
2843
             String docid0 = (String)docids.elementAt(i);
2844
             if(docid0.trim().equals(docid.trim()))
2845
             {
2846
                 documents.set(i, document);
2847
             }
2848
           }
2849
           
2850
      }
2851
    }
2852
}
(17-17/63)