Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that searches a relational DB for elements and
4
 *             attributes that have free text matches a query string,
5
 *             or structured query matches to a path specified node in the
6
 *             XML hierarchy.  It returns a result set consisting of the
7
 *             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
 *
12
 *   '$Author: tao $'
13
 *     '$Date: 2007-04-02 16:38:32 -0700 (Mon, 02 Apr 2007) $'
14
 * '$Revision: 3220 $'
15
 *
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
 */
30

    
31
package edu.ucsb.nceas.metacat;
32

    
33
import java.io.BufferedWriter;
34
import java.io.File;
35
import java.io.FileInputStream;
36
import java.io.FileOutputStream;
37
import java.io.FileReader;
38
import java.io.FileWriter;
39
import java.io.IOException;
40
import java.io.InputStream;
41
import java.io.PrintWriter;
42
import java.io.StringReader;
43
import java.io.StringWriter;
44
import java.io.OutputStream;
45
import java.sql.PreparedStatement;
46
import java.sql.ResultSet;
47
import java.sql.SQLException;
48
import java.util.Enumeration;
49
import java.util.Hashtable;
50
import java.util.StringTokenizer;
51
import java.util.Vector;
52
import java.util.zip.ZipEntry;
53
import java.util.zip.ZipOutputStream;
54

    
55
import javax.servlet.ServletOutputStream;
56
import javax.servlet.http.HttpServletResponse;
57
import javax.servlet.http.HttpSession;
58

    
59
import org.apache.log4j.Logger;
60

    
61
import org.w3c.dom.*;
62
import javax.xml.parsers.DocumentBuilderFactory;
63
import org.xml.sax.InputSource;
64
import org.w3c.dom.ls.*;
65

    
66
import edu.ucsb.nceas.morpho.datapackage.Triple;
67
import edu.ucsb.nceas.morpho.datapackage.TripleCollection;
68

    
69

    
70
/**
71
 * A Class that searches a relational DB for elements and attributes that have
72
 * free text matches a query string, or structured query matches to a path
73
 * specified node in the XML hierarchy. It returns a result set consisting of
74
 * the document ID for each document that satisfies the query
75
 */
76
public class DBQuery
77
{
78

    
79
    static final int ALL = 1;
80

    
81
    static final int WRITE = 2;
82

    
83
    static final int READ = 4;
84

    
85
    //private Connection conn = null;
86
    private String parserName = null;
87

    
88
    private MetaCatUtil util = new MetaCatUtil();
89

    
90
    private Logger logMetacat = Logger.getLogger(DBQuery.class);
91

    
92
    /** true if the metacat spatial option is installed **/
93
    private final boolean METACAT_SPATIAL = true;
94

    
95
    /** useful if you just want to grab a list of docids **/
96
    Vector docidOverride = new Vector();
97

    
98
    /**
99
     * the main routine used to test the DBQuery utility.
100
     * <p>
101
     * Usage: java DBQuery <xmlfile>
102
     *
103
     * @param xmlfile the filename of the xml file containing the query
104
     */
105
    static public void main(String[] args)
106
    {
107

    
108
        if (args.length < 1) {
109
            System.err.println("Wrong number of arguments!!!");
110
            System.err.println("USAGE: java DBQuery [-t] [-index] <xmlfile>");
111
            return;
112
        } else {
113
            try {
114

    
115
                int i = 0;
116
                boolean showRuntime = false;
117
                boolean useXMLIndex = false;
118
                if (args[i].equals("-t")) {
119
                    showRuntime = true;
120
                    i++;
121
                }
122
                if (args[i].equals("-index")) {
123
                    useXMLIndex = true;
124
                    i++;
125
                }
126
                String xmlfile = args[i];
127

    
128
                // Time the request if asked for
129
                double startTime = System.currentTimeMillis();
130

    
131
                // Open a connection to the database
132
                MetaCatUtil util = new MetaCatUtil();
133
                //Connection dbconn = util.openDBConnection();
134

    
135
                double connTime = System.currentTimeMillis();
136

    
137
                // Execute the query
138
                DBQuery queryobj = new DBQuery();
139
                FileReader xml = new FileReader(new File(xmlfile));
140
                Hashtable nodelist = null;
141
                //nodelist = queryobj.findDocuments(xml, null, null, useXMLIndex);
142

    
143
                // Print the reulting document listing
144
                StringBuffer result = new StringBuffer();
145
                String document = null;
146
                String docid = null;
147
                result.append("<?xml version=\"1.0\"?>\n");
148
                result.append("<resultset>\n");
149

    
150
                if (!showRuntime) {
151
                    Enumeration doclist = nodelist.keys();
152
                    while (doclist.hasMoreElements()) {
153
                        docid = (String) doclist.nextElement();
154
                        document = (String) nodelist.get(docid);
155
                        result.append("  <document>\n    " + document
156
                                + "\n  </document>\n");
157
                    }
158

    
159
                    result.append("</resultset>\n");
160
                }
161
                // Time the request if asked for
162
                double stopTime = System.currentTimeMillis();
163
                double dbOpenTime = (connTime - startTime) / 1000;
164
                double readTime = (stopTime - connTime) / 1000;
165
                double executionTime = (stopTime - startTime) / 1000;
166
                if (showRuntime) {
167
                    System.out.print("  " + executionTime);
168
                    System.out.print("  " + dbOpenTime);
169
                    System.out.print("  " + readTime);
170
                    System.out.print("  " + nodelist.size());
171
                    System.out.println();
172
                }
173
                //System.out.println(result);
174
                //write into a file "result.txt"
175
                if (!showRuntime) {
176
                    File f = new File("./result.txt");
177
                    FileWriter fw = new FileWriter(f);
178
                    BufferedWriter out = new BufferedWriter(fw);
179
                    out.write(result.toString());
180
                    out.flush();
181
                    out.close();
182
                    fw.close();
183
                }
184

    
185
            } catch (Exception e) {
186
                System.err.println("Error in DBQuery.main");
187
                System.err.println(e.getMessage());
188
                e.printStackTrace(System.err);
189
            }
190
        }
191
    }
192

    
193
    /**
194
     * construct an instance of the DBQuery class
195
     *
196
     * <p>
197
     * Generally, one would call the findDocuments() routine after creating an
198
     * instance to specify the search query
199
     * </p>
200
     *
201

    
202
     * @param parserName the fully qualified name of a Java class implementing
203
     *            the org.xml.sax.XMLReader interface
204
     */
205
    public DBQuery()
206
    {
207
        String parserName = MetaCatUtil.getOption("saxparser");
208
        this.parserName = parserName;
209
    }
210

    
211
    /**
212
     * 
213
     * Construct an instance of DBQuery Class
214
     * BUT accept a docid Vector that will supersede
215
     * the query.printSQL() method
216
     *
217
     * If a docid Vector is passed in,
218
     * the docids will be used to create a simple IN query 
219
     * without the multiple subselects of the printSQL() method
220
     *
221
     * Using this constructor, we just check for 
222
     * a docidOverride Vector in the findResultDoclist() method
223
     *
224
     * @param docids List of docids to display in the resultset
225
     */
226
    public DBQuery(Vector docids)
227
    {
228
        this.docidOverride = docids;
229
        String parserName = MetaCatUtil.getOption("saxparser");
230
        this.parserName = parserName;
231
    }
232

    
233
  /**
234
   * Method put the search result set into out printerwriter
235
   * @param resoponse the return response
236
   * @param out the output printer
237
   * @param params the paratermer hashtable
238
   * @param user the user name (it maybe different to the one in param)
239
   * @param groups the group array
240
   * @param sessionid  the sessionid
241
   */
242
  public void findDocuments(HttpServletResponse response,
243
                                       PrintWriter out, Hashtable params,
244
                                       String user, String[] groups,
245
                                       String sessionid)
246
  {
247
    boolean useXMLIndex = (new Boolean(MetaCatUtil.getOption("usexmlindex")))
248
               .booleanValue();
249
    findDocuments(response, out, params, user, groups, sessionid, useXMLIndex);
250

    
251
  }
252

    
253

    
254
    /**
255
     * Method put the search result set into out printerwriter
256
     * @param resoponse the return response
257
     * @param out the output printer
258
     * @param params the paratermer hashtable
259
     * @param user the user name (it maybe different to the one in param)
260
     * @param groups the group array
261
     * @param sessionid  the sessionid
262
     */
263
    public void findDocuments(HttpServletResponse response,
264
                                         PrintWriter out, Hashtable params,
265
                                         String user, String[] groups,
266
                                         String sessionid, boolean useXMLIndex)
267
    {
268
      int pagesize = 0;
269
      int pagestart = 0;
270
      
271
      if(params.containsKey("pagesize") && params.containsKey("pagestart"))
272
      {
273
        String pagesizeStr = ((String[])params.get("pagesize"))[0];
274
        String pagestartStr = ((String[])params.get("pagestart"))[0];
275
        if(pagesizeStr != null && pagestartStr != null)
276
        {
277
          pagesize = (new Integer(pagesizeStr)).intValue();
278
          pagestart = (new Integer(pagestartStr)).intValue();
279
        }
280
      }
281
      
282
      // get query and qformat
283
      String xmlquery = ((String[])params.get("query"))[0];
284

    
285
      logMetacat.warn("SESSIONID: " + sessionid);
286
      logMetacat.warn("xmlquery: " + xmlquery);
287
      String qformat = ((String[])params.get("qformat"))[0];
288
      logMetacat.warn("qformat: " + qformat);
289
      // Get the XML query and covert it into a SQL statment
290
      QuerySpecification qspec = null;
291
      if ( xmlquery != null)
292
      {
293
         xmlquery = transformQuery(xmlquery);
294
         try
295
         {
296
           qspec = new QuerySpecification(xmlquery,
297
                                          parserName,
298
                                          MetaCatUtil.getOption("accNumSeparator"));
299
         }
300
         catch (Exception ee)
301
         {
302
           logMetacat.error("error generating QuerySpecification object"
303
                                    +" in DBQuery.findDocuments"
304
                                    + ee.getMessage());
305
         }
306
      }
307

    
308

    
309

    
310
      if (qformat != null && qformat.equals(MetaCatServlet.XMLFORMAT))
311
      {
312
        //xml format
313
        response.setContentType("text/xml");
314
        createResultDocument(xmlquery, qspec, out, user, groups, useXMLIndex, 
315
          pagesize, pagestart, sessionid);
316
      }//if
317
      else
318
      {
319
        //knb format, in this case we will get whole result and sent it out
320
        response.setContentType("text/html");
321
        PrintWriter nonout = null;
322
        StringBuffer xml = createResultDocument(xmlquery, qspec, nonout, user,
323
                                                groups, useXMLIndex, pagesize, 
324
                                                pagestart, sessionid);
325
        
326
        //transfer the xml to html
327
        try
328
        {
329

    
330
         DBTransform trans = new DBTransform();
331
         response.setContentType("text/html");
332

    
333
         // if the user is a moderator, then pass a param to the 
334
         // xsl specifying the fact
335
         if(MetaCatUtil.isModerator(user, groups)){
336
        	 params.put("isModerator", new String[] {"true"});
337
         }
338

    
339
         trans.transformXMLDocument(xml.toString(), "-//NCEAS//resultset//EN",
340
                                 "-//W3C//HTML//EN", qformat, out, params,
341
                                 sessionid);
342

    
343
        }
344
        catch(Exception e)
345
        {
346
         logMetacat.error("Error in MetaCatServlet.transformResultset:"
347
                                +e.getMessage());
348
         }
349

    
350
      }//else
351

    
352
  }
353
    
354
  /**
355
   * this method parses the xml results in the string buffer and returns
356
   * just those required by the paging params.
357
   */
358
  private StringBuffer getPagedResult(MetacatResultSet mrs, int pagestart, 
359
    int pagesize)
360
  {
361
    logMetacat.warn(mrs.toString());
362
    if(pagesize == 0)
363
    { //if pagesize is 0 then we return the whole resultset
364
      return new StringBuffer(mrs.toString());
365
    }
366
    
367
    return new StringBuffer(mrs.serializeToXML(pagestart, pagestart + pagesize));
368
  }
369
  
370
  
371
  /**
372
   * Transforms a hashtable of documents to an xml or html result and sent
373
   * the content to outputstream. Keep going untill hastable is empty. stop it.
374
   * add the QuerySpecification as parameter is for ecogrid. But it is duplicate
375
   * to xmlquery String
376
   * @param xmlquery
377
   * @param qspec
378
   * @param out
379
   * @param user
380
   * @param groups
381
   * @param useXMLIndex
382
   * @param sessionid
383
   * @return
384
   */
385
    public StringBuffer createResultDocument(String xmlquery,
386
                                              QuerySpecification qspec,
387
                                              PrintWriter out,
388
                                              String user, String[] groups,
389
                                              boolean useXMLIndex)
390
    {
391
    	return createResultDocument(xmlquery,qspec,out, user,groups, useXMLIndex, 0, 0,"");
392
    }
393

    
394
  /*
395
   * Transforms a hashtable of documents to an xml or html result and sent
396
   * the content to outputstream. Keep going untill hastable is empty. stop it.
397
   * add the QuerySpecification as parameter is for ecogrid. But it is duplicate
398
   * to xmlquery String
399
   */
400
  public StringBuffer createResultDocument(String xmlquery,
401
                                            QuerySpecification qspec,
402
                                            PrintWriter out,
403
                                            String user, String[] groups,
404
                                            boolean useXMLIndex, int pagesize,
405
                                            int pagestart, String sessionid)
406
  {
407
    DBConnection dbconn = null;
408
    int serialNumber = -1;
409
    StringBuffer resultset = new StringBuffer();
410

    
411
    //try to get the cached version first    
412
    Hashtable sessionHash = MetaCatServlet.getSessionHash();
413
    HttpSession sess = (HttpSession)sessionHash.get(sessionid);
414

    
415
    QuerySpecification cachedQuerySpec = null;
416
    if (sess != null)
417
    {
418
    	cachedQuerySpec = (QuerySpecification)sess.getAttribute("query");
419
    }
420
    
421
    if(cachedQuerySpec != null && 
422
       cachedQuerySpec.printSQL(false).equals(qspec.printSQL(false)))
423
    { //use the cached resultset if the query was the same as the last
424
      MetacatResultSet mrs = (MetacatResultSet)sess.getAttribute("results");
425
      logMetacat.info("Using cached query results");
426
      //if the query is the same and the session contains the query
427
      //results, return those instead of rerunning the query
428
      if(mrs != null)
429
      { //print and return the cached buffer
430
        StringBuffer pagedResultBuffer = getPagedResult(mrs, pagestart, 
431
          pagesize);
432
        if(out != null)
433
        {
434
          out.println("<?xml version=\"1.0\"?>\n");
435
          out.println("<resultset>\n");
436
          out.println("  <query>" + xmlquery + "</query>\n");
437
          out.println(pagedResultBuffer.toString());
438
          out.println("\n</resultset>\n");
439
        }
440
        String returnString = "<?xml version=\"1.0\"?>\n";
441
        returnString += "<resultset>\n";
442
        returnString += "  <query>" + xmlquery + "</query>\n";
443
        returnString += pagedResultBuffer.toString();
444
        returnString += "\n</resultset>\n";
445
        return new StringBuffer(returnString);
446
      }
447
    }
448
    
449
    //no cached results...go on with a normal query
450
    
451
    resultset.append("<?xml version=\"1.0\"?>\n");
452
    resultset.append("<resultset>\n");
453
    resultset.append("  <query>" + xmlquery + "</query>");
454
    //send out a new query
455
    if (out != null)
456
    {
457
      out.println(resultset.toString());
458
    }
459
    if (qspec != null)
460
    {
461
      try
462
      {
463

    
464
        //checkout the dbconnection
465
        dbconn = DBConnectionPool.getDBConnection("DBQuery.findDocuments");
466
        serialNumber = dbconn.getCheckOutSerialNumber();
467

    
468
        //print out the search result
469
        // search the doc list
470
        resultset = findResultDoclist(qspec, resultset, out, user, groups,
471
                                      dbconn, useXMLIndex, pagesize, pagestart, 
472
                                      sessionid);
473

    
474
      } //try
475
      catch (IOException ioe)
476
      {
477
        logMetacat.error("IO error in DBQuery.findDocuments:");
478
        logMetacat.error(ioe.getMessage());
479

    
480
      }
481
      catch (SQLException e)
482
      {
483
        logMetacat.error("SQL Error in DBQuery.findDocuments: "
484
                                 + e.getMessage());
485
      }
486
      catch (Exception ee)
487
      {
488
        logMetacat.error("Exception in DBQuery.findDocuments: "
489
                                 + ee.getMessage());
490
        ee.printStackTrace();
491
      }
492
      finally
493
      {
494
        DBConnectionPool.returnDBConnection(dbconn, serialNumber);
495
      } //finally
496
    }//if
497
    String closeRestultset = "</resultset>";
498
    resultset.append(closeRestultset);
499
    if (out != null)
500
    {
501
      out.println(closeRestultset);
502
    }
503

    
504
    //create a DOM to cache
505
    try
506
    {
507
      
508
      //cache the query result and the query
509
      logMetacat.info("Caching query and resultset");
510
      sess.setAttribute("query", qspec);
511
      MetacatResultSet mrs = processAndCacheResults(resultset.toString(), sess);
512
      sess.setAttribute("results", mrs);
513
      StringBuffer pagedResultBuffer = getPagedResult(mrs, pagestart, pagesize);
514
      String returnString = "<?xml version=\"1.0\"?>\n";
515
      returnString += "<resultset>\n";
516
      returnString += "  <query>" + xmlquery + "</query>\n";
517
      returnString += pagedResultBuffer.toString();
518
      returnString += "\n</resultset>\n";
519
      return new StringBuffer(returnString);
520
    }
521
    catch(Exception e)
522
    {
523
      logMetacat.error("################Could not parse resultset: " + e.getMessage());
524
    }
525
    
526
    return resultset;
527
  }//createResultDocuments
528

    
529
  /**
530
   * parse the dom of the resultset into a MetacatResultSet object so it can
531
   * be cached in a reasonable way
532
   */
533
  private MetacatResultSet processAndCacheResults(String resultset, HttpSession sess)
534
    throws Exception
535
  {
536
    StringReader sreader = new StringReader(resultset.toString());
537
    InputSource inputsource = new InputSource(sreader);
538
    logMetacat.warn("processing DOM");
539
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputsource);
540
    //got the dom, now process it into an MRS
541
    MetacatResultSet mrs = new MetacatResultSet(doc);
542
    return mrs;
543
  }
544

    
545
    /*
546
     * Find the doc list which match the query
547
     */
548
    private StringBuffer findResultDoclist(QuerySpecification qspec,
549
                                      StringBuffer resultsetBuffer,
550
                                      PrintWriter out,
551
                                      String user, String[]groups,
552
                                      DBConnection dbconn, boolean useXMLIndex,
553
                                      int pagesize, int pagestart, String sessionid)
554
                                      throws Exception
555
    {
556
      String query = null;
557
      int count = 0;
558
      int index = 0;
559
      Hashtable docListResult = new Hashtable();
560
      PreparedStatement pstmt = null;
561
      String docid = null;
562
      String docname = null;
563
      String doctype = null;
564
      String createDate = null;
565
      String updateDate = null;
566
      StringBuffer document = null;
567
      int rev = 0;
568
      double startTime = 0;
569
      int offset = 1;
570
      
571
      ResultSet rs = null;
572
        
573
      offset = 1;
574
      // this is a hack for offset
575
      if (out == null)
576
      {
577
        // for html page, we put everything into one page
578
        offset =
579
            (new Integer(MetaCatUtil.getOption("web_resultsetsize"))).intValue();
580
      }
581
      else
582
      {
583
          offset =
584
              (new Integer(MetaCatUtil.getOption("app_resultsetsize"))).intValue();
585
      }
586

    
587
      /*
588
       * Check the docidOverride Vector
589
       * if defined, we bypass the qspec.printSQL() method
590
       * and contruct a simpler query based on a 
591
       * list of docids rather than a bunch of subselects
592
       */
593
      if ( this.docidOverride.size() == 0 ) {
594
          query = qspec.printSQL(useXMLIndex);
595
      } else {
596
          logMetacat.info("*** docid override " + this.docidOverride.size());
597
          StringBuffer queryBuffer = new StringBuffer( "SELECT docid,docname,doctype,date_created, date_updated, rev " );
598
          queryBuffer.append( " FROM xml_documents WHERE docid IN (" );
599
          for (int i = 0; i < docidOverride.size(); i++) {  
600
              queryBuffer.append("'");
601
              queryBuffer.append( (String)docidOverride.elementAt(i) );
602
              queryBuffer.append("',");
603
          }
604
          // empty string hack 
605
          queryBuffer.append( "'') " );
606
          query = queryBuffer.toString();
607
      } 
608

    
609
      String ownerQuery = getOwnerQuery(user);
610
      logMetacat.info("\n\n\n query: " + query);
611
      logMetacat.info("\n\n\n owner query: "+ownerQuery);
612
      // if query is not the owner query, we need to check the permission
613
      // otherwise we don't need (owner has all permission by default)
614
      if (!query.equals(ownerQuery))
615
      {
616
        // set user name and group
617
        qspec.setUserName(user);
618
        qspec.setGroup(groups);
619
        // Get access query
620
        String accessQuery = qspec.getAccessQuery();
621
        if(!query.endsWith("WHERE")){
622
            query = query + accessQuery;
623
        } else {
624
            query = query + accessQuery.substring(4, accessQuery.length());
625
        }
626
        logMetacat.warn("\n\n\n final query: " + query);
627
      }
628

    
629
      startTime = System.currentTimeMillis() / 1000;
630
      pstmt = dbconn.prepareStatement(query);
631
      rs = pstmt.executeQuery();
632
      //now we need to process the resultset based on pagesize and pagestart
633
      //if they are not 0
634
      double queryExecuteTime = System.currentTimeMillis() / 1000;
635
      logMetacat.warn("Time to execute query: "
636
                    + (queryExecuteTime - startTime));
637
      boolean tableHasRows = rs.next();
638
      while (tableHasRows)
639
      {
640
        docid = rs.getString(1).trim();
641
        docname = rs.getString(2);
642
        doctype = rs.getString(3);
643
        createDate = rs.getString(4);
644
        updateDate = rs.getString(5);
645
        rev = rs.getInt(6);
646

    
647
        // if there are returndocs to match, backtracking can be performed
648
        // otherwise, just return the document that was hit
649
        Vector returndocVec = qspec.getReturnDocList();
650
         if (returndocVec.size() != 0 && !returndocVec.contains(doctype)
651
                        && !qspec.isPercentageSearch())
652
         {
653
           logMetacat.warn("Back tracing now...");
654
           String sep = MetaCatUtil.getOption("accNumSeparator");
655
           StringBuffer btBuf = new StringBuffer();
656
           btBuf.append("select docid from xml_relation where ");
657

    
658
           //build the doctype list for the backtracking sql statement
659
           btBuf.append("packagetype in (");
660
           for (int i = 0; i < returndocVec.size(); i++)
661
           {
662
             btBuf.append("'").append((String) returndocVec.get(i)).append("'");
663
             if (i != (returndocVec.size() - 1))
664
             {
665
                btBuf.append(", ");
666
              }
667
            }
668
            btBuf.append(") ");
669
            btBuf.append("and (subject like '");
670
            btBuf.append(docid).append("'");
671
            btBuf.append("or object like '");
672
            btBuf.append(docid).append("')");
673

    
674
            PreparedStatement npstmt = dbconn.prepareStatement(btBuf.toString());
675
            //should incease usage count
676
            dbconn.increaseUsageCount(1);
677
            npstmt.execute();
678
            ResultSet btrs = npstmt.getResultSet();
679
            boolean hasBtRows = btrs.next();
680
            while (hasBtRows)
681
            {
682
               //there was a backtrackable document found
683
               DocumentImpl xmldoc = null;
684
               String packageDocid = btrs.getString(1);
685
               logMetacat.info("Getting document for docid: "
686
                                         + packageDocid);
687
                try
688
                {
689
                    //  THIS CONSTRUCTOR BUILDS THE WHOLE XML doc not
690
                    // needed here
691
                    // xmldoc = new DocumentImpl(dbconn, packageDocid);
692
                    //  thus use the following to get the doc info only
693
                    //  xmldoc = new DocumentImpl(dbconn);
694
                    String accNumber = packageDocid + MetaCatUtil.getOption("accNumSeparator") +
695
                    DBUtil.getLatestRevisionInDocumentTable(packageDocid);
696
                    xmldoc = new DocumentImpl(accNumber, false);
697
                    if (xmldoc == null)
698
                    {
699
                       logMetacat.info("Document was null for: "
700
                                                + packageDocid);
701
                    }
702
                }
703
                catch (Exception e)
704
                {
705
                    System.out.println("Error getting document in "
706
                                       + "DBQuery.findDocuments: "
707
                                       + e.getMessage());
708
                }
709

    
710
                String docid_org = xmldoc.getDocID();
711
                if (docid_org == null)
712
                {
713
                   logMetacat.info("Docid_org was null.");
714
                   //continue;
715
                }
716
                docid = docid_org.trim();
717
                docname = xmldoc.getDocname();
718
                doctype = xmldoc.getDoctype();
719
                createDate = xmldoc.getCreateDate();
720
                updateDate = xmldoc.getUpdateDate();
721
                rev = xmldoc.getRev();
722
                document = new StringBuffer();
723

    
724
                String completeDocid = docid
725
                                + MetaCatUtil.getOption("accNumSeparator");
726
                completeDocid += rev;
727
                document.append("<docid>").append(completeDocid);
728
                document.append("</docid>");
729
                if (docname != null)
730
                {
731
                  document.append("<docname>" + docname + "</docname>");
732
                }
733
                if (doctype != null)
734
                {
735
                  document.append("<doctype>" + doctype + "</doctype>");
736
                }
737
                if (createDate != null)
738
                {
739
                 document.append("<createdate>" + createDate + "</createdate>");
740
                }
741
                if (updateDate != null)
742
                {
743
                  document.append("<updatedate>" + updateDate+ "</updatedate>");
744
                }
745
                // Store the document id and the root node id
746
                docListResult.put(docid, (String) document.toString());
747
                count++;
748

    
749

    
750
                // Get the next package document linked to our hit
751
                hasBtRows = btrs.next();
752
              }//while
753
              npstmt.close();
754
              btrs.close();
755
        }
756
        else if (returndocVec.size() == 0 || returndocVec.contains(doctype))
757
        {
758

    
759
           document = new StringBuffer();
760

    
761
           String completeDocid = docid
762
                            + MetaCatUtil.getOption("accNumSeparator");
763
           completeDocid += rev;
764
           document.append("<docid>").append(completeDocid).append("</docid>");
765
           if (docname != null)
766
           {
767
               document.append("<docname>" + docname + "</docname>");
768
           }
769
           if (doctype != null)
770
           {
771
              document.append("<doctype>" + doctype + "</doctype>");
772
           }
773
           if (createDate != null)
774
           {
775
               document.append("<createdate>" + createDate + "</createdate>");
776
           }
777
           if (updateDate != null)
778
           {
779
             document.append("<updatedate>" + updateDate + "</updatedate>");
780
           }
781
           // Store the document id and the root node id
782
           docListResult.put(docid, (String) document.toString());
783
           count++;
784

    
785
        }//else
786
        // when doclist reached the offset number, send out doc list and empty
787
        // the hash table
788
        if (count == offset)
789
        {
790
          //reset count
791
          count = 0;
792
          handleSubsetResult(qspec,resultsetBuffer, out, docListResult,
793
                              user, groups,dbconn, useXMLIndex);
794
          // reset docListResult
795
          docListResult = new Hashtable();
796

    
797
        }
798
       // Advance to the next record in the cursor
799
       tableHasRows = rs.next();
800
     }//while
801
     rs.close();
802
     pstmt.close();
803
     //if docListResult is not empty, it need to be sent.
804
     if (!docListResult.isEmpty())
805
     {
806
       handleSubsetResult(qspec,resultsetBuffer, out, docListResult,
807
                              user, groups,dbconn, useXMLIndex);
808
     }
809
     double docListTime = System.currentTimeMillis() / 1000;
810
     logMetacat.warn("prepare docid list time: "
811
                    + (docListTime - queryExecuteTime));
812

    
813
     return resultsetBuffer;
814
    }//findReturnDoclist
815

    
816

    
817
    /*
818
     * Send completed search hashtable(part of reulst)to output stream
819
     * and buffer into a buffer stream
820
     */
821
    private StringBuffer handleSubsetResult(QuerySpecification qspec,
822
                                           StringBuffer resultset,
823
                                           PrintWriter out, Hashtable partOfDoclist,
824
                                           String user, String[]groups,
825
                                       DBConnection dbconn, boolean useXMLIndex)
826
                                       throws Exception
827
   {
828

    
829
     // check if there is a record in xml_returnfield
830
     // and get the returnfield_id and usage count
831
     int usage_count = getXmlReturnfieldsTableId(qspec, dbconn);
832
     boolean enterRecords = false;
833

    
834
     // get value of xml_returnfield_count
835
     int count = (new Integer(MetaCatUtil
836
                            .getOption("xml_returnfield_count")))
837
                            .intValue();
838

    
839
     // set enterRecords to true if usage_count is more than the offset
840
     // specified in metacat.properties
841
     if(usage_count > count){
842
         enterRecords = true;
843
     }
844

    
845
     if(returnfield_id < 0){
846
         logMetacat.warn("Error in getting returnfield id from"
847
                                  + "xml_returnfield table");
848
	enterRecords = false;
849
     }
850

    
851
     // get the hashtable containing the docids that already in the
852
     // xml_queryresult table
853
     logMetacat.info("size of partOfDoclist before"
854
                             + " docidsInQueryresultTable(): "
855
                             + partOfDoclist.size());
856
     Hashtable queryresultDocList = docidsInQueryresultTable(returnfield_id,
857
                                                        partOfDoclist, dbconn);
858

    
859
     // remove the keys in queryresultDocList from partOfDoclist
860
     Enumeration _keys = queryresultDocList.keys();
861
     while (_keys.hasMoreElements()){
862
         partOfDoclist.remove(_keys.nextElement());
863
     }
864

    
865
     // backup the keys-elements in partOfDoclist to check later
866
     // if the doc entry is indexed yet
867
     Hashtable partOfDoclistBackup = new Hashtable();
868
     _keys = partOfDoclist.keys();
869
     while (_keys.hasMoreElements()){
870
	 Object key = _keys.nextElement();
871
         partOfDoclistBackup.put(key, partOfDoclist.get(key));
872
     }
873

    
874
     logMetacat.info("size of partOfDoclist after"
875
                             + " docidsInQueryresultTable(): "
876
                             + partOfDoclist.size());
877

    
878
     //add return fields for the documents in partOfDoclist
879
     partOfDoclist = addReturnfield(partOfDoclist, qspec, user, groups,
880
                                        dbconn, useXMLIndex );
881
     //add relationship part part docid list for the documents in partOfDocList
882
     partOfDoclist = addRelationship(partOfDoclist, qspec, dbconn, useXMLIndex);
883

    
884

    
885
     Enumeration keys = partOfDoclist.keys();
886
     String key = null;
887
     String element = null;
888
     String query = null;
889
     int offset = (new Integer(MetaCatUtil
890
                               .getOption("queryresult_string_length")))
891
                               .intValue();
892
     while (keys.hasMoreElements())
893
     {
894
         key = (String) keys.nextElement();
895
         element = (String)partOfDoclist.get(key);
896

    
897
	 // check if the enterRecords is true, elements is not null, element's
898
         // length is less than the limit of table column and if the document
899
         // has been indexed already
900
         if(enterRecords && element != null
901
		&& element.length() < offset
902
		&& element.compareTo((String) partOfDoclistBackup.get(key)) != 0){
903
             query = "INSERT INTO xml_queryresult (returnfield_id, docid, "
904
                 + "queryresult_string) VALUES (?, ?, ?)";
905

    
906
             PreparedStatement pstmt = null;
907
             pstmt = dbconn.prepareStatement(query);
908
             pstmt.setInt(1, returnfield_id);
909
             pstmt.setString(2, key);
910
             pstmt.setString(3, element);
911

    
912
             dbconn.increaseUsageCount(1);
913
             pstmt.execute();
914
             pstmt.close();
915
         }
916

    
917
         // A string with element
918
         String xmlElement = "  <document>" + element + "</document>";
919

    
920
         //send single element to output
921
         if (out != null)
922
         {
923
             out.println(xmlElement);
924
         }
925
         resultset.append(xmlElement);
926
     }//while
927

    
928

    
929
     keys = queryresultDocList.keys();
930
     while (keys.hasMoreElements())
931
     {
932
         key = (String) keys.nextElement();
933
         element = (String)queryresultDocList.get(key);
934
         // A string with element
935
         String xmlElement = "  <document>" + element + "</document>";
936
         //send single element to output
937
         if (out != null)
938
         {
939
             out.println(xmlElement);
940
         }
941
         resultset.append(xmlElement);
942
     }//while
943

    
944
     return resultset;
945
 }
946

    
947
   /**
948
    * Get the docids already in xml_queryresult table and corresponding
949
    * queryresultstring as a hashtable
950
    */
951
   private Hashtable docidsInQueryresultTable(int returnfield_id,
952
                                              Hashtable partOfDoclist,
953
                                              DBConnection dbconn){
954

    
955
         Hashtable returnValue = new Hashtable();
956
         PreparedStatement pstmt = null;
957
         ResultSet rs = null;
958

    
959
         // get partOfDoclist as string for the query
960
         Enumeration keylist = partOfDoclist.keys();
961
         StringBuffer doclist = new StringBuffer();
962
         while (keylist.hasMoreElements())
963
         {
964
             doclist.append("'");
965
             doclist.append((String) keylist.nextElement());
966
             doclist.append("',");
967
         }//while
968

    
969

    
970
         if (doclist.length() > 0)
971
         {
972
             doclist.deleteCharAt(doclist.length() - 1); //remove the last comma
973

    
974
             // the query to find out docids from xml_queryresult
975
             String query = "select docid, queryresult_string from "
976
                          + "xml_queryresult where returnfield_id = " +
977
                          returnfield_id +" and docid in ("+ doclist + ")";
978
             logMetacat.info("Query to get docids from xml_queryresult:"
979
                                      + query);
980

    
981
             try {
982
                 // prepare and execute the query
983
                 pstmt = dbconn.prepareStatement(query);
984
                 dbconn.increaseUsageCount(1);
985
                 pstmt.execute();
986
                 rs = pstmt.getResultSet();
987
                 boolean tableHasRows = rs.next();
988
                 while (tableHasRows) {
989
                     // store the returned results in the returnValue hashtable
990
                     String key = rs.getString(1);
991
                     String element = rs.getString(2);
992

    
993
                     if(element != null){
994
                         returnValue.put(key, element);
995
                     } else {
996
                         logMetacat.info("Null elment found ("
997
                         + "DBQuery.docidsInQueryresultTable)");
998
                     }
999
                     tableHasRows = rs.next();
1000
                 }
1001
                 rs.close();
1002
                 pstmt.close();
1003
             } catch (Exception e){
1004
                 logMetacat.error("Error getting docids from "
1005
                                          + "queryresult in "
1006
                                          + "DBQuery.docidsInQueryresultTable: "
1007
                                          + e.getMessage());
1008
              }
1009
         }
1010
         return returnValue;
1011
     }
1012

    
1013

    
1014
   /**
1015
    * Method to get id from xml_returnfield table
1016
    * for a given query specification
1017
    */
1018
   private int returnfield_id;
1019
   private int getXmlReturnfieldsTableId(QuerySpecification qspec,
1020
                                           DBConnection dbconn){
1021
       int id = -1;
1022
       int count = 1;
1023
       PreparedStatement pstmt = null;
1024
       ResultSet rs = null;
1025
       String returnfield = qspec.getSortedReturnFieldString();
1026

    
1027
       // query for finding the id from xml_returnfield
1028
       String query = "SELECT returnfield_id, usage_count FROM xml_returnfield "
1029
            + "WHERE returnfield_string LIKE ?";
1030
       logMetacat.info("ReturnField Query:" + query);
1031

    
1032
       try {
1033
           // prepare and run the query
1034
           pstmt = dbconn.prepareStatement(query);
1035
           pstmt.setString(1,returnfield);
1036
           dbconn.increaseUsageCount(1);
1037
           pstmt.execute();
1038
           rs = pstmt.getResultSet();
1039
           boolean tableHasRows = rs.next();
1040

    
1041
           // if record found then increase the usage count
1042
           // else insert a new record and get the id of the new record
1043
           if(tableHasRows){
1044
               // get the id
1045
               id = rs.getInt(1);
1046
               count = rs.getInt(2) + 1;
1047
               rs.close();
1048
               pstmt.close();
1049

    
1050
               // increase the usage count
1051
               query = "UPDATE xml_returnfield SET usage_count ='" + count
1052
                   + "' WHERE returnfield_id ='"+ id +"'";
1053
               logMetacat.info("ReturnField Table Update:"+ query);
1054

    
1055
               pstmt = dbconn.prepareStatement(query);
1056
               dbconn.increaseUsageCount(1);
1057
               pstmt.execute();
1058
               pstmt.close();
1059

    
1060
           } else {
1061
               rs.close();
1062
               pstmt.close();
1063

    
1064
               // insert a new record
1065
               query = "INSERT INTO xml_returnfield (returnfield_string, usage_count)"
1066
                   + "VALUES (?, '1')";
1067
               logMetacat.info("ReturnField Table Insert:"+ query);
1068
               pstmt = dbconn.prepareStatement(query);
1069
               pstmt.setString(1, returnfield);
1070
               dbconn.increaseUsageCount(1);
1071
               pstmt.execute();
1072
               pstmt.close();
1073

    
1074
               // get the id of the new record
1075
               query = "SELECT returnfield_id FROM xml_returnfield "
1076
                   + "WHERE returnfield_string LIKE ?";
1077
               logMetacat.info("ReturnField query after Insert:" + query);
1078
               pstmt = dbconn.prepareStatement(query);
1079
               pstmt.setString(1, returnfield);
1080

    
1081
               dbconn.increaseUsageCount(1);
1082
               pstmt.execute();
1083
               rs = pstmt.getResultSet();
1084
               if(rs.next()){
1085
                   id = rs.getInt(1);
1086
               } else {
1087
                   id = -1;
1088
               }
1089
               rs.close();
1090
               pstmt.close();
1091
           }
1092

    
1093
       } catch (Exception e){
1094
           logMetacat.error("Error getting id from xml_returnfield in "
1095
                                     + "DBQuery.getXmlReturnfieldsTableId: "
1096
                                     + e.getMessage());
1097
           id = -1;
1098
       }
1099

    
1100
       returnfield_id = id;
1101
       return count;
1102
   }
1103

    
1104

    
1105
    /*
1106
     * A method to add return field to return doclist hash table
1107
     */
1108
    private Hashtable addReturnfield(Hashtable docListResult,
1109
                                      QuerySpecification qspec,
1110
                                      String user, String[]groups,
1111
                                      DBConnection dbconn, boolean useXMLIndex )
1112
                                      throws Exception
1113
    {
1114
      PreparedStatement pstmt = null;
1115
      ResultSet rs = null;
1116
      String docid = null;
1117
      String fieldname = null;
1118
      String fielddata = null;
1119
      String relation = null;
1120

    
1121
      if (qspec.containsExtendedSQL())
1122
      {
1123
        qspec.setUserName(user);
1124
        qspec.setGroup(groups);
1125
        Vector extendedFields = new Vector(qspec.getReturnFieldList());
1126
        Vector results = new Vector();
1127
        Enumeration keylist = docListResult.keys();
1128
        StringBuffer doclist = new StringBuffer();
1129
        Vector parentidList = new Vector();
1130
        Hashtable returnFieldValue = new Hashtable();
1131
        while (keylist.hasMoreElements())
1132
        {
1133
          doclist.append("'");
1134
          doclist.append((String) keylist.nextElement());
1135
          doclist.append("',");
1136
        }
1137
        if (doclist.length() > 0)
1138
        {
1139
          Hashtable controlPairs = new Hashtable();
1140
          double extendedQueryStart = System.currentTimeMillis() / 1000;
1141
          doclist.deleteCharAt(doclist.length() - 1); //remove the last comma
1142
          // check if user has permission to see the return field data
1143
          String accessControlSQL =
1144
                 qspec.printAccessControlSQLForReturnField(doclist.toString());
1145
          pstmt = dbconn.prepareStatement(accessControlSQL);
1146
          //increase dbconnection usage count
1147
          dbconn.increaseUsageCount(1);
1148
          pstmt.execute();
1149
          rs = pstmt.getResultSet();
1150
          boolean tableHasRows = rs.next();
1151
          while (tableHasRows)
1152
          {
1153
            long startNodeId = rs.getLong(1);
1154
            long endNodeId = rs.getLong(2);
1155
            controlPairs.put(new Long(startNodeId), new Long(endNodeId));
1156
            tableHasRows = rs.next();
1157
          }
1158

    
1159
           double extendedAccessQueryEnd = System.currentTimeMillis() / 1000;
1160
           logMetacat.info( "Time for execute access extended query: "
1161
                          + (extendedAccessQueryEnd - extendedQueryStart));
1162

    
1163
           String extendedQuery =
1164
               qspec.printExtendedSQL(doclist.toString(), controlPairs, useXMLIndex);
1165
           logMetacat.warn("Extended query: " + extendedQuery);
1166

    
1167
           if(extendedQuery != null){
1168
               pstmt = dbconn.prepareStatement(extendedQuery);
1169
               //increase dbconnection usage count
1170
               dbconn.increaseUsageCount(1);
1171
               pstmt.execute();
1172
               rs = pstmt.getResultSet();
1173
               double extendedQueryEnd = System.currentTimeMillis() / 1000;
1174
               logMetacat.info(
1175
                   "Time for execute extended query: "
1176
                   + (extendedQueryEnd - extendedQueryStart));
1177
               tableHasRows = rs.next();
1178
               while (tableHasRows) {
1179
                   ReturnFieldValue returnValue = new ReturnFieldValue();
1180
                   docid = rs.getString(1).trim();
1181
                   fieldname = rs.getString(2);
1182
                   fielddata = rs.getString(3);
1183
                   fielddata = MetaCatUtil.normalize(fielddata);
1184
                   String parentId = rs.getString(4);
1185
                   StringBuffer value = new StringBuffer();
1186

    
1187
                   // if xml_index is used, there would be just one record per nodeid
1188
                   // as xml_index just keeps one entry for each path
1189
                   if (useXMLIndex || !containsKey(parentidList, parentId)) {
1190
                       // don't need to merger nodedata
1191
                       value.append("<param name=\"");
1192
                       value.append(fieldname);
1193
                       value.append("\">");
1194
                       value.append(fielddata);
1195
                       value.append("</param>");
1196
                       //set returnvalue
1197
                       returnValue.setDocid(docid);
1198
                       returnValue.setFieldValue(fielddata);
1199
                       returnValue.setXMLFieldValue(value.toString());
1200
                       // Store it in hastable
1201
                       putInArray(parentidList, parentId, returnValue);
1202
                   }
1203
                   else {
1204
                       // need to merge nodedata if they have same parent id and
1205
                       // node type is text
1206
                       fielddata = (String) ( (ReturnFieldValue)
1207
                                             getArrayValue(
1208
                           parentidList, parentId)).getFieldValue()
1209
                           + fielddata;
1210
                       value.append("<param name=\"");
1211
                       value.append(fieldname);
1212
                       value.append("\">");
1213
                       value.append(fielddata);
1214
                       value.append("</param>");
1215
                       returnValue.setDocid(docid);
1216
                       returnValue.setFieldValue(fielddata);
1217
                       returnValue.setXMLFieldValue(value.toString());
1218
                       // remove the old return value from paretnidList
1219
                       parentidList.remove(parentId);
1220
                       // store the new return value in parentidlit
1221
                       putInArray(parentidList, parentId, returnValue);
1222
                   }
1223
                   tableHasRows = rs.next();
1224
               } //while
1225
               rs.close();
1226
               pstmt.close();
1227

    
1228
               // put the merger node data info into doclistReult
1229
               Enumeration xmlFieldValue = (getElements(parentidList)).
1230
                   elements();
1231
               while (xmlFieldValue.hasMoreElements()) {
1232
                   ReturnFieldValue object =
1233
                       (ReturnFieldValue) xmlFieldValue.nextElement();
1234
                   docid = object.getDocid();
1235
                   if (docListResult.containsKey(docid)) {
1236
                       String removedelement = (String) docListResult.
1237
                           remove(docid);
1238
                       docListResult.
1239
                           put(docid,
1240
                               removedelement + object.getXMLFieldValue());
1241
                   }
1242
                   else {
1243
                       docListResult.put(docid, object.getXMLFieldValue());
1244
                   }
1245
               } //while
1246
               double docListResultEnd = System.currentTimeMillis() / 1000;
1247
               logMetacat.warn(
1248
                   "Time for prepare doclistresult after"
1249
                   + " execute extended query: "
1250
                   + (docListResultEnd - extendedQueryEnd));
1251
           }
1252

    
1253
           // get attribures return
1254
           docListResult = getAttributeValueForReturn(qspec,
1255
                           docListResult, doclist.toString(), useXMLIndex);
1256
       }//if doclist lenght is great than zero
1257

    
1258
     }//if has extended query
1259

    
1260
      return docListResult;
1261
    }//addReturnfield
1262

    
1263
    /*
1264
    * A method to add relationship to return doclist hash table
1265
    */
1266
   private Hashtable addRelationship(Hashtable docListResult,
1267
                                     QuerySpecification qspec,
1268
                                     DBConnection dbconn, boolean useXMLIndex )
1269
                                     throws Exception
1270
  {
1271
    PreparedStatement pstmt = null;
1272
    ResultSet rs = null;
1273
    StringBuffer document = null;
1274
    double startRelation = System.currentTimeMillis() / 1000;
1275
    Enumeration docidkeys = docListResult.keys();
1276
    while (docidkeys.hasMoreElements())
1277
    {
1278
      //String connstring =
1279
      // "metacat://"+util.getOption("server")+"?docid=";
1280
      String connstring = "%docid=";
1281
      String docidkey = (String) docidkeys.nextElement();
1282
      pstmt = dbconn.prepareStatement(QuerySpecification
1283
                      .printRelationSQL(docidkey));
1284
      pstmt.execute();
1285
      rs = pstmt.getResultSet();
1286
      boolean tableHasRows = rs.next();
1287
      while (tableHasRows)
1288
      {
1289
        String sub = rs.getString(1);
1290
        String rel = rs.getString(2);
1291
        String obj = rs.getString(3);
1292
        String subDT = rs.getString(4);
1293
        String objDT = rs.getString(5);
1294

    
1295
        document = new StringBuffer();
1296
        document.append("<triple>");
1297
        document.append("<subject>").append(MetaCatUtil.normalize(sub));
1298
        document.append("</subject>");
1299
        if (subDT != null)
1300
        {
1301
          document.append("<subjectdoctype>").append(subDT);
1302
          document.append("</subjectdoctype>");
1303
        }
1304
        document.append("<relationship>").append(MetaCatUtil.normalize(rel));
1305
        document.append("</relationship>");
1306
        document.append("<object>").append(MetaCatUtil.normalize(obj));
1307
        document.append("</object>");
1308
        if (objDT != null)
1309
        {
1310
          document.append("<objectdoctype>").append(objDT);
1311
          document.append("</objectdoctype>");
1312
        }
1313
        document.append("</triple>");
1314

    
1315
        String removedelement = (String) docListResult.remove(docidkey);
1316
        docListResult.put(docidkey, removedelement+ document.toString());
1317
        tableHasRows = rs.next();
1318
      }//while
1319
      rs.close();
1320
      pstmt.close();
1321
    }//while
1322
    double endRelation = System.currentTimeMillis() / 1000;
1323
    logMetacat.info("Time for adding relation to docListResult: "
1324
                             + (endRelation - startRelation));
1325

    
1326
    return docListResult;
1327
  }//addRelation
1328

    
1329
  /**
1330
   * removes the <?xml version="1.0"?> tag from the beginning.  This takes a
1331
   * string as a param instead of a hashtable.
1332
   *
1333
   * @param xmlquery a string representing a query.
1334
   */
1335
   private  String transformQuery(String xmlquery)
1336
   {
1337
     xmlquery = xmlquery.trim();
1338
     int index = xmlquery.indexOf("?>");
1339
     if (index != -1)
1340
     {
1341
       return xmlquery.substring(index + 2, xmlquery.length());
1342
     }
1343
     else
1344
     {
1345
       return xmlquery;
1346
     }
1347
   }
1348

    
1349

    
1350
    /*
1351
     * A method to search if Vector contains a particular key string
1352
     */
1353
    private boolean containsKey(Vector parentidList, String parentId)
1354
    {
1355

    
1356
        Vector tempVector = null;
1357

    
1358
        for (int count = 0; count < parentidList.size(); count++) {
1359
            tempVector = (Vector) parentidList.get(count);
1360
            if (parentId.compareTo((String) tempVector.get(0)) == 0) { return true; }
1361
        }
1362
        return false;
1363
    }
1364

    
1365
    /*
1366
     * A method to put key and value in Vector
1367
     */
1368
    private void putInArray(Vector parentidList, String key,
1369
            ReturnFieldValue value)
1370
    {
1371

    
1372
        Vector tempVector = null;
1373

    
1374
        for (int count = 0; count < parentidList.size(); count++) {
1375
            tempVector = (Vector) parentidList.get(count);
1376

    
1377
            if (key.compareTo((String) tempVector.get(0)) == 0) {
1378
                tempVector.remove(1);
1379
                tempVector.add(1, value);
1380
                return;
1381
            }
1382
        }
1383

    
1384
        tempVector = new Vector();
1385
        tempVector.add(0, key);
1386
        tempVector.add(1, value);
1387
        parentidList.add(tempVector);
1388
        return;
1389
    }
1390

    
1391
    /*
1392
     * A method to get value in Vector given a key
1393
     */
1394
    private ReturnFieldValue getArrayValue(Vector parentidList, String key)
1395
    {
1396

    
1397
        Vector tempVector = null;
1398

    
1399
        for (int count = 0; count < parentidList.size(); count++) {
1400
            tempVector = (Vector) parentidList.get(count);
1401

    
1402
            if (key.compareTo((String) tempVector.get(0)) == 0) { return (ReturnFieldValue) tempVector
1403
                    .get(1); }
1404
        }
1405
        return null;
1406
    }
1407

    
1408
    /*
1409
     * A method to get enumeration of all values in Vector
1410
     */
1411
    private Vector getElements(Vector parentidList)
1412
    {
1413
        Vector enumVector = new Vector();
1414
        Vector tempVector = null;
1415

    
1416
        for (int count = 0; count < parentidList.size(); count++) {
1417
            tempVector = (Vector) parentidList.get(count);
1418

    
1419
            enumVector.add(tempVector.get(1));
1420
        }
1421
        return enumVector;
1422
    }
1423

    
1424
    /*
1425
     * A method to return search result after running a query which return
1426
     * field have attribue
1427
     */
1428
    private Hashtable getAttributeValueForReturn(QuerySpecification squery,
1429
            Hashtable docInformationList, String docList, boolean useXMLIndex)
1430
    {
1431
        StringBuffer XML = null;
1432
        String sql = null;
1433
        DBConnection dbconn = null;
1434
        PreparedStatement pstmt = null;
1435
        ResultSet rs = null;
1436
        int serialNumber = -1;
1437
        boolean tableHasRows = false;
1438

    
1439
        //check the parameter
1440
        if (squery == null || docList == null || docList.length() < 0) { return docInformationList; }
1441

    
1442
        // if has attribute as return field
1443
        if (squery.containsAttributeReturnField()) {
1444
            sql = squery.printAttributeQuery(docList, useXMLIndex);
1445
            try {
1446
                dbconn = DBConnectionPool
1447
                        .getDBConnection("DBQuery.getAttributeValue");
1448
                serialNumber = dbconn.getCheckOutSerialNumber();
1449
                pstmt = dbconn.prepareStatement(sql);
1450
                pstmt.execute();
1451
                rs = pstmt.getResultSet();
1452
                tableHasRows = rs.next();
1453
                while (tableHasRows) {
1454
                    String docid = rs.getString(1).trim();
1455
                    String fieldname = rs.getString(2);
1456
                    String fielddata = rs.getString(3);
1457
                    String attirbuteName = rs.getString(4);
1458
                    XML = new StringBuffer();
1459

    
1460
                    XML.append("<param name=\"");
1461
                    XML.append(fieldname);
1462
                    XML.append("/");
1463
                    XML.append(QuerySpecification.ATTRIBUTESYMBOL);
1464
                    XML.append(attirbuteName);
1465
                    XML.append("\">");
1466
                    XML.append(fielddata);
1467
                    XML.append("</param>");
1468
                    tableHasRows = rs.next();
1469

    
1470
                    if (docInformationList.containsKey(docid)) {
1471
                        String removedelement = (String) docInformationList
1472
                                .remove(docid);
1473
                        docInformationList.put(docid, removedelement
1474
                                + XML.toString());
1475
                    } else {
1476
                        docInformationList.put(docid, XML.toString());
1477
                    }
1478
                }//while
1479
                rs.close();
1480
                pstmt.close();
1481
            } catch (Exception se) {
1482
                logMetacat.error(
1483
                        "Error in DBQuery.getAttributeValue1: "
1484
                                + se.getMessage());
1485
            } finally {
1486
                try {
1487
                    pstmt.close();
1488
                }//try
1489
                catch (SQLException sqlE) {
1490
                    logMetacat.error(
1491
                            "Error in DBQuery.getAttributeValue2: "
1492
                                    + sqlE.getMessage());
1493
                }//catch
1494
                finally {
1495
                    DBConnectionPool.returnDBConnection(dbconn, serialNumber);
1496
                }//finally
1497
            }//finally
1498
        }//if
1499
        return docInformationList;
1500

    
1501
    }
1502

    
1503
    /*
1504
     * A method to create a query to get owner's docid list
1505
     */
1506
    private String getOwnerQuery(String owner)
1507
    {
1508
        if (owner != null) {
1509
            owner = owner.toLowerCase();
1510
        }
1511
        StringBuffer self = new StringBuffer();
1512

    
1513
        self.append("SELECT docid,docname,doctype,");
1514
        self.append("date_created, date_updated, rev ");
1515
        self.append("FROM xml_documents WHERE docid IN (");
1516
        self.append("(");
1517
        self.append("SELECT DISTINCT docid FROM xml_nodes WHERE \n");
1518
        self.append("nodedata LIKE '%%%' ");
1519
        self.append(") \n");
1520
        self.append(") ");
1521
        self.append(" AND (");
1522
        self.append(" lower(user_owner) = '" + owner + "'");
1523
        self.append(") ");
1524
        return self.toString();
1525
    }
1526

    
1527
    /**
1528
     * format a structured query as an XML document that conforms to the
1529
     * pathquery.dtd and is appropriate for submission to the DBQuery
1530
     * structured query engine
1531
     *
1532
     * @param params The list of parameters that should be included in the
1533
     *            query
1534
     */
1535
    public static String createSQuery(Hashtable params)
1536
    {
1537
        StringBuffer query = new StringBuffer();
1538
        Enumeration elements;
1539
        Enumeration keys;
1540
        String filterDoctype = null;
1541
        String casesensitive = null;
1542
        String searchmode = null;
1543
        Object nextkey;
1544
        Object nextelement;
1545
        //add the xml headers
1546
        query.append("<?xml version=\"1.0\"?>\n");
1547
        query.append("<pathquery version=\"1.2\">\n");
1548

    
1549

    
1550

    
1551
        if (params.containsKey("meta_file_id")) {
1552
            query.append("<meta_file_id>");
1553
            query.append(((String[]) params.get("meta_file_id"))[0]);
1554
            query.append("</meta_file_id>");
1555
        }
1556

    
1557
        if (params.containsKey("returndoctype")) {
1558
            String[] returnDoctypes = ((String[]) params.get("returndoctype"));
1559
            for (int i = 0; i < returnDoctypes.length; i++) {
1560
                String doctype = (String) returnDoctypes[i];
1561

    
1562
                if (!doctype.equals("any") && !doctype.equals("ANY")
1563
                        && !doctype.equals("")) {
1564
                    query.append("<returndoctype>").append(doctype);
1565
                    query.append("</returndoctype>");
1566
                }
1567
            }
1568
        }
1569

    
1570
        if (params.containsKey("filterdoctype")) {
1571
            String[] filterDoctypes = ((String[]) params.get("filterdoctype"));
1572
            for (int i = 0; i < filterDoctypes.length; i++) {
1573
                query.append("<filterdoctype>").append(filterDoctypes[i]);
1574
                query.append("</filterdoctype>");
1575
            }
1576
        }
1577

    
1578
        if (params.containsKey("returnfield")) {
1579
            String[] returnfield = ((String[]) params.get("returnfield"));
1580
            for (int i = 0; i < returnfield.length; i++) {
1581
                query.append("<returnfield>").append(returnfield[i]);
1582
                query.append("</returnfield>");
1583
            }
1584
        }
1585

    
1586
        if (params.containsKey("owner")) {
1587
            String[] owner = ((String[]) params.get("owner"));
1588
            for (int i = 0; i < owner.length; i++) {
1589
                query.append("<owner>").append(owner[i]);
1590
                query.append("</owner>");
1591
            }
1592
        }
1593

    
1594
        if (params.containsKey("site")) {
1595
            String[] site = ((String[]) params.get("site"));
1596
            for (int i = 0; i < site.length; i++) {
1597
                query.append("<site>").append(site[i]);
1598
                query.append("</site>");
1599
            }
1600
        }
1601

    
1602
        //allows the dynamic switching of boolean operators
1603
        if (params.containsKey("operator")) {
1604
            query.append("<querygroup operator=\""
1605
                    + ((String[]) params.get("operator"))[0] + "\">");
1606
        } else { //the default operator is UNION
1607
            query.append("<querygroup operator=\"UNION\">");
1608
        }
1609

    
1610
        if (params.containsKey("casesensitive")) {
1611
            casesensitive = ((String[]) params.get("casesensitive"))[0];
1612
        } else {
1613
            casesensitive = "false";
1614
        }
1615

    
1616
        if (params.containsKey("searchmode")) {
1617
            searchmode = ((String[]) params.get("searchmode"))[0];
1618
        } else {
1619
            searchmode = "contains";
1620
        }
1621

    
1622
        //anyfield is a special case because it does a
1623
        //free text search. It does not have a <pathexpr>
1624
        //tag. This allows for a free text search within the structured
1625
        //query. This is useful if the INTERSECT operator is used.
1626
        if (params.containsKey("anyfield")) {
1627
            String[] anyfield = ((String[]) params.get("anyfield"));
1628
            //allow for more than one value for anyfield
1629
            for (int i = 0; i < anyfield.length; i++) {
1630
                if (!anyfield[i].equals("")) {
1631
                    query.append("<queryterm casesensitive=\"" + casesensitive
1632
                            + "\" " + "searchmode=\"" + searchmode
1633
                            + "\"><value>" + anyfield[i]
1634
                            + "</value></queryterm>");
1635
                }
1636
            }
1637
        }
1638

    
1639
        //this while loop finds the rest of the parameters
1640
        //and attempts to query for the field specified
1641
        //by the parameter.
1642
        elements = params.elements();
1643
        keys = params.keys();
1644
        while (keys.hasMoreElements() && elements.hasMoreElements()) {
1645
            nextkey = keys.nextElement();
1646
            nextelement = elements.nextElement();
1647

    
1648
            //make sure we aren't querying for any of these
1649
            //parameters since the are already in the query
1650
            //in one form or another.
1651
            Vector ignoredParams = new Vector();
1652
            ignoredParams.add("returndoctype");
1653
            ignoredParams.add("filterdoctype");
1654
            ignoredParams.add("action");
1655
            ignoredParams.add("qformat");
1656
            ignoredParams.add("anyfield");
1657
            ignoredParams.add("returnfield");
1658
            ignoredParams.add("owner");
1659
            ignoredParams.add("site");
1660
            ignoredParams.add("operator");
1661
            ignoredParams.add("sessionid");
1662
            ignoredParams.add("pagesize");
1663
            ignoredParams.add("pagestart");
1664

    
1665
            // Also ignore parameters listed in the properties file
1666
            // so that they can be passed through to stylesheets
1667
            String paramsToIgnore = MetaCatUtil
1668
                    .getOption("query.ignored.params");
1669
            StringTokenizer st = new StringTokenizer(paramsToIgnore, ",");
1670
            while (st.hasMoreTokens()) {
1671
                ignoredParams.add(st.nextToken());
1672
            }
1673
            if (!ignoredParams.contains(nextkey.toString())) {
1674
                //allow for more than value per field name
1675
                for (int i = 0; i < ((String[]) nextelement).length; i++) {
1676
                    if (!((String[]) nextelement)[i].equals("")) {
1677
                        query.append("<queryterm casesensitive=\""
1678
                                + casesensitive + "\" " + "searchmode=\""
1679
                                + searchmode + "\">" + "<value>" +
1680
                                //add the query value
1681
                                ((String[]) nextelement)[i]
1682
                                + "</value><pathexpr>" +
1683
                                //add the path to query by
1684
                                nextkey.toString() + "</pathexpr></queryterm>");
1685
                    }
1686
                }
1687
            }
1688
        }
1689
        query.append("</querygroup></pathquery>");
1690
        //append on the end of the xml and return the result as a string
1691
        return query.toString();
1692
    }
1693

    
1694
    /**
1695
     * format a simple free-text value query as an XML document that conforms
1696
     * to the pathquery.dtd and is appropriate for submission to the DBQuery
1697
     * structured query engine
1698
     *
1699
     * @param value the text string to search for in the xml catalog
1700
     * @param doctype the type of documents to include in the result set -- use
1701
     *            "any" or "ANY" for unfiltered result sets
1702
     */
1703
    public static String createQuery(String value, String doctype)
1704
    {
1705
        StringBuffer xmlquery = new StringBuffer();
1706
        xmlquery.append("<?xml version=\"1.0\"?>\n");
1707
        xmlquery.append("<pathquery version=\"1.0\">");
1708

    
1709
        if (!doctype.equals("any") && !doctype.equals("ANY")) {
1710
            xmlquery.append("<returndoctype>");
1711
            xmlquery.append(doctype).append("</returndoctype>");
1712
        }
1713

    
1714
        xmlquery.append("<querygroup operator=\"UNION\">");
1715
        //chad added - 8/14
1716
        //the if statement allows a query to gracefully handle a null
1717
        //query. Without this if a nullpointerException is thrown.
1718
        if (!value.equals("")) {
1719
            xmlquery.append("<queryterm casesensitive=\"false\" ");
1720
            xmlquery.append("searchmode=\"contains\">");
1721
            xmlquery.append("<value>").append(value).append("</value>");
1722
            xmlquery.append("</queryterm>");
1723
        }
1724
        xmlquery.append("</querygroup>");
1725
        xmlquery.append("</pathquery>");
1726

    
1727
        return (xmlquery.toString());
1728
    }
1729

    
1730
    /**
1731
     * format a simple free-text value query as an XML document that conforms
1732
     * to the pathquery.dtd and is appropriate for submission to the DBQuery
1733
     * structured query engine
1734
     *
1735
     * @param value the text string to search for in the xml catalog
1736
     */
1737
    public static String createQuery(String value)
1738
    {
1739
        return createQuery(value, "any");
1740
    }
1741

    
1742
    /**
1743
     * Check for "READ" permission on @docid for @user and/or @group from DB
1744
     * connection
1745
     */
1746
    private boolean hasPermission(String user, String[] groups, String docid)
1747
            throws SQLException, Exception
1748
    {
1749
        // Check for READ permission on @docid for @user and/or @groups
1750
        PermissionController controller = new PermissionController(docid);
1751
        return controller.hasPermission(user, groups,
1752
                AccessControlInterface.READSTRING);
1753
    }
1754

    
1755
    /**
1756
     * Get all docIds list for a data packadge
1757
     *
1758
     * @param dataPackageDocid, the string in docId field of xml_relation table
1759
     */
1760
    private Vector getCurrentDocidListForDataPackage(String dataPackageDocid)
1761
    {
1762
        DBConnection dbConn = null;
1763
        int serialNumber = -1;
1764
        Vector docIdList = new Vector();//return value
1765
        PreparedStatement pStmt = null;
1766
        ResultSet rs = null;
1767
        String docIdInSubjectField = null;
1768
        String docIdInObjectField = null;
1769

    
1770
        // Check the parameter
1771
        if (dataPackageDocid == null || dataPackageDocid.equals("")) { return docIdList; }//if
1772

    
1773
        //the query stirng
1774
        String query = "SELECT subject, object from xml_relation where docId = ?";
1775
        try {
1776
            dbConn = DBConnectionPool
1777
                    .getDBConnection("DBQuery.getCurrentDocidListForDataPackage");
1778
            serialNumber = dbConn.getCheckOutSerialNumber();
1779
            pStmt = dbConn.prepareStatement(query);
1780
            //bind the value to query
1781
            pStmt.setString(1, dataPackageDocid);
1782

    
1783
            //excute the query
1784
            pStmt.execute();
1785
            //get the result set
1786
            rs = pStmt.getResultSet();
1787
            //process the result
1788
            while (rs.next()) {
1789
                //In order to get the whole docIds in a data packadge,
1790
                //we need to put the docIds of subject and object field in
1791
                // xml_relation
1792
                //into the return vector
1793
                docIdInSubjectField = rs.getString(1);//the result docId in
1794
                                                      // subject field
1795
                docIdInObjectField = rs.getString(2);//the result docId in
1796
                                                     // object field
1797

    
1798
                //don't put the duplicate docId into the vector
1799
                if (!docIdList.contains(docIdInSubjectField)) {
1800
                    docIdList.add(docIdInSubjectField);
1801
                }
1802

    
1803
                //don't put the duplicate docId into the vector
1804
                if (!docIdList.contains(docIdInObjectField)) {
1805
                    docIdList.add(docIdInObjectField);
1806
                }
1807
            }//while
1808
            //close the pStmt
1809
            pStmt.close();
1810
        }//try
1811
        catch (SQLException e) {
1812
            logMetacat.error("Error in getDocidListForDataPackage: "
1813
                    + e.getMessage());
1814
        }//catch
1815
        finally {
1816
            try {
1817
                pStmt.close();
1818
            }//try
1819
            catch (SQLException ee) {
1820
                logMetacat.error(
1821
                        "Error in getDocidListForDataPackage: "
1822
                                + ee.getMessage());
1823
            }//catch
1824
            finally {
1825
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1826
            }//fianlly
1827
        }//finally
1828
        return docIdList;
1829
    }//getCurrentDocidListForDataPackadge()
1830

    
1831
    /**
1832
     * Get all docIds list for a data packadge
1833
     *
1834
     * @param dataPackageDocid, the string in docId field of xml_relation table
1835
     */
1836
    private Vector getOldVersionDocidListForDataPackage(String dataPackageDocidWithRev)
1837
    {
1838

    
1839
        Vector docIdList = new Vector();//return value
1840
        Vector tripleList = null;
1841
        String xml = null;
1842

    
1843
        // Check the parameter
1844
        if (dataPackageDocidWithRev == null || dataPackageDocidWithRev.equals("")) { return docIdList; }//if
1845

    
1846
        try {
1847
            //initial a documentImpl object
1848
            DocumentImpl packageDocument = new DocumentImpl(dataPackageDocidWithRev);
1849
            //transfer to documentImpl object to string
1850
            xml = packageDocument.toString();
1851

    
1852
            //create a tripcollection object
1853
            TripleCollection tripleForPackage = new TripleCollection(
1854
                    new StringReader(xml));
1855
            //get the vetor of triples
1856
            tripleList = tripleForPackage.getCollection();
1857

    
1858
            for (int i = 0; i < tripleList.size(); i++) {
1859
                //put subject docid into docIdlist without duplicate
1860
                if (!docIdList.contains(((Triple) tripleList.elementAt(i))
1861
                        .getSubject())) {
1862
                    //put subject docid into docIdlist
1863
                    docIdList.add(((Triple) tripleList.get(i)).getSubject());
1864
                }
1865
                //put object docid into docIdlist without duplicate
1866
                if (!docIdList.contains(((Triple) tripleList.elementAt(i))
1867
                        .getObject())) {
1868
                    docIdList.add(((Triple) (tripleList.get(i))).getObject());
1869
                }
1870
            }//for
1871
        }//try
1872
        catch (Exception e) {
1873
            logMetacat.error("Error in getOldVersionAllDocumentImpl: "
1874
                    + e.getMessage());
1875
        }//catch
1876

    
1877
        // return result
1878
        return docIdList;
1879
    }//getDocidListForPackageInXMLRevisions()
1880

    
1881
    /**
1882
     * Check if the docId is a data packadge id. If the id is a data packadage
1883
     * id, it should be store in the docId fields in xml_relation table. So we
1884
     * can use a query to get the entries which the docId equals the given
1885
     * value. If the result is null. The docId is not a packadge id. Otherwise,
1886
     * it is.
1887
     *
1888
     * @param docId, the id need to be checked
1889
     */
1890
    private boolean isDataPackageId(String docId)
1891
    {
1892
        boolean result = false;
1893
        PreparedStatement pStmt = null;
1894
        ResultSet rs = null;
1895
        String query = "SELECT docId from xml_relation where docId = ?";
1896
        DBConnection dbConn = null;
1897
        int serialNumber = -1;
1898
        try {
1899
            dbConn = DBConnectionPool
1900
                    .getDBConnection("DBQuery.isDataPackageId");
1901
            serialNumber = dbConn.getCheckOutSerialNumber();
1902
            pStmt = dbConn.prepareStatement(query);
1903
            //bind the value to query
1904
            pStmt.setString(1, docId);
1905
            //execute the query
1906
            pStmt.execute();
1907
            rs = pStmt.getResultSet();
1908
            //process the result
1909
            if (rs.next()) //There are some records for the id in docId fields
1910
            {
1911
                result = true;//It is a data packadge id
1912
            }
1913
            pStmt.close();
1914
        }//try
1915
        catch (SQLException e) {
1916
            logMetacat.error("Error in isDataPackageId: "
1917
                    + e.getMessage());
1918
        } finally {
1919
            try {
1920
                pStmt.close();
1921
            }//try
1922
            catch (SQLException ee) {
1923
                logMetacat.error("Error in isDataPackageId: "
1924
                        + ee.getMessage());
1925
            }//catch
1926
            finally {
1927
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1928
            }//finally
1929
        }//finally
1930
        return result;
1931
    }//isDataPackageId()
1932

    
1933
    /**
1934
     * Check if the user has the permission to export data package
1935
     *
1936
     * @param conn, the connection
1937
     * @param docId, the id need to be checked
1938
     * @param user, the name of user
1939
     * @param groups, the user's group
1940
     */
1941
    private boolean hasPermissionToExportPackage(String docId, String user,
1942
            String[] groups) throws Exception
1943
    {
1944
        //DocumentImpl doc=new DocumentImpl(conn,docId);
1945
        return DocumentImpl.hasReadPermission(user, groups, docId);
1946
    }
1947

    
1948
    /**
1949
     * Get the current Rev for a docid in xml_documents table
1950
     *
1951
     * @param docId, the id need to get version numb If the return value is -5,
1952
     *            means no value in rev field for this docid
1953
     */
1954
    private int getCurrentRevFromXMLDoumentsTable(String docId)
1955
            throws SQLException
1956
    {
1957
        int rev = -5;
1958
        PreparedStatement pStmt = null;
1959
        ResultSet rs = null;
1960
        String query = "SELECT rev from xml_documents where docId = ?";
1961
        DBConnection dbConn = null;
1962
        int serialNumber = -1;
1963
        try {
1964
            dbConn = DBConnectionPool
1965
                    .getDBConnection("DBQuery.getCurrentRevFromXMLDocumentsTable");
1966
            serialNumber = dbConn.getCheckOutSerialNumber();
1967
            pStmt = dbConn.prepareStatement(query);
1968
            //bind the value to query
1969
            pStmt.setString(1, docId);
1970
            //execute the query
1971
            pStmt.execute();
1972
            rs = pStmt.getResultSet();
1973
            //process the result
1974
            if (rs.next()) //There are some records for rev
1975
            {
1976
                rev = rs.getInt(1);
1977
                ;//It is the version for given docid
1978
            } else {
1979
                rev = -5;
1980
            }
1981

    
1982
        }//try
1983
        catch (SQLException e) {
1984
            logMetacat.error(
1985
                    "Error in getCurrentRevFromXMLDoumentsTable: "
1986
                            + e.getMessage());
1987
            throw e;
1988
        }//catch
1989
        finally {
1990
            try {
1991
                pStmt.close();
1992
            }//try
1993
            catch (SQLException ee) {
1994
                logMetacat.error(
1995
                        "Error in getCurrentRevFromXMLDoumentsTable: "
1996
                                + ee.getMessage());
1997
            }//catch
1998
            finally {
1999
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
2000
            }//finally
2001
        }//finally
2002
        return rev;
2003
    }//getCurrentRevFromXMLDoumentsTable
2004

    
2005
    /**
2006
     * put a doc into a zip output stream
2007
     *
2008
     * @param docImpl, docmentImpl object which will be sent to zip output
2009
     *            stream
2010
     * @param zipOut, zip output stream which the docImpl will be put
2011
     * @param packageZipEntry, the zip entry name for whole package
2012
     */
2013
    private void addDocToZipOutputStream(DocumentImpl docImpl,
2014
            ZipOutputStream zipOut, String packageZipEntry)
2015
            throws ClassNotFoundException, IOException, SQLException,
2016
            McdbException, Exception
2017
    {
2018
        byte[] byteString = null;
2019
        ZipEntry zEntry = null;
2020

    
2021
        byteString = docImpl.toString().getBytes();
2022
        //use docId as the zip entry's name
2023
        zEntry = new ZipEntry(packageZipEntry + "/metadata/"
2024
                + docImpl.getDocID());
2025
        zEntry.setSize(byteString.length);
2026
        zipOut.putNextEntry(zEntry);
2027
        zipOut.write(byteString, 0, byteString.length);
2028
        zipOut.closeEntry();
2029

    
2030
    }//addDocToZipOutputStream()
2031

    
2032
    /**
2033
     * Transfer a docid vetor to a documentImpl vector. The documentImpl vetor
2034
     * only inlcudes current version. If a DocumentImple object couldn't find
2035
     * for a docid, then the String of this docid was added to vetor rather
2036
     * than DocumentImple object.
2037
     *
2038
     * @param docIdList, a vetor hold a docid list for a data package. In
2039
     *            docid, there is not version number in it.
2040
     */
2041

    
2042
    private Vector getCurrentAllDocumentImpl(Vector docIdList)
2043
            throws McdbException, Exception
2044
    {
2045
        //Connection dbConn=null;
2046
        Vector documentImplList = new Vector();
2047
        int rev = 0;
2048

    
2049
        // Check the parameter
2050
        if (docIdList.isEmpty()) { return documentImplList; }//if
2051

    
2052
        //for every docid in vector
2053
        for (int i = 0; i < docIdList.size(); i++) {
2054
            try {
2055
                //get newest version for this docId
2056
                rev = getCurrentRevFromXMLDoumentsTable((String) docIdList
2057
                        .elementAt(i));
2058

    
2059
                // There is no record for this docId in xml_documents table
2060
                if (rev == -5) {
2061
                    // Rather than put DocumentImple object, put a String
2062
                    // Object(docid)
2063
                    // into the documentImplList
2064
                    documentImplList.add((String) docIdList.elementAt(i));
2065
                    // Skip other code
2066
                    continue;
2067
                }
2068

    
2069
                String docidPlusVersion = ((String) docIdList.elementAt(i))
2070
                        + MetaCatUtil.getOption("accNumSeparator") + rev;
2071

    
2072
                //create new documentImpl object
2073
                DocumentImpl documentImplObject = new DocumentImpl(
2074
                        docidPlusVersion);
2075
                //add them to vector
2076
                documentImplList.add(documentImplObject);
2077
            }//try
2078
            catch (Exception e) {
2079
                logMetacat.error("Error in getCurrentAllDocumentImpl: "
2080
                        + e.getMessage());
2081
                // continue the for loop
2082
                continue;
2083
            }
2084
        }//for
2085
        return documentImplList;
2086
    }
2087

    
2088
    /**
2089
     * Transfer a docid vetor to a documentImpl vector. If a DocumentImple
2090
     * object couldn't find for a docid, then the String of this docid was
2091
     * added to vetor rather than DocumentImple object.
2092
     *
2093
     * @param docIdList, a vetor hold a docid list for a data package. In
2094
     *            docid, t here is version number in it.
2095
     */
2096
    private Vector getOldVersionAllDocumentImpl(Vector docIdList)
2097
    {
2098
        //Connection dbConn=null;
2099
        Vector documentImplList = new Vector();
2100
        String siteCode = null;
2101
        String uniqueId = null;
2102
        int rev = 0;
2103

    
2104
        // Check the parameter
2105
        if (docIdList.isEmpty()) { return documentImplList; }//if
2106

    
2107
        //for every docid in vector
2108
        for (int i = 0; i < docIdList.size(); i++) {
2109

    
2110
            String docidPlusVersion = (String) (docIdList.elementAt(i));
2111

    
2112
            try {
2113
                //create new documentImpl object
2114
                DocumentImpl documentImplObject = new DocumentImpl(
2115
                        docidPlusVersion);
2116
                //add them to vector
2117
                documentImplList.add(documentImplObject);
2118
            }//try
2119
            catch (McdbDocNotFoundException notFoundE) {
2120
                logMetacat.error(
2121
                        "Error in DBQuery.getOldVersionAllDocument" + "Imple"
2122
                                + notFoundE.getMessage());
2123
                // Rather than add a DocumentImple object into vetor, a String
2124
                // object
2125
                // - the doicd was added to the vector
2126
                documentImplList.add(docidPlusVersion);
2127
                // Continue the for loop
2128
                continue;
2129
            }//catch
2130
            catch (Exception e) {
2131
                logMetacat.error(
2132
                        "Error in DBQuery.getOldVersionAllDocument" + "Imple"
2133
                                + e.getMessage());
2134
                // Continue the for loop
2135
                continue;
2136
            }//catch
2137

    
2138
        }//for
2139
        return documentImplList;
2140
    }//getOldVersionAllDocumentImple
2141

    
2142
    /**
2143
     * put a data file into a zip output stream
2144
     *
2145
     * @param docImpl, docmentImpl object which will be sent to zip output
2146
     *            stream
2147
     * @param zipOut, the zip output stream which the docImpl will be put
2148
     * @param packageZipEntry, the zip entry name for whole package
2149
     */
2150
    private void addDataFileToZipOutputStream(DocumentImpl docImpl,
2151
            ZipOutputStream zipOut, String packageZipEntry)
2152
            throws ClassNotFoundException, IOException, SQLException,
2153
            McdbException, Exception
2154
    {
2155
        byte[] byteString = null;
2156
        ZipEntry zEntry = null;
2157
        // this is data file; add file to zip
2158
        String filePath = MetaCatUtil.getOption("datafilepath");
2159
        if (!filePath.endsWith("/")) {
2160
            filePath += "/";
2161
        }
2162
        String fileName = filePath + docImpl.getDocID();
2163
        zEntry = new ZipEntry(packageZipEntry + "/data/" + docImpl.getDocID());
2164
        zipOut.putNextEntry(zEntry);
2165
        FileInputStream fin = null;
2166
        try {
2167
            fin = new FileInputStream(fileName);
2168
            byte[] buf = new byte[4 * 1024]; // 4K buffer
2169
            int b = fin.read(buf);
2170
            while (b != -1) {
2171
                zipOut.write(buf, 0, b);
2172
                b = fin.read(buf);
2173
            }//while
2174
            zipOut.closeEntry();
2175
        }//try
2176
        catch (IOException ioe) {
2177
            logMetacat.error("There is an exception: "
2178
                    + ioe.getMessage());
2179
        }//catch
2180
    }//addDataFileToZipOutputStream()
2181

    
2182
    /**
2183
     * create a html summary for data package and put it into zip output stream
2184
     *
2185
     * @param docImplList, the documentImpl ojbects in data package
2186
     * @param zipOut, the zip output stream which the html should be put
2187
     * @param packageZipEntry, the zip entry name for whole package
2188
     */
2189
    private void addHtmlSummaryToZipOutputStream(Vector docImplList,
2190
            ZipOutputStream zipOut, String packageZipEntry) throws Exception
2191
    {
2192
        StringBuffer htmlDoc = new StringBuffer();
2193
        ZipEntry zEntry = null;
2194
        byte[] byteString = null;
2195
        InputStream source;
2196
        DBTransform xmlToHtml;
2197

    
2198
        //create a DBTransform ojbect
2199
        xmlToHtml = new DBTransform();
2200
        //head of html
2201
        htmlDoc.append("<html><head></head><body>");
2202
        for (int i = 0; i < docImplList.size(); i++) {
2203
            // If this String object, this means it is missed data file
2204
            if ((((docImplList.elementAt(i)).getClass()).toString())
2205
                    .equals("class java.lang.String")) {
2206

    
2207
                htmlDoc.append("<a href=\"");
2208
                String dataFileid = (String) docImplList.elementAt(i);
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

    
2214
            }//if
2215
            else if ((((DocumentImpl) docImplList.elementAt(i)).getDoctype())
2216
                    .compareTo("BIN") != 0) { //this is an xml file so we can
2217
                                              // transform it.
2218
                //transform each file individually then concatenate all of the
2219
                //transformations together.
2220

    
2221
                //for metadata xml title
2222
                htmlDoc.append("<h2>");
2223
                htmlDoc.append(((DocumentImpl) docImplList.elementAt(i))
2224
                        .getDocID());
2225
                //htmlDoc.append(".");
2226
                //htmlDoc.append(((DocumentImpl)docImplList.elementAt(i)).getRev());
2227
                htmlDoc.append("</h2>");
2228
                //do the actual transform
2229
                StringWriter docString = new StringWriter();
2230
                xmlToHtml.transformXMLDocument(((DocumentImpl) docImplList
2231
                        .elementAt(i)).toString(), "-//NCEAS//eml-generic//EN",
2232
                        "-//W3C//HTML//EN", "html", docString);
2233
                htmlDoc.append(docString.toString());
2234
                htmlDoc.append("<br><br><hr><br><br>");
2235
            }//if
2236
            else { //this is a data file so we should link to it in the html
2237
                htmlDoc.append("<a href=\"");
2238
                String dataFileid = ((DocumentImpl) docImplList.elementAt(i))
2239
                        .getDocID();
2240
                htmlDoc.append("./data/").append(dataFileid).append("\">");
2241
                htmlDoc.append("Data File: ");
2242
                htmlDoc.append(dataFileid).append("</a><br>");
2243
                htmlDoc.append("<br><hr><br>");
2244
            }//else
2245
        }//for
2246
        htmlDoc.append("</body></html>");
2247
        byteString = htmlDoc.toString().getBytes();
2248
        zEntry = new ZipEntry(packageZipEntry + "/metadata.html");
2249
        zEntry.setSize(byteString.length);
2250
        zipOut.putNextEntry(zEntry);
2251
        zipOut.write(byteString, 0, byteString.length);
2252
        zipOut.closeEntry();
2253
        //dbConn.close();
2254

    
2255
    }//addHtmlSummaryToZipOutputStream
2256

    
2257
    /**
2258
     * put a data packadge into a zip output stream
2259
     *
2260
     * @param docId, which the user want to put into zip output stream,it has version
2261
     * @param out, a servletoutput stream which the zip output stream will be
2262
     *            put
2263
     * @param user, the username of the user
2264
     * @param groups, the group of the user
2265
     */
2266
    public ZipOutputStream getZippedPackage(String docIdString,
2267
            ServletOutputStream out, String user, String[] groups,
2268
            String passWord) throws ClassNotFoundException, IOException,
2269
            SQLException, McdbException, NumberFormatException, Exception
2270
    {
2271
        ZipOutputStream zOut = null;
2272
        String elementDocid = null;
2273
        DocumentImpl docImpls = null;
2274
        //Connection dbConn = null;
2275
        Vector docIdList = new Vector();
2276
        Vector documentImplList = new Vector();
2277
        Vector htmlDocumentImplList = new Vector();
2278
        String packageId = null;
2279
        String rootName = "package";//the package zip entry name
2280

    
2281
        String docId = null;
2282
        int version = -5;
2283
        // Docid without revision
2284
        docId = MetaCatUtil.getDocIdFromString(docIdString);
2285
        // revision number
2286
        version = MetaCatUtil.getVersionFromString(docIdString);
2287

    
2288
        //check if the reqused docId is a data package id
2289
        if (!isDataPackageId(docId)) {
2290

    
2291
            /*
2292
             * Exception e = new Exception("The request the doc id "
2293
             * +docIdString+ " is not a data package id");
2294
             */
2295

    
2296
            //CB 1/6/03: if the requested docid is not a datapackage, we just
2297
            // zip
2298
            //up the single document and return the zip file.
2299
            if (!hasPermissionToExportPackage(docId, user, groups)) {
2300

    
2301
                Exception e = new Exception("User " + user
2302
                        + " does not have permission"
2303
                        + " to export the data package " + docIdString);
2304
                throw e;
2305
            }
2306

    
2307
            docImpls = new DocumentImpl(docIdString);
2308
            //checking if the user has the permission to read the documents
2309
            if (DocumentImpl.hasReadPermission(user, groups, docImpls
2310
                    .getDocID())) {
2311
                zOut = new ZipOutputStream(out);
2312
                //if the docImpls is metadata
2313
                if ((docImpls.getDoctype()).compareTo("BIN") != 0) {
2314
                    //add metadata into zip output stream
2315
                    addDocToZipOutputStream(docImpls, zOut, rootName);
2316
                }//if
2317
                else {
2318
                    //it is data file
2319
                    addDataFileToZipOutputStream(docImpls, zOut, rootName);
2320
                    htmlDocumentImplList.add(docImpls);
2321
                }//else
2322
            }//if
2323

    
2324
            zOut.finish(); //terminate the zip file
2325
            return zOut;
2326
        }
2327
        // Check the permission of user
2328
        else if (!hasPermissionToExportPackage(docId, user, groups)) {
2329

    
2330
            Exception e = new Exception("User " + user
2331
                    + " does not have permission"
2332
                    + " to export the data package " + docIdString);
2333
            throw e;
2334
        } else //it is a packadge id
2335
        {
2336
            //store the package id
2337
            packageId = docId;
2338
            //get current version in database
2339
            int currentVersion = getCurrentRevFromXMLDoumentsTable(packageId);
2340
            //If it is for current version (-1 means user didn't specify
2341
            // revision)
2342
            if ((version == -1) || version == currentVersion) {
2343
                //get current version number
2344
                version = currentVersion;
2345
                //get package zip entry name
2346
                //it should be docId.revsion.package
2347
                rootName = packageId + MetaCatUtil.getOption("accNumSeparator")
2348
                        + version + MetaCatUtil.getOption("accNumSeparator")
2349
                        + "package";
2350
                //get the whole id list for data packadge
2351
                docIdList = getCurrentDocidListForDataPackage(packageId);
2352
                //get the whole documentImple object
2353
                documentImplList = getCurrentAllDocumentImpl(docIdList);
2354

    
2355
            }//if
2356
            else if (version > currentVersion || version < -1) {
2357
                throw new Exception("The user specified docid: " + docId + "."
2358
                        + version + " doesn't exist");
2359
            }//else if
2360
            else //for an old version
2361
            {
2362

    
2363
                rootName = docIdString
2364
                        + MetaCatUtil.getOption("accNumSeparator") + "package";
2365
                //get the whole id list for data packadge
2366
                docIdList = getOldVersionDocidListForDataPackage(docIdString);
2367

    
2368
                //get the whole documentImple object
2369
                documentImplList = getOldVersionAllDocumentImpl(docIdList);
2370
            }//else
2371

    
2372
            // Make sure documentImplist is not empty
2373
            if (documentImplList.isEmpty()) { throw new Exception(
2374
                    "Couldn't find component for data package: " + packageId); }//if
2375

    
2376
            zOut = new ZipOutputStream(out);
2377
            //put every element into zip output stream
2378
            for (int i = 0; i < documentImplList.size(); i++) {
2379
                // if the object in the vetor is String, this means we couldn't
2380
                // find
2381
                // the document locally, we need find it remote
2382
                if ((((documentImplList.elementAt(i)).getClass()).toString())
2383
                        .equals("class java.lang.String")) {
2384
                    // Get String object from vetor
2385
                    String documentId = (String) documentImplList.elementAt(i);
2386
                    logMetacat.info("docid: " + documentId);
2387
                    // Get doicd without revision
2388
                    String docidWithoutRevision = MetaCatUtil
2389
                            .getDocIdFromString(documentId);
2390
                    logMetacat.info("docidWithoutRevsion: "
2391
                            + docidWithoutRevision);
2392
                    // Get revision
2393
                    String revision = MetaCatUtil
2394
                            .getRevisionStringFromString(documentId);
2395
                    logMetacat.info("revsion from docIdentifier: "
2396
                            + revision);
2397
                    // Zip entry string
2398
                    String zipEntryPath = rootName + "/data/";
2399
                    // Create a RemoteDocument object
2400
                    RemoteDocument remoteDoc = new RemoteDocument(
2401
                            docidWithoutRevision, revision, user, passWord,
2402
                            zipEntryPath);
2403
                    // Here we only read data file from remote metacat
2404
                    String docType = remoteDoc.getDocType();
2405
                    if (docType != null) {
2406
                        if (docType.equals("BIN")) {
2407
                            // Put remote document to zip output
2408
                            remoteDoc.readDocumentFromRemoteServerByZip(zOut);
2409
                            // Add String object to htmlDocumentImplList
2410
                            String elementInHtmlList = remoteDoc
2411
                                    .getDocIdWithoutRevsion()
2412
                                    + MetaCatUtil.getOption("accNumSeparator")
2413
                                    + remoteDoc.getRevision();
2414
                            htmlDocumentImplList.add(elementInHtmlList);
2415
                        }//if
2416
                    }//if
2417

    
2418
                }//if
2419
                else {
2420
                    //create a docmentImpls object (represent xml doc) base on
2421
                    // the docId
2422
                    docImpls = (DocumentImpl) documentImplList.elementAt(i);
2423
                    //checking if the user has the permission to read the
2424
                    // documents
2425
                    if (DocumentImpl.hasReadPermission(user, groups, docImpls
2426
                            .getDocID())) {
2427
                        //if the docImpls is metadata
2428
                        if ((docImpls.getDoctype()).compareTo("BIN") != 0) {
2429
                            //add metadata into zip output stream
2430
                            addDocToZipOutputStream(docImpls, zOut, rootName);
2431
                            //add the documentImpl into the vetor which will
2432
                            // be used in html
2433
                            htmlDocumentImplList.add(docImpls);
2434

    
2435
                        }//if
2436
                        else {
2437
                            //it is data file
2438
                            addDataFileToZipOutputStream(docImpls, zOut,
2439
                                    rootName);
2440
                            htmlDocumentImplList.add(docImpls);
2441
                        }//else
2442
                    }//if
2443
                }//else
2444
            }//for
2445

    
2446
            //add html summary file
2447
            addHtmlSummaryToZipOutputStream(htmlDocumentImplList, zOut,
2448
                    rootName);
2449
            zOut.finish(); //terminate the zip file
2450
            //dbConn.close();
2451
            return zOut;
2452
        }//else
2453
    }//getZippedPackage()
2454

    
2455
    private class ReturnFieldValue
2456
    {
2457

    
2458
        private String docid = null; //return field value for this docid
2459

    
2460
        private String fieldValue = null;
2461

    
2462
        private String xmlFieldValue = null; //return field value in xml
2463
                                             // format
2464

    
2465
        public void setDocid(String myDocid)
2466
        {
2467
            docid = myDocid;
2468
        }
2469

    
2470
        public String getDocid()
2471
        {
2472
            return docid;
2473
        }
2474

    
2475
        public void setFieldValue(String myValue)
2476
        {
2477
            fieldValue = myValue;
2478
        }
2479

    
2480
        public String getFieldValue()
2481
        {
2482
            return fieldValue;
2483
        }
2484

    
2485
        public void setXMLFieldValue(String xml)
2486
        {
2487
            xmlFieldValue = xml;
2488
        }
2489

    
2490
        public String getXMLFieldValue()
2491
        {
2492
            return xmlFieldValue;
2493
        }
2494

    
2495
    }
2496
}
(21-21/66)