Project

General

Profile

1 155 jones
/**
2 203 jones
 *  '$RCSfile$'
3 2043 sgarg
 *    Purpose: A Class that searches a relational DB for elements and
4 203 jones
 *             attributes that have free text matches a query string,
5 2043 sgarg
 *             or structured query matches to a path specified node in the
6
 *             XML hierarchy.  It returns a result set consisting of the
7 203 jones
 *             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 155 jones
 *
12 203 jones
 *   '$Author$'
13
 *     '$Date$'
14
 * '$Revision$'
15 669 jones
 *
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 155 jones
 */
30
31 607 bojilova
package edu.ucsb.nceas.metacat;
32 155 jones
33 5752 leinfelder
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 2074 jones
import java.sql.PreparedStatement;
46
import java.sql.ResultSet;
47
import java.sql.SQLException;
48 6602 leinfelder
import java.sql.Timestamp;
49
import java.util.ArrayList;
50
import java.util.Date;
51 5752 leinfelder
import java.util.Enumeration;
52
import java.util.Hashtable;
53
import java.util.Iterator;
54 6602 leinfelder
import java.util.List;
55 5752 leinfelder
import java.util.StringTokenizer;
56
import java.util.Vector;
57
import java.util.zip.ZipEntry;
58
import java.util.zip.ZipOutputStream;
59 2074 jones
60 940 tao
import javax.servlet.ServletOutputStream;
61 2087 tao
import javax.servlet.http.HttpServletResponse;
62 155 jones
63 2663 sgarg
import org.apache.log4j.Logger;
64 2087 tao
65 5090 daigle
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlInterface;
66 5015 daigle
import edu.ucsb.nceas.metacat.database.DBConnection;
67
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
68 5030 daigle
import edu.ucsb.nceas.metacat.properties.PropertyService;
69 4589 daigle
import edu.ucsb.nceas.metacat.util.AuthUtil;
70 5025 daigle
import edu.ucsb.nceas.metacat.util.DocumentUtil;
71 4698 daigle
import edu.ucsb.nceas.metacat.util.MetacatUtil;
72 4080 daigle
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
73 2074 jones
74 7403 leinfelder
import edu.ucsb.nceas.utilities.triple.Triple;
75
import edu.ucsb.nceas.utilities.triple.TripleCollection;
76 2912 harris
77 7403 leinfelder
78 2043 sgarg
/**
79 2075 jones
 * A Class that searches a relational DB for elements and attributes that have
80
 * free text matches a query string, or structured query matches to a path
81
 * specified node in the XML hierarchy. It returns a result set consisting of
82
 * the document ID for each document that satisfies the query
83 155 jones
 */
84 2075 jones
public class DBQuery
85
{
86 155 jones
87 2075 jones
    static final int ALL = 1;
88 2043 sgarg
89 2075 jones
    static final int WRITE = 2;
90 2043 sgarg
91 2075 jones
    static final int READ = 4;
92 5490 berkley
93
    private String qformat = "xml";
94 6035 leinfelder
95
    // are we combining the query with docid list and, if so, using INTERSECT or UNION?
96
    private String operator = null;
97 155 jones
98 2075 jones
    //private Connection conn = null;
99
    private String parserName = null;
100 706 bojilova
101 2663 sgarg
    private Logger logMetacat = Logger.getLogger(DBQuery.class);
102
103 2912 harris
    /** true if the metacat spatial option is installed **/
104
    private final boolean METACAT_SPATIAL = true;
105
106 3392 tao
    /** useful if you just want to grab a list of docids. Since the docids can be very long,
107
         it is a vector of vector  **/
108 3047 perry
    Vector docidOverride = new Vector();
109 3340 tao
110
    // a hash table serves as query reuslt cache. Key of hashtable
111 3342 tao
    // is a query string and value is result xml string
112 3340 tao
    private static Hashtable queryResultCache = new Hashtable();
113
114
    // Capacity of the query result cache
115 4080 daigle
    private static final int QUERYRESULTCACHESIZE;
116
    static {
117
    	int qryRsltCacheSize = 0;
118
    	try {
119 4212 daigle
    		qryRsltCacheSize = Integer.parseInt(PropertyService.getProperty("database.queryresultCacheSize"));
120 4080 daigle
    	} catch (PropertyNotFoundException pnfe) {
121
    		System.err.println("Could not get QUERYRESULTCACHESIZE property in static block: "
122
					+ pnfe.getMessage());
123
    	}
124
    	QUERYRESULTCACHESIZE = qryRsltCacheSize;
125
    }
126
127 3047 perry
128 3368 tao
    // Size of page for non paged query
129
    private static final int NONPAGESIZE = 99999999;
130 2075 jones
    /**
131
     * the main routine used to test the DBQuery utility.
132
     * <p>
133
     * Usage: java DBQuery <xmlfile>
134 5752 leinfelder
     * NOTE: encoding should be provided for best results
135 2075 jones
     * @param xmlfile the filename of the xml file containing the query
136
     */
137
    static public void main(String[] args)
138
    {
139 706 bojilova
140 2075 jones
        if (args.length < 1) {
141
            System.err.println("Wrong number of arguments!!!");
142
            System.err.println("USAGE: java DBQuery [-t] [-index] <xmlfile>");
143
            return;
144
        } else {
145
            try {
146 706 bojilova
147 2075 jones
                int i = 0;
148
                boolean showRuntime = false;
149
                boolean useXMLIndex = false;
150
                if (args[i].equals("-t")) {
151
                    showRuntime = true;
152
                    i++;
153
                }
154
                if (args[i].equals("-index")) {
155
                    useXMLIndex = true;
156
                    i++;
157
                }
158
                String xmlfile = args[i];
159 706 bojilova
160 2075 jones
                // Time the request if asked for
161
                double startTime = System.currentTimeMillis();
162 2043 sgarg
163 2075 jones
                // Open a connection to the database
164
                //Connection dbconn = util.openDBConnection();
165 2043 sgarg
166 2075 jones
                double connTime = System.currentTimeMillis();
167 2043 sgarg
168 2075 jones
                // Execute the query
169 2752 jones
                DBQuery queryobj = new DBQuery();
170 5752 leinfelder
                Reader xml = new InputStreamReader(new FileInputStream(new File(xmlfile)));
171 2075 jones
                Hashtable nodelist = null;
172 2087 tao
                //nodelist = queryobj.findDocuments(xml, null, null, useXMLIndex);
173 2043 sgarg
174 2075 jones
                // Print the reulting document listing
175
                StringBuffer result = new StringBuffer();
176
                String document = null;
177
                String docid = null;
178
                result.append("<?xml version=\"1.0\"?>\n");
179
                result.append("<resultset>\n");
180 2043 sgarg
181 2075 jones
                if (!showRuntime) {
182
                    Enumeration doclist = nodelist.keys();
183
                    while (doclist.hasMoreElements()) {
184
                        docid = (String) doclist.nextElement();
185
                        document = (String) nodelist.get(docid);
186
                        result.append("  <document>\n    " + document
187
                                + "\n  </document>\n");
188
                    }
189 706 bojilova
190 2075 jones
                    result.append("</resultset>\n");
191
                }
192
                // Time the request if asked for
193
                double stopTime = System.currentTimeMillis();
194
                double dbOpenTime = (connTime - startTime) / 1000;
195
                double readTime = (stopTime - connTime) / 1000;
196
                double executionTime = (stopTime - startTime) / 1000;
197
                if (showRuntime) {
198
                    System.out.print("  " + executionTime);
199
                    System.out.print("  " + dbOpenTime);
200
                    System.out.print("  " + readTime);
201
                    System.out.print("  " + nodelist.size());
202
                    System.out.println();
203
                }
204
                //System.out.println(result);
205
                //write into a file "result.txt"
206
                if (!showRuntime) {
207
                    File f = new File("./result.txt");
208 5752 leinfelder
                    Writer fw = new OutputStreamWriter(new FileOutputStream(f));
209 2075 jones
                    BufferedWriter out = new BufferedWriter(fw);
210
                    out.write(result.toString());
211
                    out.flush();
212
                    out.close();
213
                    fw.close();
214
                }
215 2043 sgarg
216 2075 jones
            } catch (Exception e) {
217
                System.err.println("Error in DBQuery.main");
218
                System.err.println(e.getMessage());
219
                e.printStackTrace(System.err);
220
            }
221
        }
222
    }
223 2043 sgarg
224 2075 jones
    /**
225
     * construct an instance of the DBQuery class
226 2087 tao
     *
227 2075 jones
     * <p>
228
     * Generally, one would call the findDocuments() routine after creating an
229
     * instance to specify the search query
230
     * </p>
231 2087 tao
     *
232
233 2075 jones
     * @param parserName the fully qualified name of a Java class implementing
234
     *            the org.xml.sax.XMLReader interface
235
     */
236 4080 daigle
    public DBQuery() throws PropertyNotFoundException
237 2075 jones
    {
238 4213 daigle
        String parserName = PropertyService.getProperty("xml.saxparser");
239 2752 jones
        this.parserName = parserName;
240 2075 jones
    }
241 2043 sgarg
242 3047 perry
    /**
243
     *
244
     * Construct an instance of DBQuery Class
245
     * BUT accept a docid Vector that will supersede
246
     * the query.printSQL() method
247
     *
248
     * If a docid Vector is passed in,
249
     * the docids will be used to create a simple IN query
250
     * without the multiple subselects of the printSQL() method
251
     *
252
     * Using this constructor, we just check for
253
     * a docidOverride Vector in the findResultDoclist() method
254
     *
255
     * @param docids List of docids to display in the resultset
256
     */
257 4080 daigle
    public DBQuery(Vector docids) throws PropertyNotFoundException
258 3047 perry
    {
259 3392 tao
    	// since the query will be too long to be handled, so we divided the
260
    	// docids vector into couple vectors.
261 4212 daigle
    	int size = (new Integer(PropertyService.getProperty("database.appResultsetSize"))).intValue();
262 5165 daigle
    	logMetacat.info("DBQuery.DBQuery - The size of select doicds is "+docids.size());
263
    	logMetacat.info("DBQuery.DBQuery - The application result size in metacat.properties is "+size);
264 3392 tao
    	Vector subset = new Vector();
265
    	if (docids != null && docids.size() > size)
266
    	{
267
    		int index = 0;
268
    		for (int i=0; i< docids.size(); i++)
269
    		{
270
271
    			if (index < size)
272
    			{
273
    				subset.add(docids.elementAt(i));
274
    				index ++;
275
    			}
276
    			else
277
    			{
278
    				docidOverride.add(subset);
279
    				subset = new Vector();
280
    				subset.add(docids.elementAt(i));
281
    			    index = 1;
282
    			}
283
    		}
284
    		if (!subset.isEmpty())
285
    		{
286
    			docidOverride.add(subset);
287
    		}
288
289
    	}
290
    	else
291
    	{
292
    		this.docidOverride.add(docids);
293
    	}
294
295 4213 daigle
        String parserName = PropertyService.getProperty("xml.saxparser");
296 3047 perry
        this.parserName = parserName;
297
    }
298 2087 tao
299
  /**
300
   * Method put the search result set into out printerwriter
301
   * @param resoponse the return response
302
   * @param out the output printer
303
   * @param params the paratermer hashtable
304
   * @param user the user name (it maybe different to the one in param)
305
   * @param groups the group array
306
   * @param sessionid  the sessionid
307
   */
308
  public void findDocuments(HttpServletResponse response,
309 5752 leinfelder
                                       Writer out, Hashtable params,
310 2087 tao
                                       String user, String[] groups,
311 4080 daigle
                                       String sessionid) throws PropertyNotFoundException
312 2087 tao
  {
313 4173 daigle
    boolean useXMLIndex = (new Boolean(PropertyService.getProperty("database.usexmlindex")))
314 2087 tao
               .booleanValue();
315
    findDocuments(response, out, params, user, groups, sessionid, useXMLIndex);
316
317
  }
318
319
320 2075 jones
    /**
321 2087 tao
     * Method put the search result set into out printerwriter
322
     * @param resoponse the return response
323
     * @param out the output printer
324
     * @param params the paratermer hashtable
325
     * @param user the user name (it maybe different to the one in param)
326
     * @param groups the group array
327
     * @param sessionid  the sessionid
328 2075 jones
     */
329 2087 tao
    public void findDocuments(HttpServletResponse response,
330 5752 leinfelder
                                         Writer out, Hashtable params,
331 2087 tao
                                         String user, String[] groups,
332
                                         String sessionid, boolean useXMLIndex)
333 2075 jones
    {
334 3211 berkley
      int pagesize = 0;
335
      int pagestart = 0;
336 5165 daigle
      long transferWarnLimit = 0;
337 3211 berkley
338
      if(params.containsKey("pagesize") && params.containsKey("pagestart"))
339
      {
340
        String pagesizeStr = ((String[])params.get("pagesize"))[0];
341
        String pagestartStr = ((String[])params.get("pagestart"))[0];
342
        if(pagesizeStr != null && pagestartStr != null)
343
        {
344
          pagesize = (new Integer(pagesizeStr)).intValue();
345
          pagestart = (new Integer(pagestartStr)).intValue();
346
        }
347
      }
348
349 3780 daigle
      String xmlquery = null;
350
      String qformat = null;
351 2087 tao
      // get query and qformat
352 3780 daigle
      try {
353
    	xmlquery = ((String[])params.get("query"))[0];
354 2168 tao
355 5165 daigle
        logMetacat.info("DBQuery.findDocuments - SESSIONID: " + sessionid);
356
        logMetacat.info("DBQuery.findDocuments - xmlquery: " + xmlquery);
357 3780 daigle
        qformat = ((String[])params.get("qformat"))[0];
358 5165 daigle
        logMetacat.info("DBQuery.findDocuments - qformat: " + qformat);
359 3780 daigle
      }
360
      catch (Exception ee)
361
      {
362 5165 daigle
        logMetacat.error("DBQuery.findDocuments - Couldn't retrieve xmlquery or qformat value from "
363 3780 daigle
                  +"params hashtable in DBQuery.findDocuments: "
364
                  + ee.getMessage());
365
      }
366 2168 tao
      // Get the XML query and covert it into a SQL statment
367
      QuerySpecification qspec = null;
368
      if ( xmlquery != null)
369
      {
370
         xmlquery = transformQuery(xmlquery);
371
         try
372
         {
373
           qspec = new QuerySpecification(xmlquery,
374
                                          parserName,
375 4212 daigle
                                          PropertyService.getProperty("document.accNumSeparator"));
376 2168 tao
         }
377
         catch (Exception ee)
378
         {
379 5165 daigle
           logMetacat.error("DBQuery.findDocuments - error generating QuerySpecification object: "
380 2663 sgarg
                                    + ee.getMessage());
381 2168 tao
         }
382
      }
383 2087 tao
384 2168 tao
385
386 5025 daigle
      if (qformat != null && qformat.equals(MetacatUtil.XMLFORMAT))
387 2087 tao
      {
388
        //xml format
389 5491 berkley
        if(response != null)
390
        {
391
            response.setContentType("text/xml");
392
        }
393 5490 berkley
        createResultDocument(xmlquery, qspec, out, user, groups, useXMLIndex,
394
          pagesize, pagestart, sessionid, qformat);
395 2087 tao
      }//if
396
      else
397
      {
398
        //knb format, in this case we will get whole result and sent it out
399 3257 berkley
        response.setContentType("text/html");
400 5752 leinfelder
        Writer nonout = null;
401 2168 tao
        StringBuffer xml = createResultDocument(xmlquery, qspec, nonout, user,
402 3211 berkley
                                                groups, useXMLIndex, pagesize,
403 5490 berkley
                                                pagestart, sessionid, qformat);
404 2658 sgarg
405 2087 tao
        //transfer the xml to html
406
        try
407
        {
408 5165 daigle
         long startHTMLTransform = System.currentTimeMillis();
409 2087 tao
         DBTransform trans = new DBTransform();
410
         response.setContentType("text/html");
411 2787 sgarg
412 3219 berkley
         // if the user is a moderator, then pass a param to the
413 2787 sgarg
         // xsl specifying the fact
414 4589 daigle
         if(AuthUtil.isModerator(user, groups)){
415 2787 sgarg
        	 params.put("isModerator", new String[] {"true"});
416
         }
417
418 2087 tao
         trans.transformXMLDocument(xml.toString(), "-//NCEAS//resultset//EN",
419
                                 "-//W3C//HTML//EN", qformat, out, params,
420
                                 sessionid);
421 5165 daigle
         long transformRunTime = System.currentTimeMillis() - startHTMLTransform;
422
423
         transferWarnLimit = Long.parseLong(PropertyService.getProperty("dbquery.transformTimeWarnLimit"));
424
425
         if (transformRunTime > transferWarnLimit) {
426
         	logMetacat.warn("DBQuery.findDocuments - The time to transfrom resultset from xml to html format is "
427
                  		                             + transformRunTime);
428
         }
429 4698 daigle
          MetacatUtil.writeDebugToFile("---------------------------------------------------------------------------------------------------------------Transfrom xml to html  "
430 5165 daigle
                             + transformRunTime);
431
          MetacatUtil.writeDebugToDelimiteredFile(" " + transformRunTime, false);
432 2087 tao
        }
433
        catch(Exception e)
434
        {
435 5165 daigle
         logMetacat.error("DBQuery.findDocuments - Error in MetaCatServlet.transformResultset:"
436 2663 sgarg
                                +e.getMessage());
437 2087 tao
         }
438
439
      }//else
440
441 3219 berkley
  }
442 5490 berkley
443
444 3220 tao
445
  /**
446
   * Transforms a hashtable of documents to an xml or html result and sent
447
   * the content to outputstream. Keep going untill hastable is empty. stop it.
448
   * add the QuerySpecification as parameter is for ecogrid. But it is duplicate
449
   * to xmlquery String
450
   * @param xmlquery
451
   * @param qspec
452
   * @param out
453
   * @param user
454
   * @param groups
455
   * @param useXMLIndex
456
   * @param sessionid
457
   * @return
458
   */
459
    public StringBuffer createResultDocument(String xmlquery,
460
                                              QuerySpecification qspec,
461 5752 leinfelder
                                              Writer out,
462 3220 tao
                                              String user, String[] groups,
463
                                              boolean useXMLIndex)
464
    {
465 5490 berkley
    	return createResultDocument(xmlquery,qspec,out, user,groups, useXMLIndex, 0, 0,"", qformat);
466 3220 tao
    }
467 2043 sgarg
468 2087 tao
  /*
469
   * Transforms a hashtable of documents to an xml or html result and sent
470 2168 tao
   * the content to outputstream. Keep going untill hastable is empty. stop it.
471
   * add the QuerySpecification as parameter is for ecogrid. But it is duplicate
472
   * to xmlquery String
473 2087 tao
   */
474 2168 tao
  public StringBuffer createResultDocument(String xmlquery,
475
                                            QuerySpecification qspec,
476 5752 leinfelder
                                            Writer out,
477 2087 tao
                                            String user, String[] groups,
478 3211 berkley
                                            boolean useXMLIndex, int pagesize,
479 5490 berkley
                                            int pagestart, String sessionid,
480
                                            String qformat)
481 2087 tao
  {
482
    DBConnection dbconn = null;
483
    int serialNumber = -1;
484
    StringBuffer resultset = new StringBuffer();
485 3219 berkley
486
    //try to get the cached version first
487 4080 daigle
    // Hashtable sessionHash = MetaCatServlet.getSessionHash();
488
    // HttpSession sess = (HttpSession)sessionHash.get(sessionid);
489 3219 berkley
490 3220 tao
491 2087 tao
    resultset.append("<?xml version=\"1.0\"?>\n");
492
    resultset.append("<resultset>\n");
493 3257 berkley
    resultset.append("  <pagestart>" + pagestart + "</pagestart>\n");
494
    resultset.append("  <pagesize>" + pagesize + "</pagesize>\n");
495
    resultset.append("  <nextpage>" + (pagestart + 1) + "</nextpage>\n");
496
    resultset.append("  <previouspage>" + (pagestart - 1) + "</previouspage>\n");
497
498 2087 tao
    resultset.append("  <query>" + xmlquery + "</query>");
499 3219 berkley
    //send out a new query
500 2087 tao
    if (out != null)
501 2075 jones
    {
502 5752 leinfelder
    	try {
503
    	  out.write(resultset.toString());
504
		} catch (IOException e) {
505
			logMetacat.error(e.getMessage(), e);
506
		}
507 2075 jones
    }
508 2168 tao
    if (qspec != null)
509 2087 tao
    {
510 2168 tao
      try
511
      {
512 2043 sgarg
513 2168 tao
        //checkout the dbconnection
514
        dbconn = DBConnectionPool.getDBConnection("DBQuery.findDocuments");
515
        serialNumber = dbconn.getCheckOutSerialNumber();
516 2087 tao
517 2168 tao
        //print out the search result
518
        // search the doc list
519 3392 tao
        Vector givenDocids = new Vector();
520
        StringBuffer resultContent = new StringBuffer();
521
        if (docidOverride == null || docidOverride.size() == 0)
522
        {
523 5165 daigle
        	logMetacat.debug("DBQuery.createResultDocument - Not in map query");
524 3392 tao
        	resultContent = findResultDoclist(qspec, out, user, groups,
525
                    dbconn, useXMLIndex, pagesize, pagestart,
526 5490 berkley
                    sessionid, givenDocids, qformat);
527 3392 tao
        }
528
        else
529
        {
530 5165 daigle
        	logMetacat.debug("DBQuery.createResultDocument - In map query");
531 3392 tao
        	// since docid can be too long to be handled. We divide it into several parts
532
        	for (int i= 0; i<docidOverride.size(); i++)
533
        	{
534 5165 daigle
        	   logMetacat.debug("DBQuery.createResultDocument - in loop===== "+i);
535 3392 tao
        		givenDocids = (Vector)docidOverride.elementAt(i);
536
        		StringBuffer subset = findResultDoclist(qspec, out, user, groups,
537
                        dbconn, useXMLIndex, pagesize, pagestart,
538 5490 berkley
                        sessionid, givenDocids, qformat);
539 3392 tao
        		resultContent.append(subset);
540
        	}
541
        }
542
543 3342 tao
        resultset.append(resultContent);
544 2168 tao
      } //try
545
      catch (IOException ioe)
546
      {
547 5165 daigle
        logMetacat.error("DBQuery.createResultDocument - IO error: " + ioe.getMessage());
548 2168 tao
      }
549
      catch (SQLException e)
550
      {
551 5165 daigle
        logMetacat.error("DBQuery.createResultDocument - SQL Error: " + e.getMessage());
552 2168 tao
      }
553
      catch (Exception ee)
554
      {
555 5165 daigle
        logMetacat.error("DBQuery.createResultDocument - General exception: "
556 2663 sgarg
                                 + ee.getMessage());
557 3219 berkley
        ee.printStackTrace();
558 2168 tao
      }
559
      finally
560
      {
561
        DBConnectionPool.returnDBConnection(dbconn, serialNumber);
562
      } //finally
563
    }//if
564 2087 tao
    String closeRestultset = "</resultset>";
565
    resultset.append(closeRestultset);
566
    if (out != null)
567
    {
568 5752 leinfelder
      try {
569
		out.write(closeRestultset);
570
		} catch (IOException e) {
571
			logMetacat.error(e.getMessage(), e);
572
		}
573 2087 tao
    }
574 2168 tao
575 3221 berkley
    //default to returning the whole resultset
576 2087 tao
    return resultset;
577
  }//createResultDocuments
578 2043 sgarg
579 2087 tao
    /*
580
     * Find the doc list which match the query
581
     */
582
    private StringBuffer findResultDoclist(QuerySpecification qspec,
583 5752 leinfelder
                                      Writer out,
584 2087 tao
                                      String user, String[]groups,
585 3211 berkley
                                      DBConnection dbconn, boolean useXMLIndex,
586 5490 berkley
                                      int pagesize, int pagestart, String sessionid,
587
                                      Vector givenDocids, String qformat)
588 2087 tao
                                      throws Exception
589
    {
590 6602 leinfelder
    	// keep track of the values we add as prepared statement question marks (?)
591
  	  List<Object> parameterValues = new ArrayList<Object>();
592
593 3342 tao
      StringBuffer resultsetBuffer = new StringBuffer();
594 3219 berkley
      String query = null;
595
      int count = 0;
596
      int index = 0;
597 3246 berkley
      ResultDocumentSet docListResult = new ResultDocumentSet();
598 3219 berkley
      PreparedStatement pstmt = null;
599
      String docid = null;
600
      String docname = null;
601
      String doctype = null;
602
      String createDate = null;
603
      String updateDate = null;
604
      StringBuffer document = null;
605 3262 berkley
      boolean lastpage = false;
606 3219 berkley
      int rev = 0;
607
      double startTime = 0;
608 3368 tao
      int offset = 1;
609 5165 daigle
      long startSelectionTime = System.currentTimeMillis();
610 3219 berkley
      ResultSet rs = null;
611 3368 tao
612
613
      // this is a hack for offset. in postgresql 7, if the returned docid list is too long,
614
      //the extend query which base on the docid will be too long to be run. So we
615
      // have to cut them into different parts. Page query don't need it somehow.
616
      if (out == null)
617 2091 tao
      {
618
        // for html page, we put everything into one page
619 2421 sgarg
        offset =
620 4212 daigle
            (new Integer(PropertyService.getProperty("database.webResultsetSize"))).intValue();
621 2091 tao
      }
622
      else
623
      {
624
          offset =
625 4212 daigle
              (new Integer(PropertyService.getProperty("database.appResultsetSize"))).intValue();
626 3368 tao
      }
627 2421 sgarg
628 3047 perry
      /*
629
       * Check the docidOverride Vector
630
       * if defined, we bypass the qspec.printSQL() method
631
       * and contruct a simpler query based on a
632
       * list of docids rather than a bunch of subselects
633
       */
634 6602 leinfelder
      // keep track of the values we add as prepared statement question marks (?)
635
	  List<Object> docidValues = new ArrayList<Object>();
636 3392 tao
      if ( givenDocids == null || givenDocids.size() == 0 ) {
637 6602 leinfelder
          query = qspec.printSQL(useXMLIndex, docidValues);
638
          parameterValues.addAll(docidValues);
639 3047 perry
      } else {
640 6035 leinfelder
    	  // condition for the docids
641 6629 leinfelder
    	  List<Object> docidConditionValues = new ArrayList<Object>();
642 6035 leinfelder
    	  StringBuffer docidCondition = new StringBuffer();
643 7407 leinfelder
    	  docidCondition.append( " xml_documents.docid IN (" );
644 3392 tao
          for (int i = 0; i < givenDocids.size(); i++) {
645 6629 leinfelder
        	  docidCondition.append("?");
646 6035 leinfelder
        	  if (i < givenDocids.size()-1) {
647
        		  docidCondition.append(",");
648
        	  }
649 6629 leinfelder
        	  docidConditionValues.add((String)givenDocids.elementAt(i));
650 3047 perry
          }
651 6035 leinfelder
          docidCondition.append( ") " );
652
653
    	  // include the docids, either exclusively, or in conjuction with the query
654
    	  if (operator == null) {
655 7407 leinfelder
    		  query = "SELECT xml_documents.docid, docname, doctype, date_created, date_updated, xml_documents.rev " +
656
    		  		"FROM xml_documents, identifier " +
657
    		  		"WHERE xml_documents.docid = identifier.docid AND xml_documents.rev = identifier.rev AND ";
658 6035 leinfelder
              query = query + docidCondition.toString();
659 6629 leinfelder
              parameterValues.addAll(docidConditionValues);
660 6035 leinfelder
    	  } else {
661
    		  // start with the keyword query, but add conditions
662 6602 leinfelder
              query = qspec.printSQL(useXMLIndex, docidValues);
663
              parameterValues.addAll(docidValues);
664 6035 leinfelder
              String myOperator = "";
665
              if (!query.endsWith("WHERE")) {
666
	              if (operator.equalsIgnoreCase(QueryGroup.UNION)) {
667
	            	  myOperator =  " OR ";
668
	              }
669
	              else {
670
	            	  myOperator =  " AND ";
671
	              }
672
              }
673
              query = query + myOperator + docidCondition.toString();
674 6629 leinfelder
              parameterValues.addAll(docidConditionValues);
675 6035 leinfelder
676
    	  }
677 3047 perry
      }
678 6629 leinfelder
      // we don't actually use this query for anything
679
      List<Object> ownerValues = new ArrayList<Object>();
680
      String ownerQuery = getOwnerQuery(user, ownerValues);
681 4574 daigle
      //logMetacat.debug("query: " + query);
682 5165 daigle
      logMetacat.debug("DBQuery.findResultDoclist - owner query: " + ownerQuery);
683 2087 tao
      // if query is not the owner query, we need to check the permission
684
      // otherwise we don't need (owner has all permission by default)
685
      if (!query.equals(ownerQuery))
686
      {
687
        // set user name and group
688
        qspec.setUserName(user);
689
        qspec.setGroup(groups);
690
        // Get access query
691
        String accessQuery = qspec.getAccessQuery();
692 2366 sgarg
        if(!query.endsWith("WHERE")){
693
            query = query + accessQuery;
694
        } else {
695
            query = query + accessQuery.substring(4, accessQuery.length());
696
        }
697 3309 tao
698 2087 tao
      }
699 5165 daigle
      logMetacat.debug("DBQuery.findResultDoclist - final selection query: " + query);
700 6774 tao
701
702
      pstmt = dbconn.prepareStatement(query);
703
      // set all the values we have collected
704
      pstmt = setPreparedStatementValues(parameterValues, pstmt);
705
706
      String queryCacheKey = null;
707 3342 tao
      // we only get cache for public
708
      if (user != null && user.equalsIgnoreCase("public")
709 6774 tao
         && pagesize == 0 && PropertyService.getProperty("database.queryCacheOn").equals("true"))
710 3342 tao
      {
711 6774 tao
          queryCacheKey = pstmt.toString() +qspec.getReturnDocList()+qspec.getReturnFieldList();
712
          String cachedResult = getResultXMLFromCache(queryCacheKey);
713
          logMetacat.debug("=======DBQuery.findResultDoclist - The key of query cache is " + queryCacheKey);
714
          //System.out.println("==========the string from cache is "+cachedResult);
715
          if (cachedResult != null)
716
          {
717
          logMetacat.info("DBQuery.findResultDoclist - result from cache !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
718
           if (out != null)
719
             {
720
                 out.write(cachedResult);
721
             }
722
           resultsetBuffer.append(cachedResult);
723
           pstmt.close();
724
           return resultsetBuffer;
725
          }
726 3342 tao
      }
727
728 3219 berkley
      startTime = System.currentTimeMillis() / 1000;
729 6602 leinfelder
      logMetacat.debug("Prepared statement after setting parameter values: " + pstmt.toString());
730 3219 berkley
      rs = pstmt.executeQuery();
731 3246 berkley
732 2087 tao
      double queryExecuteTime = System.currentTimeMillis() / 1000;
733 5165 daigle
      logMetacat.debug("DBQuery.findResultDoclist - Time to execute select docid query is "
734 2663 sgarg
                    + (queryExecuteTime - startTime));
735 4698 daigle
      MetacatUtil.writeDebugToFile("\n\n\n\n\n\nExecute selection query  "
736 3271 tao
              + (queryExecuteTime - startTime));
737 4698 daigle
      MetacatUtil.writeDebugToDelimiteredFile(""+(queryExecuteTime - startTime), false);
738 3246 berkley
739 3247 berkley
      boolean tableHasRows = rs.next();
740 3246 berkley
741
      if(pagesize == 0)
742
      { //this makes sure we get all results if there is no paging
743 3368 tao
        pagesize = NONPAGESIZE;
744
        pagestart = NONPAGESIZE;
745 3246 berkley
      }
746
747
      int currentIndex = 0;
748 2087 tao
      while (tableHasRows)
749
      {
750 5165 daigle
        logMetacat.debug("DBQuery.findResultDoclist - getting result: " + currentIndex);
751 2087 tao
        docid = rs.getString(1).trim();
752 5165 daigle
        logMetacat.debug("DBQuery.findResultDoclist -  processing: " + docid);
753 2087 tao
        docname = rs.getString(2);
754
        doctype = rs.getString(3);
755 5165 daigle
        logMetacat.debug("DBQuery.findResultDoclist - processing: " + doctype);
756 2087 tao
        createDate = rs.getString(4);
757
        updateDate = rs.getString(5);
758
        rev = rs.getInt(6);
759 3246 berkley
760 3307 tao
         Vector returndocVec = qspec.getReturnDocList();
761
       if (returndocVec.size() == 0 || returndocVec.contains(doctype))
762 2087 tao
        {
763 5165 daigle
          logMetacat.debug("DBQuery.findResultDoclist - NOT Back tracing now...");
764 2087 tao
           document = new StringBuffer();
765 2043 sgarg
766 2087 tao
           String completeDocid = docid
767 4212 daigle
                            + PropertyService.getProperty("document.accNumSeparator");
768 2087 tao
           completeDocid += rev;
769
           document.append("<docid>").append(completeDocid).append("</docid>");
770
           if (docname != null)
771
           {
772
               document.append("<docname>" + docname + "</docname>");
773 3219 berkley
           }
774
           if (doctype != null)
775
           {
776
              document.append("<doctype>" + doctype + "</doctype>");
777
           }
778
           if (createDate != null)
779
           {
780
               document.append("<createdate>" + createDate + "</createdate>");
781
           }
782
           if (updateDate != null)
783
           {
784
             document.append("<updatedate>" + updateDate + "</updatedate>");
785
           }
786
           // Store the document id and the root node id
787 3246 berkley
788
           docListResult.addResultDocument(
789
             new ResultDocument(docid, (String) document.toString()));
790 5165 daigle
           logMetacat.info("DBQuery.findResultDoclist - real result: " + docid);
791 3246 berkley
           currentIndex++;
792 3219 berkley
           count++;
793 2087 tao
        }//else
794 3246 berkley
795 2087 tao
        // when doclist reached the offset number, send out doc list and empty
796
        // the hash table
797 3368 tao
        if (count == offset && pagesize == NONPAGESIZE)
798 3246 berkley
        { //if pagesize is not 0, do this later.
799 2087 tao
          //reset count
800 3262 berkley
          //logMetacat.warn("############doing subset cache");
801 2087 tao
          count = 0;
802 3246 berkley
          handleSubsetResult(qspec, resultsetBuffer, out, docListResult,
803 5490 berkley
                              user, groups,dbconn, useXMLIndex, qformat);
804 3246 berkley
          //reset docListResult
805
          docListResult = new ResultDocumentSet();
806 3368 tao
        }
807 3246 berkley
808 5165 daigle
       logMetacat.debug("DBQuery.findResultDoclist - currentIndex: " + currentIndex);
809
       logMetacat.debug("DBQuery.findResultDoclist - page comparator: " + (pagesize * pagestart) + pagesize);
810 3246 berkley
       if(currentIndex >= ((pagesize * pagestart) + pagesize))
811
       {
812
         ResultDocumentSet pagedResultsHash = new ResultDocumentSet();
813
         for(int i=pagesize*pagestart; i<docListResult.size(); i++)
814
         {
815
           pagedResultsHash.put(docListResult.get(i));
816
         }
817
818
         docListResult = pagedResultsHash;
819
         break;
820
       }
821 2087 tao
       // Advance to the next record in the cursor
822
       tableHasRows = rs.next();
823 3246 berkley
       if(!tableHasRows)
824
       {
825 3262 berkley
         ResultDocumentSet pagedResultsHash = new ResultDocumentSet();
826
         //get the last page of information then break
827 3368 tao
         if(pagesize != NONPAGESIZE)
828 3262 berkley
         {
829
           for(int i=pagesize*pagestart; i<docListResult.size(); i++)
830
           {
831
             pagedResultsHash.put(docListResult.get(i));
832
           }
833
           docListResult = pagedResultsHash;
834
         }
835
836
         lastpage = true;
837 3246 berkley
         break;
838
       }
839 2087 tao
     }//while
840 3246 berkley
841 2087 tao
     rs.close();
842
     pstmt.close();
843 5165 daigle
     long docListTime = System.currentTimeMillis() - startSelectionTime;
844
     long docListWarnLimit = Long.parseLong(PropertyService.getProperty("dbquery.findDocListTimeWarnLimit"));
845
     if (docListTime > docListWarnLimit) {
846
    	 logMetacat.warn("DBQuery.findResultDoclist - Total time to get docid list is: "
847
                          + docListTime);
848
     }
849 4698 daigle
     MetacatUtil.writeDebugToFile("---------------------------------------------------------------------------------------------------------------Total selection: "
850 5165 daigle
             + docListTime);
851
     MetacatUtil.writeDebugToDelimiteredFile(" "+ docListTime, false);
852 2087 tao
     //if docListResult is not empty, it need to be sent.
853 3246 berkley
     if (docListResult.size() != 0)
854 2087 tao
     {
855 3342 tao
856 2087 tao
       handleSubsetResult(qspec,resultsetBuffer, out, docListResult,
857 5490 berkley
                              user, groups,dbconn, useXMLIndex, qformat);
858 2087 tao
     }
859 2091 tao
860 3262 berkley
     resultsetBuffer.append("\n<lastpage>" + lastpage + "</lastpage>\n");
861
     if (out != null)
862
     {
863 5752 leinfelder
         out.write("\n<lastpage>" + lastpage + "</lastpage>\n");
864 3262 berkley
     }
865 3342 tao
866
     // now we only cached none-paged query and user is public
867
     if (user != null && user.equalsIgnoreCase("public")
868 4212 daigle
    		 && pagesize == NONPAGESIZE && PropertyService.getProperty("database.queryCacheOn").equals("true"))
869 3342 tao
     {
870
       //System.out.println("the string stored into cache is "+ resultsetBuffer.toString());
871 6774 tao
  	   storeQueryResultIntoCache(queryCacheKey, resultsetBuffer.toString());
872 3342 tao
     }
873 3262 berkley
874 2087 tao
     return resultsetBuffer;
875
    }//findReturnDoclist
876 2043 sgarg
877
878 2087 tao
    /*
879
     * Send completed search hashtable(part of reulst)to output stream
880
     * and buffer into a buffer stream
881
     */
882
    private StringBuffer handleSubsetResult(QuerySpecification qspec,
883
                                           StringBuffer resultset,
884 5752 leinfelder
                                           Writer out, ResultDocumentSet partOfDoclist,
885 2087 tao
                                           String user, String[]groups,
886 5490 berkley
                                       DBConnection dbconn, boolean useXMLIndex,
887
                                       String qformat)
888 2087 tao
                                       throws Exception
889
   {
890 5165 daigle
     double startReturnFieldTime = System.currentTimeMillis();
891 2424 sgarg
     // check if there is a record in xml_returnfield
892
     // and get the returnfield_id and usage count
893
     int usage_count = getXmlReturnfieldsTableId(qspec, dbconn);
894
     boolean enterRecords = false;
895
896 4212 daigle
     // get value of database.xmlReturnfieldCount
897 4080 daigle
     int count = (new Integer(PropertyService
898 4212 daigle
                            .getProperty("database.xmlReturnfieldCount")))
899 2424 sgarg
                            .intValue();
900 2430 sgarg
901 2446 sgarg
     // set enterRecords to true if usage_count is more than the offset
902 2430 sgarg
     // specified in metacat.properties
903 2424 sgarg
     if(usage_count > count){
904
         enterRecords = true;
905
     }
906 3257 berkley
907 2421 sgarg
     if(returnfield_id < 0){
908 5165 daigle
         logMetacat.warn("DBQuery.handleSubsetResult - Error in getting returnfield id from"
909 2663 sgarg
                                  + "xml_returnfield table");
910 3227 berkley
         enterRecords = false;
911 2421 sgarg
     }
912
913
     // get the hashtable containing the docids that already in the
914
     // xml_queryresult table
915 5165 daigle
     logMetacat.info("DBQuery.handleSubsetResult - size of partOfDoclist before"
916 2421 sgarg
                             + " docidsInQueryresultTable(): "
917 2663 sgarg
                             + partOfDoclist.size());
918 5165 daigle
     long startGetReturnValueFromQueryresultable = System.currentTimeMillis();
919 2421 sgarg
     Hashtable queryresultDocList = docidsInQueryresultTable(returnfield_id,
920
                                                        partOfDoclist, dbconn);
921
922
     // remove the keys in queryresultDocList from partOfDoclist
923
     Enumeration _keys = queryresultDocList.keys();
924
     while (_keys.hasMoreElements()){
925 3246 berkley
         partOfDoclist.remove((String)_keys.nextElement());
926 2421 sgarg
     }
927 5165 daigle
928
     long queryResultReturnValuetime = System.currentTimeMillis() - startGetReturnValueFromQueryresultable;
929
     long queryResultWarnLimit =
930
    	 Long.parseLong(PropertyService.getProperty("dbquery.findQueryResultsTimeWarnLimit"));
931
932
     if (queryResultReturnValuetime > queryResultWarnLimit) {
933
    	 logMetacat.warn("DBQuery.handleSubsetResult - Time to get return fields from xml_queryresult table is (Part1 in return fields) " +
934
    		 queryResultReturnValuetime);
935
     }
936 4698 daigle
     MetacatUtil.writeDebugToFile("-----------------------------------------Get fields from xml_queryresult(Part1 in return fields) " +
937 5165 daigle
    		 queryResultReturnValuetime);
938
     MetacatUtil.writeDebugToDelimiteredFile(" " + queryResultReturnValuetime,false);
939
940
     long startExtendedQuery = System.currentTimeMillis();
941 2425 sgarg
     // backup the keys-elements in partOfDoclist to check later
942
     // if the doc entry is indexed yet
943
     Hashtable partOfDoclistBackup = new Hashtable();
944 3246 berkley
     Iterator itt = partOfDoclist.getDocids();
945
     while (itt.hasNext()){
946
       Object key = itt.next();
947 2425 sgarg
         partOfDoclistBackup.put(key, partOfDoclist.get(key));
948
     }
949
950 5165 daigle
     logMetacat.info("DBQuery.handleSubsetResult - size of partOfDoclist after"
951 2421 sgarg
                             + " docidsInQueryresultTable(): "
952 2663 sgarg
                             + partOfDoclist.size());
953 2421 sgarg
954
     //add return fields for the documents in partOfDoclist
955
     partOfDoclist = addReturnfield(partOfDoclist, qspec, user, groups,
956 5490 berkley
                                        dbconn, useXMLIndex, qformat);
957 5165 daigle
     long extendedQueryRunTime = startExtendedQuery - System.currentTimeMillis();
958
     long extendedQueryWarnLimit =
959
    	 Long.parseLong(PropertyService.getProperty("dbquery.extendedQueryRunTimeWarnLimit"));
960
961
     if (extendedQueryRunTime > extendedQueryWarnLimit) {
962
    	 logMetacat.warn("DBQuery.handleSubsetResult - Get fields from index and node table (Part2 in return fields) "
963
        		                                          + extendedQueryRunTime);
964
     }
965 4698 daigle
     MetacatUtil.writeDebugToFile("-----------------------------------------Get fields from extened query(Part2 in return fields) "
966 5165 daigle
             + extendedQueryRunTime);
967 4698 daigle
     MetacatUtil.writeDebugToDelimiteredFile(" "
968 5165 daigle
             + extendedQueryRunTime, false);
969 2421 sgarg
     //add relationship part part docid list for the documents in partOfDocList
970 3730 tao
     //partOfDoclist = addRelationship(partOfDoclist, qspec, dbconn, useXMLIndex);
971 2421 sgarg
972 5165 daigle
     long startStoreReturnField = System.currentTimeMillis();
973 3246 berkley
     Iterator keys = partOfDoclist.getDocids();
974 2087 tao
     String key = null;
975
     String element = null;
976 2421 sgarg
     String query = null;
977 4080 daigle
     int offset = (new Integer(PropertyService
978 4212 daigle
                               .getProperty("database.queryresultStringLength")))
979 2421 sgarg
                               .intValue();
980 3246 berkley
     while (keys.hasNext())
981 2087 tao
     {
982 3246 berkley
         key = (String) keys.next();
983 2421 sgarg
         element = (String)partOfDoclist.get(key);
984 3350 tao
985 2446 sgarg
	 // check if the enterRecords is true, elements is not null, element's
986
         // length is less than the limit of table column and if the document
987 2425 sgarg
         // has been indexed already
988 2446 sgarg
         if(enterRecords && element != null
989 2425 sgarg
		&& element.length() < offset
990
		&& element.compareTo((String) partOfDoclistBackup.get(key)) != 0){
991 2421 sgarg
             query = "INSERT INTO xml_queryresult (returnfield_id, docid, "
992 2446 sgarg
                 + "queryresult_string) VALUES (?, ?, ?)";
993
994 2421 sgarg
             PreparedStatement pstmt = null;
995
             pstmt = dbconn.prepareStatement(query);
996 2446 sgarg
             pstmt.setInt(1, returnfield_id);
997
             pstmt.setString(2, key);
998
             pstmt.setString(3, element);
999 3350 tao
1000 2421 sgarg
             dbconn.increaseUsageCount(1);
1001 3350 tao
             try
1002
             {
1003
            	 pstmt.execute();
1004
             }
1005
             catch(Exception e)
1006
             {
1007 5165 daigle
            	 logMetacat.warn("DBQuery.handleSubsetResult - couldn't insert the element to xml_queryresult table "+e.getLocalizedMessage());
1008 3350 tao
             }
1009
             finally
1010
             {
1011
                pstmt.close();
1012
             }
1013 2421 sgarg
         }
1014 3263 tao
1015 2421 sgarg
         // A string with element
1016
         String xmlElement = "  <document>" + element + "</document>";
1017 3257 berkley
1018 2421 sgarg
         //send single element to output
1019
         if (out != null)
1020
         {
1021 5752 leinfelder
             out.write(xmlElement);
1022 2421 sgarg
         }
1023
         resultset.append(xmlElement);
1024
     }//while
1025 3263 tao
1026 5165 daigle
     double storeReturnFieldTime = System.currentTimeMillis() - startStoreReturnField;
1027
     long storeReturnFieldWarnLimit =
1028
    	 Long.parseLong(PropertyService.getProperty("dbquery.storeReturnFieldTimeWarnLimit"));
1029
1030
     if (storeReturnFieldTime > storeReturnFieldWarnLimit) {
1031
    	 logMetacat.warn("DBQuery.handleSubsetResult - Time to store new return fields into xml_queryresult table (Part4 in return fields) "
1032
                   + storeReturnFieldTime);
1033
     }
1034 4698 daigle
     MetacatUtil.writeDebugToFile("-----------------------------------------Insert new record to xml_queryresult(Part4 in return fields) "
1035 5165 daigle
             + storeReturnFieldTime);
1036
     MetacatUtil.writeDebugToDelimiteredFile(" " + storeReturnFieldTime, false);
1037 3263 tao
1038 3246 berkley
     Enumeration keysE = queryresultDocList.keys();
1039
     while (keysE.hasMoreElements())
1040 2421 sgarg
     {
1041 3246 berkley
         key = (String) keysE.nextElement();
1042 2421 sgarg
         element = (String)queryresultDocList.get(key);
1043
         // A string with element
1044
         String xmlElement = "  <document>" + element + "</document>";
1045
         //send single element to output
1046
         if (out != null)
1047
         {
1048 5752 leinfelder
             out.write(xmlElement);
1049 2421 sgarg
         }
1050
         resultset.append(xmlElement);
1051
     }//while
1052 5165 daigle
     double returnFieldTime = System.currentTimeMillis() - startReturnFieldTime;
1053
     long totalReturnFieldWarnLimit =
1054
    	 Long.parseLong(PropertyService.getProperty("dbquery.totalReturnFieldTimeWarnLimit"));
1055
1056
     if (returnFieldTime > totalReturnFieldWarnLimit) {
1057
    	 logMetacat.warn("DBQuery.handleSubsetResult - Total time to get return fields is: "
1058
                           + returnFieldTime);
1059
     }
1060
     MetacatUtil.writeDebugToFile("DBQuery.handleSubsetResult - ---------------------------------------------------------------------------------------------------------------"+
1061
    		 "Total to get return fields  " + returnFieldTime);
1062
     MetacatUtil.writeDebugToDelimiteredFile("DBQuery.handleSubsetResult - "+ returnFieldTime, false);
1063 2421 sgarg
     return resultset;
1064
 }
1065
1066
   /**
1067
    * Get the docids already in xml_queryresult table and corresponding
1068
    * queryresultstring as a hashtable
1069
    */
1070
   private Hashtable docidsInQueryresultTable(int returnfield_id,
1071 3246 berkley
                                              ResultDocumentSet partOfDoclist,
1072 2421 sgarg
                                              DBConnection dbconn){
1073
1074
         Hashtable returnValue = new Hashtable();
1075
         PreparedStatement pstmt = null;
1076
         ResultSet rs = null;
1077 6629 leinfelder
1078
         // keep track of parameter values
1079
         List<Object> parameterValues = new ArrayList<Object>();
1080 2421 sgarg
1081
         // get partOfDoclist as string for the query
1082 3246 berkley
         Iterator keylist = partOfDoclist.getDocids();
1083 2421 sgarg
         StringBuffer doclist = new StringBuffer();
1084 3246 berkley
         while (keylist.hasNext())
1085 2421 sgarg
         {
1086 6629 leinfelder
             doclist.append("?,");
1087
             parameterValues.add((String) keylist.next());
1088 2421 sgarg
         }//while
1089
1090
         if (doclist.length() > 0)
1091
         {
1092
             doclist.deleteCharAt(doclist.length() - 1); //remove the last comma
1093
1094
             // the query to find out docids from xml_queryresult
1095
             String query = "select docid, queryresult_string from "
1096
                          + "xml_queryresult where returnfield_id = " +
1097
                          returnfield_id +" and docid in ("+ doclist + ")";
1098 5165 daigle
             logMetacat.info("DBQuery.docidsInQueryresultTable - Query to get docids from xml_queryresult:"
1099 2663 sgarg
                                      + query);
1100 2421 sgarg
1101
             try {
1102
                 // prepare and execute the query
1103
                 pstmt = dbconn.prepareStatement(query);
1104 6629 leinfelder
                 // bind parameter values
1105
                 pstmt = setPreparedStatementValues(parameterValues, pstmt);
1106
1107 2421 sgarg
                 dbconn.increaseUsageCount(1);
1108
                 pstmt.execute();
1109
                 rs = pstmt.getResultSet();
1110
                 boolean tableHasRows = rs.next();
1111
                 while (tableHasRows) {
1112
                     // store the returned results in the returnValue hashtable
1113
                     String key = rs.getString(1);
1114
                     String element = rs.getString(2);
1115
1116
                     if(element != null){
1117
                         returnValue.put(key, element);
1118
                     } else {
1119 5165 daigle
                         logMetacat.info("DBQuery.docidsInQueryresultTable - Null elment found ("
1120 2663 sgarg
                         + "DBQuery.docidsInQueryresultTable)");
1121 2421 sgarg
                     }
1122
                     tableHasRows = rs.next();
1123
                 }
1124
                 rs.close();
1125
                 pstmt.close();
1126
             } catch (Exception e){
1127 5165 daigle
                 logMetacat.error("DBQuery.docidsInQueryresultTable - Error getting docids from "
1128
                                          + "queryresult: " + e.getMessage());
1129 2421 sgarg
              }
1130
         }
1131
         return returnValue;
1132
     }
1133
1134
1135
   /**
1136
    * Method to get id from xml_returnfield table
1137
    * for a given query specification
1138
    */
1139 2424 sgarg
   private int returnfield_id;
1140 2421 sgarg
   private int getXmlReturnfieldsTableId(QuerySpecification qspec,
1141
                                           DBConnection dbconn){
1142
       int id = -1;
1143 2424 sgarg
       int count = 1;
1144 2421 sgarg
       PreparedStatement pstmt = null;
1145
       ResultSet rs = null;
1146
       String returnfield = qspec.getSortedReturnFieldString();
1147
1148
       // query for finding the id from xml_returnfield
1149 2446 sgarg
       String query = "SELECT returnfield_id, usage_count FROM xml_returnfield "
1150
            + "WHERE returnfield_string LIKE ?";
1151 5165 daigle
       logMetacat.info("DBQuery.getXmlReturnfieldsTableId - ReturnField Query:" + query);
1152 2421 sgarg
1153
       try {
1154
           // prepare and run the query
1155
           pstmt = dbconn.prepareStatement(query);
1156 2446 sgarg
           pstmt.setString(1,returnfield);
1157 2421 sgarg
           dbconn.increaseUsageCount(1);
1158
           pstmt.execute();
1159
           rs = pstmt.getResultSet();
1160
           boolean tableHasRows = rs.next();
1161
1162
           // if record found then increase the usage count
1163
           // else insert a new record and get the id of the new record
1164
           if(tableHasRows){
1165
               // get the id
1166
               id = rs.getInt(1);
1167 2424 sgarg
               count = rs.getInt(2) + 1;
1168 2421 sgarg
               rs.close();
1169
               pstmt.close();
1170
1171
               // increase the usage count
1172 6629 leinfelder
               query = "UPDATE xml_returnfield SET usage_count = ?"
1173
                   + " WHERE returnfield_id = ?";
1174 5165 daigle
               logMetacat.info("DBQuery.getXmlReturnfieldsTableId - ReturnField Table Update:"+ query);
1175 2421 sgarg
1176
               pstmt = dbconn.prepareStatement(query);
1177 6629 leinfelder
               pstmt.setInt(1, count);
1178
               pstmt.setInt(2, id);
1179 2421 sgarg
               dbconn.increaseUsageCount(1);
1180
               pstmt.execute();
1181
               pstmt.close();
1182
1183
           } else {
1184
               rs.close();
1185
               pstmt.close();
1186
1187
               // insert a new record
1188
               query = "INSERT INTO xml_returnfield (returnfield_string, usage_count)"
1189 2446 sgarg
                   + "VALUES (?, '1')";
1190 5165 daigle
               logMetacat.info("DBQuery.getXmlReturnfieldsTableId - ReturnField Table Insert:"+ query);
1191 2421 sgarg
               pstmt = dbconn.prepareStatement(query);
1192 2446 sgarg
               pstmt.setString(1, returnfield);
1193 2421 sgarg
               dbconn.increaseUsageCount(1);
1194
               pstmt.execute();
1195
               pstmt.close();
1196
1197
               // get the id of the new record
1198 2446 sgarg
               query = "SELECT returnfield_id FROM xml_returnfield "
1199
                   + "WHERE returnfield_string LIKE ?";
1200 5165 daigle
               logMetacat.info("DBQuery.getXmlReturnfieldsTableId - ReturnField query after Insert:" + query);
1201 2421 sgarg
               pstmt = dbconn.prepareStatement(query);
1202 2446 sgarg
               pstmt.setString(1, returnfield);
1203
1204 2421 sgarg
               dbconn.increaseUsageCount(1);
1205
               pstmt.execute();
1206
               rs = pstmt.getResultSet();
1207
               if(rs.next()){
1208
                   id = rs.getInt(1);
1209
               } else {
1210
                   id = -1;
1211
               }
1212
               rs.close();
1213
               pstmt.close();
1214 2087 tao
           }
1215 2091 tao
1216 2421 sgarg
       } catch (Exception e){
1217 5165 daigle
           logMetacat.error("DBQuery.getXmlReturnfieldsTableId - Error getting id from xml_returnfield in "
1218 2421 sgarg
                                     + "DBQuery.getXmlReturnfieldsTableId: "
1219 2663 sgarg
                                     + e.getMessage());
1220 2421 sgarg
           id = -1;
1221
       }
1222 2424 sgarg
1223
       returnfield_id = id;
1224
       return count;
1225 2087 tao
   }
1226 2043 sgarg
1227
1228 2087 tao
    /*
1229
     * A method to add return field to return doclist hash table
1230
     */
1231 3246 berkley
    private ResultDocumentSet addReturnfield(ResultDocumentSet docListResult,
1232 2087 tao
                                      QuerySpecification qspec,
1233
                                      String user, String[]groups,
1234 5490 berkley
                                      DBConnection dbconn, boolean useXMLIndex,
1235
                                      String qformat)
1236 2087 tao
                                      throws Exception
1237
    {
1238
      PreparedStatement pstmt = null;
1239
      ResultSet rs = null;
1240
      String docid = null;
1241
      String fieldname = null;
1242 3635 leinfelder
      String fieldtype = null;
1243 2087 tao
      String fielddata = null;
1244
      String relation = null;
1245 6629 leinfelder
      // keep track of parameter values
1246
      List<Object> parameterValues = new ArrayList<Object>();
1247 2087 tao
1248
      if (qspec.containsExtendedSQL())
1249
      {
1250
        qspec.setUserName(user);
1251
        qspec.setGroup(groups);
1252
        Vector extendedFields = new Vector(qspec.getReturnFieldList());
1253
        Vector results = new Vector();
1254 3246 berkley
        Iterator keylist = docListResult.getDocids();
1255 2087 tao
        StringBuffer doclist = new StringBuffer();
1256 6629 leinfelder
        List<Object> doclistValues = new ArrayList<Object>();
1257 2087 tao
        Vector parentidList = new Vector();
1258
        Hashtable returnFieldValue = new Hashtable();
1259 3246 berkley
        while (keylist.hasNext())
1260 2087 tao
        {
1261 5490 berkley
          String key = (String)keylist.next();
1262 6629 leinfelder
          doclist.append("?,");
1263
          doclistValues.add(key);
1264 2087 tao
        }
1265
        if (doclist.length() > 0)
1266
        {
1267
          Hashtable controlPairs = new Hashtable();
1268
          doclist.deleteCharAt(doclist.length() - 1); //remove the last comma
1269 3248 tao
          boolean tableHasRows = false;
1270 3349 tao
1271 2087 tao
1272 6629 leinfelder
1273 2087 tao
           String extendedQuery =
1274 6734 leinfelder
               qspec.printExtendedSQL(doclist.toString(), useXMLIndex, parameterValues, doclistValues);
1275
           // DO not add doclist values -- they are included in the query
1276
           //parameterValues.addAll(doclistValues);
1277 5165 daigle
           logMetacat.info("DBQuery.addReturnfield - Extended query: " + extendedQuery);
1278 2376 sgarg
1279 2474 sgarg
           if(extendedQuery != null){
1280 5165 daigle
//        	   long extendedQueryStart = System.currentTimeMillis();
1281 2474 sgarg
               pstmt = dbconn.prepareStatement(extendedQuery);
1282 6602 leinfelder
               // set the parameter values
1283
               pstmt = DBQuery.setPreparedStatementValues(parameterValues, pstmt);
1284 2474 sgarg
               //increase dbconnection usage count
1285
               dbconn.increaseUsageCount(1);
1286
               pstmt.execute();
1287
               rs = pstmt.getResultSet();
1288
               tableHasRows = rs.next();
1289
               while (tableHasRows) {
1290
                   ReturnFieldValue returnValue = new ReturnFieldValue();
1291
                   docid = rs.getString(1).trim();
1292
                   fieldname = rs.getString(2);
1293 5490 berkley
1294
                   if(qformat.toLowerCase().trim().equals("xml"))
1295
                   {
1296
                       byte[] b = rs.getBytes(3);
1297 5756 leinfelder
                       fielddata = new String(b, 0, b.length, MetaCatServlet.DEFAULT_ENCODING);
1298 5490 berkley
                   }
1299
                   else
1300
                   {
1301
                       fielddata = rs.getString(3);
1302
                   }
1303
1304
                   //System.out.println("raw fielddata: " + fielddata);
1305 4698 daigle
                   fielddata = MetacatUtil.normalize(fielddata);
1306 5490 berkley
                   //System.out.println("normalized fielddata: " + fielddata);
1307 2474 sgarg
                   String parentId = rs.getString(4);
1308 3635 leinfelder
                   fieldtype = rs.getString(5);
1309 2474 sgarg
                   StringBuffer value = new StringBuffer();
1310 2043 sgarg
1311 3635 leinfelder
                   //handle case when usexmlindex is true differently
1312
                   //at one point merging the nodedata (for large text elements) was
1313
                   //deemed unnecessary - but now it is needed.  but not for attribute nodes
1314 2474 sgarg
                   if (useXMLIndex || !containsKey(parentidList, parentId)) {
1315 3635 leinfelder
                	   //merge node data only for non-ATTRIBUTEs
1316
                	   if (fieldtype != null && !fieldtype.equals("ATTRIBUTE")) {
1317
	                	   //try merging the data
1318
	                	   ReturnFieldValue existingRFV =
1319
	                		   getArrayValue(parentidList, parentId);
1320 5387 berkley
	                	   if (existingRFV != null && !existingRFV.getFieldType().equals("ATTRIBUTE")) {
1321 3635 leinfelder
	                		   fielddata = existingRFV.getFieldValue() + fielddata;
1322
	                	   }
1323
                	   }
1324 5387 berkley
                	   //System.out.println("fieldname: " + fieldname + " fielddata: " + fielddata);
1325 5490 berkley
1326 2474 sgarg
                       value.append("<param name=\"");
1327
                       value.append(fieldname);
1328
                       value.append("\">");
1329
                       value.append(fielddata);
1330
                       value.append("</param>");
1331
                       //set returnvalue
1332
                       returnValue.setDocid(docid);
1333
                       returnValue.setFieldValue(fielddata);
1334 3635 leinfelder
                       returnValue.setFieldType(fieldtype);
1335 2474 sgarg
                       returnValue.setXMLFieldValue(value.toString());
1336
                       // Store it in hastable
1337
                       putInArray(parentidList, parentId, returnValue);
1338
                   }
1339
                   else {
1340 5490 berkley
1341 2474 sgarg
                       // need to merge nodedata if they have same parent id and
1342
                       // node type is text
1343
                       fielddata = (String) ( (ReturnFieldValue)
1344
                                             getArrayValue(
1345
                           parentidList, parentId)).getFieldValue()
1346
                           + fielddata;
1347 5490 berkley
                       //System.out.println("fieldname: " + fieldname + " fielddata: " + fielddata);
1348 2474 sgarg
                       value.append("<param name=\"");
1349
                       value.append(fieldname);
1350
                       value.append("\">");
1351
                       value.append(fielddata);
1352
                       value.append("</param>");
1353
                       returnValue.setDocid(docid);
1354
                       returnValue.setFieldValue(fielddata);
1355 3635 leinfelder
                       returnValue.setFieldType(fieldtype);
1356 2474 sgarg
                       returnValue.setXMLFieldValue(value.toString());
1357
                       // remove the old return value from paretnidList
1358
                       parentidList.remove(parentId);
1359
                       // store the new return value in parentidlit
1360
                       putInArray(parentidList, parentId, returnValue);
1361
                   }
1362
                   tableHasRows = rs.next();
1363
               } //while
1364
               rs.close();
1365
               pstmt.close();
1366 2043 sgarg
1367 2474 sgarg
               // put the merger node data info into doclistReult
1368
               Enumeration xmlFieldValue = (getElements(parentidList)).
1369
                   elements();
1370
               while (xmlFieldValue.hasMoreElements()) {
1371
                   ReturnFieldValue object =
1372
                       (ReturnFieldValue) xmlFieldValue.nextElement();
1373
                   docid = object.getDocid();
1374 3246 berkley
                   if (docListResult.containsDocid(docid)) {
1375 2474 sgarg
                       String removedelement = (String) docListResult.
1376
                           remove(docid);
1377
                       docListResult.
1378 3246 berkley
                           addResultDocument(new ResultDocument(docid,
1379
                               removedelement + object.getXMLFieldValue()));
1380 2474 sgarg
                   }
1381
                   else {
1382 3246 berkley
                       docListResult.addResultDocument(
1383
                         new ResultDocument(docid, object.getXMLFieldValue()));
1384 2474 sgarg
                   }
1385
               } //while
1386 5165 daigle
//               double docListResultEnd = System.currentTimeMillis() / 1000;
1387
//               logMetacat.warn(
1388
//                   "Time to prepare ResultDocumentSet after"
1389
//                   + " execute extended query: "
1390
//                   + (docListResultEnd - extendedQueryEnd));
1391 2474 sgarg
           }
1392 2087 tao
       }//if doclist lenght is great than zero
1393
     }//if has extended query
1394 2043 sgarg
1395 2087 tao
      return docListResult;
1396
    }//addReturnfield
1397 2043 sgarg
1398 3730 tao
1399 2087 tao
  /**
1400
   * removes the <?xml version="1.0"?> tag from the beginning.  This takes a
1401
   * string as a param instead of a hashtable.
1402
   *
1403
   * @param xmlquery a string representing a query.
1404
   */
1405
   private  String transformQuery(String xmlquery)
1406
   {
1407
     xmlquery = xmlquery.trim();
1408
     int index = xmlquery.indexOf("?>");
1409
     if (index != -1)
1410
     {
1411
       return xmlquery.substring(index + 2, xmlquery.length());
1412
     }
1413
     else
1414
     {
1415
       return xmlquery;
1416
     }
1417
   }
1418 3340 tao
1419
   /*
1420 3342 tao
    * Method to store query string and result xml string into query result
1421 3340 tao
    * cache. If the size alreay reache the limitation, the cache will be
1422
    * cleared first, then store them.
1423
    */
1424 3342 tao
   private void storeQueryResultIntoCache(String query, String resultXML)
1425 3340 tao
   {
1426
	   synchronized (queryResultCache)
1427
	   {
1428
		   if (queryResultCache.size() >= QUERYRESULTCACHESIZE)
1429
		   {
1430
			   queryResultCache.clear();
1431
		   }
1432 3342 tao
		   queryResultCache.put(query, resultXML);
1433 3340 tao
1434
	   }
1435
   }
1436
1437
   /*
1438 3342 tao
    * Method to get result xml string from query result cache.
1439
    * Note: the returned string can be null.
1440 3340 tao
    */
1441 3342 tao
   private String getResultXMLFromCache(String query)
1442 3340 tao
   {
1443 3342 tao
	   String resultSet = null;
1444 3340 tao
	   synchronized (queryResultCache)
1445
	   {
1446
          try
1447
          {
1448 5165 daigle
        	 logMetacat.info("DBQuery.getResultXMLFromCache - Get query from cache");
1449 3342 tao
		     resultSet = (String)queryResultCache.get(query);
1450 3340 tao
1451
          }
1452
          catch (Exception e)
1453
          {
1454
        	  resultSet = null;
1455
          }
1456
1457
	   }
1458
	   return resultSet;
1459
   }
1460
1461
   /**
1462
    * Method to clear the query result cache.
1463
    */
1464
   public static void clearQueryResultCache()
1465
   {
1466
	   synchronized (queryResultCache)
1467
	   {
1468
		   queryResultCache.clear();
1469
	   }
1470
   }
1471 6602 leinfelder
1472
   /**
1473
    * Set the parameter values in the prepared statement using instrospection
1474
    * of the given value objects
1475
    * @param parameterValues
1476
    * @param pstmt
1477
    * @return
1478
    * @throws SQLException
1479
    */
1480
   public static PreparedStatement setPreparedStatementValues(List<Object> parameterValues, PreparedStatement pstmt) throws SQLException {
1481
	   // set all the values we have collected
1482
      int parameterIndex = 1;
1483
      for (Object parameterValue: parameterValues) {
1484
    	  if (parameterValue instanceof String) {
1485
    		  pstmt.setString(parameterIndex, (String) parameterValue);
1486
    	  }
1487
    	  else if (parameterValue instanceof Integer) {
1488
    		  pstmt.setInt(parameterIndex, (Integer) parameterValue);
1489
    	  }
1490
    	  else if (parameterValue instanceof Float) {
1491
    		  pstmt.setFloat(parameterIndex, (Float) parameterValue);
1492
    	  }
1493
    	  else if (parameterValue instanceof Double) {
1494
    		  pstmt.setDouble(parameterIndex, (Double) parameterValue);
1495
    	  }
1496
    	  else if (parameterValue instanceof Date) {
1497
    		  pstmt.setTimestamp(parameterIndex, new Timestamp(((Date) parameterValue).getTime()));
1498
    	  }
1499
    	  else {
1500
    		  pstmt.setObject(parameterIndex, parameterValue);
1501
    	  }
1502
    	  parameterIndex++;
1503
      }
1504
      return pstmt;
1505
   }
1506 2087 tao
1507
1508 2075 jones
    /*
1509
     * A method to search if Vector contains a particular key string
1510
     */
1511
    private boolean containsKey(Vector parentidList, String parentId)
1512
    {
1513 2043 sgarg
1514 2075 jones
        Vector tempVector = null;
1515 2043 sgarg
1516 2075 jones
        for (int count = 0; count < parentidList.size(); count++) {
1517
            tempVector = (Vector) parentidList.get(count);
1518 2360 sgarg
            if (parentId.compareTo((String) tempVector.get(0)) == 0) { return true; }
1519 2075 jones
        }
1520
        return false;
1521 2043 sgarg
    }
1522 3635 leinfelder
1523 2075 jones
    /*
1524
     * A method to put key and value in Vector
1525
     */
1526
    private void putInArray(Vector parentidList, String key,
1527
            ReturnFieldValue value)
1528
    {
1529 2043 sgarg
1530 2075 jones
        Vector tempVector = null;
1531 3635 leinfelder
        //only filter if the field type is NOT an attribute (say, for text)
1532
        String fieldType = value.getFieldType();
1533
        if (fieldType != null && !fieldType.equals("ATTRIBUTE")) {
1534
1535
	        for (int count = 0; count < parentidList.size(); count++) {
1536
	            tempVector = (Vector) parentidList.get(count);
1537
1538
	            if (key.compareTo((String) tempVector.get(0)) == 0) {
1539
	                tempVector.remove(1);
1540
	                tempVector.add(1, value);
1541
	                return;
1542
	            }
1543
	        }
1544 2075 jones
        }
1545 2043 sgarg
1546 2075 jones
        tempVector = new Vector();
1547
        tempVector.add(0, key);
1548
        tempVector.add(1, value);
1549
        parentidList.add(tempVector);
1550
        return;
1551 2043 sgarg
    }
1552
1553 2075 jones
    /*
1554
     * A method to get value in Vector given a key
1555
     */
1556
    private ReturnFieldValue getArrayValue(Vector parentidList, String key)
1557 1353 tao
    {
1558 2043 sgarg
1559 2075 jones
        Vector tempVector = null;
1560 2043 sgarg
1561 5490 berkley
        for (int count = 0; count < parentidList.size(); count++) {
1562 2075 jones
            tempVector = (Vector) parentidList.get(count);
1563 2043 sgarg
1564 5490 berkley
            if (key.compareTo((String) tempVector.get(0)) == 0) { return (ReturnFieldValue) tempVector
1565
                    .get(1); }
1566 2075 jones
        }
1567
        return null;
1568 2045 tao
    }
1569 436 berkley
1570 2075 jones
    /*
1571
     * A method to get enumeration of all values in Vector
1572
     */
1573
    private Vector getElements(Vector parentidList)
1574 342 berkley
    {
1575 2446 sgarg
        Vector enumVector = new Vector();
1576 2075 jones
        Vector tempVector = null;
1577 2043 sgarg
1578 2075 jones
        for (int count = 0; count < parentidList.size(); count++) {
1579
            tempVector = (Vector) parentidList.get(count);
1580 744 jones
1581 2446 sgarg
            enumVector.add(tempVector.get(1));
1582 744 jones
        }
1583 2446 sgarg
        return enumVector;
1584 372 berkley
    }
1585 2043 sgarg
1586 3308 tao
1587 2043 sgarg
1588 2075 jones
    /*
1589
     * A method to create a query to get owner's docid list
1590
     */
1591 6629 leinfelder
    private String getOwnerQuery(String owner, List<Object> parameterValues)
1592 372 berkley
    {
1593 2075 jones
        if (owner != null) {
1594
            owner = owner.toLowerCase();
1595
        }
1596
        StringBuffer self = new StringBuffer();
1597 2043 sgarg
1598 2075 jones
        self.append("SELECT docid,docname,doctype,");
1599
        self.append("date_created, date_updated, rev ");
1600
        self.append("FROM xml_documents WHERE docid IN (");
1601
        self.append("(");
1602
        self.append("SELECT DISTINCT docid FROM xml_nodes WHERE \n");
1603
        self.append("nodedata LIKE '%%%' ");
1604
        self.append(") \n");
1605
        self.append(") ");
1606
        self.append(" AND (");
1607 6629 leinfelder
        self.append(" lower(user_owner) = ?");
1608 2075 jones
        self.append(") ");
1609 6629 leinfelder
        parameterValues.add(owner);
1610 2075 jones
        return self.toString();
1611 342 berkley
    }
1612 2043 sgarg
1613 2075 jones
    /**
1614
     * format a structured query as an XML document that conforms to the
1615
     * pathquery.dtd and is appropriate for submission to the DBQuery
1616
     * structured query engine
1617 2087 tao
     *
1618 2075 jones
     * @param params The list of parameters that should be included in the
1619
     *            query
1620
     */
1621 4080 daigle
    public static String createSQuery(Hashtable params) throws PropertyNotFoundException
1622 342 berkley
    {
1623 2075 jones
        StringBuffer query = new StringBuffer();
1624
        Enumeration elements;
1625
        Enumeration keys;
1626
        String filterDoctype = null;
1627
        String casesensitive = null;
1628
        String searchmode = null;
1629
        Object nextkey;
1630
        Object nextelement;
1631
        //add the xml headers
1632
        query.append("<?xml version=\"1.0\"?>\n");
1633 2091 tao
        query.append("<pathquery version=\"1.2\">\n");
1634 372 berkley
1635 2091 tao
1636
1637 2075 jones
        if (params.containsKey("meta_file_id")) {
1638
            query.append("<meta_file_id>");
1639
            query.append(((String[]) params.get("meta_file_id"))[0]);
1640
            query.append("</meta_file_id>");
1641 372 berkley
        }
1642 2043 sgarg
1643 2075 jones
        if (params.containsKey("returndoctype")) {
1644
            String[] returnDoctypes = ((String[]) params.get("returndoctype"));
1645
            for (int i = 0; i < returnDoctypes.length; i++) {
1646
                String doctype = (String) returnDoctypes[i];
1647 181 jones
1648 2075 jones
                if (!doctype.equals("any") && !doctype.equals("ANY")
1649
                        && !doctype.equals("")) {
1650
                    query.append("<returndoctype>").append(doctype);
1651
                    query.append("</returndoctype>");
1652
                }
1653
            }
1654
        }
1655 181 jones
1656 2075 jones
        if (params.containsKey("filterdoctype")) {
1657
            String[] filterDoctypes = ((String[]) params.get("filterdoctype"));
1658
            for (int i = 0; i < filterDoctypes.length; i++) {
1659
                query.append("<filterdoctype>").append(filterDoctypes[i]);
1660
                query.append("</filterdoctype>");
1661
            }
1662
        }
1663 181 jones
1664 2075 jones
        if (params.containsKey("returnfield")) {
1665
            String[] returnfield = ((String[]) params.get("returnfield"));
1666
            for (int i = 0; i < returnfield.length; i++) {
1667
                query.append("<returnfield>").append(returnfield[i]);
1668
                query.append("</returnfield>");
1669
            }
1670
        }
1671 2043 sgarg
1672 2075 jones
        if (params.containsKey("owner")) {
1673
            String[] owner = ((String[]) params.get("owner"));
1674
            for (int i = 0; i < owner.length; i++) {
1675
                query.append("<owner>").append(owner[i]);
1676
                query.append("</owner>");
1677
            }
1678
        }
1679 181 jones
1680 2075 jones
        if (params.containsKey("site")) {
1681
            String[] site = ((String[]) params.get("site"));
1682
            for (int i = 0; i < site.length; i++) {
1683
                query.append("<site>").append(site[i]);
1684
                query.append("</site>");
1685
            }
1686
        }
1687 2043 sgarg
1688 2075 jones
        //allows the dynamic switching of boolean operators
1689
        if (params.containsKey("operator")) {
1690
            query.append("<querygroup operator=\""
1691
                    + ((String[]) params.get("operator"))[0] + "\">");
1692
        } else { //the default operator is UNION
1693
            query.append("<querygroup operator=\"UNION\">");
1694
        }
1695 940 tao
1696 2075 jones
        if (params.containsKey("casesensitive")) {
1697
            casesensitive = ((String[]) params.get("casesensitive"))[0];
1698
        } else {
1699
            casesensitive = "false";
1700
        }
1701 2043 sgarg
1702 2075 jones
        if (params.containsKey("searchmode")) {
1703
            searchmode = ((String[]) params.get("searchmode"))[0];
1704
        } else {
1705
            searchmode = "contains";
1706 940 tao
        }
1707
1708 2075 jones
        //anyfield is a special case because it does a
1709
        //free text search. It does not have a <pathexpr>
1710
        //tag. This allows for a free text search within the structured
1711
        //query. This is useful if the INTERSECT operator is used.
1712
        if (params.containsKey("anyfield")) {
1713
            String[] anyfield = ((String[]) params.get("anyfield"));
1714
            //allow for more than one value for anyfield
1715
            for (int i = 0; i < anyfield.length; i++) {
1716 4135 berkley
                if (anyfield[i] != null && !anyfield[i].equals("")) {
1717 2075 jones
                    query.append("<queryterm casesensitive=\"" + casesensitive
1718
                            + "\" " + "searchmode=\"" + searchmode
1719
                            + "\"><value>" + anyfield[i]
1720
                            + "</value></queryterm>");
1721
                }
1722
            }
1723 940 tao
        }
1724 2043 sgarg
1725 2075 jones
        //this while loop finds the rest of the parameters
1726
        //and attempts to query for the field specified
1727
        //by the parameter.
1728
        elements = params.elements();
1729
        keys = params.keys();
1730
        while (keys.hasMoreElements() && elements.hasMoreElements()) {
1731
            nextkey = keys.nextElement();
1732
            nextelement = elements.nextElement();
1733 2043 sgarg
1734 2075 jones
            //make sure we aren't querying for any of these
1735
            //parameters since the are already in the query
1736
            //in one form or another.
1737
            Vector ignoredParams = new Vector();
1738
            ignoredParams.add("returndoctype");
1739
            ignoredParams.add("filterdoctype");
1740
            ignoredParams.add("action");
1741
            ignoredParams.add("qformat");
1742
            ignoredParams.add("anyfield");
1743
            ignoredParams.add("returnfield");
1744
            ignoredParams.add("owner");
1745
            ignoredParams.add("site");
1746
            ignoredParams.add("operator");
1747 2091 tao
            ignoredParams.add("sessionid");
1748 3211 berkley
            ignoredParams.add("pagesize");
1749
            ignoredParams.add("pagestart");
1750 4135 berkley
            ignoredParams.add("searchmode");
1751 2043 sgarg
1752 2075 jones
            // Also ignore parameters listed in the properties file
1753
            // so that they can be passed through to stylesheets
1754 4080 daigle
            String paramsToIgnore = PropertyService
1755 4173 daigle
                    .getProperty("database.queryignoredparams");
1756 2075 jones
            StringTokenizer st = new StringTokenizer(paramsToIgnore, ",");
1757
            while (st.hasMoreTokens()) {
1758
                ignoredParams.add(st.nextToken());
1759
            }
1760
            if (!ignoredParams.contains(nextkey.toString())) {
1761
                //allow for more than value per field name
1762
                for (int i = 0; i < ((String[]) nextelement).length; i++) {
1763
                    if (!((String[]) nextelement)[i].equals("")) {
1764
                        query.append("<queryterm casesensitive=\""
1765
                                + casesensitive + "\" " + "searchmode=\""
1766 2087 tao
                                + searchmode + "\">" + "<value>" +
1767 2075 jones
                                //add the query value
1768
                                ((String[]) nextelement)[i]
1769 2087 tao
                                + "</value><pathexpr>" +
1770 2075 jones
                                //add the path to query by
1771
                                nextkey.toString() + "</pathexpr></queryterm>");
1772
                    }
1773
                }
1774
            }
1775
        }
1776
        query.append("</querygroup></pathquery>");
1777
        //append on the end of the xml and return the result as a string
1778
        return query.toString();
1779
    }
1780 2043 sgarg
1781 2075 jones
    /**
1782
     * format a simple free-text value query as an XML document that conforms
1783
     * to the pathquery.dtd and is appropriate for submission to the DBQuery
1784
     * structured query engine
1785 2087 tao
     *
1786 2075 jones
     * @param value the text string to search for in the xml catalog
1787
     * @param doctype the type of documents to include in the result set -- use
1788
     *            "any" or "ANY" for unfiltered result sets
1789
     */
1790
    public static String createQuery(String value, String doctype)
1791 1292 tao
    {
1792 2075 jones
        StringBuffer xmlquery = new StringBuffer();
1793
        xmlquery.append("<?xml version=\"1.0\"?>\n");
1794
        xmlquery.append("<pathquery version=\"1.0\">");
1795 2043 sgarg
1796 2075 jones
        if (!doctype.equals("any") && !doctype.equals("ANY")) {
1797
            xmlquery.append("<returndoctype>");
1798
            xmlquery.append(doctype).append("</returndoctype>");
1799
        }
1800 2043 sgarg
1801 2075 jones
        xmlquery.append("<querygroup operator=\"UNION\">");
1802
        //chad added - 8/14
1803
        //the if statement allows a query to gracefully handle a null
1804
        //query. Without this if a nullpointerException is thrown.
1805
        if (!value.equals("")) {
1806
            xmlquery.append("<queryterm casesensitive=\"false\" ");
1807
            xmlquery.append("searchmode=\"contains\">");
1808
            xmlquery.append("<value>").append(value).append("</value>");
1809
            xmlquery.append("</queryterm>");
1810 1217 tao
        }
1811 2075 jones
        xmlquery.append("</querygroup>");
1812
        xmlquery.append("</pathquery>");
1813 2043 sgarg
1814 2075 jones
        return (xmlquery.toString());
1815
    }
1816 2043 sgarg
1817 2075 jones
    /**
1818
     * format a simple free-text value query as an XML document that conforms
1819
     * to the pathquery.dtd and is appropriate for submission to the DBQuery
1820
     * structured query engine
1821 2087 tao
     *
1822 2075 jones
     * @param value the text string to search for in the xml catalog
1823
     */
1824
    public static String createQuery(String value)
1825 940 tao
    {
1826 2075 jones
        return createQuery(value, "any");
1827 940 tao
    }
1828 2043 sgarg
1829 2075 jones
    /**
1830
     * Check for "READ" permission on @docid for @user and/or @group from DB
1831
     * connection
1832
     */
1833
    private boolean hasPermission(String user, String[] groups, String docid)
1834
            throws SQLException, Exception
1835 940 tao
    {
1836 2075 jones
        // Check for READ permission on @docid for @user and/or @groups
1837
        PermissionController controller = new PermissionController(docid);
1838
        return controller.hasPermission(user, groups,
1839
                AccessControlInterface.READSTRING);
1840
    }
1841 2043 sgarg
1842 2075 jones
    /**
1843
     * Get all docIds list for a data packadge
1844 2087 tao
     *
1845 2075 jones
     * @param dataPackageDocid, the string in docId field of xml_relation table
1846
     */
1847
    private Vector getCurrentDocidListForDataPackage(String dataPackageDocid)
1848 940 tao
    {
1849 2075 jones
        DBConnection dbConn = null;
1850
        int serialNumber = -1;
1851
        Vector docIdList = new Vector();//return value
1852
        PreparedStatement pStmt = null;
1853
        ResultSet rs = null;
1854
        String docIdInSubjectField = null;
1855
        String docIdInObjectField = null;
1856 2043 sgarg
1857 2075 jones
        // Check the parameter
1858
        if (dataPackageDocid == null || dataPackageDocid.equals("")) { return docIdList; }//if
1859 940 tao
1860 2075 jones
        //the query stirng
1861
        String query = "SELECT subject, object from xml_relation where docId = ?";
1862
        try {
1863
            dbConn = DBConnectionPool
1864
                    .getDBConnection("DBQuery.getCurrentDocidListForDataPackage");
1865
            serialNumber = dbConn.getCheckOutSerialNumber();
1866
            pStmt = dbConn.prepareStatement(query);
1867
            //bind the value to query
1868
            pStmt.setString(1, dataPackageDocid);
1869 2043 sgarg
1870 2075 jones
            //excute the query
1871
            pStmt.execute();
1872
            //get the result set
1873
            rs = pStmt.getResultSet();
1874
            //process the result
1875
            while (rs.next()) {
1876
                //In order to get the whole docIds in a data packadge,
1877
                //we need to put the docIds of subject and object field in
1878
                // xml_relation
1879
                //into the return vector
1880
                docIdInSubjectField = rs.getString(1);//the result docId in
1881
                                                      // subject field
1882
                docIdInObjectField = rs.getString(2);//the result docId in
1883
                                                     // object field
1884 940 tao
1885 2075 jones
                //don't put the duplicate docId into the vector
1886
                if (!docIdList.contains(docIdInSubjectField)) {
1887
                    docIdList.add(docIdInSubjectField);
1888
                }
1889 2043 sgarg
1890 2075 jones
                //don't put the duplicate docId into the vector
1891
                if (!docIdList.contains(docIdInObjectField)) {
1892
                    docIdList.add(docIdInObjectField);
1893
                }
1894
            }//while
1895
            //close the pStmt
1896
            pStmt.close();
1897
        }//try
1898
        catch (SQLException e) {
1899 5165 daigle
            logMetacat.error("DBQuery.getCurrentDocidListForDataPackage - Error in getDocidListForDataPackage: "
1900 2663 sgarg
                    + e.getMessage());
1901 2075 jones
        }//catch
1902
        finally {
1903
            try {
1904
                pStmt.close();
1905
            }//try
1906
            catch (SQLException ee) {
1907 5165 daigle
                logMetacat.error("DBQuery.getCurrentDocidListForDataPackage - SQL Error: "
1908 2663 sgarg
                                + ee.getMessage());
1909 2075 jones
            }//catch
1910
            finally {
1911
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1912
            }//fianlly
1913
        }//finally
1914
        return docIdList;
1915
    }//getCurrentDocidListForDataPackadge()
1916 2043 sgarg
1917 2075 jones
    /**
1918
     * Get all docIds list for a data packadge
1919 2087 tao
     *
1920 2075 jones
     * @param dataPackageDocid, the string in docId field of xml_relation table
1921
     */
1922 2641 tao
    private Vector getOldVersionDocidListForDataPackage(String dataPackageDocidWithRev)
1923 940 tao
    {
1924 2043 sgarg
1925 2075 jones
        Vector docIdList = new Vector();//return value
1926
        Vector tripleList = null;
1927
        String xml = null;
1928 2043 sgarg
1929 2075 jones
        // Check the parameter
1930 2641 tao
        if (dataPackageDocidWithRev == null || dataPackageDocidWithRev.equals("")) { return docIdList; }//if
1931 2043 sgarg
1932 2075 jones
        try {
1933
            //initial a documentImpl object
1934 2641 tao
            DocumentImpl packageDocument = new DocumentImpl(dataPackageDocidWithRev);
1935 2075 jones
            //transfer to documentImpl object to string
1936
            xml = packageDocument.toString();
1937 2043 sgarg
1938 2075 jones
            //create a tripcollection object
1939
            TripleCollection tripleForPackage = new TripleCollection(
1940
                    new StringReader(xml));
1941
            //get the vetor of triples
1942
            tripleList = tripleForPackage.getCollection();
1943 2043 sgarg
1944 2075 jones
            for (int i = 0; i < tripleList.size(); i++) {
1945
                //put subject docid into docIdlist without duplicate
1946
                if (!docIdList.contains(((Triple) tripleList.elementAt(i))
1947
                        .getSubject())) {
1948
                    //put subject docid into docIdlist
1949
                    docIdList.add(((Triple) tripleList.get(i)).getSubject());
1950
                }
1951
                //put object docid into docIdlist without duplicate
1952
                if (!docIdList.contains(((Triple) tripleList.elementAt(i))
1953
                        .getObject())) {
1954
                    docIdList.add(((Triple) (tripleList.get(i))).getObject());
1955
                }
1956
            }//for
1957
        }//try
1958
        catch (Exception e) {
1959 5165 daigle
            logMetacat.error("DBQuery.getCurrentDocidListForDataPackage - General error: "
1960 2663 sgarg
                    + e.getMessage());
1961 2075 jones
        }//catch
1962 2043 sgarg
1963 2075 jones
        // return result
1964
        return docIdList;
1965
    }//getDocidListForPackageInXMLRevisions()
1966 2043 sgarg
1967 2075 jones
    /**
1968
     * Check if the docId is a data packadge id. If the id is a data packadage
1969
     * id, it should be store in the docId fields in xml_relation table. So we
1970
     * can use a query to get the entries which the docId equals the given
1971
     * value. If the result is null. The docId is not a packadge id. Otherwise,
1972
     * it is.
1973 2087 tao
     *
1974 2075 jones
     * @param docId, the id need to be checked
1975
     */
1976
    private boolean isDataPackageId(String docId)
1977 940 tao
    {
1978 2075 jones
        boolean result = false;
1979
        PreparedStatement pStmt = null;
1980
        ResultSet rs = null;
1981
        String query = "SELECT docId from xml_relation where docId = ?";
1982
        DBConnection dbConn = null;
1983
        int serialNumber = -1;
1984
        try {
1985
            dbConn = DBConnectionPool
1986
                    .getDBConnection("DBQuery.isDataPackageId");
1987
            serialNumber = dbConn.getCheckOutSerialNumber();
1988
            pStmt = dbConn.prepareStatement(query);
1989
            //bind the value to query
1990
            pStmt.setString(1, docId);
1991
            //execute the query
1992
            pStmt.execute();
1993
            rs = pStmt.getResultSet();
1994
            //process the result
1995
            if (rs.next()) //There are some records for the id in docId fields
1996
            {
1997
                result = true;//It is a data packadge id
1998
            }
1999
            pStmt.close();
2000
        }//try
2001
        catch (SQLException e) {
2002 5165 daigle
            logMetacat.error("DBQuery.isDataPackageId - SQL Error: "
2003 2663 sgarg
                    + e.getMessage());
2004 2075 jones
        } finally {
2005
            try {
2006
                pStmt.close();
2007
            }//try
2008
            catch (SQLException ee) {
2009 5165 daigle
                logMetacat.error("DBQuery.isDataPackageId - SQL Error in isDataPackageId: "
2010 2663 sgarg
                        + ee.getMessage());
2011 2075 jones
            }//catch
2012
            finally {
2013
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
2014
            }//finally
2015
        }//finally
2016
        return result;
2017
    }//isDataPackageId()
2018 2043 sgarg
2019 6035 leinfelder
    public String getOperator() {
2020
		return operator;
2021
	}
2022
2023 2075 jones
    /**
2024 6035 leinfelder
     * Specifies if and how docid overrides should be included in the general query
2025
     * @param operator null, UNION, or INTERSECT (see QueryGroup)
2026
     */
2027
	public void setOperator(String operator) {
2028
		this.operator = operator;
2029
	}
2030
2031
	/**
2032 2075 jones
     * Check if the user has the permission to export data package
2033 2087 tao
     *
2034 2075 jones
     * @param conn, the connection
2035
     * @param docId, the id need to be checked
2036
     * @param user, the name of user
2037
     * @param groups, the user's group
2038
     */
2039
    private boolean hasPermissionToExportPackage(String docId, String user,
2040
            String[] groups) throws Exception
2041 940 tao
    {
2042 2075 jones
        //DocumentImpl doc=new DocumentImpl(conn,docId);
2043
        return DocumentImpl.hasReadPermission(user, groups, docId);
2044
    }
2045 2043 sgarg
2046 2075 jones
    /**
2047
     * Get the current Rev for a docid in xml_documents table
2048 2087 tao
     *
2049 2075 jones
     * @param docId, the id need to get version numb If the return value is -5,
2050
     *            means no value in rev field for this docid
2051
     */
2052
    private int getCurrentRevFromXMLDoumentsTable(String docId)
2053
            throws SQLException
2054
    {
2055
        int rev = -5;
2056
        PreparedStatement pStmt = null;
2057
        ResultSet rs = null;
2058
        String query = "SELECT rev from xml_documents where docId = ?";
2059
        DBConnection dbConn = null;
2060
        int serialNumber = -1;
2061
        try {
2062
            dbConn = DBConnectionPool
2063
                    .getDBConnection("DBQuery.getCurrentRevFromXMLDocumentsTable");
2064
            serialNumber = dbConn.getCheckOutSerialNumber();
2065
            pStmt = dbConn.prepareStatement(query);
2066
            //bind the value to query
2067
            pStmt.setString(1, docId);
2068
            //execute the query
2069
            pStmt.execute();
2070
            rs = pStmt.getResultSet();
2071
            //process the result
2072
            if (rs.next()) //There are some records for rev
2073
            {
2074
                rev = rs.getInt(1);
2075
                ;//It is the version for given docid
2076
            } else {
2077
                rev = -5;
2078
            }
2079 2043 sgarg
2080 1292 tao
        }//try
2081 2075 jones
        catch (SQLException e) {
2082 5165 daigle
            logMetacat.error("DBQuery.getCurrentRevFromXMLDoumentsTable - SQL Error: "
2083 2663 sgarg
                            + e.getMessage());
2084 2075 jones
            throw e;
2085 1292 tao
        }//catch
2086 2075 jones
        finally {
2087
            try {
2088
                pStmt.close();
2089
            }//try
2090
            catch (SQLException ee) {
2091 2663 sgarg
                logMetacat.error(
2092 5165 daigle
                        "DBQuery.getCurrentRevFromXMLDoumentsTable - SQL Error: "
2093 2663 sgarg
                                + ee.getMessage());
2094 2075 jones
            }//catch
2095
            finally {
2096
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
2097
            }//finally
2098
        }//finally
2099
        return rev;
2100
    }//getCurrentRevFromXMLDoumentsTable
2101 2043 sgarg
2102 2075 jones
    /**
2103
     * put a doc into a zip output stream
2104 2087 tao
     *
2105 2075 jones
     * @param docImpl, docmentImpl object which will be sent to zip output
2106
     *            stream
2107
     * @param zipOut, zip output stream which the docImpl will be put
2108
     * @param packageZipEntry, the zip entry name for whole package
2109
     */
2110
    private void addDocToZipOutputStream(DocumentImpl docImpl,
2111
            ZipOutputStream zipOut, String packageZipEntry)
2112
            throws ClassNotFoundException, IOException, SQLException,
2113
            McdbException, Exception
2114
    {
2115
        byte[] byteString = null;
2116
        ZipEntry zEntry = null;
2117 2043 sgarg
2118 5760 leinfelder
        byteString = docImpl.getBytes();
2119 2075 jones
        //use docId as the zip entry's name
2120
        zEntry = new ZipEntry(packageZipEntry + "/metadata/"
2121
                + docImpl.getDocID());
2122
        zEntry.setSize(byteString.length);
2123
        zipOut.putNextEntry(zEntry);
2124
        zipOut.write(byteString, 0, byteString.length);
2125
        zipOut.closeEntry();
2126 2043 sgarg
2127 2075 jones
    }//addDocToZipOutputStream()
2128 940 tao
2129 2075 jones
    /**
2130
     * Transfer a docid vetor to a documentImpl vector. The documentImpl vetor
2131
     * only inlcudes current version. If a DocumentImple object couldn't find
2132
     * for a docid, then the String of this docid was added to vetor rather
2133
     * than DocumentImple object.
2134 2087 tao
     *
2135 2075 jones
     * @param docIdList, a vetor hold a docid list for a data package. In
2136
     *            docid, there is not version number in it.
2137
     */
2138 2043 sgarg
2139 2075 jones
    private Vector getCurrentAllDocumentImpl(Vector docIdList)
2140
            throws McdbException, Exception
2141 940 tao
    {
2142 2075 jones
        //Connection dbConn=null;
2143
        Vector documentImplList = new Vector();
2144
        int rev = 0;
2145 2043 sgarg
2146 2075 jones
        // Check the parameter
2147
        if (docIdList.isEmpty()) { return documentImplList; }//if
2148 2043 sgarg
2149 2075 jones
        //for every docid in vector
2150
        for (int i = 0; i < docIdList.size(); i++) {
2151
            try {
2152
                //get newest version for this docId
2153
                rev = getCurrentRevFromXMLDoumentsTable((String) docIdList
2154
                        .elementAt(i));
2155 940 tao
2156 2075 jones
                // There is no record for this docId in xml_documents table
2157
                if (rev == -5) {
2158
                    // Rather than put DocumentImple object, put a String
2159
                    // Object(docid)
2160
                    // into the documentImplList
2161
                    documentImplList.add((String) docIdList.elementAt(i));
2162
                    // Skip other code
2163
                    continue;
2164
                }
2165 2043 sgarg
2166 2075 jones
                String docidPlusVersion = ((String) docIdList.elementAt(i))
2167 4212 daigle
                        + PropertyService.getProperty("document.accNumSeparator") + rev;
2168 2043 sgarg
2169 2075 jones
                //create new documentImpl object
2170
                DocumentImpl documentImplObject = new DocumentImpl(
2171
                        docidPlusVersion);
2172
                //add them to vector
2173
                documentImplList.add(documentImplObject);
2174
            }//try
2175
            catch (Exception e) {
2176 5165 daigle
                logMetacat.error("DBQuery.getCurrentAllDocumentImpl - General error: "
2177 2663 sgarg
                        + e.getMessage());
2178 2075 jones
                // continue the for loop
2179
                continue;
2180
            }
2181
        }//for
2182
        return documentImplList;
2183
    }
2184 2043 sgarg
2185 2075 jones
    /**
2186
     * Transfer a docid vetor to a documentImpl vector. If a DocumentImple
2187
     * object couldn't find for a docid, then the String of this docid was
2188
     * added to vetor rather than DocumentImple object.
2189 2087 tao
     *
2190 2075 jones
     * @param docIdList, a vetor hold a docid list for a data package. In
2191
     *            docid, t here is version number in it.
2192
     */
2193
    private Vector getOldVersionAllDocumentImpl(Vector docIdList)
2194
    {
2195
        //Connection dbConn=null;
2196
        Vector documentImplList = new Vector();
2197
        String siteCode = null;
2198
        String uniqueId = null;
2199
        int rev = 0;
2200 2043 sgarg
2201 2075 jones
        // Check the parameter
2202
        if (docIdList.isEmpty()) { return documentImplList; }//if
2203 2043 sgarg
2204 2075 jones
        //for every docid in vector
2205
        for (int i = 0; i < docIdList.size(); i++) {
2206 2043 sgarg
2207 2075 jones
            String docidPlusVersion = (String) (docIdList.elementAt(i));
2208
2209
            try {
2210
                //create new documentImpl object
2211
                DocumentImpl documentImplObject = new DocumentImpl(
2212
                        docidPlusVersion);
2213
                //add them to vector
2214
                documentImplList.add(documentImplObject);
2215
            }//try
2216
            catch (McdbDocNotFoundException notFoundE) {
2217 5165 daigle
                logMetacat.error("DBQuery.getOldVersionAllDocument - Error finding doc "
2218
                		+ docidPlusVersion + " : " + notFoundE.getMessage());
2219 2075 jones
                // Rather than add a DocumentImple object into vetor, a String
2220
                // object
2221
                // - the doicd was added to the vector
2222
                documentImplList.add(docidPlusVersion);
2223
                // Continue the for loop
2224
                continue;
2225
            }//catch
2226
            catch (Exception e) {
2227 2663 sgarg
                logMetacat.error(
2228 5165 daigle
                        "DBQuery.getOldVersionAllDocument - General error: "
2229 2663 sgarg
                                + e.getMessage());
2230 2075 jones
                // Continue the for loop
2231
                continue;
2232
            }//catch
2233
2234
        }//for
2235
        return documentImplList;
2236
    }//getOldVersionAllDocumentImple
2237
2238
    /**
2239
     * put a data file into a zip output stream
2240 2087 tao
     *
2241 2075 jones
     * @param docImpl, docmentImpl object which will be sent to zip output
2242
     *            stream
2243
     * @param zipOut, the zip output stream which the docImpl will be put
2244
     * @param packageZipEntry, the zip entry name for whole package
2245
     */
2246
    private void addDataFileToZipOutputStream(DocumentImpl docImpl,
2247
            ZipOutputStream zipOut, String packageZipEntry)
2248
            throws ClassNotFoundException, IOException, SQLException,
2249
            McdbException, Exception
2250 940 tao
    {
2251 2075 jones
        byte[] byteString = null;
2252
        ZipEntry zEntry = null;
2253
        // this is data file; add file to zip
2254 4080 daigle
        String filePath = PropertyService.getProperty("application.datafilepath");
2255 2075 jones
        if (!filePath.endsWith("/")) {
2256
            filePath += "/";
2257
        }
2258
        String fileName = filePath + docImpl.getDocID();
2259
        zEntry = new ZipEntry(packageZipEntry + "/data/" + docImpl.getDocID());
2260
        zipOut.putNextEntry(zEntry);
2261
        FileInputStream fin = null;
2262
        try {
2263
            fin = new FileInputStream(fileName);
2264
            byte[] buf = new byte[4 * 1024]; // 4K buffer
2265
            int b = fin.read(buf);
2266
            while (b != -1) {
2267
                zipOut.write(buf, 0, b);
2268
                b = fin.read(buf);
2269
            }//while
2270
            zipOut.closeEntry();
2271
        }//try
2272
        catch (IOException ioe) {
2273 5165 daigle
            logMetacat.error("DBQuery.addDataFileToZipOutputStream - I/O error: "
2274 2663 sgarg
                    + ioe.getMessage());
2275 2075 jones
        }//catch
2276
    }//addDataFileToZipOutputStream()
2277 2043 sgarg
2278 2075 jones
    /**
2279
     * create a html summary for data package and put it into zip output stream
2280 2087 tao
     *
2281 2075 jones
     * @param docImplList, the documentImpl ojbects in data package
2282
     * @param zipOut, the zip output stream which the html should be put
2283
     * @param packageZipEntry, the zip entry name for whole package
2284
     */
2285
    private void addHtmlSummaryToZipOutputStream(Vector docImplList,
2286
            ZipOutputStream zipOut, String packageZipEntry) throws Exception
2287
    {
2288
        StringBuffer htmlDoc = new StringBuffer();
2289
        ZipEntry zEntry = null;
2290
        byte[] byteString = null;
2291
        InputStream source;
2292
        DBTransform xmlToHtml;
2293 2043 sgarg
2294 2075 jones
        //create a DBTransform ojbect
2295
        xmlToHtml = new DBTransform();
2296
        //head of html
2297
        htmlDoc.append("<html><head></head><body>");
2298
        for (int i = 0; i < docImplList.size(); i++) {
2299
            // If this String object, this means it is missed data file
2300
            if ((((docImplList.elementAt(i)).getClass()).toString())
2301
                    .equals("class java.lang.String")) {
2302 2043 sgarg
2303 2075 jones
                htmlDoc.append("<a href=\"");
2304
                String dataFileid = (String) docImplList.elementAt(i);
2305
                htmlDoc.append("./data/").append(dataFileid).append("\">");
2306
                htmlDoc.append("Data File: ");
2307
                htmlDoc.append(dataFileid).append("</a><br>");
2308
                htmlDoc.append("<br><hr><br>");
2309 1356 tao
2310 2075 jones
            }//if
2311
            else if ((((DocumentImpl) docImplList.elementAt(i)).getDoctype())
2312
                    .compareTo("BIN") != 0) { //this is an xml file so we can
2313
                                              // transform it.
2314
                //transform each file individually then concatenate all of the
2315
                //transformations together.
2316 1356 tao
2317 2075 jones
                //for metadata xml title
2318
                htmlDoc.append("<h2>");
2319
                htmlDoc.append(((DocumentImpl) docImplList.elementAt(i))
2320
                        .getDocID());
2321
                //htmlDoc.append(".");
2322
                //htmlDoc.append(((DocumentImpl)docImplList.elementAt(i)).getRev());
2323
                htmlDoc.append("</h2>");
2324
                //do the actual transform
2325
                StringWriter docString = new StringWriter();
2326
                xmlToHtml.transformXMLDocument(((DocumentImpl) docImplList
2327
                        .elementAt(i)).toString(), "-//NCEAS//eml-generic//EN",
2328 5025 daigle
                        "-//W3C//HTML//EN", "html", docString, null, null);
2329 2075 jones
                htmlDoc.append(docString.toString());
2330
                htmlDoc.append("<br><br><hr><br><br>");
2331
            }//if
2332
            else { //this is a data file so we should link to it in the html
2333
                htmlDoc.append("<a href=\"");
2334
                String dataFileid = ((DocumentImpl) docImplList.elementAt(i))
2335
                        .getDocID();
2336
                htmlDoc.append("./data/").append(dataFileid).append("\">");
2337
                htmlDoc.append("Data File: ");
2338
                htmlDoc.append(dataFileid).append("</a><br>");
2339
                htmlDoc.append("<br><hr><br>");
2340
            }//else
2341
        }//for
2342
        htmlDoc.append("</body></html>");
2343 5760 leinfelder
        // use standard encoding even though the different docs might have use different encodings,
2344
        // the String objects in java should be correct and able to be encoded as the same Metacat default
2345
        byteString = htmlDoc.toString().getBytes(MetaCatServlet.DEFAULT_ENCODING);
2346 2075 jones
        zEntry = new ZipEntry(packageZipEntry + "/metadata.html");
2347
        zEntry.setSize(byteString.length);
2348
        zipOut.putNextEntry(zEntry);
2349
        zipOut.write(byteString, 0, byteString.length);
2350
        zipOut.closeEntry();
2351
        //dbConn.close();
2352 1356 tao
2353 2075 jones
    }//addHtmlSummaryToZipOutputStream
2354 1356 tao
2355 2075 jones
    /**
2356
     * put a data packadge into a zip output stream
2357 2087 tao
     *
2358 2641 tao
     * @param docId, which the user want to put into zip output stream,it has version
2359 2075 jones
     * @param out, a servletoutput stream which the zip output stream will be
2360
     *            put
2361
     * @param user, the username of the user
2362
     * @param groups, the group of the user
2363
     */
2364
    public ZipOutputStream getZippedPackage(String docIdString,
2365
            ServletOutputStream out, String user, String[] groups,
2366
            String passWord) throws ClassNotFoundException, IOException,
2367
            SQLException, McdbException, NumberFormatException, Exception
2368 945 tao
    {
2369 2075 jones
        ZipOutputStream zOut = null;
2370
        String elementDocid = null;
2371
        DocumentImpl docImpls = null;
2372
        //Connection dbConn = null;
2373
        Vector docIdList = new Vector();
2374
        Vector documentImplList = new Vector();
2375
        Vector htmlDocumentImplList = new Vector();
2376
        String packageId = null;
2377
        String rootName = "package";//the package zip entry name
2378 2043 sgarg
2379 2075 jones
        String docId = null;
2380
        int version = -5;
2381
        // Docid without revision
2382 5025 daigle
        docId = DocumentUtil.getDocIdFromString(docIdString);
2383 2075 jones
        // revision number
2384 5025 daigle
        version = DocumentUtil.getVersionFromString(docIdString);
2385 2043 sgarg
2386 2075 jones
        //check if the reqused docId is a data package id
2387
        if (!isDataPackageId(docId)) {
2388 2043 sgarg
2389 2075 jones
            /*
2390
             * Exception e = new Exception("The request the doc id "
2391
             * +docIdString+ " is not a data package id");
2392
             */
2393 940 tao
2394 2075 jones
            //CB 1/6/03: if the requested docid is not a datapackage, we just
2395
            // zip
2396
            //up the single document and return the zip file.
2397
            if (!hasPermissionToExportPackage(docId, user, groups)) {
2398 2043 sgarg
2399 2075 jones
                Exception e = new Exception("User " + user
2400
                        + " does not have permission"
2401
                        + " to export the data package " + docIdString);
2402
                throw e;
2403
            }
2404 2043 sgarg
2405 2641 tao
            docImpls = new DocumentImpl(docIdString);
2406 2075 jones
            //checking if the user has the permission to read the documents
2407
            if (DocumentImpl.hasReadPermission(user, groups, docImpls
2408
                    .getDocID())) {
2409
                zOut = new ZipOutputStream(out);
2410
                //if the docImpls is metadata
2411
                if ((docImpls.getDoctype()).compareTo("BIN") != 0) {
2412
                    //add metadata into zip output stream
2413
                    addDocToZipOutputStream(docImpls, zOut, rootName);
2414
                }//if
2415
                else {
2416
                    //it is data file
2417
                    addDataFileToZipOutputStream(docImpls, zOut, rootName);
2418
                    htmlDocumentImplList.add(docImpls);
2419
                }//else
2420 1292 tao
            }//if
2421 2043 sgarg
2422 2075 jones
            zOut.finish(); //terminate the zip file
2423
            return zOut;
2424
        }
2425
        // Check the permission of user
2426
        else if (!hasPermissionToExportPackage(docId, user, groups)) {
2427
2428
            Exception e = new Exception("User " + user
2429
                    + " does not have permission"
2430
                    + " to export the data package " + docIdString);
2431
            throw e;
2432
        } else //it is a packadge id
2433 1292 tao
        {
2434 2075 jones
            //store the package id
2435
            packageId = docId;
2436
            //get current version in database
2437
            int currentVersion = getCurrentRevFromXMLDoumentsTable(packageId);
2438
            //If it is for current version (-1 means user didn't specify
2439
            // revision)
2440
            if ((version == -1) || version == currentVersion) {
2441
                //get current version number
2442
                version = currentVersion;
2443
                //get package zip entry name
2444
                //it should be docId.revsion.package
2445 4212 daigle
                rootName = packageId + PropertyService.getProperty("document.accNumSeparator")
2446
                        + version + PropertyService.getProperty("document.accNumSeparator")
2447 2075 jones
                        + "package";
2448
                //get the whole id list for data packadge
2449
                docIdList = getCurrentDocidListForDataPackage(packageId);
2450
                //get the whole documentImple object
2451
                documentImplList = getCurrentAllDocumentImpl(docIdList);
2452 2043 sgarg
2453 1292 tao
            }//if
2454 2075 jones
            else if (version > currentVersion || version < -1) {
2455
                throw new Exception("The user specified docid: " + docId + "."
2456
                        + version + " doesn't exist");
2457
            }//else if
2458
            else //for an old version
2459 1292 tao
            {
2460 2075 jones
2461
                rootName = docIdString
2462 4212 daigle
                        + PropertyService.getProperty("document.accNumSeparator") + "package";
2463 2075 jones
                //get the whole id list for data packadge
2464
                docIdList = getOldVersionDocidListForDataPackage(docIdString);
2465
2466
                //get the whole documentImple object
2467
                documentImplList = getOldVersionAllDocumentImpl(docIdList);
2468 1292 tao
            }//else
2469 940 tao
2470 2075 jones
            // Make sure documentImplist is not empty
2471
            if (documentImplList.isEmpty()) { throw new Exception(
2472
                    "Couldn't find component for data package: " + packageId); }//if
2473 2043 sgarg
2474 2075 jones
            zOut = new ZipOutputStream(out);
2475
            //put every element into zip output stream
2476
            for (int i = 0; i < documentImplList.size(); i++) {
2477
                // if the object in the vetor is String, this means we couldn't
2478
                // find
2479
                // the document locally, we need find it remote
2480
                if ((((documentImplList.elementAt(i)).getClass()).toString())
2481
                        .equals("class java.lang.String")) {
2482
                    // Get String object from vetor
2483
                    String documentId = (String) documentImplList.elementAt(i);
2484 5165 daigle
                    logMetacat.info("DBQuery.getZippedPackage - docid: " + documentId);
2485 2075 jones
                    // Get doicd without revision
2486 5025 daigle
                    String docidWithoutRevision =
2487
                    	DocumentUtil.getDocIdFromString(documentId);
2488 5165 daigle
                    logMetacat.info("DBQuery.getZippedPackage - docidWithoutRevsion: "
2489 2663 sgarg
                            + docidWithoutRevision);
2490 2075 jones
                    // Get revision
2491 5025 daigle
                    String revision =
2492
                    	DocumentUtil.getRevisionStringFromString(documentId);
2493 5165 daigle
                    logMetacat.info("DBQuery.getZippedPackage - revision from docIdentifier: "
2494 2663 sgarg
                            + revision);
2495 2075 jones
                    // Zip entry string
2496
                    String zipEntryPath = rootName + "/data/";
2497
                    // Create a RemoteDocument object
2498
                    RemoteDocument remoteDoc = new RemoteDocument(
2499
                            docidWithoutRevision, revision, user, passWord,
2500
                            zipEntryPath);
2501
                    // Here we only read data file from remote metacat
2502
                    String docType = remoteDoc.getDocType();
2503
                    if (docType != null) {
2504
                        if (docType.equals("BIN")) {
2505
                            // Put remote document to zip output
2506
                            remoteDoc.readDocumentFromRemoteServerByZip(zOut);
2507
                            // Add String object to htmlDocumentImplList
2508
                            String elementInHtmlList = remoteDoc
2509
                                    .getDocIdWithoutRevsion()
2510 4212 daigle
                                    + PropertyService.getProperty("document.accNumSeparator")
2511 2075 jones
                                    + remoteDoc.getRevision();
2512
                            htmlDocumentImplList.add(elementInHtmlList);
2513
                        }//if
2514
                    }//if
2515 1361 tao
2516 2075 jones
                }//if
2517
                else {
2518
                    //create a docmentImpls object (represent xml doc) base on
2519
                    // the docId
2520
                    docImpls = (DocumentImpl) documentImplList.elementAt(i);
2521
                    //checking if the user has the permission to read the
2522
                    // documents
2523
                    if (DocumentImpl.hasReadPermission(user, groups, docImpls
2524
                            .getDocID())) {
2525
                        //if the docImpls is metadata
2526
                        if ((docImpls.getDoctype()).compareTo("BIN") != 0) {
2527
                            //add metadata into zip output stream
2528
                            addDocToZipOutputStream(docImpls, zOut, rootName);
2529
                            //add the documentImpl into the vetor which will
2530
                            // be used in html
2531
                            htmlDocumentImplList.add(docImpls);
2532 2043 sgarg
2533 2075 jones
                        }//if
2534
                        else {
2535
                            //it is data file
2536
                            addDataFileToZipOutputStream(docImpls, zOut,
2537
                                    rootName);
2538
                            htmlDocumentImplList.add(docImpls);
2539
                        }//else
2540
                    }//if
2541
                }//else
2542
            }//for
2543 2043 sgarg
2544 2075 jones
            //add html summary file
2545
            addHtmlSummaryToZipOutputStream(htmlDocumentImplList, zOut,
2546
                    rootName);
2547
            zOut.finish(); //terminate the zip file
2548
            //dbConn.close();
2549
            return zOut;
2550
        }//else
2551
    }//getZippedPackage()
2552 2043 sgarg
2553 2075 jones
    private class ReturnFieldValue
2554 1361 tao
    {
2555 2043 sgarg
2556 2075 jones
        private String docid = null; //return field value for this docid
2557 2043 sgarg
2558 2075 jones
        private String fieldValue = null;
2559 2043 sgarg
2560 2075 jones
        private String xmlFieldValue = null; //return field value in xml
2561
                                             // format
2562 3635 leinfelder
        private String fieldType = null; //ATTRIBUTE, TEXT...
2563 2075 jones
2564
        public void setDocid(String myDocid)
2565
        {
2566
            docid = myDocid;
2567
        }
2568
2569
        public String getDocid()
2570
        {
2571
            return docid;
2572
        }
2573
2574
        public void setFieldValue(String myValue)
2575
        {
2576
            fieldValue = myValue;
2577
        }
2578
2579
        public String getFieldValue()
2580
        {
2581
            return fieldValue;
2582
        }
2583
2584
        public void setXMLFieldValue(String xml)
2585
        {
2586
            xmlFieldValue = xml;
2587
        }
2588
2589
        public String getXMLFieldValue()
2590
        {
2591
            return xmlFieldValue;
2592
        }
2593 3635 leinfelder
2594
        public void setFieldType(String myType)
2595
        {
2596
            fieldType = myType;
2597
        }
2598 2075 jones
2599 3635 leinfelder
        public String getFieldType()
2600
        {
2601
            return fieldType;
2602
        }
2603
2604 1361 tao
    }
2605 3246 berkley
2606
    /**
2607
     * a class to store one result document consisting of a docid and a document
2608
     */
2609
    private class ResultDocument
2610
    {
2611
      public String docid;
2612
      public String document;
2613
2614
      public ResultDocument(String docid, String document)
2615
      {
2616
        this.docid = docid;
2617
        this.document = document;
2618
      }
2619
    }
2620
2621
    /**
2622
     * a private class to handle a set of resultDocuments
2623
     */
2624
    private class ResultDocumentSet
2625
    {
2626
      private Vector docids;
2627
      private Vector documents;
2628
2629
      public ResultDocumentSet()
2630
      {
2631
        docids = new Vector();
2632
        documents = new Vector();
2633
      }
2634
2635
      /**
2636
       * adds a result document to the set
2637
       */
2638
      public void addResultDocument(ResultDocument rd)
2639
      {
2640
        if(rd.docid == null)
2641 3263 tao
          return;
2642 3246 berkley
        if(rd.document == null)
2643
          rd.document = "";
2644 3349 tao
2645 3263 tao
           docids.addElement(rd.docid);
2646
           documents.addElement(rd.document);
2647 3349 tao
2648 3246 berkley
      }
2649
2650
      /**
2651
       * gets an iterator of docids
2652
       */
2653
      public Iterator getDocids()
2654
      {
2655
        return docids.iterator();
2656
      }
2657
2658
      /**
2659
       * gets an iterator of documents
2660
       */
2661
      public Iterator getDocuments()
2662
      {
2663
        return documents.iterator();
2664
      }
2665
2666
      /**
2667
       * returns the size of the set
2668
       */
2669
      public int size()
2670
      {
2671
        return docids.size();
2672
      }
2673
2674
      /**
2675
       * tests to see if this set contains the given docid
2676
       */
2677 3337 tao
      private boolean containsDocid(String docid)
2678 3246 berkley
      {
2679
        for(int i=0; i<docids.size(); i++)
2680
        {
2681
          String docid0 = (String)docids.elementAt(i);
2682
          if(docid0.trim().equals(docid.trim()))
2683
          {
2684
            return true;
2685
          }
2686
        }
2687
        return false;
2688
      }
2689
2690
      /**
2691
       * removes the element with the given docid
2692
       */
2693
      public String remove(String docid)
2694
      {
2695
        for(int i=0; i<docids.size(); i++)
2696
        {
2697
          String docid0 = (String)docids.elementAt(i);
2698
          if(docid0.trim().equals(docid.trim()))
2699
          {
2700
            String returnDoc = (String)documents.elementAt(i);
2701
            documents.remove(i);
2702
            docids.remove(i);
2703
            return returnDoc;
2704
          }
2705
        }
2706
        return null;
2707
      }
2708
2709
      /**
2710
       * add a result document
2711
       */
2712
      public void put(ResultDocument rd)
2713
      {
2714
        addResultDocument(rd);
2715
      }
2716
2717
      /**
2718
       * add a result document by components
2719
       */
2720
      public void put(String docid, String document)
2721
      {
2722
        addResultDocument(new ResultDocument(docid, document));
2723
      }
2724
2725
      /**
2726
       * get the document part of the result document by docid
2727
       */
2728
      public Object get(String docid)
2729
      {
2730
        for(int i=0; i<docids.size(); i++)
2731
        {
2732
          String docid0 = (String)docids.elementAt(i);
2733
          if(docid0.trim().equals(docid.trim()))
2734
          {
2735
            return documents.elementAt(i);
2736
          }
2737
        }
2738
        return null;
2739
      }
2740
2741
      /**
2742
       * get the document part of the result document by an object
2743
       */
2744
      public Object get(Object o)
2745
      {
2746
        return get((String)o);
2747
      }
2748
2749
      /**
2750
       * get an entire result document by index number
2751
       */
2752
      public ResultDocument get(int index)
2753
      {
2754
        return new ResultDocument((String)docids.elementAt(index),
2755
          (String)documents.elementAt(index));
2756
      }
2757
2758
      /**
2759
       * return a string representation of this object
2760
       */
2761
      public String toString()
2762
      {
2763
        String s = "";
2764
        for(int i=0; i<docids.size(); i++)
2765
        {
2766
          s += (String)docids.elementAt(i) + "\n";
2767
        }
2768
        return s;
2769
      }
2770 3263 tao
      /*
2771
       * Set a new document value for a given docid
2772
       */
2773
      public void set(String docid, String document)
2774
      {
2775
    	   for(int i=0; i<docids.size(); i++)
2776
           {
2777
             String docid0 = (String)docids.elementAt(i);
2778
             if(docid0.trim().equals(docid.trim()))
2779
             {
2780
                 documents.set(i, document);
2781
             }
2782
           }
2783
2784
      }
2785 3246 berkley
    }
2786 155 jones
}