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