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
 *    Release: @release@
12
 *
13
 *   '$Author: tao $'
14
 *     '$Date: 2005-10-04 10:58:48 -0700 (Tue, 04 Oct 2005) $'
15
 * '$Revision: 2641 $'
16
 *
17
 * This program is free software; you can redistribute it and/or modify
18
 * it under the terms of the GNU General Public License as published by
19
 * the Free Software Foundation; either version 2 of the License, or
20
 * (at your option) any later version.
21
 *
22
 * This program is distributed in the hope that it will be useful,
23
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25
 * GNU General Public License for more details.
26
 *
27
 * You should have received a copy of the GNU General Public License
28
 * along with this program; if not, write to the Free Software
29
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
30
 */
31

    
32
package edu.ucsb.nceas.metacat;
33

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

    
54
import javax.servlet.ServletOutputStream;
55
import javax.servlet.http.HttpServletResponse;
56

    
57

    
58
import edu.ucsb.nceas.morpho.datapackage.Triple;
59
import edu.ucsb.nceas.morpho.datapackage.TripleCollection;
60

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

    
70
    static final int ALL = 1;
71

    
72
    static final int WRITE = 2;
73

    
74
    static final int READ = 4;
75

    
76
    //private Connection conn = null;
77
    private String parserName = null;
78

    
79
    private MetaCatUtil util = new MetaCatUtil();
80

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

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

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

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

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

    
118
                double connTime = System.currentTimeMillis();
119

    
120
                // Execute the query
121
                DBQuery queryobj = new DBQuery(MetaCatUtil
122
                        .getOption("saxparser"));
123
                FileReader xml = new FileReader(new File(xmlfile));
124
                Hashtable nodelist = null;
125
                //nodelist = queryobj.findDocuments(xml, null, null, useXMLIndex);
126

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

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

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

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

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

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

    
194

    
195
  /**
196
   * Method put the search result set into out printerwriter
197
   * @param resoponse the return response
198
   * @param out the output printer
199
   * @param params the paratermer hashtable
200
   * @param user the user name (it maybe different to the one in param)
201
   * @param groups the group array
202
   * @param sessionid  the sessionid
203
   */
204
  public void findDocuments(HttpServletResponse response,
205
                                       PrintWriter out, Hashtable params,
206
                                       String user, String[] groups,
207
                                       String sessionid)
208
  {
209
    boolean useXMLIndex = (new Boolean(MetaCatUtil.getOption("usexmlindex")))
210
               .booleanValue();
211
    findDocuments(response, out, params, user, groups, sessionid, useXMLIndex);
212

    
213
  }
214

    
215

    
216
    /**
217
     * Method put the search result set into out printerwriter
218
     * @param resoponse the return response
219
     * @param out the output printer
220
     * @param params the paratermer hashtable
221
     * @param user the user name (it maybe different to the one in param)
222
     * @param groups the group array
223
     * @param sessionid  the sessionid
224
     */
225
    public void findDocuments(HttpServletResponse response,
226
                                         PrintWriter out, Hashtable params,
227
                                         String user, String[] groups,
228
                                         String sessionid, boolean useXMLIndex)
229
    {
230
      // get query and qformat
231
      String xmlquery = ((String[])params.get("query"))[0];
232

    
233
      MetaCatUtil.debugMessage("xmlquery: " + xmlquery, 30);
234
      String qformat = ((String[])params.get("qformat"))[0];
235
      MetaCatUtil.debugMessage("qformat: " + qformat, 30);
236
      // Get the XML query and covert it into a SQL statment
237
      QuerySpecification qspec = null;
238
      if ( xmlquery != null)
239
      {
240
         xmlquery = transformQuery(xmlquery);
241
         try
242
         {
243
           qspec = new QuerySpecification(xmlquery,
244
                                          parserName,
245
                                          MetaCatUtil.getOption("accNumSeparator"));
246
         }
247
         catch (Exception ee)
248
         {
249
           MetaCatUtil.debugMessage("erro to generate QuerySpecification object"
250
                                    +" in DBQuery.findDocuments"
251
                                    + ee.getMessage(), 30);
252
         }
253
      }
254

    
255

    
256

    
257
      if (qformat != null && qformat.equals(MetaCatServlet.XMLFORMAT))
258
      {
259
        //xml format
260
        response.setContentType("text/xml");
261
        createResultDocument(xmlquery, qspec, out, user, groups, useXMLIndex);
262
      }//if
263
      else
264
      {
265
        //knb format, in this case we will get whole result and sent it out
266
        response.setContentType("text/html");
267
        PrintWriter nonout = null;
268
        StringBuffer xml = createResultDocument(xmlquery, qspec, nonout, user,
269
                                                groups, useXMLIndex);
270
        //transfer the xml to html
271
        try
272
        {
273

    
274
         DBTransform trans = new DBTransform();
275
         response.setContentType("text/html");
276
         trans.transformXMLDocument(xml.toString(), "-//NCEAS//resultset//EN",
277
                                 "-//W3C//HTML//EN", qformat, out, params,
278
                                 sessionid);
279

    
280
        }
281
        catch(Exception e)
282
        {
283
         MetaCatUtil.debugMessage("Error in MetaCatServlet.transformResultset:"
284
                                +e.getMessage(), 30);
285
         }
286

    
287
      }//else
288

    
289
    }
290

    
291
  /*
292
   * Transforms a hashtable of documents to an xml or html result and sent
293
   * the content to outputstream. Keep going untill hastable is empty. stop it.
294
   * add the QuerySpecification as parameter is for ecogrid. But it is duplicate
295
   * to xmlquery String
296
   */
297
  public StringBuffer createResultDocument(String xmlquery,
298
                                            QuerySpecification qspec,
299
                                            PrintWriter out,
300
                                            String user, String[] groups,
301
                                            boolean useXMLIndex)
302
  {
303
    DBConnection dbconn = null;
304
    int serialNumber = -1;
305
    StringBuffer resultset = new StringBuffer();
306
    resultset.append("<?xml version=\"1.0\"?>\n");
307
    resultset.append("<resultset>\n");
308
    resultset.append("  <query>" + xmlquery + "</query>");
309
    // sent query part out
310
    if (out != null)
311
    {
312
      out.println(resultset.toString());
313
    }
314
    if (qspec != null)
315
    {
316
      try
317
      {
318

    
319
        //checkout the dbconnection
320
        dbconn = DBConnectionPool.getDBConnection("DBQuery.findDocuments");
321
        serialNumber = dbconn.getCheckOutSerialNumber();
322

    
323
        //print out the search result
324
        // search the doc list
325
        resultset = findResultDoclist(qspec, resultset, out, user, groups,
326
                                      dbconn, useXMLIndex);
327

    
328
      } //try
329
      catch (IOException ioe)
330
      {
331
        MetaCatUtil.debugMessage("IO error in DBQuery.findDocuments:", 30);
332
        MetaCatUtil.debugMessage(ioe.getMessage(), 30);
333

    
334
      }
335
      catch (SQLException e)
336
      {
337
        MetaCatUtil.debugMessage("SQL Error in DBQuery.findDocuments: "
338
                                 + e.getMessage(), 30);
339
      }
340
      catch (Exception ee)
341
      {
342
        MetaCatUtil.debugMessage("Exception in DBQuery.findDocuments: "
343
                                 + ee.getMessage(), 30);
344
      }
345
      finally
346
      {
347
        DBConnectionPool.returnDBConnection(dbconn, serialNumber);
348
      } //finally
349
    }//if
350
    String closeRestultset = "</resultset>";
351
    resultset.append(closeRestultset);
352
    if (out != null)
353
    {
354
      out.println(closeRestultset);
355
    }
356

    
357
    return resultset;
358
  }//createResultDocuments
359

    
360

    
361

    
362
    /*
363
     * Find the doc list which match the query
364
     */
365
    private StringBuffer findResultDoclist(QuerySpecification qspec,
366
                                      StringBuffer resultsetBuffer,
367
                                      PrintWriter out,
368
                                      String user, String[]groups,
369
                                      DBConnection dbconn, boolean useXMLIndex )
370
                                      throws Exception
371
    {
372
      int offset = 1;
373
      // this is a hack for offset
374
      if (out == null)
375
      {
376
        // for html page, we put everything into one page
377
        offset =
378
            (new Integer(MetaCatUtil.getOption("web_resultsetsize"))).intValue();
379
      }
380
      else
381
      {
382
          offset =
383
              (new Integer(MetaCatUtil.getOption("app_resultsetsize"))).intValue();
384
      }
385

    
386
      int count = 0;
387
      int index = 0;
388
      Hashtable docListResult = new Hashtable();
389
      PreparedStatement pstmt = null;
390
      String docid = null;
391
      String docname = null;
392
      String doctype = null;
393
      String createDate = null;
394
      String updateDate = null;
395
      StringBuffer document = null;
396
      int rev = 0;
397
      String query = qspec.printSQL(useXMLIndex);
398
      String ownerQuery = getOwnerQuery(user);
399
      MetaCatUtil.debugMessage("query: " + query, 30);
400
      //MetaCatUtil.debugMessage("query: "+ownerQuery, 30);
401
      // if query is not the owner query, we need to check the permission
402
      // otherwise we don't need (owner has all permission by default)
403
      if (!query.equals(ownerQuery))
404
      {
405
        // set user name and group
406
        qspec.setUserName(user);
407
        qspec.setGroup(groups);
408
        // Get access query
409
        String accessQuery = qspec.getAccessQuery();
410
        if(!query.endsWith("WHERE")){
411
            query = query + accessQuery;
412
        } else {
413
            query = query + accessQuery.substring(4, accessQuery.length());
414
        }
415
        MetaCatUtil.debugMessage(" final query: " + query, 30);
416
      }
417

    
418
      double startTime = System.currentTimeMillis() / 1000;
419
      pstmt = dbconn.prepareStatement(query);
420

    
421
      // Execute the SQL query using the JDBC connection
422
      pstmt.execute();
423
      ResultSet rs = pstmt.getResultSet();
424
      double queryExecuteTime = System.currentTimeMillis() / 1000;
425
      MetaCatUtil.debugMessage("Time for execute query: "
426
                    + (queryExecuteTime - startTime), 30);
427
      boolean tableHasRows = rs.next();
428
      while (tableHasRows)
429
      {
430
        docid = rs.getString(1).trim();
431
        docname = rs.getString(2);
432
        doctype = rs.getString(3);
433
        createDate = rs.getString(4);
434
        updateDate = rs.getString(5);
435
        rev = rs.getInt(6);
436

    
437
        // if there are returndocs to match, backtracking can be performed
438
        // otherwise, just return the document that was hit
439
        Vector returndocVec = qspec.getReturnDocList();
440
         if (returndocVec.size() != 0 && !returndocVec.contains(doctype)
441
                        && !qspec.isPercentageSearch())
442
        {
443
           MetaCatUtil.debugMessage("Back tracing now...", 20);
444
           String sep = MetaCatUtil.getOption("accNumSeparator");
445
           StringBuffer btBuf = new StringBuffer();
446
           btBuf.append("select docid from xml_relation where ");
447

    
448
           //build the doctype list for the backtracking sql statement
449
           btBuf.append("packagetype in (");
450
           for (int i = 0; i < returndocVec.size(); i++)
451
           {
452
             btBuf.append("'").append((String) returndocVec.get(i)).append("'");
453
             if (i != (returndocVec.size() - 1))
454
             {
455
                btBuf.append(", ");
456
              }
457
            }
458
            btBuf.append(") ");
459
            btBuf.append("and (subject like '");
460
            btBuf.append(docid).append("'");
461
            btBuf.append("or object like '");
462
            btBuf.append(docid).append("')");
463

    
464
            PreparedStatement npstmt = dbconn.prepareStatement(btBuf.toString());
465
            //should incease usage count
466
            dbconn.increaseUsageCount(1);
467
            npstmt.execute();
468
            ResultSet btrs = npstmt.getResultSet();
469
            boolean hasBtRows = btrs.next();
470
            while (hasBtRows)
471
            {
472
               //there was a backtrackable document found
473
               DocumentImpl xmldoc = null;
474
               String packageDocid = btrs.getString(1);
475
               MetaCatUtil.debugMessage("Getting document for docid: "
476
                                         + packageDocid, 40);
477
                try
478
                {
479
                    //  THIS CONSTRUCTOR BUILDS THE WHOLE XML doc not
480
                    // needed here
481
                    // xmldoc = new DocumentImpl(dbconn, packageDocid);
482
                    //  thus use the following to get the doc info only
483
                    //  xmldoc = new DocumentImpl(dbconn);
484
                    String accNumber = packageDocid + MetaCatUtil.getOption("accNumSeparator") +
485
                    DBUtil.getLatestRevisionInDocumentTable(packageDocid);
486
                    xmldoc = new DocumentImpl(accNumber, false);
487
                    if (xmldoc == null)
488
                    {
489
                       MetaCatUtil.debugMessage("Document was null for: "
490
                                                + packageDocid, 50);
491
                    }
492
                }
493
                catch (Exception e)
494
                {
495
                    System.out.println("Error getting document in "
496
                                       + "DBQuery.findDocuments: "
497
                                       + e.getMessage());
498
                }
499

    
500
                String docid_org = xmldoc.getDocID();
501
                if (docid_org == null)
502
                {
503
                   MetaCatUtil.debugMessage("Docid_org was null.", 40);
504
                   //continue;
505
                }
506
                docid = docid_org.trim();
507
                docname = xmldoc.getDocname();
508
                doctype = xmldoc.getDoctype();
509
                createDate = xmldoc.getCreateDate();
510
                updateDate = xmldoc.getUpdateDate();
511
                rev = xmldoc.getRev();
512
                document = new StringBuffer();
513

    
514
                String completeDocid = docid
515
                                + MetaCatUtil.getOption("accNumSeparator");
516
                completeDocid += rev;
517
                document.append("<docid>").append(completeDocid);
518
                document.append("</docid>");
519
                if (docname != null)
520
                {
521
                  document.append("<docname>" + docname + "</docname>");
522
                }
523
                if (doctype != null)
524
                {
525
                  document.append("<doctype>" + doctype + "</doctype>");
526
                }
527
                if (createDate != null)
528
                {
529
                 document.append("<createdate>" + createDate + "</createdate>");
530
                }
531
                if (updateDate != null)
532
                {
533
                  document.append("<updatedate>" + updateDate+ "</updatedate>");
534
                }
535
                // Store the document id and the root node id
536
                docListResult.put(docid, (String) document.toString());
537
                count++;
538

    
539

    
540
                // Get the next package document linked to our hit
541
                hasBtRows = btrs.next();
542
              }//while
543
              npstmt.close();
544
              btrs.close();
545
        }
546
        else if (returndocVec.size() == 0 || returndocVec.contains(doctype))
547
        {
548

    
549
           document = new StringBuffer();
550

    
551
           String completeDocid = docid
552
                            + MetaCatUtil.getOption("accNumSeparator");
553
           completeDocid += rev;
554
           document.append("<docid>").append(completeDocid).append("</docid>");
555
           if (docname != null)
556
           {
557
               document.append("<docname>" + docname + "</docname>");
558
            }
559
            if (doctype != null)
560
            {
561
               document.append("<doctype>" + doctype + "</doctype>");
562
            }
563
            if (createDate != null)
564
            {
565
                document.append("<createdate>" + createDate + "</createdate>");
566
             }
567
             if (updateDate != null)
568
             {
569
               document.append("<updatedate>" + updateDate + "</updatedate>");
570
             }
571
              // Store the document id and the root node id
572
              docListResult.put(docid, (String) document.toString());
573
              count++;
574

    
575

    
576
        }//else
577
        // when doclist reached the offset number, send out doc list and empty
578
        // the hash table
579
        if (count == offset)
580
        {
581
          //reset count
582
          count = 0;
583
          handleSubsetResult(qspec,resultsetBuffer, out, docListResult,
584
                              user, groups,dbconn, useXMLIndex);
585
          // reset docListResult
586
          docListResult = new Hashtable();
587

    
588
        }
589
       // Advance to the next record in the cursor
590
       tableHasRows = rs.next();
591
     }//while
592
     rs.close();
593
     pstmt.close();
594
     //if docListResult is not empty, it need to be sent.
595
     if (!docListResult.isEmpty())
596
     {
597
       handleSubsetResult(qspec,resultsetBuffer, out, docListResult,
598
                              user, groups,dbconn, useXMLIndex);
599
     }
600
     double docListTime = System.currentTimeMillis() / 1000;
601
     MetaCatUtil.debugMessage("prepare docid list time: "
602
                    + (docListTime - queryExecuteTime), 30);
603

    
604
     return resultsetBuffer;
605
    }//findReturnDoclist
606

    
607

    
608
    /*
609
     * Send completed search hashtable(part of reulst)to output stream
610
     * and buffer into a buffer stream
611
     */
612
    private StringBuffer handleSubsetResult(QuerySpecification qspec,
613
                                           StringBuffer resultset,
614
                                           PrintWriter out, Hashtable partOfDoclist,
615
                                           String user, String[]groups,
616
                                       DBConnection dbconn, boolean useXMLIndex)
617
                                       throws Exception
618
   {
619

    
620
     // check if there is a record in xml_returnfield
621
     // and get the returnfield_id and usage count
622
     int usage_count = getXmlReturnfieldsTableId(qspec, dbconn);
623
     boolean enterRecords = false;
624

    
625
     // get value of xml_returnfield_count
626
     int count = (new Integer(MetaCatUtil
627
                            .getOption("xml_returnfield_count")))
628
                            .intValue();
629

    
630
     // set enterRecords to true if usage_count is more than the offset
631
     // specified in metacat.properties
632
     if(usage_count > count){
633
         enterRecords = true;
634
     }
635

    
636
     if(returnfield_id < 0){
637
         MetaCatUtil.debugMessage("Error in getting returnfield id from"
638
                                  + "xml_returnfield table", 20);
639
	enterRecords = false;
640
     }
641

    
642
     // get the hashtable containing the docids that already in the
643
     // xml_queryresult table
644
     MetaCatUtil.debugMessage("size of partOfDoclist before"
645
                             + " docidsInQueryresultTable(): "
646
                             + partOfDoclist.size() , 50);
647
     Hashtable queryresultDocList = docidsInQueryresultTable(returnfield_id,
648
                                                        partOfDoclist, dbconn);
649

    
650
     // remove the keys in queryresultDocList from partOfDoclist
651
     Enumeration _keys = queryresultDocList.keys();
652
     while (_keys.hasMoreElements()){
653
         partOfDoclist.remove(_keys.nextElement());
654
     }
655

    
656
     // backup the keys-elements in partOfDoclist to check later
657
     // if the doc entry is indexed yet
658
     Hashtable partOfDoclistBackup = new Hashtable();
659
     _keys = partOfDoclist.keys();
660
     while (_keys.hasMoreElements()){
661
	 Object key = _keys.nextElement();
662
         partOfDoclistBackup.put(key, partOfDoclist.get(key));
663
     }
664

    
665
     MetaCatUtil.debugMessage("size of partOfDoclist after"
666
                             + " docidsInQueryresultTable(): "
667
                             + partOfDoclist.size() , 50);
668

    
669
     //add return fields for the documents in partOfDoclist
670
     partOfDoclist = addReturnfield(partOfDoclist, qspec, user, groups,
671
                                        dbconn, useXMLIndex );
672
     //add relationship part part docid list for the documents in partOfDocList
673
     partOfDoclist = addRelationship(partOfDoclist, qspec, dbconn, useXMLIndex);
674

    
675

    
676
     Enumeration keys = partOfDoclist.keys();
677
     String key = null;
678
     String element = null;
679
     String query = null;
680
     int offset = (new Integer(MetaCatUtil
681
                               .getOption("queryresult_string_length")))
682
                               .intValue();
683
     while (keys.hasMoreElements())
684
     {
685
         key = (String) keys.nextElement();
686
         element = (String)partOfDoclist.get(key);
687

    
688
	 // check if the enterRecords is true, elements is not null, element's
689
         // length is less than the limit of table column and if the document
690
         // has been indexed already
691
         if(enterRecords && element != null
692
		&& element.length() < offset
693
		&& element.compareTo((String) partOfDoclistBackup.get(key)) != 0){
694
             query = "INSERT INTO xml_queryresult (returnfield_id, docid, "
695
                 + "queryresult_string) VALUES (?, ?, ?)";
696

    
697
             PreparedStatement pstmt = null;
698
             pstmt = dbconn.prepareStatement(query);
699
             pstmt.setInt(1, returnfield_id);
700
             pstmt.setString(2, key);
701
             pstmt.setString(3, element);
702

    
703
             dbconn.increaseUsageCount(1);
704
             pstmt.execute();
705
             pstmt.close();
706
         }
707

    
708
         // A string with element
709
         String xmlElement = "  <document>" + element + "</document>";
710

    
711
         //send single element to output
712
         if (out != null)
713
         {
714
             out.println(xmlElement);
715
         }
716
         resultset.append(xmlElement);
717
     }//while
718

    
719

    
720
     keys = queryresultDocList.keys();
721
     while (keys.hasMoreElements())
722
     {
723
         key = (String) keys.nextElement();
724
         element = (String)queryresultDocList.get(key);
725
         // A string with element
726
         String xmlElement = "  <document>" + element + "</document>";
727
         //send single element to output
728
         if (out != null)
729
         {
730
             out.println(xmlElement);
731
         }
732
         resultset.append(xmlElement);
733
     }//while
734

    
735
     return resultset;
736
 }
737

    
738
   /**
739
    * Get the docids already in xml_queryresult table and corresponding
740
    * queryresultstring as a hashtable
741
    */
742
   private Hashtable docidsInQueryresultTable(int returnfield_id,
743
                                              Hashtable partOfDoclist,
744
                                              DBConnection dbconn){
745

    
746
         Hashtable returnValue = new Hashtable();
747
         PreparedStatement pstmt = null;
748
         ResultSet rs = null;
749

    
750
         // get partOfDoclist as string for the query
751
         Enumeration keylist = partOfDoclist.keys();
752
         StringBuffer doclist = new StringBuffer();
753
         while (keylist.hasMoreElements())
754
         {
755
             doclist.append("'");
756
             doclist.append((String) keylist.nextElement());
757
             doclist.append("',");
758
         }//while
759

    
760

    
761
         if (doclist.length() > 0)
762
         {
763
             doclist.deleteCharAt(doclist.length() - 1); //remove the last comma
764

    
765
             // the query to find out docids from xml_queryresult
766
             String query = "select docid, queryresult_string from "
767
                          + "xml_queryresult where returnfield_id = " +
768
                          returnfield_id +" and docid in ("+ doclist + ")";
769
             MetaCatUtil.debugMessage("Query to get docids from xml_queryresult:"
770
                                      + query, 50);
771

    
772
             try {
773
                 // prepare and execute the query
774
                 pstmt = dbconn.prepareStatement(query);
775
                 dbconn.increaseUsageCount(1);
776
                 pstmt.execute();
777
                 rs = pstmt.getResultSet();
778
                 boolean tableHasRows = rs.next();
779
                 while (tableHasRows) {
780
                     // store the returned results in the returnValue hashtable
781
                     String key = rs.getString(1);
782
                     String element = rs.getString(2);
783

    
784
                     if(element != null){
785
                         returnValue.put(key, element);
786
                     } else {
787
                         MetaCatUtil.debugMessage("Null elment found ("
788
                         + "DBQuery.docidsInQueryresultTable)", 50);
789
                     }
790
                     tableHasRows = rs.next();
791
                 }
792
                 rs.close();
793
                 pstmt.close();
794
             } catch (Exception e){
795
                 MetaCatUtil.debugMessage("Error getting docids from "
796
                                          + "queryresult in "
797
                                          + "DBQuery.docidsInQueryresultTable: "
798
                                          + e.getMessage(), 20);
799
              }
800
         }
801
         return returnValue;
802
     }
803

    
804

    
805
   /**
806
    * Method to get id from xml_returnfield table
807
    * for a given query specification
808
    */
809
   private int returnfield_id;
810
   private int getXmlReturnfieldsTableId(QuerySpecification qspec,
811
                                           DBConnection dbconn){
812
       int id = -1;
813
       int count = 1;
814
       PreparedStatement pstmt = null;
815
       ResultSet rs = null;
816
       String returnfield = qspec.getSortedReturnFieldString();
817

    
818
       // query for finding the id from xml_returnfield
819
       String query = "SELECT returnfield_id, usage_count FROM xml_returnfield "
820
            + "WHERE returnfield_string LIKE ?";
821
       MetaCatUtil.debugMessage("ReturnField Query:" + query, 50);
822

    
823
       try {
824
           // prepare and run the query
825
           pstmt = dbconn.prepareStatement(query);
826
           pstmt.setString(1,returnfield);
827
           dbconn.increaseUsageCount(1);
828
           pstmt.execute();
829
           rs = pstmt.getResultSet();
830
           boolean tableHasRows = rs.next();
831

    
832
           // if record found then increase the usage count
833
           // else insert a new record and get the id of the new record
834
           if(tableHasRows){
835
               // get the id
836
               id = rs.getInt(1);
837
               count = rs.getInt(2) + 1;
838
               rs.close();
839
               pstmt.close();
840

    
841
               // increase the usage count
842
               query = "UPDATE xml_returnfield SET usage_count ='" + count
843
                   + "' WHERE returnfield_id ='"+ id +"'";
844
               MetaCatUtil.debugMessage("ReturnField Table Update:"+ query, 50);
845

    
846
               pstmt = dbconn.prepareStatement(query);
847
               dbconn.increaseUsageCount(1);
848
               pstmt.execute();
849
               pstmt.close();
850

    
851
           } else {
852
               rs.close();
853
               pstmt.close();
854

    
855
               // insert a new record
856
               query = "INSERT INTO xml_returnfield (returnfield_string, usage_count)"
857
                   + "VALUES (?, '1')";
858
               MetaCatUtil.debugMessage("ReturnField Table Insert:"+ query, 50);
859
               pstmt = dbconn.prepareStatement(query);
860
               pstmt.setString(1, returnfield);
861
               dbconn.increaseUsageCount(1);
862
               pstmt.execute();
863
               pstmt.close();
864

    
865
               // get the id of the new record
866
               query = "SELECT returnfield_id FROM xml_returnfield "
867
                   + "WHERE returnfield_string LIKE ?";
868
               MetaCatUtil.debugMessage("ReturnField query after Insert:" + query, 50);
869
               pstmt = dbconn.prepareStatement(query);
870
               pstmt.setString(1, returnfield);
871

    
872
               dbconn.increaseUsageCount(1);
873
               pstmt.execute();
874
               rs = pstmt.getResultSet();
875
               if(rs.next()){
876
                   id = rs.getInt(1);
877
               } else {
878
                   id = -1;
879
               }
880
               rs.close();
881
               pstmt.close();
882
           }
883

    
884
       } catch (Exception e){
885
           MetaCatUtil.debugMessage("Error getting id from xml_returnfield in "
886
                                     + "DBQuery.getXmlReturnfieldsTableId: "
887
                                     + e.getMessage(), 20);
888
           id = -1;
889
       }
890

    
891
       returnfield_id = id;
892
       return count;
893
   }
894

    
895

    
896
    /*
897
     * A method to add return field to return doclist hash table
898
     */
899
    private Hashtable addReturnfield(Hashtable docListResult,
900
                                      QuerySpecification qspec,
901
                                      String user, String[]groups,
902
                                      DBConnection dbconn, boolean useXMLIndex )
903
                                      throws Exception
904
    {
905
      PreparedStatement pstmt = null;
906
      ResultSet rs = null;
907
      String docid = null;
908
      String fieldname = null;
909
      String fielddata = null;
910
      String relation = null;
911

    
912
      if (qspec.containsExtendedSQL())
913
      {
914
        qspec.setUserName(user);
915
        qspec.setGroup(groups);
916
        Vector extendedFields = new Vector(qspec.getReturnFieldList());
917
        Vector results = new Vector();
918
        Enumeration keylist = docListResult.keys();
919
        StringBuffer doclist = new StringBuffer();
920
        Vector parentidList = new Vector();
921
        Hashtable returnFieldValue = new Hashtable();
922
        while (keylist.hasMoreElements())
923
        {
924
          doclist.append("'");
925
          doclist.append((String) keylist.nextElement());
926
          doclist.append("',");
927
        }
928
        if (doclist.length() > 0)
929
        {
930
          Hashtable controlPairs = new Hashtable();
931
          double extendedQueryStart = System.currentTimeMillis() / 1000;
932
          doclist.deleteCharAt(doclist.length() - 1); //remove the last comma
933
          // check if user has permission to see the return field data
934
          String accessControlSQL =
935
                 qspec.printAccessControlSQLForReturnField(doclist.toString());
936
          pstmt = dbconn.prepareStatement(accessControlSQL);
937
          //increase dbconnection usage count
938
          dbconn.increaseUsageCount(1);
939
          pstmt.execute();
940
          rs = pstmt.getResultSet();
941
          boolean tableHasRows = rs.next();
942
          while (tableHasRows)
943
          {
944
            long startNodeId = rs.getLong(1);
945
            long endNodeId = rs.getLong(2);
946
            controlPairs.put(new Long(startNodeId), new Long(endNodeId));
947
            tableHasRows = rs.next();
948
          }
949

    
950
           double extendedAccessQueryEnd = System.currentTimeMillis() / 1000;
951
           MetaCatUtil.debugMessage( "Time for execute access extended query: "
952
                          + (extendedAccessQueryEnd - extendedQueryStart),30);
953

    
954
           String extendedQuery =
955
               qspec.printExtendedSQL(doclist.toString(), controlPairs, useXMLIndex);
956
           MetaCatUtil.debugMessage("Extended query: " + extendedQuery, 30);
957

    
958
           if(extendedQuery != null){
959
               pstmt = dbconn.prepareStatement(extendedQuery);
960
               //increase dbconnection usage count
961
               dbconn.increaseUsageCount(1);
962
               pstmt.execute();
963
               rs = pstmt.getResultSet();
964
               double extendedQueryEnd = System.currentTimeMillis() / 1000;
965
               MetaCatUtil.debugMessage(
966
                   "Time for execute extended query: "
967
                   + (extendedQueryEnd - extendedQueryStart), 30);
968
               tableHasRows = rs.next();
969
               while (tableHasRows) {
970
                   ReturnFieldValue returnValue = new ReturnFieldValue();
971
                   docid = rs.getString(1).trim();
972
                   fieldname = rs.getString(2);
973
                   fielddata = rs.getString(3);
974
                   fielddata = MetaCatUtil.normalize(fielddata);
975
                   String parentId = rs.getString(4);
976
                   StringBuffer value = new StringBuffer();
977

    
978
                   // if xml_index is used, there would be just one record per nodeid
979
                   // as xml_index just keeps one entry for each path
980
                   if (useXMLIndex || !containsKey(parentidList, parentId)) {
981
                       // don't need to merger nodedata
982
                       value.append("<param name=\"");
983
                       value.append(fieldname);
984
                       value.append("\">");
985
                       value.append(fielddata);
986
                       value.append("</param>");
987
                       //set returnvalue
988
                       returnValue.setDocid(docid);
989
                       returnValue.setFieldValue(fielddata);
990
                       returnValue.setXMLFieldValue(value.toString());
991
                       // Store it in hastable
992
                       putInArray(parentidList, parentId, returnValue);
993
                   }
994
                   else {
995
                       // need to merge nodedata if they have same parent id and
996
                       // node type is text
997
                       fielddata = (String) ( (ReturnFieldValue)
998
                                             getArrayValue(
999
                           parentidList, parentId)).getFieldValue()
1000
                           + fielddata;
1001
                       value.append("<param name=\"");
1002
                       value.append(fieldname);
1003
                       value.append("\">");
1004
                       value.append(fielddata);
1005
                       value.append("</param>");
1006
                       returnValue.setDocid(docid);
1007
                       returnValue.setFieldValue(fielddata);
1008
                       returnValue.setXMLFieldValue(value.toString());
1009
                       // remove the old return value from paretnidList
1010
                       parentidList.remove(parentId);
1011
                       // store the new return value in parentidlit
1012
                       putInArray(parentidList, parentId, returnValue);
1013
                   }
1014
                   tableHasRows = rs.next();
1015
               } //while
1016
               rs.close();
1017
               pstmt.close();
1018

    
1019
               // put the merger node data info into doclistReult
1020
               Enumeration xmlFieldValue = (getElements(parentidList)).
1021
                   elements();
1022
               while (xmlFieldValue.hasMoreElements()) {
1023
                   ReturnFieldValue object =
1024
                       (ReturnFieldValue) xmlFieldValue.nextElement();
1025
                   docid = object.getDocid();
1026
                   if (docListResult.containsKey(docid)) {
1027
                       String removedelement = (String) docListResult.
1028
                           remove(docid);
1029
                       docListResult.
1030
                           put(docid,
1031
                               removedelement + object.getXMLFieldValue());
1032
                   }
1033
                   else {
1034
                       docListResult.put(docid, object.getXMLFieldValue());
1035
                   }
1036
               } //while
1037
               double docListResultEnd = System.currentTimeMillis() / 1000;
1038
               MetaCatUtil.debugMessage(
1039
                   "Time for prepare doclistresult after"
1040
                   + " execute extended query: "
1041
                   + (docListResultEnd - extendedQueryEnd),
1042
                   30);
1043
           }
1044

    
1045
           // get attribures return
1046
           docListResult = getAttributeValueForReturn(qspec,
1047
                           docListResult, doclist.toString(), useXMLIndex);
1048
       }//if doclist lenght is great than zero
1049

    
1050
     }//if has extended query
1051

    
1052
      return docListResult;
1053
    }//addReturnfield
1054

    
1055
    /*
1056
    * A method to add relationship to return doclist hash table
1057
    */
1058
   private Hashtable addRelationship(Hashtable docListResult,
1059
                                     QuerySpecification qspec,
1060
                                     DBConnection dbconn, boolean useXMLIndex )
1061
                                     throws Exception
1062
  {
1063
    PreparedStatement pstmt = null;
1064
    ResultSet rs = null;
1065
    StringBuffer document = null;
1066
    double startRelation = System.currentTimeMillis() / 1000;
1067
    Enumeration docidkeys = docListResult.keys();
1068
    while (docidkeys.hasMoreElements())
1069
    {
1070
      //String connstring =
1071
      // "metacat://"+util.getOption("server")+"?docid=";
1072
      String connstring = "%docid=";
1073
      String docidkey = (String) docidkeys.nextElement();
1074
      pstmt = dbconn.prepareStatement(QuerySpecification
1075
                      .printRelationSQL(docidkey));
1076
      pstmt.execute();
1077
      rs = pstmt.getResultSet();
1078
      boolean tableHasRows = rs.next();
1079
      while (tableHasRows)
1080
      {
1081
        String sub = rs.getString(1);
1082
        String rel = rs.getString(2);
1083
        String obj = rs.getString(3);
1084
        String subDT = rs.getString(4);
1085
        String objDT = rs.getString(5);
1086

    
1087
        document = new StringBuffer();
1088
        document.append("<triple>");
1089
        document.append("<subject>").append(MetaCatUtil.normalize(sub));
1090
        document.append("</subject>");
1091
        if (subDT != null)
1092
        {
1093
          document.append("<subjectdoctype>").append(subDT);
1094
          document.append("</subjectdoctype>");
1095
        }
1096
        document.append("<relationship>").append(MetaCatUtil.normalize(rel));
1097
        document.append("</relationship>");
1098
        document.append("<object>").append(MetaCatUtil.normalize(obj));
1099
        document.append("</object>");
1100
        if (objDT != null)
1101
        {
1102
          document.append("<objectdoctype>").append(objDT);
1103
          document.append("</objectdoctype>");
1104
        }
1105
        document.append("</triple>");
1106

    
1107
        String removedelement = (String) docListResult.remove(docidkey);
1108
        docListResult.put(docidkey, removedelement+ document.toString());
1109
        tableHasRows = rs.next();
1110
      }//while
1111
      rs.close();
1112
      pstmt.close();
1113
    }//while
1114
    double endRelation = System.currentTimeMillis() / 1000;
1115
    MetaCatUtil.debugMessage("Time for adding relation to docListResult: "
1116
                             + (endRelation - startRelation), 30);
1117

    
1118
    return docListResult;
1119
  }//addRelation
1120

    
1121
  /**
1122
   * removes the <?xml version="1.0"?> tag from the beginning.  This takes a
1123
   * string as a param instead of a hashtable.
1124
   *
1125
   * @param xmlquery a string representing a query.
1126
   */
1127
   private  String transformQuery(String xmlquery)
1128
   {
1129
     xmlquery = xmlquery.trim();
1130
     int index = xmlquery.indexOf("?>");
1131
     if (index != -1)
1132
     {
1133
       return xmlquery.substring(index + 2, xmlquery.length());
1134
     }
1135
     else
1136
     {
1137
       return xmlquery;
1138
     }
1139
   }
1140

    
1141

    
1142
    /*
1143
     * A method to search if Vector contains a particular key string
1144
     */
1145
    private boolean containsKey(Vector parentidList, String parentId)
1146
    {
1147

    
1148
        Vector tempVector = null;
1149

    
1150
        for (int count = 0; count < parentidList.size(); count++) {
1151
            tempVector = (Vector) parentidList.get(count);
1152
            if (parentId.compareTo((String) tempVector.get(0)) == 0) { return true; }
1153
        }
1154
        return false;
1155
    }
1156

    
1157
    /*
1158
     * A method to put key and value in Vector
1159
     */
1160
    private void putInArray(Vector parentidList, String key,
1161
            ReturnFieldValue value)
1162
    {
1163

    
1164
        Vector tempVector = null;
1165

    
1166
        for (int count = 0; count < parentidList.size(); count++) {
1167
            tempVector = (Vector) parentidList.get(count);
1168

    
1169
            if (key.compareTo((String) tempVector.get(0)) == 0) {
1170
                tempVector.remove(1);
1171
                tempVector.add(1, value);
1172
                return;
1173
            }
1174
        }
1175

    
1176
        tempVector = new Vector();
1177
        tempVector.add(0, key);
1178
        tempVector.add(1, value);
1179
        parentidList.add(tempVector);
1180
        return;
1181
    }
1182

    
1183
    /*
1184
     * A method to get value in Vector given a key
1185
     */
1186
    private ReturnFieldValue getArrayValue(Vector parentidList, String key)
1187
    {
1188

    
1189
        Vector tempVector = null;
1190

    
1191
        for (int count = 0; count < parentidList.size(); count++) {
1192
            tempVector = (Vector) parentidList.get(count);
1193

    
1194
            if (key.compareTo((String) tempVector.get(0)) == 0) { return (ReturnFieldValue) tempVector
1195
                    .get(1); }
1196
        }
1197
        return null;
1198
    }
1199

    
1200
    /*
1201
     * A method to get enumeration of all values in Vector
1202
     */
1203
    private Vector getElements(Vector parentidList)
1204
    {
1205
        Vector enumVector = new Vector();
1206
        Vector tempVector = null;
1207

    
1208
        for (int count = 0; count < parentidList.size(); count++) {
1209
            tempVector = (Vector) parentidList.get(count);
1210

    
1211
            enumVector.add(tempVector.get(1));
1212
        }
1213
        return enumVector;
1214
    }
1215

    
1216
    /*
1217
     * A method to return search result after running a query which return
1218
     * field have attribue
1219
     */
1220
    private Hashtable getAttributeValueForReturn(QuerySpecification squery,
1221
            Hashtable docInformationList, String docList, boolean useXMLIndex)
1222
    {
1223
        StringBuffer XML = null;
1224
        String sql = null;
1225
        DBConnection dbconn = null;
1226
        PreparedStatement pstmt = null;
1227
        ResultSet rs = null;
1228
        int serialNumber = -1;
1229
        boolean tableHasRows = false;
1230

    
1231
        //check the parameter
1232
        if (squery == null || docList == null || docList.length() < 0) { return docInformationList; }
1233

    
1234
        // if has attribute as return field
1235
        if (squery.containsAttributeReturnField()) {
1236
            sql = squery.printAttributeQuery(docList, useXMLIndex);
1237
            try {
1238
                dbconn = DBConnectionPool
1239
                        .getDBConnection("DBQuery.getAttributeValue");
1240
                serialNumber = dbconn.getCheckOutSerialNumber();
1241
                pstmt = dbconn.prepareStatement(sql);
1242
                pstmt.execute();
1243
                rs = pstmt.getResultSet();
1244
                tableHasRows = rs.next();
1245
                while (tableHasRows) {
1246
                    String docid = rs.getString(1).trim();
1247
                    String fieldname = rs.getString(2);
1248
                    String fielddata = rs.getString(3);
1249
                    String attirbuteName = rs.getString(4);
1250
                    XML = new StringBuffer();
1251

    
1252
                    XML.append("<param name=\"");
1253
                    XML.append(fieldname);
1254
                    XML.append("/");
1255
                    XML.append(QuerySpecification.ATTRIBUTESYMBOL);
1256
                    XML.append(attirbuteName);
1257
                    XML.append("\">");
1258
                    XML.append(fielddata);
1259
                    XML.append("</param>");
1260
                    tableHasRows = rs.next();
1261

    
1262
                    if (docInformationList.containsKey(docid)) {
1263
                        String removedelement = (String) docInformationList
1264
                                .remove(docid);
1265
                        docInformationList.put(docid, removedelement
1266
                                + XML.toString());
1267
                    } else {
1268
                        docInformationList.put(docid, XML.toString());
1269
                    }
1270
                }//while
1271
                rs.close();
1272
                pstmt.close();
1273
            } catch (Exception se) {
1274
                MetaCatUtil.debugMessage(
1275
                        "Error in DBQuery.getAttributeValue1: "
1276
                                + se.getMessage(), 30);
1277
            } finally {
1278
                try {
1279
                    pstmt.close();
1280
                }//try
1281
                catch (SQLException sqlE) {
1282
                    MetaCatUtil.debugMessage(
1283
                            "Error in DBQuery.getAttributeValue2: "
1284
                                    + sqlE.getMessage(), 30);
1285
                }//catch
1286
                finally {
1287
                    DBConnectionPool.returnDBConnection(dbconn, serialNumber);
1288
                }//finally
1289
            }//finally
1290
        }//if
1291
        return docInformationList;
1292

    
1293
    }
1294

    
1295
    /*
1296
     * A method to create a query to get owner's docid list
1297
     */
1298
    private String getOwnerQuery(String owner)
1299
    {
1300
        if (owner != null) {
1301
            owner = owner.toLowerCase();
1302
        }
1303
        StringBuffer self = new StringBuffer();
1304

    
1305
        self.append("SELECT docid,docname,doctype,");
1306
        self.append("date_created, date_updated, rev ");
1307
        self.append("FROM xml_documents WHERE docid IN (");
1308
        self.append("(");
1309
        self.append("SELECT DISTINCT docid FROM xml_nodes WHERE \n");
1310
        self.append("nodedata LIKE '%%%' ");
1311
        self.append(") \n");
1312
        self.append(") ");
1313
        self.append(" AND (");
1314
        self.append(" lower(user_owner) = '" + owner + "'");
1315
        self.append(") ");
1316
        return self.toString();
1317
    }
1318

    
1319
    /**
1320
     * format a structured query as an XML document that conforms to the
1321
     * pathquery.dtd and is appropriate for submission to the DBQuery
1322
     * structured query engine
1323
     *
1324
     * @param params The list of parameters that should be included in the
1325
     *            query
1326
     */
1327
    public static String createSQuery(Hashtable params)
1328
    {
1329
        StringBuffer query = new StringBuffer();
1330
        Enumeration elements;
1331
        Enumeration keys;
1332
        String filterDoctype = null;
1333
        String casesensitive = null;
1334
        String searchmode = null;
1335
        Object nextkey;
1336
        Object nextelement;
1337
        //add the xml headers
1338
        query.append("<?xml version=\"1.0\"?>\n");
1339
        query.append("<pathquery version=\"1.2\">\n");
1340

    
1341

    
1342

    
1343
        if (params.containsKey("meta_file_id")) {
1344
            query.append("<meta_file_id>");
1345
            query.append(((String[]) params.get("meta_file_id"))[0]);
1346
            query.append("</meta_file_id>");
1347
        }
1348

    
1349
        if (params.containsKey("returndoctype")) {
1350
            String[] returnDoctypes = ((String[]) params.get("returndoctype"));
1351
            for (int i = 0; i < returnDoctypes.length; i++) {
1352
                String doctype = (String) returnDoctypes[i];
1353

    
1354
                if (!doctype.equals("any") && !doctype.equals("ANY")
1355
                        && !doctype.equals("")) {
1356
                    query.append("<returndoctype>").append(doctype);
1357
                    query.append("</returndoctype>");
1358
                }
1359
            }
1360
        }
1361

    
1362
        if (params.containsKey("filterdoctype")) {
1363
            String[] filterDoctypes = ((String[]) params.get("filterdoctype"));
1364
            for (int i = 0; i < filterDoctypes.length; i++) {
1365
                query.append("<filterdoctype>").append(filterDoctypes[i]);
1366
                query.append("</filterdoctype>");
1367
            }
1368
        }
1369

    
1370
        if (params.containsKey("returnfield")) {
1371
            String[] returnfield = ((String[]) params.get("returnfield"));
1372
            for (int i = 0; i < returnfield.length; i++) {
1373
                query.append("<returnfield>").append(returnfield[i]);
1374
                query.append("</returnfield>");
1375
            }
1376
        }
1377

    
1378
        if (params.containsKey("owner")) {
1379
            String[] owner = ((String[]) params.get("owner"));
1380
            for (int i = 0; i < owner.length; i++) {
1381
                query.append("<owner>").append(owner[i]);
1382
                query.append("</owner>");
1383
            }
1384
        }
1385

    
1386
        if (params.containsKey("site")) {
1387
            String[] site = ((String[]) params.get("site"));
1388
            for (int i = 0; i < site.length; i++) {
1389
                query.append("<site>").append(site[i]);
1390
                query.append("</site>");
1391
            }
1392
        }
1393

    
1394
        //allows the dynamic switching of boolean operators
1395
        if (params.containsKey("operator")) {
1396
            query.append("<querygroup operator=\""
1397
                    + ((String[]) params.get("operator"))[0] + "\">");
1398
        } else { //the default operator is UNION
1399
            query.append("<querygroup operator=\"UNION\">");
1400
        }
1401

    
1402
        if (params.containsKey("casesensitive")) {
1403
            casesensitive = ((String[]) params.get("casesensitive"))[0];
1404
        } else {
1405
            casesensitive = "false";
1406
        }
1407

    
1408
        if (params.containsKey("searchmode")) {
1409
            searchmode = ((String[]) params.get("searchmode"))[0];
1410
        } else {
1411
            searchmode = "contains";
1412
        }
1413

    
1414
        //anyfield is a special case because it does a
1415
        //free text search. It does not have a <pathexpr>
1416
        //tag. This allows for a free text search within the structured
1417
        //query. This is useful if the INTERSECT operator is used.
1418
        if (params.containsKey("anyfield")) {
1419
            String[] anyfield = ((String[]) params.get("anyfield"));
1420
            //allow for more than one value for anyfield
1421
            for (int i = 0; i < anyfield.length; i++) {
1422
                if (!anyfield[i].equals("")) {
1423
                    query.append("<queryterm casesensitive=\"" + casesensitive
1424
                            + "\" " + "searchmode=\"" + searchmode
1425
                            + "\"><value>" + anyfield[i]
1426
                            + "</value></queryterm>");
1427
                }
1428
            }
1429
        }
1430

    
1431
        //this while loop finds the rest of the parameters
1432
        //and attempts to query for the field specified
1433
        //by the parameter.
1434
        elements = params.elements();
1435
        keys = params.keys();
1436
        while (keys.hasMoreElements() && elements.hasMoreElements()) {
1437
            nextkey = keys.nextElement();
1438
            nextelement = elements.nextElement();
1439

    
1440
            //make sure we aren't querying for any of these
1441
            //parameters since the are already in the query
1442
            //in one form or another.
1443
            Vector ignoredParams = new Vector();
1444
            ignoredParams.add("returndoctype");
1445
            ignoredParams.add("filterdoctype");
1446
            ignoredParams.add("action");
1447
            ignoredParams.add("qformat");
1448
            ignoredParams.add("anyfield");
1449
            ignoredParams.add("returnfield");
1450
            ignoredParams.add("owner");
1451
            ignoredParams.add("site");
1452
            ignoredParams.add("operator");
1453
            ignoredParams.add("sessionid");
1454

    
1455
            // Also ignore parameters listed in the properties file
1456
            // so that they can be passed through to stylesheets
1457
            String paramsToIgnore = MetaCatUtil
1458
                    .getOption("query.ignored.params");
1459
            StringTokenizer st = new StringTokenizer(paramsToIgnore, ",");
1460
            while (st.hasMoreTokens()) {
1461
                ignoredParams.add(st.nextToken());
1462
            }
1463
            if (!ignoredParams.contains(nextkey.toString())) {
1464
                //allow for more than value per field name
1465
                for (int i = 0; i < ((String[]) nextelement).length; i++) {
1466
                    if (!((String[]) nextelement)[i].equals("")) {
1467
                        query.append("<queryterm casesensitive=\""
1468
                                + casesensitive + "\" " + "searchmode=\""
1469
                                + searchmode + "\">" + "<value>" +
1470
                                //add the query value
1471
                                ((String[]) nextelement)[i]
1472
                                + "</value><pathexpr>" +
1473
                                //add the path to query by
1474
                                nextkey.toString() + "</pathexpr></queryterm>");
1475
                    }
1476
                }
1477
            }
1478
        }
1479
        query.append("</querygroup></pathquery>");
1480
        //append on the end of the xml and return the result as a string
1481
        return query.toString();
1482
    }
1483

    
1484
    /**
1485
     * format a simple free-text value query as an XML document that conforms
1486
     * to the pathquery.dtd and is appropriate for submission to the DBQuery
1487
     * structured query engine
1488
     *
1489
     * @param value the text string to search for in the xml catalog
1490
     * @param doctype the type of documents to include in the result set -- use
1491
     *            "any" or "ANY" for unfiltered result sets
1492
     */
1493
    public static String createQuery(String value, String doctype)
1494
    {
1495
        StringBuffer xmlquery = new StringBuffer();
1496
        xmlquery.append("<?xml version=\"1.0\"?>\n");
1497
        xmlquery.append("<pathquery version=\"1.0\">");
1498

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

    
1504
        xmlquery.append("<querygroup operator=\"UNION\">");
1505
        //chad added - 8/14
1506
        //the if statement allows a query to gracefully handle a null
1507
        //query. Without this if a nullpointerException is thrown.
1508
        if (!value.equals("")) {
1509
            xmlquery.append("<queryterm casesensitive=\"false\" ");
1510
            xmlquery.append("searchmode=\"contains\">");
1511
            xmlquery.append("<value>").append(value).append("</value>");
1512
            xmlquery.append("</queryterm>");
1513
        }
1514
        xmlquery.append("</querygroup>");
1515
        xmlquery.append("</pathquery>");
1516

    
1517
        return (xmlquery.toString());
1518
    }
1519

    
1520
    /**
1521
     * format a simple free-text value query as an XML document that conforms
1522
     * to the pathquery.dtd and is appropriate for submission to the DBQuery
1523
     * structured query engine
1524
     *
1525
     * @param value the text string to search for in the xml catalog
1526
     */
1527
    public static String createQuery(String value)
1528
    {
1529
        return createQuery(value, "any");
1530
    }
1531

    
1532
    /**
1533
     * Check for "READ" permission on @docid for @user and/or @group from DB
1534
     * connection
1535
     */
1536
    private boolean hasPermission(String user, String[] groups, String docid)
1537
            throws SQLException, Exception
1538
    {
1539
        // Check for READ permission on @docid for @user and/or @groups
1540
        PermissionController controller = new PermissionController(docid);
1541
        return controller.hasPermission(user, groups,
1542
                AccessControlInterface.READSTRING);
1543
    }
1544

    
1545
    /**
1546
     * Get all docIds list for a data packadge
1547
     *
1548
     * @param dataPackageDocid, the string in docId field of xml_relation table
1549
     */
1550
    private Vector getCurrentDocidListForDataPackage(String dataPackageDocid)
1551
    {
1552
        DBConnection dbConn = null;
1553
        int serialNumber = -1;
1554
        Vector docIdList = new Vector();//return value
1555
        PreparedStatement pStmt = null;
1556
        ResultSet rs = null;
1557
        String docIdInSubjectField = null;
1558
        String docIdInObjectField = null;
1559

    
1560
        // Check the parameter
1561
        if (dataPackageDocid == null || dataPackageDocid.equals("")) { return docIdList; }//if
1562

    
1563
        //the query stirng
1564
        String query = "SELECT subject, object from xml_relation where docId = ?";
1565
        try {
1566
            dbConn = DBConnectionPool
1567
                    .getDBConnection("DBQuery.getCurrentDocidListForDataPackage");
1568
            serialNumber = dbConn.getCheckOutSerialNumber();
1569
            pStmt = dbConn.prepareStatement(query);
1570
            //bind the value to query
1571
            pStmt.setString(1, dataPackageDocid);
1572

    
1573
            //excute the query
1574
            pStmt.execute();
1575
            //get the result set
1576
            rs = pStmt.getResultSet();
1577
            //process the result
1578
            while (rs.next()) {
1579
                //In order to get the whole docIds in a data packadge,
1580
                //we need to put the docIds of subject and object field in
1581
                // xml_relation
1582
                //into the return vector
1583
                docIdInSubjectField = rs.getString(1);//the result docId in
1584
                                                      // subject field
1585
                docIdInObjectField = rs.getString(2);//the result docId in
1586
                                                     // object field
1587

    
1588
                //don't put the duplicate docId into the vector
1589
                if (!docIdList.contains(docIdInSubjectField)) {
1590
                    docIdList.add(docIdInSubjectField);
1591
                }
1592

    
1593
                //don't put the duplicate docId into the vector
1594
                if (!docIdList.contains(docIdInObjectField)) {
1595
                    docIdList.add(docIdInObjectField);
1596
                }
1597
            }//while
1598
            //close the pStmt
1599
            pStmt.close();
1600
        }//try
1601
        catch (SQLException e) {
1602
            MetaCatUtil.debugMessage("Error in getDocidListForDataPackage: "
1603
                    + e.getMessage(), 30);
1604
        }//catch
1605
        finally {
1606
            try {
1607
                pStmt.close();
1608
            }//try
1609
            catch (SQLException ee) {
1610
                MetaCatUtil.debugMessage(
1611
                        "Error in getDocidListForDataPackage: "
1612
                                + ee.getMessage(), 30);
1613
            }//catch
1614
            finally {
1615
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1616
            }//fianlly
1617
        }//finally
1618
        return docIdList;
1619
    }//getCurrentDocidListForDataPackadge()
1620

    
1621
    /**
1622
     * Get all docIds list for a data packadge
1623
     *
1624
     * @param dataPackageDocid, the string in docId field of xml_relation table
1625
     */
1626
    private Vector getOldVersionDocidListForDataPackage(String dataPackageDocidWithRev)
1627
    {
1628

    
1629
        Vector docIdList = new Vector();//return value
1630
        Vector tripleList = null;
1631
        String xml = null;
1632

    
1633
        // Check the parameter
1634
        if (dataPackageDocidWithRev == null || dataPackageDocidWithRev.equals("")) { return docIdList; }//if
1635

    
1636
        try {
1637
            //initial a documentImpl object
1638
            DocumentImpl packageDocument = new DocumentImpl(dataPackageDocidWithRev);
1639
            //transfer to documentImpl object to string
1640
            xml = packageDocument.toString();
1641

    
1642
            //create a tripcollection object
1643
            TripleCollection tripleForPackage = new TripleCollection(
1644
                    new StringReader(xml));
1645
            //get the vetor of triples
1646
            tripleList = tripleForPackage.getCollection();
1647

    
1648
            for (int i = 0; i < tripleList.size(); i++) {
1649
                //put subject docid into docIdlist without duplicate
1650
                if (!docIdList.contains(((Triple) tripleList.elementAt(i))
1651
                        .getSubject())) {
1652
                    //put subject docid into docIdlist
1653
                    docIdList.add(((Triple) tripleList.get(i)).getSubject());
1654
                }
1655
                //put object docid into docIdlist without duplicate
1656
                if (!docIdList.contains(((Triple) tripleList.elementAt(i))
1657
                        .getObject())) {
1658
                    docIdList.add(((Triple) (tripleList.get(i))).getObject());
1659
                }
1660
            }//for
1661
        }//try
1662
        catch (Exception e) {
1663
            MetaCatUtil.debugMessage("Error in getOldVersionAllDocumentImpl: "
1664
                    + e.getMessage(), 30);
1665
        }//catch
1666

    
1667
        // return result
1668
        return docIdList;
1669
    }//getDocidListForPackageInXMLRevisions()
1670

    
1671
    /**
1672
     * Check if the docId is a data packadge id. If the id is a data packadage
1673
     * id, it should be store in the docId fields in xml_relation table. So we
1674
     * can use a query to get the entries which the docId equals the given
1675
     * value. If the result is null. The docId is not a packadge id. Otherwise,
1676
     * it is.
1677
     *
1678
     * @param docId, the id need to be checked
1679
     */
1680
    private boolean isDataPackageId(String docId)
1681
    {
1682
        boolean result = false;
1683
        PreparedStatement pStmt = null;
1684
        ResultSet rs = null;
1685
        String query = "SELECT docId from xml_relation where docId = ?";
1686
        DBConnection dbConn = null;
1687
        int serialNumber = -1;
1688
        try {
1689
            dbConn = DBConnectionPool
1690
                    .getDBConnection("DBQuery.isDataPackageId");
1691
            serialNumber = dbConn.getCheckOutSerialNumber();
1692
            pStmt = dbConn.prepareStatement(query);
1693
            //bind the value to query
1694
            pStmt.setString(1, docId);
1695
            //execute the query
1696
            pStmt.execute();
1697
            rs = pStmt.getResultSet();
1698
            //process the result
1699
            if (rs.next()) //There are some records for the id in docId fields
1700
            {
1701
                result = true;//It is a data packadge id
1702
            }
1703
            pStmt.close();
1704
        }//try
1705
        catch (SQLException e) {
1706
            MetaCatUtil.debugMessage("Error in isDataPackageId: "
1707
                    + e.getMessage(), 30);
1708
        } finally {
1709
            try {
1710
                pStmt.close();
1711
            }//try
1712
            catch (SQLException ee) {
1713
                MetaCatUtil.debugMessage("Error in isDataPackageId: "
1714
                        + ee.getMessage(), 30);
1715
            }//catch
1716
            finally {
1717
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1718
            }//finally
1719
        }//finally
1720
        return result;
1721
    }//isDataPackageId()
1722

    
1723
    /**
1724
     * Check if the user has the permission to export data package
1725
     *
1726
     * @param conn, the connection
1727
     * @param docId, the id need to be checked
1728
     * @param user, the name of user
1729
     * @param groups, the user's group
1730
     */
1731
    private boolean hasPermissionToExportPackage(String docId, String user,
1732
            String[] groups) throws Exception
1733
    {
1734
        //DocumentImpl doc=new DocumentImpl(conn,docId);
1735
        return DocumentImpl.hasReadPermission(user, groups, docId);
1736
    }
1737

    
1738
    /**
1739
     * Get the current Rev for a docid in xml_documents table
1740
     *
1741
     * @param docId, the id need to get version numb If the return value is -5,
1742
     *            means no value in rev field for this docid
1743
     */
1744
    private int getCurrentRevFromXMLDoumentsTable(String docId)
1745
            throws SQLException
1746
    {
1747
        int rev = -5;
1748
        PreparedStatement pStmt = null;
1749
        ResultSet rs = null;
1750
        String query = "SELECT rev from xml_documents where docId = ?";
1751
        DBConnection dbConn = null;
1752
        int serialNumber = -1;
1753
        try {
1754
            dbConn = DBConnectionPool
1755
                    .getDBConnection("DBQuery.getCurrentRevFromXMLDocumentsTable");
1756
            serialNumber = dbConn.getCheckOutSerialNumber();
1757
            pStmt = dbConn.prepareStatement(query);
1758
            //bind the value to query
1759
            pStmt.setString(1, docId);
1760
            //execute the query
1761
            pStmt.execute();
1762
            rs = pStmt.getResultSet();
1763
            //process the result
1764
            if (rs.next()) //There are some records for rev
1765
            {
1766
                rev = rs.getInt(1);
1767
                ;//It is the version for given docid
1768
            } else {
1769
                rev = -5;
1770
            }
1771

    
1772
        }//try
1773
        catch (SQLException e) {
1774
            MetaCatUtil.debugMessage(
1775
                    "Error in getCurrentRevFromXMLDoumentsTable: "
1776
                            + e.getMessage(), 30);
1777
            throw e;
1778
        }//catch
1779
        finally {
1780
            try {
1781
                pStmt.close();
1782
            }//try
1783
            catch (SQLException ee) {
1784
                MetaCatUtil.debugMessage(
1785
                        "Error in getCurrentRevFromXMLDoumentsTable: "
1786
                                + ee.getMessage(), 30);
1787
            }//catch
1788
            finally {
1789
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1790
            }//finally
1791
        }//finally
1792
        return rev;
1793
    }//getCurrentRevFromXMLDoumentsTable
1794

    
1795
    /**
1796
     * put a doc into a zip output stream
1797
     *
1798
     * @param docImpl, docmentImpl object which will be sent to zip output
1799
     *            stream
1800
     * @param zipOut, zip output stream which the docImpl will be put
1801
     * @param packageZipEntry, the zip entry name for whole package
1802
     */
1803
    private void addDocToZipOutputStream(DocumentImpl docImpl,
1804
            ZipOutputStream zipOut, String packageZipEntry)
1805
            throws ClassNotFoundException, IOException, SQLException,
1806
            McdbException, Exception
1807
    {
1808
        byte[] byteString = null;
1809
        ZipEntry zEntry = null;
1810

    
1811
        byteString = docImpl.toString().getBytes();
1812
        //use docId as the zip entry's name
1813
        zEntry = new ZipEntry(packageZipEntry + "/metadata/"
1814
                + docImpl.getDocID());
1815
        zEntry.setSize(byteString.length);
1816
        zipOut.putNextEntry(zEntry);
1817
        zipOut.write(byteString, 0, byteString.length);
1818
        zipOut.closeEntry();
1819

    
1820
    }//addDocToZipOutputStream()
1821

    
1822
    /**
1823
     * Transfer a docid vetor to a documentImpl vector. The documentImpl vetor
1824
     * only inlcudes current version. If a DocumentImple object couldn't find
1825
     * for a docid, then the String of this docid was added to vetor rather
1826
     * than DocumentImple object.
1827
     *
1828
     * @param docIdList, a vetor hold a docid list for a data package. In
1829
     *            docid, there is not version number in it.
1830
     */
1831

    
1832
    private Vector getCurrentAllDocumentImpl(Vector docIdList)
1833
            throws McdbException, Exception
1834
    {
1835
        //Connection dbConn=null;
1836
        Vector documentImplList = new Vector();
1837
        int rev = 0;
1838

    
1839
        // Check the parameter
1840
        if (docIdList.isEmpty()) { return documentImplList; }//if
1841

    
1842
        //for every docid in vector
1843
        for (int i = 0; i < docIdList.size(); i++) {
1844
            try {
1845
                //get newest version for this docId
1846
                rev = getCurrentRevFromXMLDoumentsTable((String) docIdList
1847
                        .elementAt(i));
1848

    
1849
                // There is no record for this docId in xml_documents table
1850
                if (rev == -5) {
1851
                    // Rather than put DocumentImple object, put a String
1852
                    // Object(docid)
1853
                    // into the documentImplList
1854
                    documentImplList.add((String) docIdList.elementAt(i));
1855
                    // Skip other code
1856
                    continue;
1857
                }
1858

    
1859
                String docidPlusVersion = ((String) docIdList.elementAt(i))
1860
                        + MetaCatUtil.getOption("accNumSeparator") + rev;
1861

    
1862
                //create new documentImpl object
1863
                DocumentImpl documentImplObject = new DocumentImpl(
1864
                        docidPlusVersion);
1865
                //add them to vector
1866
                documentImplList.add(documentImplObject);
1867
            }//try
1868
            catch (Exception e) {
1869
                MetaCatUtil.debugMessage("Error in getCurrentAllDocumentImpl: "
1870
                        + e.getMessage(), 30);
1871
                // continue the for loop
1872
                continue;
1873
            }
1874
        }//for
1875
        return documentImplList;
1876
    }
1877

    
1878
    /**
1879
     * Transfer a docid vetor to a documentImpl vector. If a DocumentImple
1880
     * object couldn't find for a docid, then the String of this docid was
1881
     * added to vetor rather than DocumentImple object.
1882
     *
1883
     * @param docIdList, a vetor hold a docid list for a data package. In
1884
     *            docid, t here is version number in it.
1885
     */
1886
    private Vector getOldVersionAllDocumentImpl(Vector docIdList)
1887
    {
1888
        //Connection dbConn=null;
1889
        Vector documentImplList = new Vector();
1890
        String siteCode = null;
1891
        String uniqueId = null;
1892
        int rev = 0;
1893

    
1894
        // Check the parameter
1895
        if (docIdList.isEmpty()) { return documentImplList; }//if
1896

    
1897
        //for every docid in vector
1898
        for (int i = 0; i < docIdList.size(); i++) {
1899

    
1900
            String docidPlusVersion = (String) (docIdList.elementAt(i));
1901

    
1902
            try {
1903
                //create new documentImpl object
1904
                DocumentImpl documentImplObject = new DocumentImpl(
1905
                        docidPlusVersion);
1906
                //add them to vector
1907
                documentImplList.add(documentImplObject);
1908
            }//try
1909
            catch (McdbDocNotFoundException notFoundE) {
1910
                MetaCatUtil.debugMessage(
1911
                        "Error in DBQuery.getOldVersionAllDocument" + "Imple"
1912
                                + notFoundE.getMessage(), 30);
1913
                // Rather than add a DocumentImple object into vetor, a String
1914
                // object
1915
                // - the doicd was added to the vector
1916
                documentImplList.add(docidPlusVersion);
1917
                // Continue the for loop
1918
                continue;
1919
            }//catch
1920
            catch (Exception e) {
1921
                MetaCatUtil.debugMessage(
1922
                        "Error in DBQuery.getOldVersionAllDocument" + "Imple"
1923
                                + e.getMessage(), 30);
1924
                // Continue the for loop
1925
                continue;
1926
            }//catch
1927

    
1928
        }//for
1929
        return documentImplList;
1930
    }//getOldVersionAllDocumentImple
1931

    
1932
    /**
1933
     * put a data file into a zip output stream
1934
     *
1935
     * @param docImpl, docmentImpl object which will be sent to zip output
1936
     *            stream
1937
     * @param zipOut, the zip output stream which the docImpl will be put
1938
     * @param packageZipEntry, the zip entry name for whole package
1939
     */
1940
    private void addDataFileToZipOutputStream(DocumentImpl docImpl,
1941
            ZipOutputStream zipOut, String packageZipEntry)
1942
            throws ClassNotFoundException, IOException, SQLException,
1943
            McdbException, Exception
1944
    {
1945
        byte[] byteString = null;
1946
        ZipEntry zEntry = null;
1947
        // this is data file; add file to zip
1948
        String filePath = MetaCatUtil.getOption("datafilepath");
1949
        if (!filePath.endsWith("/")) {
1950
            filePath += "/";
1951
        }
1952
        String fileName = filePath + docImpl.getDocID();
1953
        zEntry = new ZipEntry(packageZipEntry + "/data/" + docImpl.getDocID());
1954
        zipOut.putNextEntry(zEntry);
1955
        FileInputStream fin = null;
1956
        try {
1957
            fin = new FileInputStream(fileName);
1958
            byte[] buf = new byte[4 * 1024]; // 4K buffer
1959
            int b = fin.read(buf);
1960
            while (b != -1) {
1961
                zipOut.write(buf, 0, b);
1962
                b = fin.read(buf);
1963
            }//while
1964
            zipOut.closeEntry();
1965
        }//try
1966
        catch (IOException ioe) {
1967
            MetaCatUtil.debugMessage("There is an exception: "
1968
                    + ioe.getMessage(), 30);
1969
        }//catch
1970
    }//addDataFileToZipOutputStream()
1971

    
1972
    /**
1973
     * create a html summary for data package and put it into zip output stream
1974
     *
1975
     * @param docImplList, the documentImpl ojbects in data package
1976
     * @param zipOut, the zip output stream which the html should be put
1977
     * @param packageZipEntry, the zip entry name for whole package
1978
     */
1979
    private void addHtmlSummaryToZipOutputStream(Vector docImplList,
1980
            ZipOutputStream zipOut, String packageZipEntry) throws Exception
1981
    {
1982
        StringBuffer htmlDoc = new StringBuffer();
1983
        ZipEntry zEntry = null;
1984
        byte[] byteString = null;
1985
        InputStream source;
1986
        DBTransform xmlToHtml;
1987

    
1988
        //create a DBTransform ojbect
1989
        xmlToHtml = new DBTransform();
1990
        //head of html
1991
        htmlDoc.append("<html><head></head><body>");
1992
        for (int i = 0; i < docImplList.size(); i++) {
1993
            // If this String object, this means it is missed data file
1994
            if ((((docImplList.elementAt(i)).getClass()).toString())
1995
                    .equals("class java.lang.String")) {
1996

    
1997
                htmlDoc.append("<a href=\"");
1998
                String dataFileid = (String) docImplList.elementAt(i);
1999
                htmlDoc.append("./data/").append(dataFileid).append("\">");
2000
                htmlDoc.append("Data File: ");
2001
                htmlDoc.append(dataFileid).append("</a><br>");
2002
                htmlDoc.append("<br><hr><br>");
2003

    
2004
            }//if
2005
            else if ((((DocumentImpl) docImplList.elementAt(i)).getDoctype())
2006
                    .compareTo("BIN") != 0) { //this is an xml file so we can
2007
                                              // transform it.
2008
                //transform each file individually then concatenate all of the
2009
                //transformations together.
2010

    
2011
                //for metadata xml title
2012
                htmlDoc.append("<h2>");
2013
                htmlDoc.append(((DocumentImpl) docImplList.elementAt(i))
2014
                        .getDocID());
2015
                //htmlDoc.append(".");
2016
                //htmlDoc.append(((DocumentImpl)docImplList.elementAt(i)).getRev());
2017
                htmlDoc.append("</h2>");
2018
                //do the actual transform
2019
                StringWriter docString = new StringWriter();
2020
                xmlToHtml.transformXMLDocument(((DocumentImpl) docImplList
2021
                        .elementAt(i)).toString(), "-//NCEAS//eml-generic//EN",
2022
                        "-//W3C//HTML//EN", "html", docString);
2023
                htmlDoc.append(docString.toString());
2024
                htmlDoc.append("<br><br><hr><br><br>");
2025
            }//if
2026
            else { //this is a data file so we should link to it in the html
2027
                htmlDoc.append("<a href=\"");
2028
                String dataFileid = ((DocumentImpl) docImplList.elementAt(i))
2029
                        .getDocID();
2030
                htmlDoc.append("./data/").append(dataFileid).append("\">");
2031
                htmlDoc.append("Data File: ");
2032
                htmlDoc.append(dataFileid).append("</a><br>");
2033
                htmlDoc.append("<br><hr><br>");
2034
            }//else
2035
        }//for
2036
        htmlDoc.append("</body></html>");
2037
        byteString = htmlDoc.toString().getBytes();
2038
        zEntry = new ZipEntry(packageZipEntry + "/metadata.html");
2039
        zEntry.setSize(byteString.length);
2040
        zipOut.putNextEntry(zEntry);
2041
        zipOut.write(byteString, 0, byteString.length);
2042
        zipOut.closeEntry();
2043
        //dbConn.close();
2044

    
2045
    }//addHtmlSummaryToZipOutputStream
2046

    
2047
    /**
2048
     * put a data packadge into a zip output stream
2049
     *
2050
     * @param docId, which the user want to put into zip output stream,it has version
2051
     * @param out, a servletoutput stream which the zip output stream will be
2052
     *            put
2053
     * @param user, the username of the user
2054
     * @param groups, the group of the user
2055
     */
2056
    public ZipOutputStream getZippedPackage(String docIdString,
2057
            ServletOutputStream out, String user, String[] groups,
2058
            String passWord) throws ClassNotFoundException, IOException,
2059
            SQLException, McdbException, NumberFormatException, Exception
2060
    {
2061
        ZipOutputStream zOut = null;
2062
        String elementDocid = null;
2063
        DocumentImpl docImpls = null;
2064
        //Connection dbConn = null;
2065
        Vector docIdList = new Vector();
2066
        Vector documentImplList = new Vector();
2067
        Vector htmlDocumentImplList = new Vector();
2068
        String packageId = null;
2069
        String rootName = "package";//the package zip entry name
2070

    
2071
        String docId = null;
2072
        int version = -5;
2073
        // Docid without revision
2074
        docId = MetaCatUtil.getDocIdFromString(docIdString);
2075
        // revision number
2076
        version = MetaCatUtil.getVersionFromString(docIdString);
2077

    
2078
        //check if the reqused docId is a data package id
2079
        if (!isDataPackageId(docId)) {
2080

    
2081
            /*
2082
             * Exception e = new Exception("The request the doc id "
2083
             * +docIdString+ " is not a data package id");
2084
             */
2085

    
2086
            //CB 1/6/03: if the requested docid is not a datapackage, we just
2087
            // zip
2088
            //up the single document and return the zip file.
2089
            if (!hasPermissionToExportPackage(docId, user, groups)) {
2090

    
2091
                Exception e = new Exception("User " + user
2092
                        + " does not have permission"
2093
                        + " to export the data package " + docIdString);
2094
                throw e;
2095
            }
2096

    
2097
            docImpls = new DocumentImpl(docIdString);
2098
            //checking if the user has the permission to read the documents
2099
            if (DocumentImpl.hasReadPermission(user, groups, docImpls
2100
                    .getDocID())) {
2101
                zOut = new ZipOutputStream(out);
2102
                //if the docImpls is metadata
2103
                if ((docImpls.getDoctype()).compareTo("BIN") != 0) {
2104
                    //add metadata into zip output stream
2105
                    addDocToZipOutputStream(docImpls, zOut, rootName);
2106
                }//if
2107
                else {
2108
                    //it is data file
2109
                    addDataFileToZipOutputStream(docImpls, zOut, rootName);
2110
                    htmlDocumentImplList.add(docImpls);
2111
                }//else
2112
            }//if
2113

    
2114
            zOut.finish(); //terminate the zip file
2115
            return zOut;
2116
        }
2117
        // Check the permission of user
2118
        else if (!hasPermissionToExportPackage(docId, user, groups)) {
2119

    
2120
            Exception e = new Exception("User " + user
2121
                    + " does not have permission"
2122
                    + " to export the data package " + docIdString);
2123
            throw e;
2124
        } else //it is a packadge id
2125
        {
2126
            //store the package id
2127
            packageId = docId;
2128
            //get current version in database
2129
            int currentVersion = getCurrentRevFromXMLDoumentsTable(packageId);
2130
            //If it is for current version (-1 means user didn't specify
2131
            // revision)
2132
            if ((version == -1) || version == currentVersion) {
2133
                //get current version number
2134
                version = currentVersion;
2135
                //get package zip entry name
2136
                //it should be docId.revsion.package
2137
                rootName = packageId + MetaCatUtil.getOption("accNumSeparator")
2138
                        + version + MetaCatUtil.getOption("accNumSeparator")
2139
                        + "package";
2140
                //get the whole id list for data packadge
2141
                docIdList = getCurrentDocidListForDataPackage(packageId);
2142
                //get the whole documentImple object
2143
                documentImplList = getCurrentAllDocumentImpl(docIdList);
2144

    
2145
            }//if
2146
            else if (version > currentVersion || version < -1) {
2147
                throw new Exception("The user specified docid: " + docId + "."
2148
                        + version + " doesn't exist");
2149
            }//else if
2150
            else //for an old version
2151
            {
2152

    
2153
                rootName = docIdString
2154
                        + MetaCatUtil.getOption("accNumSeparator") + "package";
2155
                //get the whole id list for data packadge
2156
                docIdList = getOldVersionDocidListForDataPackage(docIdString);
2157

    
2158
                //get the whole documentImple object
2159
                documentImplList = getOldVersionAllDocumentImpl(docIdList);
2160
            }//else
2161

    
2162
            // Make sure documentImplist is not empty
2163
            if (documentImplList.isEmpty()) { throw new Exception(
2164
                    "Couldn't find component for data package: " + packageId); }//if
2165

    
2166
            zOut = new ZipOutputStream(out);
2167
            //put every element into zip output stream
2168
            for (int i = 0; i < documentImplList.size(); i++) {
2169
                // if the object in the vetor is String, this means we couldn't
2170
                // find
2171
                // the document locally, we need find it remote
2172
                if ((((documentImplList.elementAt(i)).getClass()).toString())
2173
                        .equals("class java.lang.String")) {
2174
                    // Get String object from vetor
2175
                    String documentId = (String) documentImplList.elementAt(i);
2176
                    MetaCatUtil.debugMessage("docid: " + documentId, 30);
2177
                    // Get doicd without revision
2178
                    String docidWithoutRevision = MetaCatUtil
2179
                            .getDocIdFromString(documentId);
2180
                    MetaCatUtil.debugMessage("docidWithoutRevsion: "
2181
                            + docidWithoutRevision, 30);
2182
                    // Get revision
2183
                    String revision = MetaCatUtil
2184
                            .getRevisionStringFromString(documentId);
2185
                    MetaCatUtil.debugMessage("revsion from docIdentifier: "
2186
                            + revision, 30);
2187
                    // Zip entry string
2188
                    String zipEntryPath = rootName + "/data/";
2189
                    // Create a RemoteDocument object
2190
                    RemoteDocument remoteDoc = new RemoteDocument(
2191
                            docidWithoutRevision, revision, user, passWord,
2192
                            zipEntryPath);
2193
                    // Here we only read data file from remote metacat
2194
                    String docType = remoteDoc.getDocType();
2195
                    if (docType != null) {
2196
                        if (docType.equals("BIN")) {
2197
                            // Put remote document to zip output
2198
                            remoteDoc.readDocumentFromRemoteServerByZip(zOut);
2199
                            // Add String object to htmlDocumentImplList
2200
                            String elementInHtmlList = remoteDoc
2201
                                    .getDocIdWithoutRevsion()
2202
                                    + MetaCatUtil.getOption("accNumSeparator")
2203
                                    + remoteDoc.getRevision();
2204
                            htmlDocumentImplList.add(elementInHtmlList);
2205
                        }//if
2206
                    }//if
2207

    
2208
                }//if
2209
                else {
2210
                    //create a docmentImpls object (represent xml doc) base on
2211
                    // the docId
2212
                    docImpls = (DocumentImpl) documentImplList.elementAt(i);
2213
                    //checking if the user has the permission to read the
2214
                    // documents
2215
                    if (DocumentImpl.hasReadPermission(user, groups, docImpls
2216
                            .getDocID())) {
2217
                        //if the docImpls is metadata
2218
                        if ((docImpls.getDoctype()).compareTo("BIN") != 0) {
2219
                            //add metadata into zip output stream
2220
                            addDocToZipOutputStream(docImpls, zOut, rootName);
2221
                            //add the documentImpl into the vetor which will
2222
                            // be used in html
2223
                            htmlDocumentImplList.add(docImpls);
2224

    
2225
                        }//if
2226
                        else {
2227
                            //it is data file
2228
                            addDataFileToZipOutputStream(docImpls, zOut,
2229
                                    rootName);
2230
                            htmlDocumentImplList.add(docImpls);
2231
                        }//else
2232
                    }//if
2233
                }//else
2234
            }//for
2235

    
2236
            //add html summary file
2237
            addHtmlSummaryToZipOutputStream(htmlDocumentImplList, zOut,
2238
                    rootName);
2239
            zOut.finish(); //terminate the zip file
2240
            //dbConn.close();
2241
            return zOut;
2242
        }//else
2243
    }//getZippedPackage()
2244

    
2245
    private class ReturnFieldValue
2246
    {
2247

    
2248
        private String docid = null; //return field value for this docid
2249

    
2250
        private String fieldValue = null;
2251

    
2252
        private String xmlFieldValue = null; //return field value in xml
2253
                                             // format
2254

    
2255
        public void setDocid(String myDocid)
2256
        {
2257
            docid = myDocid;
2258
        }
2259

    
2260
        public String getDocid()
2261
        {
2262
            return docid;
2263
        }
2264

    
2265
        public void setFieldValue(String myValue)
2266
        {
2267
            fieldValue = myValue;
2268
        }
2269

    
2270
        public String getFieldValue()
2271
        {
2272
            return fieldValue;
2273
        }
2274

    
2275
        public void setXMLFieldValue(String xml)
2276
        {
2277
            xmlFieldValue = xml;
2278
        }
2279

    
2280
        public String getXMLFieldValue()
2281
        {
2282
            return xmlFieldValue;
2283
        }
2284

    
2285
    }
2286
}
(22-22/63)