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