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

    
31
package edu.ucsb.nceas.metacat;
32

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

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

    
44
import org.apache.log4j.Logger;
45

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

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

    
54

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

    
64
    static final int ALL = 1;
65

    
66
    static final int WRITE = 2;
67

    
68
    static final int READ = 4;
69

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

    
73
    private MetaCatUtil util = new MetaCatUtil();
74

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

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

    
80
    /** useful if you just want to grab a list of docids **/
81
    Vector docidOverride = new Vector();
82

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

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

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

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

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

    
120
                double connTime = System.currentTimeMillis();
121

    
122
                // Execute the query
123
                DBQuery queryobj = new DBQuery();
124
                FileReader xml = new FileReader(new File(xmlfile));
125
                Hashtable nodelist = null;
126
                //nodelist = queryobj.findDocuments(xml, null, null, useXMLIndex);
127

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

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

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

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

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

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

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

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

    
236
  }
237

    
238

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

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

    
293

    
294

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

    
318
         // if the user is a moderator, then pass a param to the 
319
         // xsl specifying the fact
320
         if(MetaCatUtil.isModerator(user, groups)){
321
        	 params.put("isModerator", new String[] {"true"});
322
         }
323

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

    
340
      }//else
341

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

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

    
384
    //try to get the cached version first    
385
    Hashtable sessionHash = MetaCatServlet.getSessionHash();
386
    HttpSession sess = (HttpSession)sessionHash.get(sessionid);
387

    
388
    QuerySpecification cachedQuerySpec = null;
389
    if (sess != null)
390
    {
391
    	cachedQuerySpec = (QuerySpecification)sess.getAttribute("query");
392
    }
393
    
394
    resultset.append("<?xml version=\"1.0\"?>\n");
395
    resultset.append("<resultset>\n");
396
    resultset.append("  <pagestart>" + pagestart + "</pagestart>\n");
397
    resultset.append("  <pagesize>" + pagesize + "</pagesize>\n");
398
    resultset.append("  <nextpage>" + (pagestart + 1) + "</nextpage>\n");
399
    resultset.append("  <previouspage>" + (pagestart - 1) + "</previouspage>\n");
400

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

    
412
        //checkout the dbconnection
413
        dbconn = DBConnectionPool.getDBConnection("DBQuery.findDocuments");
414
        serialNumber = dbconn.getCheckOutSerialNumber();
415

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

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

    
451
    //default to returning the whole resultset
452
    return resultset;
453
  }//createResultDocuments
454

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

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

    
539
      startTime = System.currentTimeMillis() / 1000;
540
      pstmt = dbconn.prepareStatement(query);
541
      rs = pstmt.executeQuery();
542

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

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

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

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

    
676

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

    
694
     // get value of xml_returnfield_count
695
     int count = (new Integer(MetaCatUtil
696
                            .getOption("xml_returnfield_count")))
697
                            .intValue();
698

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

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

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

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

    
741
     logMetacat.info("size of partOfDoclist after"
742
                             + " docidsInQueryresultTable(): "
743
                             + partOfDoclist.size());
744

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

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

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

    
780
             PreparedStatement pstmt = null;
781
             pstmt = dbconn.prepareStatement(query);
782
             pstmt.setInt(1, returnfield_id);
783
             pstmt.setString(2, key);
784
             pstmt.setString(3, element);
785

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

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

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

    
842
         Hashtable returnValue = new Hashtable();
843
         PreparedStatement pstmt = null;
844
         ResultSet rs = null;
845

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

    
856

    
857
         if (doclist.length() > 0)
858
         {
859
             doclist.deleteCharAt(doclist.length() - 1); //remove the last comma
860

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

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

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

    
900

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

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

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

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

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

    
942
               pstmt = dbconn.prepareStatement(query);
943
               dbconn.increaseUsageCount(1);
944
               pstmt.execute();
945
               pstmt.close();
946

    
947
           } else {
948
               rs.close();
949
               pstmt.close();
950

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

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

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

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

    
987
       returnfield_id = id;
988
       return count;
989
   }
990

    
991

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

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

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

    
1050
           String extendedQuery =
1051
               qspec.printExtendedSQL(doclist.toString(), useXMLIndex);
1052
           logMetacat.info("Extended query: " + extendedQuery);
1053

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

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

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

    
1146
           // get attribures return
1147
           double startGetAttribute = System.currentTimeMillis()/1000;
1148
           docListResult = getAttributeValueForReturn(qspec,
1149
                           docListResult, doclist.toString(), useXMLIndex);
1150
           double endGetAttribute = System.currentTimeMillis()/1000;
1151
           logMetacat.warn(
1152
                   "Time to get attribute return value after"
1153
                   + " execute extended query: "
1154
                   + (endGetAttribute - startGetAttribute));
1155
           MetaCatUtil.writeDebugToFile(
1156
                   "Get attribute return field "
1157
                   + (endGetAttribute - startGetAttribute));
1158
           MetaCatUtil.writeDebugToDelimiteredFile(" "+ (endGetAttribute - startGetAttribute), false);
1159
           
1160
           
1161
       }//if doclist lenght is great than zero
1162

    
1163
     }//if has extended query
1164

    
1165
      return docListResult;
1166
    }//addReturnfield
1167

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

    
1204
        document = new StringBuffer();
1205
        document.append("<triple>");
1206
        document.append("<subject>").append(MetaCatUtil.normalize(sub));
1207
        document.append("</subject>");
1208
        if (subDT != null)
1209
        {
1210
          document.append("<subjectdoctype>").append(subDT);
1211
          document.append("</subjectdoctype>");
1212
        }
1213
        document.append("<relationship>").append(MetaCatUtil.normalize(rel));
1214
        document.append("</relationship>");
1215
        document.append("<object>").append(MetaCatUtil.normalize(obj));
1216
        document.append("</object>");
1217
        if (objDT != null)
1218
        {
1219
          document.append("<objectdoctype>").append(objDT);
1220
          document.append("</objectdoctype>");
1221
        }
1222
        document.append("</triple>");
1223

    
1224
        String removedelement = (String) docListResult.get(docidkey);
1225
        docListResult.set(docidkey, removedelement+ document.toString());
1226
        tableHasRows = rs.next();
1227
      }//while
1228
      rs.close();
1229
      pstmt.close();
1230
      
1231
    }//while
1232
    double endRelation = System.currentTimeMillis() / 1000;
1233
    logMetacat.warn("Time to add relationship to return fields (part 3 in return fields): "
1234
                             + (endRelation - startRelation));
1235
    MetaCatUtil.writeDebugToFile("-----------------------------------------Add relationship to return field(part3 in return fields): "
1236
            + (endRelation - startRelation));
1237
    MetaCatUtil.writeDebugToDelimiteredFile(" "+ (endRelation - startRelation), false);
1238

    
1239
    return docListResult;
1240
  }//addRelation
1241

    
1242
  /**
1243
   * removes the <?xml version="1.0"?> tag from the beginning.  This takes a
1244
   * string as a param instead of a hashtable.
1245
   *
1246
   * @param xmlquery a string representing a query.
1247
   */
1248
   private  String transformQuery(String xmlquery)
1249
   {
1250
     xmlquery = xmlquery.trim();
1251
     int index = xmlquery.indexOf("?>");
1252
     if (index != -1)
1253
     {
1254
       return xmlquery.substring(index + 2, xmlquery.length());
1255
     }
1256
     else
1257
     {
1258
       return xmlquery;
1259
     }
1260
   }
1261

    
1262

    
1263
    /*
1264
     * A method to search if Vector contains a particular key string
1265
     */
1266
    private boolean containsKey(Vector parentidList, String parentId)
1267
    {
1268

    
1269
        Vector tempVector = null;
1270

    
1271
        for (int count = 0; count < parentidList.size(); count++) {
1272
            tempVector = (Vector) parentidList.get(count);
1273
            if (parentId.compareTo((String) tempVector.get(0)) == 0) { return true; }
1274
        }
1275
        return false;
1276
    }
1277

    
1278
    /*
1279
     * A method to put key and value in Vector
1280
     */
1281
    private void putInArray(Vector parentidList, String key,
1282
            ReturnFieldValue value)
1283
    {
1284

    
1285
        Vector tempVector = null;
1286

    
1287
        for (int count = 0; count < parentidList.size(); count++) {
1288
            tempVector = (Vector) parentidList.get(count);
1289

    
1290
            if (key.compareTo((String) tempVector.get(0)) == 0) {
1291
                tempVector.remove(1);
1292
                tempVector.add(1, value);
1293
                return;
1294
            }
1295
        }
1296

    
1297
        tempVector = new Vector();
1298
        tempVector.add(0, key);
1299
        tempVector.add(1, value);
1300
        parentidList.add(tempVector);
1301
        return;
1302
    }
1303

    
1304
    /*
1305
     * A method to get value in Vector given a key
1306
     */
1307
    private ReturnFieldValue getArrayValue(Vector parentidList, String key)
1308
    {
1309

    
1310
        Vector tempVector = null;
1311

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

    
1315
            if (key.compareTo((String) tempVector.get(0)) == 0) { return (ReturnFieldValue) tempVector
1316
                    .get(1); }
1317
        }
1318
        return null;
1319
    }
1320

    
1321
    /*
1322
     * A method to get enumeration of all values in Vector
1323
     */
1324
    private Vector getElements(Vector parentidList)
1325
    {
1326
        Vector enumVector = new Vector();
1327
        Vector tempVector = null;
1328

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

    
1332
            enumVector.add(tempVector.get(1));
1333
        }
1334
        return enumVector;
1335
    }
1336

    
1337
    /*
1338
     * A method to return search result after running a query which return
1339
     * field have attribue
1340
     */
1341
    private ResultDocumentSet getAttributeValueForReturn(QuerySpecification squery,
1342
            ResultDocumentSet docInformationList, String docList, boolean useXMLIndex)
1343
    {
1344
        StringBuffer XML = null;
1345
        String sql = null;
1346
        DBConnection dbconn = null;
1347
        PreparedStatement pstmt = null;
1348
        ResultSet rs = null;
1349
        int serialNumber = -1;
1350
        boolean tableHasRows = false;
1351

    
1352
        //check the parameter
1353
        if (squery == null || docList == null || docList.length() < 0) { return docInformationList; }
1354

    
1355
        // if has attribute as return field
1356
        if (squery.containsAttributeReturnField()) {
1357
            sql = squery.printAttributeQuery(docList, useXMLIndex);
1358
            try {
1359
                dbconn = DBConnectionPool
1360
                        .getDBConnection("DBQuery.getAttributeValue");
1361
                serialNumber = dbconn.getCheckOutSerialNumber();
1362
                pstmt = dbconn.prepareStatement(sql);
1363
                pstmt.execute();
1364
                rs = pstmt.getResultSet();
1365
                tableHasRows = rs.next();
1366
                while (tableHasRows) {
1367
                    String docid = rs.getString(1).trim();
1368
                    String fieldname = rs.getString(2);
1369
                    String fielddata = rs.getString(3);
1370
                    String attirbuteName = rs.getString(4);
1371
                    XML = new StringBuffer();
1372

    
1373
                    XML.append("<param name=\"");
1374
                    XML.append(fieldname);
1375
                    XML.append("/");
1376
                    XML.append(QuerySpecification.ATTRIBUTESYMBOL);
1377
                    XML.append(attirbuteName);
1378
                    XML.append("\">");
1379
                    XML.append(fielddata);
1380
                    XML.append("</param>");
1381
                    tableHasRows = rs.next();
1382

    
1383
                    if (docInformationList.containsDocid(docid)) {
1384
                        String removedelement = (String) docInformationList
1385
                                .remove(docid);
1386
                        docInformationList.put(docid, removedelement
1387
                                + XML.toString());
1388
                    } else {
1389
                        docInformationList.put(docid, XML.toString());
1390
                    }
1391
                }//while
1392
                rs.close();
1393
                pstmt.close();
1394
            } catch (Exception se) {
1395
                logMetacat.error(
1396
                        "Error in DBQuery.getAttributeValue1: "
1397
                                + se.getMessage());
1398
            } finally {
1399
                try {
1400
                    pstmt.close();
1401
                }//try
1402
                catch (SQLException sqlE) {
1403
                    logMetacat.error(
1404
                            "Error in DBQuery.getAttributeValue2: "
1405
                                    + sqlE.getMessage());
1406
                }//catch
1407
                finally {
1408
                    DBConnectionPool.returnDBConnection(dbconn, serialNumber);
1409
                }//finally
1410
            }//finally
1411
        }//if
1412
        return docInformationList;
1413

    
1414
    }
1415

    
1416
    /*
1417
     * A method to create a query to get owner's docid list
1418
     */
1419
    private String getOwnerQuery(String owner)
1420
    {
1421
        if (owner != null) {
1422
            owner = owner.toLowerCase();
1423
        }
1424
        StringBuffer self = new StringBuffer();
1425

    
1426
        self.append("SELECT docid,docname,doctype,");
1427
        self.append("date_created, date_updated, rev ");
1428
        self.append("FROM xml_documents WHERE docid IN (");
1429
        self.append("(");
1430
        self.append("SELECT DISTINCT docid FROM xml_nodes WHERE \n");
1431
        self.append("nodedata LIKE '%%%' ");
1432
        self.append(") \n");
1433
        self.append(") ");
1434
        self.append(" AND (");
1435
        self.append(" lower(user_owner) = '" + owner + "'");
1436
        self.append(") ");
1437
        return self.toString();
1438
    }
1439

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

    
1462

    
1463

    
1464
        if (params.containsKey("meta_file_id")) {
1465
            query.append("<meta_file_id>");
1466
            query.append(((String[]) params.get("meta_file_id"))[0]);
1467
            query.append("</meta_file_id>");
1468
        }
1469

    
1470
        if (params.containsKey("returndoctype")) {
1471
            String[] returnDoctypes = ((String[]) params.get("returndoctype"));
1472
            for (int i = 0; i < returnDoctypes.length; i++) {
1473
                String doctype = (String) returnDoctypes[i];
1474

    
1475
                if (!doctype.equals("any") && !doctype.equals("ANY")
1476
                        && !doctype.equals("")) {
1477
                    query.append("<returndoctype>").append(doctype);
1478
                    query.append("</returndoctype>");
1479
                }
1480
            }
1481
        }
1482

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

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

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

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

    
1515
        //allows the dynamic switching of boolean operators
1516
        if (params.containsKey("operator")) {
1517
            query.append("<querygroup operator=\""
1518
                    + ((String[]) params.get("operator"))[0] + "\">");
1519
        } else { //the default operator is UNION
1520
            query.append("<querygroup operator=\"UNION\">");
1521
        }
1522

    
1523
        if (params.containsKey("casesensitive")) {
1524
            casesensitive = ((String[]) params.get("casesensitive"))[0];
1525
        } else {
1526
            casesensitive = "false";
1527
        }
1528

    
1529
        if (params.containsKey("searchmode")) {
1530
            searchmode = ((String[]) params.get("searchmode"))[0];
1531
        } else {
1532
            searchmode = "contains";
1533
        }
1534

    
1535
        //anyfield is a special case because it does a
1536
        //free text search. It does not have a <pathexpr>
1537
        //tag. This allows for a free text search within the structured
1538
        //query. This is useful if the INTERSECT operator is used.
1539
        if (params.containsKey("anyfield")) {
1540
            String[] anyfield = ((String[]) params.get("anyfield"));
1541
            //allow for more than one value for anyfield
1542
            for (int i = 0; i < anyfield.length; i++) {
1543
                if (!anyfield[i].equals("")) {
1544
                    query.append("<queryterm casesensitive=\"" + casesensitive
1545
                            + "\" " + "searchmode=\"" + searchmode
1546
                            + "\"><value>" + anyfield[i]
1547
                            + "</value></queryterm>");
1548
                }
1549
            }
1550
        }
1551

    
1552
        //this while loop finds the rest of the parameters
1553
        //and attempts to query for the field specified
1554
        //by the parameter.
1555
        elements = params.elements();
1556
        keys = params.keys();
1557
        while (keys.hasMoreElements() && elements.hasMoreElements()) {
1558
            nextkey = keys.nextElement();
1559
            nextelement = elements.nextElement();
1560

    
1561
            //make sure we aren't querying for any of these
1562
            //parameters since the are already in the query
1563
            //in one form or another.
1564
            Vector ignoredParams = new Vector();
1565
            ignoredParams.add("returndoctype");
1566
            ignoredParams.add("filterdoctype");
1567
            ignoredParams.add("action");
1568
            ignoredParams.add("qformat");
1569
            ignoredParams.add("anyfield");
1570
            ignoredParams.add("returnfield");
1571
            ignoredParams.add("owner");
1572
            ignoredParams.add("site");
1573
            ignoredParams.add("operator");
1574
            ignoredParams.add("sessionid");
1575
            ignoredParams.add("pagesize");
1576
            ignoredParams.add("pagestart");
1577

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

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

    
1622
        if (!doctype.equals("any") && !doctype.equals("ANY")) {
1623
            xmlquery.append("<returndoctype>");
1624
            xmlquery.append(doctype).append("</returndoctype>");
1625
        }
1626

    
1627
        xmlquery.append("<querygroup operator=\"UNION\">");
1628
        //chad added - 8/14
1629
        //the if statement allows a query to gracefully handle a null
1630
        //query. Without this if a nullpointerException is thrown.
1631
        if (!value.equals("")) {
1632
            xmlquery.append("<queryterm casesensitive=\"false\" ");
1633
            xmlquery.append("searchmode=\"contains\">");
1634
            xmlquery.append("<value>").append(value).append("</value>");
1635
            xmlquery.append("</queryterm>");
1636
        }
1637
        xmlquery.append("</querygroup>");
1638
        xmlquery.append("</pathquery>");
1639

    
1640
        return (xmlquery.toString());
1641
    }
1642

    
1643
    /**
1644
     * format a simple free-text value query as an XML document that conforms
1645
     * to the pathquery.dtd and is appropriate for submission to the DBQuery
1646
     * structured query engine
1647
     *
1648
     * @param value the text string to search for in the xml catalog
1649
     */
1650
    public static String createQuery(String value)
1651
    {
1652
        return createQuery(value, "any");
1653
    }
1654

    
1655
    /**
1656
     * Check for "READ" permission on @docid for @user and/or @group from DB
1657
     * connection
1658
     */
1659
    private boolean hasPermission(String user, String[] groups, String docid)
1660
            throws SQLException, Exception
1661
    {
1662
        // Check for READ permission on @docid for @user and/or @groups
1663
        PermissionController controller = new PermissionController(docid);
1664
        return controller.hasPermission(user, groups,
1665
                AccessControlInterface.READSTRING);
1666
    }
1667

    
1668
    /**
1669
     * Get all docIds list for a data packadge
1670
     *
1671
     * @param dataPackageDocid, the string in docId field of xml_relation table
1672
     */
1673
    private Vector getCurrentDocidListForDataPackage(String dataPackageDocid)
1674
    {
1675
        DBConnection dbConn = null;
1676
        int serialNumber = -1;
1677
        Vector docIdList = new Vector();//return value
1678
        PreparedStatement pStmt = null;
1679
        ResultSet rs = null;
1680
        String docIdInSubjectField = null;
1681
        String docIdInObjectField = null;
1682

    
1683
        // Check the parameter
1684
        if (dataPackageDocid == null || dataPackageDocid.equals("")) { return docIdList; }//if
1685

    
1686
        //the query stirng
1687
        String query = "SELECT subject, object from xml_relation where docId = ?";
1688
        try {
1689
            dbConn = DBConnectionPool
1690
                    .getDBConnection("DBQuery.getCurrentDocidListForDataPackage");
1691
            serialNumber = dbConn.getCheckOutSerialNumber();
1692
            pStmt = dbConn.prepareStatement(query);
1693
            //bind the value to query
1694
            pStmt.setString(1, dataPackageDocid);
1695

    
1696
            //excute the query
1697
            pStmt.execute();
1698
            //get the result set
1699
            rs = pStmt.getResultSet();
1700
            //process the result
1701
            while (rs.next()) {
1702
                //In order to get the whole docIds in a data packadge,
1703
                //we need to put the docIds of subject and object field in
1704
                // xml_relation
1705
                //into the return vector
1706
                docIdInSubjectField = rs.getString(1);//the result docId in
1707
                                                      // subject field
1708
                docIdInObjectField = rs.getString(2);//the result docId in
1709
                                                     // object field
1710

    
1711
                //don't put the duplicate docId into the vector
1712
                if (!docIdList.contains(docIdInSubjectField)) {
1713
                    docIdList.add(docIdInSubjectField);
1714
                }
1715

    
1716
                //don't put the duplicate docId into the vector
1717
                if (!docIdList.contains(docIdInObjectField)) {
1718
                    docIdList.add(docIdInObjectField);
1719
                }
1720
            }//while
1721
            //close the pStmt
1722
            pStmt.close();
1723
        }//try
1724
        catch (SQLException e) {
1725
            logMetacat.error("Error in getDocidListForDataPackage: "
1726
                    + e.getMessage());
1727
        }//catch
1728
        finally {
1729
            try {
1730
                pStmt.close();
1731
            }//try
1732
            catch (SQLException ee) {
1733
                logMetacat.error(
1734
                        "Error in getDocidListForDataPackage: "
1735
                                + ee.getMessage());
1736
            }//catch
1737
            finally {
1738
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1739
            }//fianlly
1740
        }//finally
1741
        return docIdList;
1742
    }//getCurrentDocidListForDataPackadge()
1743

    
1744
    /**
1745
     * Get all docIds list for a data packadge
1746
     *
1747
     * @param dataPackageDocid, the string in docId field of xml_relation table
1748
     */
1749
    private Vector getOldVersionDocidListForDataPackage(String dataPackageDocidWithRev)
1750
    {
1751

    
1752
        Vector docIdList = new Vector();//return value
1753
        Vector tripleList = null;
1754
        String xml = null;
1755

    
1756
        // Check the parameter
1757
        if (dataPackageDocidWithRev == null || dataPackageDocidWithRev.equals("")) { return docIdList; }//if
1758

    
1759
        try {
1760
            //initial a documentImpl object
1761
            DocumentImpl packageDocument = new DocumentImpl(dataPackageDocidWithRev);
1762
            //transfer to documentImpl object to string
1763
            xml = packageDocument.toString();
1764

    
1765
            //create a tripcollection object
1766
            TripleCollection tripleForPackage = new TripleCollection(
1767
                    new StringReader(xml));
1768
            //get the vetor of triples
1769
            tripleList = tripleForPackage.getCollection();
1770

    
1771
            for (int i = 0; i < tripleList.size(); i++) {
1772
                //put subject docid into docIdlist without duplicate
1773
                if (!docIdList.contains(((Triple) tripleList.elementAt(i))
1774
                        .getSubject())) {
1775
                    //put subject docid into docIdlist
1776
                    docIdList.add(((Triple) tripleList.get(i)).getSubject());
1777
                }
1778
                //put object docid into docIdlist without duplicate
1779
                if (!docIdList.contains(((Triple) tripleList.elementAt(i))
1780
                        .getObject())) {
1781
                    docIdList.add(((Triple) (tripleList.get(i))).getObject());
1782
                }
1783
            }//for
1784
        }//try
1785
        catch (Exception e) {
1786
            logMetacat.error("Error in getOldVersionAllDocumentImpl: "
1787
                    + e.getMessage());
1788
        }//catch
1789

    
1790
        // return result
1791
        return docIdList;
1792
    }//getDocidListForPackageInXMLRevisions()
1793

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

    
1846
    /**
1847
     * Check if the user has the permission to export data package
1848
     *
1849
     * @param conn, the connection
1850
     * @param docId, the id need to be checked
1851
     * @param user, the name of user
1852
     * @param groups, the user's group
1853
     */
1854
    private boolean hasPermissionToExportPackage(String docId, String user,
1855
            String[] groups) throws Exception
1856
    {
1857
        //DocumentImpl doc=new DocumentImpl(conn,docId);
1858
        return DocumentImpl.hasReadPermission(user, groups, docId);
1859
    }
1860

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

    
1895
        }//try
1896
        catch (SQLException e) {
1897
            logMetacat.error(
1898
                    "Error in getCurrentRevFromXMLDoumentsTable: "
1899
                            + e.getMessage());
1900
            throw e;
1901
        }//catch
1902
        finally {
1903
            try {
1904
                pStmt.close();
1905
            }//try
1906
            catch (SQLException ee) {
1907
                logMetacat.error(
1908
                        "Error in getCurrentRevFromXMLDoumentsTable: "
1909
                                + ee.getMessage());
1910
            }//catch
1911
            finally {
1912
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1913
            }//finally
1914
        }//finally
1915
        return rev;
1916
    }//getCurrentRevFromXMLDoumentsTable
1917

    
1918
    /**
1919
     * put a doc into a zip output stream
1920
     *
1921
     * @param docImpl, docmentImpl object which will be sent to zip output
1922
     *            stream
1923
     * @param zipOut, zip output stream which the docImpl will be put
1924
     * @param packageZipEntry, the zip entry name for whole package
1925
     */
1926
    private void addDocToZipOutputStream(DocumentImpl docImpl,
1927
            ZipOutputStream zipOut, String packageZipEntry)
1928
            throws ClassNotFoundException, IOException, SQLException,
1929
            McdbException, Exception
1930
    {
1931
        byte[] byteString = null;
1932
        ZipEntry zEntry = null;
1933

    
1934
        byteString = docImpl.toString().getBytes();
1935
        //use docId as the zip entry's name
1936
        zEntry = new ZipEntry(packageZipEntry + "/metadata/"
1937
                + docImpl.getDocID());
1938
        zEntry.setSize(byteString.length);
1939
        zipOut.putNextEntry(zEntry);
1940
        zipOut.write(byteString, 0, byteString.length);
1941
        zipOut.closeEntry();
1942

    
1943
    }//addDocToZipOutputStream()
1944

    
1945
    /**
1946
     * Transfer a docid vetor to a documentImpl vector. The documentImpl vetor
1947
     * only inlcudes current version. If a DocumentImple object couldn't find
1948
     * for a docid, then the String of this docid was added to vetor rather
1949
     * than DocumentImple object.
1950
     *
1951
     * @param docIdList, a vetor hold a docid list for a data package. In
1952
     *            docid, there is not version number in it.
1953
     */
1954

    
1955
    private Vector getCurrentAllDocumentImpl(Vector docIdList)
1956
            throws McdbException, Exception
1957
    {
1958
        //Connection dbConn=null;
1959
        Vector documentImplList = new Vector();
1960
        int rev = 0;
1961

    
1962
        // Check the parameter
1963
        if (docIdList.isEmpty()) { return documentImplList; }//if
1964

    
1965
        //for every docid in vector
1966
        for (int i = 0; i < docIdList.size(); i++) {
1967
            try {
1968
                //get newest version for this docId
1969
                rev = getCurrentRevFromXMLDoumentsTable((String) docIdList
1970
                        .elementAt(i));
1971

    
1972
                // There is no record for this docId in xml_documents table
1973
                if (rev == -5) {
1974
                    // Rather than put DocumentImple object, put a String
1975
                    // Object(docid)
1976
                    // into the documentImplList
1977
                    documentImplList.add((String) docIdList.elementAt(i));
1978
                    // Skip other code
1979
                    continue;
1980
                }
1981

    
1982
                String docidPlusVersion = ((String) docIdList.elementAt(i))
1983
                        + MetaCatUtil.getOption("accNumSeparator") + rev;
1984

    
1985
                //create new documentImpl object
1986
                DocumentImpl documentImplObject = new DocumentImpl(
1987
                        docidPlusVersion);
1988
                //add them to vector
1989
                documentImplList.add(documentImplObject);
1990
            }//try
1991
            catch (Exception e) {
1992
                logMetacat.error("Error in getCurrentAllDocumentImpl: "
1993
                        + e.getMessage());
1994
                // continue the for loop
1995
                continue;
1996
            }
1997
        }//for
1998
        return documentImplList;
1999
    }
2000

    
2001
    /**
2002
     * Transfer a docid vetor to a documentImpl vector. If a DocumentImple
2003
     * object couldn't find for a docid, then the String of this docid was
2004
     * added to vetor rather than DocumentImple object.
2005
     *
2006
     * @param docIdList, a vetor hold a docid list for a data package. In
2007
     *            docid, t here is version number in it.
2008
     */
2009
    private Vector getOldVersionAllDocumentImpl(Vector docIdList)
2010
    {
2011
        //Connection dbConn=null;
2012
        Vector documentImplList = new Vector();
2013
        String siteCode = null;
2014
        String uniqueId = null;
2015
        int rev = 0;
2016

    
2017
        // Check the parameter
2018
        if (docIdList.isEmpty()) { return documentImplList; }//if
2019

    
2020
        //for every docid in vector
2021
        for (int i = 0; i < docIdList.size(); i++) {
2022

    
2023
            String docidPlusVersion = (String) (docIdList.elementAt(i));
2024

    
2025
            try {
2026
                //create new documentImpl object
2027
                DocumentImpl documentImplObject = new DocumentImpl(
2028
                        docidPlusVersion);
2029
                //add them to vector
2030
                documentImplList.add(documentImplObject);
2031
            }//try
2032
            catch (McdbDocNotFoundException notFoundE) {
2033
                logMetacat.error(
2034
                        "Error in DBQuery.getOldVersionAllDocument" + "Imple"
2035
                                + notFoundE.getMessage());
2036
                // Rather than add a DocumentImple object into vetor, a String
2037
                // object
2038
                // - the doicd was added to the vector
2039
                documentImplList.add(docidPlusVersion);
2040
                // Continue the for loop
2041
                continue;
2042
            }//catch
2043
            catch (Exception e) {
2044
                logMetacat.error(
2045
                        "Error in DBQuery.getOldVersionAllDocument" + "Imple"
2046
                                + e.getMessage());
2047
                // Continue the for loop
2048
                continue;
2049
            }//catch
2050

    
2051
        }//for
2052
        return documentImplList;
2053
    }//getOldVersionAllDocumentImple
2054

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

    
2095
    /**
2096
     * create a html summary for data package and put it into zip output stream
2097
     *
2098
     * @param docImplList, the documentImpl ojbects in data package
2099
     * @param zipOut, the zip output stream which the html should be put
2100
     * @param packageZipEntry, the zip entry name for whole package
2101
     */
2102
    private void addHtmlSummaryToZipOutputStream(Vector docImplList,
2103
            ZipOutputStream zipOut, String packageZipEntry) throws Exception
2104
    {
2105
        StringBuffer htmlDoc = new StringBuffer();
2106
        ZipEntry zEntry = null;
2107
        byte[] byteString = null;
2108
        InputStream source;
2109
        DBTransform xmlToHtml;
2110

    
2111
        //create a DBTransform ojbect
2112
        xmlToHtml = new DBTransform();
2113
        //head of html
2114
        htmlDoc.append("<html><head></head><body>");
2115
        for (int i = 0; i < docImplList.size(); i++) {
2116
            // If this String object, this means it is missed data file
2117
            if ((((docImplList.elementAt(i)).getClass()).toString())
2118
                    .equals("class java.lang.String")) {
2119

    
2120
                htmlDoc.append("<a href=\"");
2121
                String dataFileid = (String) docImplList.elementAt(i);
2122
                htmlDoc.append("./data/").append(dataFileid).append("\">");
2123
                htmlDoc.append("Data File: ");
2124
                htmlDoc.append(dataFileid).append("</a><br>");
2125
                htmlDoc.append("<br><hr><br>");
2126

    
2127
            }//if
2128
            else if ((((DocumentImpl) docImplList.elementAt(i)).getDoctype())
2129
                    .compareTo("BIN") != 0) { //this is an xml file so we can
2130
                                              // transform it.
2131
                //transform each file individually then concatenate all of the
2132
                //transformations together.
2133

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

    
2168
    }//addHtmlSummaryToZipOutputStream
2169

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

    
2194
        String docId = null;
2195
        int version = -5;
2196
        // Docid without revision
2197
        docId = MetaCatUtil.getDocIdFromString(docIdString);
2198
        // revision number
2199
        version = MetaCatUtil.getVersionFromString(docIdString);
2200

    
2201
        //check if the reqused docId is a data package id
2202
        if (!isDataPackageId(docId)) {
2203

    
2204
            /*
2205
             * Exception e = new Exception("The request the doc id "
2206
             * +docIdString+ " is not a data package id");
2207
             */
2208

    
2209
            //CB 1/6/03: if the requested docid is not a datapackage, we just
2210
            // zip
2211
            //up the single document and return the zip file.
2212
            if (!hasPermissionToExportPackage(docId, user, groups)) {
2213

    
2214
                Exception e = new Exception("User " + user
2215
                        + " does not have permission"
2216
                        + " to export the data package " + docIdString);
2217
                throw e;
2218
            }
2219

    
2220
            docImpls = new DocumentImpl(docIdString);
2221
            //checking if the user has the permission to read the documents
2222
            if (DocumentImpl.hasReadPermission(user, groups, docImpls
2223
                    .getDocID())) {
2224
                zOut = new ZipOutputStream(out);
2225
                //if the docImpls is metadata
2226
                if ((docImpls.getDoctype()).compareTo("BIN") != 0) {
2227
                    //add metadata into zip output stream
2228
                    addDocToZipOutputStream(docImpls, zOut, rootName);
2229
                }//if
2230
                else {
2231
                    //it is data file
2232
                    addDataFileToZipOutputStream(docImpls, zOut, rootName);
2233
                    htmlDocumentImplList.add(docImpls);
2234
                }//else
2235
            }//if
2236

    
2237
            zOut.finish(); //terminate the zip file
2238
            return zOut;
2239
        }
2240
        // Check the permission of user
2241
        else if (!hasPermissionToExportPackage(docId, user, groups)) {
2242

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

    
2268
            }//if
2269
            else if (version > currentVersion || version < -1) {
2270
                throw new Exception("The user specified docid: " + docId + "."
2271
                        + version + " doesn't exist");
2272
            }//else if
2273
            else //for an old version
2274
            {
2275

    
2276
                rootName = docIdString
2277
                        + MetaCatUtil.getOption("accNumSeparator") + "package";
2278
                //get the whole id list for data packadge
2279
                docIdList = getOldVersionDocidListForDataPackage(docIdString);
2280

    
2281
                //get the whole documentImple object
2282
                documentImplList = getOldVersionAllDocumentImpl(docIdList);
2283
            }//else
2284

    
2285
            // Make sure documentImplist is not empty
2286
            if (documentImplList.isEmpty()) { throw new Exception(
2287
                    "Couldn't find component for data package: " + packageId); }//if
2288

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

    
2331
                }//if
2332
                else {
2333
                    //create a docmentImpls object (represent xml doc) base on
2334
                    // the docId
2335
                    docImpls = (DocumentImpl) documentImplList.elementAt(i);
2336
                    //checking if the user has the permission to read the
2337
                    // documents
2338
                    if (DocumentImpl.hasReadPermission(user, groups, docImpls
2339
                            .getDocID())) {
2340
                        //if the docImpls is metadata
2341
                        if ((docImpls.getDoctype()).compareTo("BIN") != 0) {
2342
                            //add metadata into zip output stream
2343
                            addDocToZipOutputStream(docImpls, zOut, rootName);
2344
                            //add the documentImpl into the vetor which will
2345
                            // be used in html
2346
                            htmlDocumentImplList.add(docImpls);
2347

    
2348
                        }//if
2349
                        else {
2350
                            //it is data file
2351
                            addDataFileToZipOutputStream(docImpls, zOut,
2352
                                    rootName);
2353
                            htmlDocumentImplList.add(docImpls);
2354
                        }//else
2355
                    }//if
2356
                }//else
2357
            }//for
2358

    
2359
            //add html summary file
2360
            addHtmlSummaryToZipOutputStream(htmlDocumentImplList, zOut,
2361
                    rootName);
2362
            zOut.finish(); //terminate the zip file
2363
            //dbConn.close();
2364
            return zOut;
2365
        }//else
2366
    }//getZippedPackage()
2367

    
2368
    private class ReturnFieldValue
2369
    {
2370

    
2371
        private String docid = null; //return field value for this docid
2372

    
2373
        private String fieldValue = null;
2374

    
2375
        private String xmlFieldValue = null; //return field value in xml
2376
                                             // format
2377

    
2378
        public void setDocid(String myDocid)
2379
        {
2380
            docid = myDocid;
2381
        }
2382

    
2383
        public String getDocid()
2384
        {
2385
            return docid;
2386
        }
2387

    
2388
        public void setFieldValue(String myValue)
2389
        {
2390
            fieldValue = myValue;
2391
        }
2392

    
2393
        public String getFieldValue()
2394
        {
2395
            return fieldValue;
2396
        }
2397

    
2398
        public void setXMLFieldValue(String xml)
2399
        {
2400
            xmlFieldValue = xml;
2401
        }
2402

    
2403
        public String getXMLFieldValue()
2404
        {
2405
            return xmlFieldValue;
2406
        }
2407

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