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: berkley $'
13
 *     '$Date: 2007-04-18 16:03:04 -0700 (Wed, 18 Apr 2007) $'
14
 * '$Revision: 3247 $'
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.*;
34
import java.util.zip.*;
35
import java.sql.PreparedStatement;
36
import java.sql.ResultSet;
37
import java.sql.SQLException;
38
import java.util.*;
39

    
40
import javax.servlet.ServletOutputStream;
41
import javax.servlet.http.HttpServletResponse;
42
import javax.servlet.http.HttpSession;
43

    
44
import org.apache.log4j.Logger;
45

    
46
import org.w3c.dom.*;
47
import javax.xml.parsers.DocumentBuilderFactory;
48
import org.xml.sax.InputSource;
49
import org.w3c.dom.ls.*;
50

    
51
import edu.ucsb.nceas.morpho.datapackage.Triple;
52
import edu.ucsb.nceas.morpho.datapackage.TripleCollection;
53

    
54

    
55
/**
56
 * A Class that searches a relational DB for elements and attributes that have
57
 * free text matches a query string, or structured query matches to a path
58
 * specified node in the XML hierarchy. It returns a result set consisting of
59
 * the document ID for each document that satisfies the query
60
 */
61
public class DBQuery
62
{
63

    
64
    static final int ALL = 1;
65

    
66
    static final int WRITE = 2;
67

    
68
    static final int READ = 4;
69

    
70
    //private Connection conn = null;
71
    private String parserName = null;
72

    
73
    private MetaCatUtil util = new MetaCatUtil();
74

    
75
    private Logger logMetacat = Logger.getLogger(DBQuery.class);
76

    
77
    /** true if the metacat spatial option is installed **/
78
    private final boolean METACAT_SPATIAL = true;
79

    
80
    /** useful if you just want to grab a list of docids **/
81
    Vector docidOverride = new Vector();
82

    
83
    /**
84
     * the main routine used to test the DBQuery utility.
85
     * <p>
86
     * Usage: java DBQuery <xmlfile>
87
     *
88
     * @param xmlfile the filename of the xml file containing the query
89
     */
90
    static public void main(String[] args)
91
    {
92

    
93
        if (args.length < 1) {
94
            System.err.println("Wrong number of arguments!!!");
95
            System.err.println("USAGE: java DBQuery [-t] [-index] <xmlfile>");
96
            return;
97
        } else {
98
            try {
99

    
100
                int i = 0;
101
                boolean showRuntime = false;
102
                boolean useXMLIndex = false;
103
                if (args[i].equals("-t")) {
104
                    showRuntime = true;
105
                    i++;
106
                }
107
                if (args[i].equals("-index")) {
108
                    useXMLIndex = true;
109
                    i++;
110
                }
111
                String xmlfile = args[i];
112

    
113
                // Time the request if asked for
114
                double startTime = System.currentTimeMillis();
115

    
116
                // Open a connection to the database
117
                MetaCatUtil util = new MetaCatUtil();
118
                //Connection dbconn = util.openDBConnection();
119

    
120
                double connTime = System.currentTimeMillis();
121

    
122
                // Execute the query
123
                DBQuery queryobj = new DBQuery();
124
                FileReader xml = new FileReader(new File(xmlfile));
125
                Hashtable nodelist = null;
126
                //nodelist = queryobj.findDocuments(xml, null, null, useXMLIndex);
127

    
128
                // Print the reulting document listing
129
                StringBuffer result = new StringBuffer();
130
                String document = null;
131
                String docid = null;
132
                result.append("<?xml version=\"1.0\"?>\n");
133
                result.append("<resultset>\n");
134

    
135
                if (!showRuntime) {
136
                    Enumeration doclist = nodelist.keys();
137
                    while (doclist.hasMoreElements()) {
138
                        docid = (String) doclist.nextElement();
139
                        document = (String) nodelist.get(docid);
140
                        result.append("  <document>\n    " + document
141
                                + "\n  </document>\n");
142
                    }
143

    
144
                    result.append("</resultset>\n");
145
                }
146
                // Time the request if asked for
147
                double stopTime = System.currentTimeMillis();
148
                double dbOpenTime = (connTime - startTime) / 1000;
149
                double readTime = (stopTime - connTime) / 1000;
150
                double executionTime = (stopTime - startTime) / 1000;
151
                if (showRuntime) {
152
                    System.out.print("  " + executionTime);
153
                    System.out.print("  " + dbOpenTime);
154
                    System.out.print("  " + readTime);
155
                    System.out.print("  " + nodelist.size());
156
                    System.out.println();
157
                }
158
                //System.out.println(result);
159
                //write into a file "result.txt"
160
                if (!showRuntime) {
161
                    File f = new File("./result.txt");
162
                    FileWriter fw = new FileWriter(f);
163
                    BufferedWriter out = new BufferedWriter(fw);
164
                    out.write(result.toString());
165
                    out.flush();
166
                    out.close();
167
                    fw.close();
168
                }
169

    
170
            } catch (Exception e) {
171
                System.err.println("Error in DBQuery.main");
172
                System.err.println(e.getMessage());
173
                e.printStackTrace(System.err);
174
            }
175
        }
176
    }
177

    
178
    /**
179
     * construct an instance of the DBQuery class
180
     *
181
     * <p>
182
     * Generally, one would call the findDocuments() routine after creating an
183
     * instance to specify the search query
184
     * </p>
185
     *
186

    
187
     * @param parserName the fully qualified name of a Java class implementing
188
     *            the org.xml.sax.XMLReader interface
189
     */
190
    public DBQuery()
191
    {
192
        String parserName = MetaCatUtil.getOption("saxparser");
193
        this.parserName = parserName;
194
    }
195

    
196
    /**
197
     * 
198
     * Construct an instance of DBQuery Class
199
     * BUT accept a docid Vector that will supersede
200
     * the query.printSQL() method
201
     *
202
     * If a docid Vector is passed in,
203
     * the docids will be used to create a simple IN query 
204
     * without the multiple subselects of the printSQL() method
205
     *
206
     * Using this constructor, we just check for 
207
     * a docidOverride Vector in the findResultDoclist() method
208
     *
209
     * @param docids List of docids to display in the resultset
210
     */
211
    public DBQuery(Vector docids)
212
    {
213
        this.docidOverride = docids;
214
        String parserName = MetaCatUtil.getOption("saxparser");
215
        this.parserName = parserName;
216
    }
217

    
218
  /**
219
   * Method put the search result set into out printerwriter
220
   * @param resoponse the return response
221
   * @param out the output printer
222
   * @param params the paratermer hashtable
223
   * @param user the user name (it maybe different to the one in param)
224
   * @param groups the group array
225
   * @param sessionid  the sessionid
226
   */
227
  public void findDocuments(HttpServletResponse response,
228
                                       PrintWriter out, Hashtable params,
229
                                       String user, String[] groups,
230
                                       String sessionid)
231
  {
232
    boolean useXMLIndex = (new Boolean(MetaCatUtil.getOption("usexmlindex")))
233
               .booleanValue();
234
    findDocuments(response, out, params, user, groups, sessionid, useXMLIndex);
235

    
236
  }
237

    
238

    
239
    /**
240
     * Method put the search result set into out printerwriter
241
     * @param resoponse the return response
242
     * @param out the output printer
243
     * @param params the paratermer hashtable
244
     * @param user the user name (it maybe different to the one in param)
245
     * @param groups the group array
246
     * @param sessionid  the sessionid
247
     */
248
    public void findDocuments(HttpServletResponse response,
249
                                         PrintWriter out, Hashtable params,
250
                                         String user, String[] groups,
251
                                         String sessionid, boolean useXMLIndex)
252
    {
253
      int pagesize = 0;
254
      int pagestart = 0;
255
      
256
      if(params.containsKey("pagesize") && params.containsKey("pagestart"))
257
      {
258
        String pagesizeStr = ((String[])params.get("pagesize"))[0];
259
        String pagestartStr = ((String[])params.get("pagestart"))[0];
260
        if(pagesizeStr != null && pagestartStr != null)
261
        {
262
          pagesize = (new Integer(pagesizeStr)).intValue();
263
          pagestart = (new Integer(pagestartStr)).intValue();
264
        }
265
      }
266
      
267
      // get query and qformat
268
      String xmlquery = ((String[])params.get("query"))[0];
269

    
270
      logMetacat.info("SESSIONID: " + sessionid);
271
      logMetacat.info("xmlquery: " + xmlquery);
272
      String qformat = ((String[])params.get("qformat"))[0];
273
      logMetacat.info("qformat: " + qformat);
274
      // Get the XML query and covert it into a SQL statment
275
      QuerySpecification qspec = null;
276
      if ( xmlquery != null)
277
      {
278
         xmlquery = transformQuery(xmlquery);
279
         try
280
         {
281
           qspec = new QuerySpecification(xmlquery,
282
                                          parserName,
283
                                          MetaCatUtil.getOption("accNumSeparator"));
284
         }
285
         catch (Exception ee)
286
         {
287
           logMetacat.error("error generating QuerySpecification object"
288
                                    +" in DBQuery.findDocuments"
289
                                    + ee.getMessage());
290
         }
291
      }
292

    
293

    
294

    
295
      if (qformat != null && qformat.equals(MetaCatServlet.XMLFORMAT))
296
      {
297
        //xml format
298
        response.setContentType("text/xml");
299
        createResultDocument(xmlquery, qspec, out, user, groups, useXMLIndex, 
300
          pagesize, pagestart, sessionid);
301
      }//if
302
      else
303
      {
304
        //knb format, in this case we will get whole result and sent it out
305
        response.setContentType("text/html");
306
        PrintWriter nonout = null;
307
        StringBuffer xml = createResultDocument(xmlquery, qspec, nonout, user,
308
                                                groups, useXMLIndex, pagesize, 
309
                                                pagestart, sessionid);
310
        
311
        //transfer the xml to html
312
        try
313
        {
314

    
315
         DBTransform trans = new DBTransform();
316
         response.setContentType("text/html");
317

    
318
         // if the user is a moderator, then pass a param to the 
319
         // xsl specifying the fact
320
         if(MetaCatUtil.isModerator(user, groups)){
321
        	 params.put("isModerator", new String[] {"true"});
322
         }
323

    
324
         trans.transformXMLDocument(xml.toString(), "-//NCEAS//resultset//EN",
325
                                 "-//W3C//HTML//EN", qformat, out, params,
326
                                 sessionid);
327

    
328
        }
329
        catch(Exception e)
330
        {
331
         logMetacat.error("Error in MetaCatServlet.transformResultset:"
332
                                +e.getMessage());
333
         }
334

    
335
      }//else
336

    
337
  }
338
  
339
  /**
340
   * Transforms a hashtable of documents to an xml or html result and sent
341
   * the content to outputstream. Keep going untill hastable is empty. stop it.
342
   * add the QuerySpecification as parameter is for ecogrid. But it is duplicate
343
   * to xmlquery String
344
   * @param xmlquery
345
   * @param qspec
346
   * @param out
347
   * @param user
348
   * @param groups
349
   * @param useXMLIndex
350
   * @param sessionid
351
   * @return
352
   */
353
    public StringBuffer createResultDocument(String xmlquery,
354
                                              QuerySpecification qspec,
355
                                              PrintWriter out,
356
                                              String user, String[] groups,
357
                                              boolean useXMLIndex)
358
    {
359
    	return createResultDocument(xmlquery,qspec,out, user,groups, useXMLIndex, 0, 0,"");
360
    }
361

    
362
  /*
363
   * Transforms a hashtable of documents to an xml or html result and sent
364
   * the content to outputstream. Keep going untill hastable is empty. stop it.
365
   * add the QuerySpecification as parameter is for ecogrid. But it is duplicate
366
   * to xmlquery String
367
   */
368
  public StringBuffer createResultDocument(String xmlquery,
369
                                            QuerySpecification qspec,
370
                                            PrintWriter out,
371
                                            String user, String[] groups,
372
                                            boolean useXMLIndex, int pagesize,
373
                                            int pagestart, String sessionid)
374
  {
375
    DBConnection dbconn = null;
376
    int serialNumber = -1;
377
    StringBuffer resultset = new StringBuffer();
378

    
379
    //try to get the cached version first    
380
    Hashtable sessionHash = MetaCatServlet.getSessionHash();
381
    HttpSession sess = (HttpSession)sessionHash.get(sessionid);
382

    
383
    QuerySpecification cachedQuerySpec = null;
384
    if (sess != null)
385
    {
386
    	cachedQuerySpec = (QuerySpecification)sess.getAttribute("query");
387
    }
388
    
389
    resultset.append("<?xml version=\"1.0\"?>\n");
390
    resultset.append("<resultset>\n");
391
    resultset.append("  <query>" + xmlquery + "</query>");
392
    //send out a new query
393
    if (out != null)
394
    {
395
      out.println(resultset.toString());
396
    }
397
    if (qspec != null)
398
    {
399
      try
400
      {
401

    
402
        //checkout the dbconnection
403
        dbconn = DBConnectionPool.getDBConnection("DBQuery.findDocuments");
404
        serialNumber = dbconn.getCheckOutSerialNumber();
405

    
406
        //print out the search result
407
        // search the doc list
408
        resultset = findResultDoclist(qspec, resultset, out, user, groups,
409
                                      dbconn, useXMLIndex, pagesize, pagestart, 
410
                                      sessionid);
411
      } //try
412
      catch (IOException ioe)
413
      {
414
        logMetacat.error("IO error in DBQuery.findDocuments:");
415
        logMetacat.error(ioe.getMessage());
416

    
417
      }
418
      catch (SQLException e)
419
      {
420
        logMetacat.error("SQL Error in DBQuery.findDocuments: "
421
                                 + e.getMessage());
422
      }
423
      catch (Exception ee)
424
      {
425
        logMetacat.error("Exception in DBQuery.findDocuments: "
426
                                 + ee.getMessage());
427
        ee.printStackTrace();
428
      }
429
      finally
430
      {
431
        DBConnectionPool.returnDBConnection(dbconn, serialNumber);
432
      } //finally
433
    }//if
434
    String closeRestultset = "</resultset>";
435
    resultset.append(closeRestultset);
436
    if (out != null)
437
    {
438
      out.println(closeRestultset);
439
    }
440

    
441
    //default to returning the whole resultset
442
    return resultset;
443
  }//createResultDocuments
444

    
445
    /*
446
     * Find the doc list which match the query
447
     */
448
    private StringBuffer findResultDoclist(QuerySpecification qspec,
449
                                      StringBuffer resultsetBuffer,
450
                                      PrintWriter out,
451
                                      String user, String[]groups,
452
                                      DBConnection dbconn, boolean useXMLIndex,
453
                                      int pagesize, int pagestart, String sessionid)
454
                                      throws Exception
455
    {
456
      String query = null;
457
      int count = 0;
458
      int index = 0;
459
      ResultDocumentSet docListResult = new ResultDocumentSet();
460
      PreparedStatement pstmt = null;
461
      String docid = null;
462
      String docname = null;
463
      String doctype = null;
464
      String createDate = null;
465
      String updateDate = null;
466
      StringBuffer document = null;
467
      int rev = 0;
468
      double startTime = 0;
469
      int offset = 1;
470
      
471
      ResultSet rs = null;
472
        
473
      offset = 1;
474
      // this is a hack for offset
475
      if (out == null)
476
      {
477
        // for html page, we put everything into one page
478
        offset =
479
            (new Integer(MetaCatUtil.getOption("web_resultsetsize"))).intValue();
480
      }
481
      else
482
      {
483
          offset =
484
              (new Integer(MetaCatUtil.getOption("app_resultsetsize"))).intValue();
485
      }
486

    
487
      /*
488
       * Check the docidOverride Vector
489
       * if defined, we bypass the qspec.printSQL() method
490
       * and contruct a simpler query based on a 
491
       * list of docids rather than a bunch of subselects
492
       */
493
      if ( this.docidOverride.size() == 0 ) {
494
          query = qspec.printSQL(useXMLIndex);
495
      } else {
496
          logMetacat.info("*** docid override " + this.docidOverride.size());
497
          StringBuffer queryBuffer = new StringBuffer( "SELECT docid,docname,doctype,date_created, date_updated, rev " );
498
          queryBuffer.append( " FROM xml_documents WHERE docid IN (" );
499
          for (int i = 0; i < docidOverride.size(); i++) {  
500
              queryBuffer.append("'");
501
              queryBuffer.append( (String)docidOverride.elementAt(i) );
502
              queryBuffer.append("',");
503
          }
504
          // empty string hack 
505
          queryBuffer.append( "'') " );
506
          query = queryBuffer.toString();
507
      } 
508
      String ownerQuery = getOwnerQuery(user);
509
      logMetacat.info("\n\n\n query: " + query);
510
      logMetacat.info("\n\n\n owner query: "+ownerQuery);
511
      // if query is not the owner query, we need to check the permission
512
      // otherwise we don't need (owner has all permission by default)
513
      if (!query.equals(ownerQuery))
514
      {
515
        // set user name and group
516
        qspec.setUserName(user);
517
        qspec.setGroup(groups);
518
        // Get access query
519
        String accessQuery = qspec.getAccessQuery();
520
        if(!query.endsWith("WHERE")){
521
            query = query + accessQuery;
522
        } else {
523
            query = query + accessQuery.substring(4, accessQuery.length());
524
        }
525
        logMetacat.info("\n\n\n final query: " + query);
526
      }
527

    
528
      startTime = System.currentTimeMillis() / 1000;
529
      pstmt = dbconn.prepareStatement(query);
530
      rs = pstmt.executeQuery();
531

    
532
      double queryExecuteTime = System.currentTimeMillis() / 1000;
533
      logMetacat.warn("Time to execute query: "
534
                    + (queryExecuteTime - startTime));
535

    
536
      boolean tableHasRows = rs.next();
537
      
538
      if(pagesize == 0)
539
      { //this makes sure we get all results if there is no paging
540
        pagesize = 99999;
541
        pagestart = 99999;
542
      } 
543
      
544
      int currentIndex = 0;
545
      while (tableHasRows)
546
      //for(int z=pagestart * pagesize; z<(pagesize * pagestart) + pagesize;)
547
      {
548
        logMetacat.warn("############getting result: " + currentIndex);
549
        docid = rs.getString(1).trim();
550
        logMetacat.warn("############processing: " + docid);
551
        docname = rs.getString(2);
552
        doctype = rs.getString(3);
553
        logMetacat.warn("############processing: " + doctype);
554
        createDate = rs.getString(4);
555
        updateDate = rs.getString(5);
556
        rev = rs.getInt(6);
557
        
558
        // if there are returndocs to match, backtracking can be performed
559
        // otherwise, just return the document that was hit
560
        Vector returndocVec = qspec.getReturnDocList();
561
        if (returndocVec.size() != 0 && !returndocVec.contains(doctype)
562
             && !qspec.isPercentageSearch())
563
         {
564
           logMetacat.warn("Back tracing now...");
565
           String sep = MetaCatUtil.getOption("accNumSeparator");
566
           StringBuffer btBuf = new StringBuffer();
567
           btBuf.append("select docid from xml_relation where ");
568

    
569
           //build the doctype list for the backtracking sql statement
570
           btBuf.append("packagetype in (");
571
           for (int i = 0; i < returndocVec.size(); i++)
572
           {
573
             btBuf.append("'").append((String) returndocVec.get(i)).append("'");
574
             if (i != (returndocVec.size() - 1))
575
             {
576
                btBuf.append(", ");
577
              }
578
            }
579
            btBuf.append(") ");
580
            btBuf.append("and (subject like '");
581
            btBuf.append(docid).append("'");
582
            btBuf.append("or object like '");
583
            btBuf.append(docid).append("')");
584

    
585
            PreparedStatement npstmt = dbconn.prepareStatement(btBuf.toString());
586
            //should incease usage count
587
            dbconn.increaseUsageCount(1);
588
            npstmt.execute();
589
            ResultSet btrs = npstmt.getResultSet();
590
            boolean hasBtRows = btrs.next();
591
            while (hasBtRows)
592
            {
593
               //there was a backtrackable document found
594
               DocumentImpl xmldoc = null;
595
               String packageDocid = btrs.getString(1);
596
               logMetacat.info("Getting document for docid: "
597
                                         + packageDocid);
598
                try
599
                {
600
                    //  THIS CONSTRUCTOR BUILDS THE WHOLE XML doc not
601
                    // needed here
602
                    // xmldoc = new DocumentImpl(dbconn, packageDocid);
603
                    //  thus use the following to get the doc info only
604
                    //  xmldoc = new DocumentImpl(dbconn);
605
                    String accNumber = packageDocid + MetaCatUtil.getOption("accNumSeparator") +
606
                    DBUtil.getLatestRevisionInDocumentTable(packageDocid);
607
                    xmldoc = new DocumentImpl(accNumber, false);
608
                    if (xmldoc == null)
609
                    {
610
                       logMetacat.info("Document was null for: "
611
                                                + packageDocid);
612
                    }
613
                }
614
                catch (Exception e)
615
                {
616
                    System.out.println("Error getting document in "
617
                                       + "DBQuery.findDocuments: "
618
                                       + e.getMessage());
619
                }
620

    
621
                String docid_org = xmldoc.getDocID();
622
                if (docid_org == null)
623
                {
624
                   logMetacat.info("Docid_org was null.");
625
                   //continue;
626
                }
627
                docid = docid_org.trim();
628
                docname = xmldoc.getDocname();
629
                doctype = xmldoc.getDoctype();
630
                createDate = xmldoc.getCreateDate();
631
                updateDate = xmldoc.getUpdateDate();
632
                rev = xmldoc.getRev();
633
                document = new StringBuffer();
634

    
635
                String completeDocid = docid
636
                                + MetaCatUtil.getOption("accNumSeparator");
637
                completeDocid += rev;
638
                document.append("<docid>").append(completeDocid);
639
                document.append("</docid>");
640
                if (docname != null)
641
                {
642
                  document.append("<docname>" + docname + "</docname>");
643
                }
644
                if (doctype != null)
645
                {
646
                  document.append("<doctype>" + doctype + "</doctype>");
647
                }
648
                if (createDate != null)
649
                {
650
                 document.append("<createdate>" + createDate + "</createdate>");
651
                }
652
                if (updateDate != null)
653
                {
654
                  document.append("<updatedate>" + updateDate+ "</updatedate>");
655
                }
656
                // Store the document id and the root node id
657
                docListResult.addResultDocument(
658
                  new ResultDocument(docid, (String) document.toString()));
659
                currentIndex++;
660
                logMetacat.warn("$$$$$$$real result: " + docid);
661
                count++;
662

    
663
                // Get the next package document linked to our hit
664
                hasBtRows = btrs.next();
665
              }//while
666
              npstmt.close();
667
              btrs.close();
668
        }
669
        else if (returndocVec.size() == 0 || returndocVec.contains(doctype))
670
        {
671
          logMetacat.warn("NOT Back tracing now...");
672
           document = new StringBuffer();
673

    
674
           String completeDocid = docid
675
                            + MetaCatUtil.getOption("accNumSeparator");
676
           completeDocid += rev;
677
           document.append("<docid>").append(completeDocid).append("</docid>");
678
           if (docname != null)
679
           {
680
               document.append("<docname>" + docname + "</docname>");
681
           }
682
           if (doctype != null)
683
           {
684
              document.append("<doctype>" + doctype + "</doctype>");
685
           }
686
           if (createDate != null)
687
           {
688
               document.append("<createdate>" + createDate + "</createdate>");
689
           }
690
           if (updateDate != null)
691
           {
692
             document.append("<updatedate>" + updateDate + "</updatedate>");
693
           }
694
           // Store the document id and the root node id
695
           
696
           docListResult.addResultDocument(
697
             new ResultDocument(docid, (String) document.toString()));
698
           logMetacat.warn("$$$$$$$real result: " + docid);
699
           currentIndex++;
700
           count++;
701
        }//else
702
        
703
        // when doclist reached the offset number, send out doc list and empty
704
        // the hash table
705
        if (count == offset && pagesize == 0)
706
        { //if pagesize is not 0, do this later.
707
          //reset count
708
          logMetacat.warn("############doing subset cache");
709
          count = 0;
710
          handleSubsetResult(qspec, resultsetBuffer, out, docListResult,
711
                              user, groups,dbconn, useXMLIndex);
712
          //reset docListResult
713
          docListResult = new ResultDocumentSet();
714
        }
715
       
716
        logMetacat.warn("currentIndex: " + currentIndex);
717
       if(currentIndex >= ((pagesize * pagestart) + pagesize))
718
       {
719
         ResultDocumentSet pagedResultsHash = new ResultDocumentSet();
720
         for(int i=pagesize*pagestart; i<docListResult.size(); i++)
721
         {
722
           pagedResultsHash.put(docListResult.get(i));
723
         }
724
         
725
         docListResult = pagedResultsHash;
726
         break;
727
       }
728
       // Advance to the next record in the cursor
729
       tableHasRows = rs.next();
730
       if(!tableHasRows)
731
       {
732
         break;
733
       }
734
     }//while
735
     
736
     rs.close();
737
     pstmt.close();
738
     //if docListResult is not empty, it need to be sent.
739
     if (docListResult.size() != 0)
740
     {
741
       handleSubsetResult(qspec,resultsetBuffer, out, docListResult,
742
                              user, groups,dbconn, useXMLIndex);
743
     }
744
     double docListTime = System.currentTimeMillis() / 1000;
745
     logMetacat.warn("prepare docid list time: "
746
                    + (docListTime - queryExecuteTime));
747

    
748
     return resultsetBuffer;
749
    }//findReturnDoclist
750

    
751

    
752
    /*
753
     * Send completed search hashtable(part of reulst)to output stream
754
     * and buffer into a buffer stream
755
     */
756
    private StringBuffer handleSubsetResult(QuerySpecification qspec,
757
                                           StringBuffer resultset,
758
                                           PrintWriter out, ResultDocumentSet partOfDoclist,
759
                                           String user, String[]groups,
760
                                       DBConnection dbconn, boolean useXMLIndex)
761
                                       throws Exception
762
   {
763
     
764
     // check if there is a record in xml_returnfield
765
     // and get the returnfield_id and usage count
766
     int usage_count = getXmlReturnfieldsTableId(qspec, dbconn);
767
     boolean enterRecords = false;
768

    
769
     // get value of xml_returnfield_count
770
     int count = (new Integer(MetaCatUtil
771
                            .getOption("xml_returnfield_count")))
772
                            .intValue();
773

    
774
     // set enterRecords to true if usage_count is more than the offset
775
     // specified in metacat.properties
776
     if(usage_count > count){
777
         enterRecords = true;
778
     }
779

    
780
     if(returnfield_id < 0){
781
         logMetacat.warn("Error in getting returnfield id from"
782
                                  + "xml_returnfield table");
783
         enterRecords = false;
784
     }
785

    
786
     // get the hashtable containing the docids that already in the
787
     // xml_queryresult table
788
     logMetacat.info("size of partOfDoclist before"
789
                             + " docidsInQueryresultTable(): "
790
                             + partOfDoclist.size());
791
     Hashtable queryresultDocList = docidsInQueryresultTable(returnfield_id,
792
                                                        partOfDoclist, dbconn);
793

    
794
     // remove the keys in queryresultDocList from partOfDoclist
795
     Enumeration _keys = queryresultDocList.keys();
796
     while (_keys.hasMoreElements()){
797
         partOfDoclist.remove((String)_keys.nextElement());
798
     }
799

    
800
     // backup the keys-elements in partOfDoclist to check later
801
     // if the doc entry is indexed yet
802
     Hashtable partOfDoclistBackup = new Hashtable();
803
     Iterator itt = partOfDoclist.getDocids();
804
     while (itt.hasNext()){
805
       Object key = itt.next();
806
         partOfDoclistBackup.put(key, partOfDoclist.get(key));
807
     }
808

    
809
     logMetacat.info("size of partOfDoclist after"
810
                             + " docidsInQueryresultTable(): "
811
                             + partOfDoclist.size());
812

    
813
     //add return fields for the documents in partOfDoclist
814
     partOfDoclist = addReturnfield(partOfDoclist, qspec, user, groups,
815
                                        dbconn, useXMLIndex);
816
     //add relationship part part docid list for the documents in partOfDocList
817
     partOfDoclist = addRelationship(partOfDoclist, qspec, dbconn, useXMLIndex);
818

    
819

    
820
     Iterator keys = partOfDoclist.getDocids();
821
     String key = null;
822
     String element = null;
823
     String query = null;
824
     int offset = (new Integer(MetaCatUtil
825
                               .getOption("queryresult_string_length")))
826
                               .intValue();
827
     while (keys.hasNext())
828
     {
829
         key = (String) keys.next();
830
         element = (String)partOfDoclist.get(key);
831

    
832
	 // check if the enterRecords is true, elements is not null, element's
833
         // length is less than the limit of table column and if the document
834
         // has been indexed already
835
         if(enterRecords && element != null
836
		&& element.length() < offset
837
		&& element.compareTo((String) partOfDoclistBackup.get(key)) != 0){
838
             query = "INSERT INTO xml_queryresult (returnfield_id, docid, "
839
                 + "queryresult_string) VALUES (?, ?, ?)";
840

    
841
             PreparedStatement pstmt = null;
842
             pstmt = dbconn.prepareStatement(query);
843
             pstmt.setInt(1, returnfield_id);
844
             pstmt.setString(2, key);
845
             pstmt.setString(3, element);
846

    
847
             dbconn.increaseUsageCount(1);
848
             pstmt.execute();
849
             pstmt.close();
850
         }
851

    
852
         // A string with element
853
         String xmlElement = "  <document>" + element + "</document>";
854

    
855
         //send single element to output
856
         if (out != null)
857
         {
858
             out.println(xmlElement);
859
         }
860
         resultset.append(xmlElement);
861
     }//while
862

    
863

    
864
     Enumeration keysE = queryresultDocList.keys();
865
     while (keysE.hasMoreElements())
866
     {
867
         key = (String) keysE.nextElement();
868
         element = (String)queryresultDocList.get(key);
869
         // A string with element
870
         String xmlElement = "  <document>" + element + "</document>";
871
         //send single element to output
872
         if (out != null)
873
         {
874
             out.println(xmlElement);
875
         }
876
         resultset.append(xmlElement);
877
     }//while
878

    
879
     return resultset;
880
 }
881

    
882
   /**
883
    * Get the docids already in xml_queryresult table and corresponding
884
    * queryresultstring as a hashtable
885
    */
886
   private Hashtable docidsInQueryresultTable(int returnfield_id,
887
                                              ResultDocumentSet partOfDoclist,
888
                                              DBConnection dbconn){
889

    
890
         Hashtable returnValue = new Hashtable();
891
         PreparedStatement pstmt = null;
892
         ResultSet rs = null;
893

    
894
         // get partOfDoclist as string for the query
895
         Iterator keylist = partOfDoclist.getDocids();
896
         StringBuffer doclist = new StringBuffer();
897
         while (keylist.hasNext())
898
         {
899
             doclist.append("'");
900
             doclist.append((String) keylist.next());
901
             doclist.append("',");
902
         }//while
903

    
904

    
905
         if (doclist.length() > 0)
906
         {
907
             doclist.deleteCharAt(doclist.length() - 1); //remove the last comma
908

    
909
             // the query to find out docids from xml_queryresult
910
             String query = "select docid, queryresult_string from "
911
                          + "xml_queryresult where returnfield_id = " +
912
                          returnfield_id +" and docid in ("+ doclist + ")";
913
             logMetacat.info("Query to get docids from xml_queryresult:"
914
                                      + query);
915

    
916
             try {
917
                 // prepare and execute the query
918
                 pstmt = dbconn.prepareStatement(query);
919
                 dbconn.increaseUsageCount(1);
920
                 pstmt.execute();
921
                 rs = pstmt.getResultSet();
922
                 boolean tableHasRows = rs.next();
923
                 while (tableHasRows) {
924
                     // store the returned results in the returnValue hashtable
925
                     String key = rs.getString(1);
926
                     String element = rs.getString(2);
927

    
928
                     if(element != null){
929
                         returnValue.put(key, element);
930
                     } else {
931
                         logMetacat.info("Null elment found ("
932
                         + "DBQuery.docidsInQueryresultTable)");
933
                     }
934
                     tableHasRows = rs.next();
935
                 }
936
                 rs.close();
937
                 pstmt.close();
938
             } catch (Exception e){
939
                 logMetacat.error("Error getting docids from "
940
                                          + "queryresult in "
941
                                          + "DBQuery.docidsInQueryresultTable: "
942
                                          + e.getMessage());
943
              }
944
         }
945
         return returnValue;
946
     }
947

    
948

    
949
   /**
950
    * Method to get id from xml_returnfield table
951
    * for a given query specification
952
    */
953
   private int returnfield_id;
954
   private int getXmlReturnfieldsTableId(QuerySpecification qspec,
955
                                           DBConnection dbconn){
956
       int id = -1;
957
       int count = 1;
958
       PreparedStatement pstmt = null;
959
       ResultSet rs = null;
960
       String returnfield = qspec.getSortedReturnFieldString();
961

    
962
       // query for finding the id from xml_returnfield
963
       String query = "SELECT returnfield_id, usage_count FROM xml_returnfield "
964
            + "WHERE returnfield_string LIKE ?";
965
       logMetacat.info("ReturnField Query:" + query);
966

    
967
       try {
968
           // prepare and run the query
969
           pstmt = dbconn.prepareStatement(query);
970
           pstmt.setString(1,returnfield);
971
           dbconn.increaseUsageCount(1);
972
           pstmt.execute();
973
           rs = pstmt.getResultSet();
974
           boolean tableHasRows = rs.next();
975

    
976
           // if record found then increase the usage count
977
           // else insert a new record and get the id of the new record
978
           if(tableHasRows){
979
               // get the id
980
               id = rs.getInt(1);
981
               count = rs.getInt(2) + 1;
982
               rs.close();
983
               pstmt.close();
984

    
985
               // increase the usage count
986
               query = "UPDATE xml_returnfield SET usage_count ='" + count
987
                   + "' WHERE returnfield_id ='"+ id +"'";
988
               logMetacat.info("ReturnField Table Update:"+ query);
989

    
990
               pstmt = dbconn.prepareStatement(query);
991
               dbconn.increaseUsageCount(1);
992
               pstmt.execute();
993
               pstmt.close();
994

    
995
           } else {
996
               rs.close();
997
               pstmt.close();
998

    
999
               // insert a new record
1000
               query = "INSERT INTO xml_returnfield (returnfield_string, usage_count)"
1001
                   + "VALUES (?, '1')";
1002
               logMetacat.info("ReturnField Table Insert:"+ query);
1003
               pstmt = dbconn.prepareStatement(query);
1004
               pstmt.setString(1, returnfield);
1005
               dbconn.increaseUsageCount(1);
1006
               pstmt.execute();
1007
               pstmt.close();
1008

    
1009
               // get the id of the new record
1010
               query = "SELECT returnfield_id FROM xml_returnfield "
1011
                   + "WHERE returnfield_string LIKE ?";
1012
               logMetacat.info("ReturnField query after Insert:" + query);
1013
               pstmt = dbconn.prepareStatement(query);
1014
               pstmt.setString(1, returnfield);
1015

    
1016
               dbconn.increaseUsageCount(1);
1017
               pstmt.execute();
1018
               rs = pstmt.getResultSet();
1019
               if(rs.next()){
1020
                   id = rs.getInt(1);
1021
               } else {
1022
                   id = -1;
1023
               }
1024
               rs.close();
1025
               pstmt.close();
1026
           }
1027

    
1028
       } catch (Exception e){
1029
           logMetacat.error("Error getting id from xml_returnfield in "
1030
                                     + "DBQuery.getXmlReturnfieldsTableId: "
1031
                                     + e.getMessage());
1032
           id = -1;
1033
       }
1034

    
1035
       returnfield_id = id;
1036
       return count;
1037
   }
1038

    
1039

    
1040
    /*
1041
     * A method to add return field to return doclist hash table
1042
     */
1043
    private ResultDocumentSet addReturnfield(ResultDocumentSet docListResult,
1044
                                      QuerySpecification qspec,
1045
                                      String user, String[]groups,
1046
                                      DBConnection dbconn, boolean useXMLIndex )
1047
                                      throws Exception
1048
    {
1049
      PreparedStatement pstmt = null;
1050
      ResultSet rs = null;
1051
      String docid = null;
1052
      String fieldname = null;
1053
      String fielddata = null;
1054
      String relation = null;
1055

    
1056
      if (qspec.containsExtendedSQL())
1057
      {
1058
        qspec.setUserName(user);
1059
        qspec.setGroup(groups);
1060
        Vector extendedFields = new Vector(qspec.getReturnFieldList());
1061
        Vector results = new Vector();
1062
        Iterator keylist = docListResult.getDocids();
1063
        StringBuffer doclist = new StringBuffer();
1064
        Vector parentidList = new Vector();
1065
        Hashtable returnFieldValue = new Hashtable();
1066
        while (keylist.hasNext())
1067
        {
1068
          doclist.append("'");
1069
          doclist.append((String) keylist.next());
1070
          doclist.append("',");
1071
        }
1072
        if (doclist.length() > 0)
1073
        {
1074
          Hashtable controlPairs = new Hashtable();
1075
          double extendedQueryStart = System.currentTimeMillis() / 1000;
1076
          doclist.deleteCharAt(doclist.length() - 1); //remove the last comma
1077
          // check if user has permission to see the return field data
1078
          String accessControlSQL =
1079
                 qspec.printAccessControlSQLForReturnField(doclist.toString());
1080
          pstmt = dbconn.prepareStatement(accessControlSQL);
1081
          //increase dbconnection usage count
1082
          dbconn.increaseUsageCount(1);
1083
          pstmt.execute();
1084
          rs = pstmt.getResultSet();
1085
          boolean tableHasRows = rs.next();
1086
          while (tableHasRows)
1087
          {
1088
            long startNodeId = rs.getLong(1);
1089
            long endNodeId = rs.getLong(2);
1090
            controlPairs.put(new Long(startNodeId), new Long(endNodeId));
1091
            tableHasRows = rs.next();
1092
          }
1093

    
1094
           double extendedAccessQueryEnd = System.currentTimeMillis() / 1000;
1095
           logMetacat.info( "Time for execute access extended query: "
1096
                          + (extendedAccessQueryEnd - extendedQueryStart));
1097

    
1098
           String extendedQuery =
1099
               qspec.printExtendedSQL(doclist.toString(), controlPairs, useXMLIndex);
1100
           logMetacat.info("Extended query: " + extendedQuery);
1101

    
1102
           if(extendedQuery != null){
1103
               pstmt = dbconn.prepareStatement(extendedQuery);
1104
               //increase dbconnection usage count
1105
               dbconn.increaseUsageCount(1);
1106
               pstmt.execute();
1107
               rs = pstmt.getResultSet();
1108
               double extendedQueryEnd = System.currentTimeMillis() / 1000;
1109
               logMetacat.info(
1110
                   "Time for execute extended query: "
1111
                   + (extendedQueryEnd - extendedQueryStart));
1112
               tableHasRows = rs.next();
1113
               while (tableHasRows) {
1114
                   ReturnFieldValue returnValue = new ReturnFieldValue();
1115
                   docid = rs.getString(1).trim();
1116
                   fieldname = rs.getString(2);
1117
                   fielddata = rs.getString(3);
1118
                   fielddata = MetaCatUtil.normalize(fielddata);
1119
                   String parentId = rs.getString(4);
1120
                   StringBuffer value = new StringBuffer();
1121

    
1122
                   // if xml_index is used, there would be just one record per nodeid
1123
                   // as xml_index just keeps one entry for each path
1124
                   if (useXMLIndex || !containsKey(parentidList, parentId)) {
1125
                       // don't need to merger nodedata
1126
                       value.append("<param name=\"");
1127
                       value.append(fieldname);
1128
                       value.append("\">");
1129
                       value.append(fielddata);
1130
                       value.append("</param>");
1131
                       //set returnvalue
1132
                       returnValue.setDocid(docid);
1133
                       returnValue.setFieldValue(fielddata);
1134
                       returnValue.setXMLFieldValue(value.toString());
1135
                       // Store it in hastable
1136
                       putInArray(parentidList, parentId, returnValue);
1137
                   }
1138
                   else {
1139
                       // need to merge nodedata if they have same parent id and
1140
                       // node type is text
1141
                       fielddata = (String) ( (ReturnFieldValue)
1142
                                             getArrayValue(
1143
                           parentidList, parentId)).getFieldValue()
1144
                           + fielddata;
1145
                       value.append("<param name=\"");
1146
                       value.append(fieldname);
1147
                       value.append("\">");
1148
                       value.append(fielddata);
1149
                       value.append("</param>");
1150
                       returnValue.setDocid(docid);
1151
                       returnValue.setFieldValue(fielddata);
1152
                       returnValue.setXMLFieldValue(value.toString());
1153
                       // remove the old return value from paretnidList
1154
                       parentidList.remove(parentId);
1155
                       // store the new return value in parentidlit
1156
                       putInArray(parentidList, parentId, returnValue);
1157
                   }
1158
                   tableHasRows = rs.next();
1159
               } //while
1160
               rs.close();
1161
               pstmt.close();
1162

    
1163
               // put the merger node data info into doclistReult
1164
               Enumeration xmlFieldValue = (getElements(parentidList)).
1165
                   elements();
1166
               while (xmlFieldValue.hasMoreElements()) {
1167
                   ReturnFieldValue object =
1168
                       (ReturnFieldValue) xmlFieldValue.nextElement();
1169
                   docid = object.getDocid();
1170
                   if (docListResult.containsDocid(docid)) {
1171
                       String removedelement = (String) docListResult.
1172
                           remove(docid);
1173
                       docListResult.
1174
                           addResultDocument(new ResultDocument(docid,
1175
                               removedelement + object.getXMLFieldValue()));
1176
                   }
1177
                   else {
1178
                       docListResult.addResultDocument(
1179
                         new ResultDocument(docid, object.getXMLFieldValue()));
1180
                   }
1181
               } //while
1182
               double docListResultEnd = System.currentTimeMillis() / 1000;
1183
               logMetacat.warn(
1184
                   "Time for prepare doclistresult after"
1185
                   + " execute extended query: "
1186
                   + (docListResultEnd - extendedQueryEnd));
1187
           }
1188

    
1189
           // get attribures return
1190
           docListResult = getAttributeValueForReturn(qspec,
1191
                           docListResult, doclist.toString(), useXMLIndex);
1192
       }//if doclist lenght is great than zero
1193

    
1194
     }//if has extended query
1195

    
1196
      return docListResult;
1197
    }//addReturnfield
1198

    
1199
    /*
1200
    * A method to add relationship to return doclist hash table
1201
    */
1202
   private ResultDocumentSet addRelationship(ResultDocumentSet docListResult,
1203
                                     QuerySpecification qspec,
1204
                                     DBConnection dbconn, boolean useXMLIndex )
1205
                                     throws Exception
1206
  {
1207
    PreparedStatement pstmt = null;
1208
    ResultSet rs = null;
1209
    StringBuffer document = null;
1210
    double startRelation = System.currentTimeMillis() / 1000;
1211
    Iterator docidkeys = docListResult.getDocids();
1212
    while (docidkeys.hasNext())
1213
    {
1214
      //String connstring =
1215
      // "metacat://"+util.getOption("server")+"?docid=";
1216
      String connstring = "%docid=";
1217
      String docidkey = (String) docidkeys.next();
1218
      pstmt = dbconn.prepareStatement(QuerySpecification
1219
                      .printRelationSQL(docidkey));
1220
      pstmt.execute();
1221
      rs = pstmt.getResultSet();
1222
      boolean tableHasRows = rs.next();
1223
      while (tableHasRows)
1224
      {
1225
        String sub = rs.getString(1);
1226
        String rel = rs.getString(2);
1227
        String obj = rs.getString(3);
1228
        String subDT = rs.getString(4);
1229
        String objDT = rs.getString(5);
1230

    
1231
        document = new StringBuffer();
1232
        document.append("<triple>");
1233
        document.append("<subject>").append(MetaCatUtil.normalize(sub));
1234
        document.append("</subject>");
1235
        if (subDT != null)
1236
        {
1237
          document.append("<subjectdoctype>").append(subDT);
1238
          document.append("</subjectdoctype>");
1239
        }
1240
        document.append("<relationship>").append(MetaCatUtil.normalize(rel));
1241
        document.append("</relationship>");
1242
        document.append("<object>").append(MetaCatUtil.normalize(obj));
1243
        document.append("</object>");
1244
        if (objDT != null)
1245
        {
1246
          document.append("<objectdoctype>").append(objDT);
1247
          document.append("</objectdoctype>");
1248
        }
1249
        document.append("</triple>");
1250

    
1251
        String removedelement = (String) docListResult.remove(docidkey);
1252
        docListResult.put(docidkey, removedelement+ document.toString());
1253
        tableHasRows = rs.next();
1254
      }//while
1255
      rs.close();
1256
      pstmt.close();
1257
    }//while
1258
    double endRelation = System.currentTimeMillis() / 1000;
1259
    logMetacat.info("Time for adding relation to docListResult: "
1260
                             + (endRelation - startRelation));
1261

    
1262
    return docListResult;
1263
  }//addRelation
1264

    
1265
  /**
1266
   * removes the <?xml version="1.0"?> tag from the beginning.  This takes a
1267
   * string as a param instead of a hashtable.
1268
   *
1269
   * @param xmlquery a string representing a query.
1270
   */
1271
   private  String transformQuery(String xmlquery)
1272
   {
1273
     xmlquery = xmlquery.trim();
1274
     int index = xmlquery.indexOf("?>");
1275
     if (index != -1)
1276
     {
1277
       return xmlquery.substring(index + 2, xmlquery.length());
1278
     }
1279
     else
1280
     {
1281
       return xmlquery;
1282
     }
1283
   }
1284

    
1285

    
1286
    /*
1287
     * A method to search if Vector contains a particular key string
1288
     */
1289
    private boolean containsKey(Vector parentidList, String parentId)
1290
    {
1291

    
1292
        Vector tempVector = null;
1293

    
1294
        for (int count = 0; count < parentidList.size(); count++) {
1295
            tempVector = (Vector) parentidList.get(count);
1296
            if (parentId.compareTo((String) tempVector.get(0)) == 0) { return true; }
1297
        }
1298
        return false;
1299
    }
1300

    
1301
    /*
1302
     * A method to put key and value in Vector
1303
     */
1304
    private void putInArray(Vector parentidList, String key,
1305
            ReturnFieldValue value)
1306
    {
1307

    
1308
        Vector tempVector = null;
1309

    
1310
        for (int count = 0; count < parentidList.size(); count++) {
1311
            tempVector = (Vector) parentidList.get(count);
1312

    
1313
            if (key.compareTo((String) tempVector.get(0)) == 0) {
1314
                tempVector.remove(1);
1315
                tempVector.add(1, value);
1316
                return;
1317
            }
1318
        }
1319

    
1320
        tempVector = new Vector();
1321
        tempVector.add(0, key);
1322
        tempVector.add(1, value);
1323
        parentidList.add(tempVector);
1324
        return;
1325
    }
1326

    
1327
    /*
1328
     * A method to get value in Vector given a key
1329
     */
1330
    private ReturnFieldValue getArrayValue(Vector parentidList, String key)
1331
    {
1332

    
1333
        Vector tempVector = null;
1334

    
1335
        for (int count = 0; count < parentidList.size(); count++) {
1336
            tempVector = (Vector) parentidList.get(count);
1337

    
1338
            if (key.compareTo((String) tempVector.get(0)) == 0) { return (ReturnFieldValue) tempVector
1339
                    .get(1); }
1340
        }
1341
        return null;
1342
    }
1343

    
1344
    /*
1345
     * A method to get enumeration of all values in Vector
1346
     */
1347
    private Vector getElements(Vector parentidList)
1348
    {
1349
        Vector enumVector = new Vector();
1350
        Vector tempVector = null;
1351

    
1352
        for (int count = 0; count < parentidList.size(); count++) {
1353
            tempVector = (Vector) parentidList.get(count);
1354

    
1355
            enumVector.add(tempVector.get(1));
1356
        }
1357
        return enumVector;
1358
    }
1359

    
1360
    /*
1361
     * A method to return search result after running a query which return
1362
     * field have attribue
1363
     */
1364
    private ResultDocumentSet getAttributeValueForReturn(QuerySpecification squery,
1365
            ResultDocumentSet docInformationList, String docList, boolean useXMLIndex)
1366
    {
1367
        StringBuffer XML = null;
1368
        String sql = null;
1369
        DBConnection dbconn = null;
1370
        PreparedStatement pstmt = null;
1371
        ResultSet rs = null;
1372
        int serialNumber = -1;
1373
        boolean tableHasRows = false;
1374

    
1375
        //check the parameter
1376
        if (squery == null || docList == null || docList.length() < 0) { return docInformationList; }
1377

    
1378
        // if has attribute as return field
1379
        if (squery.containsAttributeReturnField()) {
1380
            sql = squery.printAttributeQuery(docList, useXMLIndex);
1381
            try {
1382
                dbconn = DBConnectionPool
1383
                        .getDBConnection("DBQuery.getAttributeValue");
1384
                serialNumber = dbconn.getCheckOutSerialNumber();
1385
                pstmt = dbconn.prepareStatement(sql);
1386
                pstmt.execute();
1387
                rs = pstmt.getResultSet();
1388
                tableHasRows = rs.next();
1389
                while (tableHasRows) {
1390
                    String docid = rs.getString(1).trim();
1391
                    String fieldname = rs.getString(2);
1392
                    String fielddata = rs.getString(3);
1393
                    String attirbuteName = rs.getString(4);
1394
                    XML = new StringBuffer();
1395

    
1396
                    XML.append("<param name=\"");
1397
                    XML.append(fieldname);
1398
                    XML.append("/");
1399
                    XML.append(QuerySpecification.ATTRIBUTESYMBOL);
1400
                    XML.append(attirbuteName);
1401
                    XML.append("\">");
1402
                    XML.append(fielddata);
1403
                    XML.append("</param>");
1404
                    tableHasRows = rs.next();
1405

    
1406
                    if (docInformationList.containsDocid(docid)) {
1407
                        String removedelement = (String) docInformationList
1408
                                .remove(docid);
1409
                        docInformationList.put(docid, removedelement
1410
                                + XML.toString());
1411
                    } else {
1412
                        docInformationList.put(docid, XML.toString());
1413
                    }
1414
                }//while
1415
                rs.close();
1416
                pstmt.close();
1417
            } catch (Exception se) {
1418
                logMetacat.error(
1419
                        "Error in DBQuery.getAttributeValue1: "
1420
                                + se.getMessage());
1421
            } finally {
1422
                try {
1423
                    pstmt.close();
1424
                }//try
1425
                catch (SQLException sqlE) {
1426
                    logMetacat.error(
1427
                            "Error in DBQuery.getAttributeValue2: "
1428
                                    + sqlE.getMessage());
1429
                }//catch
1430
                finally {
1431
                    DBConnectionPool.returnDBConnection(dbconn, serialNumber);
1432
                }//finally
1433
            }//finally
1434
        }//if
1435
        return docInformationList;
1436

    
1437
    }
1438

    
1439
    /*
1440
     * A method to create a query to get owner's docid list
1441
     */
1442
    private String getOwnerQuery(String owner)
1443
    {
1444
        if (owner != null) {
1445
            owner = owner.toLowerCase();
1446
        }
1447
        StringBuffer self = new StringBuffer();
1448

    
1449
        self.append("SELECT docid,docname,doctype,");
1450
        self.append("date_created, date_updated, rev ");
1451
        self.append("FROM xml_documents WHERE docid IN (");
1452
        self.append("(");
1453
        self.append("SELECT DISTINCT docid FROM xml_nodes WHERE \n");
1454
        self.append("nodedata LIKE '%%%' ");
1455
        self.append(") \n");
1456
        self.append(") ");
1457
        self.append(" AND (");
1458
        self.append(" lower(user_owner) = '" + owner + "'");
1459
        self.append(") ");
1460
        return self.toString();
1461
    }
1462

    
1463
    /**
1464
     * format a structured query as an XML document that conforms to the
1465
     * pathquery.dtd and is appropriate for submission to the DBQuery
1466
     * structured query engine
1467
     *
1468
     * @param params The list of parameters that should be included in the
1469
     *            query
1470
     */
1471
    public static String createSQuery(Hashtable params)
1472
    {
1473
        StringBuffer query = new StringBuffer();
1474
        Enumeration elements;
1475
        Enumeration keys;
1476
        String filterDoctype = null;
1477
        String casesensitive = null;
1478
        String searchmode = null;
1479
        Object nextkey;
1480
        Object nextelement;
1481
        //add the xml headers
1482
        query.append("<?xml version=\"1.0\"?>\n");
1483
        query.append("<pathquery version=\"1.2\">\n");
1484

    
1485

    
1486

    
1487
        if (params.containsKey("meta_file_id")) {
1488
            query.append("<meta_file_id>");
1489
            query.append(((String[]) params.get("meta_file_id"))[0]);
1490
            query.append("</meta_file_id>");
1491
        }
1492

    
1493
        if (params.containsKey("returndoctype")) {
1494
            String[] returnDoctypes = ((String[]) params.get("returndoctype"));
1495
            for (int i = 0; i < returnDoctypes.length; i++) {
1496
                String doctype = (String) returnDoctypes[i];
1497

    
1498
                if (!doctype.equals("any") && !doctype.equals("ANY")
1499
                        && !doctype.equals("")) {
1500
                    query.append("<returndoctype>").append(doctype);
1501
                    query.append("</returndoctype>");
1502
                }
1503
            }
1504
        }
1505

    
1506
        if (params.containsKey("filterdoctype")) {
1507
            String[] filterDoctypes = ((String[]) params.get("filterdoctype"));
1508
            for (int i = 0; i < filterDoctypes.length; i++) {
1509
                query.append("<filterdoctype>").append(filterDoctypes[i]);
1510
                query.append("</filterdoctype>");
1511
            }
1512
        }
1513

    
1514
        if (params.containsKey("returnfield")) {
1515
            String[] returnfield = ((String[]) params.get("returnfield"));
1516
            for (int i = 0; i < returnfield.length; i++) {
1517
                query.append("<returnfield>").append(returnfield[i]);
1518
                query.append("</returnfield>");
1519
            }
1520
        }
1521

    
1522
        if (params.containsKey("owner")) {
1523
            String[] owner = ((String[]) params.get("owner"));
1524
            for (int i = 0; i < owner.length; i++) {
1525
                query.append("<owner>").append(owner[i]);
1526
                query.append("</owner>");
1527
            }
1528
        }
1529

    
1530
        if (params.containsKey("site")) {
1531
            String[] site = ((String[]) params.get("site"));
1532
            for (int i = 0; i < site.length; i++) {
1533
                query.append("<site>").append(site[i]);
1534
                query.append("</site>");
1535
            }
1536
        }
1537

    
1538
        //allows the dynamic switching of boolean operators
1539
        if (params.containsKey("operator")) {
1540
            query.append("<querygroup operator=\""
1541
                    + ((String[]) params.get("operator"))[0] + "\">");
1542
        } else { //the default operator is UNION
1543
            query.append("<querygroup operator=\"UNION\">");
1544
        }
1545

    
1546
        if (params.containsKey("casesensitive")) {
1547
            casesensitive = ((String[]) params.get("casesensitive"))[0];
1548
        } else {
1549
            casesensitive = "false";
1550
        }
1551

    
1552
        if (params.containsKey("searchmode")) {
1553
            searchmode = ((String[]) params.get("searchmode"))[0];
1554
        } else {
1555
            searchmode = "contains";
1556
        }
1557

    
1558
        //anyfield is a special case because it does a
1559
        //free text search. It does not have a <pathexpr>
1560
        //tag. This allows for a free text search within the structured
1561
        //query. This is useful if the INTERSECT operator is used.
1562
        if (params.containsKey("anyfield")) {
1563
            String[] anyfield = ((String[]) params.get("anyfield"));
1564
            //allow for more than one value for anyfield
1565
            for (int i = 0; i < anyfield.length; i++) {
1566
                if (!anyfield[i].equals("")) {
1567
                    query.append("<queryterm casesensitive=\"" + casesensitive
1568
                            + "\" " + "searchmode=\"" + searchmode
1569
                            + "\"><value>" + anyfield[i]
1570
                            + "</value></queryterm>");
1571
                }
1572
            }
1573
        }
1574

    
1575
        //this while loop finds the rest of the parameters
1576
        //and attempts to query for the field specified
1577
        //by the parameter.
1578
        elements = params.elements();
1579
        keys = params.keys();
1580
        while (keys.hasMoreElements() && elements.hasMoreElements()) {
1581
            nextkey = keys.nextElement();
1582
            nextelement = elements.nextElement();
1583

    
1584
            //make sure we aren't querying for any of these
1585
            //parameters since the are already in the query
1586
            //in one form or another.
1587
            Vector ignoredParams = new Vector();
1588
            ignoredParams.add("returndoctype");
1589
            ignoredParams.add("filterdoctype");
1590
            ignoredParams.add("action");
1591
            ignoredParams.add("qformat");
1592
            ignoredParams.add("anyfield");
1593
            ignoredParams.add("returnfield");
1594
            ignoredParams.add("owner");
1595
            ignoredParams.add("site");
1596
            ignoredParams.add("operator");
1597
            ignoredParams.add("sessionid");
1598
            ignoredParams.add("pagesize");
1599
            ignoredParams.add("pagestart");
1600

    
1601
            // Also ignore parameters listed in the properties file
1602
            // so that they can be passed through to stylesheets
1603
            String paramsToIgnore = MetaCatUtil
1604
                    .getOption("query.ignored.params");
1605
            StringTokenizer st = new StringTokenizer(paramsToIgnore, ",");
1606
            while (st.hasMoreTokens()) {
1607
                ignoredParams.add(st.nextToken());
1608
            }
1609
            if (!ignoredParams.contains(nextkey.toString())) {
1610
                //allow for more than value per field name
1611
                for (int i = 0; i < ((String[]) nextelement).length; i++) {
1612
                    if (!((String[]) nextelement)[i].equals("")) {
1613
                        query.append("<queryterm casesensitive=\""
1614
                                + casesensitive + "\" " + "searchmode=\""
1615
                                + searchmode + "\">" + "<value>" +
1616
                                //add the query value
1617
                                ((String[]) nextelement)[i]
1618
                                + "</value><pathexpr>" +
1619
                                //add the path to query by
1620
                                nextkey.toString() + "</pathexpr></queryterm>");
1621
                    }
1622
                }
1623
            }
1624
        }
1625
        query.append("</querygroup></pathquery>");
1626
        //append on the end of the xml and return the result as a string
1627
        return query.toString();
1628
    }
1629

    
1630
    /**
1631
     * format a simple free-text value query as an XML document that conforms
1632
     * to the pathquery.dtd and is appropriate for submission to the DBQuery
1633
     * structured query engine
1634
     *
1635
     * @param value the text string to search for in the xml catalog
1636
     * @param doctype the type of documents to include in the result set -- use
1637
     *            "any" or "ANY" for unfiltered result sets
1638
     */
1639
    public static String createQuery(String value, String doctype)
1640
    {
1641
        StringBuffer xmlquery = new StringBuffer();
1642
        xmlquery.append("<?xml version=\"1.0\"?>\n");
1643
        xmlquery.append("<pathquery version=\"1.0\">");
1644

    
1645
        if (!doctype.equals("any") && !doctype.equals("ANY")) {
1646
            xmlquery.append("<returndoctype>");
1647
            xmlquery.append(doctype).append("</returndoctype>");
1648
        }
1649

    
1650
        xmlquery.append("<querygroup operator=\"UNION\">");
1651
        //chad added - 8/14
1652
        //the if statement allows a query to gracefully handle a null
1653
        //query. Without this if a nullpointerException is thrown.
1654
        if (!value.equals("")) {
1655
            xmlquery.append("<queryterm casesensitive=\"false\" ");
1656
            xmlquery.append("searchmode=\"contains\">");
1657
            xmlquery.append("<value>").append(value).append("</value>");
1658
            xmlquery.append("</queryterm>");
1659
        }
1660
        xmlquery.append("</querygroup>");
1661
        xmlquery.append("</pathquery>");
1662

    
1663
        return (xmlquery.toString());
1664
    }
1665

    
1666
    /**
1667
     * format a simple free-text value query as an XML document that conforms
1668
     * to the pathquery.dtd and is appropriate for submission to the DBQuery
1669
     * structured query engine
1670
     *
1671
     * @param value the text string to search for in the xml catalog
1672
     */
1673
    public static String createQuery(String value)
1674
    {
1675
        return createQuery(value, "any");
1676
    }
1677

    
1678
    /**
1679
     * Check for "READ" permission on @docid for @user and/or @group from DB
1680
     * connection
1681
     */
1682
    private boolean hasPermission(String user, String[] groups, String docid)
1683
            throws SQLException, Exception
1684
    {
1685
        // Check for READ permission on @docid for @user and/or @groups
1686
        PermissionController controller = new PermissionController(docid);
1687
        return controller.hasPermission(user, groups,
1688
                AccessControlInterface.READSTRING);
1689
    }
1690

    
1691
    /**
1692
     * Get all docIds list for a data packadge
1693
     *
1694
     * @param dataPackageDocid, the string in docId field of xml_relation table
1695
     */
1696
    private Vector getCurrentDocidListForDataPackage(String dataPackageDocid)
1697
    {
1698
        DBConnection dbConn = null;
1699
        int serialNumber = -1;
1700
        Vector docIdList = new Vector();//return value
1701
        PreparedStatement pStmt = null;
1702
        ResultSet rs = null;
1703
        String docIdInSubjectField = null;
1704
        String docIdInObjectField = null;
1705

    
1706
        // Check the parameter
1707
        if (dataPackageDocid == null || dataPackageDocid.equals("")) { return docIdList; }//if
1708

    
1709
        //the query stirng
1710
        String query = "SELECT subject, object from xml_relation where docId = ?";
1711
        try {
1712
            dbConn = DBConnectionPool
1713
                    .getDBConnection("DBQuery.getCurrentDocidListForDataPackage");
1714
            serialNumber = dbConn.getCheckOutSerialNumber();
1715
            pStmt = dbConn.prepareStatement(query);
1716
            //bind the value to query
1717
            pStmt.setString(1, dataPackageDocid);
1718

    
1719
            //excute the query
1720
            pStmt.execute();
1721
            //get the result set
1722
            rs = pStmt.getResultSet();
1723
            //process the result
1724
            while (rs.next()) {
1725
                //In order to get the whole docIds in a data packadge,
1726
                //we need to put the docIds of subject and object field in
1727
                // xml_relation
1728
                //into the return vector
1729
                docIdInSubjectField = rs.getString(1);//the result docId in
1730
                                                      // subject field
1731
                docIdInObjectField = rs.getString(2);//the result docId in
1732
                                                     // object field
1733

    
1734
                //don't put the duplicate docId into the vector
1735
                if (!docIdList.contains(docIdInSubjectField)) {
1736
                    docIdList.add(docIdInSubjectField);
1737
                }
1738

    
1739
                //don't put the duplicate docId into the vector
1740
                if (!docIdList.contains(docIdInObjectField)) {
1741
                    docIdList.add(docIdInObjectField);
1742
                }
1743
            }//while
1744
            //close the pStmt
1745
            pStmt.close();
1746
        }//try
1747
        catch (SQLException e) {
1748
            logMetacat.error("Error in getDocidListForDataPackage: "
1749
                    + e.getMessage());
1750
        }//catch
1751
        finally {
1752
            try {
1753
                pStmt.close();
1754
            }//try
1755
            catch (SQLException ee) {
1756
                logMetacat.error(
1757
                        "Error in getDocidListForDataPackage: "
1758
                                + ee.getMessage());
1759
            }//catch
1760
            finally {
1761
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1762
            }//fianlly
1763
        }//finally
1764
        return docIdList;
1765
    }//getCurrentDocidListForDataPackadge()
1766

    
1767
    /**
1768
     * Get all docIds list for a data packadge
1769
     *
1770
     * @param dataPackageDocid, the string in docId field of xml_relation table
1771
     */
1772
    private Vector getOldVersionDocidListForDataPackage(String dataPackageDocidWithRev)
1773
    {
1774

    
1775
        Vector docIdList = new Vector();//return value
1776
        Vector tripleList = null;
1777
        String xml = null;
1778

    
1779
        // Check the parameter
1780
        if (dataPackageDocidWithRev == null || dataPackageDocidWithRev.equals("")) { return docIdList; }//if
1781

    
1782
        try {
1783
            //initial a documentImpl object
1784
            DocumentImpl packageDocument = new DocumentImpl(dataPackageDocidWithRev);
1785
            //transfer to documentImpl object to string
1786
            xml = packageDocument.toString();
1787

    
1788
            //create a tripcollection object
1789
            TripleCollection tripleForPackage = new TripleCollection(
1790
                    new StringReader(xml));
1791
            //get the vetor of triples
1792
            tripleList = tripleForPackage.getCollection();
1793

    
1794
            for (int i = 0; i < tripleList.size(); i++) {
1795
                //put subject docid into docIdlist without duplicate
1796
                if (!docIdList.contains(((Triple) tripleList.elementAt(i))
1797
                        .getSubject())) {
1798
                    //put subject docid into docIdlist
1799
                    docIdList.add(((Triple) tripleList.get(i)).getSubject());
1800
                }
1801
                //put object docid into docIdlist without duplicate
1802
                if (!docIdList.contains(((Triple) tripleList.elementAt(i))
1803
                        .getObject())) {
1804
                    docIdList.add(((Triple) (tripleList.get(i))).getObject());
1805
                }
1806
            }//for
1807
        }//try
1808
        catch (Exception e) {
1809
            logMetacat.error("Error in getOldVersionAllDocumentImpl: "
1810
                    + e.getMessage());
1811
        }//catch
1812

    
1813
        // return result
1814
        return docIdList;
1815
    }//getDocidListForPackageInXMLRevisions()
1816

    
1817
    /**
1818
     * Check if the docId is a data packadge id. If the id is a data packadage
1819
     * id, it should be store in the docId fields in xml_relation table. So we
1820
     * can use a query to get the entries which the docId equals the given
1821
     * value. If the result is null. The docId is not a packadge id. Otherwise,
1822
     * it is.
1823
     *
1824
     * @param docId, the id need to be checked
1825
     */
1826
    private boolean isDataPackageId(String docId)
1827
    {
1828
        boolean result = false;
1829
        PreparedStatement pStmt = null;
1830
        ResultSet rs = null;
1831
        String query = "SELECT docId from xml_relation where docId = ?";
1832
        DBConnection dbConn = null;
1833
        int serialNumber = -1;
1834
        try {
1835
            dbConn = DBConnectionPool
1836
                    .getDBConnection("DBQuery.isDataPackageId");
1837
            serialNumber = dbConn.getCheckOutSerialNumber();
1838
            pStmt = dbConn.prepareStatement(query);
1839
            //bind the value to query
1840
            pStmt.setString(1, docId);
1841
            //execute the query
1842
            pStmt.execute();
1843
            rs = pStmt.getResultSet();
1844
            //process the result
1845
            if (rs.next()) //There are some records for the id in docId fields
1846
            {
1847
                result = true;//It is a data packadge id
1848
            }
1849
            pStmt.close();
1850
        }//try
1851
        catch (SQLException e) {
1852
            logMetacat.error("Error in isDataPackageId: "
1853
                    + e.getMessage());
1854
        } finally {
1855
            try {
1856
                pStmt.close();
1857
            }//try
1858
            catch (SQLException ee) {
1859
                logMetacat.error("Error in isDataPackageId: "
1860
                        + ee.getMessage());
1861
            }//catch
1862
            finally {
1863
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1864
            }//finally
1865
        }//finally
1866
        return result;
1867
    }//isDataPackageId()
1868

    
1869
    /**
1870
     * Check if the user has the permission to export data package
1871
     *
1872
     * @param conn, the connection
1873
     * @param docId, the id need to be checked
1874
     * @param user, the name of user
1875
     * @param groups, the user's group
1876
     */
1877
    private boolean hasPermissionToExportPackage(String docId, String user,
1878
            String[] groups) throws Exception
1879
    {
1880
        //DocumentImpl doc=new DocumentImpl(conn,docId);
1881
        return DocumentImpl.hasReadPermission(user, groups, docId);
1882
    }
1883

    
1884
    /**
1885
     * Get the current Rev for a docid in xml_documents table
1886
     *
1887
     * @param docId, the id need to get version numb If the return value is -5,
1888
     *            means no value in rev field for this docid
1889
     */
1890
    private int getCurrentRevFromXMLDoumentsTable(String docId)
1891
            throws SQLException
1892
    {
1893
        int rev = -5;
1894
        PreparedStatement pStmt = null;
1895
        ResultSet rs = null;
1896
        String query = "SELECT rev from xml_documents where docId = ?";
1897
        DBConnection dbConn = null;
1898
        int serialNumber = -1;
1899
        try {
1900
            dbConn = DBConnectionPool
1901
                    .getDBConnection("DBQuery.getCurrentRevFromXMLDocumentsTable");
1902
            serialNumber = dbConn.getCheckOutSerialNumber();
1903
            pStmt = dbConn.prepareStatement(query);
1904
            //bind the value to query
1905
            pStmt.setString(1, docId);
1906
            //execute the query
1907
            pStmt.execute();
1908
            rs = pStmt.getResultSet();
1909
            //process the result
1910
            if (rs.next()) //There are some records for rev
1911
            {
1912
                rev = rs.getInt(1);
1913
                ;//It is the version for given docid
1914
            } else {
1915
                rev = -5;
1916
            }
1917

    
1918
        }//try
1919
        catch (SQLException e) {
1920
            logMetacat.error(
1921
                    "Error in getCurrentRevFromXMLDoumentsTable: "
1922
                            + e.getMessage());
1923
            throw e;
1924
        }//catch
1925
        finally {
1926
            try {
1927
                pStmt.close();
1928
            }//try
1929
            catch (SQLException ee) {
1930
                logMetacat.error(
1931
                        "Error in getCurrentRevFromXMLDoumentsTable: "
1932
                                + ee.getMessage());
1933
            }//catch
1934
            finally {
1935
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1936
            }//finally
1937
        }//finally
1938
        return rev;
1939
    }//getCurrentRevFromXMLDoumentsTable
1940

    
1941
    /**
1942
     * put a doc into a zip output stream
1943
     *
1944
     * @param docImpl, docmentImpl object which will be sent to zip output
1945
     *            stream
1946
     * @param zipOut, zip output stream which the docImpl will be put
1947
     * @param packageZipEntry, the zip entry name for whole package
1948
     */
1949
    private void addDocToZipOutputStream(DocumentImpl docImpl,
1950
            ZipOutputStream zipOut, String packageZipEntry)
1951
            throws ClassNotFoundException, IOException, SQLException,
1952
            McdbException, Exception
1953
    {
1954
        byte[] byteString = null;
1955
        ZipEntry zEntry = null;
1956

    
1957
        byteString = docImpl.toString().getBytes();
1958
        //use docId as the zip entry's name
1959
        zEntry = new ZipEntry(packageZipEntry + "/metadata/"
1960
                + docImpl.getDocID());
1961
        zEntry.setSize(byteString.length);
1962
        zipOut.putNextEntry(zEntry);
1963
        zipOut.write(byteString, 0, byteString.length);
1964
        zipOut.closeEntry();
1965

    
1966
    }//addDocToZipOutputStream()
1967

    
1968
    /**
1969
     * Transfer a docid vetor to a documentImpl vector. The documentImpl vetor
1970
     * only inlcudes current version. If a DocumentImple object couldn't find
1971
     * for a docid, then the String of this docid was added to vetor rather
1972
     * than DocumentImple object.
1973
     *
1974
     * @param docIdList, a vetor hold a docid list for a data package. In
1975
     *            docid, there is not version number in it.
1976
     */
1977

    
1978
    private Vector getCurrentAllDocumentImpl(Vector docIdList)
1979
            throws McdbException, Exception
1980
    {
1981
        //Connection dbConn=null;
1982
        Vector documentImplList = new Vector();
1983
        int rev = 0;
1984

    
1985
        // Check the parameter
1986
        if (docIdList.isEmpty()) { return documentImplList; }//if
1987

    
1988
        //for every docid in vector
1989
        for (int i = 0; i < docIdList.size(); i++) {
1990
            try {
1991
                //get newest version for this docId
1992
                rev = getCurrentRevFromXMLDoumentsTable((String) docIdList
1993
                        .elementAt(i));
1994

    
1995
                // There is no record for this docId in xml_documents table
1996
                if (rev == -5) {
1997
                    // Rather than put DocumentImple object, put a String
1998
                    // Object(docid)
1999
                    // into the documentImplList
2000
                    documentImplList.add((String) docIdList.elementAt(i));
2001
                    // Skip other code
2002
                    continue;
2003
                }
2004

    
2005
                String docidPlusVersion = ((String) docIdList.elementAt(i))
2006
                        + MetaCatUtil.getOption("accNumSeparator") + rev;
2007

    
2008
                //create new documentImpl object
2009
                DocumentImpl documentImplObject = new DocumentImpl(
2010
                        docidPlusVersion);
2011
                //add them to vector
2012
                documentImplList.add(documentImplObject);
2013
            }//try
2014
            catch (Exception e) {
2015
                logMetacat.error("Error in getCurrentAllDocumentImpl: "
2016
                        + e.getMessage());
2017
                // continue the for loop
2018
                continue;
2019
            }
2020
        }//for
2021
        return documentImplList;
2022
    }
2023

    
2024
    /**
2025
     * Transfer a docid vetor to a documentImpl vector. If a DocumentImple
2026
     * object couldn't find for a docid, then the String of this docid was
2027
     * added to vetor rather than DocumentImple object.
2028
     *
2029
     * @param docIdList, a vetor hold a docid list for a data package. In
2030
     *            docid, t here is version number in it.
2031
     */
2032
    private Vector getOldVersionAllDocumentImpl(Vector docIdList)
2033
    {
2034
        //Connection dbConn=null;
2035
        Vector documentImplList = new Vector();
2036
        String siteCode = null;
2037
        String uniqueId = null;
2038
        int rev = 0;
2039

    
2040
        // Check the parameter
2041
        if (docIdList.isEmpty()) { return documentImplList; }//if
2042

    
2043
        //for every docid in vector
2044
        for (int i = 0; i < docIdList.size(); i++) {
2045

    
2046
            String docidPlusVersion = (String) (docIdList.elementAt(i));
2047

    
2048
            try {
2049
                //create new documentImpl object
2050
                DocumentImpl documentImplObject = new DocumentImpl(
2051
                        docidPlusVersion);
2052
                //add them to vector
2053
                documentImplList.add(documentImplObject);
2054
            }//try
2055
            catch (McdbDocNotFoundException notFoundE) {
2056
                logMetacat.error(
2057
                        "Error in DBQuery.getOldVersionAllDocument" + "Imple"
2058
                                + notFoundE.getMessage());
2059
                // Rather than add a DocumentImple object into vetor, a String
2060
                // object
2061
                // - the doicd was added to the vector
2062
                documentImplList.add(docidPlusVersion);
2063
                // Continue the for loop
2064
                continue;
2065
            }//catch
2066
            catch (Exception e) {
2067
                logMetacat.error(
2068
                        "Error in DBQuery.getOldVersionAllDocument" + "Imple"
2069
                                + e.getMessage());
2070
                // Continue the for loop
2071
                continue;
2072
            }//catch
2073

    
2074
        }//for
2075
        return documentImplList;
2076
    }//getOldVersionAllDocumentImple
2077

    
2078
    /**
2079
     * put a data file into a zip output stream
2080
     *
2081
     * @param docImpl, docmentImpl object which will be sent to zip output
2082
     *            stream
2083
     * @param zipOut, the zip output stream which the docImpl will be put
2084
     * @param packageZipEntry, the zip entry name for whole package
2085
     */
2086
    private void addDataFileToZipOutputStream(DocumentImpl docImpl,
2087
            ZipOutputStream zipOut, String packageZipEntry)
2088
            throws ClassNotFoundException, IOException, SQLException,
2089
            McdbException, Exception
2090
    {
2091
        byte[] byteString = null;
2092
        ZipEntry zEntry = null;
2093
        // this is data file; add file to zip
2094
        String filePath = MetaCatUtil.getOption("datafilepath");
2095
        if (!filePath.endsWith("/")) {
2096
            filePath += "/";
2097
        }
2098
        String fileName = filePath + docImpl.getDocID();
2099
        zEntry = new ZipEntry(packageZipEntry + "/data/" + docImpl.getDocID());
2100
        zipOut.putNextEntry(zEntry);
2101
        FileInputStream fin = null;
2102
        try {
2103
            fin = new FileInputStream(fileName);
2104
            byte[] buf = new byte[4 * 1024]; // 4K buffer
2105
            int b = fin.read(buf);
2106
            while (b != -1) {
2107
                zipOut.write(buf, 0, b);
2108
                b = fin.read(buf);
2109
            }//while
2110
            zipOut.closeEntry();
2111
        }//try
2112
        catch (IOException ioe) {
2113
            logMetacat.error("There is an exception: "
2114
                    + ioe.getMessage());
2115
        }//catch
2116
    }//addDataFileToZipOutputStream()
2117

    
2118
    /**
2119
     * create a html summary for data package and put it into zip output stream
2120
     *
2121
     * @param docImplList, the documentImpl ojbects in data package
2122
     * @param zipOut, the zip output stream which the html should be put
2123
     * @param packageZipEntry, the zip entry name for whole package
2124
     */
2125
    private void addHtmlSummaryToZipOutputStream(Vector docImplList,
2126
            ZipOutputStream zipOut, String packageZipEntry) throws Exception
2127
    {
2128
        StringBuffer htmlDoc = new StringBuffer();
2129
        ZipEntry zEntry = null;
2130
        byte[] byteString = null;
2131
        InputStream source;
2132
        DBTransform xmlToHtml;
2133

    
2134
        //create a DBTransform ojbect
2135
        xmlToHtml = new DBTransform();
2136
        //head of html
2137
        htmlDoc.append("<html><head></head><body>");
2138
        for (int i = 0; i < docImplList.size(); i++) {
2139
            // If this String object, this means it is missed data file
2140
            if ((((docImplList.elementAt(i)).getClass()).toString())
2141
                    .equals("class java.lang.String")) {
2142

    
2143
                htmlDoc.append("<a href=\"");
2144
                String dataFileid = (String) docImplList.elementAt(i);
2145
                htmlDoc.append("./data/").append(dataFileid).append("\">");
2146
                htmlDoc.append("Data File: ");
2147
                htmlDoc.append(dataFileid).append("</a><br>");
2148
                htmlDoc.append("<br><hr><br>");
2149

    
2150
            }//if
2151
            else if ((((DocumentImpl) docImplList.elementAt(i)).getDoctype())
2152
                    .compareTo("BIN") != 0) { //this is an xml file so we can
2153
                                              // transform it.
2154
                //transform each file individually then concatenate all of the
2155
                //transformations together.
2156

    
2157
                //for metadata xml title
2158
                htmlDoc.append("<h2>");
2159
                htmlDoc.append(((DocumentImpl) docImplList.elementAt(i))
2160
                        .getDocID());
2161
                //htmlDoc.append(".");
2162
                //htmlDoc.append(((DocumentImpl)docImplList.elementAt(i)).getRev());
2163
                htmlDoc.append("</h2>");
2164
                //do the actual transform
2165
                StringWriter docString = new StringWriter();
2166
                xmlToHtml.transformXMLDocument(((DocumentImpl) docImplList
2167
                        .elementAt(i)).toString(), "-//NCEAS//eml-generic//EN",
2168
                        "-//W3C//HTML//EN", "html", docString);
2169
                htmlDoc.append(docString.toString());
2170
                htmlDoc.append("<br><br><hr><br><br>");
2171
            }//if
2172
            else { //this is a data file so we should link to it in the html
2173
                htmlDoc.append("<a href=\"");
2174
                String dataFileid = ((DocumentImpl) docImplList.elementAt(i))
2175
                        .getDocID();
2176
                htmlDoc.append("./data/").append(dataFileid).append("\">");
2177
                htmlDoc.append("Data File: ");
2178
                htmlDoc.append(dataFileid).append("</a><br>");
2179
                htmlDoc.append("<br><hr><br>");
2180
            }//else
2181
        }//for
2182
        htmlDoc.append("</body></html>");
2183
        byteString = htmlDoc.toString().getBytes();
2184
        zEntry = new ZipEntry(packageZipEntry + "/metadata.html");
2185
        zEntry.setSize(byteString.length);
2186
        zipOut.putNextEntry(zEntry);
2187
        zipOut.write(byteString, 0, byteString.length);
2188
        zipOut.closeEntry();
2189
        //dbConn.close();
2190

    
2191
    }//addHtmlSummaryToZipOutputStream
2192

    
2193
    /**
2194
     * put a data packadge into a zip output stream
2195
     *
2196
     * @param docId, which the user want to put into zip output stream,it has version
2197
     * @param out, a servletoutput stream which the zip output stream will be
2198
     *            put
2199
     * @param user, the username of the user
2200
     * @param groups, the group of the user
2201
     */
2202
    public ZipOutputStream getZippedPackage(String docIdString,
2203
            ServletOutputStream out, String user, String[] groups,
2204
            String passWord) throws ClassNotFoundException, IOException,
2205
            SQLException, McdbException, NumberFormatException, Exception
2206
    {
2207
        ZipOutputStream zOut = null;
2208
        String elementDocid = null;
2209
        DocumentImpl docImpls = null;
2210
        //Connection dbConn = null;
2211
        Vector docIdList = new Vector();
2212
        Vector documentImplList = new Vector();
2213
        Vector htmlDocumentImplList = new Vector();
2214
        String packageId = null;
2215
        String rootName = "package";//the package zip entry name
2216

    
2217
        String docId = null;
2218
        int version = -5;
2219
        // Docid without revision
2220
        docId = MetaCatUtil.getDocIdFromString(docIdString);
2221
        // revision number
2222
        version = MetaCatUtil.getVersionFromString(docIdString);
2223

    
2224
        //check if the reqused docId is a data package id
2225
        if (!isDataPackageId(docId)) {
2226

    
2227
            /*
2228
             * Exception e = new Exception("The request the doc id "
2229
             * +docIdString+ " is not a data package id");
2230
             */
2231

    
2232
            //CB 1/6/03: if the requested docid is not a datapackage, we just
2233
            // zip
2234
            //up the single document and return the zip file.
2235
            if (!hasPermissionToExportPackage(docId, user, groups)) {
2236

    
2237
                Exception e = new Exception("User " + user
2238
                        + " does not have permission"
2239
                        + " to export the data package " + docIdString);
2240
                throw e;
2241
            }
2242

    
2243
            docImpls = new DocumentImpl(docIdString);
2244
            //checking if the user has the permission to read the documents
2245
            if (DocumentImpl.hasReadPermission(user, groups, docImpls
2246
                    .getDocID())) {
2247
                zOut = new ZipOutputStream(out);
2248
                //if the docImpls is metadata
2249
                if ((docImpls.getDoctype()).compareTo("BIN") != 0) {
2250
                    //add metadata into zip output stream
2251
                    addDocToZipOutputStream(docImpls, zOut, rootName);
2252
                }//if
2253
                else {
2254
                    //it is data file
2255
                    addDataFileToZipOutputStream(docImpls, zOut, rootName);
2256
                    htmlDocumentImplList.add(docImpls);
2257
                }//else
2258
            }//if
2259

    
2260
            zOut.finish(); //terminate the zip file
2261
            return zOut;
2262
        }
2263
        // Check the permission of user
2264
        else if (!hasPermissionToExportPackage(docId, user, groups)) {
2265

    
2266
            Exception e = new Exception("User " + user
2267
                    + " does not have permission"
2268
                    + " to export the data package " + docIdString);
2269
            throw e;
2270
        } else //it is a packadge id
2271
        {
2272
            //store the package id
2273
            packageId = docId;
2274
            //get current version in database
2275
            int currentVersion = getCurrentRevFromXMLDoumentsTable(packageId);
2276
            //If it is for current version (-1 means user didn't specify
2277
            // revision)
2278
            if ((version == -1) || version == currentVersion) {
2279
                //get current version number
2280
                version = currentVersion;
2281
                //get package zip entry name
2282
                //it should be docId.revsion.package
2283
                rootName = packageId + MetaCatUtil.getOption("accNumSeparator")
2284
                        + version + MetaCatUtil.getOption("accNumSeparator")
2285
                        + "package";
2286
                //get the whole id list for data packadge
2287
                docIdList = getCurrentDocidListForDataPackage(packageId);
2288
                //get the whole documentImple object
2289
                documentImplList = getCurrentAllDocumentImpl(docIdList);
2290

    
2291
            }//if
2292
            else if (version > currentVersion || version < -1) {
2293
                throw new Exception("The user specified docid: " + docId + "."
2294
                        + version + " doesn't exist");
2295
            }//else if
2296
            else //for an old version
2297
            {
2298

    
2299
                rootName = docIdString
2300
                        + MetaCatUtil.getOption("accNumSeparator") + "package";
2301
                //get the whole id list for data packadge
2302
                docIdList = getOldVersionDocidListForDataPackage(docIdString);
2303

    
2304
                //get the whole documentImple object
2305
                documentImplList = getOldVersionAllDocumentImpl(docIdList);
2306
            }//else
2307

    
2308
            // Make sure documentImplist is not empty
2309
            if (documentImplList.isEmpty()) { throw new Exception(
2310
                    "Couldn't find component for data package: " + packageId); }//if
2311

    
2312
            zOut = new ZipOutputStream(out);
2313
            //put every element into zip output stream
2314
            for (int i = 0; i < documentImplList.size(); i++) {
2315
                // if the object in the vetor is String, this means we couldn't
2316
                // find
2317
                // the document locally, we need find it remote
2318
                if ((((documentImplList.elementAt(i)).getClass()).toString())
2319
                        .equals("class java.lang.String")) {
2320
                    // Get String object from vetor
2321
                    String documentId = (String) documentImplList.elementAt(i);
2322
                    logMetacat.info("docid: " + documentId);
2323
                    // Get doicd without revision
2324
                    String docidWithoutRevision = MetaCatUtil
2325
                            .getDocIdFromString(documentId);
2326
                    logMetacat.info("docidWithoutRevsion: "
2327
                            + docidWithoutRevision);
2328
                    // Get revision
2329
                    String revision = MetaCatUtil
2330
                            .getRevisionStringFromString(documentId);
2331
                    logMetacat.info("revsion from docIdentifier: "
2332
                            + revision);
2333
                    // Zip entry string
2334
                    String zipEntryPath = rootName + "/data/";
2335
                    // Create a RemoteDocument object
2336
                    RemoteDocument remoteDoc = new RemoteDocument(
2337
                            docidWithoutRevision, revision, user, passWord,
2338
                            zipEntryPath);
2339
                    // Here we only read data file from remote metacat
2340
                    String docType = remoteDoc.getDocType();
2341
                    if (docType != null) {
2342
                        if (docType.equals("BIN")) {
2343
                            // Put remote document to zip output
2344
                            remoteDoc.readDocumentFromRemoteServerByZip(zOut);
2345
                            // Add String object to htmlDocumentImplList
2346
                            String elementInHtmlList = remoteDoc
2347
                                    .getDocIdWithoutRevsion()
2348
                                    + MetaCatUtil.getOption("accNumSeparator")
2349
                                    + remoteDoc.getRevision();
2350
                            htmlDocumentImplList.add(elementInHtmlList);
2351
                        }//if
2352
                    }//if
2353

    
2354
                }//if
2355
                else {
2356
                    //create a docmentImpls object (represent xml doc) base on
2357
                    // the docId
2358
                    docImpls = (DocumentImpl) documentImplList.elementAt(i);
2359
                    //checking if the user has the permission to read the
2360
                    // documents
2361
                    if (DocumentImpl.hasReadPermission(user, groups, docImpls
2362
                            .getDocID())) {
2363
                        //if the docImpls is metadata
2364
                        if ((docImpls.getDoctype()).compareTo("BIN") != 0) {
2365
                            //add metadata into zip output stream
2366
                            addDocToZipOutputStream(docImpls, zOut, rootName);
2367
                            //add the documentImpl into the vetor which will
2368
                            // be used in html
2369
                            htmlDocumentImplList.add(docImpls);
2370

    
2371
                        }//if
2372
                        else {
2373
                            //it is data file
2374
                            addDataFileToZipOutputStream(docImpls, zOut,
2375
                                    rootName);
2376
                            htmlDocumentImplList.add(docImpls);
2377
                        }//else
2378
                    }//if
2379
                }//else
2380
            }//for
2381

    
2382
            //add html summary file
2383
            addHtmlSummaryToZipOutputStream(htmlDocumentImplList, zOut,
2384
                    rootName);
2385
            zOut.finish(); //terminate the zip file
2386
            //dbConn.close();
2387
            return zOut;
2388
        }//else
2389
    }//getZippedPackage()
2390

    
2391
    private class ReturnFieldValue
2392
    {
2393

    
2394
        private String docid = null; //return field value for this docid
2395

    
2396
        private String fieldValue = null;
2397

    
2398
        private String xmlFieldValue = null; //return field value in xml
2399
                                             // format
2400

    
2401
        public void setDocid(String myDocid)
2402
        {
2403
            docid = myDocid;
2404
        }
2405

    
2406
        public String getDocid()
2407
        {
2408
            return docid;
2409
        }
2410

    
2411
        public void setFieldValue(String myValue)
2412
        {
2413
            fieldValue = myValue;
2414
        }
2415

    
2416
        public String getFieldValue()
2417
        {
2418
            return fieldValue;
2419
        }
2420

    
2421
        public void setXMLFieldValue(String xml)
2422
        {
2423
            xmlFieldValue = xml;
2424
        }
2425

    
2426
        public String getXMLFieldValue()
2427
        {
2428
            return xmlFieldValue;
2429
        }
2430

    
2431
    }
2432
    
2433
    /**
2434
     * a class to store one result document consisting of a docid and a document
2435
     */
2436
    private class ResultDocument
2437
    {
2438
      public String docid;
2439
      public String document;
2440
      
2441
      public ResultDocument(String docid, String document)
2442
      {
2443
        this.docid = docid;
2444
        this.document = document;
2445
      }
2446
    }
2447
    
2448
    /**
2449
     * a private class to handle a set of resultDocuments
2450
     */
2451
    private class ResultDocumentSet
2452
    {
2453
      private Vector docids;
2454
      private Vector documents;
2455
      
2456
      public ResultDocumentSet()
2457
      {
2458
        docids = new Vector();
2459
        documents = new Vector();
2460
      }
2461
      
2462
      /**
2463
       * adds a result document to the set
2464
       */
2465
      public void addResultDocument(ResultDocument rd)
2466
      {
2467
        if(rd.docid == null)
2468
          rd.docid = "";
2469
        if(rd.document == null)
2470
          rd.document = "";
2471
        
2472
        docids.addElement(rd.docid);
2473
        documents.addElement(rd.document);
2474
      }
2475
      
2476
      /**
2477
       * gets an iterator of docids
2478
       */
2479
      public Iterator getDocids()
2480
      {
2481
        return docids.iterator();
2482
      }
2483
      
2484
      /**
2485
       * gets an iterator of documents
2486
       */
2487
      public Iterator getDocuments()
2488
      {
2489
        return documents.iterator();
2490
      }
2491
      
2492
      /**
2493
       * returns the size of the set
2494
       */
2495
      public int size()
2496
      {
2497
        return docids.size();
2498
      }
2499
      
2500
      /**
2501
       * tests to see if this set contains the given docid
2502
       */
2503
      public boolean containsDocid(String docid)
2504
      {
2505
        for(int i=0; i<docids.size(); i++)
2506
        {
2507
          String docid0 = (String)docids.elementAt(i);
2508
          if(docid0.trim().equals(docid.trim()))
2509
          {
2510
            return true;
2511
          }
2512
        }
2513
        return false;
2514
      }
2515
      
2516
      /**
2517
       * removes the element with the given docid
2518
       */
2519
      public String remove(String docid)
2520
      {
2521
        for(int i=0; i<docids.size(); i++)
2522
        {
2523
          String docid0 = (String)docids.elementAt(i);
2524
          if(docid0.trim().equals(docid.trim()))
2525
          {
2526
            String returnDoc = (String)documents.elementAt(i);
2527
            documents.remove(i);
2528
            docids.remove(i);
2529
            return returnDoc;
2530
          }
2531
        }
2532
        return null;
2533
      }
2534
      
2535
      /**
2536
       * add a result document
2537
       */
2538
      public void put(ResultDocument rd)
2539
      {
2540
        addResultDocument(rd);
2541
      }
2542
      
2543
      /**
2544
       * add a result document by components
2545
       */
2546
      public void put(String docid, String document)
2547
      {
2548
        addResultDocument(new ResultDocument(docid, document));
2549
      }
2550
      
2551
      /**
2552
       * get the document part of the result document by docid
2553
       */
2554
      public Object get(String docid)
2555
      {
2556
        for(int i=0; i<docids.size(); i++)
2557
        {
2558
          String docid0 = (String)docids.elementAt(i);
2559
          if(docid0.trim().equals(docid.trim()))
2560
          {
2561
            return documents.elementAt(i);
2562
          }
2563
        }
2564
        return null;
2565
      }
2566
      
2567
      /**
2568
       * get the document part of the result document by an object
2569
       */
2570
      public Object get(Object o)
2571
      {
2572
        return get((String)o);
2573
      }
2574
      
2575
      /**
2576
       * get an entire result document by index number
2577
       */
2578
      public ResultDocument get(int index)
2579
      {
2580
        return new ResultDocument((String)docids.elementAt(index), 
2581
          (String)documents.elementAt(index));
2582
      }
2583
      
2584
      /**
2585
       * return a string representation of this object
2586
       */
2587
      public String toString()
2588
      {
2589
        String s = "";
2590
        for(int i=0; i<docids.size(); i++)
2591
        {
2592
          s += (String)docids.elementAt(i) + "\n";
2593
        }
2594
        return s;
2595
      }
2596
    }
2597
}
(21-21/66)