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