Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that searches a relational DB for elements and
4
 *             attributes that have free text matches a query string,
5
 *             or structured query matches to a path specified node in the
6
 *             XML hierarchy.  It returns a result set consisting of the
7
 *             document ID for each document that satisfies the query
8
 *  Copyright: 2000 Regents of the University of California and the
9
 *             National Center for Ecological Analysis and Synthesis
10
 *    Authors: Matt Jones
11
 *
12
 *   '$Author: tao $'
13
 *     '$Date: 2007-04-19 15:45:36 -0700 (Thu, 19 Apr 2007) $'
14
 * '$Revision: 3248 $'
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
          boolean tableHasRows = false;
1078
          // check if user has permission to see the return field data
1079
          /*String accessControlSQL =
1080
                 qspec.printAccessControlSQLForReturnField(doclist.toString());
1081
          pstmt = dbconn.prepareStatement(accessControlSQL);
1082
          //increase dbconnection usage count
1083
          dbconn.increaseUsageCount(1);
1084
          pstmt.execute();
1085
          rs = pstmt.getResultSet();
1086
          tableHasRows = rs.next();
1087
          while (tableHasRows)
1088
          {
1089
            long startNodeId = rs.getLong(1);
1090
            long endNodeId = rs.getLong(2);
1091
            controlPairs.put(new Long(startNodeId), new Long(endNodeId));
1092
            tableHasRows = rs.next();
1093
          }*/
1094

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

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

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

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

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

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

    
1195
     }//if has extended query
1196

    
1197
      return docListResult;
1198
    }//addReturnfield
1199

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

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

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

    
1263
    return docListResult;
1264
  }//addRelation
1265

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

    
1286

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

    
1293
        Vector tempVector = null;
1294

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

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

    
1309
        Vector tempVector = null;
1310

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

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

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

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

    
1334
        Vector tempVector = null;
1335

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

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

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

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

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

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

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

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

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

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

    
1438
    }
1439

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

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

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

    
1486

    
1487

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1967
    }//addDocToZipOutputStream()
1968

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
2192
    }//addHtmlSummaryToZipOutputStream
2193

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
2392
    private class ReturnFieldValue
2393
    {
2394

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

    
2397
        private String fieldValue = null;
2398

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

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

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

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

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

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

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

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