Project

General

Profile

1 155 jones
/**
2 203 jones
 *  '$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 349 jones
 *    Release: @release@
12 155 jones
 *
13 203 jones
 *   '$Author$'
14
 *     '$Date$'
15
 * '$Revision$'
16 669 jones
 *
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 155 jones
 */
31
32 607 bojilova
package edu.ucsb.nceas.metacat;
33 155 jones
34 940 tao
import edu.ucsb.nceas.morpho.datapackage.*;
35 155 jones
import java.io.*;
36 401 berkley
import java.util.Vector;
37 940 tao
import java.util.zip.*;
38 155 jones
import java.net.URL;
39
import java.net.MalformedURLException;
40
import java.sql.*;
41
import java.util.Stack;
42
import java.util.Hashtable;
43
import java.util.Enumeration;
44 706 bojilova
import java.io.File;
45
import java.io.FileWriter;
46
import java.io.BufferedWriter;
47 940 tao
import javax.servlet.ServletOutputStream;
48 155 jones
49
/**
50 172 jones
 * A Class that searches a relational DB for elements and
51
 * attributes that have free text matches a query string,
52
 * or structured query matches to a path specified node in the
53
 * XML hierarchy.  It returns a result set consisting of the
54
 * document ID for each document that satisfies the query
55 155 jones
 */
56
public class DBQuery {
57
58 441 bojilova
  static final int ALL = 1;
59
  static final int WRITE = 2;
60
  static final int READ = 4;
61 940 tao
62 535 jones
  private Connection  conn = null;
63
  private String  parserName = null;
64 465 berkley
  private MetaCatUtil util = new MetaCatUtil();
65 155 jones
  /**
66
   * the main routine used to test the DBQuery utility.
67 184 jones
   * <p>
68
   * Usage: java DBQuery <xmlfile>
69 155 jones
   *
70 170 jones
   * @param xmlfile the filename of the xml file containing the query
71 155 jones
   */
72
  static public void main(String[] args) {
73
74 184 jones
     if (args.length < 1)
75 155 jones
     {
76
        System.err.println("Wrong number of arguments!!!");
77 706 bojilova
        System.err.println("USAGE: java DBQuery [-t] [-index] <xmlfile>");
78 155 jones
        return;
79
     } else {
80
        try {
81
82 706 bojilova
          int i = 0;
83
          boolean showRuntime = false;
84
          boolean useXMLIndex = false;
85
          if ( args[i].equals( "-t" ) ) {
86
            showRuntime = true;
87
            i++;
88
          }
89
          if ( args[i].equals( "-index" ) ) {
90
            useXMLIndex = true;
91
            i++;
92
          }
93
          String xmlfile  = args[i];
94
95
          // Time the request if asked for
96
          double startTime = System.currentTimeMillis();
97
98 155 jones
          // Open a connection to the database
99 184 jones
          MetaCatUtil   util = new MetaCatUtil();
100
          Connection dbconn = util.openDBConnection();
101 706 bojilova
102 705 berkley
          double connTime = System.currentTimeMillis();
103 706 bojilova
104 170 jones
          // Execute the query
105 184 jones
          DBQuery queryobj = new DBQuery(dbconn, util.getOption("saxparser"));
106 170 jones
          FileReader xml = new FileReader(new File(xmlfile));
107 155 jones
          Hashtable nodelist = null;
108 706 bojilova
          nodelist = queryobj.findDocuments(xml, null, null, useXMLIndex);
109
110 172 jones
          // Print the reulting document listing
111 155 jones
          StringBuffer result = new StringBuffer();
112
          String document = null;
113 170 jones
          String docid = null;
114 155 jones
          result.append("<?xml version=\"1.0\"?>\n");
115 296 higgins
          result.append("<resultset>\n");
116 940 tao
117 743 jones
          if (!showRuntime)
118 710 berkley
          {
119
            Enumeration doclist = nodelist.keys();
120
            while (doclist.hasMoreElements()) {
121
              docid = (String)doclist.nextElement();
122
              document = (String)nodelist.get(docid);
123
              result.append("  <document>\n    " + document +
124
                            "\n  </document>\n");
125
            }
126
127
            result.append("</resultset>\n");
128 155 jones
          }
129 706 bojilova
          // Time the request if asked for
130
          double stopTime = System.currentTimeMillis();
131 705 berkley
          double dbOpenTime = (connTime - startTime)/1000;
132 706 bojilova
          double readTime = (stopTime - connTime)/1000;
133 705 berkley
          double executionTime = (stopTime - startTime)/1000;
134 706 bojilova
          if (showRuntime) {
135 710 berkley
            System.out.print("  " + executionTime);
136
            System.out.print("  " + dbOpenTime);
137
            System.out.print("  " + readTime);
138
            System.out.print("  " + nodelist.size());
139
            System.out.println();
140 706 bojilova
          }
141
          //System.out.println(result);
142
          //write into a file "result.txt"
143 743 jones
          if (!showRuntime)
144 710 berkley
          {
145
            File f = new File("./result.txt");
146
            FileWriter fw = new FileWriter(f);
147
            BufferedWriter out = new BufferedWriter(fw);
148
            out.write(result.toString());
149
            out.flush();
150
            out.close();
151
            fw.close();
152
          }
153
154
        }
155
        catch (Exception e) {
156 675 berkley
          System.err.println("Error in DBQuery.main");
157 155 jones
          System.err.println(e.getMessage());
158
          e.printStackTrace(System.err);
159
        }
160
     }
161
  }
162
163
  /**
164
   * construct an instance of the DBQuery class
165
   *
166
   * <p>Generally, one would call the findDocuments() routine after creating
167
   * an instance to specify the search query</p>
168
   *
169
   * @param conn the JDBC connection that we use for the query
170 172 jones
   * @param parserName the fully qualified name of a Java class implementing
171 185 jones
   *                   the org.xml.sax.XMLReader interface
172 155 jones
   */
173 172 jones
  public DBQuery( Connection conn, String parserName )
174 155 jones
                  throws IOException,
175
                         SQLException,
176 172 jones
                         ClassNotFoundException {
177 155 jones
    this.conn = conn;
178 172 jones
    this.parserName = parserName;
179 155 jones
  }
180
181 745 jones
  /**
182
   * routine to search the elements and attributes looking to match query
183
   *
184
   * @param xmlquery the xml serialization of the query (@see pathquery.dtd)
185
   * @param user the username of the user
186
   * @param group the group of the user
187
   */
188 802 bojilova
  public Hashtable findDocuments(Reader xmlquery, String user, String[] groups)
189 465 berkley
  {
190 802 bojilova
    return findDocuments(xmlquery, user, groups, true);
191 465 berkley
  }
192 706 bojilova
193 155 jones
  /**
194
   * routine to search the elements and attributes looking to match query
195
   *
196 178 jones
   * @param xmlquery the xml serialization of the query (@see pathquery.dtd)
197 465 berkley
   * @param user the username of the user
198
   * @param group the group of the user
199 745 jones
   * @param useXMLIndex flag whether to search using the path index
200 155 jones
   */
201 802 bojilova
  public Hashtable findDocuments(Reader xmlquery, String user, String[] groups,
202 745 jones
                                 boolean useXMLIndex)
203 453 berkley
  {
204 535 jones
      Hashtable   docListResult = new Hashtable();
205 667 berkley
      PreparedStatement pstmt = null;
206 170 jones
      String docid = null;
207 155 jones
      String docname = null;
208
      String doctype = null;
209 401 berkley
      String createDate = null;
210
      String updateDate = null;
211
      String fieldname = null;
212
      String fielddata = null;
213 453 berkley
      String relation = null;
214 667 berkley
      Connection dbconn = null;
215 766 bojilova
      Connection dbconn2 = null;
216 624 berkley
      int rev = 0;
217 155 jones
      StringBuffer document = null;
218 465 berkley
219 155 jones
      try {
220 743 jones
        if (conn == null || conn.isClosed()) {
221 710 berkley
          dbconn = util.openDBConnection();
222 743 jones
        } else {
223 710 berkley
          dbconn = conn;
224
        }
225 766 bojilova
        // problem with ODBC driver multi-threading
226 790 bojilova
     //   dbconn2 = util.openDBConnection(); // for use by AccessControlList
227 766 bojilova
228 172 jones
        // Get the XML query and covert it into a SQL statment
229 178 jones
        QuerySpecification qspec = new QuerySpecification(xmlquery,
230 535 jones
                                   parserName,
231 624 berkley
                                   util.getOption("accNumSeparator"));
232 790 bojilova
  //System.out.println(qspec.printSQL(useXMLIndex));
233 706 bojilova
        pstmt = dbconn.prepareStatement( qspec.printSQL(useXMLIndex) );
234 790 bojilova
235 172 jones
        // Execute the SQL query using the JDBC connection
236 155 jones
        pstmt.execute();
237
        ResultSet rs = pstmt.getResultSet();
238
        boolean tableHasRows = rs.next();
239 667 berkley
        while (tableHasRows)
240
        {
241 768 bojilova
          docid = rs.getString(1).trim();
242 802 bojilova
          if ( !hasPermission(dbconn, user, groups, docid) ) {
243 612 bojilova
            // Advance to the next record in the cursor
244
            tableHasRows = rs.next();
245
            continue;
246
          }
247 155 jones
          docname = rs.getString(2);
248
          doctype = rs.getString(3);
249 692 bojilova
          createDate = rs.getString(4);
250
          updateDate = rs.getString(5);
251
          rev = rs.getInt(6);
252 743 jones
253 745 jones
          // if there are returndocs to match, backtracking can be performed
254
          // otherwise, just return the document that was hit
255
          Vector returndocVec = qspec.getReturnDocList();
256 743 jones
          if (returndocVec.size() != 0 && !returndocVec.contains(doctype))
257
          {
258 745 jones
            MetaCatUtil.debugMessage("Back tracing now...");
259 743 jones
            String sep = util.getOption("accNumSeparator");
260 465 berkley
            StringBuffer btBuf = new StringBuffer();
261 743 jones
            btBuf.append("select docid from xml_relation where ");
262
263 465 berkley
            //build the doctype list for the backtracking sql statement
264 743 jones
            btBuf.append("packagetype in (");
265 465 berkley
            for(int i=0; i<returndocVec.size(); i++)
266
            {
267
              btBuf.append("'").append((String)returndocVec.get(i)).append("'");
268 743 jones
              if (i != (returndocVec.size() - 1))
269 465 berkley
              {
270
                btBuf.append(", ");
271 475 berkley
              }
272 465 berkley
            }
273
            btBuf.append(") ");
274 743 jones
275
            btBuf.append("and (subject like '");
276
            btBuf.append(docid).append(sep).append(rev).append("'");
277
            btBuf.append("or object like '");
278
            btBuf.append(docid).append(sep).append(rev).append("')");
279 667 berkley
280 743 jones
            PreparedStatement npstmt = dbconn.
281
                                       prepareStatement(btBuf.toString());
282 671 berkley
            npstmt.execute();
283
            ResultSet btrs = npstmt.getResultSet();
284 465 berkley
            boolean hasBtRows = btrs.next();
285 743 jones
            while (hasBtRows)
286 465 berkley
            { //there was a backtrackable document found
287
              DocumentImpl xmldoc = null;
288 743 jones
              String packageDocid = btrs.getString(1);
289 800 jones
              util.debugMessage("Getting document for docid: " + packageDocid);
290 465 berkley
              try
291
              {
292 800 jones
                //  THIS CONSTRUCTOR BUILDS THE WHOLE XML doc not needed here
293
                // xmldoc = new DocumentImpl(dbconn, packageDocid);
294
                //  thus use the following to get the doc info only
295
                //  xmldoc = new DocumentImpl(dbconn);
296
                xmldoc = new DocumentImpl(dbconn, packageDocid, false);
297
                if (xmldoc == null) {
298
                  util.debugMessage("Document was null for: " + packageDocid);
299
                }
300 465 berkley
              }
301
              catch(Exception e)
302
              {
303 675 berkley
                System.out.println("Error getting document in " +
304
                                   "DBQuery.findDocuments: " + e.getMessage());
305 465 berkley
              }
306
307 800 jones
              String docid_org = xmldoc.getDocID();
308
              if (docid_org == null) {
309
                util.debugMessage("Docid_org was null.");
310
              }
311
              docid   = docid_org.trim();
312 465 berkley
              docname = xmldoc.getDocname();
313
              doctype = xmldoc.getDoctype();
314
              createDate = xmldoc.getCreateDate();
315
              updateDate = xmldoc.getUpdateDate();
316 743 jones
              rev = xmldoc.getRev();
317
318
              document = new StringBuffer();
319
320
              String completeDocid = docid + util.getOption("accNumSeparator");
321
              completeDocid += rev;
322
              document.append("<docid>").append(completeDocid);
323
              document.append("</docid>");
324
              if (docname != null) {
325
                document.append("<docname>" + docname + "</docname>");
326
              }
327
              if (doctype != null) {
328
                document.append("<doctype>" + doctype + "</doctype>");
329
              }
330
              if (createDate != null) {
331
                document.append("<createdate>" + createDate + "</createdate>");
332
              }
333
              if (updateDate != null) {
334
                document.append("<updatedate>" + updateDate + "</updatedate>");
335
              }
336
              // Store the document id and the root node id
337
              docListResult.put(docid,(String)document.toString());
338
339
              // Get the next package document linked to our hit
340
              hasBtRows = btrs.next();
341 465 berkley
            }
342 671 berkley
            npstmt.close();
343 465 berkley
            btrs.close();
344 743 jones
          } else {
345 465 berkley
346 743 jones
            document = new StringBuffer();
347
348 624 berkley
            String completeDocid = docid + util.getOption("accNumSeparator");
349
            completeDocid += rev;
350
            document.append("<docid>").append(completeDocid).append("</docid>");
351 465 berkley
            if (docname != null) {
352
              document.append("<docname>" + docname + "</docname>");
353
            }
354
            if (doctype != null) {
355
              document.append("<doctype>" + doctype + "</doctype>");
356
            }
357 743 jones
            if (createDate != null) {
358 465 berkley
              document.append("<createdate>" + createDate + "</createdate>");
359
            }
360 743 jones
            if (updateDate != null) {
361 465 berkley
              document.append("<updatedate>" + updateDate + "</updatedate>");
362
            }
363
            // Store the document id and the root node id
364
            docListResult.put(docid,(String)document.toString());
365 743 jones
366 155 jones
          }
367
368
          // Advance to the next record in the cursor
369
          tableHasRows = rs.next();
370
        }
371 667 berkley
        rs.close();
372 818 berkley
        pstmt.close();
373 401 berkley
374 743 jones
        if (qspec.containsExtendedSQL())
375 401 berkley
        {
376
          Vector extendedFields = new Vector(qspec.getReturnFieldList());
377
          Vector results = new Vector();
378 465 berkley
          Enumeration keylist = docListResult.keys();
379
          StringBuffer doclist = new StringBuffer();
380
          while(keylist.hasMoreElements())
381
          {
382
            doclist.append("'");
383
            doclist.append((String)keylist.nextElement());
384
            doclist.append("',");
385
          }
386 834 jones
          if (doclist.length() > 0) {
387
            doclist.deleteCharAt(doclist.length()-1); //remove the last comma
388
            //pstmt.close();
389
            pstmt = dbconn.prepareStatement(qspec.printExtendedSQL(
390 465 berkley
                                        doclist.toString()));
391 834 jones
            pstmt.execute();
392
            rs = pstmt.getResultSet();
393 401 berkley
            tableHasRows = rs.next();
394 834 jones
            while(tableHasRows)
395 401 berkley
            {
396 834 jones
              docid = rs.getString(1).trim();
397
              if ( !hasPermission(dbconn, user, groups, docid) ) {
398
                // Advance to the next record in the cursor
399
                tableHasRows = rs.next();
400
                continue;
401
              }
402
              fieldname = rs.getString(2);
403
              fielddata = rs.getString(3);
404
405
              document = new StringBuffer();
406
407
              document.append("<param name=\"");
408
              document.append(fieldname);
409
              document.append("\">");
410
              document.append(fielddata);
411
              document.append("</param>");
412
413
              tableHasRows = rs.next();
414
              if (docListResult.containsKey(docid))
415
              {
416
                String removedelement = (String)docListResult.remove(docid);
417
                docListResult.put(docid, removedelement + document.toString());
418
              }
419
              else
420
              {
421
                docListResult.put(docid, document.toString());
422
              }
423 401 berkley
            }
424
          }
425 667 berkley
          rs.close();
426 401 berkley
        }
427 818 berkley
        pstmt.close();
428
429 465 berkley
        //this loop adds the relation data to the resultdoc
430
        //this code might be able to be added to the backtracking code above
431
        Enumeration docidkeys = docListResult.keys();
432
        while(docidkeys.hasMoreElements())
433 453 berkley
        {
434 602 berkley
          //String connstring = "metacat://"+util.getOption("server")+"?docid=";
435
          String connstring = "%docid=";
436 465 berkley
          String docidkey = (String)docidkeys.nextElement();
437 743 jones
          pstmt = dbconn.prepareStatement(qspec.printRelationSQL(docidkey));
438 465 berkley
          pstmt.execute();
439
          rs = pstmt.getResultSet();
440
          tableHasRows = rs.next();
441
          while(tableHasRows)
442
          {
443
            String sub = rs.getString(1);
444
            String rel = rs.getString(2);
445
            String obj = rs.getString(3);
446 489 berkley
            String subDT = rs.getString(4);
447
            String objDT = rs.getString(5);
448
449 894 berkley
            document = new StringBuffer();
450
            document.append("<triple>");
451
            document.append("<subject>").append(MetaCatUtil.normalize(sub));
452
            document.append("</subject>");
453
            if ( subDT != null ) {
454
              document.append("<subjectdoctype>").append(subDT);
455
              document.append("</subjectdoctype>");
456
            }
457 940 tao
            document.append("<relationship>").
458
                                          append(MetaCatUtil.normalize(rel));
459 894 berkley
            document.append("</relationship>");
460
            document.append("<object>").append(MetaCatUtil.normalize(obj));
461
            document.append("</object>");
462
            if ( objDT != null ) {
463
              document.append("<objectdoctype>").append(objDT);
464
              document.append("</objectdoctype>");
465
            }
466
            document.append("</triple>");
467
468
            String removedelement = (String)docListResult.remove(docidkey);
469
            docListResult.put(docidkey, removedelement +
470
                              document.toString());
471 465 berkley
            tableHasRows = rs.next();
472 453 berkley
          }
473 667 berkley
          rs.close();
474
          pstmt.close();
475 453 berkley
        }
476 667 berkley
477 155 jones
      } catch (SQLException e) {
478 667 berkley
        System.err.println("SQL Error in DBQuery.findDocuments: " +
479
                           e.getMessage());
480 170 jones
      } catch (IOException ioe) {
481 675 berkley
        System.err.println("IO error in DBQuery.findDocuments:");
482 170 jones
        System.err.println(ioe.getMessage());
483 667 berkley
      } catch (Exception ee) {
484 800 jones
        System.err.println("Exception in DBQuery.findDocuments: " +
485 667 berkley
                           ee.getMessage());
486 800 jones
        ee.printStackTrace(System.err);
487 155 jones
      }
488 667 berkley
      finally {
489
        try
490
        {
491
          dbconn.close();
492 790 bojilova
        //  dbconn2.close();
493 667 berkley
        }
494
        catch(SQLException sqle)
495
        {
496
          System.out.println("error closing conn in DBQuery.findDocuments");
497
        }
498
      }
499 423 berkley
    //System.out.println("docListResult: ");
500
    //System.out.println(docListResult.toString());
501 155 jones
    return docListResult;
502
  }
503 342 berkley
504
  /**
505 436 berkley
   * returns a string array of the contents of a particular node.
506
   * If the node appears more than once, the contents are returned
507
   * in the order in which they appearred in the document.
508
   * @param nodename the name or path of the particular node.
509
   * @param docid the docid of the document you want the node from.
510
   * @param conn a database connection-this allows this method to be static
511
   */
512
  public static Object[] getNodeContent(String nodename, String docid,
513
                                        Connection conn)
514
  {
515
    StringBuffer query = new StringBuffer();
516
    Vector result = new Vector();
517 667 berkley
    PreparedStatement pstmt = null;
518 436 berkley
    query.append("select nodedata from xml_nodes where parentnodeid in ");
519
    query.append("(select nodeid from xml_index where path like '");
520
    query.append(nodename);
521
    query.append("' and docid like '").append(docid).append("')");
522
    try
523
    {
524
      pstmt = conn.prepareStatement(query.toString());
525
526
      // Execute the SQL query using the JDBC connection
527
      pstmt.execute();
528
      ResultSet rs = pstmt.getResultSet();
529
      boolean tableHasRows = rs.next();
530
      while (tableHasRows)
531
      {
532
        result.add(rs.getString(1));
533
        System.out.println(rs.getString(1));
534
        tableHasRows = rs.next();
535
      }
536
    }
537
    catch (SQLException e)
538
    {
539 675 berkley
      System.err.println("Error in DBQuery.getNodeContent: " + e.getMessage());
540 667 berkley
    } finally {
541
      try
542
      {
543
        pstmt.close();
544
      }
545
      catch(SQLException sqle) {}
546
    }
547 436 berkley
    return result.toArray();
548
  }
549
550
  /**
551 342 berkley
   * format a structured query as an XML document that conforms
552
   * to the pathquery.dtd and is appropriate for submission to the DBQuery
553
   * structured query engine
554
   *
555 743 jones
   * @param params The list of parameters that should be included in the query
556 342 berkley
   */
557 372 berkley
  public static String createSQuery(Hashtable params)
558 350 berkley
  {
559
    StringBuffer query = new StringBuffer();
560 342 berkley
    Enumeration elements;
561
    Enumeration keys;
562 743 jones
    String filterDoctype = null;
563 372 berkley
    String casesensitive = null;
564
    String searchmode = null;
565 342 berkley
    Object nextkey;
566
    Object nextelement;
567 350 berkley
    //add the xml headers
568
    query.append("<?xml version=\"1.0\"?>\n");
569 743 jones
    query.append("<pathquery version=\"1.0\">\n");
570
571
    if (params.containsKey("meta_file_id"))
572 342 berkley
    {
573 743 jones
      query.append("<meta_file_id>");
574 342 berkley
      query.append( ((String[])params.get("meta_file_id"))[0]);
575 535 jones
      query.append("</meta_file_id>");
576 342 berkley
    }
577 350 berkley
578 743 jones
    if (params.containsKey("returndoctype"))
579 372 berkley
    {
580 744 jones
      String[] returnDoctypes = ((String[])params.get("returndoctype"));
581
      for(int i=0; i<returnDoctypes.length; i++)
582
      {
583
        String doctype = (String)returnDoctypes[i];
584
585
        if (!doctype.equals("any") &&
586
            !doctype.equals("ANY") &&
587
            !doctype.equals("") )
588
        {
589
          query.append("<returndoctype>").append(doctype);
590
          query.append("</returndoctype>");
591
        }
592
      }
593 372 berkley
    }
594 744 jones
595 743 jones
    if (params.containsKey("filterdoctype"))
596
    {
597
      String[] filterDoctypes = ((String[])params.get("filterdoctype"));
598
      for(int i=0; i<filterDoctypes.length; i++)
599
      {
600
        query.append("<filterdoctype>").append(filterDoctypes[i]);
601
        query.append("</filterdoctype>");
602
      }
603
    }
604 372 berkley
605 743 jones
    if (params.containsKey("returnfield"))
606 401 berkley
    {
607
      String[] returnfield = ((String[])params.get("returnfield"));
608
      for(int i=0; i<returnfield.length; i++)
609
      {
610
        query.append("<returnfield>").append(returnfield[i]);
611
        query.append("</returnfield>");
612
      }
613
    }
614
615 743 jones
    if (params.containsKey("owner"))
616 535 jones
    {
617
      String[] owner = ((String[])params.get("owner"));
618
      for(int i=0; i<owner.length; i++)
619
      {
620
        query.append("<owner>").append(owner[i]);
621
        query.append("</owner>");
622
      }
623
    }
624
625 743 jones
    if (params.containsKey("site"))
626 535 jones
    {
627
      String[] site = ((String[])params.get("site"));
628
      for(int i=0; i<site.length; i++)
629
      {
630
        query.append("<site>").append(site[i]);
631
        query.append("</site>");
632
      }
633
    }
634
635 350 berkley
    //allows the dynamic switching of boolean operators
636 743 jones
    if (params.containsKey("operator"))
637 350 berkley
    {
638
      query.append("<querygroup operator=\"" +
639 535 jones
                ((String[])params.get("operator"))[0] + "\">");
640 350 berkley
    }
641
    else
642
    { //the default operator is UNION
643
      query.append("<querygroup operator=\"UNION\">");
644
    }
645 535 jones
646 743 jones
    if (params.containsKey("casesensitive"))
647 372 berkley
    {
648
      casesensitive = ((String[])params.get("casesensitive"))[0];
649
    }
650
    else
651
    {
652
      casesensitive = "false";
653
    }
654
655 743 jones
    if (params.containsKey("searchmode"))
656 372 berkley
    {
657
      searchmode = ((String[])params.get("searchmode"))[0];
658
    }
659
    else
660
    {
661
      searchmode = "contains";
662
    }
663 535 jones
664 342 berkley
    //anyfield is a special case because it does a
665
    //free text search.  It does not have a <pathexpr>
666 350 berkley
    //tag.  This allows for a free text search within the structured
667
    //query.  This is useful if the INTERSECT operator is used.
668 743 jones
    if (params.containsKey("anyfield"))
669 342 berkley
    {
670 372 berkley
       String[] anyfield = ((String[])params.get("anyfield"));
671
       //allow for more than one value for anyfield
672
       for(int i=0; i<anyfield.length; i++)
673 350 berkley
       {
674 743 jones
         if (!anyfield[i].equals(""))
675 372 berkley
         {
676
           query.append("<queryterm casesensitive=\"" + casesensitive +
677
                        "\" " + "searchmode=\"" + searchmode + "\"><value>" +
678 535 jones
                        anyfield[i] +
679
                        "</value></queryterm>");
680 372 berkley
         }
681 350 berkley
       }
682 342 berkley
    }
683 535 jones
684 342 berkley
    //this while loop finds the rest of the parameters
685
    //and attempts to query for the field specified
686
    //by the parameter.
687
    elements = params.elements();
688
    keys = params.keys();
689
    while(keys.hasMoreElements() && elements.hasMoreElements())
690
    {
691
      nextkey = keys.nextElement();
692 535 jones
      nextelement = elements.nextElement();
693 372 berkley
694 535 jones
      //make sure we aren't querying for any of these
695
      //parameters since the are already in the query
696 342 berkley
      //in one form or another.
697 743 jones
      if (!nextkey.toString().equals("returndoctype") &&
698
         !nextkey.toString().equals("filterdoctype")  &&
699 535 jones
         !nextkey.toString().equals("action")  &&
700
         !nextkey.toString().equals("qformat") &&
701
         !nextkey.toString().equals("anyfield") &&
702 401 berkley
         !nextkey.toString().equals("returnfield") &&
703 535 jones
         !nextkey.toString().equals("owner") &&
704
         !nextkey.toString().equals("site") &&
705
         !nextkey.toString().equals("operator") )
706
      {
707 372 berkley
        //allow for more than value per field name
708
        for(int i=0; i<((String[])nextelement).length; i++)
709
        {
710 743 jones
          if (!((String[])nextelement)[i].equals(""))
711 372 berkley
          {
712
            query.append("<queryterm casesensitive=\"" + casesensitive +"\" " +
713 535 jones
                         "searchmode=\"" + searchmode + "\">" +
714
                         "<value>" +
715 372 berkley
                         //add the query value
716 535 jones
                         ((String[])nextelement)[i] +
717
                         "</value><pathexpr>" +
718
                         //add the path to query by
719 372 berkley
                         nextkey.toString() +
720
                         "</pathexpr></queryterm>");
721
          }
722
        }
723 535 jones
      }
724 342 berkley
    }
725
    query.append("</querygroup></pathquery>");
726 350 berkley
    //append on the end of the xml and return the result as a string
727 342 berkley
    return query.toString();
728
  }
729
730 181 jones
  /**
731
   * format a simple free-text value query as an XML document that conforms
732
   * to the pathquery.dtd and is appropriate for submission to the DBQuery
733
   * structured query engine
734
   *
735
   * @param value the text string to search for in the xml catalog
736
   * @param doctype the type of documents to include in the result set -- use
737
   *        "any" or "ANY" for unfiltered result sets
738
   */
739
   public static String createQuery(String value, String doctype) {
740
     StringBuffer xmlquery = new StringBuffer();
741
     xmlquery.append("<?xml version=\"1.0\"?>\n");
742
     xmlquery.append("<pathquery version=\"1.0\">");
743
744
     if (!doctype.equals("any") && !doctype.equals("ANY")) {
745
       xmlquery.append("<returndoctype>");
746
       xmlquery.append(doctype).append("</returndoctype>");
747
     }
748
749
     xmlquery.append("<querygroup operator=\"UNION\">");
750 350 berkley
     //chad added - 8/14
751
     //the if statement allows a query to gracefully handle a null
752
     //query.  Without this if a nullpointerException is thrown.
753 743 jones
     if (!value.equals(""))
754 350 berkley
     {
755
       xmlquery.append("<queryterm casesensitive=\"false\" ");
756
       xmlquery.append("searchmode=\"contains\">");
757
       xmlquery.append("<value>").append(value).append("</value>");
758
       xmlquery.append("</queryterm>");
759
     }
760 181 jones
     xmlquery.append("</querygroup>");
761
     xmlquery.append("</pathquery>");
762
763
764
     return (xmlquery.toString());
765
   }
766
767
  /**
768
   * format a simple free-text value query as an XML document that conforms
769
   * to the pathquery.dtd and is appropriate for submission to the DBQuery
770
   * structured query engine
771
   *
772
   * @param value the text string to search for in the xml catalog
773
   */
774
   public static String createQuery(String value) {
775
     return createQuery(value, "any");
776
   }
777 441 bojilova
778 570 bojilova
  /**
779
    * Check for "READ" permission on @docid for @user and/or @group
780
    * from DB connection
781
    */
782
  private boolean hasPermission ( Connection conn, String user,
783 802 bojilova
                                  String[] groups, String docid )
784 957 tao
                  throws SQLException, Exception
785 570 bojilova
  {
786 802 bojilova
    // Check for READ permission on @docid for @user and/or @groups
787 607 bojilova
    AccessControlList aclobj = new AccessControlList(conn);
788 802 bojilova
    return aclobj.hasPermission("READ", user, groups, docid);
789 441 bojilova
  }
790 940 tao
791
  /**
792
    * Get all docIds list for a data packadge
793
    * @param dataPackageDocid, the string in docId field of xml_relation table
794
    */
795
  private Vector getCurrentDocidListForDataPackage(String dataPackageDocid)
796
  {
797
    Vector docIdList=new Vector();//return value
798
    PreparedStatement pStmt;
799
    ResultSet rs=null;
800
    String docIdInSubjectField=null;
801
    String docIdInObjectField=null;
802
    //the query stirng
803
    String query="SELECT subject, object from xml_relation where docId = ?";
804
    try
805
    {
806
      pStmt=conn.prepareStatement(query);
807
      //bind the value to query
808
      pStmt.setString(1, dataPackageDocid);
809
810
      //excute the query
811
      pStmt.execute();
812
      //get the result set
813
      rs=pStmt.getResultSet();
814
      //process the result
815
      while (rs.next())
816
      {
817
        //In order to get the whole docIds in a data packadge,
818
        //we need to put the docIds of subject and object field in xml_relation
819
        //into the return vector
820
        docIdInSubjectField=rs.getString(1);//the result docId in subject field
821
        docIdInObjectField=rs.getString(2);//the result docId in object field
822
823
        //don't put the duplicate docId into the vector
824
        if (!docIdList.contains(docIdInSubjectField))
825
        {
826
          docIdList.add(docIdInSubjectField);
827
        }
828
829
        //don't put the duplicate docId into the vector
830
        if (!docIdList.contains(docIdInObjectField))
831
        {
832
          docIdList.add(docIdInObjectField);
833
        }
834
      }//while
835
      //close the pStmt
836
      pStmt.close();
837
    }//try
838
    catch (SQLException e)
839
    {
840
      util.debugMessage("Error in getDocidListForDataPackage: "
841
                            +e.getMessage());
842
    }//catch
843
    return docIdList;
844
  }//getCurrentDocidListForDataPackadge()
845
846
  /**
847
   * Get all docIds list for a data packadge
848
   * @param dataPackageDocid, the string in docId field of xml_relation table
849
   */
850
  private Vector getOldVersionDocidListForDataPackage(String dataPackageDocid)
851
                                 throws SQLException, McdbException,Exception
852
  {
853 441 bojilova
854 940 tao
    Vector docIdList=new Vector();//return value
855
    Vector tripleList=null;
856
    String xml=null;
857
    Connection dbConn=null;
858
859
    if (conn == null || conn.isClosed())
860
    {
861
      dbConn = util.openDBConnection();
862
    }
863
    else
864
    {
865
      dbConn=conn;
866
    }
867
    //initial a documentImpl object
868
    DocumentImpl packageDocument = new DocumentImpl(dbConn, dataPackageDocid);
869
    //transfer to documentImpl object to string
870
    xml=packageDocument.toString();
871
872
    //create a tripcollection object
873
    TripleCollection tripleForPackage = new
874
                                     TripleCollection(new StringReader(xml));
875
    //get the vetor of triples
876
    tripleList=tripleForPackage.getCollection();
877
878
    for (int i= 0; i<tripleList.size(); i++)
879
    {
880
      //put subject docid  into docIdlist without duplicate
881
      if (!docIdList.contains(((Triple)tripleList.elementAt(i)).getSubject()))
882
      {
883
        //put subject docid  into docIdlist
884
         docIdList.add(((Triple)tripleList.get(i)).getSubject());
885
      }
886
      //put object docid into docIdlist without duplicate
887
      if (!docIdList.contains(((Triple)tripleList.elementAt(i)).getObject()))
888
      {
889
         docIdList.add(((Triple)(tripleList.get(i))).getObject());
890
      }
891
    }//for
892
893
    return docIdList;
894
  }//getDocidListForPackageInXMLRevisions()
895
896
  /**
897
   * Check if the docId is a data packadge id. If the id is a data packadage
898
   *id, it should be store in the docId fields in xml_relation table.
899
   *So we can use a query to get the entries which the docId equals the given
900
   *value. If the result is null. The docId is not a packadge id. Otherwise,
901
   * it is.
902
   * @param docId, the id need to be checked
903
   */
904
  private boolean isDataPackageId(String docId)
905
  {
906
    boolean result=false;
907
    PreparedStatement pStmt;
908
    ResultSet rs=null;
909
    String query="SELECT docId from xml_relation where docId = ?";
910
    try
911
    {
912
      pStmt=conn.prepareStatement(query);
913
      //bind the value to query
914
      pStmt.setString(1, docId);
915
      //execute the query
916
      pStmt.execute();
917
      rs=pStmt.getResultSet();
918
      //process the result
919
      if (rs.next()) //There are some records for the id in docId fields
920
      {
921
        result=true;//It is a data packadge id
922
      }
923
      pStmt.close();
924
    }//try
925
    catch (SQLException e)
926
    {
927
      util.debugMessage("Error in getDocidListForDataPackadge: "
928
                            +e.getMessage());
929
    }
930
    return result;
931
  }//isDataPackageId()
932
933
  /**
934 945 tao
   * Check if the user has the permission to export data package
935
   * @param conn, the connection
936
   * @param docId, the id need to be checked
937
   * @param user, the name of user
938
   * @param groups, the user's group
939
   */
940
   private boolean hasPermissionToExportPackage(Connection conn, String docId,
941
                                        String user, String[] groups)
942
                   throws Exception
943
   {
944
     DocumentImpl doc=new DocumentImpl(conn,docId);
945
     return doc.hasReadPermission(conn, user, groups,docId);
946
   }
947
948
  /**
949 940 tao
   *Get the current Rev for a docid in xml_documents table
950
   * @param docId, the id need to get version numb
951
   * If the return value is -5, means no value in rev field for this docid
952
   */
953
  private int getCurrentRevFromXMLDoumentsTable(String docId)
954
  {
955
    int rev=-5;
956
    PreparedStatement pStmt;
957
    ResultSet rs=null;
958
    String query="SELECT rev from xml_documents where docId = ?";
959
    try
960
    {
961
      pStmt=conn.prepareStatement(query);
962
      //bind the value to query
963
      pStmt.setString(1, docId);
964
      //execute the query
965
      pStmt.execute();
966
      rs=pStmt.getResultSet();
967
      //process the result
968
      if (rs.next()) //There are some records for rev
969
      {
970
        rev=rs.getInt(1);;//It is the version for given docid
971
      }
972
      else
973
      {
974
        rev=-5;
975
      }
976
      pStmt.close();
977
    }//try
978
    catch (SQLException e)
979
    {
980
      util.debugMessage("Error in getDocidListForDataPackadge: "
981
                            +e.getMessage());
982
    }
983
    return rev;
984
  }//getCurrentRevFromXMLDoumentsTable
985
986
 /**
987
   *put a doc into a zip output stream
988
   *@param docImpl, docmentImpl object which will be sent to zip output stream
989
   *@param zipOut, zip output stream which the docImpl will be put
990
   *@param packageZipEntry, the zip entry name for whole package
991
   */
992
  private void addDocToZipOutputStream(DocumentImpl docImpl,
993
                                ZipOutputStream zipOut, String packageZipEntry)
994
               throws ClassNotFoundException, IOException, SQLException,
995
                      McdbException, Exception
996
  {
997
    byte[] byteString = null;
998
    ZipEntry zEntry = null;
999
1000
    byteString = docImpl.toString().getBytes();
1001
    //use docId as the zip entry's name
1002
    zEntry = new ZipEntry(packageZipEntry+"/metadata/"+docImpl.getDocID());
1003
    zEntry.setSize(byteString.length);
1004
    zipOut.putNextEntry(zEntry);
1005
    zipOut.write(byteString, 0, byteString.length);
1006
    zipOut.closeEntry();
1007
1008
  }//addDocToZipOutputStream()
1009
1010
1011
  /**
1012
   *transfer a docid vetor to a documentImpl vector. The documentImpl vetor
1013 948 tao
   *only inlcudes current version
1014 940 tao
   *@param docIdList, a vetor hold a docid list for a data package. In docid,
1015
   *there is not version number in it.
1016
   */
1017
1018
  private Vector getCurrentAllDocumentImpl( Vector docIdList)
1019
                              throws McdbException,Exception
1020
  {
1021
    Connection dbConn=null;
1022
    Vector documentImplList=new Vector();
1023
    int rev=0;
1024
1025
    if (conn == null || conn.isClosed())
1026
    {
1027
      dbConn = util.openDBConnection();
1028
    }
1029
    else
1030
    {
1031
      dbConn=conn;
1032
    }
1033
    //for every docid in vector
1034
    for (int i=0;i<docIdList.size();i++)
1035
    {
1036
      //get newest version for this docId
1037
      rev=getCurrentRevFromXMLDoumentsTable((String)docIdList.elementAt(i));
1038 948 tao
1039
      String docidPlusVersion=((String)docIdList.elementAt(i))
1040
                        +util.getOption("accNumSeparator")+rev;
1041
      //create new documentImpl object
1042
      DocumentImpl documentImplObject =
1043 940 tao
                                    new DocumentImpl(dbConn,docidPlusVersion);
1044 948 tao
      //add them to vector
1045
      documentImplList.add(documentImplObject);
1046
1047 940 tao
    }//for
1048
    return documentImplList;
1049
  }
1050
1051
  /**
1052
   *transfer a docid vetor to a documentImpl vector. The documentImpl vetor
1053 948 tao
   *does not inlcude old version
1054 940 tao
   *@param docIdList, a vetor hold a docid list for a data package. In docid,
1055
   *there is version number in it.
1056
   */
1057
  private Vector getOldVersionAllDocumentImpl( Vector docIdList)
1058
                              throws McdbException,Exception
1059
  {
1060
    Connection dbConn=null;
1061
    Vector documentImplList=new Vector();
1062
    String siteCode=null;
1063
    String uniqueId=null;
1064
    int rev=0;
1065
1066
    if (conn == null || conn.isClosed())
1067
    {
1068
      dbConn = util.openDBConnection();
1069
    }
1070
    else
1071
    {
1072
      dbConn=conn;
1073
    }
1074
    //for every docid in vector
1075
    for (int i=0;i<docIdList.size();i++)
1076
    {
1077
1078 948 tao
        String docidPlusVersion=(String)(docIdList.elementAt(i));
1079 940 tao
        //create new documentImpl object
1080
        DocumentImpl documentImplObject =
1081
                                    new DocumentImpl(dbConn,docidPlusVersion);
1082
        //add them to vector
1083
        documentImplList.add(documentImplObject);
1084 948 tao
1085 940 tao
    }//for
1086
    return documentImplList;
1087
  }
1088
  /**
1089
   *put a data file into a zip output stream
1090
   *@param docImpl, docmentImpl object which will be sent to zip output stream
1091
   *@param zipOut, the zip output stream which the docImpl will be put
1092
   *@param packageZipEntry, the zip entry name for whole package
1093
   */
1094
  private void addDataFileToZipOutputStream(DocumentImpl docImpl,
1095
                                ZipOutputStream zipOut, String packageZipEntry)
1096
               throws ClassNotFoundException, IOException, SQLException,
1097
                      McdbException, Exception
1098
  {
1099
    byte[] byteString = null;
1100
    ZipEntry zEntry = null;
1101
    // this is data file; add file to zip
1102
    String filePath = util.getOption("datafilepath");
1103
    if (!filePath.endsWith("/"))
1104
    {
1105
      filePath += "/";
1106
    }
1107
    String fileName = filePath + docImpl.getDocID();
1108 963 berkley
    zEntry = new ZipEntry(packageZipEntry+"/data/"+docImpl.getDocID());
1109 940 tao
    zipOut.putNextEntry(zEntry);
1110
    FileInputStream fin = null;
1111
    try
1112
    {
1113
      fin = new FileInputStream(fileName);
1114
      byte[] buf = new byte[4 * 1024]; // 4K buffer
1115
      int b = fin.read(buf);
1116
      while (b != -1)
1117
      {
1118
        zipOut.write(buf, 0, b);
1119
        b = fin.read(buf);
1120
      }//while
1121
      zipOut.closeEntry();
1122
    }//try
1123
    catch (IOException ioe)
1124
    {
1125
      util.debugMessage("There is an exception: "+ioe.getMessage());
1126
    }//catch
1127
  }//addDataFileToZipOutputStream()
1128
1129
  /**
1130
   *create a html summary for data package and put it into zip output stream
1131
   *@param docImplList, the documentImpl ojbects in data package
1132
   *@param zipOut, the zip output stream which the html should be put
1133
   *@param packageZipEntry, the zip entry name for whole package
1134
   */
1135
   private void addHtmlSummaryToZipOutputStream(Vector docImplList,
1136
                                ZipOutputStream zipOut, String packageZipEntry)
1137
                                           throws Exception
1138
  {
1139
    StringBuffer htmlDoc = new StringBuffer();
1140
    ZipEntry zEntry = null;
1141
    byte[] byteString=null;
1142
    InputStream source;
1143
    DBTransform xmlToHtml;
1144
    Connection dbConn;
1145
1146
    //get the connection to the database
1147
    if (conn == null || conn.isClosed())
1148
    {
1149
      dbConn = util.openDBConnection();
1150
    }
1151
    else
1152
    {
1153
      dbConn=conn;
1154
    }
1155
1156
    //create a DBTransform ojbect
1157
    xmlToHtml = new DBTransform(dbConn);
1158
    //head of html
1159
    htmlDoc.append("<html><head></head><body>");
1160
    for (int i=0; i<docImplList.size(); i++)
1161
    {
1162
      if ((((DocumentImpl)docImplList.elementAt(i)).getDoctype()).
1163
                                                         compareTo("BIN")!=0)
1164
      { //this is an xml file so we can transform it.
1165
        //transform each file individually then concatenate all of the
1166
        //transformations together.
1167
1168
        //for metadata xml title
1169
        htmlDoc.append("<h2>");
1170
        htmlDoc.append(((DocumentImpl)docImplList.elementAt(i)).getDocID());
1171
        //htmlDoc.append(".");
1172
        //htmlDoc.append(((DocumentImpl)docImplList.elementAt(i)).getRev());
1173
        htmlDoc.append("</h2>");
1174
        //do the actual transform
1175
        StringWriter docString = new StringWriter();
1176
        xmlToHtml.transformXMLDocument(
1177
                        ((DocumentImpl)docImplList.elementAt(i)).toString(),
1178
           "-//NCEAS//eml-generic//EN", "-//W3C//HTML//EN", "html", docString);
1179
        htmlDoc.append(docString.toString());
1180
        htmlDoc.append("<br><br><hr><br><br>");
1181
      }//if
1182
      else
1183
      { //this is a data file so we should link to it in the html
1184
        htmlDoc.append("<a href=\"");
1185
        String dataFileName = null;
1186
        String dataFileid =((DocumentImpl)docImplList.elementAt(i)).getDocID();
1187
        htmlDoc.append("./data/").append(dataFileid).append("\">");
1188
        htmlDoc.append("Data File: ");
1189
        htmlDoc.append(dataFileid).append("</a><br>");
1190
        htmlDoc.append("<br><hr><br>");
1191
      }//else
1192
    }//for
1193
    htmlDoc.append("</body></html>");
1194
    byteString = htmlDoc.toString().getBytes();
1195
    zEntry = new ZipEntry(packageZipEntry+"/metadata.html");
1196
    zEntry.setSize(byteString.length);
1197
    zipOut.putNextEntry(zEntry);
1198
    zipOut.write(byteString, 0, byteString.length);
1199
    zipOut.closeEntry();
1200
    dbConn.close();
1201
1202
  }//addHtmlSummaryToZipOutputStream
1203
1204 945 tao
1205
1206 940 tao
  /**
1207
   * put a data packadge into a zip output stream
1208
   * @param docId, which the user want to put into zip output stream
1209
   * @param out, a servletoutput stream which the zip output stream will be put
1210
   * @param user, the username of the user
1211
   * @param groups, the group of the user
1212
   */
1213
  public ZipOutputStream getZippedPackage(String docIdString,
1214
                    ServletOutputStream out, String user, String[] groups)
1215
                    throws ClassNotFoundException, IOException, SQLException,
1216
                      McdbException, NumberFormatException, Exception
1217
  {
1218
    ZipOutputStream zOut = null;
1219
    String elementDocid=null;
1220
    DocumentImpl docImpls=null;
1221
    Connection dbConn = null;
1222
    Vector docIdList=new Vector();
1223 945 tao
    Vector documentImplList=new Vector();
1224
    Vector htmlDocumentImplList=new Vector();
1225 940 tao
    String packageId=null;
1226
    String rootName="package";//the package zip entry name
1227
1228
    String docId=null;
1229
    int version=-5;
1230
    docId=MetaCatUtil.getDocIdFromString(docIdString);
1231
    version=MetaCatUtil.getVersionFromString(docIdString);
1232
1233
    //get the connection to the database
1234
    if (conn == null || conn.isClosed())
1235
    {
1236
      dbConn = util.openDBConnection();
1237
    }
1238
    else
1239
    {
1240
      dbConn=conn;
1241
    }
1242
1243
    //check if the reqused docId is a data package id
1244
    if (!isDataPackageId(docId))//if it is not, throw a exception
1245
    {
1246 945 tao
      Exception e = new Exception("The request the doc id " +docIdString+
1247 940 tao
                                    " is not a data package id");
1248
      dbConn.close();
1249
      throw e;
1250
    }
1251 945 tao
    else if(!hasPermissionToExportPackage(dbConn, docId, user, groups))
1252
    {
1253
      Exception e = new Exception("User " + user + " does not have permission"
1254
                       +" to export the data package " + docIdString);
1255
      dbConn.close();
1256
      throw e;
1257
    }
1258 940 tao
    else //it is a packadge id
1259
    {
1260
      //store the package id
1261
      packageId=docId;
1262
      //If it is for current version
1263
      if ((version ==-1)||version==getCurrentRevFromXMLDoumentsTable(packageId))
1264
      {
1265
        //get current version number
1266
        version=getCurrentRevFromXMLDoumentsTable(packageId);
1267
        //get package zip entry name
1268
        //it should be docId.revsion.package
1269
        rootName=packageId+util.getOption("accNumSeparator")+version+
1270
                                  util.getOption("accNumSeparator")+"package";
1271
        //get the whole id list for data packadge
1272
        docIdList=getCurrentDocidListForDataPackage(packageId);
1273
        //get the whole documentImple object
1274
        documentImplList=getCurrentAllDocumentImpl(docIdList);
1275
1276
      }//if
1277
      else  //for an old version
1278
      {
1279
1280
        rootName=docIdString+util.getOption("accNumSeparator")+"package";
1281
        //get the whole id list for data packadge
1282
        docIdList=getOldVersionDocidListForDataPackage(docIdString);
1283
1284
        //get the whole documentImple object
1285
        documentImplList=getOldVersionAllDocumentImpl(docIdList);
1286
      }//else
1287
1288
1289
      zOut = new ZipOutputStream(out);
1290
1291
      //put every element into zip output stream
1292
      for (int i=0; i < documentImplList.size(); i++ )
1293
      {
1294
        //create a docmentImpls object (represent xml doc) base on the docId
1295
        docImpls=(DocumentImpl)documentImplList.elementAt(i);
1296 948 tao
1297
        //checking if the user has the permission to read the documents
1298
        if (docImpls.hasReadPermission(dbConn,user,groups,docImpls.getDocID()))
1299
        {
1300
            //if the docImpls is metadata
1301
          if ((docImpls.getDoctype()).compareTo("BIN")!=0)
1302
          {
1303
              //add metadata into zip output stream
1304
              addDocToZipOutputStream(docImpls, zOut, rootName);
1305
              //add the documentImpl into the vetor which will be used in html
1306
              htmlDocumentImplList.add(docImpls);
1307 953 tao
1308 948 tao
          }//if
1309 953 tao
1310 948 tao
          else
1311
          {
1312 945 tao
1313
           //it is data file
1314 940 tao
           addDataFileToZipOutputStream(docImpls, zOut, rootName);
1315 945 tao
           htmlDocumentImplList.add(docImpls);
1316 948 tao
          }//else
1317
        }//if
1318 940 tao
1319
      }//for
1320
1321
      //add html summary file
1322 945 tao
      addHtmlSummaryToZipOutputStream(htmlDocumentImplList, zOut, rootName);
1323 940 tao
      zOut.finish(); //terminate the zip file
1324
      dbConn.close();
1325
      return zOut;
1326
    }//else
1327
  }//getZippedPackage()
1328
1329 155 jones
}