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-22 17:20:43 -0700 (Wed, 22 Aug 2007) $'
14
 * '$Revision: 3368 $'
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 result xml string
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
    // Size of page for non paged query
91
    private static final int NONPAGESIZE = 99999999;
92
    /**
93
     * the main routine used to test the DBQuery utility.
94
     * <p>
95
     * Usage: java DBQuery <xmlfile>
96
     *
97
     * @param xmlfile the filename of the xml file containing the query
98
     */
99
    static public void main(String[] args)
100
    {
101

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

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

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

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

    
129
                double connTime = System.currentTimeMillis();
130

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

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

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

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

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

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

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

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

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

    
245
  }
246

    
247

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

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

    
302

    
303

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

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

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

    
349
      }//else
350

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

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

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

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

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

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

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

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

    
456
    //default to returning the whole resultset
457
    return resultset;
458
  }//createResultDocuments
459

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

    
505
      /*
506
       * Check the docidOverride Vector
507
       * if defined, we bypass the qspec.printSQL() method
508
       * and contruct a simpler query based on a 
509
       * list of docids rather than a bunch of subselects
510
       */
511
      if ( this.docidOverride.size() == 0 ) {
512
          query = qspec.printSQL(useXMLIndex);
513
      } else {
514
          logMetacat.info("*** docid override " + this.docidOverride.size());
515
          StringBuffer queryBuffer = new StringBuffer( "SELECT docid,docname,doctype,date_created, date_updated, rev " );
516
          queryBuffer.append( " FROM xml_documents WHERE docid IN (" );
517
          for (int i = 0; i < docidOverride.size(); i++) {  
518
              queryBuffer.append("'");
519
              queryBuffer.append( (String)docidOverride.elementAt(i) );
520
              queryBuffer.append("',");
521
          }
522
          // empty string hack 
523
          queryBuffer.append( "'') " );
524
          query = queryBuffer.toString();
525
      } 
526
      String ownerQuery = getOwnerQuery(user);
527
      logMetacat.info("\n\n\n query: " + query);
528
      logMetacat.info("\n\n\n owner query: "+ownerQuery);
529
      // if query is not the owner query, we need to check the permission
530
      // otherwise we don't need (owner has all permission by default)
531
      if (!query.equals(ownerQuery))
532
      {
533
        // set user name and group
534
        qspec.setUserName(user);
535
        qspec.setGroup(groups);
536
        // Get access query
537
        String accessQuery = qspec.getAccessQuery();
538
        if(!query.endsWith("WHERE")){
539
            query = query + accessQuery;
540
        } else {
541
            query = query + accessQuery.substring(4, accessQuery.length());
542
        }
543
        
544
      }
545
      logMetacat.warn("============ final selection query: " + query);
546
      String selectionAndExtendedQuery = null;
547
      // we only get cache for public
548
      if (user != null && user.equalsIgnoreCase("public") 
549
     		 && pagesize == 0 && MetaCatUtil.getOption("query_cache_on").equals("true"))
550
      {
551
    	  selectionAndExtendedQuery = query +qspec.getReturnDocList()+qspec.getReturnFieldList();
552
   	      String cachedResult = getResultXMLFromCache(selectionAndExtendedQuery);
553
   	      logMetacat.debug("The key of query cache is "+selectionAndExtendedQuery);
554
   	      //System.out.println("==========the string from cache is "+cachedResult);
555
   	      if (cachedResult != null)
556
   	      {
557
   	    	 if (out != null)
558
   	         {
559
   	             out.println(cachedResult);
560
   	         }
561
   	    	 resultsetBuffer.append(cachedResult);
562
   	    	 return resultsetBuffer;
563
   	      }
564
      }
565
      
566
      startTime = System.currentTimeMillis() / 1000;
567
      pstmt = dbconn.prepareStatement(query);
568
      rs = pstmt.executeQuery();
569

    
570
      double queryExecuteTime = System.currentTimeMillis() / 1000;
571
      logMetacat.warn("Time to execute select docid query is "
572
                    + (queryExecuteTime - startTime));
573
      MetaCatUtil.writeDebugToFile("\n\n\n\n\n\nExecute selection query  "
574
              + (queryExecuteTime - startTime));
575
      MetaCatUtil.writeDebugToDelimiteredFile(""+(queryExecuteTime - startTime), false);
576

    
577
      boolean tableHasRows = rs.next();
578
      
579
      if(pagesize == 0)
580
      { //this makes sure we get all results if there is no paging
581
        pagesize = NONPAGESIZE;
582
        pagestart = NONPAGESIZE;
583
      } 
584
      
585
      int currentIndex = 0;
586
      while (tableHasRows)
587
      {
588
        logMetacat.info("############getting result: " + currentIndex);
589
        docid = rs.getString(1).trim();
590
        logMetacat.info("############processing: " + docid);
591
        docname = rs.getString(2);
592
        doctype = rs.getString(3);
593
        logMetacat.info("############processing: " + doctype);
594
        createDate = rs.getString(4);
595
        updateDate = rs.getString(5);
596
        rev = rs.getInt(6);
597
        
598
         Vector returndocVec = qspec.getReturnDocList();
599
       if (returndocVec.size() == 0 || returndocVec.contains(doctype))
600
        {
601
          logMetacat.info("NOT Back tracing now...");
602
           document = new StringBuffer();
603

    
604
           String completeDocid = docid
605
                            + MetaCatUtil.getOption("accNumSeparator");
606
           completeDocid += rev;
607
           document.append("<docid>").append(completeDocid).append("</docid>");
608
           if (docname != null)
609
           {
610
               document.append("<docname>" + docname + "</docname>");
611
           }
612
           if (doctype != null)
613
           {
614
              document.append("<doctype>" + doctype + "</doctype>");
615
           }
616
           if (createDate != null)
617
           {
618
               document.append("<createdate>" + createDate + "</createdate>");
619
           }
620
           if (updateDate != null)
621
           {
622
             document.append("<updatedate>" + updateDate + "</updatedate>");
623
           }
624
           // Store the document id and the root node id
625
           
626
           docListResult.addResultDocument(
627
             new ResultDocument(docid, (String) document.toString()));
628
           logMetacat.info("$$$$$$$real result: " + docid);
629
           currentIndex++;
630
           count++;
631
        }//else
632
        
633
        // when doclist reached the offset number, send out doc list and empty
634
        // the hash table
635
        if (count == offset && pagesize == NONPAGESIZE)
636
        { //if pagesize is not 0, do this later.
637
          //reset count
638
          //logMetacat.warn("############doing subset cache");
639
          count = 0;
640
          handleSubsetResult(qspec, resultsetBuffer, out, docListResult,
641
                              user, groups,dbconn, useXMLIndex);
642
          //reset docListResult
643
          docListResult = new ResultDocumentSet();
644
        }
645
       
646
       logMetacat.info("currentIndex: " + currentIndex);
647
       logMetacat.info("page comparator: " + (pagesize * pagestart) + pagesize);
648
       if(currentIndex >= ((pagesize * pagestart) + pagesize))
649
       {
650
         ResultDocumentSet pagedResultsHash = new ResultDocumentSet();
651
         for(int i=pagesize*pagestart; i<docListResult.size(); i++)
652
         {
653
           pagedResultsHash.put(docListResult.get(i));
654
         }
655
         
656
         docListResult = pagedResultsHash;
657
         break;
658
       }
659
       // Advance to the next record in the cursor
660
       tableHasRows = rs.next();
661
       if(!tableHasRows)
662
       {
663
         ResultDocumentSet pagedResultsHash = new ResultDocumentSet();
664
         //get the last page of information then break
665
         if(pagesize != NONPAGESIZE)
666
         {
667
           for(int i=pagesize*pagestart; i<docListResult.size(); i++)
668
           {
669
             pagedResultsHash.put(docListResult.get(i));
670
           }
671
           docListResult = pagedResultsHash;
672
         }
673
         
674
         lastpage = true;
675
         break;
676
       }
677
     }//while
678
     
679
     rs.close();
680
     pstmt.close();
681
     double docListTime = System.currentTimeMillis() / 1000;
682
     logMetacat.warn("======Total time to get docid list is: "
683
                          + (docListTime - startSelectionTime ));
684
     MetaCatUtil.writeDebugToFile("---------------------------------------------------------------------------------------------------------------Total selection: "
685
             + (docListTime - startSelectionTime ));
686
     MetaCatUtil.writeDebugToDelimiteredFile(" "+ (docListTime - startSelectionTime ), false);
687
     //if docListResult is not empty, it need to be sent.
688
     if (docListResult.size() != 0)
689
     {
690
      
691
       handleSubsetResult(qspec,resultsetBuffer, out, docListResult,
692
                              user, groups,dbconn, useXMLIndex);
693
     }
694

    
695
     resultsetBuffer.append("\n<lastpage>" + lastpage + "</lastpage>\n");
696
     if (out != null)
697
     {
698
         out.println("\n<lastpage>" + lastpage + "</lastpage>\n");
699
     }
700
     
701
     // now we only cached none-paged query and user is public
702
     if (user != null && user.equalsIgnoreCase("public") 
703
    		 && pagesize == NONPAGESIZE && MetaCatUtil.getOption("query_cache_on").equals("true"))
704
     {
705
       //System.out.println("the string stored into cache is "+ resultsetBuffer.toString());
706
  	   storeQueryResultIntoCache(selectionAndExtendedQuery, resultsetBuffer.toString());
707
     }
708
          
709
     return resultsetBuffer;
710
    }//findReturnDoclist
711

    
712

    
713
    /*
714
     * Send completed search hashtable(part of reulst)to output stream
715
     * and buffer into a buffer stream
716
     */
717
    private StringBuffer handleSubsetResult(QuerySpecification qspec,
718
                                           StringBuffer resultset,
719
                                           PrintWriter out, ResultDocumentSet partOfDoclist,
720
                                           String user, String[]groups,
721
                                       DBConnection dbconn, boolean useXMLIndex)
722
                                       throws Exception
723
   {
724
     double startReturnField = System.currentTimeMillis()/1000;
725
     // check if there is a record in xml_returnfield
726
     // and get the returnfield_id and usage count
727
     int usage_count = getXmlReturnfieldsTableId(qspec, dbconn);
728
     boolean enterRecords = false;
729

    
730
     // get value of xml_returnfield_count
731
     int count = (new Integer(MetaCatUtil
732
                            .getOption("xml_returnfield_count")))
733
                            .intValue();
734

    
735
     // set enterRecords to true if usage_count is more than the offset
736
     // specified in metacat.properties
737
     if(usage_count > count){
738
         enterRecords = true;
739
     }
740

    
741
     if(returnfield_id < 0){
742
         logMetacat.warn("Error in getting returnfield id from"
743
                                  + "xml_returnfield table");
744
         enterRecords = false;
745
     }
746

    
747
     // get the hashtable containing the docids that already in the
748
     // xml_queryresult table
749
     logMetacat.info("size of partOfDoclist before"
750
                             + " docidsInQueryresultTable(): "
751
                             + partOfDoclist.size());
752
     double startGetReturnValueFromQueryresultable = System.currentTimeMillis()/1000;
753
     Hashtable queryresultDocList = docidsInQueryresultTable(returnfield_id,
754
                                                        partOfDoclist, dbconn);
755

    
756
     // remove the keys in queryresultDocList from partOfDoclist
757
     Enumeration _keys = queryresultDocList.keys();
758
     while (_keys.hasMoreElements()){
759
         partOfDoclist.remove((String)_keys.nextElement());
760
     }
761
     double endGetReturnValueFromQueryresultable = System.currentTimeMillis()/1000;
762
     logMetacat.warn("Time to get return fields from xml_queryresult table is (Part1 in return fields) " +
763
          		               (endGetReturnValueFromQueryresultable-startGetReturnValueFromQueryresultable));
764
     MetaCatUtil.writeDebugToFile("-----------------------------------------Get fields from xml_queryresult(Part1 in return fields) " +
765
               (endGetReturnValueFromQueryresultable-startGetReturnValueFromQueryresultable));
766
     MetaCatUtil.writeDebugToDelimiteredFile(" " +
767
             (endGetReturnValueFromQueryresultable-startGetReturnValueFromQueryresultable),false);
768
     // backup the keys-elements in partOfDoclist to check later
769
     // if the doc entry is indexed yet
770
     Hashtable partOfDoclistBackup = new Hashtable();
771
     Iterator itt = partOfDoclist.getDocids();
772
     while (itt.hasNext()){
773
       Object key = itt.next();
774
         partOfDoclistBackup.put(key, partOfDoclist.get(key));
775
     }
776

    
777
     logMetacat.info("size of partOfDoclist after"
778
                             + " docidsInQueryresultTable(): "
779
                             + partOfDoclist.size());
780

    
781
     //add return fields for the documents in partOfDoclist
782
     partOfDoclist = addReturnfield(partOfDoclist, qspec, user, groups,
783
                                        dbconn, useXMLIndex);
784
     double endExtendedQuery = System.currentTimeMillis()/1000;
785
     logMetacat.warn("Get fields from index and node table (Part2 in return fields) "
786
        		                                          + (endExtendedQuery - endGetReturnValueFromQueryresultable));
787
     MetaCatUtil.writeDebugToFile("-----------------------------------------Get fields from extened query(Part2 in return fields) "
788
             + (endExtendedQuery - endGetReturnValueFromQueryresultable));
789
     MetaCatUtil.writeDebugToDelimiteredFile(" "
790
             + (endExtendedQuery - endGetReturnValueFromQueryresultable), false);
791
     //add relationship part part docid list for the documents in partOfDocList
792
     partOfDoclist = addRelationship(partOfDoclist, qspec, dbconn, useXMLIndex);
793

    
794
     double startStoreReturnField = System.currentTimeMillis()/1000;
795
     Iterator keys = partOfDoclist.getDocids();
796
     String key = null;
797
     String element = null;
798
     String query = null;
799
     int offset = (new Integer(MetaCatUtil
800
                               .getOption("queryresult_string_length")))
801
                               .intValue();
802
     while (keys.hasNext())
803
     {
804
         key = (String) keys.next();
805
         element = (String)partOfDoclist.get(key);
806
         
807
	 // check if the enterRecords is true, elements is not null, element's
808
         // length is less than the limit of table column and if the document
809
         // has been indexed already
810
         if(enterRecords && element != null
811
		&& element.length() < offset
812
		&& element.compareTo((String) partOfDoclistBackup.get(key)) != 0){
813
             query = "INSERT INTO xml_queryresult (returnfield_id, docid, "
814
                 + "queryresult_string) VALUES (?, ?, ?)";
815

    
816
             PreparedStatement pstmt = null;
817
             pstmt = dbconn.prepareStatement(query);
818
             pstmt.setInt(1, returnfield_id);
819
             pstmt.setString(2, key);
820
             pstmt.setString(3, element);
821
            
822
             dbconn.increaseUsageCount(1);
823
             try
824
             {
825
            	 pstmt.execute();
826
             }
827
             catch(Exception e)
828
             {
829
            	 logMetacat.warn("couldn't insert the element to xml_queryresult table "+e.getLocalizedMessage());
830
             }
831
             finally
832
             {
833
                pstmt.close();
834
             }
835
         }
836
        
837
         // A string with element
838
         String xmlElement = "  <document>" + element + "</document>";
839

    
840
         //send single element to output
841
         if (out != null)
842
         {
843
             out.println(xmlElement);
844
         }
845
         resultset.append(xmlElement);
846
     }//while
847
     
848
     double endStoreReturnField = System.currentTimeMillis()/1000;
849
     logMetacat.warn("Time to store new return fields into xml_queryresult table (Part4 in return fields) "
850
                   + (endStoreReturnField -startStoreReturnField));
851
     MetaCatUtil.writeDebugToFile("-----------------------------------------Insert new record to xml_queryresult(Part4 in return fields) "
852
             + (endStoreReturnField -startStoreReturnField));
853
     MetaCatUtil.writeDebugToDelimiteredFile(" "
854
             + (endStoreReturnField -startStoreReturnField), false);
855
     
856
     Enumeration keysE = queryresultDocList.keys();
857
     while (keysE.hasMoreElements())
858
     {
859
         key = (String) keysE.nextElement();
860
         element = (String)queryresultDocList.get(key);
861
         // A string with element
862
         String xmlElement = "  <document>" + element + "</document>";
863
         //send single element to output
864
         if (out != null)
865
         {
866
             out.println(xmlElement);
867
         }
868
         resultset.append(xmlElement);
869
     }//while
870
     double returnFieldTime = System.currentTimeMillis() / 1000;
871
     logMetacat.warn("======Total time to get return fields is: "
872
                           + (returnFieldTime - startReturnField));
873
     MetaCatUtil.writeDebugToFile("---------------------------------------------------------------------------------------------------------------"+
874
    		 "Total to get return fields  "
875
                                   + (returnFieldTime - startReturnField));
876
     MetaCatUtil.writeDebugToDelimiteredFile(" "+ (returnFieldTime - startReturnField), false);
877
     return resultset;
878
 }
879

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

    
888
         Hashtable returnValue = new Hashtable();
889
         PreparedStatement pstmt = null;
890
         ResultSet rs = null;
891

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

    
902

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

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

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

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

    
946

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

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

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

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

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

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

    
993
           } else {
994
               rs.close();
995
               pstmt.close();
996

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

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

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

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

    
1033
       returnfield_id = id;
1034
       return count;
1035
   }
1036

    
1037

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

    
1054
      if (qspec.containsExtendedSQL())
1055
      {
1056
        qspec.setUserName(user);
1057
        qspec.setGroup(groups);
1058
        Vector extendedFields = new Vector(qspec.getReturnFieldList());
1059
        Vector results = new Vector();
1060
        Iterator keylist = docListResult.getDocids();
1061
        StringBuffer doclist = new StringBuffer();
1062
        Vector parentidList = new Vector();
1063
        Hashtable returnFieldValue = new Hashtable();
1064
        while (keylist.hasNext())
1065
        {
1066
          doclist.append("'");
1067
          doclist.append((String) keylist.next());
1068
          doclist.append("',");
1069
        }
1070
        if (doclist.length() > 0)
1071
        {
1072
          Hashtable controlPairs = new Hashtable();
1073
          doclist.deleteCharAt(doclist.length() - 1); //remove the last comma
1074
          boolean tableHasRows = false;
1075
        
1076

    
1077
           String extendedQuery =
1078
               qspec.printExtendedSQL(doclist.toString(), useXMLIndex);
1079
           logMetacat.info("Extended query: " + extendedQuery);
1080

    
1081
           if(extendedQuery != null){
1082
        	   double extendedQueryStart = System.currentTimeMillis() / 1000;
1083
               pstmt = dbconn.prepareStatement(extendedQuery);
1084
               //increase dbconnection usage count
1085
               dbconn.increaseUsageCount(1);
1086
               pstmt.execute();
1087
               rs = pstmt.getResultSet();
1088
               double extendedQueryEnd = System.currentTimeMillis() / 1000;
1089
               logMetacat.warn(
1090
                   "Time to execute extended query: "
1091
                   + (extendedQueryEnd - extendedQueryStart));
1092
               MetaCatUtil.writeDebugToFile(
1093
                       "Execute extended query "
1094
                       + (extendedQueryEnd - extendedQueryStart));
1095
               MetaCatUtil.writeDebugToDelimiteredFile(" "+ (extendedQueryEnd - extendedQueryStart), false);
1096
               tableHasRows = rs.next();
1097
               while (tableHasRows) {
1098
                   ReturnFieldValue returnValue = new ReturnFieldValue();
1099
                   docid = rs.getString(1).trim();
1100
                   fieldname = rs.getString(2);
1101
                   fielddata = rs.getString(3);
1102
                   fielddata = MetaCatUtil.normalize(fielddata);
1103
                   String parentId = rs.getString(4);
1104
                   StringBuffer value = new StringBuffer();
1105

    
1106
                   // if xml_index is used, there would be just one record per nodeid
1107
                   // as xml_index just keeps one entry for each path
1108
                   if (useXMLIndex || !containsKey(parentidList, parentId)) {
1109
                       // don't need to merger nodedata
1110
                       value.append("<param name=\"");
1111
                       value.append(fieldname);
1112
                       value.append("\">");
1113
                       value.append(fielddata);
1114
                       value.append("</param>");
1115
                       //set returnvalue
1116
                       returnValue.setDocid(docid);
1117
                       returnValue.setFieldValue(fielddata);
1118
                       returnValue.setXMLFieldValue(value.toString());
1119
                       // Store it in hastable
1120
                       putInArray(parentidList, parentId, returnValue);
1121
                   }
1122
                   else {
1123
                       // need to merge nodedata if they have same parent id and
1124
                       // node type is text
1125
                       fielddata = (String) ( (ReturnFieldValue)
1126
                                             getArrayValue(
1127
                           parentidList, parentId)).getFieldValue()
1128
                           + fielddata;
1129
                       value.append("<param name=\"");
1130
                       value.append(fieldname);
1131
                       value.append("\">");
1132
                       value.append(fielddata);
1133
                       value.append("</param>");
1134
                       returnValue.setDocid(docid);
1135
                       returnValue.setFieldValue(fielddata);
1136
                       returnValue.setXMLFieldValue(value.toString());
1137
                       // remove the old return value from paretnidList
1138
                       parentidList.remove(parentId);
1139
                       // store the new return value in parentidlit
1140
                       putInArray(parentidList, parentId, returnValue);
1141
                   }
1142
                   tableHasRows = rs.next();
1143
               } //while
1144
               rs.close();
1145
               pstmt.close();
1146

    
1147
               // put the merger node data info into doclistReult
1148
               Enumeration xmlFieldValue = (getElements(parentidList)).
1149
                   elements();
1150
               while (xmlFieldValue.hasMoreElements()) {
1151
                   ReturnFieldValue object =
1152
                       (ReturnFieldValue) xmlFieldValue.nextElement();
1153
                   docid = object.getDocid();
1154
                   if (docListResult.containsDocid(docid)) {
1155
                       String removedelement = (String) docListResult.
1156
                           remove(docid);
1157
                       docListResult.
1158
                           addResultDocument(new ResultDocument(docid,
1159
                               removedelement + object.getXMLFieldValue()));
1160
                   }
1161
                   else {
1162
                       docListResult.addResultDocument(
1163
                         new ResultDocument(docid, object.getXMLFieldValue()));
1164
                   }
1165
               } //while
1166
               double docListResultEnd = System.currentTimeMillis() / 1000;
1167
               logMetacat.warn(
1168
                   "Time to prepare ResultDocumentSet after"
1169
                   + " execute extended query: "
1170
                   + (docListResultEnd - extendedQueryEnd));
1171
           }
1172

    
1173
         
1174
           
1175
           
1176
       }//if doclist lenght is great than zero
1177

    
1178
     }//if has extended query
1179

    
1180
      return docListResult;
1181
    }//addReturnfield
1182

    
1183
    /*
1184
    * A method to add relationship to return doclist hash table
1185
    */
1186
   private ResultDocumentSet addRelationship(ResultDocumentSet docListResult,
1187
                                     QuerySpecification qspec,
1188
                                     DBConnection dbconn, boolean useXMLIndex )
1189
                                     throws Exception
1190
  {
1191
    PreparedStatement pstmt = null;
1192
    ResultSet rs = null;
1193
    StringBuffer document = null;
1194
    double startRelation = System.currentTimeMillis() / 1000;
1195
    Iterator docidkeys = docListResult.getDocids();
1196
    while (docidkeys.hasNext())
1197
    {
1198
      //String connstring =
1199
      // "metacat://"+util.getOption("server")+"?docid=";
1200
      String connstring = "%docid=";
1201
      String docidkey;
1202
      synchronized(docListResult)
1203
      {
1204
        docidkey = (String) docidkeys.next();
1205
      }
1206
      pstmt = dbconn.prepareStatement(QuerySpecification
1207
                      .printRelationSQL(docidkey));
1208
      pstmt.execute();
1209
      rs = pstmt.getResultSet();
1210
      boolean tableHasRows = rs.next();
1211
      while (tableHasRows)
1212
      {
1213
        String sub = rs.getString(1);
1214
        String rel = rs.getString(2);
1215
        String obj = rs.getString(3);
1216
        String subDT = rs.getString(4);
1217
        String objDT = rs.getString(5);
1218

    
1219
        document = new StringBuffer();
1220
        document.append("<triple>");
1221
        document.append("<subject>").append(MetaCatUtil.normalize(sub));
1222
        document.append("</subject>");
1223
        if (subDT != null)
1224
        {
1225
          document.append("<subjectdoctype>").append(subDT);
1226
          document.append("</subjectdoctype>");
1227
        }
1228
        document.append("<relationship>").append(MetaCatUtil.normalize(rel));
1229
        document.append("</relationship>");
1230
        document.append("<object>").append(MetaCatUtil.normalize(obj));
1231
        document.append("</object>");
1232
        if (objDT != null)
1233
        {
1234
          document.append("<objectdoctype>").append(objDT);
1235
          document.append("</objectdoctype>");
1236
        }
1237
        document.append("</triple>");
1238

    
1239
        String removedelement = (String) docListResult.get(docidkey);
1240
        docListResult.set(docidkey, removedelement+ document.toString());
1241
        tableHasRows = rs.next();
1242
      }//while
1243
      rs.close();
1244
      pstmt.close();
1245
      
1246
    }//while
1247
    double endRelation = System.currentTimeMillis() / 1000;
1248
    logMetacat.warn("Time to add relationship to return fields (part 3 in return fields): "
1249
                             + (endRelation - startRelation));
1250
    MetaCatUtil.writeDebugToFile("-----------------------------------------Add relationship to return field(part3 in return fields): "
1251
            + (endRelation - startRelation));
1252
    MetaCatUtil.writeDebugToDelimiteredFile(" "+ (endRelation - startRelation), false);
1253

    
1254
    return docListResult;
1255
  }//addRelation
1256

    
1257
  /**
1258
   * removes the <?xml version="1.0"?> tag from the beginning.  This takes a
1259
   * string as a param instead of a hashtable.
1260
   *
1261
   * @param xmlquery a string representing a query.
1262
   */
1263
   private  String transformQuery(String xmlquery)
1264
   {
1265
     xmlquery = xmlquery.trim();
1266
     int index = xmlquery.indexOf("?>");
1267
     if (index != -1)
1268
     {
1269
       return xmlquery.substring(index + 2, xmlquery.length());
1270
     }
1271
     else
1272
     {
1273
       return xmlquery;
1274
     }
1275
   }
1276
   
1277
   /*
1278
    * Method to store query string and result xml string into query result
1279
    * cache. If the size alreay reache the limitation, the cache will be
1280
    * cleared first, then store them.
1281
    */
1282
   private void storeQueryResultIntoCache(String query, String resultXML)
1283
   {
1284
	   synchronized (queryResultCache)
1285
	   {
1286
		   if (queryResultCache.size() >= QUERYRESULTCACHESIZE)
1287
		   {
1288
			   queryResultCache.clear();
1289
		   }
1290
		   queryResultCache.put(query, resultXML);
1291
		   
1292
	   }
1293
   }
1294
   
1295
   /*
1296
    * Method to get result xml string from query result cache. 
1297
    * Note: the returned string can be null.
1298
    */
1299
   private String getResultXMLFromCache(String query)
1300
   {
1301
	   String resultSet = null;
1302
	   synchronized (queryResultCache)
1303
	   {
1304
          try
1305
          {
1306
        	 logMetacat.info("Get query from cache ===");
1307
		     resultSet = (String)queryResultCache.get(query);
1308
		   
1309
          }
1310
          catch (Exception e)
1311
          {
1312
        	  resultSet = null;
1313
          }
1314
		   
1315
	   }
1316
	   return resultSet;
1317
   }
1318
   
1319
   /**
1320
    * Method to clear the query result cache.
1321
    */
1322
   public static void clearQueryResultCache()
1323
   {
1324
	   synchronized (queryResultCache)
1325
	   {
1326
		   queryResultCache.clear();
1327
	   }
1328
   }
1329

    
1330

    
1331
    /*
1332
     * A method to search if Vector contains a particular key string
1333
     */
1334
    private boolean containsKey(Vector parentidList, String parentId)
1335
    {
1336

    
1337
        Vector tempVector = null;
1338

    
1339
        for (int count = 0; count < parentidList.size(); count++) {
1340
            tempVector = (Vector) parentidList.get(count);
1341
            if (parentId.compareTo((String) tempVector.get(0)) == 0) { return true; }
1342
        }
1343
        return false;
1344
    }
1345

    
1346
    /*
1347
     * A method to put key and value in Vector
1348
     */
1349
    private void putInArray(Vector parentidList, String key,
1350
            ReturnFieldValue value)
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) {
1359
                tempVector.remove(1);
1360
                tempVector.add(1, value);
1361
                return;
1362
            }
1363
        }
1364

    
1365
        tempVector = new Vector();
1366
        tempVector.add(0, key);
1367
        tempVector.add(1, value);
1368
        parentidList.add(tempVector);
1369
        return;
1370
    }
1371

    
1372
    /*
1373
     * A method to get value in Vector given a key
1374
     */
1375
    private ReturnFieldValue getArrayValue(Vector parentidList, String key)
1376
    {
1377

    
1378
        Vector tempVector = null;
1379

    
1380
        for (int count = 0; count < parentidList.size(); count++) {
1381
            tempVector = (Vector) parentidList.get(count);
1382

    
1383
            if (key.compareTo((String) tempVector.get(0)) == 0) { return (ReturnFieldValue) tempVector
1384
                    .get(1); }
1385
        }
1386
        return null;
1387
    }
1388

    
1389
    /*
1390
     * A method to get enumeration of all values in Vector
1391
     */
1392
    private Vector getElements(Vector parentidList)
1393
    {
1394
        Vector enumVector = new Vector();
1395
        Vector tempVector = null;
1396

    
1397
        for (int count = 0; count < parentidList.size(); count++) {
1398
            tempVector = (Vector) parentidList.get(count);
1399

    
1400
            enumVector.add(tempVector.get(1));
1401
        }
1402
        return enumVector;
1403
    }
1404

    
1405
  
1406

    
1407
    /*
1408
     * A method to create a query to get owner's docid list
1409
     */
1410
    private String getOwnerQuery(String owner)
1411
    {
1412
        if (owner != null) {
1413
            owner = owner.toLowerCase();
1414
        }
1415
        StringBuffer self = new StringBuffer();
1416

    
1417
        self.append("SELECT docid,docname,doctype,");
1418
        self.append("date_created, date_updated, rev ");
1419
        self.append("FROM xml_documents WHERE docid IN (");
1420
        self.append("(");
1421
        self.append("SELECT DISTINCT docid FROM xml_nodes WHERE \n");
1422
        self.append("nodedata LIKE '%%%' ");
1423
        self.append(") \n");
1424
        self.append(") ");
1425
        self.append(" AND (");
1426
        self.append(" lower(user_owner) = '" + owner + "'");
1427
        self.append(") ");
1428
        return self.toString();
1429
    }
1430

    
1431
    /**
1432
     * format a structured query as an XML document that conforms to the
1433
     * pathquery.dtd and is appropriate for submission to the DBQuery
1434
     * structured query engine
1435
     *
1436
     * @param params The list of parameters that should be included in the
1437
     *            query
1438
     */
1439
    public static String createSQuery(Hashtable params)
1440
    {
1441
        StringBuffer query = new StringBuffer();
1442
        Enumeration elements;
1443
        Enumeration keys;
1444
        String filterDoctype = null;
1445
        String casesensitive = null;
1446
        String searchmode = null;
1447
        Object nextkey;
1448
        Object nextelement;
1449
        //add the xml headers
1450
        query.append("<?xml version=\"1.0\"?>\n");
1451
        query.append("<pathquery version=\"1.2\">\n");
1452

    
1453

    
1454

    
1455
        if (params.containsKey("meta_file_id")) {
1456
            query.append("<meta_file_id>");
1457
            query.append(((String[]) params.get("meta_file_id"))[0]);
1458
            query.append("</meta_file_id>");
1459
        }
1460

    
1461
        if (params.containsKey("returndoctype")) {
1462
            String[] returnDoctypes = ((String[]) params.get("returndoctype"));
1463
            for (int i = 0; i < returnDoctypes.length; i++) {
1464
                String doctype = (String) returnDoctypes[i];
1465

    
1466
                if (!doctype.equals("any") && !doctype.equals("ANY")
1467
                        && !doctype.equals("")) {
1468
                    query.append("<returndoctype>").append(doctype);
1469
                    query.append("</returndoctype>");
1470
                }
1471
            }
1472
        }
1473

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

    
1482
        if (params.containsKey("returnfield")) {
1483
            String[] returnfield = ((String[]) params.get("returnfield"));
1484
            for (int i = 0; i < returnfield.length; i++) {
1485
                query.append("<returnfield>").append(returnfield[i]);
1486
                query.append("</returnfield>");
1487
            }
1488
        }
1489

    
1490
        if (params.containsKey("owner")) {
1491
            String[] owner = ((String[]) params.get("owner"));
1492
            for (int i = 0; i < owner.length; i++) {
1493
                query.append("<owner>").append(owner[i]);
1494
                query.append("</owner>");
1495
            }
1496
        }
1497

    
1498
        if (params.containsKey("site")) {
1499
            String[] site = ((String[]) params.get("site"));
1500
            for (int i = 0; i < site.length; i++) {
1501
                query.append("<site>").append(site[i]);
1502
                query.append("</site>");
1503
            }
1504
        }
1505

    
1506
        //allows the dynamic switching of boolean operators
1507
        if (params.containsKey("operator")) {
1508
            query.append("<querygroup operator=\""
1509
                    + ((String[]) params.get("operator"))[0] + "\">");
1510
        } else { //the default operator is UNION
1511
            query.append("<querygroup operator=\"UNION\">");
1512
        }
1513

    
1514
        if (params.containsKey("casesensitive")) {
1515
            casesensitive = ((String[]) params.get("casesensitive"))[0];
1516
        } else {
1517
            casesensitive = "false";
1518
        }
1519

    
1520
        if (params.containsKey("searchmode")) {
1521
            searchmode = ((String[]) params.get("searchmode"))[0];
1522
        } else {
1523
            searchmode = "contains";
1524
        }
1525

    
1526
        //anyfield is a special case because it does a
1527
        //free text search. It does not have a <pathexpr>
1528
        //tag. This allows for a free text search within the structured
1529
        //query. This is useful if the INTERSECT operator is used.
1530
        if (params.containsKey("anyfield")) {
1531
            String[] anyfield = ((String[]) params.get("anyfield"));
1532
            //allow for more than one value for anyfield
1533
            for (int i = 0; i < anyfield.length; i++) {
1534
                if (!anyfield[i].equals("")) {
1535
                    query.append("<queryterm casesensitive=\"" + casesensitive
1536
                            + "\" " + "searchmode=\"" + searchmode
1537
                            + "\"><value>" + anyfield[i]
1538
                            + "</value></queryterm>");
1539
                }
1540
            }
1541
        }
1542

    
1543
        //this while loop finds the rest of the parameters
1544
        //and attempts to query for the field specified
1545
        //by the parameter.
1546
        elements = params.elements();
1547
        keys = params.keys();
1548
        while (keys.hasMoreElements() && elements.hasMoreElements()) {
1549
            nextkey = keys.nextElement();
1550
            nextelement = elements.nextElement();
1551

    
1552
            //make sure we aren't querying for any of these
1553
            //parameters since the are already in the query
1554
            //in one form or another.
1555
            Vector ignoredParams = new Vector();
1556
            ignoredParams.add("returndoctype");
1557
            ignoredParams.add("filterdoctype");
1558
            ignoredParams.add("action");
1559
            ignoredParams.add("qformat");
1560
            ignoredParams.add("anyfield");
1561
            ignoredParams.add("returnfield");
1562
            ignoredParams.add("owner");
1563
            ignoredParams.add("site");
1564
            ignoredParams.add("operator");
1565
            ignoredParams.add("sessionid");
1566
            ignoredParams.add("pagesize");
1567
            ignoredParams.add("pagestart");
1568

    
1569
            // Also ignore parameters listed in the properties file
1570
            // so that they can be passed through to stylesheets
1571
            String paramsToIgnore = MetaCatUtil
1572
                    .getOption("query.ignored.params");
1573
            StringTokenizer st = new StringTokenizer(paramsToIgnore, ",");
1574
            while (st.hasMoreTokens()) {
1575
                ignoredParams.add(st.nextToken());
1576
            }
1577
            if (!ignoredParams.contains(nextkey.toString())) {
1578
                //allow for more than value per field name
1579
                for (int i = 0; i < ((String[]) nextelement).length; i++) {
1580
                    if (!((String[]) nextelement)[i].equals("")) {
1581
                        query.append("<queryterm casesensitive=\""
1582
                                + casesensitive + "\" " + "searchmode=\""
1583
                                + searchmode + "\">" + "<value>" +
1584
                                //add the query value
1585
                                ((String[]) nextelement)[i]
1586
                                + "</value><pathexpr>" +
1587
                                //add the path to query by
1588
                                nextkey.toString() + "</pathexpr></queryterm>");
1589
                    }
1590
                }
1591
            }
1592
        }
1593
        query.append("</querygroup></pathquery>");
1594
        //append on the end of the xml and return the result as a string
1595
        return query.toString();
1596
    }
1597

    
1598
    /**
1599
     * format a simple free-text value query as an XML document that conforms
1600
     * to the pathquery.dtd and is appropriate for submission to the DBQuery
1601
     * structured query engine
1602
     *
1603
     * @param value the text string to search for in the xml catalog
1604
     * @param doctype the type of documents to include in the result set -- use
1605
     *            "any" or "ANY" for unfiltered result sets
1606
     */
1607
    public static String createQuery(String value, String doctype)
1608
    {
1609
        StringBuffer xmlquery = new StringBuffer();
1610
        xmlquery.append("<?xml version=\"1.0\"?>\n");
1611
        xmlquery.append("<pathquery version=\"1.0\">");
1612

    
1613
        if (!doctype.equals("any") && !doctype.equals("ANY")) {
1614
            xmlquery.append("<returndoctype>");
1615
            xmlquery.append(doctype).append("</returndoctype>");
1616
        }
1617

    
1618
        xmlquery.append("<querygroup operator=\"UNION\">");
1619
        //chad added - 8/14
1620
        //the if statement allows a query to gracefully handle a null
1621
        //query. Without this if a nullpointerException is thrown.
1622
        if (!value.equals("")) {
1623
            xmlquery.append("<queryterm casesensitive=\"false\" ");
1624
            xmlquery.append("searchmode=\"contains\">");
1625
            xmlquery.append("<value>").append(value).append("</value>");
1626
            xmlquery.append("</queryterm>");
1627
        }
1628
        xmlquery.append("</querygroup>");
1629
        xmlquery.append("</pathquery>");
1630

    
1631
        return (xmlquery.toString());
1632
    }
1633

    
1634
    /**
1635
     * format a simple free-text value query as an XML document that conforms
1636
     * to the pathquery.dtd and is appropriate for submission to the DBQuery
1637
     * structured query engine
1638
     *
1639
     * @param value the text string to search for in the xml catalog
1640
     */
1641
    public static String createQuery(String value)
1642
    {
1643
        return createQuery(value, "any");
1644
    }
1645

    
1646
    /**
1647
     * Check for "READ" permission on @docid for @user and/or @group from DB
1648
     * connection
1649
     */
1650
    private boolean hasPermission(String user, String[] groups, String docid)
1651
            throws SQLException, Exception
1652
    {
1653
        // Check for READ permission on @docid for @user and/or @groups
1654
        PermissionController controller = new PermissionController(docid);
1655
        return controller.hasPermission(user, groups,
1656
                AccessControlInterface.READSTRING);
1657
    }
1658

    
1659
    /**
1660
     * Get all docIds list for a data packadge
1661
     *
1662
     * @param dataPackageDocid, the string in docId field of xml_relation table
1663
     */
1664
    private Vector getCurrentDocidListForDataPackage(String dataPackageDocid)
1665
    {
1666
        DBConnection dbConn = null;
1667
        int serialNumber = -1;
1668
        Vector docIdList = new Vector();//return value
1669
        PreparedStatement pStmt = null;
1670
        ResultSet rs = null;
1671
        String docIdInSubjectField = null;
1672
        String docIdInObjectField = null;
1673

    
1674
        // Check the parameter
1675
        if (dataPackageDocid == null || dataPackageDocid.equals("")) { return docIdList; }//if
1676

    
1677
        //the query stirng
1678
        String query = "SELECT subject, object from xml_relation where docId = ?";
1679
        try {
1680
            dbConn = DBConnectionPool
1681
                    .getDBConnection("DBQuery.getCurrentDocidListForDataPackage");
1682
            serialNumber = dbConn.getCheckOutSerialNumber();
1683
            pStmt = dbConn.prepareStatement(query);
1684
            //bind the value to query
1685
            pStmt.setString(1, dataPackageDocid);
1686

    
1687
            //excute the query
1688
            pStmt.execute();
1689
            //get the result set
1690
            rs = pStmt.getResultSet();
1691
            //process the result
1692
            while (rs.next()) {
1693
                //In order to get the whole docIds in a data packadge,
1694
                //we need to put the docIds of subject and object field in
1695
                // xml_relation
1696
                //into the return vector
1697
                docIdInSubjectField = rs.getString(1);//the result docId in
1698
                                                      // subject field
1699
                docIdInObjectField = rs.getString(2);//the result docId in
1700
                                                     // object field
1701

    
1702
                //don't put the duplicate docId into the vector
1703
                if (!docIdList.contains(docIdInSubjectField)) {
1704
                    docIdList.add(docIdInSubjectField);
1705
                }
1706

    
1707
                //don't put the duplicate docId into the vector
1708
                if (!docIdList.contains(docIdInObjectField)) {
1709
                    docIdList.add(docIdInObjectField);
1710
                }
1711
            }//while
1712
            //close the pStmt
1713
            pStmt.close();
1714
        }//try
1715
        catch (SQLException e) {
1716
            logMetacat.error("Error in getDocidListForDataPackage: "
1717
                    + e.getMessage());
1718
        }//catch
1719
        finally {
1720
            try {
1721
                pStmt.close();
1722
            }//try
1723
            catch (SQLException ee) {
1724
                logMetacat.error(
1725
                        "Error in getDocidListForDataPackage: "
1726
                                + ee.getMessage());
1727
            }//catch
1728
            finally {
1729
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1730
            }//fianlly
1731
        }//finally
1732
        return docIdList;
1733
    }//getCurrentDocidListForDataPackadge()
1734

    
1735
    /**
1736
     * Get all docIds list for a data packadge
1737
     *
1738
     * @param dataPackageDocid, the string in docId field of xml_relation table
1739
     */
1740
    private Vector getOldVersionDocidListForDataPackage(String dataPackageDocidWithRev)
1741
    {
1742

    
1743
        Vector docIdList = new Vector();//return value
1744
        Vector tripleList = null;
1745
        String xml = null;
1746

    
1747
        // Check the parameter
1748
        if (dataPackageDocidWithRev == null || dataPackageDocidWithRev.equals("")) { return docIdList; }//if
1749

    
1750
        try {
1751
            //initial a documentImpl object
1752
            DocumentImpl packageDocument = new DocumentImpl(dataPackageDocidWithRev);
1753
            //transfer to documentImpl object to string
1754
            xml = packageDocument.toString();
1755

    
1756
            //create a tripcollection object
1757
            TripleCollection tripleForPackage = new TripleCollection(
1758
                    new StringReader(xml));
1759
            //get the vetor of triples
1760
            tripleList = tripleForPackage.getCollection();
1761

    
1762
            for (int i = 0; i < tripleList.size(); i++) {
1763
                //put subject docid into docIdlist without duplicate
1764
                if (!docIdList.contains(((Triple) tripleList.elementAt(i))
1765
                        .getSubject())) {
1766
                    //put subject docid into docIdlist
1767
                    docIdList.add(((Triple) tripleList.get(i)).getSubject());
1768
                }
1769
                //put object docid into docIdlist without duplicate
1770
                if (!docIdList.contains(((Triple) tripleList.elementAt(i))
1771
                        .getObject())) {
1772
                    docIdList.add(((Triple) (tripleList.get(i))).getObject());
1773
                }
1774
            }//for
1775
        }//try
1776
        catch (Exception e) {
1777
            logMetacat.error("Error in getOldVersionAllDocumentImpl: "
1778
                    + e.getMessage());
1779
        }//catch
1780

    
1781
        // return result
1782
        return docIdList;
1783
    }//getDocidListForPackageInXMLRevisions()
1784

    
1785
    /**
1786
     * Check if the docId is a data packadge id. If the id is a data packadage
1787
     * id, it should be store in the docId fields in xml_relation table. So we
1788
     * can use a query to get the entries which the docId equals the given
1789
     * value. If the result is null. The docId is not a packadge id. Otherwise,
1790
     * it is.
1791
     *
1792
     * @param docId, the id need to be checked
1793
     */
1794
    private boolean isDataPackageId(String docId)
1795
    {
1796
        boolean result = false;
1797
        PreparedStatement pStmt = null;
1798
        ResultSet rs = null;
1799
        String query = "SELECT docId from xml_relation where docId = ?";
1800
        DBConnection dbConn = null;
1801
        int serialNumber = -1;
1802
        try {
1803
            dbConn = DBConnectionPool
1804
                    .getDBConnection("DBQuery.isDataPackageId");
1805
            serialNumber = dbConn.getCheckOutSerialNumber();
1806
            pStmt = dbConn.prepareStatement(query);
1807
            //bind the value to query
1808
            pStmt.setString(1, docId);
1809
            //execute the query
1810
            pStmt.execute();
1811
            rs = pStmt.getResultSet();
1812
            //process the result
1813
            if (rs.next()) //There are some records for the id in docId fields
1814
            {
1815
                result = true;//It is a data packadge id
1816
            }
1817
            pStmt.close();
1818
        }//try
1819
        catch (SQLException e) {
1820
            logMetacat.error("Error in isDataPackageId: "
1821
                    + e.getMessage());
1822
        } finally {
1823
            try {
1824
                pStmt.close();
1825
            }//try
1826
            catch (SQLException ee) {
1827
                logMetacat.error("Error in isDataPackageId: "
1828
                        + ee.getMessage());
1829
            }//catch
1830
            finally {
1831
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1832
            }//finally
1833
        }//finally
1834
        return result;
1835
    }//isDataPackageId()
1836

    
1837
    /**
1838
     * Check if the user has the permission to export data package
1839
     *
1840
     * @param conn, the connection
1841
     * @param docId, the id need to be checked
1842
     * @param user, the name of user
1843
     * @param groups, the user's group
1844
     */
1845
    private boolean hasPermissionToExportPackage(String docId, String user,
1846
            String[] groups) throws Exception
1847
    {
1848
        //DocumentImpl doc=new DocumentImpl(conn,docId);
1849
        return DocumentImpl.hasReadPermission(user, groups, docId);
1850
    }
1851

    
1852
    /**
1853
     * Get the current Rev for a docid in xml_documents table
1854
     *
1855
     * @param docId, the id need to get version numb If the return value is -5,
1856
     *            means no value in rev field for this docid
1857
     */
1858
    private int getCurrentRevFromXMLDoumentsTable(String docId)
1859
            throws SQLException
1860
    {
1861
        int rev = -5;
1862
        PreparedStatement pStmt = null;
1863
        ResultSet rs = null;
1864
        String query = "SELECT rev from xml_documents where docId = ?";
1865
        DBConnection dbConn = null;
1866
        int serialNumber = -1;
1867
        try {
1868
            dbConn = DBConnectionPool
1869
                    .getDBConnection("DBQuery.getCurrentRevFromXMLDocumentsTable");
1870
            serialNumber = dbConn.getCheckOutSerialNumber();
1871
            pStmt = dbConn.prepareStatement(query);
1872
            //bind the value to query
1873
            pStmt.setString(1, docId);
1874
            //execute the query
1875
            pStmt.execute();
1876
            rs = pStmt.getResultSet();
1877
            //process the result
1878
            if (rs.next()) //There are some records for rev
1879
            {
1880
                rev = rs.getInt(1);
1881
                ;//It is the version for given docid
1882
            } else {
1883
                rev = -5;
1884
            }
1885

    
1886
        }//try
1887
        catch (SQLException e) {
1888
            logMetacat.error(
1889
                    "Error in getCurrentRevFromXMLDoumentsTable: "
1890
                            + e.getMessage());
1891
            throw e;
1892
        }//catch
1893
        finally {
1894
            try {
1895
                pStmt.close();
1896
            }//try
1897
            catch (SQLException ee) {
1898
                logMetacat.error(
1899
                        "Error in getCurrentRevFromXMLDoumentsTable: "
1900
                                + ee.getMessage());
1901
            }//catch
1902
            finally {
1903
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1904
            }//finally
1905
        }//finally
1906
        return rev;
1907
    }//getCurrentRevFromXMLDoumentsTable
1908

    
1909
    /**
1910
     * put a doc into a zip output stream
1911
     *
1912
     * @param docImpl, docmentImpl object which will be sent to zip output
1913
     *            stream
1914
     * @param zipOut, zip output stream which the docImpl will be put
1915
     * @param packageZipEntry, the zip entry name for whole package
1916
     */
1917
    private void addDocToZipOutputStream(DocumentImpl docImpl,
1918
            ZipOutputStream zipOut, String packageZipEntry)
1919
            throws ClassNotFoundException, IOException, SQLException,
1920
            McdbException, Exception
1921
    {
1922
        byte[] byteString = null;
1923
        ZipEntry zEntry = null;
1924

    
1925
        byteString = docImpl.toString().getBytes();
1926
        //use docId as the zip entry's name
1927
        zEntry = new ZipEntry(packageZipEntry + "/metadata/"
1928
                + docImpl.getDocID());
1929
        zEntry.setSize(byteString.length);
1930
        zipOut.putNextEntry(zEntry);
1931
        zipOut.write(byteString, 0, byteString.length);
1932
        zipOut.closeEntry();
1933

    
1934
    }//addDocToZipOutputStream()
1935

    
1936
    /**
1937
     * Transfer a docid vetor to a documentImpl vector. The documentImpl vetor
1938
     * only inlcudes current version. If a DocumentImple object couldn't find
1939
     * for a docid, then the String of this docid was added to vetor rather
1940
     * than DocumentImple object.
1941
     *
1942
     * @param docIdList, a vetor hold a docid list for a data package. In
1943
     *            docid, there is not version number in it.
1944
     */
1945

    
1946
    private Vector getCurrentAllDocumentImpl(Vector docIdList)
1947
            throws McdbException, Exception
1948
    {
1949
        //Connection dbConn=null;
1950
        Vector documentImplList = new Vector();
1951
        int rev = 0;
1952

    
1953
        // Check the parameter
1954
        if (docIdList.isEmpty()) { return documentImplList; }//if
1955

    
1956
        //for every docid in vector
1957
        for (int i = 0; i < docIdList.size(); i++) {
1958
            try {
1959
                //get newest version for this docId
1960
                rev = getCurrentRevFromXMLDoumentsTable((String) docIdList
1961
                        .elementAt(i));
1962

    
1963
                // There is no record for this docId in xml_documents table
1964
                if (rev == -5) {
1965
                    // Rather than put DocumentImple object, put a String
1966
                    // Object(docid)
1967
                    // into the documentImplList
1968
                    documentImplList.add((String) docIdList.elementAt(i));
1969
                    // Skip other code
1970
                    continue;
1971
                }
1972

    
1973
                String docidPlusVersion = ((String) docIdList.elementAt(i))
1974
                        + MetaCatUtil.getOption("accNumSeparator") + rev;
1975

    
1976
                //create new documentImpl object
1977
                DocumentImpl documentImplObject = new DocumentImpl(
1978
                        docidPlusVersion);
1979
                //add them to vector
1980
                documentImplList.add(documentImplObject);
1981
            }//try
1982
            catch (Exception e) {
1983
                logMetacat.error("Error in getCurrentAllDocumentImpl: "
1984
                        + e.getMessage());
1985
                // continue the for loop
1986
                continue;
1987
            }
1988
        }//for
1989
        return documentImplList;
1990
    }
1991

    
1992
    /**
1993
     * Transfer a docid vetor to a documentImpl vector. If a DocumentImple
1994
     * object couldn't find for a docid, then the String of this docid was
1995
     * added to vetor rather than DocumentImple object.
1996
     *
1997
     * @param docIdList, a vetor hold a docid list for a data package. In
1998
     *            docid, t here is version number in it.
1999
     */
2000
    private Vector getOldVersionAllDocumentImpl(Vector docIdList)
2001
    {
2002
        //Connection dbConn=null;
2003
        Vector documentImplList = new Vector();
2004
        String siteCode = null;
2005
        String uniqueId = null;
2006
        int rev = 0;
2007

    
2008
        // Check the parameter
2009
        if (docIdList.isEmpty()) { return documentImplList; }//if
2010

    
2011
        //for every docid in vector
2012
        for (int i = 0; i < docIdList.size(); i++) {
2013

    
2014
            String docidPlusVersion = (String) (docIdList.elementAt(i));
2015

    
2016
            try {
2017
                //create new documentImpl object
2018
                DocumentImpl documentImplObject = new DocumentImpl(
2019
                        docidPlusVersion);
2020
                //add them to vector
2021
                documentImplList.add(documentImplObject);
2022
            }//try
2023
            catch (McdbDocNotFoundException notFoundE) {
2024
                logMetacat.error(
2025
                        "Error in DBQuery.getOldVersionAllDocument" + "Imple"
2026
                                + notFoundE.getMessage());
2027
                // Rather than add a DocumentImple object into vetor, a String
2028
                // object
2029
                // - the doicd was added to the vector
2030
                documentImplList.add(docidPlusVersion);
2031
                // Continue the for loop
2032
                continue;
2033
            }//catch
2034
            catch (Exception e) {
2035
                logMetacat.error(
2036
                        "Error in DBQuery.getOldVersionAllDocument" + "Imple"
2037
                                + e.getMessage());
2038
                // Continue the for loop
2039
                continue;
2040
            }//catch
2041

    
2042
        }//for
2043
        return documentImplList;
2044
    }//getOldVersionAllDocumentImple
2045

    
2046
    /**
2047
     * put a data file into a zip output stream
2048
     *
2049
     * @param docImpl, docmentImpl object which will be sent to zip output
2050
     *            stream
2051
     * @param zipOut, the zip output stream which the docImpl will be put
2052
     * @param packageZipEntry, the zip entry name for whole package
2053
     */
2054
    private void addDataFileToZipOutputStream(DocumentImpl docImpl,
2055
            ZipOutputStream zipOut, String packageZipEntry)
2056
            throws ClassNotFoundException, IOException, SQLException,
2057
            McdbException, Exception
2058
    {
2059
        byte[] byteString = null;
2060
        ZipEntry zEntry = null;
2061
        // this is data file; add file to zip
2062
        String filePath = MetaCatUtil.getOption("datafilepath");
2063
        if (!filePath.endsWith("/")) {
2064
            filePath += "/";
2065
        }
2066
        String fileName = filePath + docImpl.getDocID();
2067
        zEntry = new ZipEntry(packageZipEntry + "/data/" + docImpl.getDocID());
2068
        zipOut.putNextEntry(zEntry);
2069
        FileInputStream fin = null;
2070
        try {
2071
            fin = new FileInputStream(fileName);
2072
            byte[] buf = new byte[4 * 1024]; // 4K buffer
2073
            int b = fin.read(buf);
2074
            while (b != -1) {
2075
                zipOut.write(buf, 0, b);
2076
                b = fin.read(buf);
2077
            }//while
2078
            zipOut.closeEntry();
2079
        }//try
2080
        catch (IOException ioe) {
2081
            logMetacat.error("There is an exception: "
2082
                    + ioe.getMessage());
2083
        }//catch
2084
    }//addDataFileToZipOutputStream()
2085

    
2086
    /**
2087
     * create a html summary for data package and put it into zip output stream
2088
     *
2089
     * @param docImplList, the documentImpl ojbects in data package
2090
     * @param zipOut, the zip output stream which the html should be put
2091
     * @param packageZipEntry, the zip entry name for whole package
2092
     */
2093
    private void addHtmlSummaryToZipOutputStream(Vector docImplList,
2094
            ZipOutputStream zipOut, String packageZipEntry) throws Exception
2095
    {
2096
        StringBuffer htmlDoc = new StringBuffer();
2097
        ZipEntry zEntry = null;
2098
        byte[] byteString = null;
2099
        InputStream source;
2100
        DBTransform xmlToHtml;
2101

    
2102
        //create a DBTransform ojbect
2103
        xmlToHtml = new DBTransform();
2104
        //head of html
2105
        htmlDoc.append("<html><head></head><body>");
2106
        for (int i = 0; i < docImplList.size(); i++) {
2107
            // If this String object, this means it is missed data file
2108
            if ((((docImplList.elementAt(i)).getClass()).toString())
2109
                    .equals("class java.lang.String")) {
2110

    
2111
                htmlDoc.append("<a href=\"");
2112
                String dataFileid = (String) docImplList.elementAt(i);
2113
                htmlDoc.append("./data/").append(dataFileid).append("\">");
2114
                htmlDoc.append("Data File: ");
2115
                htmlDoc.append(dataFileid).append("</a><br>");
2116
                htmlDoc.append("<br><hr><br>");
2117

    
2118
            }//if
2119
            else if ((((DocumentImpl) docImplList.elementAt(i)).getDoctype())
2120
                    .compareTo("BIN") != 0) { //this is an xml file so we can
2121
                                              // transform it.
2122
                //transform each file individually then concatenate all of the
2123
                //transformations together.
2124

    
2125
                //for metadata xml title
2126
                htmlDoc.append("<h2>");
2127
                htmlDoc.append(((DocumentImpl) docImplList.elementAt(i))
2128
                        .getDocID());
2129
                //htmlDoc.append(".");
2130
                //htmlDoc.append(((DocumentImpl)docImplList.elementAt(i)).getRev());
2131
                htmlDoc.append("</h2>");
2132
                //do the actual transform
2133
                StringWriter docString = new StringWriter();
2134
                xmlToHtml.transformXMLDocument(((DocumentImpl) docImplList
2135
                        .elementAt(i)).toString(), "-//NCEAS//eml-generic//EN",
2136
                        "-//W3C//HTML//EN", "html", docString);
2137
                htmlDoc.append(docString.toString());
2138
                htmlDoc.append("<br><br><hr><br><br>");
2139
            }//if
2140
            else { //this is a data file so we should link to it in the html
2141
                htmlDoc.append("<a href=\"");
2142
                String dataFileid = ((DocumentImpl) docImplList.elementAt(i))
2143
                        .getDocID();
2144
                htmlDoc.append("./data/").append(dataFileid).append("\">");
2145
                htmlDoc.append("Data File: ");
2146
                htmlDoc.append(dataFileid).append("</a><br>");
2147
                htmlDoc.append("<br><hr><br>");
2148
            }//else
2149
        }//for
2150
        htmlDoc.append("</body></html>");
2151
        byteString = htmlDoc.toString().getBytes();
2152
        zEntry = new ZipEntry(packageZipEntry + "/metadata.html");
2153
        zEntry.setSize(byteString.length);
2154
        zipOut.putNextEntry(zEntry);
2155
        zipOut.write(byteString, 0, byteString.length);
2156
        zipOut.closeEntry();
2157
        //dbConn.close();
2158

    
2159
    }//addHtmlSummaryToZipOutputStream
2160

    
2161
    /**
2162
     * put a data packadge into a zip output stream
2163
     *
2164
     * @param docId, which the user want to put into zip output stream,it has version
2165
     * @param out, a servletoutput stream which the zip output stream will be
2166
     *            put
2167
     * @param user, the username of the user
2168
     * @param groups, the group of the user
2169
     */
2170
    public ZipOutputStream getZippedPackage(String docIdString,
2171
            ServletOutputStream out, String user, String[] groups,
2172
            String passWord) throws ClassNotFoundException, IOException,
2173
            SQLException, McdbException, NumberFormatException, Exception
2174
    {
2175
        ZipOutputStream zOut = null;
2176
        String elementDocid = null;
2177
        DocumentImpl docImpls = null;
2178
        //Connection dbConn = null;
2179
        Vector docIdList = new Vector();
2180
        Vector documentImplList = new Vector();
2181
        Vector htmlDocumentImplList = new Vector();
2182
        String packageId = null;
2183
        String rootName = "package";//the package zip entry name
2184

    
2185
        String docId = null;
2186
        int version = -5;
2187
        // Docid without revision
2188
        docId = MetaCatUtil.getDocIdFromString(docIdString);
2189
        // revision number
2190
        version = MetaCatUtil.getVersionFromString(docIdString);
2191

    
2192
        //check if the reqused docId is a data package id
2193
        if (!isDataPackageId(docId)) {
2194

    
2195
            /*
2196
             * Exception e = new Exception("The request the doc id "
2197
             * +docIdString+ " is not a data package id");
2198
             */
2199

    
2200
            //CB 1/6/03: if the requested docid is not a datapackage, we just
2201
            // zip
2202
            //up the single document and return the zip file.
2203
            if (!hasPermissionToExportPackage(docId, user, groups)) {
2204

    
2205
                Exception e = new Exception("User " + user
2206
                        + " does not have permission"
2207
                        + " to export the data package " + docIdString);
2208
                throw e;
2209
            }
2210

    
2211
            docImpls = new DocumentImpl(docIdString);
2212
            //checking if the user has the permission to read the documents
2213
            if (DocumentImpl.hasReadPermission(user, groups, docImpls
2214
                    .getDocID())) {
2215
                zOut = new ZipOutputStream(out);
2216
                //if the docImpls is metadata
2217
                if ((docImpls.getDoctype()).compareTo("BIN") != 0) {
2218
                    //add metadata into zip output stream
2219
                    addDocToZipOutputStream(docImpls, zOut, rootName);
2220
                }//if
2221
                else {
2222
                    //it is data file
2223
                    addDataFileToZipOutputStream(docImpls, zOut, rootName);
2224
                    htmlDocumentImplList.add(docImpls);
2225
                }//else
2226
            }//if
2227

    
2228
            zOut.finish(); //terminate the zip file
2229
            return zOut;
2230
        }
2231
        // Check the permission of user
2232
        else if (!hasPermissionToExportPackage(docId, user, groups)) {
2233

    
2234
            Exception e = new Exception("User " + user
2235
                    + " does not have permission"
2236
                    + " to export the data package " + docIdString);
2237
            throw e;
2238
        } else //it is a packadge id
2239
        {
2240
            //store the package id
2241
            packageId = docId;
2242
            //get current version in database
2243
            int currentVersion = getCurrentRevFromXMLDoumentsTable(packageId);
2244
            //If it is for current version (-1 means user didn't specify
2245
            // revision)
2246
            if ((version == -1) || version == currentVersion) {
2247
                //get current version number
2248
                version = currentVersion;
2249
                //get package zip entry name
2250
                //it should be docId.revsion.package
2251
                rootName = packageId + MetaCatUtil.getOption("accNumSeparator")
2252
                        + version + MetaCatUtil.getOption("accNumSeparator")
2253
                        + "package";
2254
                //get the whole id list for data packadge
2255
                docIdList = getCurrentDocidListForDataPackage(packageId);
2256
                //get the whole documentImple object
2257
                documentImplList = getCurrentAllDocumentImpl(docIdList);
2258

    
2259
            }//if
2260
            else if (version > currentVersion || version < -1) {
2261
                throw new Exception("The user specified docid: " + docId + "."
2262
                        + version + " doesn't exist");
2263
            }//else if
2264
            else //for an old version
2265
            {
2266

    
2267
                rootName = docIdString
2268
                        + MetaCatUtil.getOption("accNumSeparator") + "package";
2269
                //get the whole id list for data packadge
2270
                docIdList = getOldVersionDocidListForDataPackage(docIdString);
2271

    
2272
                //get the whole documentImple object
2273
                documentImplList = getOldVersionAllDocumentImpl(docIdList);
2274
            }//else
2275

    
2276
            // Make sure documentImplist is not empty
2277
            if (documentImplList.isEmpty()) { throw new Exception(
2278
                    "Couldn't find component for data package: " + packageId); }//if
2279

    
2280
            zOut = new ZipOutputStream(out);
2281
            //put every element into zip output stream
2282
            for (int i = 0; i < documentImplList.size(); i++) {
2283
                // if the object in the vetor is String, this means we couldn't
2284
                // find
2285
                // the document locally, we need find it remote
2286
                if ((((documentImplList.elementAt(i)).getClass()).toString())
2287
                        .equals("class java.lang.String")) {
2288
                    // Get String object from vetor
2289
                    String documentId = (String) documentImplList.elementAt(i);
2290
                    logMetacat.info("docid: " + documentId);
2291
                    // Get doicd without revision
2292
                    String docidWithoutRevision = MetaCatUtil
2293
                            .getDocIdFromString(documentId);
2294
                    logMetacat.info("docidWithoutRevsion: "
2295
                            + docidWithoutRevision);
2296
                    // Get revision
2297
                    String revision = MetaCatUtil
2298
                            .getRevisionStringFromString(documentId);
2299
                    logMetacat.info("revsion from docIdentifier: "
2300
                            + revision);
2301
                    // Zip entry string
2302
                    String zipEntryPath = rootName + "/data/";
2303
                    // Create a RemoteDocument object
2304
                    RemoteDocument remoteDoc = new RemoteDocument(
2305
                            docidWithoutRevision, revision, user, passWord,
2306
                            zipEntryPath);
2307
                    // Here we only read data file from remote metacat
2308
                    String docType = remoteDoc.getDocType();
2309
                    if (docType != null) {
2310
                        if (docType.equals("BIN")) {
2311
                            // Put remote document to zip output
2312
                            remoteDoc.readDocumentFromRemoteServerByZip(zOut);
2313
                            // Add String object to htmlDocumentImplList
2314
                            String elementInHtmlList = remoteDoc
2315
                                    .getDocIdWithoutRevsion()
2316
                                    + MetaCatUtil.getOption("accNumSeparator")
2317
                                    + remoteDoc.getRevision();
2318
                            htmlDocumentImplList.add(elementInHtmlList);
2319
                        }//if
2320
                    }//if
2321

    
2322
                }//if
2323
                else {
2324
                    //create a docmentImpls object (represent xml doc) base on
2325
                    // the docId
2326
                    docImpls = (DocumentImpl) documentImplList.elementAt(i);
2327
                    //checking if the user has the permission to read the
2328
                    // documents
2329
                    if (DocumentImpl.hasReadPermission(user, groups, docImpls
2330
                            .getDocID())) {
2331
                        //if the docImpls is metadata
2332
                        if ((docImpls.getDoctype()).compareTo("BIN") != 0) {
2333
                            //add metadata into zip output stream
2334
                            addDocToZipOutputStream(docImpls, zOut, rootName);
2335
                            //add the documentImpl into the vetor which will
2336
                            // be used in html
2337
                            htmlDocumentImplList.add(docImpls);
2338

    
2339
                        }//if
2340
                        else {
2341
                            //it is data file
2342
                            addDataFileToZipOutputStream(docImpls, zOut,
2343
                                    rootName);
2344
                            htmlDocumentImplList.add(docImpls);
2345
                        }//else
2346
                    }//if
2347
                }//else
2348
            }//for
2349

    
2350
            //add html summary file
2351
            addHtmlSummaryToZipOutputStream(htmlDocumentImplList, zOut,
2352
                    rootName);
2353
            zOut.finish(); //terminate the zip file
2354
            //dbConn.close();
2355
            return zOut;
2356
        }//else
2357
    }//getZippedPackage()
2358

    
2359
    private class ReturnFieldValue
2360
    {
2361

    
2362
        private String docid = null; //return field value for this docid
2363

    
2364
        private String fieldValue = null;
2365

    
2366
        private String xmlFieldValue = null; //return field value in xml
2367
                                             // format
2368

    
2369
        public void setDocid(String myDocid)
2370
        {
2371
            docid = myDocid;
2372
        }
2373

    
2374
        public String getDocid()
2375
        {
2376
            return docid;
2377
        }
2378

    
2379
        public void setFieldValue(String myValue)
2380
        {
2381
            fieldValue = myValue;
2382
        }
2383

    
2384
        public String getFieldValue()
2385
        {
2386
            return fieldValue;
2387
        }
2388

    
2389
        public void setXMLFieldValue(String xml)
2390
        {
2391
            xmlFieldValue = xml;
2392
        }
2393

    
2394
        public String getXMLFieldValue()
2395
        {
2396
            return xmlFieldValue;
2397
        }
2398

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