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-05-05 23:07:48 -0700 (Sat, 05 May 2007) $'
14
 * '$Revision: 3253 $'
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 for transfering resultset from xml to html format is "
329
        		                             +(endHTMLTransform -startHTMLTransform));
330
        }
331
        catch(Exception e)
332
        {
333
         logMetacat.error("Error in MetaCatServlet.transformResultset:"
334
                                +e.getMessage());
335
         }
336

    
337
      }//else
338

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

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

    
381
    //try to get the cached version first    
382
    Hashtable sessionHash = MetaCatServlet.getSessionHash();
383
    HttpSession sess = (HttpSession)sessionHash.get(sessionid);
384

    
385
    QuerySpecification cachedQuerySpec = null;
386
    if (sess != null)
387
    {
388
    	cachedQuerySpec = (QuerySpecification)sess.getAttribute("query");
389
    }
390
    
391
    resultset.append("<?xml version=\"1.0\"?>\n");
392
    resultset.append("<resultset>\n");
393
    resultset.append("  <query>" + xmlquery + "</query>");
394
    //send out a new query
395
    if (out != null)
396
    {
397
      out.println(resultset.toString());
398
    }
399
    if (qspec != null)
400
    {
401
      try
402
      {
403

    
404
        //checkout the dbconnection
405
        dbconn = DBConnectionPool.getDBConnection("DBQuery.findDocuments");
406
        serialNumber = dbconn.getCheckOutSerialNumber();
407

    
408
        //print out the search result
409
        // search the doc list
410
        resultset = findResultDoclist(qspec, resultset, out, user, groups,
411
                                      dbconn, useXMLIndex, pagesize, pagestart, 
412
                                      sessionid);
413
      } //try
414
      catch (IOException ioe)
415
      {
416
        logMetacat.error("IO error in DBQuery.findDocuments:");
417
        logMetacat.error(ioe.getMessage());
418

    
419
      }
420
      catch (SQLException e)
421
      {
422
        logMetacat.error("SQL Error in DBQuery.findDocuments: "
423
                                 + e.getMessage());
424
      }
425
      catch (Exception ee)
426
      {
427
        logMetacat.error("Exception in DBQuery.findDocuments: "
428
                                 + ee.getMessage());
429
        ee.printStackTrace();
430
      }
431
      finally
432
      {
433
        DBConnectionPool.returnDBConnection(dbconn, serialNumber);
434
      } //finally
435
    }//if
436
    String closeRestultset = "</resultset>";
437
    resultset.append(closeRestultset);
438
    if (out != null)
439
    {
440
      out.println(closeRestultset);
441
    }
442

    
443
    //default to returning the whole resultset
444
    return resultset;
445
  }//createResultDocuments
446

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

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

    
531
      startTime = System.currentTimeMillis() / 1000;
532
      pstmt = dbconn.prepareStatement(query);
533
      rs = pstmt.executeQuery();
534

    
535
      double queryExecuteTime = System.currentTimeMillis() / 1000;
536
      logMetacat.warn("Time to execute select docid query is "
537
                    + (queryExecuteTime - startTime));
538

    
539
      boolean tableHasRows = rs.next();
540
      
541
      if(pagesize == 0)
542
      { //this makes sure we get all results if there is no paging
543
        pagesize = 99999;
544
        pagestart = 99999;
545
      } 
546
      
547
      int currentIndex = 0;
548
      while (tableHasRows)
549
      //for(int z=pagestart * pagesize; z<(pagesize * pagestart) + pagesize;)
550
      {
551
        logMetacat.warn("############getting result: " + currentIndex);
552
        docid = rs.getString(1).trim();
553
        logMetacat.warn("############processing: " + docid);
554
        docname = rs.getString(2);
555
        doctype = rs.getString(3);
556
        logMetacat.warn("############processing: " + doctype);
557
        createDate = rs.getString(4);
558
        updateDate = rs.getString(5);
559
        rev = rs.getInt(6);
560
        
561
        // if there are returndocs to match, backtracking can be performed
562
        // otherwise, just return the document that was hit
563
        Vector returndocVec = qspec.getReturnDocList();
564
        if (returndocVec.size() != 0 && !returndocVec.contains(doctype)
565
             && !qspec.isPercentageSearch())
566
         {
567
           logMetacat.warn("Back tracing now...");
568
           double startBackTracingTime = System.currentTimeMillis()/1000;
569
           String sep = MetaCatUtil.getOption("accNumSeparator");
570
           StringBuffer btBuf = new StringBuffer();
571
           btBuf.append("select docid from xml_relation where ");
572

    
573
           //build the doctype list for the backtracking sql statement
574
           btBuf.append("packagetype in (");
575
           for (int i = 0; i < returndocVec.size(); i++)
576
           {
577
             btBuf.append("'").append((String) returndocVec.get(i)).append("'");
578
             if (i != (returndocVec.size() - 1))
579
             {
580
                btBuf.append(", ");
581
              }
582
            }
583
            btBuf.append(") ");
584
            btBuf.append("and (subject like '");
585
            btBuf.append(docid).append("'");
586
            btBuf.append("or object like '");
587
            btBuf.append(docid).append("')");
588

    
589
            PreparedStatement npstmt = dbconn.prepareStatement(btBuf.toString());
590
            //should incease usage count
591
            dbconn.increaseUsageCount(1);
592
            npstmt.execute();
593
            ResultSet btrs = npstmt.getResultSet();
594
            boolean hasBtRows = btrs.next();
595
            Hashtable list = new Hashtable();
596
            while (hasBtRows)
597
            {
598
               //there was a backtrackable document found
599
               DocumentImpl xmldoc = null;
600
               String packageDocid = btrs.getString(1);
601
               logMetacat.info("Getting document for docid: "
602
                                         + packageDocid);
603
                try
604
                {
605
                    //  THIS CONSTRUCTOR BUILDS THE WHOLE XML doc not
606
                    // needed here
607
                    // xmldoc = new DocumentImpl(dbconn, packageDocid);
608
                    //  thus use the following to get the doc info only
609
                    //  xmldoc = new DocumentImpl(dbconn);
610
                    String accNumber = packageDocid + MetaCatUtil.getOption("accNumSeparator") +
611
                    DBUtil.getLatestRevisionInDocumentTable(packageDocid);
612
                    xmldoc = new DocumentImpl(accNumber, false);
613
                    if (xmldoc == null)
614
                    {
615
                       logMetacat.info("Document was null for: "
616
                                                + packageDocid);
617
                    }
618
                }
619
                catch (Exception e)
620
                {
621
                    System.out.println("Error getting document in "
622
                                       + "DBQuery.findDocuments: "
623
                                       + e.getMessage());
624
                }
625

    
626
                String docid_org = xmldoc.getDocID();
627
                if (docid_org == null)
628
                {
629
                   logMetacat.info("Docid_org was null.");
630
                   hasBtRows = btrs.next();
631
                   continue;
632
                }
633
                docid = docid_org.trim();
634
                if (list.containsKey(docid))
635
                {
636
                	logMetacat.info("DocumentResultSet already has docid "+docid+" and skip it");
637
                    hasBtRows = btrs.next();
638
                    continue;
639
                }
640
                docname = xmldoc.getDocname();
641
                doctype = xmldoc.getDoctype();
642
                createDate = xmldoc.getCreateDate();
643
                updateDate = xmldoc.getUpdateDate();
644
                rev = xmldoc.getRev();
645
                document = new StringBuffer();
646

    
647
                String completeDocid = docid
648
                                + MetaCatUtil.getOption("accNumSeparator");
649
                completeDocid += rev;
650
                document.append("<docid>").append(completeDocid);
651
                document.append("</docid>");
652
                if (docname != null)
653
                {
654
                  document.append("<docname>" + docname + "</docname>");
655
                }
656
                if (doctype != null)
657
                {
658
                  document.append("<doctype>" + doctype + "</doctype>");
659
                }
660
                if (createDate != null)
661
                {
662
                 document.append("<createdate>" + createDate + "</createdate>");
663
                }
664
                if (updateDate != null)
665
                {
666
                  document.append("<updatedate>" + updateDate+ "</updatedate>");
667
                }
668
                // Store the document id and the root node id
669
                docListResult.addResultDocument(
670
                  new ResultDocument(docid, (String) document.toString()));
671
                list.put(docid, docid);
672
                currentIndex++;
673
                logMetacat.warn("$$$$$$$real result: " + docid);
674
                count++;
675

    
676
                // Get the next package document linked to our hit
677
                hasBtRows = btrs.next();
678
              }//while
679
              npstmt.close();
680
              btrs.close();
681
              double endBackTracingTime = System.currentTimeMillis()/1000;
682
              logMetacat.warn("Back tracing time for one doc is "+
683
            		               (endBackTracingTime - startBackTracingTime));
684
        }
685
        else if (returndocVec.size() == 0 || returndocVec.contains(doctype))
686
        {
687
          logMetacat.warn("NOT Back tracing now...");
688
           document = new StringBuffer();
689

    
690
           String completeDocid = docid
691
                            + MetaCatUtil.getOption("accNumSeparator");
692
           completeDocid += rev;
693
           document.append("<docid>").append(completeDocid).append("</docid>");
694
           if (docname != null)
695
           {
696
               document.append("<docname>" + docname + "</docname>");
697
           }
698
           if (doctype != null)
699
           {
700
              document.append("<doctype>" + doctype + "</doctype>");
701
           }
702
           if (createDate != null)
703
           {
704
               document.append("<createdate>" + createDate + "</createdate>");
705
           }
706
           if (updateDate != null)
707
           {
708
             document.append("<updatedate>" + updateDate + "</updatedate>");
709
           }
710
           // Store the document id and the root node id
711
           
712
           docListResult.addResultDocument(
713
             new ResultDocument(docid, (String) document.toString()));
714
           logMetacat.warn("$$$$$$$real result: " + docid);
715
           currentIndex++;
716
           count++;
717
        }//else
718
        
719
        // when doclist reached the offset number, send out doc list and empty
720
        // the hash table
721
        if (count == offset && pagesize == 0)
722
        { //if pagesize is not 0, do this later.
723
          //reset count
724
          logMetacat.warn("############doing subset cache");
725
          count = 0;
726
          handleSubsetResult(qspec, resultsetBuffer, out, docListResult,
727
                              user, groups,dbconn, useXMLIndex);
728
          //reset docListResult
729
          docListResult = new ResultDocumentSet();
730
        }
731
       
732
        logMetacat.warn("currentIndex: " + currentIndex);
733
       if(currentIndex >= ((pagesize * pagestart) + pagesize))
734
       {
735
         ResultDocumentSet pagedResultsHash = new ResultDocumentSet();
736
         for(int i=pagesize*pagestart; i<docListResult.size(); i++)
737
         {
738
           pagedResultsHash.put(docListResult.get(i));
739
         }
740
         
741
         docListResult = pagedResultsHash;
742
         break;
743
       }
744
       // Advance to the next record in the cursor
745
       tableHasRows = rs.next();
746
       if(!tableHasRows)
747
       {
748
         break;
749
       }
750
     }//while
751
     
752
     rs.close();
753
     pstmt.close();
754
     double docListTime = System.currentTimeMillis() / 1000;
755
     logMetacat.warn("======Total time to get docid list is: "
756
                    + (docListTime - startSelectionTime ));
757
     //if docListResult is not empty, it need to be sent.
758
     if (docListResult.size() != 0)
759
     {
760
       handleSubsetResult(qspec,resultsetBuffer, out, docListResult,
761
                              user, groups,dbconn, useXMLIndex);
762
       double returnFieldTime = System.currentTimeMillis() / 1000;
763
       logMetacat.warn("======Total time to get return fields is: "
764
                      + (returnFieldTime - docListTime));
765
     }
766
     
767

    
768
     return resultsetBuffer;
769
    }//findReturnDoclist
770

    
771

    
772
    /*
773
     * Send completed search hashtable(part of reulst)to output stream
774
     * and buffer into a buffer stream
775
     */
776
    private StringBuffer handleSubsetResult(QuerySpecification qspec,
777
                                           StringBuffer resultset,
778
                                           PrintWriter out, ResultDocumentSet partOfDoclist,
779
                                           String user, String[]groups,
780
                                       DBConnection dbconn, boolean useXMLIndex)
781
                                       throws Exception
782
   {
783
     
784
     // check if there is a record in xml_returnfield
785
     // and get the returnfield_id and usage count
786
    double startGetReturnValueFromQueryresultable = System.currentTimeMillis()/1000;
787
     int usage_count = getXmlReturnfieldsTableId(qspec, dbconn);
788
     boolean enterRecords = false;
789

    
790
     // get value of xml_returnfield_count
791
     int count = (new Integer(MetaCatUtil
792
                            .getOption("xml_returnfield_count")))
793
                            .intValue();
794

    
795
     // set enterRecords to true if usage_count is more than the offset
796
     // specified in metacat.properties
797
     if(usage_count > count){
798
         enterRecords = true;
799
     }
800
     
801
     if(returnfield_id < 0){
802
         logMetacat.warn("Error in getting returnfield id from"
803
                                  + "xml_returnfield table");
804
         enterRecords = false;
805
     }
806

    
807
     // get the hashtable containing the docids that already in the
808
     // xml_queryresult table
809
     logMetacat.info("size of partOfDoclist before"
810
                             + " docidsInQueryresultTable(): "
811
                             + partOfDoclist.size());
812
     
813
     Hashtable queryresultDocList = docidsInQueryresultTable(returnfield_id,
814
                                                        partOfDoclist, dbconn);
815

    
816
     // remove the keys in queryresultDocList from partOfDoclist
817
     Enumeration _keys = queryresultDocList.keys();
818
     while (_keys.hasMoreElements()){
819
         partOfDoclist.remove((String)_keys.nextElement());
820
     }
821
     double endGetReturnValueFromQueryresultable = System.currentTimeMillis()/1000;
822
     logMetacat.warn("Time to get return fields from xml_queryresult table is (Part1 in return fields) " +
823
    		               (endGetReturnValueFromQueryresultable-startGetReturnValueFromQueryresultable));
824
     // backup the keys-elements in partOfDoclist to check later
825
     // if the doc entry is indexed yet
826
     Hashtable partOfDoclistBackup = new Hashtable();
827
     Iterator itt = partOfDoclist.getDocids();
828
     while (itt.hasNext()){
829
       Object key = itt.next();
830
         partOfDoclistBackup.put(key, partOfDoclist.get(key));
831
     }
832

    
833
     logMetacat.info("size of partOfDoclist after"
834
                             + " docidsInQueryresultTable(): "
835
                             + partOfDoclist.size());
836

    
837
     //add return fields for the documents in partOfDoclist
838
     partOfDoclist = addReturnfield(partOfDoclist, qspec, user, groups,
839
                                        dbconn, useXMLIndex);
840
     double endExtendedQuery = System.currentTimeMillis()/1000;
841
     logMetacat.warn("Time to get return fields through execute extended query (Part2 in return fields) "
842
    		                                          + (endExtendedQuery - endGetReturnValueFromQueryresultable));
843
     //add relationship part part docid list for the documents in partOfDocList
844
     partOfDoclist = addRelationship(partOfDoclist, qspec, dbconn, useXMLIndex);
845

    
846
     double startStoreReturnField = System.currentTimeMillis()/1000;
847
     Iterator keys = partOfDoclist.getDocids();
848
     String key = null;
849
     String element = null;
850
     String query = null;
851
     int offset = (new Integer(MetaCatUtil
852
                               .getOption("queryresult_string_length")))
853
                               .intValue();
854
     while (keys.hasNext())
855
     {
856
         key = (String) keys.next();
857
         element = (String)partOfDoclist.get(key);
858

    
859
	 // check if the enterRecords is true, elements is not null, element's
860
         // length is less than the limit of table column and if the document
861
         // has been indexed already
862
         if(enterRecords && element != null
863
		&& element.length() < offset
864
		&& element.compareTo((String) partOfDoclistBackup.get(key)) != 0){
865
             query = "INSERT INTO xml_queryresult (returnfield_id, docid, "
866
                 + "queryresult_string) VALUES (?, ?, ?)";
867

    
868
             PreparedStatement pstmt = null;
869
             pstmt = dbconn.prepareStatement(query);
870
             pstmt.setInt(1, returnfield_id);
871
             pstmt.setString(2, key);
872
             pstmt.setString(3, element);
873

    
874
             dbconn.increaseUsageCount(1);
875
             pstmt.execute();
876
             pstmt.close();
877
         }
878
         double endStoreReturnField = System.currentTimeMillis()/1000;
879
         logMetacat.warn("Time to store new return fields into xml_queryresult table (Part4 in return fields) "
880
                 + (endStoreReturnField -startStoreReturnField));
881
         // A string with element
882
         String xmlElement = "  <document>" + element + "</document>";
883
        
884
         //send single element to output
885
         if (out != null)
886
         {
887
             out.println(xmlElement);
888
         }
889
         resultset.append(xmlElement);
890
     }//while
891

    
892

    
893
     Enumeration keysE = queryresultDocList.keys();
894
     while (keysE.hasMoreElements())
895
     {
896
         key = (String) keysE.nextElement();
897
         element = (String)queryresultDocList.get(key);
898
         // A string with element
899
         String xmlElement = "  <document>" + element + "</document>";
900
         //send single element to output
901
         if (out != null)
902
         {
903
             out.println(xmlElement);
904
         }
905
         resultset.append(xmlElement);
906
     }//while
907

    
908
     return resultset;
909
 }
910

    
911
   /**
912
    * Get the docids already in xml_queryresult table and corresponding
913
    * queryresultstring as a hashtable
914
    */
915
   private Hashtable docidsInQueryresultTable(int returnfield_id,
916
                                              ResultDocumentSet partOfDoclist,
917
                                              DBConnection dbconn){
918

    
919
         Hashtable returnValue = new Hashtable();
920
         PreparedStatement pstmt = null;
921
         ResultSet rs = null;
922

    
923
         // get partOfDoclist as string for the query
924
         Iterator keylist = partOfDoclist.getDocids();
925
         StringBuffer doclist = new StringBuffer();
926
         while (keylist.hasNext())
927
         {
928
             doclist.append("'");
929
             doclist.append((String) keylist.next());
930
             doclist.append("',");
931
         }//while
932

    
933

    
934
         if (doclist.length() > 0)
935
         {
936
             doclist.deleteCharAt(doclist.length() - 1); //remove the last comma
937

    
938
             // the query to find out docids from xml_queryresult
939
             String query = "select docid, queryresult_string from "
940
                          + "xml_queryresult where returnfield_id = " +
941
                          returnfield_id +" and docid in ("+ doclist + ")";
942
             logMetacat.info("Query to get docids from xml_queryresult:"
943
                                      + query);
944

    
945
             try {
946
                 // prepare and execute the query
947
                 pstmt = dbconn.prepareStatement(query);
948
                 dbconn.increaseUsageCount(1);
949
                 pstmt.execute();
950
                 rs = pstmt.getResultSet();
951
                 boolean tableHasRows = rs.next();
952
                 while (tableHasRows) {
953
                     // store the returned results in the returnValue hashtable
954
                     String key = rs.getString(1);
955
                     String element = rs.getString(2);
956

    
957
                     if(element != null){
958
                         returnValue.put(key, element);
959
                     } else {
960
                         logMetacat.info("Null elment found ("
961
                         + "DBQuery.docidsInQueryresultTable)");
962
                     }
963
                     tableHasRows = rs.next();
964
                 }
965
                 rs.close();
966
                 pstmt.close();
967
             } catch (Exception e){
968
                 logMetacat.error("Error getting docids from "
969
                                          + "queryresult in "
970
                                          + "DBQuery.docidsInQueryresultTable: "
971
                                          + e.getMessage());
972
              }
973
         }
974
         return returnValue;
975
     }
976

    
977

    
978
   /**
979
    * Method to get id from xml_returnfield table
980
    * for a given query specification
981
    */
982
   private int returnfield_id;
983
   private int getXmlReturnfieldsTableId(QuerySpecification qspec,
984
                                           DBConnection dbconn){
985
       int id = -1;
986
       int count = 1;
987
       PreparedStatement pstmt = null;
988
       ResultSet rs = null;
989
       String returnfield = qspec.getSortedReturnFieldString();
990

    
991
       // query for finding the id from xml_returnfield
992
       String query = "SELECT returnfield_id, usage_count FROM xml_returnfield "
993
            + "WHERE returnfield_string LIKE ?";
994
       logMetacat.info("ReturnField Query:" + query);
995

    
996
       try {
997
           // prepare and run the query
998
           pstmt = dbconn.prepareStatement(query);
999
           pstmt.setString(1,returnfield);
1000
           dbconn.increaseUsageCount(1);
1001
           pstmt.execute();
1002
           rs = pstmt.getResultSet();
1003
           boolean tableHasRows = rs.next();
1004

    
1005
           // if record found then increase the usage count
1006
           // else insert a new record and get the id of the new record
1007
           if(tableHasRows){
1008
               // get the id
1009
               id = rs.getInt(1);
1010
               count = rs.getInt(2) + 1;
1011
               rs.close();
1012
               pstmt.close();
1013

    
1014
               // increase the usage count
1015
               query = "UPDATE xml_returnfield SET usage_count ='" + count
1016
                   + "' WHERE returnfield_id ='"+ id +"'";
1017
               logMetacat.info("ReturnField Table Update:"+ query);
1018

    
1019
               pstmt = dbconn.prepareStatement(query);
1020
               dbconn.increaseUsageCount(1);
1021
               pstmt.execute();
1022
               pstmt.close();
1023

    
1024
           } else {
1025
               rs.close();
1026
               pstmt.close();
1027

    
1028
               // insert a new record
1029
               query = "INSERT INTO xml_returnfield (returnfield_string, usage_count)"
1030
                   + "VALUES (?, '1')";
1031
               logMetacat.info("ReturnField Table Insert:"+ query);
1032
               pstmt = dbconn.prepareStatement(query);
1033
               pstmt.setString(1, returnfield);
1034
               dbconn.increaseUsageCount(1);
1035
               pstmt.execute();
1036
               pstmt.close();
1037

    
1038
               // get the id of the new record
1039
               query = "SELECT returnfield_id FROM xml_returnfield "
1040
                   + "WHERE returnfield_string LIKE ?";
1041
               logMetacat.info("ReturnField query after Insert:" + query);
1042
               pstmt = dbconn.prepareStatement(query);
1043
               pstmt.setString(1, returnfield);
1044

    
1045
               dbconn.increaseUsageCount(1);
1046
               pstmt.execute();
1047
               rs = pstmt.getResultSet();
1048
               if(rs.next()){
1049
                   id = rs.getInt(1);
1050
               } else {
1051
                   id = -1;
1052
               }
1053
               rs.close();
1054
               pstmt.close();
1055
           }
1056

    
1057
       } catch (Exception e){
1058
           logMetacat.error("Error getting id from xml_returnfield in "
1059
                                     + "DBQuery.getXmlReturnfieldsTableId: "
1060
                                     + e.getMessage());
1061
           id = -1;
1062
       }
1063

    
1064
       returnfield_id = id;
1065
       return count;
1066
   }
1067

    
1068

    
1069
    /*
1070
     * A method to add return field to return doclist hash table
1071
     */
1072
    private ResultDocumentSet addReturnfield(ResultDocumentSet docListResult,
1073
                                      QuerySpecification qspec,
1074
                                      String user, String[]groups,
1075
                                      DBConnection dbconn, boolean useXMLIndex )
1076
                                      throws Exception
1077
    {
1078
      PreparedStatement pstmt = null;
1079
      ResultSet rs = null;
1080
      String docid = null;
1081
      String fieldname = null;
1082
      String fielddata = null;
1083
      String relation = null;
1084

    
1085
      if (qspec.containsExtendedSQL())
1086
      {
1087
        qspec.setUserName(user);
1088
        qspec.setGroup(groups);
1089
        Vector extendedFields = new Vector(qspec.getReturnFieldList());
1090
        Vector results = new Vector();
1091
        Iterator keylist = docListResult.getDocids();
1092
        StringBuffer doclist = new StringBuffer();
1093
        Vector parentidList = new Vector();
1094
        Hashtable returnFieldValue = new Hashtable();
1095
        while (keylist.hasNext())
1096
        {
1097
          doclist.append("'");
1098
          doclist.append((String) keylist.next());
1099
          doclist.append("',");
1100
        }
1101
        if (doclist.length() > 0)
1102
        {
1103
          Hashtable controlPairs = new Hashtable();
1104
          double extendedQueryStart = System.currentTimeMillis() / 1000;
1105
          doclist.deleteCharAt(doclist.length() - 1); //remove the last comma
1106
          boolean tableHasRows = false;
1107
          // check if user has permission to see the return field data
1108
          /*String accessControlSQL =
1109
                 qspec.printAccessControlSQLForReturnField(doclist.toString());
1110
          pstmt = dbconn.prepareStatement(accessControlSQL);
1111
          //increase dbconnection usage count
1112
          dbconn.increaseUsageCount(1);
1113
          pstmt.execute();
1114
          rs = pstmt.getResultSet();
1115
          tableHasRows = rs.next();
1116
          while (tableHasRows)
1117
          {
1118
            long startNodeId = rs.getLong(1);
1119
            long endNodeId = rs.getLong(2);
1120
            controlPairs.put(new Long(startNodeId), new Long(endNodeId));
1121
            tableHasRows = rs.next();
1122
          }*/
1123

    
1124
          /* double extendedAccessQueryEnd = System.currentTimeMillis() / 1000;
1125
           logMetacat.info( "Time for execute access extended query: "
1126
                          + (extendedAccessQueryEnd - extendedQueryStart));*/
1127

    
1128
           String extendedQuery =
1129
               qspec.printExtendedSQL(doclist.toString(), useXMLIndex);
1130
           logMetacat.info("Extended query: " + extendedQuery);
1131

    
1132
           if(extendedQuery != null){
1133
               pstmt = dbconn.prepareStatement(extendedQuery);
1134
               //increase dbconnection usage count
1135
               dbconn.increaseUsageCount(1);
1136
               pstmt.execute();
1137
               rs = pstmt.getResultSet();
1138
               double extendedQueryEnd = System.currentTimeMillis() / 1000;
1139
               logMetacat.info(
1140
                   "Time to execute extended query: "
1141
                   + (extendedQueryEnd - extendedQueryStart));
1142
               tableHasRows = rs.next();
1143
               while (tableHasRows) {
1144
                   ReturnFieldValue returnValue = new ReturnFieldValue();
1145
                   docid = rs.getString(1).trim();
1146
                   fieldname = rs.getString(2);
1147
                   fielddata = rs.getString(3);
1148
                   fielddata = MetaCatUtil.normalize(fielddata);
1149
                   String parentId = rs.getString(4);
1150
                   StringBuffer value = new StringBuffer();
1151

    
1152
                   // if xml_index is used, there would be just one record per nodeid
1153
                   // as xml_index just keeps one entry for each path
1154
                   if (useXMLIndex || !containsKey(parentidList, parentId)) {
1155
                       // don't need to merger nodedata
1156
                       value.append("<param name=\"");
1157
                       value.append(fieldname);
1158
                       value.append("\">");
1159
                       value.append(fielddata);
1160
                       value.append("</param>");
1161
                       //set returnvalue
1162
                       returnValue.setDocid(docid);
1163
                       returnValue.setFieldValue(fielddata);
1164
                       returnValue.setXMLFieldValue(value.toString());
1165
                       // Store it in hastable
1166
                       putInArray(parentidList, parentId, returnValue);
1167
                   }
1168
                   else {
1169
                       // need to merge nodedata if they have same parent id and
1170
                       // node type is text
1171
                       fielddata = (String) ( (ReturnFieldValue)
1172
                                             getArrayValue(
1173
                           parentidList, parentId)).getFieldValue()
1174
                           + fielddata;
1175
                       value.append("<param name=\"");
1176
                       value.append(fieldname);
1177
                       value.append("\">");
1178
                       value.append(fielddata);
1179
                       value.append("</param>");
1180
                       returnValue.setDocid(docid);
1181
                       returnValue.setFieldValue(fielddata);
1182
                       returnValue.setXMLFieldValue(value.toString());
1183
                       // remove the old return value from paretnidList
1184
                       parentidList.remove(parentId);
1185
                       // store the new return value in parentidlit
1186
                       putInArray(parentidList, parentId, returnValue);
1187
                   }
1188
                   tableHasRows = rs.next();
1189
               } //while
1190
               rs.close();
1191
               pstmt.close();
1192

    
1193
               // put the merger node data info into doclistReult
1194
               Enumeration xmlFieldValue = (getElements(parentidList)).
1195
                   elements();
1196
               while (xmlFieldValue.hasMoreElements()) {
1197
                   ReturnFieldValue object =
1198
                       (ReturnFieldValue) xmlFieldValue.nextElement();
1199
                   docid = object.getDocid();
1200
                   if (docListResult.containsDocid(docid)) {
1201
                       String removedelement = (String) docListResult.
1202
                           remove(docid);
1203
                       docListResult.
1204
                           addResultDocument(new ResultDocument(docid,
1205
                               removedelement + object.getXMLFieldValue()));
1206
                   }
1207
                   else {
1208
                       docListResult.addResultDocument(
1209
                         new ResultDocument(docid, object.getXMLFieldValue()));
1210
                   }
1211
               } //while
1212
               double docListResultEnd = System.currentTimeMillis() / 1000;
1213
               logMetacat.warn(
1214
                   "Time for prepare ResultDocumentSet after"
1215
                   + " executing extended query: "
1216
                   + (docListResultEnd - extendedQueryEnd));
1217
           }
1218

    
1219
           // get attribures return
1220
           docListResult = getAttributeValueForReturn(qspec,
1221
                           docListResult, doclist.toString(), useXMLIndex);
1222
       }//if doclist lenght is great than zero
1223

    
1224
     }//if has extended query
1225

    
1226
      return docListResult;
1227
    }//addReturnfield
1228

    
1229
    /*
1230
    * A method to add relationship to return doclist hash table
1231
    */
1232
   private ResultDocumentSet addRelationship(ResultDocumentSet docListResult,
1233
                                     QuerySpecification qspec,
1234
                                     DBConnection dbconn, boolean useXMLIndex )
1235
                                     throws Exception
1236
  {
1237
    PreparedStatement pstmt = null;
1238
    ResultSet rs = null;
1239
    StringBuffer document = null;
1240
    double startRelation = System.currentTimeMillis() / 1000;
1241
    Iterator docidkeys = docListResult.getDocids();
1242
    //System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!1beofre the while loop the docid keys is "+docidkeys);
1243
    while (docidkeys.hasNext())
1244
    {
1245
      //String connstring =
1246
      // "metacat://"+util.getOption("server")+"?docid=";
1247
      String connstring = "%docid=";
1248
      //System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!1beofre the null line the docid keys is "+docidkeys);
1249
      String docidkey = (String) docidkeys.next();
1250
      //System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!1after the null line the docid string is "+docidkey);
1251
      pstmt = dbconn.prepareStatement(QuerySpecification
1252
                      .printRelationSQL(docidkey));
1253
      pstmt.execute();
1254
      rs = pstmt.getResultSet();
1255
      boolean tableHasRows = rs.next();
1256
      while (tableHasRows)
1257
      {
1258
        String sub = rs.getString(1);
1259
        String rel = rs.getString(2);
1260
        String obj = rs.getString(3);
1261
        String subDT = rs.getString(4);
1262
        String objDT = rs.getString(5);
1263

    
1264
        document = new StringBuffer();
1265
        document.append("<triple>");
1266
        document.append("<subject>").append(MetaCatUtil.normalize(sub));
1267
        document.append("</subject>");
1268
        if (subDT != null)
1269
        {
1270
          document.append("<subjectdoctype>").append(subDT);
1271
          document.append("</subjectdoctype>");
1272
        }
1273
        document.append("<relationship>").append(MetaCatUtil.normalize(rel));
1274
        document.append("</relationship>");
1275
        document.append("<object>").append(MetaCatUtil.normalize(obj));
1276
        document.append("</object>");
1277
        if (objDT != null)
1278
        {
1279
          document.append("<objectdoctype>").append(objDT);
1280
          document.append("</objectdoctype>");
1281
        }
1282
        document.append("</triple>");
1283

    
1284
        String removedelement = (String) docListResult.remove(docidkey);
1285
        docListResult.put(docidkey, removedelement+ document.toString());
1286
        tableHasRows = rs.next();
1287
      }//while
1288
      rs.close();
1289
      pstmt.close();
1290
    }//while
1291
    double endRelation = System.currentTimeMillis() / 1000;
1292
    logMetacat.info("Time to add relationship to return fields (part 3 in return fields): "
1293
                             + (endRelation - startRelation));
1294

    
1295
    return docListResult;
1296
  }//addRelation
1297

    
1298
  /**
1299
   * removes the <?xml version="1.0"?> tag from the beginning.  This takes a
1300
   * string as a param instead of a hashtable.
1301
   *
1302
   * @param xmlquery a string representing a query.
1303
   */
1304
   private  String transformQuery(String xmlquery)
1305
   {
1306
     xmlquery = xmlquery.trim();
1307
     int index = xmlquery.indexOf("?>");
1308
     if (index != -1)
1309
     {
1310
       return xmlquery.substring(index + 2, xmlquery.length());
1311
     }
1312
     else
1313
     {
1314
       return xmlquery;
1315
     }
1316
   }
1317

    
1318

    
1319
    /*
1320
     * A method to search if Vector contains a particular key string
1321
     */
1322
    private boolean containsKey(Vector parentidList, String parentId)
1323
    {
1324

    
1325
        Vector tempVector = null;
1326

    
1327
        for (int count = 0; count < parentidList.size(); count++) {
1328
            tempVector = (Vector) parentidList.get(count);
1329
            if (parentId.compareTo((String) tempVector.get(0)) == 0) { return true; }
1330
        }
1331
        return false;
1332
    }
1333

    
1334
    /*
1335
     * A method to put key and value in Vector
1336
     */
1337
    private void putInArray(Vector parentidList, String key,
1338
            ReturnFieldValue value)
1339
    {
1340

    
1341
        Vector tempVector = null;
1342

    
1343
        for (int count = 0; count < parentidList.size(); count++) {
1344
            tempVector = (Vector) parentidList.get(count);
1345

    
1346
            if (key.compareTo((String) tempVector.get(0)) == 0) {
1347
                tempVector.remove(1);
1348
                tempVector.add(1, value);
1349
                return;
1350
            }
1351
        }
1352

    
1353
        tempVector = new Vector();
1354
        tempVector.add(0, key);
1355
        tempVector.add(1, value);
1356
        parentidList.add(tempVector);
1357
        return;
1358
    }
1359

    
1360
    /*
1361
     * A method to get value in Vector given a key
1362
     */
1363
    private ReturnFieldValue getArrayValue(Vector parentidList, String key)
1364
    {
1365

    
1366
        Vector tempVector = null;
1367

    
1368
        for (int count = 0; count < parentidList.size(); count++) {
1369
            tempVector = (Vector) parentidList.get(count);
1370

    
1371
            if (key.compareTo((String) tempVector.get(0)) == 0) { return (ReturnFieldValue) tempVector
1372
                    .get(1); }
1373
        }
1374
        return null;
1375
    }
1376

    
1377
    /*
1378
     * A method to get enumeration of all values in Vector
1379
     */
1380
    private Vector getElements(Vector parentidList)
1381
    {
1382
        Vector enumVector = new Vector();
1383
        Vector tempVector = null;
1384

    
1385
        for (int count = 0; count < parentidList.size(); count++) {
1386
            tempVector = (Vector) parentidList.get(count);
1387

    
1388
            enumVector.add(tempVector.get(1));
1389
        }
1390
        return enumVector;
1391
    }
1392

    
1393
    /*
1394
     * A method to return search result after running a query which return
1395
     * field have attribue
1396
     */
1397
    private ResultDocumentSet getAttributeValueForReturn(QuerySpecification squery,
1398
            ResultDocumentSet docInformationList, String docList, boolean useXMLIndex)
1399
    {
1400
        StringBuffer XML = null;
1401
        String sql = null;
1402
        DBConnection dbconn = null;
1403
        PreparedStatement pstmt = null;
1404
        ResultSet rs = null;
1405
        int serialNumber = -1;
1406
        boolean tableHasRows = false;
1407

    
1408
        //check the parameter
1409
        if (squery == null || docList == null || docList.length() < 0) { return docInformationList; }
1410

    
1411
        // if has attribute as return field
1412
        if (squery.containsAttributeReturnField()) {
1413
            sql = squery.printAttributeQuery(docList, useXMLIndex);
1414
            try {
1415
                dbconn = DBConnectionPool
1416
                        .getDBConnection("DBQuery.getAttributeValue");
1417
                serialNumber = dbconn.getCheckOutSerialNumber();
1418
                pstmt = dbconn.prepareStatement(sql);
1419
                pstmt.execute();
1420
                rs = pstmt.getResultSet();
1421
                tableHasRows = rs.next();
1422
                while (tableHasRows) {
1423
                    String docid = rs.getString(1).trim();
1424
                    String fieldname = rs.getString(2);
1425
                    String fielddata = rs.getString(3);
1426
                    String attirbuteName = rs.getString(4);
1427
                    XML = new StringBuffer();
1428

    
1429
                    XML.append("<param name=\"");
1430
                    XML.append(fieldname);
1431
                    XML.append("/");
1432
                    XML.append(QuerySpecification.ATTRIBUTESYMBOL);
1433
                    XML.append(attirbuteName);
1434
                    XML.append("\">");
1435
                    XML.append(fielddata);
1436
                    XML.append("</param>");
1437
                    tableHasRows = rs.next();
1438

    
1439
                    if (docInformationList.containsDocid(docid)) {
1440
                        String removedelement = (String) docInformationList
1441
                                .remove(docid);
1442
                        docInformationList.put(docid, removedelement
1443
                                + XML.toString());
1444
                    } else {
1445
                        docInformationList.put(docid, XML.toString());
1446
                    }
1447
                }//while
1448
                rs.close();
1449
                pstmt.close();
1450
            } catch (Exception se) {
1451
                logMetacat.error(
1452
                        "Error in DBQuery.getAttributeValue1: "
1453
                                + se.getMessage());
1454
            } finally {
1455
                try {
1456
                    pstmt.close();
1457
                }//try
1458
                catch (SQLException sqlE) {
1459
                    logMetacat.error(
1460
                            "Error in DBQuery.getAttributeValue2: "
1461
                                    + sqlE.getMessage());
1462
                }//catch
1463
                finally {
1464
                    DBConnectionPool.returnDBConnection(dbconn, serialNumber);
1465
                }//finally
1466
            }//finally
1467
        }//if
1468
        return docInformationList;
1469

    
1470
    }
1471

    
1472
    /*
1473
     * A method to create a query to get owner's docid list
1474
     */
1475
    private String getOwnerQuery(String owner)
1476
    {
1477
        if (owner != null) {
1478
            owner = owner.toLowerCase();
1479
        }
1480
        StringBuffer self = new StringBuffer();
1481

    
1482
        self.append("SELECT docid,docname,doctype,");
1483
        self.append("date_created, date_updated, rev ");
1484
        self.append("FROM xml_documents WHERE docid IN (");
1485
        self.append("(");
1486
        self.append("SELECT DISTINCT docid FROM xml_nodes WHERE \n");
1487
        self.append("nodedata LIKE '%%%' ");
1488
        self.append(") \n");
1489
        self.append(") ");
1490
        self.append(" AND (");
1491
        self.append(" lower(user_owner) = '" + owner + "'");
1492
        self.append(") ");
1493
        return self.toString();
1494
    }
1495

    
1496
    /**
1497
     * format a structured query as an XML document that conforms to the
1498
     * pathquery.dtd and is appropriate for submission to the DBQuery
1499
     * structured query engine
1500
     *
1501
     * @param params The list of parameters that should be included in the
1502
     *            query
1503
     */
1504
    public static String createSQuery(Hashtable params)
1505
    {
1506
        StringBuffer query = new StringBuffer();
1507
        Enumeration elements;
1508
        Enumeration keys;
1509
        String filterDoctype = null;
1510
        String casesensitive = null;
1511
        String searchmode = null;
1512
        Object nextkey;
1513
        Object nextelement;
1514
        //add the xml headers
1515
        query.append("<?xml version=\"1.0\"?>\n");
1516
        query.append("<pathquery version=\"1.2\">\n");
1517

    
1518

    
1519

    
1520
        if (params.containsKey("meta_file_id")) {
1521
            query.append("<meta_file_id>");
1522
            query.append(((String[]) params.get("meta_file_id"))[0]);
1523
            query.append("</meta_file_id>");
1524
        }
1525

    
1526
        if (params.containsKey("returndoctype")) {
1527
            String[] returnDoctypes = ((String[]) params.get("returndoctype"));
1528
            for (int i = 0; i < returnDoctypes.length; i++) {
1529
                String doctype = (String) returnDoctypes[i];
1530

    
1531
                if (!doctype.equals("any") && !doctype.equals("ANY")
1532
                        && !doctype.equals("")) {
1533
                    query.append("<returndoctype>").append(doctype);
1534
                    query.append("</returndoctype>");
1535
                }
1536
            }
1537
        }
1538

    
1539
        if (params.containsKey("filterdoctype")) {
1540
            String[] filterDoctypes = ((String[]) params.get("filterdoctype"));
1541
            for (int i = 0; i < filterDoctypes.length; i++) {
1542
                query.append("<filterdoctype>").append(filterDoctypes[i]);
1543
                query.append("</filterdoctype>");
1544
            }
1545
        }
1546

    
1547
        if (params.containsKey("returnfield")) {
1548
            String[] returnfield = ((String[]) params.get("returnfield"));
1549
            for (int i = 0; i < returnfield.length; i++) {
1550
                query.append("<returnfield>").append(returnfield[i]);
1551
                query.append("</returnfield>");
1552
            }
1553
        }
1554

    
1555
        if (params.containsKey("owner")) {
1556
            String[] owner = ((String[]) params.get("owner"));
1557
            for (int i = 0; i < owner.length; i++) {
1558
                query.append("<owner>").append(owner[i]);
1559
                query.append("</owner>");
1560
            }
1561
        }
1562

    
1563
        if (params.containsKey("site")) {
1564
            String[] site = ((String[]) params.get("site"));
1565
            for (int i = 0; i < site.length; i++) {
1566
                query.append("<site>").append(site[i]);
1567
                query.append("</site>");
1568
            }
1569
        }
1570

    
1571
        //allows the dynamic switching of boolean operators
1572
        if (params.containsKey("operator")) {
1573
            query.append("<querygroup operator=\""
1574
                    + ((String[]) params.get("operator"))[0] + "\">");
1575
        } else { //the default operator is UNION
1576
            query.append("<querygroup operator=\"UNION\">");
1577
        }
1578

    
1579
        if (params.containsKey("casesensitive")) {
1580
            casesensitive = ((String[]) params.get("casesensitive"))[0];
1581
        } else {
1582
            casesensitive = "false";
1583
        }
1584

    
1585
        if (params.containsKey("searchmode")) {
1586
            searchmode = ((String[]) params.get("searchmode"))[0];
1587
        } else {
1588
            searchmode = "contains";
1589
        }
1590

    
1591
        //anyfield is a special case because it does a
1592
        //free text search. It does not have a <pathexpr>
1593
        //tag. This allows for a free text search within the structured
1594
        //query. This is useful if the INTERSECT operator is used.
1595
        if (params.containsKey("anyfield")) {
1596
            String[] anyfield = ((String[]) params.get("anyfield"));
1597
            //allow for more than one value for anyfield
1598
            for (int i = 0; i < anyfield.length; i++) {
1599
                if (!anyfield[i].equals("")) {
1600
                    query.append("<queryterm casesensitive=\"" + casesensitive
1601
                            + "\" " + "searchmode=\"" + searchmode
1602
                            + "\"><value>" + anyfield[i]
1603
                            + "</value></queryterm>");
1604
                }
1605
            }
1606
        }
1607

    
1608
        //this while loop finds the rest of the parameters
1609
        //and attempts to query for the field specified
1610
        //by the parameter.
1611
        elements = params.elements();
1612
        keys = params.keys();
1613
        while (keys.hasMoreElements() && elements.hasMoreElements()) {
1614
            nextkey = keys.nextElement();
1615
            nextelement = elements.nextElement();
1616

    
1617
            //make sure we aren't querying for any of these
1618
            //parameters since the are already in the query
1619
            //in one form or another.
1620
            Vector ignoredParams = new Vector();
1621
            ignoredParams.add("returndoctype");
1622
            ignoredParams.add("filterdoctype");
1623
            ignoredParams.add("action");
1624
            ignoredParams.add("qformat");
1625
            ignoredParams.add("anyfield");
1626
            ignoredParams.add("returnfield");
1627
            ignoredParams.add("owner");
1628
            ignoredParams.add("site");
1629
            ignoredParams.add("operator");
1630
            ignoredParams.add("sessionid");
1631
            ignoredParams.add("pagesize");
1632
            ignoredParams.add("pagestart");
1633

    
1634
            // Also ignore parameters listed in the properties file
1635
            // so that they can be passed through to stylesheets
1636
            String paramsToIgnore = MetaCatUtil
1637
                    .getOption("query.ignored.params");
1638
            StringTokenizer st = new StringTokenizer(paramsToIgnore, ",");
1639
            while (st.hasMoreTokens()) {
1640
                ignoredParams.add(st.nextToken());
1641
            }
1642
            if (!ignoredParams.contains(nextkey.toString())) {
1643
                //allow for more than value per field name
1644
                for (int i = 0; i < ((String[]) nextelement).length; i++) {
1645
                    if (!((String[]) nextelement)[i].equals("")) {
1646
                        query.append("<queryterm casesensitive=\""
1647
                                + casesensitive + "\" " + "searchmode=\""
1648
                                + searchmode + "\">" + "<value>" +
1649
                                //add the query value
1650
                                ((String[]) nextelement)[i]
1651
                                + "</value><pathexpr>" +
1652
                                //add the path to query by
1653
                                nextkey.toString() + "</pathexpr></queryterm>");
1654
                    }
1655
                }
1656
            }
1657
        }
1658
        query.append("</querygroup></pathquery>");
1659
        //append on the end of the xml and return the result as a string
1660
        return query.toString();
1661
    }
1662

    
1663
    /**
1664
     * format a simple free-text value query as an XML document that conforms
1665
     * to the pathquery.dtd and is appropriate for submission to the DBQuery
1666
     * structured query engine
1667
     *
1668
     * @param value the text string to search for in the xml catalog
1669
     * @param doctype the type of documents to include in the result set -- use
1670
     *            "any" or "ANY" for unfiltered result sets
1671
     */
1672
    public static String createQuery(String value, String doctype)
1673
    {
1674
        StringBuffer xmlquery = new StringBuffer();
1675
        xmlquery.append("<?xml version=\"1.0\"?>\n");
1676
        xmlquery.append("<pathquery version=\"1.0\">");
1677

    
1678
        if (!doctype.equals("any") && !doctype.equals("ANY")) {
1679
            xmlquery.append("<returndoctype>");
1680
            xmlquery.append(doctype).append("</returndoctype>");
1681
        }
1682

    
1683
        xmlquery.append("<querygroup operator=\"UNION\">");
1684
        //chad added - 8/14
1685
        //the if statement allows a query to gracefully handle a null
1686
        //query. Without this if a nullpointerException is thrown.
1687
        if (!value.equals("")) {
1688
            xmlquery.append("<queryterm casesensitive=\"false\" ");
1689
            xmlquery.append("searchmode=\"contains\">");
1690
            xmlquery.append("<value>").append(value).append("</value>");
1691
            xmlquery.append("</queryterm>");
1692
        }
1693
        xmlquery.append("</querygroup>");
1694
        xmlquery.append("</pathquery>");
1695

    
1696
        return (xmlquery.toString());
1697
    }
1698

    
1699
    /**
1700
     * format a simple free-text value query as an XML document that conforms
1701
     * to the pathquery.dtd and is appropriate for submission to the DBQuery
1702
     * structured query engine
1703
     *
1704
     * @param value the text string to search for in the xml catalog
1705
     */
1706
    public static String createQuery(String value)
1707
    {
1708
        return createQuery(value, "any");
1709
    }
1710

    
1711
    /**
1712
     * Check for "READ" permission on @docid for @user and/or @group from DB
1713
     * connection
1714
     */
1715
    private boolean hasPermission(String user, String[] groups, String docid)
1716
            throws SQLException, Exception
1717
    {
1718
        // Check for READ permission on @docid for @user and/or @groups
1719
        PermissionController controller = new PermissionController(docid);
1720
        return controller.hasPermission(user, groups,
1721
                AccessControlInterface.READSTRING);
1722
    }
1723

    
1724
    /**
1725
     * Get all docIds list for a data packadge
1726
     *
1727
     * @param dataPackageDocid, the string in docId field of xml_relation table
1728
     */
1729
    private Vector getCurrentDocidListForDataPackage(String dataPackageDocid)
1730
    {
1731
        DBConnection dbConn = null;
1732
        int serialNumber = -1;
1733
        Vector docIdList = new Vector();//return value
1734
        PreparedStatement pStmt = null;
1735
        ResultSet rs = null;
1736
        String docIdInSubjectField = null;
1737
        String docIdInObjectField = null;
1738

    
1739
        // Check the parameter
1740
        if (dataPackageDocid == null || dataPackageDocid.equals("")) { return docIdList; }//if
1741

    
1742
        //the query stirng
1743
        String query = "SELECT subject, object from xml_relation where docId = ?";
1744
        try {
1745
            dbConn = DBConnectionPool
1746
                    .getDBConnection("DBQuery.getCurrentDocidListForDataPackage");
1747
            serialNumber = dbConn.getCheckOutSerialNumber();
1748
            pStmt = dbConn.prepareStatement(query);
1749
            //bind the value to query
1750
            pStmt.setString(1, dataPackageDocid);
1751

    
1752
            //excute the query
1753
            pStmt.execute();
1754
            //get the result set
1755
            rs = pStmt.getResultSet();
1756
            //process the result
1757
            while (rs.next()) {
1758
                //In order to get the whole docIds in a data packadge,
1759
                //we need to put the docIds of subject and object field in
1760
                // xml_relation
1761
                //into the return vector
1762
                docIdInSubjectField = rs.getString(1);//the result docId in
1763
                                                      // subject field
1764
                docIdInObjectField = rs.getString(2);//the result docId in
1765
                                                     // object field
1766

    
1767
                //don't put the duplicate docId into the vector
1768
                if (!docIdList.contains(docIdInSubjectField)) {
1769
                    docIdList.add(docIdInSubjectField);
1770
                }
1771

    
1772
                //don't put the duplicate docId into the vector
1773
                if (!docIdList.contains(docIdInObjectField)) {
1774
                    docIdList.add(docIdInObjectField);
1775
                }
1776
            }//while
1777
            //close the pStmt
1778
            pStmt.close();
1779
        }//try
1780
        catch (SQLException e) {
1781
            logMetacat.error("Error in getDocidListForDataPackage: "
1782
                    + e.getMessage());
1783
        }//catch
1784
        finally {
1785
            try {
1786
                pStmt.close();
1787
            }//try
1788
            catch (SQLException ee) {
1789
                logMetacat.error(
1790
                        "Error in getDocidListForDataPackage: "
1791
                                + ee.getMessage());
1792
            }//catch
1793
            finally {
1794
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1795
            }//fianlly
1796
        }//finally
1797
        return docIdList;
1798
    }//getCurrentDocidListForDataPackadge()
1799

    
1800
    /**
1801
     * Get all docIds list for a data packadge
1802
     *
1803
     * @param dataPackageDocid, the string in docId field of xml_relation table
1804
     */
1805
    private Vector getOldVersionDocidListForDataPackage(String dataPackageDocidWithRev)
1806
    {
1807

    
1808
        Vector docIdList = new Vector();//return value
1809
        Vector tripleList = null;
1810
        String xml = null;
1811

    
1812
        // Check the parameter
1813
        if (dataPackageDocidWithRev == null || dataPackageDocidWithRev.equals("")) { return docIdList; }//if
1814

    
1815
        try {
1816
            //initial a documentImpl object
1817
            DocumentImpl packageDocument = new DocumentImpl(dataPackageDocidWithRev);
1818
            //transfer to documentImpl object to string
1819
            xml = packageDocument.toString();
1820

    
1821
            //create a tripcollection object
1822
            TripleCollection tripleForPackage = new TripleCollection(
1823
                    new StringReader(xml));
1824
            //get the vetor of triples
1825
            tripleList = tripleForPackage.getCollection();
1826

    
1827
            for (int i = 0; i < tripleList.size(); i++) {
1828
                //put subject docid into docIdlist without duplicate
1829
                if (!docIdList.contains(((Triple) tripleList.elementAt(i))
1830
                        .getSubject())) {
1831
                    //put subject docid into docIdlist
1832
                    docIdList.add(((Triple) tripleList.get(i)).getSubject());
1833
                }
1834
                //put object docid into docIdlist without duplicate
1835
                if (!docIdList.contains(((Triple) tripleList.elementAt(i))
1836
                        .getObject())) {
1837
                    docIdList.add(((Triple) (tripleList.get(i))).getObject());
1838
                }
1839
            }//for
1840
        }//try
1841
        catch (Exception e) {
1842
            logMetacat.error("Error in getOldVersionAllDocumentImpl: "
1843
                    + e.getMessage());
1844
        }//catch
1845

    
1846
        // return result
1847
        return docIdList;
1848
    }//getDocidListForPackageInXMLRevisions()
1849

    
1850
    /**
1851
     * Check if the docId is a data packadge id. If the id is a data packadage
1852
     * id, it should be store in the docId fields in xml_relation table. So we
1853
     * can use a query to get the entries which the docId equals the given
1854
     * value. If the result is null. The docId is not a packadge id. Otherwise,
1855
     * it is.
1856
     *
1857
     * @param docId, the id need to be checked
1858
     */
1859
    private boolean isDataPackageId(String docId)
1860
    {
1861
        boolean result = false;
1862
        PreparedStatement pStmt = null;
1863
        ResultSet rs = null;
1864
        String query = "SELECT docId from xml_relation where docId = ?";
1865
        DBConnection dbConn = null;
1866
        int serialNumber = -1;
1867
        try {
1868
            dbConn = DBConnectionPool
1869
                    .getDBConnection("DBQuery.isDataPackageId");
1870
            serialNumber = dbConn.getCheckOutSerialNumber();
1871
            pStmt = dbConn.prepareStatement(query);
1872
            //bind the value to query
1873
            pStmt.setString(1, docId);
1874
            //execute the query
1875
            pStmt.execute();
1876
            rs = pStmt.getResultSet();
1877
            //process the result
1878
            if (rs.next()) //There are some records for the id in docId fields
1879
            {
1880
                result = true;//It is a data packadge id
1881
            }
1882
            pStmt.close();
1883
        }//try
1884
        catch (SQLException e) {
1885
            logMetacat.error("Error in isDataPackageId: "
1886
                    + e.getMessage());
1887
        } finally {
1888
            try {
1889
                pStmt.close();
1890
            }//try
1891
            catch (SQLException ee) {
1892
                logMetacat.error("Error in isDataPackageId: "
1893
                        + ee.getMessage());
1894
            }//catch
1895
            finally {
1896
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1897
            }//finally
1898
        }//finally
1899
        return result;
1900
    }//isDataPackageId()
1901

    
1902
    /**
1903
     * Check if the user has the permission to export data package
1904
     *
1905
     * @param conn, the connection
1906
     * @param docId, the id need to be checked
1907
     * @param user, the name of user
1908
     * @param groups, the user's group
1909
     */
1910
    private boolean hasPermissionToExportPackage(String docId, String user,
1911
            String[] groups) throws Exception
1912
    {
1913
        //DocumentImpl doc=new DocumentImpl(conn,docId);
1914
        return DocumentImpl.hasReadPermission(user, groups, docId);
1915
    }
1916

    
1917
    /**
1918
     * Get the current Rev for a docid in xml_documents table
1919
     *
1920
     * @param docId, the id need to get version numb If the return value is -5,
1921
     *            means no value in rev field for this docid
1922
     */
1923
    private int getCurrentRevFromXMLDoumentsTable(String docId)
1924
            throws SQLException
1925
    {
1926
        int rev = -5;
1927
        PreparedStatement pStmt = null;
1928
        ResultSet rs = null;
1929
        String query = "SELECT rev from xml_documents where docId = ?";
1930
        DBConnection dbConn = null;
1931
        int serialNumber = -1;
1932
        try {
1933
            dbConn = DBConnectionPool
1934
                    .getDBConnection("DBQuery.getCurrentRevFromXMLDocumentsTable");
1935
            serialNumber = dbConn.getCheckOutSerialNumber();
1936
            pStmt = dbConn.prepareStatement(query);
1937
            //bind the value to query
1938
            pStmt.setString(1, docId);
1939
            //execute the query
1940
            pStmt.execute();
1941
            rs = pStmt.getResultSet();
1942
            //process the result
1943
            if (rs.next()) //There are some records for rev
1944
            {
1945
                rev = rs.getInt(1);
1946
                ;//It is the version for given docid
1947
            } else {
1948
                rev = -5;
1949
            }
1950

    
1951
        }//try
1952
        catch (SQLException e) {
1953
            logMetacat.error(
1954
                    "Error in getCurrentRevFromXMLDoumentsTable: "
1955
                            + e.getMessage());
1956
            throw e;
1957
        }//catch
1958
        finally {
1959
            try {
1960
                pStmt.close();
1961
            }//try
1962
            catch (SQLException ee) {
1963
                logMetacat.error(
1964
                        "Error in getCurrentRevFromXMLDoumentsTable: "
1965
                                + ee.getMessage());
1966
            }//catch
1967
            finally {
1968
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1969
            }//finally
1970
        }//finally
1971
        return rev;
1972
    }//getCurrentRevFromXMLDoumentsTable
1973

    
1974
    /**
1975
     * put a doc into a zip output stream
1976
     *
1977
     * @param docImpl, docmentImpl object which will be sent to zip output
1978
     *            stream
1979
     * @param zipOut, zip output stream which the docImpl will be put
1980
     * @param packageZipEntry, the zip entry name for whole package
1981
     */
1982
    private void addDocToZipOutputStream(DocumentImpl docImpl,
1983
            ZipOutputStream zipOut, String packageZipEntry)
1984
            throws ClassNotFoundException, IOException, SQLException,
1985
            McdbException, Exception
1986
    {
1987
        byte[] byteString = null;
1988
        ZipEntry zEntry = null;
1989

    
1990
        byteString = docImpl.toString().getBytes();
1991
        //use docId as the zip entry's name
1992
        zEntry = new ZipEntry(packageZipEntry + "/metadata/"
1993
                + docImpl.getDocID());
1994
        zEntry.setSize(byteString.length);
1995
        zipOut.putNextEntry(zEntry);
1996
        zipOut.write(byteString, 0, byteString.length);
1997
        zipOut.closeEntry();
1998

    
1999
    }//addDocToZipOutputStream()
2000

    
2001
    /**
2002
     * Transfer a docid vetor to a documentImpl vector. The documentImpl vetor
2003
     * only inlcudes current version. If a DocumentImple object couldn't find
2004
     * for a docid, then the String of this docid was added to vetor rather
2005
     * than DocumentImple object.
2006
     *
2007
     * @param docIdList, a vetor hold a docid list for a data package. In
2008
     *            docid, there is not version number in it.
2009
     */
2010

    
2011
    private Vector getCurrentAllDocumentImpl(Vector docIdList)
2012
            throws McdbException, Exception
2013
    {
2014
        //Connection dbConn=null;
2015
        Vector documentImplList = new Vector();
2016
        int rev = 0;
2017

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

    
2021
        //for every docid in vector
2022
        for (int i = 0; i < docIdList.size(); i++) {
2023
            try {
2024
                //get newest version for this docId
2025
                rev = getCurrentRevFromXMLDoumentsTable((String) docIdList
2026
                        .elementAt(i));
2027

    
2028
                // There is no record for this docId in xml_documents table
2029
                if (rev == -5) {
2030
                    // Rather than put DocumentImple object, put a String
2031
                    // Object(docid)
2032
                    // into the documentImplList
2033
                    documentImplList.add((String) docIdList.elementAt(i));
2034
                    // Skip other code
2035
                    continue;
2036
                }
2037

    
2038
                String docidPlusVersion = ((String) docIdList.elementAt(i))
2039
                        + MetaCatUtil.getOption("accNumSeparator") + rev;
2040

    
2041
                //create new documentImpl object
2042
                DocumentImpl documentImplObject = new DocumentImpl(
2043
                        docidPlusVersion);
2044
                //add them to vector
2045
                documentImplList.add(documentImplObject);
2046
            }//try
2047
            catch (Exception e) {
2048
                logMetacat.error("Error in getCurrentAllDocumentImpl: "
2049
                        + e.getMessage());
2050
                // continue the for loop
2051
                continue;
2052
            }
2053
        }//for
2054
        return documentImplList;
2055
    }
2056

    
2057
    /**
2058
     * Transfer a docid vetor to a documentImpl vector. If a DocumentImple
2059
     * object couldn't find for a docid, then the String of this docid was
2060
     * added to vetor rather than DocumentImple object.
2061
     *
2062
     * @param docIdList, a vetor hold a docid list for a data package. In
2063
     *            docid, t here is version number in it.
2064
     */
2065
    private Vector getOldVersionAllDocumentImpl(Vector docIdList)
2066
    {
2067
        //Connection dbConn=null;
2068
        Vector documentImplList = new Vector();
2069
        String siteCode = null;
2070
        String uniqueId = null;
2071
        int rev = 0;
2072

    
2073
        // Check the parameter
2074
        if (docIdList.isEmpty()) { return documentImplList; }//if
2075

    
2076
        //for every docid in vector
2077
        for (int i = 0; i < docIdList.size(); i++) {
2078

    
2079
            String docidPlusVersion = (String) (docIdList.elementAt(i));
2080

    
2081
            try {
2082
                //create new documentImpl object
2083
                DocumentImpl documentImplObject = new DocumentImpl(
2084
                        docidPlusVersion);
2085
                //add them to vector
2086
                documentImplList.add(documentImplObject);
2087
            }//try
2088
            catch (McdbDocNotFoundException notFoundE) {
2089
                logMetacat.error(
2090
                        "Error in DBQuery.getOldVersionAllDocument" + "Imple"
2091
                                + notFoundE.getMessage());
2092
                // Rather than add a DocumentImple object into vetor, a String
2093
                // object
2094
                // - the doicd was added to the vector
2095
                documentImplList.add(docidPlusVersion);
2096
                // Continue the for loop
2097
                continue;
2098
            }//catch
2099
            catch (Exception e) {
2100
                logMetacat.error(
2101
                        "Error in DBQuery.getOldVersionAllDocument" + "Imple"
2102
                                + e.getMessage());
2103
                // Continue the for loop
2104
                continue;
2105
            }//catch
2106

    
2107
        }//for
2108
        return documentImplList;
2109
    }//getOldVersionAllDocumentImple
2110

    
2111
    /**
2112
     * put a data file into a zip output stream
2113
     *
2114
     * @param docImpl, docmentImpl object which will be sent to zip output
2115
     *            stream
2116
     * @param zipOut, the zip output stream which the docImpl will be put
2117
     * @param packageZipEntry, the zip entry name for whole package
2118
     */
2119
    private void addDataFileToZipOutputStream(DocumentImpl docImpl,
2120
            ZipOutputStream zipOut, String packageZipEntry)
2121
            throws ClassNotFoundException, IOException, SQLException,
2122
            McdbException, Exception
2123
    {
2124
        byte[] byteString = null;
2125
        ZipEntry zEntry = null;
2126
        // this is data file; add file to zip
2127
        String filePath = MetaCatUtil.getOption("datafilepath");
2128
        if (!filePath.endsWith("/")) {
2129
            filePath += "/";
2130
        }
2131
        String fileName = filePath + docImpl.getDocID();
2132
        zEntry = new ZipEntry(packageZipEntry + "/data/" + docImpl.getDocID());
2133
        zipOut.putNextEntry(zEntry);
2134
        FileInputStream fin = null;
2135
        try {
2136
            fin = new FileInputStream(fileName);
2137
            byte[] buf = new byte[4 * 1024]; // 4K buffer
2138
            int b = fin.read(buf);
2139
            while (b != -1) {
2140
                zipOut.write(buf, 0, b);
2141
                b = fin.read(buf);
2142
            }//while
2143
            zipOut.closeEntry();
2144
        }//try
2145
        catch (IOException ioe) {
2146
            logMetacat.error("There is an exception: "
2147
                    + ioe.getMessage());
2148
        }//catch
2149
    }//addDataFileToZipOutputStream()
2150

    
2151
    /**
2152
     * create a html summary for data package and put it into zip output stream
2153
     *
2154
     * @param docImplList, the documentImpl ojbects in data package
2155
     * @param zipOut, the zip output stream which the html should be put
2156
     * @param packageZipEntry, the zip entry name for whole package
2157
     */
2158
    private void addHtmlSummaryToZipOutputStream(Vector docImplList,
2159
            ZipOutputStream zipOut, String packageZipEntry) throws Exception
2160
    {
2161
        StringBuffer htmlDoc = new StringBuffer();
2162
        ZipEntry zEntry = null;
2163
        byte[] byteString = null;
2164
        InputStream source;
2165
        DBTransform xmlToHtml;
2166

    
2167
        //create a DBTransform ojbect
2168
        xmlToHtml = new DBTransform();
2169
        //head of html
2170
        htmlDoc.append("<html><head></head><body>");
2171
        for (int i = 0; i < docImplList.size(); i++) {
2172
            // If this String object, this means it is missed data file
2173
            if ((((docImplList.elementAt(i)).getClass()).toString())
2174
                    .equals("class java.lang.String")) {
2175

    
2176
                htmlDoc.append("<a href=\"");
2177
                String dataFileid = (String) docImplList.elementAt(i);
2178
                htmlDoc.append("./data/").append(dataFileid).append("\">");
2179
                htmlDoc.append("Data File: ");
2180
                htmlDoc.append(dataFileid).append("</a><br>");
2181
                htmlDoc.append("<br><hr><br>");
2182

    
2183
            }//if
2184
            else if ((((DocumentImpl) docImplList.elementAt(i)).getDoctype())
2185
                    .compareTo("BIN") != 0) { //this is an xml file so we can
2186
                                              // transform it.
2187
                //transform each file individually then concatenate all of the
2188
                //transformations together.
2189

    
2190
                //for metadata xml title
2191
                htmlDoc.append("<h2>");
2192
                htmlDoc.append(((DocumentImpl) docImplList.elementAt(i))
2193
                        .getDocID());
2194
                //htmlDoc.append(".");
2195
                //htmlDoc.append(((DocumentImpl)docImplList.elementAt(i)).getRev());
2196
                htmlDoc.append("</h2>");
2197
                //do the actual transform
2198
                StringWriter docString = new StringWriter();
2199
                xmlToHtml.transformXMLDocument(((DocumentImpl) docImplList
2200
                        .elementAt(i)).toString(), "-//NCEAS//eml-generic//EN",
2201
                        "-//W3C//HTML//EN", "html", docString);
2202
                htmlDoc.append(docString.toString());
2203
                htmlDoc.append("<br><br><hr><br><br>");
2204
            }//if
2205
            else { //this is a data file so we should link to it in the html
2206
                htmlDoc.append("<a href=\"");
2207
                String dataFileid = ((DocumentImpl) docImplList.elementAt(i))
2208
                        .getDocID();
2209
                htmlDoc.append("./data/").append(dataFileid).append("\">");
2210
                htmlDoc.append("Data File: ");
2211
                htmlDoc.append(dataFileid).append("</a><br>");
2212
                htmlDoc.append("<br><hr><br>");
2213
            }//else
2214
        }//for
2215
        htmlDoc.append("</body></html>");
2216
        byteString = htmlDoc.toString().getBytes();
2217
        zEntry = new ZipEntry(packageZipEntry + "/metadata.html");
2218
        zEntry.setSize(byteString.length);
2219
        zipOut.putNextEntry(zEntry);
2220
        zipOut.write(byteString, 0, byteString.length);
2221
        zipOut.closeEntry();
2222
        //dbConn.close();
2223

    
2224
    }//addHtmlSummaryToZipOutputStream
2225

    
2226
    /**
2227
     * put a data packadge into a zip output stream
2228
     *
2229
     * @param docId, which the user want to put into zip output stream,it has version
2230
     * @param out, a servletoutput stream which the zip output stream will be
2231
     *            put
2232
     * @param user, the username of the user
2233
     * @param groups, the group of the user
2234
     */
2235
    public ZipOutputStream getZippedPackage(String docIdString,
2236
            ServletOutputStream out, String user, String[] groups,
2237
            String passWord) throws ClassNotFoundException, IOException,
2238
            SQLException, McdbException, NumberFormatException, Exception
2239
    {
2240
        ZipOutputStream zOut = null;
2241
        String elementDocid = null;
2242
        DocumentImpl docImpls = null;
2243
        //Connection dbConn = null;
2244
        Vector docIdList = new Vector();
2245
        Vector documentImplList = new Vector();
2246
        Vector htmlDocumentImplList = new Vector();
2247
        String packageId = null;
2248
        String rootName = "package";//the package zip entry name
2249

    
2250
        String docId = null;
2251
        int version = -5;
2252
        // Docid without revision
2253
        docId = MetaCatUtil.getDocIdFromString(docIdString);
2254
        // revision number
2255
        version = MetaCatUtil.getVersionFromString(docIdString);
2256

    
2257
        //check if the reqused docId is a data package id
2258
        if (!isDataPackageId(docId)) {
2259

    
2260
            /*
2261
             * Exception e = new Exception("The request the doc id "
2262
             * +docIdString+ " is not a data package id");
2263
             */
2264

    
2265
            //CB 1/6/03: if the requested docid is not a datapackage, we just
2266
            // zip
2267
            //up the single document and return the zip file.
2268
            if (!hasPermissionToExportPackage(docId, user, groups)) {
2269

    
2270
                Exception e = new Exception("User " + user
2271
                        + " does not have permission"
2272
                        + " to export the data package " + docIdString);
2273
                throw e;
2274
            }
2275

    
2276
            docImpls = new DocumentImpl(docIdString);
2277
            //checking if the user has the permission to read the documents
2278
            if (DocumentImpl.hasReadPermission(user, groups, docImpls
2279
                    .getDocID())) {
2280
                zOut = new ZipOutputStream(out);
2281
                //if the docImpls is metadata
2282
                if ((docImpls.getDoctype()).compareTo("BIN") != 0) {
2283
                    //add metadata into zip output stream
2284
                    addDocToZipOutputStream(docImpls, zOut, rootName);
2285
                }//if
2286
                else {
2287
                    //it is data file
2288
                    addDataFileToZipOutputStream(docImpls, zOut, rootName);
2289
                    htmlDocumentImplList.add(docImpls);
2290
                }//else
2291
            }//if
2292

    
2293
            zOut.finish(); //terminate the zip file
2294
            return zOut;
2295
        }
2296
        // Check the permission of user
2297
        else if (!hasPermissionToExportPackage(docId, user, groups)) {
2298

    
2299
            Exception e = new Exception("User " + user
2300
                    + " does not have permission"
2301
                    + " to export the data package " + docIdString);
2302
            throw e;
2303
        } else //it is a packadge id
2304
        {
2305
            //store the package id
2306
            packageId = docId;
2307
            //get current version in database
2308
            int currentVersion = getCurrentRevFromXMLDoumentsTable(packageId);
2309
            //If it is for current version (-1 means user didn't specify
2310
            // revision)
2311
            if ((version == -1) || version == currentVersion) {
2312
                //get current version number
2313
                version = currentVersion;
2314
                //get package zip entry name
2315
                //it should be docId.revsion.package
2316
                rootName = packageId + MetaCatUtil.getOption("accNumSeparator")
2317
                        + version + MetaCatUtil.getOption("accNumSeparator")
2318
                        + "package";
2319
                //get the whole id list for data packadge
2320
                docIdList = getCurrentDocidListForDataPackage(packageId);
2321
                //get the whole documentImple object
2322
                documentImplList = getCurrentAllDocumentImpl(docIdList);
2323

    
2324
            }//if
2325
            else if (version > currentVersion || version < -1) {
2326
                throw new Exception("The user specified docid: " + docId + "."
2327
                        + version + " doesn't exist");
2328
            }//else if
2329
            else //for an old version
2330
            {
2331

    
2332
                rootName = docIdString
2333
                        + MetaCatUtil.getOption("accNumSeparator") + "package";
2334
                //get the whole id list for data packadge
2335
                docIdList = getOldVersionDocidListForDataPackage(docIdString);
2336

    
2337
                //get the whole documentImple object
2338
                documentImplList = getOldVersionAllDocumentImpl(docIdList);
2339
            }//else
2340

    
2341
            // Make sure documentImplist is not empty
2342
            if (documentImplList.isEmpty()) { throw new Exception(
2343
                    "Couldn't find component for data package: " + packageId); }//if
2344

    
2345
            zOut = new ZipOutputStream(out);
2346
            //put every element into zip output stream
2347
            for (int i = 0; i < documentImplList.size(); i++) {
2348
                // if the object in the vetor is String, this means we couldn't
2349
                // find
2350
                // the document locally, we need find it remote
2351
                if ((((documentImplList.elementAt(i)).getClass()).toString())
2352
                        .equals("class java.lang.String")) {
2353
                    // Get String object from vetor
2354
                    String documentId = (String) documentImplList.elementAt(i);
2355
                    logMetacat.info("docid: " + documentId);
2356
                    // Get doicd without revision
2357
                    String docidWithoutRevision = MetaCatUtil
2358
                            .getDocIdFromString(documentId);
2359
                    logMetacat.info("docidWithoutRevsion: "
2360
                            + docidWithoutRevision);
2361
                    // Get revision
2362
                    String revision = MetaCatUtil
2363
                            .getRevisionStringFromString(documentId);
2364
                    logMetacat.info("revsion from docIdentifier: "
2365
                            + revision);
2366
                    // Zip entry string
2367
                    String zipEntryPath = rootName + "/data/";
2368
                    // Create a RemoteDocument object
2369
                    RemoteDocument remoteDoc = new RemoteDocument(
2370
                            docidWithoutRevision, revision, user, passWord,
2371
                            zipEntryPath);
2372
                    // Here we only read data file from remote metacat
2373
                    String docType = remoteDoc.getDocType();
2374
                    if (docType != null) {
2375
                        if (docType.equals("BIN")) {
2376
                            // Put remote document to zip output
2377
                            remoteDoc.readDocumentFromRemoteServerByZip(zOut);
2378
                            // Add String object to htmlDocumentImplList
2379
                            String elementInHtmlList = remoteDoc
2380
                                    .getDocIdWithoutRevsion()
2381
                                    + MetaCatUtil.getOption("accNumSeparator")
2382
                                    + remoteDoc.getRevision();
2383
                            htmlDocumentImplList.add(elementInHtmlList);
2384
                        }//if
2385
                    }//if
2386

    
2387
                }//if
2388
                else {
2389
                    //create a docmentImpls object (represent xml doc) base on
2390
                    // the docId
2391
                    docImpls = (DocumentImpl) documentImplList.elementAt(i);
2392
                    //checking if the user has the permission to read the
2393
                    // documents
2394
                    if (DocumentImpl.hasReadPermission(user, groups, docImpls
2395
                            .getDocID())) {
2396
                        //if the docImpls is metadata
2397
                        if ((docImpls.getDoctype()).compareTo("BIN") != 0) {
2398
                            //add metadata into zip output stream
2399
                            addDocToZipOutputStream(docImpls, zOut, rootName);
2400
                            //add the documentImpl into the vetor which will
2401
                            // be used in html
2402
                            htmlDocumentImplList.add(docImpls);
2403

    
2404
                        }//if
2405
                        else {
2406
                            //it is data file
2407
                            addDataFileToZipOutputStream(docImpls, zOut,
2408
                                    rootName);
2409
                            htmlDocumentImplList.add(docImpls);
2410
                        }//else
2411
                    }//if
2412
                }//else
2413
            }//for
2414

    
2415
            //add html summary file
2416
            addHtmlSummaryToZipOutputStream(htmlDocumentImplList, zOut,
2417
                    rootName);
2418
            zOut.finish(); //terminate the zip file
2419
            //dbConn.close();
2420
            return zOut;
2421
        }//else
2422
    }//getZippedPackage()
2423

    
2424
    private class ReturnFieldValue
2425
    {
2426

    
2427
        private String docid = null; //return field value for this docid
2428

    
2429
        private String fieldValue = null;
2430

    
2431
        private String xmlFieldValue = null; //return field value in xml
2432
                                             // format
2433

    
2434
        public void setDocid(String myDocid)
2435
        {
2436
            docid = myDocid;
2437
        }
2438

    
2439
        public String getDocid()
2440
        {
2441
            return docid;
2442
        }
2443

    
2444
        public void setFieldValue(String myValue)
2445
        {
2446
            fieldValue = myValue;
2447
        }
2448

    
2449
        public String getFieldValue()
2450
        {
2451
            return fieldValue;
2452
        }
2453

    
2454
        public void setXMLFieldValue(String xml)
2455
        {
2456
            xmlFieldValue = xml;
2457
        }
2458

    
2459
        public String getXMLFieldValue()
2460
        {
2461
            return xmlFieldValue;
2462
        }
2463

    
2464
    }
2465
    
2466
    /**
2467
     * a class to store one result document consisting of a docid and a document
2468
     */
2469
    private class ResultDocument
2470
    {
2471
      public String docid;
2472
      public String document;
2473
      
2474
      public ResultDocument(String docid, String document)
2475
      {
2476
        this.docid = docid;
2477
        this.document = document;
2478
      }
2479
    }
2480
    
2481
    /**
2482
     * a private class to handle a set of resultDocuments
2483
     */
2484
    private class ResultDocumentSet
2485
    {
2486
      private Vector docids;
2487
      private Vector documents;
2488
      
2489
      public ResultDocumentSet()
2490
      {
2491
        docids = new Vector();
2492
        documents = new Vector();
2493
      }
2494
      
2495
      /**
2496
       * adds a result document to the set
2497
       */
2498
      public void addResultDocument(ResultDocument rd)
2499
      {
2500
        if(rd.docid == null)
2501
          rd.docid = "";
2502
        if(rd.document == null)
2503
          rd.document = "";
2504
        
2505
        docids.addElement(rd.docid);
2506
        documents.addElement(rd.document);
2507
      }
2508
      
2509
      /**
2510
       * gets an iterator of docids
2511
       */
2512
      public Iterator getDocids()
2513
      {
2514
        return docids.iterator();
2515
      }
2516
      
2517
      /**
2518
       * gets an iterator of documents
2519
       */
2520
      public Iterator getDocuments()
2521
      {
2522
        return documents.iterator();
2523
      }
2524
      
2525
      /**
2526
       * returns the size of the set
2527
       */
2528
      public int size()
2529
      {
2530
        return docids.size();
2531
      }
2532
      
2533
      /**
2534
       * tests to see if this set contains the given docid
2535
       */
2536
      public boolean containsDocid(String docid)
2537
      {
2538
        for(int i=0; i<docids.size(); i++)
2539
        {
2540
          String docid0 = (String)docids.elementAt(i);
2541
          if(docid0.trim().equals(docid.trim()))
2542
          {
2543
            return true;
2544
          }
2545
        }
2546
        return false;
2547
      }
2548
      
2549
      /**
2550
       * removes the element with the given docid
2551
       */
2552
      public String remove(String docid)
2553
      {
2554
        for(int i=0; i<docids.size(); i++)
2555
        {
2556
          String docid0 = (String)docids.elementAt(i);
2557
          if(docid0.trim().equals(docid.trim()))
2558
          {
2559
            String returnDoc = (String)documents.elementAt(i);
2560
            documents.remove(i);
2561
            docids.remove(i);
2562
            return returnDoc;
2563
          }
2564
        }
2565
        return null;
2566
      }
2567
      
2568
      /**
2569
       * add a result document
2570
       */
2571
      public void put(ResultDocument rd)
2572
      {
2573
        addResultDocument(rd);
2574
      }
2575
      
2576
      /**
2577
       * add a result document by components
2578
       */
2579
      public void put(String docid, String document)
2580
      {
2581
        addResultDocument(new ResultDocument(docid, document));
2582
      }
2583
      
2584
      /**
2585
       * get the document part of the result document by docid
2586
       */
2587
      public Object get(String docid)
2588
      {
2589
        for(int i=0; i<docids.size(); i++)
2590
        {
2591
          String docid0 = (String)docids.elementAt(i);
2592
          if(docid0.trim().equals(docid.trim()))
2593
          {
2594
            return documents.elementAt(i);
2595
          }
2596
        }
2597
        return null;
2598
      }
2599
      
2600
      /**
2601
       * get the document part of the result document by an object
2602
       */
2603
      public Object get(Object o)
2604
      {
2605
        return get((String)o);
2606
      }
2607
      
2608
      /**
2609
       * get an entire result document by index number
2610
       */
2611
      public ResultDocument get(int index)
2612
      {
2613
        return new ResultDocument((String)docids.elementAt(index), 
2614
          (String)documents.elementAt(index));
2615
      }
2616
      
2617
      /**
2618
       * return a string representation of this object
2619
       */
2620
      public String toString()
2621
      {
2622
        String s = "";
2623
        for(int i=0; i<docids.size(); i++)
2624
        {
2625
          s += (String)docids.elementAt(i) + "\n";
2626
        }
2627
        return s;
2628
      }
2629
    }
2630
}
(21-21/66)