Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that searches a relational DB for elements and
4
 *             attributes that have free text matches a query string,
5
 *             or structured query matches to a path specified node in the
6
 *             XML hierarchy.  It returns a result set consisting of the
7
 *             document ID for each document that satisfies the query
8
 *  Copyright: 2000 Regents of the University of California and the
9
 *             National Center for Ecological Analysis and Synthesis
10
 *    Authors: Matt Jones
11
 *
12
 *   '$Author: tao $'
13
 *     '$Date: 2007-08-01 15:51:09 -0700 (Wed, 01 Aug 2007) $'
14
 * '$Revision: 3340 $'
15
 *
16
 * This program is free software; you can redistribute it and/or modify
17
 * it under the terms of the GNU General Public License as published by
18
 * the Free Software Foundation; either version 2 of the License, or
19
 * (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU General Public License
27
 * along with this program; if not, write to the Free Software
28
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29
 */
30

    
31
package edu.ucsb.nceas.metacat;
32

    
33
import java.io.*;
34
import java.util.zip.*;
35
import java.sql.PreparedStatement;
36
import java.sql.ResultSet;
37
import java.sql.SQLException;
38
import java.util.*;
39

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

    
44
import org.apache.log4j.Logger;
45

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

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

    
54

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

    
64
    static final int ALL = 1;
65

    
66
    static final int WRITE = 2;
67

    
68
    static final int READ = 4;
69

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

    
73
    private MetaCatUtil util = new MetaCatUtil();
74

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

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

    
80
    /** useful if you just want to grab a list of docids **/
81
    Vector docidOverride = new Vector();
82
    
83
    // a hash table serves as query reuslt cache. Key of hashtable
84
    // is a query string and value is ResultDocumentSet Object
85
    private static Hashtable queryResultCache = new Hashtable();
86
    
87
    // Capacity of the query result cache
88
    private static final int QUERYRESULTCACHESIZE = Integer.parseInt(MetaCatUtil.getOption("queryresult_cache_size"));
89

    
90
    /**
91
     * the main routine used to test the DBQuery utility.
92
     * <p>
93
     * Usage: java DBQuery <xmlfile>
94
     *
95
     * @param xmlfile the filename of the xml file containing the query
96
     */
97
    static public void main(String[] args)
98
    {
99

    
100
        if (args.length < 1) {
101
            System.err.println("Wrong number of arguments!!!");
102
            System.err.println("USAGE: java DBQuery [-t] [-index] <xmlfile>");
103
            return;
104
        } else {
105
            try {
106

    
107
                int i = 0;
108
                boolean showRuntime = false;
109
                boolean useXMLIndex = false;
110
                if (args[i].equals("-t")) {
111
                    showRuntime = true;
112
                    i++;
113
                }
114
                if (args[i].equals("-index")) {
115
                    useXMLIndex = true;
116
                    i++;
117
                }
118
                String xmlfile = args[i];
119

    
120
                // Time the request if asked for
121
                double startTime = System.currentTimeMillis();
122

    
123
                // Open a connection to the database
124
                MetaCatUtil util = new MetaCatUtil();
125
                //Connection dbconn = util.openDBConnection();
126

    
127
                double connTime = System.currentTimeMillis();
128

    
129
                // Execute the query
130
                DBQuery queryobj = new DBQuery();
131
                FileReader xml = new FileReader(new File(xmlfile));
132
                Hashtable nodelist = null;
133
                //nodelist = queryobj.findDocuments(xml, null, null, useXMLIndex);
134

    
135
                // Print the reulting document listing
136
                StringBuffer result = new StringBuffer();
137
                String document = null;
138
                String docid = null;
139
                result.append("<?xml version=\"1.0\"?>\n");
140
                result.append("<resultset>\n");
141

    
142
                if (!showRuntime) {
143
                    Enumeration doclist = nodelist.keys();
144
                    while (doclist.hasMoreElements()) {
145
                        docid = (String) doclist.nextElement();
146
                        document = (String) nodelist.get(docid);
147
                        result.append("  <document>\n    " + document
148
                                + "\n  </document>\n");
149
                    }
150

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

    
177
            } catch (Exception e) {
178
                System.err.println("Error in DBQuery.main");
179
                System.err.println(e.getMessage());
180
                e.printStackTrace(System.err);
181
            }
182
        }
183
    }
184

    
185
    /**
186
     * construct an instance of the DBQuery class
187
     *
188
     * <p>
189
     * Generally, one would call the findDocuments() routine after creating an
190
     * instance to specify the search query
191
     * </p>
192
     *
193

    
194
     * @param parserName the fully qualified name of a Java class implementing
195
     *            the org.xml.sax.XMLReader interface
196
     */
197
    public DBQuery()
198
    {
199
        String parserName = MetaCatUtil.getOption("saxparser");
200
        this.parserName = parserName;
201
    }
202

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

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

    
243
  }
244

    
245

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

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

    
300

    
301

    
302
      if (qformat != null && qformat.equals(MetaCatServlet.XMLFORMAT))
303
      {
304
        //xml format
305
        response.setContentType("text/xml");
306
        createResultDocument(xmlquery, qspec, out, user, groups, useXMLIndex, 
307
          pagesize, pagestart, sessionid);
308
      }//if
309
      else
310
      {
311
        //knb format, in this case we will get whole result and sent it out
312
        response.setContentType("text/html");
313
        PrintWriter nonout = null;
314
        StringBuffer xml = createResultDocument(xmlquery, qspec, nonout, user,
315
                                                groups, useXMLIndex, pagesize, 
316
                                                pagestart, sessionid);
317
        
318
        //transfer the xml to html
319
        try
320
        {
321
         double startHTMLTransform = System.currentTimeMillis()/1000;
322
         DBTransform trans = new DBTransform();
323
         response.setContentType("text/html");
324

    
325
         // if the user is a moderator, then pass a param to the 
326
         // xsl specifying the fact
327
         if(MetaCatUtil.isModerator(user, groups)){
328
        	 params.put("isModerator", new String[] {"true"});
329
         }
330

    
331
         trans.transformXMLDocument(xml.toString(), "-//NCEAS//resultset//EN",
332
                                 "-//W3C//HTML//EN", qformat, out, params,
333
                                 sessionid);
334
         double endHTMLTransform = System.currentTimeMillis()/1000;
335
          logMetacat.warn("The time to transfrom resultset from xml to html format is "
336
                  		                             +(endHTMLTransform -startHTMLTransform));
337
          MetaCatUtil.writeDebugToFile("---------------------------------------------------------------------------------------------------------------Transfrom xml to html  "
338
                             +(endHTMLTransform -startHTMLTransform));
339
          MetaCatUtil.writeDebugToDelimiteredFile(" "+(endHTMLTransform -startHTMLTransform), false);
340
        }
341
        catch(Exception e)
342
        {
343
         logMetacat.error("Error in MetaCatServlet.transformResultset:"
344
                                +e.getMessage());
345
         }
346

    
347
      }//else
348

    
349
  }
350
  
351
  /**
352
   * Transforms a hashtable of documents to an xml or html result and sent
353
   * the content to outputstream. Keep going untill hastable is empty. stop it.
354
   * add the QuerySpecification as parameter is for ecogrid. But it is duplicate
355
   * to xmlquery String
356
   * @param xmlquery
357
   * @param qspec
358
   * @param out
359
   * @param user
360
   * @param groups
361
   * @param useXMLIndex
362
   * @param sessionid
363
   * @return
364
   */
365
    public StringBuffer createResultDocument(String xmlquery,
366
                                              QuerySpecification qspec,
367
                                              PrintWriter out,
368
                                              String user, String[] groups,
369
                                              boolean useXMLIndex)
370
    {
371
    	return createResultDocument(xmlquery,qspec,out, user,groups, useXMLIndex, 0, 0,"");
372
    }
373

    
374
  /*
375
   * Transforms a hashtable of documents to an xml or html result and sent
376
   * the content to outputstream. Keep going untill hastable is empty. stop it.
377
   * add the QuerySpecification as parameter is for ecogrid. But it is duplicate
378
   * to xmlquery String
379
   */
380
  public StringBuffer createResultDocument(String xmlquery,
381
                                            QuerySpecification qspec,
382
                                            PrintWriter out,
383
                                            String user, String[] groups,
384
                                            boolean useXMLIndex, int pagesize,
385
                                            int pagestart, String sessionid)
386
  {
387
    DBConnection dbconn = null;
388
    int serialNumber = -1;
389
    StringBuffer resultset = new StringBuffer();
390

    
391
    //try to get the cached version first    
392
    Hashtable sessionHash = MetaCatServlet.getSessionHash();
393
    HttpSession sess = (HttpSession)sessionHash.get(sessionid);
394

    
395
    
396
    
397
    resultset.append("<?xml version=\"1.0\"?>\n");
398
    resultset.append("<resultset>\n");
399
    resultset.append("  <pagestart>" + pagestart + "</pagestart>\n");
400
    resultset.append("  <pagesize>" + pagesize + "</pagesize>\n");
401
    resultset.append("  <nextpage>" + (pagestart + 1) + "</nextpage>\n");
402
    resultset.append("  <previouspage>" + (pagestart - 1) + "</previouspage>\n");
403

    
404
    resultset.append("  <query>" + xmlquery + "</query>");
405
    //send out a new query
406
    if (out != null)
407
    {
408
      out.println(resultset.toString());
409
    }
410
    if (qspec != null)
411
    {
412
      try
413
      {
414

    
415
        //checkout the dbconnection
416
        dbconn = DBConnectionPool.getDBConnection("DBQuery.findDocuments");
417
        serialNumber = dbconn.getCheckOutSerialNumber();
418

    
419
        //print out the search result
420
        // search the doc list
421
        resultset = findResultDoclist(qspec, resultset, out, user, groups,
422
                                      dbconn, useXMLIndex, pagesize, pagestart, 
423
                                      sessionid);
424
      } //try
425
      catch (IOException ioe)
426
      {
427
        logMetacat.error("IO error in DBQuery.findDocuments:");
428
        logMetacat.error(ioe.getMessage());
429

    
430
      }
431
      catch (SQLException e)
432
      {
433
        logMetacat.error("SQL Error in DBQuery.findDocuments: "
434
                                 + e.getMessage());
435
      }
436
      catch (Exception ee)
437
      {
438
        logMetacat.error("Exception in DBQuery.findDocuments: "
439
                                 + ee.getMessage());
440
        ee.printStackTrace();
441
      }
442
      finally
443
      {
444
        DBConnectionPool.returnDBConnection(dbconn, serialNumber);
445
      } //finally
446
    }//if
447
    String closeRestultset = "</resultset>";
448
    resultset.append(closeRestultset);
449
    if (out != null)
450
    {
451
      out.println(closeRestultset);
452
    }
453

    
454
    //default to returning the whole resultset
455
    return resultset;
456
  }//createResultDocuments
457

    
458
    /*
459
     * Find the doc list which match the query
460
     */
461
    private StringBuffer findResultDoclist(QuerySpecification qspec,
462
                                      StringBuffer resultsetBuffer,
463
                                      PrintWriter out,
464
                                      String user, String[]groups,
465
                                      DBConnection dbconn, boolean useXMLIndex,
466
                                      int pagesize, int pagestart, String sessionid)
467
                                      throws Exception
468
    {
469
      String query = null;
470
      int count = 0;
471
      int index = 0;
472
      ResultDocumentSet docListResult = new ResultDocumentSet();
473
      PreparedStatement pstmt = null;
474
      String docid = null;
475
      String docname = null;
476
      String doctype = null;
477
      String createDate = null;
478
      String updateDate = null;
479
      StringBuffer document = null;
480
      boolean lastpage = false;
481
      int rev = 0;
482
      double startTime = 0;
483
      //int offset = 1;
484
      double startSelectionTime = System.currentTimeMillis()/1000;
485
      ResultSet rs = null;
486
        
487
      //offset = 1;
488
      // this is a hack for offset
489
      /*if (out == null)
490
      {
491
        // for html page, we put everything into one page
492
        offset =
493
            (new Integer(MetaCatUtil.getOption("web_resultsetsize"))).intValue();
494
      }
495
      else
496
      {
497
          offset =
498
              (new Integer(MetaCatUtil.getOption("app_resultsetsize"))).intValue();
499
      }*/
500

    
501
      /*
502
       * Check the docidOverride Vector
503
       * if defined, we bypass the qspec.printSQL() method
504
       * and contruct a simpler query based on a 
505
       * list of docids rather than a bunch of subselects
506
       */
507
      if ( this.docidOverride.size() == 0 ) {
508
          query = qspec.printSQL(useXMLIndex);
509
      } else {
510
          logMetacat.info("*** docid override " + this.docidOverride.size());
511
          StringBuffer queryBuffer = new StringBuffer( "SELECT docid,docname,doctype,date_created, date_updated, rev " );
512
          queryBuffer.append( " FROM xml_documents WHERE docid IN (" );
513
          for (int i = 0; i < docidOverride.size(); i++) {  
514
              queryBuffer.append("'");
515
              queryBuffer.append( (String)docidOverride.elementAt(i) );
516
              queryBuffer.append("',");
517
          }
518
          // empty string hack 
519
          queryBuffer.append( "'') " );
520
          query = queryBuffer.toString();
521
      } 
522
      String ownerQuery = getOwnerQuery(user);
523
      logMetacat.info("\n\n\n query: " + query);
524
      logMetacat.info("\n\n\n owner query: "+ownerQuery);
525
      // if query is not the owner query, we need to check the permission
526
      // otherwise we don't need (owner has all permission by default)
527
      if (!query.equals(ownerQuery))
528
      {
529
        // set user name and group
530
        qspec.setUserName(user);
531
        qspec.setGroup(groups);
532
        // Get access query
533
        String accessQuery = qspec.getAccessQuery();
534
        if(!query.endsWith("WHERE")){
535
            query = query + accessQuery;
536
        } else {
537
            query = query + accessQuery.substring(4, accessQuery.length());
538
        }
539
        
540
      }
541
      logMetacat.warn("============ final selection query: " + query);
542
      startTime = System.currentTimeMillis() / 1000;
543
      pstmt = dbconn.prepareStatement(query);
544
      rs = pstmt.executeQuery();
545

    
546
      double queryExecuteTime = System.currentTimeMillis() / 1000;
547
      logMetacat.warn("Time to execute select docid query is "
548
                    + (queryExecuteTime - startTime));
549
      MetaCatUtil.writeDebugToFile("\n\n\n\n\n\nExecute selection query  "
550
              + (queryExecuteTime - startTime));
551
      MetaCatUtil.writeDebugToDelimiteredFile(""+(queryExecuteTime - startTime), false);
552

    
553
      boolean tableHasRows = rs.next();
554
      
555
      if(pagesize == 0)
556
      { //this makes sure we get all results if there is no paging
557
        pagesize = 99999;
558
        pagestart = 99999;
559
      } 
560
      
561
      int currentIndex = 0;
562
      while (tableHasRows)
563
      {
564
        logMetacat.info("############getting result: " + currentIndex);
565
        docid = rs.getString(1).trim();
566
        logMetacat.info("############processing: " + docid);
567
        docname = rs.getString(2);
568
        doctype = rs.getString(3);
569
        logMetacat.info("############processing: " + doctype);
570
        createDate = rs.getString(4);
571
        updateDate = rs.getString(5);
572
        rev = rs.getInt(6);
573
        
574
         Vector returndocVec = qspec.getReturnDocList();
575
       if (returndocVec.size() == 0 || returndocVec.contains(doctype))
576
        {
577
          logMetacat.info("NOT Back tracing now...");
578
           document = new StringBuffer();
579

    
580
           String completeDocid = docid
581
                            + MetaCatUtil.getOption("accNumSeparator");
582
           completeDocid += rev;
583
           document.append("<docid>").append(completeDocid).append("</docid>");
584
           if (docname != null)
585
           {
586
               document.append("<docname>" + docname + "</docname>");
587
           }
588
           if (doctype != null)
589
           {
590
              document.append("<doctype>" + doctype + "</doctype>");
591
           }
592
           if (createDate != null)
593
           {
594
               document.append("<createdate>" + createDate + "</createdate>");
595
           }
596
           if (updateDate != null)
597
           {
598
             document.append("<updatedate>" + updateDate + "</updatedate>");
599
           }
600
           // Store the document id and the root node id
601
           
602
           docListResult.addResultDocument(
603
             new ResultDocument(docid, (String) document.toString()));
604
           logMetacat.info("$$$$$$$real result: " + docid);
605
           currentIndex++;
606
           count++;
607
        }//else
608
        
609
        // when doclist reached the offset number, send out doc list and empty
610
        // the hash table
611
        /*if (count == offset && pagesize == 0)
612
        { //if pagesize is not 0, do this later.
613
          //reset count
614
          //logMetacat.warn("############doing subset cache");
615
          count = 0;
616
          handleSubsetResult(qspec, resultsetBuffer, out, docListResult,
617
                              user, groups,dbconn, useXMLIndex);
618
          //reset docListResult
619
          docListResult = new ResultDocumentSet();
620
        }*/
621
       
622
       logMetacat.info("currentIndex: " + currentIndex);
623
       logMetacat.info("page comparator: " + (pagesize * pagestart) + pagesize);
624
       if(currentIndex >= ((pagesize * pagestart) + pagesize))
625
       {
626
         ResultDocumentSet pagedResultsHash = new ResultDocumentSet();
627
         for(int i=pagesize*pagestart; i<docListResult.size(); i++)
628
         {
629
           pagedResultsHash.put(docListResult.get(i));
630
         }
631
         
632
         docListResult = pagedResultsHash;
633
         break;
634
       }
635
       // Advance to the next record in the cursor
636
       tableHasRows = rs.next();
637
       if(!tableHasRows)
638
       {
639
         ResultDocumentSet pagedResultsHash = new ResultDocumentSet();
640
         //get the last page of information then break
641
         if(pagesize != 99999)
642
         {
643
           for(int i=pagesize*pagestart; i<docListResult.size(); i++)
644
           {
645
             pagedResultsHash.put(docListResult.get(i));
646
           }
647
           docListResult = pagedResultsHash;
648
         }
649
         
650
         lastpage = true;
651
         break;
652
       }
653
     }//while
654
     
655
     rs.close();
656
     pstmt.close();
657
     double docListTime = System.currentTimeMillis() / 1000;
658
     logMetacat.warn("======Total time to get docid list is: "
659
                          + (docListTime - startSelectionTime ));
660
     MetaCatUtil.writeDebugToFile("---------------------------------------------------------------------------------------------------------------Total selection: "
661
             + (docListTime - startSelectionTime ));
662
     MetaCatUtil.writeDebugToDelimiteredFile(" "+ (docListTime - startSelectionTime ), false);
663
     //if docListResult is not empty, it need to be sent.
664
     if (docListResult.size() != 0)
665
     {
666
       handleSubsetResult(qspec,resultsetBuffer, out, docListResult,
667
                              user, groups,dbconn, useXMLIndex);
668
     }
669

    
670
     resultsetBuffer.append("\n<lastpage>" + lastpage + "</lastpage>\n");
671
     if (out != null)
672
     {
673
         out.println("\n<lastpage>" + lastpage + "</lastpage>\n");
674
     }
675
          
676
     return resultsetBuffer;
677
    }//findReturnDoclist
678

    
679

    
680
    /*
681
     * Send completed search hashtable(part of reulst)to output stream
682
     * and buffer into a buffer stream
683
     */
684
    private StringBuffer handleSubsetResult(QuerySpecification qspec,
685
                                           StringBuffer resultset,
686
                                           PrintWriter out, ResultDocumentSet partOfDoclist,
687
                                           String user, String[]groups,
688
                                       DBConnection dbconn, boolean useXMLIndex)
689
                                       throws Exception
690
   {
691
     double startReturnField = System.currentTimeMillis()/1000;
692
     // check if there is a record in xml_returnfield
693
     // and get the returnfield_id and usage count
694
     int usage_count = getXmlReturnfieldsTableId(qspec, dbconn);
695
     boolean enterRecords = false;
696

    
697
     // get value of xml_returnfield_count
698
     int count = (new Integer(MetaCatUtil
699
                            .getOption("xml_returnfield_count")))
700
                            .intValue();
701

    
702
     // set enterRecords to true if usage_count is more than the offset
703
     // specified in metacat.properties
704
     if(usage_count > count){
705
         enterRecords = true;
706
     }
707

    
708
     if(returnfield_id < 0){
709
         logMetacat.warn("Error in getting returnfield id from"
710
                                  + "xml_returnfield table");
711
         enterRecords = false;
712
     }
713

    
714
     // get the hashtable containing the docids that already in the
715
     // xml_queryresult table
716
     logMetacat.info("size of partOfDoclist before"
717
                             + " docidsInQueryresultTable(): "
718
                             + partOfDoclist.size());
719
     double startGetReturnValueFromQueryresultable = System.currentTimeMillis()/1000;
720
     Hashtable queryresultDocList = docidsInQueryresultTable(returnfield_id,
721
                                                        partOfDoclist, dbconn);
722

    
723
     // remove the keys in queryresultDocList from partOfDoclist
724
     Enumeration _keys = queryresultDocList.keys();
725
     while (_keys.hasMoreElements()){
726
         partOfDoclist.remove((String)_keys.nextElement());
727
     }
728
     double endGetReturnValueFromQueryresultable = System.currentTimeMillis()/1000;
729
     logMetacat.warn("Time to get return fields from xml_queryresult table is (Part1 in return fields) " +
730
          		               (endGetReturnValueFromQueryresultable-startGetReturnValueFromQueryresultable));
731
     MetaCatUtil.writeDebugToFile("-----------------------------------------Get fields from xml_queryresult(Part1 in return fields) " +
732
               (endGetReturnValueFromQueryresultable-startGetReturnValueFromQueryresultable));
733
     MetaCatUtil.writeDebugToDelimiteredFile(" " +
734
             (endGetReturnValueFromQueryresultable-startGetReturnValueFromQueryresultable),false);
735
     // backup the keys-elements in partOfDoclist to check later
736
     // if the doc entry is indexed yet
737
     Hashtable partOfDoclistBackup = new Hashtable();
738
     Iterator itt = partOfDoclist.getDocids();
739
     while (itt.hasNext()){
740
       Object key = itt.next();
741
         partOfDoclistBackup.put(key, partOfDoclist.get(key));
742
     }
743

    
744
     logMetacat.info("size of partOfDoclist after"
745
                             + " docidsInQueryresultTable(): "
746
                             + partOfDoclist.size());
747

    
748
     //add return fields for the documents in partOfDoclist
749
     partOfDoclist = addReturnfield(partOfDoclist, qspec, user, groups,
750
                                        dbconn, useXMLIndex);
751
     double endExtendedQuery = System.currentTimeMillis()/1000;
752
     logMetacat.warn("Get fields from index and node table (Part2 in return fields) "
753
        		                                          + (endExtendedQuery - endGetReturnValueFromQueryresultable));
754
     MetaCatUtil.writeDebugToFile("-----------------------------------------Get fields from extened query(Part2 in return fields) "
755
             + (endExtendedQuery - endGetReturnValueFromQueryresultable));
756
     MetaCatUtil.writeDebugToDelimiteredFile(" "
757
             + (endExtendedQuery - endGetReturnValueFromQueryresultable), false);
758
     //add relationship part part docid list for the documents in partOfDocList
759
     partOfDoclist = addRelationship(partOfDoclist, qspec, dbconn, useXMLIndex);
760

    
761
     double startStoreReturnField = System.currentTimeMillis()/1000;
762
     Iterator keys = partOfDoclist.getDocids();
763
     String key = null;
764
     String element = null;
765
     String query = null;
766
     int offset = (new Integer(MetaCatUtil
767
                               .getOption("queryresult_string_length")))
768
                               .intValue();
769
     while (keys.hasNext())
770
     {
771
         key = (String) keys.next();
772
         element = (String)partOfDoclist.get(key);
773

    
774
	 // check if the enterRecords is true, elements is not null, element's
775
         // length is less than the limit of table column and if the document
776
         // has been indexed already
777
         if(enterRecords && element != null
778
		&& element.length() < offset
779
		&& element.compareTo((String) partOfDoclistBackup.get(key)) != 0){
780
             query = "INSERT INTO xml_queryresult (returnfield_id, docid, "
781
                 + "queryresult_string) VALUES (?, ?, ?)";
782

    
783
             PreparedStatement pstmt = null;
784
             pstmt = dbconn.prepareStatement(query);
785
             pstmt.setInt(1, returnfield_id);
786
             pstmt.setString(2, key);
787
             pstmt.setString(3, element);
788

    
789
             dbconn.increaseUsageCount(1);
790
             pstmt.execute();
791
             pstmt.close();
792
         }
793
        
794
         // A string with element
795
         String xmlElement = "  <document>" + element + "</document>";
796

    
797
         //send single element to output
798
         if (out != null)
799
         {
800
             out.println(xmlElement);
801
         }
802
         resultset.append(xmlElement);
803
     }//while
804
     
805
     double endStoreReturnField = System.currentTimeMillis()/1000;
806
     logMetacat.warn("Time to store new return fields into xml_queryresult table (Part4 in return fields) "
807
                   + (endStoreReturnField -startStoreReturnField));
808
     MetaCatUtil.writeDebugToFile("-----------------------------------------Insert new record to xml_queryresult(Part4 in return fields) "
809
             + (endStoreReturnField -startStoreReturnField));
810
     MetaCatUtil.writeDebugToDelimiteredFile(" "
811
             + (endStoreReturnField -startStoreReturnField), false);
812
     
813
     Enumeration keysE = queryresultDocList.keys();
814
     while (keysE.hasMoreElements())
815
     {
816
         key = (String) keysE.nextElement();
817
         element = (String)queryresultDocList.get(key);
818
         // A string with element
819
         String xmlElement = "  <document>" + element + "</document>";
820
         //send single element to output
821
         if (out != null)
822
         {
823
             out.println(xmlElement);
824
         }
825
         resultset.append(xmlElement);
826
     }//while
827
     double returnFieldTime = System.currentTimeMillis() / 1000;
828
     logMetacat.warn("======Total time to get return fields is: "
829
                           + (returnFieldTime - startReturnField));
830
     MetaCatUtil.writeDebugToFile("---------------------------------------------------------------------------------------------------------------"+
831
    		 "Total to get return fields  "
832
                                   + (returnFieldTime - startReturnField));
833
     MetaCatUtil.writeDebugToDelimiteredFile(" "+ (returnFieldTime - startReturnField), false);
834
     return resultset;
835
 }
836

    
837
   /**
838
    * Get the docids already in xml_queryresult table and corresponding
839
    * queryresultstring as a hashtable
840
    */
841
   private Hashtable docidsInQueryresultTable(int returnfield_id,
842
                                              ResultDocumentSet partOfDoclist,
843
                                              DBConnection dbconn){
844

    
845
         Hashtable returnValue = new Hashtable();
846
         PreparedStatement pstmt = null;
847
         ResultSet rs = null;
848

    
849
         // get partOfDoclist as string for the query
850
         Iterator keylist = partOfDoclist.getDocids();
851
         StringBuffer doclist = new StringBuffer();
852
         while (keylist.hasNext())
853
         {
854
             doclist.append("'");
855
             doclist.append((String) keylist.next());
856
             doclist.append("',");
857
         }//while
858

    
859

    
860
         if (doclist.length() > 0)
861
         {
862
             doclist.deleteCharAt(doclist.length() - 1); //remove the last comma
863

    
864
             // the query to find out docids from xml_queryresult
865
             String query = "select docid, queryresult_string from "
866
                          + "xml_queryresult where returnfield_id = " +
867
                          returnfield_id +" and docid in ("+ doclist + ")";
868
             logMetacat.info("Query to get docids from xml_queryresult:"
869
                                      + query);
870

    
871
             try {
872
                 // prepare and execute the query
873
                 pstmt = dbconn.prepareStatement(query);
874
                 dbconn.increaseUsageCount(1);
875
                 pstmt.execute();
876
                 rs = pstmt.getResultSet();
877
                 boolean tableHasRows = rs.next();
878
                 while (tableHasRows) {
879
                     // store the returned results in the returnValue hashtable
880
                     String key = rs.getString(1);
881
                     String element = rs.getString(2);
882

    
883
                     if(element != null){
884
                         returnValue.put(key, element);
885
                     } else {
886
                         logMetacat.info("Null elment found ("
887
                         + "DBQuery.docidsInQueryresultTable)");
888
                     }
889
                     tableHasRows = rs.next();
890
                 }
891
                 rs.close();
892
                 pstmt.close();
893
             } catch (Exception e){
894
                 logMetacat.error("Error getting docids from "
895
                                          + "queryresult in "
896
                                          + "DBQuery.docidsInQueryresultTable: "
897
                                          + e.getMessage());
898
              }
899
         }
900
         return returnValue;
901
     }
902

    
903

    
904
   /**
905
    * Method to get id from xml_returnfield table
906
    * for a given query specification
907
    */
908
   private int returnfield_id;
909
   private int getXmlReturnfieldsTableId(QuerySpecification qspec,
910
                                           DBConnection dbconn){
911
       int id = -1;
912
       int count = 1;
913
       PreparedStatement pstmt = null;
914
       ResultSet rs = null;
915
       String returnfield = qspec.getSortedReturnFieldString();
916

    
917
       // query for finding the id from xml_returnfield
918
       String query = "SELECT returnfield_id, usage_count FROM xml_returnfield "
919
            + "WHERE returnfield_string LIKE ?";
920
       logMetacat.info("ReturnField Query:" + query);
921

    
922
       try {
923
           // prepare and run the query
924
           pstmt = dbconn.prepareStatement(query);
925
           pstmt.setString(1,returnfield);
926
           dbconn.increaseUsageCount(1);
927
           pstmt.execute();
928
           rs = pstmt.getResultSet();
929
           boolean tableHasRows = rs.next();
930

    
931
           // if record found then increase the usage count
932
           // else insert a new record and get the id of the new record
933
           if(tableHasRows){
934
               // get the id
935
               id = rs.getInt(1);
936
               count = rs.getInt(2) + 1;
937
               rs.close();
938
               pstmt.close();
939

    
940
               // increase the usage count
941
               query = "UPDATE xml_returnfield SET usage_count ='" + count
942
                   + "' WHERE returnfield_id ='"+ id +"'";
943
               logMetacat.info("ReturnField Table Update:"+ query);
944

    
945
               pstmt = dbconn.prepareStatement(query);
946
               dbconn.increaseUsageCount(1);
947
               pstmt.execute();
948
               pstmt.close();
949

    
950
           } else {
951
               rs.close();
952
               pstmt.close();
953

    
954
               // insert a new record
955
               query = "INSERT INTO xml_returnfield (returnfield_string, usage_count)"
956
                   + "VALUES (?, '1')";
957
               logMetacat.info("ReturnField Table Insert:"+ query);
958
               pstmt = dbconn.prepareStatement(query);
959
               pstmt.setString(1, returnfield);
960
               dbconn.increaseUsageCount(1);
961
               pstmt.execute();
962
               pstmt.close();
963

    
964
               // get the id of the new record
965
               query = "SELECT returnfield_id FROM xml_returnfield "
966
                   + "WHERE returnfield_string LIKE ?";
967
               logMetacat.info("ReturnField query after Insert:" + query);
968
               pstmt = dbconn.prepareStatement(query);
969
               pstmt.setString(1, returnfield);
970

    
971
               dbconn.increaseUsageCount(1);
972
               pstmt.execute();
973
               rs = pstmt.getResultSet();
974
               if(rs.next()){
975
                   id = rs.getInt(1);
976
               } else {
977
                   id = -1;
978
               }
979
               rs.close();
980
               pstmt.close();
981
           }
982

    
983
       } catch (Exception e){
984
           logMetacat.error("Error getting id from xml_returnfield in "
985
                                     + "DBQuery.getXmlReturnfieldsTableId: "
986
                                     + e.getMessage());
987
           id = -1;
988
       }
989

    
990
       returnfield_id = id;
991
       return count;
992
   }
993

    
994

    
995
    /*
996
     * A method to add return field to return doclist hash table
997
     */
998
    private ResultDocumentSet addReturnfield(ResultDocumentSet docListResult,
999
                                      QuerySpecification qspec,
1000
                                      String user, String[]groups,
1001
                                      DBConnection dbconn, boolean useXMLIndex )
1002
                                      throws Exception
1003
    {
1004
      PreparedStatement pstmt = null;
1005
      ResultSet rs = null;
1006
      String docid = null;
1007
      String fieldname = null;
1008
      String fielddata = null;
1009
      String relation = null;
1010

    
1011
      if (qspec.containsExtendedSQL())
1012
      {
1013
        qspec.setUserName(user);
1014
        qspec.setGroup(groups);
1015
        Vector extendedFields = new Vector(qspec.getReturnFieldList());
1016
        Vector results = new Vector();
1017
        Iterator keylist = docListResult.getDocids();
1018
        StringBuffer doclist = new StringBuffer();
1019
        Vector parentidList = new Vector();
1020
        Hashtable returnFieldValue = new Hashtable();
1021
        while (keylist.hasNext())
1022
        {
1023
          doclist.append("'");
1024
          doclist.append((String) keylist.next());
1025
          doclist.append("',");
1026
        }
1027
        if (doclist.length() > 0)
1028
        {
1029
          Hashtable controlPairs = new Hashtable();
1030
          doclist.deleteCharAt(doclist.length() - 1); //remove the last comma
1031
          boolean tableHasRows = false;
1032
          // check if user has permission to see the return field data
1033
          /*String accessControlSQL =
1034
                 qspec.printAccessControlSQLForReturnField(doclist.toString());
1035
          pstmt = dbconn.prepareStatement(accessControlSQL);
1036
          //increase dbconnection usage count
1037
          dbconn.increaseUsageCount(1);
1038
          pstmt.execute();
1039
          rs = pstmt.getResultSet();
1040
          tableHasRows = rs.next();
1041
          while (tableHasRows)
1042
          {
1043
            long startNodeId = rs.getLong(1);
1044
            long endNodeId = rs.getLong(2);
1045
            controlPairs.put(new Long(startNodeId), new Long(endNodeId));
1046
            tableHasRows = rs.next();
1047
          }*/
1048

    
1049
           /*double extendedAccessQueryEnd = System.currentTimeMillis() / 1000;
1050
           logMetacat.info( "Time for execute access extended query: "
1051
                          + (extendedAccessQueryEnd - extendedQueryStart));*/
1052

    
1053
           String extendedQuery =
1054
               qspec.printExtendedSQL(doclist.toString(), useXMLIndex);
1055
           logMetacat.info("Extended query: " + extendedQuery);
1056

    
1057
           if(extendedQuery != null){
1058
        	   double extendedQueryStart = System.currentTimeMillis() / 1000;
1059
               pstmt = dbconn.prepareStatement(extendedQuery);
1060
               //increase dbconnection usage count
1061
               dbconn.increaseUsageCount(1);
1062
               pstmt.execute();
1063
               rs = pstmt.getResultSet();
1064
               double extendedQueryEnd = System.currentTimeMillis() / 1000;
1065
               logMetacat.warn(
1066
                   "Time to execute extended query: "
1067
                   + (extendedQueryEnd - extendedQueryStart));
1068
               MetaCatUtil.writeDebugToFile(
1069
                       "Execute extended query "
1070
                       + (extendedQueryEnd - extendedQueryStart));
1071
               MetaCatUtil.writeDebugToDelimiteredFile(" "+ (extendedQueryEnd - extendedQueryStart), false);
1072
               tableHasRows = rs.next();
1073
               while (tableHasRows) {
1074
                   ReturnFieldValue returnValue = new ReturnFieldValue();
1075
                   docid = rs.getString(1).trim();
1076
                   fieldname = rs.getString(2);
1077
                   fielddata = rs.getString(3);
1078
                   fielddata = MetaCatUtil.normalize(fielddata);
1079
                   String parentId = rs.getString(4);
1080
                   StringBuffer value = new StringBuffer();
1081

    
1082
                   // if xml_index is used, there would be just one record per nodeid
1083
                   // as xml_index just keeps one entry for each path
1084
                   if (useXMLIndex || !containsKey(parentidList, parentId)) {
1085
                       // don't need to merger nodedata
1086
                       value.append("<param name=\"");
1087
                       value.append(fieldname);
1088
                       value.append("\">");
1089
                       value.append(fielddata);
1090
                       value.append("</param>");
1091
                       //set returnvalue
1092
                       returnValue.setDocid(docid);
1093
                       returnValue.setFieldValue(fielddata);
1094
                       returnValue.setXMLFieldValue(value.toString());
1095
                       // Store it in hastable
1096
                       putInArray(parentidList, parentId, returnValue);
1097
                   }
1098
                   else {
1099
                       // need to merge nodedata if they have same parent id and
1100
                       // node type is text
1101
                       fielddata = (String) ( (ReturnFieldValue)
1102
                                             getArrayValue(
1103
                           parentidList, parentId)).getFieldValue()
1104
                           + fielddata;
1105
                       value.append("<param name=\"");
1106
                       value.append(fieldname);
1107
                       value.append("\">");
1108
                       value.append(fielddata);
1109
                       value.append("</param>");
1110
                       returnValue.setDocid(docid);
1111
                       returnValue.setFieldValue(fielddata);
1112
                       returnValue.setXMLFieldValue(value.toString());
1113
                       // remove the old return value from paretnidList
1114
                       parentidList.remove(parentId);
1115
                       // store the new return value in parentidlit
1116
                       putInArray(parentidList, parentId, returnValue);
1117
                   }
1118
                   tableHasRows = rs.next();
1119
               } //while
1120
               rs.close();
1121
               pstmt.close();
1122

    
1123
               // put the merger node data info into doclistReult
1124
               Enumeration xmlFieldValue = (getElements(parentidList)).
1125
                   elements();
1126
               while (xmlFieldValue.hasMoreElements()) {
1127
                   ReturnFieldValue object =
1128
                       (ReturnFieldValue) xmlFieldValue.nextElement();
1129
                   docid = object.getDocid();
1130
                   if (docListResult.containsDocid(docid)) {
1131
                       String removedelement = (String) docListResult.
1132
                           remove(docid);
1133
                       docListResult.
1134
                           addResultDocument(new ResultDocument(docid,
1135
                               removedelement + object.getXMLFieldValue()));
1136
                   }
1137
                   else {
1138
                       docListResult.addResultDocument(
1139
                         new ResultDocument(docid, object.getXMLFieldValue()));
1140
                   }
1141
               } //while
1142
               double docListResultEnd = System.currentTimeMillis() / 1000;
1143
               logMetacat.warn(
1144
                   "Time to prepare ResultDocumentSet after"
1145
                   + " execute extended query: "
1146
                   + (docListResultEnd - extendedQueryEnd));
1147
           }
1148

    
1149
         
1150
           
1151
           
1152
       }//if doclist lenght is great than zero
1153

    
1154
     }//if has extended query
1155

    
1156
      return docListResult;
1157
    }//addReturnfield
1158

    
1159
    /*
1160
    * A method to add relationship to return doclist hash table
1161
    */
1162
   private ResultDocumentSet addRelationship(ResultDocumentSet docListResult,
1163
                                     QuerySpecification qspec,
1164
                                     DBConnection dbconn, boolean useXMLIndex )
1165
                                     throws Exception
1166
  {
1167
    PreparedStatement pstmt = null;
1168
    ResultSet rs = null;
1169
    StringBuffer document = null;
1170
    double startRelation = System.currentTimeMillis() / 1000;
1171
    Iterator docidkeys = docListResult.getDocids();
1172
    while (docidkeys.hasNext())
1173
    {
1174
      //String connstring =
1175
      // "metacat://"+util.getOption("server")+"?docid=";
1176
      String connstring = "%docid=";
1177
      String docidkey;
1178
      synchronized(docListResult)
1179
      {
1180
        docidkey = (String) docidkeys.next();
1181
      }
1182
      pstmt = dbconn.prepareStatement(QuerySpecification
1183
                      .printRelationSQL(docidkey));
1184
      pstmt.execute();
1185
      rs = pstmt.getResultSet();
1186
      boolean tableHasRows = rs.next();
1187
      while (tableHasRows)
1188
      {
1189
        String sub = rs.getString(1);
1190
        String rel = rs.getString(2);
1191
        String obj = rs.getString(3);
1192
        String subDT = rs.getString(4);
1193
        String objDT = rs.getString(5);
1194

    
1195
        document = new StringBuffer();
1196
        document.append("<triple>");
1197
        document.append("<subject>").append(MetaCatUtil.normalize(sub));
1198
        document.append("</subject>");
1199
        if (subDT != null)
1200
        {
1201
          document.append("<subjectdoctype>").append(subDT);
1202
          document.append("</subjectdoctype>");
1203
        }
1204
        document.append("<relationship>").append(MetaCatUtil.normalize(rel));
1205
        document.append("</relationship>");
1206
        document.append("<object>").append(MetaCatUtil.normalize(obj));
1207
        document.append("</object>");
1208
        if (objDT != null)
1209
        {
1210
          document.append("<objectdoctype>").append(objDT);
1211
          document.append("</objectdoctype>");
1212
        }
1213
        document.append("</triple>");
1214

    
1215
        String removedelement = (String) docListResult.get(docidkey);
1216
        docListResult.set(docidkey, removedelement+ document.toString());
1217
        tableHasRows = rs.next();
1218
      }//while
1219
      rs.close();
1220
      pstmt.close();
1221
      
1222
    }//while
1223
    double endRelation = System.currentTimeMillis() / 1000;
1224
    logMetacat.warn("Time to add relationship to return fields (part 3 in return fields): "
1225
                             + (endRelation - startRelation));
1226
    MetaCatUtil.writeDebugToFile("-----------------------------------------Add relationship to return field(part3 in return fields): "
1227
            + (endRelation - startRelation));
1228
    MetaCatUtil.writeDebugToDelimiteredFile(" "+ (endRelation - startRelation), false);
1229

    
1230
    return docListResult;
1231
  }//addRelation
1232

    
1233
  /**
1234
   * removes the <?xml version="1.0"?> tag from the beginning.  This takes a
1235
   * string as a param instead of a hashtable.
1236
   *
1237
   * @param xmlquery a string representing a query.
1238
   */
1239
   private  String transformQuery(String xmlquery)
1240
   {
1241
     xmlquery = xmlquery.trim();
1242
     int index = xmlquery.indexOf("?>");
1243
     if (index != -1)
1244
     {
1245
       return xmlquery.substring(index + 2, xmlquery.length());
1246
     }
1247
     else
1248
     {
1249
       return xmlquery;
1250
     }
1251
   }
1252
   
1253
   /*
1254
    * Method to store query string and DocumentResultSet into query result
1255
    * cache. If the size alreay reache the limitation, the cache will be
1256
    * cleared first, then store them.
1257
    */
1258
   private void storeQueryResultIntoCache(String query, ResultDocumentSet resultSet)
1259
   {
1260
	   synchronized (queryResultCache)
1261
	   {
1262
		   if (queryResultCache.size() >= QUERYRESULTCACHESIZE)
1263
		   {
1264
			   queryResultCache.clear();
1265
		   }
1266
		   queryResultCache.put(query, resultSet);
1267
		   
1268
	   }
1269
   }
1270
   
1271
   /*
1272
    * Method to get DocumentResultSet from query result cache. 
1273
    * Note: the returned ResultDoucmentSet can be null.
1274
    */
1275
   private ResultDocumentSet getResultDocumentSetFromCache(String query)
1276
   {
1277
	   ResultDocumentSet resultSet = null;
1278
	   synchronized (queryResultCache)
1279
	   {
1280
          try
1281
          {
1282
		     resultSet = (ResultDocumentSet)queryResultCache.get(query);
1283
		   
1284
          }
1285
          catch (Exception e)
1286
          {
1287
        	  resultSet = null;
1288
          }
1289
		   
1290
	   }
1291
	   return resultSet;
1292
   }
1293
   
1294
   /**
1295
    * Method to clear the query result cache.
1296
    */
1297
   public static void clearQueryResultCache()
1298
   {
1299
	   synchronized (queryResultCache)
1300
	   {
1301
		   queryResultCache.clear();
1302
	   }
1303
   }
1304

    
1305

    
1306
    /*
1307
     * A method to search if Vector contains a particular key string
1308
     */
1309
    private boolean containsKey(Vector parentidList, String parentId)
1310
    {
1311

    
1312
        Vector tempVector = null;
1313

    
1314
        for (int count = 0; count < parentidList.size(); count++) {
1315
            tempVector = (Vector) parentidList.get(count);
1316
            if (parentId.compareTo((String) tempVector.get(0)) == 0) { return true; }
1317
        }
1318
        return false;
1319
    }
1320

    
1321
    /*
1322
     * A method to put key and value in Vector
1323
     */
1324
    private void putInArray(Vector parentidList, String key,
1325
            ReturnFieldValue value)
1326
    {
1327

    
1328
        Vector tempVector = null;
1329

    
1330
        for (int count = 0; count < parentidList.size(); count++) {
1331
            tempVector = (Vector) parentidList.get(count);
1332

    
1333
            if (key.compareTo((String) tempVector.get(0)) == 0) {
1334
                tempVector.remove(1);
1335
                tempVector.add(1, value);
1336
                return;
1337
            }
1338
        }
1339

    
1340
        tempVector = new Vector();
1341
        tempVector.add(0, key);
1342
        tempVector.add(1, value);
1343
        parentidList.add(tempVector);
1344
        return;
1345
    }
1346

    
1347
    /*
1348
     * A method to get value in Vector given a key
1349
     */
1350
    private ReturnFieldValue getArrayValue(Vector parentidList, String key)
1351
    {
1352

    
1353
        Vector tempVector = null;
1354

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

    
1358
            if (key.compareTo((String) tempVector.get(0)) == 0) { return (ReturnFieldValue) tempVector
1359
                    .get(1); }
1360
        }
1361
        return null;
1362
    }
1363

    
1364
    /*
1365
     * A method to get enumeration of all values in Vector
1366
     */
1367
    private Vector getElements(Vector parentidList)
1368
    {
1369
        Vector enumVector = new Vector();
1370
        Vector tempVector = null;
1371

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

    
1375
            enumVector.add(tempVector.get(1));
1376
        }
1377
        return enumVector;
1378
    }
1379

    
1380
  
1381

    
1382
    /*
1383
     * A method to create a query to get owner's docid list
1384
     */
1385
    private String getOwnerQuery(String owner)
1386
    {
1387
        if (owner != null) {
1388
            owner = owner.toLowerCase();
1389
        }
1390
        StringBuffer self = new StringBuffer();
1391

    
1392
        self.append("SELECT docid,docname,doctype,");
1393
        self.append("date_created, date_updated, rev ");
1394
        self.append("FROM xml_documents WHERE docid IN (");
1395
        self.append("(");
1396
        self.append("SELECT DISTINCT docid FROM xml_nodes WHERE \n");
1397
        self.append("nodedata LIKE '%%%' ");
1398
        self.append(") \n");
1399
        self.append(") ");
1400
        self.append(" AND (");
1401
        self.append(" lower(user_owner) = '" + owner + "'");
1402
        self.append(") ");
1403
        return self.toString();
1404
    }
1405

    
1406
    /**
1407
     * format a structured query as an XML document that conforms to the
1408
     * pathquery.dtd and is appropriate for submission to the DBQuery
1409
     * structured query engine
1410
     *
1411
     * @param params The list of parameters that should be included in the
1412
     *            query
1413
     */
1414
    public static String createSQuery(Hashtable params)
1415
    {
1416
        StringBuffer query = new StringBuffer();
1417
        Enumeration elements;
1418
        Enumeration keys;
1419
        String filterDoctype = null;
1420
        String casesensitive = null;
1421
        String searchmode = null;
1422
        Object nextkey;
1423
        Object nextelement;
1424
        //add the xml headers
1425
        query.append("<?xml version=\"1.0\"?>\n");
1426
        query.append("<pathquery version=\"1.2\">\n");
1427

    
1428

    
1429

    
1430
        if (params.containsKey("meta_file_id")) {
1431
            query.append("<meta_file_id>");
1432
            query.append(((String[]) params.get("meta_file_id"))[0]);
1433
            query.append("</meta_file_id>");
1434
        }
1435

    
1436
        if (params.containsKey("returndoctype")) {
1437
            String[] returnDoctypes = ((String[]) params.get("returndoctype"));
1438
            for (int i = 0; i < returnDoctypes.length; i++) {
1439
                String doctype = (String) returnDoctypes[i];
1440

    
1441
                if (!doctype.equals("any") && !doctype.equals("ANY")
1442
                        && !doctype.equals("")) {
1443
                    query.append("<returndoctype>").append(doctype);
1444
                    query.append("</returndoctype>");
1445
                }
1446
            }
1447
        }
1448

    
1449
        if (params.containsKey("filterdoctype")) {
1450
            String[] filterDoctypes = ((String[]) params.get("filterdoctype"));
1451
            for (int i = 0; i < filterDoctypes.length; i++) {
1452
                query.append("<filterdoctype>").append(filterDoctypes[i]);
1453
                query.append("</filterdoctype>");
1454
            }
1455
        }
1456

    
1457
        if (params.containsKey("returnfield")) {
1458
            String[] returnfield = ((String[]) params.get("returnfield"));
1459
            for (int i = 0; i < returnfield.length; i++) {
1460
                query.append("<returnfield>").append(returnfield[i]);
1461
                query.append("</returnfield>");
1462
            }
1463
        }
1464

    
1465
        if (params.containsKey("owner")) {
1466
            String[] owner = ((String[]) params.get("owner"));
1467
            for (int i = 0; i < owner.length; i++) {
1468
                query.append("<owner>").append(owner[i]);
1469
                query.append("</owner>");
1470
            }
1471
        }
1472

    
1473
        if (params.containsKey("site")) {
1474
            String[] site = ((String[]) params.get("site"));
1475
            for (int i = 0; i < site.length; i++) {
1476
                query.append("<site>").append(site[i]);
1477
                query.append("</site>");
1478
            }
1479
        }
1480

    
1481
        //allows the dynamic switching of boolean operators
1482
        if (params.containsKey("operator")) {
1483
            query.append("<querygroup operator=\""
1484
                    + ((String[]) params.get("operator"))[0] + "\">");
1485
        } else { //the default operator is UNION
1486
            query.append("<querygroup operator=\"UNION\">");
1487
        }
1488

    
1489
        if (params.containsKey("casesensitive")) {
1490
            casesensitive = ((String[]) params.get("casesensitive"))[0];
1491
        } else {
1492
            casesensitive = "false";
1493
        }
1494

    
1495
        if (params.containsKey("searchmode")) {
1496
            searchmode = ((String[]) params.get("searchmode"))[0];
1497
        } else {
1498
            searchmode = "contains";
1499
        }
1500

    
1501
        //anyfield is a special case because it does a
1502
        //free text search. It does not have a <pathexpr>
1503
        //tag. This allows for a free text search within the structured
1504
        //query. This is useful if the INTERSECT operator is used.
1505
        if (params.containsKey("anyfield")) {
1506
            String[] anyfield = ((String[]) params.get("anyfield"));
1507
            //allow for more than one value for anyfield
1508
            for (int i = 0; i < anyfield.length; i++) {
1509
                if (!anyfield[i].equals("")) {
1510
                    query.append("<queryterm casesensitive=\"" + casesensitive
1511
                            + "\" " + "searchmode=\"" + searchmode
1512
                            + "\"><value>" + anyfield[i]
1513
                            + "</value></queryterm>");
1514
                }
1515
            }
1516
        }
1517

    
1518
        //this while loop finds the rest of the parameters
1519
        //and attempts to query for the field specified
1520
        //by the parameter.
1521
        elements = params.elements();
1522
        keys = params.keys();
1523
        while (keys.hasMoreElements() && elements.hasMoreElements()) {
1524
            nextkey = keys.nextElement();
1525
            nextelement = elements.nextElement();
1526

    
1527
            //make sure we aren't querying for any of these
1528
            //parameters since the are already in the query
1529
            //in one form or another.
1530
            Vector ignoredParams = new Vector();
1531
            ignoredParams.add("returndoctype");
1532
            ignoredParams.add("filterdoctype");
1533
            ignoredParams.add("action");
1534
            ignoredParams.add("qformat");
1535
            ignoredParams.add("anyfield");
1536
            ignoredParams.add("returnfield");
1537
            ignoredParams.add("owner");
1538
            ignoredParams.add("site");
1539
            ignoredParams.add("operator");
1540
            ignoredParams.add("sessionid");
1541
            ignoredParams.add("pagesize");
1542
            ignoredParams.add("pagestart");
1543

    
1544
            // Also ignore parameters listed in the properties file
1545
            // so that they can be passed through to stylesheets
1546
            String paramsToIgnore = MetaCatUtil
1547
                    .getOption("query.ignored.params");
1548
            StringTokenizer st = new StringTokenizer(paramsToIgnore, ",");
1549
            while (st.hasMoreTokens()) {
1550
                ignoredParams.add(st.nextToken());
1551
            }
1552
            if (!ignoredParams.contains(nextkey.toString())) {
1553
                //allow for more than value per field name
1554
                for (int i = 0; i < ((String[]) nextelement).length; i++) {
1555
                    if (!((String[]) nextelement)[i].equals("")) {
1556
                        query.append("<queryterm casesensitive=\""
1557
                                + casesensitive + "\" " + "searchmode=\""
1558
                                + searchmode + "\">" + "<value>" +
1559
                                //add the query value
1560
                                ((String[]) nextelement)[i]
1561
                                + "</value><pathexpr>" +
1562
                                //add the path to query by
1563
                                nextkey.toString() + "</pathexpr></queryterm>");
1564
                    }
1565
                }
1566
            }
1567
        }
1568
        query.append("</querygroup></pathquery>");
1569
        //append on the end of the xml and return the result as a string
1570
        return query.toString();
1571
    }
1572

    
1573
    /**
1574
     * format a simple free-text value query as an XML document that conforms
1575
     * to the pathquery.dtd and is appropriate for submission to the DBQuery
1576
     * structured query engine
1577
     *
1578
     * @param value the text string to search for in the xml catalog
1579
     * @param doctype the type of documents to include in the result set -- use
1580
     *            "any" or "ANY" for unfiltered result sets
1581
     */
1582
    public static String createQuery(String value, String doctype)
1583
    {
1584
        StringBuffer xmlquery = new StringBuffer();
1585
        xmlquery.append("<?xml version=\"1.0\"?>\n");
1586
        xmlquery.append("<pathquery version=\"1.0\">");
1587

    
1588
        if (!doctype.equals("any") && !doctype.equals("ANY")) {
1589
            xmlquery.append("<returndoctype>");
1590
            xmlquery.append(doctype).append("</returndoctype>");
1591
        }
1592

    
1593
        xmlquery.append("<querygroup operator=\"UNION\">");
1594
        //chad added - 8/14
1595
        //the if statement allows a query to gracefully handle a null
1596
        //query. Without this if a nullpointerException is thrown.
1597
        if (!value.equals("")) {
1598
            xmlquery.append("<queryterm casesensitive=\"false\" ");
1599
            xmlquery.append("searchmode=\"contains\">");
1600
            xmlquery.append("<value>").append(value).append("</value>");
1601
            xmlquery.append("</queryterm>");
1602
        }
1603
        xmlquery.append("</querygroup>");
1604
        xmlquery.append("</pathquery>");
1605

    
1606
        return (xmlquery.toString());
1607
    }
1608

    
1609
    /**
1610
     * format a simple free-text value query as an XML document that conforms
1611
     * to the pathquery.dtd and is appropriate for submission to the DBQuery
1612
     * structured query engine
1613
     *
1614
     * @param value the text string to search for in the xml catalog
1615
     */
1616
    public static String createQuery(String value)
1617
    {
1618
        return createQuery(value, "any");
1619
    }
1620

    
1621
    /**
1622
     * Check for "READ" permission on @docid for @user and/or @group from DB
1623
     * connection
1624
     */
1625
    private boolean hasPermission(String user, String[] groups, String docid)
1626
            throws SQLException, Exception
1627
    {
1628
        // Check for READ permission on @docid for @user and/or @groups
1629
        PermissionController controller = new PermissionController(docid);
1630
        return controller.hasPermission(user, groups,
1631
                AccessControlInterface.READSTRING);
1632
    }
1633

    
1634
    /**
1635
     * Get all docIds list for a data packadge
1636
     *
1637
     * @param dataPackageDocid, the string in docId field of xml_relation table
1638
     */
1639
    private Vector getCurrentDocidListForDataPackage(String dataPackageDocid)
1640
    {
1641
        DBConnection dbConn = null;
1642
        int serialNumber = -1;
1643
        Vector docIdList = new Vector();//return value
1644
        PreparedStatement pStmt = null;
1645
        ResultSet rs = null;
1646
        String docIdInSubjectField = null;
1647
        String docIdInObjectField = null;
1648

    
1649
        // Check the parameter
1650
        if (dataPackageDocid == null || dataPackageDocid.equals("")) { return docIdList; }//if
1651

    
1652
        //the query stirng
1653
        String query = "SELECT subject, object from xml_relation where docId = ?";
1654
        try {
1655
            dbConn = DBConnectionPool
1656
                    .getDBConnection("DBQuery.getCurrentDocidListForDataPackage");
1657
            serialNumber = dbConn.getCheckOutSerialNumber();
1658
            pStmt = dbConn.prepareStatement(query);
1659
            //bind the value to query
1660
            pStmt.setString(1, dataPackageDocid);
1661

    
1662
            //excute the query
1663
            pStmt.execute();
1664
            //get the result set
1665
            rs = pStmt.getResultSet();
1666
            //process the result
1667
            while (rs.next()) {
1668
                //In order to get the whole docIds in a data packadge,
1669
                //we need to put the docIds of subject and object field in
1670
                // xml_relation
1671
                //into the return vector
1672
                docIdInSubjectField = rs.getString(1);//the result docId in
1673
                                                      // subject field
1674
                docIdInObjectField = rs.getString(2);//the result docId in
1675
                                                     // object field
1676

    
1677
                //don't put the duplicate docId into the vector
1678
                if (!docIdList.contains(docIdInSubjectField)) {
1679
                    docIdList.add(docIdInSubjectField);
1680
                }
1681

    
1682
                //don't put the duplicate docId into the vector
1683
                if (!docIdList.contains(docIdInObjectField)) {
1684
                    docIdList.add(docIdInObjectField);
1685
                }
1686
            }//while
1687
            //close the pStmt
1688
            pStmt.close();
1689
        }//try
1690
        catch (SQLException e) {
1691
            logMetacat.error("Error in getDocidListForDataPackage: "
1692
                    + e.getMessage());
1693
        }//catch
1694
        finally {
1695
            try {
1696
                pStmt.close();
1697
            }//try
1698
            catch (SQLException ee) {
1699
                logMetacat.error(
1700
                        "Error in getDocidListForDataPackage: "
1701
                                + ee.getMessage());
1702
            }//catch
1703
            finally {
1704
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1705
            }//fianlly
1706
        }//finally
1707
        return docIdList;
1708
    }//getCurrentDocidListForDataPackadge()
1709

    
1710
    /**
1711
     * Get all docIds list for a data packadge
1712
     *
1713
     * @param dataPackageDocid, the string in docId field of xml_relation table
1714
     */
1715
    private Vector getOldVersionDocidListForDataPackage(String dataPackageDocidWithRev)
1716
    {
1717

    
1718
        Vector docIdList = new Vector();//return value
1719
        Vector tripleList = null;
1720
        String xml = null;
1721

    
1722
        // Check the parameter
1723
        if (dataPackageDocidWithRev == null || dataPackageDocidWithRev.equals("")) { return docIdList; }//if
1724

    
1725
        try {
1726
            //initial a documentImpl object
1727
            DocumentImpl packageDocument = new DocumentImpl(dataPackageDocidWithRev);
1728
            //transfer to documentImpl object to string
1729
            xml = packageDocument.toString();
1730

    
1731
            //create a tripcollection object
1732
            TripleCollection tripleForPackage = new TripleCollection(
1733
                    new StringReader(xml));
1734
            //get the vetor of triples
1735
            tripleList = tripleForPackage.getCollection();
1736

    
1737
            for (int i = 0; i < tripleList.size(); i++) {
1738
                //put subject docid into docIdlist without duplicate
1739
                if (!docIdList.contains(((Triple) tripleList.elementAt(i))
1740
                        .getSubject())) {
1741
                    //put subject docid into docIdlist
1742
                    docIdList.add(((Triple) tripleList.get(i)).getSubject());
1743
                }
1744
                //put object docid into docIdlist without duplicate
1745
                if (!docIdList.contains(((Triple) tripleList.elementAt(i))
1746
                        .getObject())) {
1747
                    docIdList.add(((Triple) (tripleList.get(i))).getObject());
1748
                }
1749
            }//for
1750
        }//try
1751
        catch (Exception e) {
1752
            logMetacat.error("Error in getOldVersionAllDocumentImpl: "
1753
                    + e.getMessage());
1754
        }//catch
1755

    
1756
        // return result
1757
        return docIdList;
1758
    }//getDocidListForPackageInXMLRevisions()
1759

    
1760
    /**
1761
     * Check if the docId is a data packadge id. If the id is a data packadage
1762
     * id, it should be store in the docId fields in xml_relation table. So we
1763
     * can use a query to get the entries which the docId equals the given
1764
     * value. If the result is null. The docId is not a packadge id. Otherwise,
1765
     * it is.
1766
     *
1767
     * @param docId, the id need to be checked
1768
     */
1769
    private boolean isDataPackageId(String docId)
1770
    {
1771
        boolean result = false;
1772
        PreparedStatement pStmt = null;
1773
        ResultSet rs = null;
1774
        String query = "SELECT docId from xml_relation where docId = ?";
1775
        DBConnection dbConn = null;
1776
        int serialNumber = -1;
1777
        try {
1778
            dbConn = DBConnectionPool
1779
                    .getDBConnection("DBQuery.isDataPackageId");
1780
            serialNumber = dbConn.getCheckOutSerialNumber();
1781
            pStmt = dbConn.prepareStatement(query);
1782
            //bind the value to query
1783
            pStmt.setString(1, docId);
1784
            //execute the query
1785
            pStmt.execute();
1786
            rs = pStmt.getResultSet();
1787
            //process the result
1788
            if (rs.next()) //There are some records for the id in docId fields
1789
            {
1790
                result = true;//It is a data packadge id
1791
            }
1792
            pStmt.close();
1793
        }//try
1794
        catch (SQLException e) {
1795
            logMetacat.error("Error in isDataPackageId: "
1796
                    + e.getMessage());
1797
        } finally {
1798
            try {
1799
                pStmt.close();
1800
            }//try
1801
            catch (SQLException ee) {
1802
                logMetacat.error("Error in isDataPackageId: "
1803
                        + ee.getMessage());
1804
            }//catch
1805
            finally {
1806
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1807
            }//finally
1808
        }//finally
1809
        return result;
1810
    }//isDataPackageId()
1811

    
1812
    /**
1813
     * Check if the user has the permission to export data package
1814
     *
1815
     * @param conn, the connection
1816
     * @param docId, the id need to be checked
1817
     * @param user, the name of user
1818
     * @param groups, the user's group
1819
     */
1820
    private boolean hasPermissionToExportPackage(String docId, String user,
1821
            String[] groups) throws Exception
1822
    {
1823
        //DocumentImpl doc=new DocumentImpl(conn,docId);
1824
        return DocumentImpl.hasReadPermission(user, groups, docId);
1825
    }
1826

    
1827
    /**
1828
     * Get the current Rev for a docid in xml_documents table
1829
     *
1830
     * @param docId, the id need to get version numb If the return value is -5,
1831
     *            means no value in rev field for this docid
1832
     */
1833
    private int getCurrentRevFromXMLDoumentsTable(String docId)
1834
            throws SQLException
1835
    {
1836
        int rev = -5;
1837
        PreparedStatement pStmt = null;
1838
        ResultSet rs = null;
1839
        String query = "SELECT rev from xml_documents where docId = ?";
1840
        DBConnection dbConn = null;
1841
        int serialNumber = -1;
1842
        try {
1843
            dbConn = DBConnectionPool
1844
                    .getDBConnection("DBQuery.getCurrentRevFromXMLDocumentsTable");
1845
            serialNumber = dbConn.getCheckOutSerialNumber();
1846
            pStmt = dbConn.prepareStatement(query);
1847
            //bind the value to query
1848
            pStmt.setString(1, docId);
1849
            //execute the query
1850
            pStmt.execute();
1851
            rs = pStmt.getResultSet();
1852
            //process the result
1853
            if (rs.next()) //There are some records for rev
1854
            {
1855
                rev = rs.getInt(1);
1856
                ;//It is the version for given docid
1857
            } else {
1858
                rev = -5;
1859
            }
1860

    
1861
        }//try
1862
        catch (SQLException e) {
1863
            logMetacat.error(
1864
                    "Error in getCurrentRevFromXMLDoumentsTable: "
1865
                            + e.getMessage());
1866
            throw e;
1867
        }//catch
1868
        finally {
1869
            try {
1870
                pStmt.close();
1871
            }//try
1872
            catch (SQLException ee) {
1873
                logMetacat.error(
1874
                        "Error in getCurrentRevFromXMLDoumentsTable: "
1875
                                + ee.getMessage());
1876
            }//catch
1877
            finally {
1878
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1879
            }//finally
1880
        }//finally
1881
        return rev;
1882
    }//getCurrentRevFromXMLDoumentsTable
1883

    
1884
    /**
1885
     * put a doc into a zip output stream
1886
     *
1887
     * @param docImpl, docmentImpl object which will be sent to zip output
1888
     *            stream
1889
     * @param zipOut, zip output stream which the docImpl will be put
1890
     * @param packageZipEntry, the zip entry name for whole package
1891
     */
1892
    private void addDocToZipOutputStream(DocumentImpl docImpl,
1893
            ZipOutputStream zipOut, String packageZipEntry)
1894
            throws ClassNotFoundException, IOException, SQLException,
1895
            McdbException, Exception
1896
    {
1897
        byte[] byteString = null;
1898
        ZipEntry zEntry = null;
1899

    
1900
        byteString = docImpl.toString().getBytes();
1901
        //use docId as the zip entry's name
1902
        zEntry = new ZipEntry(packageZipEntry + "/metadata/"
1903
                + docImpl.getDocID());
1904
        zEntry.setSize(byteString.length);
1905
        zipOut.putNextEntry(zEntry);
1906
        zipOut.write(byteString, 0, byteString.length);
1907
        zipOut.closeEntry();
1908

    
1909
    }//addDocToZipOutputStream()
1910

    
1911
    /**
1912
     * Transfer a docid vetor to a documentImpl vector. The documentImpl vetor
1913
     * only inlcudes current version. If a DocumentImple object couldn't find
1914
     * for a docid, then the String of this docid was added to vetor rather
1915
     * than DocumentImple object.
1916
     *
1917
     * @param docIdList, a vetor hold a docid list for a data package. In
1918
     *            docid, there is not version number in it.
1919
     */
1920

    
1921
    private Vector getCurrentAllDocumentImpl(Vector docIdList)
1922
            throws McdbException, Exception
1923
    {
1924
        //Connection dbConn=null;
1925
        Vector documentImplList = new Vector();
1926
        int rev = 0;
1927

    
1928
        // Check the parameter
1929
        if (docIdList.isEmpty()) { return documentImplList; }//if
1930

    
1931
        //for every docid in vector
1932
        for (int i = 0; i < docIdList.size(); i++) {
1933
            try {
1934
                //get newest version for this docId
1935
                rev = getCurrentRevFromXMLDoumentsTable((String) docIdList
1936
                        .elementAt(i));
1937

    
1938
                // There is no record for this docId in xml_documents table
1939
                if (rev == -5) {
1940
                    // Rather than put DocumentImple object, put a String
1941
                    // Object(docid)
1942
                    // into the documentImplList
1943
                    documentImplList.add((String) docIdList.elementAt(i));
1944
                    // Skip other code
1945
                    continue;
1946
                }
1947

    
1948
                String docidPlusVersion = ((String) docIdList.elementAt(i))
1949
                        + MetaCatUtil.getOption("accNumSeparator") + rev;
1950

    
1951
                //create new documentImpl object
1952
                DocumentImpl documentImplObject = new DocumentImpl(
1953
                        docidPlusVersion);
1954
                //add them to vector
1955
                documentImplList.add(documentImplObject);
1956
            }//try
1957
            catch (Exception e) {
1958
                logMetacat.error("Error in getCurrentAllDocumentImpl: "
1959
                        + e.getMessage());
1960
                // continue the for loop
1961
                continue;
1962
            }
1963
        }//for
1964
        return documentImplList;
1965
    }
1966

    
1967
    /**
1968
     * Transfer a docid vetor to a documentImpl vector. If a DocumentImple
1969
     * object couldn't find for a docid, then the String of this docid was
1970
     * added to vetor rather than DocumentImple object.
1971
     *
1972
     * @param docIdList, a vetor hold a docid list for a data package. In
1973
     *            docid, t here is version number in it.
1974
     */
1975
    private Vector getOldVersionAllDocumentImpl(Vector docIdList)
1976
    {
1977
        //Connection dbConn=null;
1978
        Vector documentImplList = new Vector();
1979
        String siteCode = null;
1980
        String uniqueId = null;
1981
        int rev = 0;
1982

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

    
1986
        //for every docid in vector
1987
        for (int i = 0; i < docIdList.size(); i++) {
1988

    
1989
            String docidPlusVersion = (String) (docIdList.elementAt(i));
1990

    
1991
            try {
1992
                //create new documentImpl object
1993
                DocumentImpl documentImplObject = new DocumentImpl(
1994
                        docidPlusVersion);
1995
                //add them to vector
1996
                documentImplList.add(documentImplObject);
1997
            }//try
1998
            catch (McdbDocNotFoundException notFoundE) {
1999
                logMetacat.error(
2000
                        "Error in DBQuery.getOldVersionAllDocument" + "Imple"
2001
                                + notFoundE.getMessage());
2002
                // Rather than add a DocumentImple object into vetor, a String
2003
                // object
2004
                // - the doicd was added to the vector
2005
                documentImplList.add(docidPlusVersion);
2006
                // Continue the for loop
2007
                continue;
2008
            }//catch
2009
            catch (Exception e) {
2010
                logMetacat.error(
2011
                        "Error in DBQuery.getOldVersionAllDocument" + "Imple"
2012
                                + e.getMessage());
2013
                // Continue the for loop
2014
                continue;
2015
            }//catch
2016

    
2017
        }//for
2018
        return documentImplList;
2019
    }//getOldVersionAllDocumentImple
2020

    
2021
    /**
2022
     * put a data file into a zip output stream
2023
     *
2024
     * @param docImpl, docmentImpl object which will be sent to zip output
2025
     *            stream
2026
     * @param zipOut, the zip output stream which the docImpl will be put
2027
     * @param packageZipEntry, the zip entry name for whole package
2028
     */
2029
    private void addDataFileToZipOutputStream(DocumentImpl docImpl,
2030
            ZipOutputStream zipOut, String packageZipEntry)
2031
            throws ClassNotFoundException, IOException, SQLException,
2032
            McdbException, Exception
2033
    {
2034
        byte[] byteString = null;
2035
        ZipEntry zEntry = null;
2036
        // this is data file; add file to zip
2037
        String filePath = MetaCatUtil.getOption("datafilepath");
2038
        if (!filePath.endsWith("/")) {
2039
            filePath += "/";
2040
        }
2041
        String fileName = filePath + docImpl.getDocID();
2042
        zEntry = new ZipEntry(packageZipEntry + "/data/" + docImpl.getDocID());
2043
        zipOut.putNextEntry(zEntry);
2044
        FileInputStream fin = null;
2045
        try {
2046
            fin = new FileInputStream(fileName);
2047
            byte[] buf = new byte[4 * 1024]; // 4K buffer
2048
            int b = fin.read(buf);
2049
            while (b != -1) {
2050
                zipOut.write(buf, 0, b);
2051
                b = fin.read(buf);
2052
            }//while
2053
            zipOut.closeEntry();
2054
        }//try
2055
        catch (IOException ioe) {
2056
            logMetacat.error("There is an exception: "
2057
                    + ioe.getMessage());
2058
        }//catch
2059
    }//addDataFileToZipOutputStream()
2060

    
2061
    /**
2062
     * create a html summary for data package and put it into zip output stream
2063
     *
2064
     * @param docImplList, the documentImpl ojbects in data package
2065
     * @param zipOut, the zip output stream which the html should be put
2066
     * @param packageZipEntry, the zip entry name for whole package
2067
     */
2068
    private void addHtmlSummaryToZipOutputStream(Vector docImplList,
2069
            ZipOutputStream zipOut, String packageZipEntry) throws Exception
2070
    {
2071
        StringBuffer htmlDoc = new StringBuffer();
2072
        ZipEntry zEntry = null;
2073
        byte[] byteString = null;
2074
        InputStream source;
2075
        DBTransform xmlToHtml;
2076

    
2077
        //create a DBTransform ojbect
2078
        xmlToHtml = new DBTransform();
2079
        //head of html
2080
        htmlDoc.append("<html><head></head><body>");
2081
        for (int i = 0; i < docImplList.size(); i++) {
2082
            // If this String object, this means it is missed data file
2083
            if ((((docImplList.elementAt(i)).getClass()).toString())
2084
                    .equals("class java.lang.String")) {
2085

    
2086
                htmlDoc.append("<a href=\"");
2087
                String dataFileid = (String) docImplList.elementAt(i);
2088
                htmlDoc.append("./data/").append(dataFileid).append("\">");
2089
                htmlDoc.append("Data File: ");
2090
                htmlDoc.append(dataFileid).append("</a><br>");
2091
                htmlDoc.append("<br><hr><br>");
2092

    
2093
            }//if
2094
            else if ((((DocumentImpl) docImplList.elementAt(i)).getDoctype())
2095
                    .compareTo("BIN") != 0) { //this is an xml file so we can
2096
                                              // transform it.
2097
                //transform each file individually then concatenate all of the
2098
                //transformations together.
2099

    
2100
                //for metadata xml title
2101
                htmlDoc.append("<h2>");
2102
                htmlDoc.append(((DocumentImpl) docImplList.elementAt(i))
2103
                        .getDocID());
2104
                //htmlDoc.append(".");
2105
                //htmlDoc.append(((DocumentImpl)docImplList.elementAt(i)).getRev());
2106
                htmlDoc.append("</h2>");
2107
                //do the actual transform
2108
                StringWriter docString = new StringWriter();
2109
                xmlToHtml.transformXMLDocument(((DocumentImpl) docImplList
2110
                        .elementAt(i)).toString(), "-//NCEAS//eml-generic//EN",
2111
                        "-//W3C//HTML//EN", "html", docString);
2112
                htmlDoc.append(docString.toString());
2113
                htmlDoc.append("<br><br><hr><br><br>");
2114
            }//if
2115
            else { //this is a data file so we should link to it in the html
2116
                htmlDoc.append("<a href=\"");
2117
                String dataFileid = ((DocumentImpl) docImplList.elementAt(i))
2118
                        .getDocID();
2119
                htmlDoc.append("./data/").append(dataFileid).append("\">");
2120
                htmlDoc.append("Data File: ");
2121
                htmlDoc.append(dataFileid).append("</a><br>");
2122
                htmlDoc.append("<br><hr><br>");
2123
            }//else
2124
        }//for
2125
        htmlDoc.append("</body></html>");
2126
        byteString = htmlDoc.toString().getBytes();
2127
        zEntry = new ZipEntry(packageZipEntry + "/metadata.html");
2128
        zEntry.setSize(byteString.length);
2129
        zipOut.putNextEntry(zEntry);
2130
        zipOut.write(byteString, 0, byteString.length);
2131
        zipOut.closeEntry();
2132
        //dbConn.close();
2133

    
2134
    }//addHtmlSummaryToZipOutputStream
2135

    
2136
    /**
2137
     * put a data packadge into a zip output stream
2138
     *
2139
     * @param docId, which the user want to put into zip output stream,it has version
2140
     * @param out, a servletoutput stream which the zip output stream will be
2141
     *            put
2142
     * @param user, the username of the user
2143
     * @param groups, the group of the user
2144
     */
2145
    public ZipOutputStream getZippedPackage(String docIdString,
2146
            ServletOutputStream out, String user, String[] groups,
2147
            String passWord) throws ClassNotFoundException, IOException,
2148
            SQLException, McdbException, NumberFormatException, Exception
2149
    {
2150
        ZipOutputStream zOut = null;
2151
        String elementDocid = null;
2152
        DocumentImpl docImpls = null;
2153
        //Connection dbConn = null;
2154
        Vector docIdList = new Vector();
2155
        Vector documentImplList = new Vector();
2156
        Vector htmlDocumentImplList = new Vector();
2157
        String packageId = null;
2158
        String rootName = "package";//the package zip entry name
2159

    
2160
        String docId = null;
2161
        int version = -5;
2162
        // Docid without revision
2163
        docId = MetaCatUtil.getDocIdFromString(docIdString);
2164
        // revision number
2165
        version = MetaCatUtil.getVersionFromString(docIdString);
2166

    
2167
        //check if the reqused docId is a data package id
2168
        if (!isDataPackageId(docId)) {
2169

    
2170
            /*
2171
             * Exception e = new Exception("The request the doc id "
2172
             * +docIdString+ " is not a data package id");
2173
             */
2174

    
2175
            //CB 1/6/03: if the requested docid is not a datapackage, we just
2176
            // zip
2177
            //up the single document and return the zip file.
2178
            if (!hasPermissionToExportPackage(docId, user, groups)) {
2179

    
2180
                Exception e = new Exception("User " + user
2181
                        + " does not have permission"
2182
                        + " to export the data package " + docIdString);
2183
                throw e;
2184
            }
2185

    
2186
            docImpls = new DocumentImpl(docIdString);
2187
            //checking if the user has the permission to read the documents
2188
            if (DocumentImpl.hasReadPermission(user, groups, docImpls
2189
                    .getDocID())) {
2190
                zOut = new ZipOutputStream(out);
2191
                //if the docImpls is metadata
2192
                if ((docImpls.getDoctype()).compareTo("BIN") != 0) {
2193
                    //add metadata into zip output stream
2194
                    addDocToZipOutputStream(docImpls, zOut, rootName);
2195
                }//if
2196
                else {
2197
                    //it is data file
2198
                    addDataFileToZipOutputStream(docImpls, zOut, rootName);
2199
                    htmlDocumentImplList.add(docImpls);
2200
                }//else
2201
            }//if
2202

    
2203
            zOut.finish(); //terminate the zip file
2204
            return zOut;
2205
        }
2206
        // Check the permission of user
2207
        else if (!hasPermissionToExportPackage(docId, user, groups)) {
2208

    
2209
            Exception e = new Exception("User " + user
2210
                    + " does not have permission"
2211
                    + " to export the data package " + docIdString);
2212
            throw e;
2213
        } else //it is a packadge id
2214
        {
2215
            //store the package id
2216
            packageId = docId;
2217
            //get current version in database
2218
            int currentVersion = getCurrentRevFromXMLDoumentsTable(packageId);
2219
            //If it is for current version (-1 means user didn't specify
2220
            // revision)
2221
            if ((version == -1) || version == currentVersion) {
2222
                //get current version number
2223
                version = currentVersion;
2224
                //get package zip entry name
2225
                //it should be docId.revsion.package
2226
                rootName = packageId + MetaCatUtil.getOption("accNumSeparator")
2227
                        + version + MetaCatUtil.getOption("accNumSeparator")
2228
                        + "package";
2229
                //get the whole id list for data packadge
2230
                docIdList = getCurrentDocidListForDataPackage(packageId);
2231
                //get the whole documentImple object
2232
                documentImplList = getCurrentAllDocumentImpl(docIdList);
2233

    
2234
            }//if
2235
            else if (version > currentVersion || version < -1) {
2236
                throw new Exception("The user specified docid: " + docId + "."
2237
                        + version + " doesn't exist");
2238
            }//else if
2239
            else //for an old version
2240
            {
2241

    
2242
                rootName = docIdString
2243
                        + MetaCatUtil.getOption("accNumSeparator") + "package";
2244
                //get the whole id list for data packadge
2245
                docIdList = getOldVersionDocidListForDataPackage(docIdString);
2246

    
2247
                //get the whole documentImple object
2248
                documentImplList = getOldVersionAllDocumentImpl(docIdList);
2249
            }//else
2250

    
2251
            // Make sure documentImplist is not empty
2252
            if (documentImplList.isEmpty()) { throw new Exception(
2253
                    "Couldn't find component for data package: " + packageId); }//if
2254

    
2255
            zOut = new ZipOutputStream(out);
2256
            //put every element into zip output stream
2257
            for (int i = 0; i < documentImplList.size(); i++) {
2258
                // if the object in the vetor is String, this means we couldn't
2259
                // find
2260
                // the document locally, we need find it remote
2261
                if ((((documentImplList.elementAt(i)).getClass()).toString())
2262
                        .equals("class java.lang.String")) {
2263
                    // Get String object from vetor
2264
                    String documentId = (String) documentImplList.elementAt(i);
2265
                    logMetacat.info("docid: " + documentId);
2266
                    // Get doicd without revision
2267
                    String docidWithoutRevision = MetaCatUtil
2268
                            .getDocIdFromString(documentId);
2269
                    logMetacat.info("docidWithoutRevsion: "
2270
                            + docidWithoutRevision);
2271
                    // Get revision
2272
                    String revision = MetaCatUtil
2273
                            .getRevisionStringFromString(documentId);
2274
                    logMetacat.info("revsion from docIdentifier: "
2275
                            + revision);
2276
                    // Zip entry string
2277
                    String zipEntryPath = rootName + "/data/";
2278
                    // Create a RemoteDocument object
2279
                    RemoteDocument remoteDoc = new RemoteDocument(
2280
                            docidWithoutRevision, revision, user, passWord,
2281
                            zipEntryPath);
2282
                    // Here we only read data file from remote metacat
2283
                    String docType = remoteDoc.getDocType();
2284
                    if (docType != null) {
2285
                        if (docType.equals("BIN")) {
2286
                            // Put remote document to zip output
2287
                            remoteDoc.readDocumentFromRemoteServerByZip(zOut);
2288
                            // Add String object to htmlDocumentImplList
2289
                            String elementInHtmlList = remoteDoc
2290
                                    .getDocIdWithoutRevsion()
2291
                                    + MetaCatUtil.getOption("accNumSeparator")
2292
                                    + remoteDoc.getRevision();
2293
                            htmlDocumentImplList.add(elementInHtmlList);
2294
                        }//if
2295
                    }//if
2296

    
2297
                }//if
2298
                else {
2299
                    //create a docmentImpls object (represent xml doc) base on
2300
                    // the docId
2301
                    docImpls = (DocumentImpl) documentImplList.elementAt(i);
2302
                    //checking if the user has the permission to read the
2303
                    // documents
2304
                    if (DocumentImpl.hasReadPermission(user, groups, docImpls
2305
                            .getDocID())) {
2306
                        //if the docImpls is metadata
2307
                        if ((docImpls.getDoctype()).compareTo("BIN") != 0) {
2308
                            //add metadata into zip output stream
2309
                            addDocToZipOutputStream(docImpls, zOut, rootName);
2310
                            //add the documentImpl into the vetor which will
2311
                            // be used in html
2312
                            htmlDocumentImplList.add(docImpls);
2313

    
2314
                        }//if
2315
                        else {
2316
                            //it is data file
2317
                            addDataFileToZipOutputStream(docImpls, zOut,
2318
                                    rootName);
2319
                            htmlDocumentImplList.add(docImpls);
2320
                        }//else
2321
                    }//if
2322
                }//else
2323
            }//for
2324

    
2325
            //add html summary file
2326
            addHtmlSummaryToZipOutputStream(htmlDocumentImplList, zOut,
2327
                    rootName);
2328
            zOut.finish(); //terminate the zip file
2329
            //dbConn.close();
2330
            return zOut;
2331
        }//else
2332
    }//getZippedPackage()
2333

    
2334
    private class ReturnFieldValue
2335
    {
2336

    
2337
        private String docid = null; //return field value for this docid
2338

    
2339
        private String fieldValue = null;
2340

    
2341
        private String xmlFieldValue = null; //return field value in xml
2342
                                             // format
2343

    
2344
        public void setDocid(String myDocid)
2345
        {
2346
            docid = myDocid;
2347
        }
2348

    
2349
        public String getDocid()
2350
        {
2351
            return docid;
2352
        }
2353

    
2354
        public void setFieldValue(String myValue)
2355
        {
2356
            fieldValue = myValue;
2357
        }
2358

    
2359
        public String getFieldValue()
2360
        {
2361
            return fieldValue;
2362
        }
2363

    
2364
        public void setXMLFieldValue(String xml)
2365
        {
2366
            xmlFieldValue = xml;
2367
        }
2368

    
2369
        public String getXMLFieldValue()
2370
        {
2371
            return xmlFieldValue;
2372
        }
2373

    
2374
    }
2375
    
2376
    /**
2377
     * a class to store one result document consisting of a docid and a document
2378
     */
2379
    private class ResultDocument
2380
    {
2381
      public String docid;
2382
      public String document;
2383
      
2384
      public ResultDocument(String docid, String document)
2385
      {
2386
        this.docid = docid;
2387
        this.document = document;
2388
      }
2389
    }
2390
    
2391
    /**
2392
     * a private class to handle a set of resultDocuments
2393
     */
2394
    private class ResultDocumentSet
2395
    {
2396
      private Vector docids;
2397
      private Vector documents;
2398
      
2399
      public ResultDocumentSet()
2400
      {
2401
        docids = new Vector();
2402
        documents = new Vector();
2403
      }
2404
      
2405
      /**
2406
       * adds a result document to the set
2407
       */
2408
      public void addResultDocument(ResultDocument rd)
2409
      {
2410
        if(rd.docid == null)
2411
          return;
2412
        if(rd.document == null)
2413
          rd.document = "";
2414
        if (!containsDocid(rd.docid))
2415
        {
2416
           docids.addElement(rd.docid);
2417
           documents.addElement(rd.document);
2418
        }
2419
      }
2420
      
2421
      /**
2422
       * gets an iterator of docids
2423
       */
2424
      public Iterator getDocids()
2425
      {
2426
        return docids.iterator();
2427
      }
2428
      
2429
      /**
2430
       * gets an iterator of documents
2431
       */
2432
      public Iterator getDocuments()
2433
      {
2434
        return documents.iterator();
2435
      }
2436
      
2437
      /**
2438
       * returns the size of the set
2439
       */
2440
      public int size()
2441
      {
2442
        return docids.size();
2443
      }
2444
      
2445
      /**
2446
       * tests to see if this set contains the given docid
2447
       */
2448
      private boolean containsDocid(String docid)
2449
      {
2450
        for(int i=0; i<docids.size(); i++)
2451
        {
2452
          String docid0 = (String)docids.elementAt(i);
2453
          if(docid0.trim().equals(docid.trim()))
2454
          {
2455
            return true;
2456
          }
2457
        }
2458
        return false;
2459
      }
2460
      
2461
      /**
2462
       * removes the element with the given docid
2463
       */
2464
      public String remove(String docid)
2465
      {
2466
        for(int i=0; i<docids.size(); i++)
2467
        {
2468
          String docid0 = (String)docids.elementAt(i);
2469
          if(docid0.trim().equals(docid.trim()))
2470
          {
2471
            String returnDoc = (String)documents.elementAt(i);
2472
            documents.remove(i);
2473
            docids.remove(i);
2474
            return returnDoc;
2475
          }
2476
        }
2477
        return null;
2478
      }
2479
      
2480
      /**
2481
       * add a result document
2482
       */
2483
      public void put(ResultDocument rd)
2484
      {
2485
        addResultDocument(rd);
2486
      }
2487
      
2488
      /**
2489
       * add a result document by components
2490
       */
2491
      public void put(String docid, String document)
2492
      {
2493
        addResultDocument(new ResultDocument(docid, document));
2494
      }
2495
      
2496
      /**
2497
       * get the document part of the result document by docid
2498
       */
2499
      public Object get(String docid)
2500
      {
2501
        for(int i=0; i<docids.size(); i++)
2502
        {
2503
          String docid0 = (String)docids.elementAt(i);
2504
          if(docid0.trim().equals(docid.trim()))
2505
          {
2506
            return documents.elementAt(i);
2507
          }
2508
        }
2509
        return null;
2510
      }
2511
      
2512
      /**
2513
       * get the document part of the result document by an object
2514
       */
2515
      public Object get(Object o)
2516
      {
2517
        return get((String)o);
2518
      }
2519
      
2520
      /**
2521
       * get an entire result document by index number
2522
       */
2523
      public ResultDocument get(int index)
2524
      {
2525
        return new ResultDocument((String)docids.elementAt(index), 
2526
          (String)documents.elementAt(index));
2527
      }
2528
      
2529
      /**
2530
       * return a string representation of this object
2531
       */
2532
      public String toString()
2533
      {
2534
        String s = "";
2535
        for(int i=0; i<docids.size(); i++)
2536
        {
2537
          s += (String)docids.elementAt(i) + "\n";
2538
        }
2539
        return s;
2540
      }
2541
      /*
2542
       * Set a new document value for a given docid
2543
       */
2544
      public void set(String docid, String document)
2545
      {
2546
    	   for(int i=0; i<docids.size(); i++)
2547
           {
2548
             String docid0 = (String)docids.elementAt(i);
2549
             if(docid0.trim().equals(docid.trim()))
2550
             {
2551
                 documents.set(i, document);
2552
             }
2553
           }
2554
           
2555
      }
2556
    }
2557
}
(21-21/66)