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