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: daigle $'
13
 *     '$Date: 2009-10-16 11:10:37 -0700 (Fri, 16 Oct 2009) $'
14
 * '$Revision: 5090 $'
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.metacat.accesscontrol.AccessControlInterface;
52
import edu.ucsb.nceas.metacat.database.DBConnection;
53
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
54
import edu.ucsb.nceas.metacat.properties.PropertyService;
55
import edu.ucsb.nceas.metacat.util.AuthUtil;
56
import edu.ucsb.nceas.metacat.util.DocumentUtil;
57
import edu.ucsb.nceas.metacat.util.MetacatUtil;
58
import edu.ucsb.nceas.morpho.datapackage.Triple;
59
import edu.ucsb.nceas.morpho.datapackage.TripleCollection;
60
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
61

    
62

    
63
/**
64
 * A Class that searches a relational DB for elements and attributes that have
65
 * free text matches a query string, or structured query matches to a path
66
 * specified node in the XML hierarchy. It returns a result set consisting of
67
 * the document ID for each document that satisfies the query
68
 */
69
public class DBQuery
70
{
71

    
72
    static final int ALL = 1;
73

    
74
    static final int WRITE = 2;
75

    
76
    static final int READ = 4;
77

    
78
    //private Connection conn = null;
79
    private String parserName = null;
80

    
81
    private Logger logMetacat = Logger.getLogger(DBQuery.class);
82

    
83
    /** true if the metacat spatial option is installed **/
84
    private final boolean METACAT_SPATIAL = true;
85

    
86
    /** useful if you just want to grab a list of docids. Since the docids can be very long,
87
         it is a vector of vector  **/
88
    Vector docidOverride = new Vector();
89
    
90
    // a hash table serves as query reuslt cache. Key of hashtable
91
    // is a query string and value is result xml string
92
    private static Hashtable queryResultCache = new Hashtable();
93
    
94
    // Capacity of the query result cache
95
    private static final int QUERYRESULTCACHESIZE;
96
    static {
97
    	int qryRsltCacheSize = 0;
98
    	try {
99
    		qryRsltCacheSize = Integer.parseInt(PropertyService.getProperty("database.queryresultCacheSize"));
100
    	} catch (PropertyNotFoundException pnfe) {
101
    		System.err.println("Could not get QUERYRESULTCACHESIZE property in static block: "
102
					+ pnfe.getMessage());
103
    	}
104
    	QUERYRESULTCACHESIZE = qryRsltCacheSize;
105
    }
106
    
107

    
108
    // Size of page for non paged query
109
    private static final int NONPAGESIZE = 99999999;
110
    /**
111
     * the main routine used to test the DBQuery utility.
112
     * <p>
113
     * Usage: java DBQuery <xmlfile>
114
     *
115
     * @param xmlfile the filename of the xml file containing the query
116
     */
117
    static public void main(String[] args)
118
    {
119

    
120
        if (args.length < 1) {
121
            System.err.println("Wrong number of arguments!!!");
122
            System.err.println("USAGE: java DBQuery [-t] [-index] <xmlfile>");
123
            return;
124
        } else {
125
            try {
126

    
127
                int i = 0;
128
                boolean showRuntime = false;
129
                boolean useXMLIndex = false;
130
                if (args[i].equals("-t")) {
131
                    showRuntime = true;
132
                    i++;
133
                }
134
                if (args[i].equals("-index")) {
135
                    useXMLIndex = true;
136
                    i++;
137
                }
138
                String xmlfile = args[i];
139

    
140
                // Time the request if asked for
141
                double startTime = System.currentTimeMillis();
142

    
143
                // Open a connection to the database
144
                //Connection dbconn = util.openDBConnection();
145

    
146
                double connTime = System.currentTimeMillis();
147

    
148
                // Execute the query
149
                DBQuery queryobj = new DBQuery();
150
                FileReader xml = new FileReader(new File(xmlfile));
151
                Hashtable nodelist = null;
152
                //nodelist = queryobj.findDocuments(xml, null, null, useXMLIndex);
153

    
154
                // Print the reulting document listing
155
                StringBuffer result = new StringBuffer();
156
                String document = null;
157
                String docid = null;
158
                result.append("<?xml version=\"1.0\"?>\n");
159
                result.append("<resultset>\n");
160

    
161
                if (!showRuntime) {
162
                    Enumeration doclist = nodelist.keys();
163
                    while (doclist.hasMoreElements()) {
164
                        docid = (String) doclist.nextElement();
165
                        document = (String) nodelist.get(docid);
166
                        result.append("  <document>\n    " + document
167
                                + "\n  </document>\n");
168
                    }
169

    
170
                    result.append("</resultset>\n");
171
                }
172
                // Time the request if asked for
173
                double stopTime = System.currentTimeMillis();
174
                double dbOpenTime = (connTime - startTime) / 1000;
175
                double readTime = (stopTime - connTime) / 1000;
176
                double executionTime = (stopTime - startTime) / 1000;
177
                if (showRuntime) {
178
                    System.out.print("  " + executionTime);
179
                    System.out.print("  " + dbOpenTime);
180
                    System.out.print("  " + readTime);
181
                    System.out.print("  " + nodelist.size());
182
                    System.out.println();
183
                }
184
                //System.out.println(result);
185
                //write into a file "result.txt"
186
                if (!showRuntime) {
187
                    File f = new File("./result.txt");
188
                    FileWriter fw = new FileWriter(f);
189
                    BufferedWriter out = new BufferedWriter(fw);
190
                    out.write(result.toString());
191
                    out.flush();
192
                    out.close();
193
                    fw.close();
194
                }
195

    
196
            } catch (Exception e) {
197
                System.err.println("Error in DBQuery.main");
198
                System.err.println(e.getMessage());
199
                e.printStackTrace(System.err);
200
            }
201
        }
202
    }
203

    
204
    /**
205
     * construct an instance of the DBQuery class
206
     *
207
     * <p>
208
     * Generally, one would call the findDocuments() routine after creating an
209
     * instance to specify the search query
210
     * </p>
211
     *
212

    
213
     * @param parserName the fully qualified name of a Java class implementing
214
     *            the org.xml.sax.XMLReader interface
215
     */
216
    public DBQuery() throws PropertyNotFoundException
217
    {
218
        String parserName = PropertyService.getProperty("xml.saxparser");
219
        this.parserName = parserName;
220
    }
221

    
222
    /**
223
     * 
224
     * Construct an instance of DBQuery Class
225
     * BUT accept a docid Vector that will supersede
226
     * the query.printSQL() method
227
     *
228
     * If a docid Vector is passed in,
229
     * the docids will be used to create a simple IN query 
230
     * without the multiple subselects of the printSQL() method
231
     *
232
     * Using this constructor, we just check for 
233
     * a docidOverride Vector in the findResultDoclist() method
234
     *
235
     * @param docids List of docids to display in the resultset
236
     */
237
    public DBQuery(Vector docids) throws PropertyNotFoundException
238
    {
239
    	// since the query will be too long to be handled, so we divided the 
240
    	// docids vector into couple vectors.
241
    	int size = (new Integer(PropertyService.getProperty("database.appResultsetSize"))).intValue();
242
    	logMetacat.info("The size of select doicds is "+docids.size());
243
    	logMetacat.info("The application result size in metacat.properties is "+size);
244
    	Vector subset = new Vector();
245
    	if (docids != null && docids.size() > size)
246
    	{
247
    		int index = 0;
248
    		for (int i=0; i< docids.size(); i++)
249
    		{
250
    			
251
    			if (index < size)
252
    			{  	
253
    				subset.add(docids.elementAt(i));
254
    				index ++;
255
    			}
256
    			else
257
    			{
258
    				docidOverride.add(subset);
259
    				subset = new Vector();
260
    				subset.add(docids.elementAt(i));
261
    			    index = 1;
262
    			}
263
    		}
264
    		if (!subset.isEmpty())
265
    		{
266
    			docidOverride.add(subset);
267
    		}
268
    		
269
    	}
270
    	else
271
    	{
272
    		this.docidOverride.add(docids);
273
    	}
274
        
275
        String parserName = PropertyService.getProperty("xml.saxparser");
276
        this.parserName = parserName;
277
    }
278

    
279
  /**
280
   * Method put the search result set into out printerwriter
281
   * @param resoponse the return response
282
   * @param out the output printer
283
   * @param params the paratermer hashtable
284
   * @param user the user name (it maybe different to the one in param)
285
   * @param groups the group array
286
   * @param sessionid  the sessionid
287
   */
288
  public void findDocuments(HttpServletResponse response,
289
                                       PrintWriter out, Hashtable params,
290
                                       String user, String[] groups,
291
                                       String sessionid) throws PropertyNotFoundException
292
  {
293
    boolean useXMLIndex = (new Boolean(PropertyService.getProperty("database.usexmlindex")))
294
               .booleanValue();
295
    findDocuments(response, out, params, user, groups, sessionid, useXMLIndex);
296

    
297
  }
298

    
299

    
300
    /**
301
     * Method put the search result set into out printerwriter
302
     * @param resoponse the return response
303
     * @param out the output printer
304
     * @param params the paratermer hashtable
305
     * @param user the user name (it maybe different to the one in param)
306
     * @param groups the group array
307
     * @param sessionid  the sessionid
308
     */
309
    public void findDocuments(HttpServletResponse response,
310
                                         PrintWriter out, Hashtable params,
311
                                         String user, String[] groups,
312
                                         String sessionid, boolean useXMLIndex)
313
    {
314
      int pagesize = 0;
315
      int pagestart = 0;
316
      
317
      if(params.containsKey("pagesize") && params.containsKey("pagestart"))
318
      {
319
        String pagesizeStr = ((String[])params.get("pagesize"))[0];
320
        String pagestartStr = ((String[])params.get("pagestart"))[0];
321
        if(pagesizeStr != null && pagestartStr != null)
322
        {
323
          pagesize = (new Integer(pagesizeStr)).intValue();
324
          pagestart = (new Integer(pagestartStr)).intValue();
325
        }
326
      }
327
      
328
      String xmlquery = null;
329
      String qformat = null;
330
      // get query and qformat
331
      try {
332
    	xmlquery = ((String[])params.get("query"))[0];
333

    
334
        logMetacat.info("SESSIONID: " + sessionid);
335
        logMetacat.info("xmlquery: " + xmlquery);
336
        qformat = ((String[])params.get("qformat"))[0];
337
        logMetacat.info("qformat: " + qformat);
338
      }
339
      catch (Exception ee)
340
      {
341
        logMetacat.error("Couldn't retrieve xmlquery or qformat value from "
342
                  +"params hashtable in DBQuery.findDocuments: "
343
                  + ee.getMessage()); 
344
      }
345
      // Get the XML query and covert it into a SQL statment
346
      QuerySpecification qspec = null;
347
      if ( xmlquery != null)
348
      {
349
         xmlquery = transformQuery(xmlquery);
350
         try
351
         {
352
           qspec = new QuerySpecification(xmlquery,
353
                                          parserName,
354
                                          PropertyService.getProperty("document.accNumSeparator"));
355
         }
356
         catch (Exception ee)
357
         {
358
           logMetacat.error("error generating QuerySpecification object"
359
                                    +" in DBQuery.findDocuments"
360
                                    + ee.getMessage());
361
         }
362
      }
363

    
364

    
365

    
366
      if (qformat != null && qformat.equals(MetacatUtil.XMLFORMAT))
367
      {
368
        //xml format
369
        response.setContentType("text/xml");
370
        createResultDocument(xmlquery, qspec, out, user, groups, useXMLIndex, 
371
          pagesize, pagestart, sessionid);
372
      }//if
373
      else
374
      {
375
        //knb format, in this case we will get whole result and sent it out
376
        response.setContentType("text/html");
377
        PrintWriter nonout = null;
378
        StringBuffer xml = createResultDocument(xmlquery, qspec, nonout, user,
379
                                                groups, useXMLIndex, pagesize, 
380
                                                pagestart, sessionid);
381
        
382
        //transfer the xml to html
383
        try
384
        {
385
         double startHTMLTransform = System.currentTimeMillis()/1000;
386
         DBTransform trans = new DBTransform();
387
         response.setContentType("text/html");
388

    
389
         // if the user is a moderator, then pass a param to the 
390
         // xsl specifying the fact
391
         if(AuthUtil.isModerator(user, groups)){
392
        	 params.put("isModerator", new String[] {"true"});
393
         }
394

    
395
         trans.transformXMLDocument(xml.toString(), "-//NCEAS//resultset//EN",
396
                                 "-//W3C//HTML//EN", qformat, out, params,
397
                                 sessionid);
398
         double endHTMLTransform = System.currentTimeMillis()/1000;
399
          logMetacat.warn("The time to transfrom resultset from xml to html format is "
400
                  		                             +(endHTMLTransform -startHTMLTransform));
401
          MetacatUtil.writeDebugToFile("---------------------------------------------------------------------------------------------------------------Transfrom xml to html  "
402
                             +(endHTMLTransform -startHTMLTransform));
403
          MetacatUtil.writeDebugToDelimiteredFile(" "+(endHTMLTransform -startHTMLTransform), false);
404
        }
405
        catch(Exception e)
406
        {
407
         logMetacat.error("Error in MetaCatServlet.transformResultset:"
408
                                +e.getMessage());
409
         }
410

    
411
      }//else
412

    
413
  }
414
  
415
  /**
416
   * Transforms a hashtable of documents to an xml or html result and sent
417
   * the content to outputstream. Keep going untill hastable is empty. stop it.
418
   * add the QuerySpecification as parameter is for ecogrid. But it is duplicate
419
   * to xmlquery String
420
   * @param xmlquery
421
   * @param qspec
422
   * @param out
423
   * @param user
424
   * @param groups
425
   * @param useXMLIndex
426
   * @param sessionid
427
   * @return
428
   */
429
    public StringBuffer createResultDocument(String xmlquery,
430
                                              QuerySpecification qspec,
431
                                              PrintWriter out,
432
                                              String user, String[] groups,
433
                                              boolean useXMLIndex)
434
    {
435
    	return createResultDocument(xmlquery,qspec,out, user,groups, useXMLIndex, 0, 0,"");
436
    }
437

    
438
  /*
439
   * Transforms a hashtable of documents to an xml or html result and sent
440
   * the content to outputstream. Keep going untill hastable is empty. stop it.
441
   * add the QuerySpecification as parameter is for ecogrid. But it is duplicate
442
   * to xmlquery String
443
   */
444
  public StringBuffer createResultDocument(String xmlquery,
445
                                            QuerySpecification qspec,
446
                                            PrintWriter out,
447
                                            String user, String[] groups,
448
                                            boolean useXMLIndex, int pagesize,
449
                                            int pagestart, String sessionid)
450
  {
451
    DBConnection dbconn = null;
452
    int serialNumber = -1;
453
    StringBuffer resultset = new StringBuffer();
454

    
455
    //try to get the cached version first    
456
    // Hashtable sessionHash = MetaCatServlet.getSessionHash();
457
    // HttpSession sess = (HttpSession)sessionHash.get(sessionid);
458

    
459
    
460
    resultset.append("<?xml version=\"1.0\"?>\n");
461
    resultset.append("<resultset>\n");
462
    resultset.append("  <pagestart>" + pagestart + "</pagestart>\n");
463
    resultset.append("  <pagesize>" + pagesize + "</pagesize>\n");
464
    resultset.append("  <nextpage>" + (pagestart + 1) + "</nextpage>\n");
465
    resultset.append("  <previouspage>" + (pagestart - 1) + "</previouspage>\n");
466

    
467
    resultset.append("  <query>" + xmlquery + "</query>");
468
    //send out a new query
469
    if (out != null)
470
    {
471
      out.println(resultset.toString());
472
    }
473
    if (qspec != null)
474
    {
475
      try
476
      {
477

    
478
        //checkout the dbconnection
479
        dbconn = DBConnectionPool.getDBConnection("DBQuery.findDocuments");
480
        serialNumber = dbconn.getCheckOutSerialNumber();
481

    
482
        //print out the search result
483
        // search the doc list
484
        Vector givenDocids = new Vector();
485
        StringBuffer resultContent = new StringBuffer();
486
        if (docidOverride == null || docidOverride.size() == 0)
487
        {
488
        	logMetacat.info("Not in map query");
489
        	resultContent = findResultDoclist(qspec, out, user, groups,
490
                    dbconn, useXMLIndex, pagesize, pagestart, 
491
                    sessionid, givenDocids);
492
        }
493
        else
494
        {
495
        	logMetacat.info("In map query");
496
        	// since docid can be too long to be handled. We divide it into several parts
497
        	for (int i= 0; i<docidOverride.size(); i++)
498
        	{
499
        	   logMetacat.info("in loop===== "+i);
500
        		givenDocids = (Vector)docidOverride.elementAt(i);
501
        		StringBuffer subset = findResultDoclist(qspec, out, user, groups,
502
                        dbconn, useXMLIndex, pagesize, pagestart, 
503
                        sessionid, givenDocids);
504
        		resultContent.append(subset);
505
        	}
506
        }
507
           
508
        resultset.append(resultContent);
509
      } //try
510
      catch (IOException ioe)
511
      {
512
        logMetacat.error("IO error in DBQuery.findDocuments:");
513
        logMetacat.error(ioe.getMessage());
514

    
515
      }
516
      catch (SQLException e)
517
      {
518
        logMetacat.error("SQL Error in DBQuery.findDocuments: "
519
                                 + e.getMessage());
520
      }
521
      catch (Exception ee)
522
      {
523
        logMetacat.error("Exception in DBQuery.findDocuments: "
524
                                 + ee.getMessage());
525
        ee.printStackTrace();
526
      }
527
      finally
528
      {
529
        DBConnectionPool.returnDBConnection(dbconn, serialNumber);
530
      } //finally
531
    }//if
532
    String closeRestultset = "</resultset>";
533
    resultset.append(closeRestultset);
534
    if (out != null)
535
    {
536
      out.println(closeRestultset);
537
    }
538

    
539
    //default to returning the whole resultset
540
    return resultset;
541
  }//createResultDocuments
542

    
543
    /*
544
     * Find the doc list which match the query
545
     */
546
    private StringBuffer findResultDoclist(QuerySpecification qspec,
547
                                      PrintWriter out,
548
                                      String user, String[]groups,
549
                                      DBConnection dbconn, boolean useXMLIndex,
550
                                      int pagesize, int pagestart, String sessionid, Vector givenDocids)
551
                                      throws Exception
552
    {
553
      StringBuffer resultsetBuffer = new StringBuffer();
554
      String query = null;
555
      int count = 0;
556
      int index = 0;
557
      ResultDocumentSet docListResult = new ResultDocumentSet();
558
      PreparedStatement pstmt = null;
559
      String docid = null;
560
      String docname = null;
561
      String doctype = null;
562
      String createDate = null;
563
      String updateDate = null;
564
      StringBuffer document = null;
565
      boolean lastpage = false;
566
      int rev = 0;
567
      double startTime = 0;
568
      int offset = 1;
569
      double startSelectionTime = System.currentTimeMillis()/1000;
570
      ResultSet rs = null;
571
           
572
   
573
      // this is a hack for offset. in postgresql 7, if the returned docid list is too long,
574
      //the extend query which base on the docid will be too long to be run. So we 
575
      // have to cut them into different parts. Page query don't need it somehow.
576
      if (out == null)
577
      {
578
        // for html page, we put everything into one page
579
        offset =
580
            (new Integer(PropertyService.getProperty("database.webResultsetSize"))).intValue();
581
      }
582
      else
583
      {
584
          offset =
585
              (new Integer(PropertyService.getProperty("database.appResultsetSize"))).intValue();
586
      }
587

    
588
      /*
589
       * Check the docidOverride Vector
590
       * if defined, we bypass the qspec.printSQL() method
591
       * and contruct a simpler query based on a 
592
       * list of docids rather than a bunch of subselects
593
       */
594
      if ( givenDocids == null || givenDocids.size() == 0 ) {
595
          query = qspec.printSQL(useXMLIndex);
596
      } else {
597
          logMetacat.info("*** docid override " + givenDocids.size());
598
          StringBuffer queryBuffer = new StringBuffer( "SELECT docid,docname,doctype,date_created, date_updated, rev " );
599
          queryBuffer.append( " FROM xml_documents WHERE docid IN (" );
600
          for (int i = 0; i < givenDocids.size(); i++) {  
601
              queryBuffer.append("'");
602
              queryBuffer.append( (String)givenDocids.elementAt(i) );
603
              queryBuffer.append("',");
604
          }
605
          // empty string hack 
606
          queryBuffer.append( "'') " );
607
          query = queryBuffer.toString();
608
      } 
609
      String ownerQuery = getOwnerQuery(user);
610
      //logMetacat.debug("query: " + query);
611
      logMetacat.debug("owner query: "+ownerQuery);
612
      // if query is not the owner query, we need to check the permission
613
      // otherwise we don't need (owner has all permission by default)
614
      if (!query.equals(ownerQuery))
615
      {
616
        // set user name and group
617
        qspec.setUserName(user);
618
        qspec.setGroup(groups);
619
        // Get access query
620
        String accessQuery = qspec.getAccessQuery();
621
        if(!query.endsWith("WHERE")){
622
            query = query + accessQuery;
623
        } else {
624
            query = query + accessQuery.substring(4, accessQuery.length());
625
        }
626
        
627
      }
628
      logMetacat.debug("============ final selection query: " + query);
629
      String selectionAndExtendedQuery = null;
630
      // we only get cache for public
631
      if (user != null && user.equalsIgnoreCase("public") 
632
     		 && pagesize == 0 && PropertyService.getProperty("database.queryCacheOn").equals("true"))
633
      {
634
    	  selectionAndExtendedQuery = query +qspec.getReturnDocList()+qspec.getReturnFieldList();
635
   	      String cachedResult = getResultXMLFromCache(selectionAndExtendedQuery);
636
   	      logMetacat.debug("The key of query cache is "+selectionAndExtendedQuery);
637
   	      //System.out.println("==========the string from cache is "+cachedResult);
638
   	      if (cachedResult != null)
639
   	      {
640
   	    	logMetacat.info("result from cache !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
641
   	    	 if (out != null)
642
   	         {
643
   	             out.println(cachedResult);
644
   	         }
645
   	    	 resultsetBuffer.append(cachedResult);
646
   	    	 return resultsetBuffer;
647
   	      }
648
      }
649
      
650
      startTime = System.currentTimeMillis() / 1000;
651
      pstmt = dbconn.prepareStatement(query);
652
      rs = pstmt.executeQuery();
653

    
654
      double queryExecuteTime = System.currentTimeMillis() / 1000;
655
      logMetacat.debug("Time to execute select docid query is "
656
                    + (queryExecuteTime - startTime));
657
      MetacatUtil.writeDebugToFile("\n\n\n\n\n\nExecute selection query  "
658
              + (queryExecuteTime - startTime));
659
      MetacatUtil.writeDebugToDelimiteredFile(""+(queryExecuteTime - startTime), false);
660

    
661
      boolean tableHasRows = rs.next();
662
      
663
      if(pagesize == 0)
664
      { //this makes sure we get all results if there is no paging
665
        pagesize = NONPAGESIZE;
666
        pagestart = NONPAGESIZE;
667
      } 
668
      
669
      int currentIndex = 0;
670
      while (tableHasRows)
671
      {
672
        logMetacat.debug("############getting result: " + currentIndex);
673
        docid = rs.getString(1).trim();
674
        logMetacat.debug("############processing: " + docid);
675
        docname = rs.getString(2);
676
        doctype = rs.getString(3);
677
        logMetacat.debug("############processing: " + doctype);
678
        createDate = rs.getString(4);
679
        updateDate = rs.getString(5);
680
        rev = rs.getInt(6);
681
        
682
         Vector returndocVec = qspec.getReturnDocList();
683
       if (returndocVec.size() == 0 || returndocVec.contains(doctype))
684
        {
685
          logMetacat.debug("NOT Back tracing now...");
686
           document = new StringBuffer();
687

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

    
779
     resultsetBuffer.append("\n<lastpage>" + lastpage + "</lastpage>\n");
780
     if (out != null)
781
     {
782
         out.println("\n<lastpage>" + lastpage + "</lastpage>\n");
783
     }
784
     
785
     // now we only cached none-paged query and user is public
786
     if (user != null && user.equalsIgnoreCase("public") 
787
    		 && pagesize == NONPAGESIZE && PropertyService.getProperty("database.queryCacheOn").equals("true"))
788
     {
789
       //System.out.println("the string stored into cache is "+ resultsetBuffer.toString());
790
  	   storeQueryResultIntoCache(selectionAndExtendedQuery, resultsetBuffer.toString());
791
     }
792
          
793
     return resultsetBuffer;
794
    }//findReturnDoclist
795

    
796

    
797
    /*
798
     * Send completed search hashtable(part of reulst)to output stream
799
     * and buffer into a buffer stream
800
     */
801
    private StringBuffer handleSubsetResult(QuerySpecification qspec,
802
                                           StringBuffer resultset,
803
                                           PrintWriter out, ResultDocumentSet partOfDoclist,
804
                                           String user, String[]groups,
805
                                       DBConnection dbconn, boolean useXMLIndex)
806
                                       throws Exception
807
   {
808
     double startReturnField = System.currentTimeMillis()/1000;
809
     // check if there is a record in xml_returnfield
810
     // and get the returnfield_id and usage count
811
     int usage_count = getXmlReturnfieldsTableId(qspec, dbconn);
812
     boolean enterRecords = false;
813

    
814
     // get value of database.xmlReturnfieldCount
815
     int count = (new Integer(PropertyService
816
                            .getProperty("database.xmlReturnfieldCount")))
817
                            .intValue();
818

    
819
     // set enterRecords to true if usage_count is more than the offset
820
     // specified in metacat.properties
821
     if(usage_count > count){
822
         enterRecords = true;
823
     }
824

    
825
     if(returnfield_id < 0){
826
         logMetacat.warn("Error in getting returnfield id from"
827
                                  + "xml_returnfield table");
828
         enterRecords = false;
829
     }
830

    
831
     // get the hashtable containing the docids that already in the
832
     // xml_queryresult table
833
     logMetacat.info("size of partOfDoclist before"
834
                             + " docidsInQueryresultTable(): "
835
                             + partOfDoclist.size());
836
     double startGetReturnValueFromQueryresultable = System.currentTimeMillis()/1000;
837
     Hashtable queryresultDocList = docidsInQueryresultTable(returnfield_id,
838
                                                        partOfDoclist, dbconn);
839

    
840
     // remove the keys in queryresultDocList from partOfDoclist
841
     Enumeration _keys = queryresultDocList.keys();
842
     while (_keys.hasMoreElements()){
843
         partOfDoclist.remove((String)_keys.nextElement());
844
     }
845
     double endGetReturnValueFromQueryresultable = System.currentTimeMillis()/1000;
846
     logMetacat.warn("Time to get return fields from xml_queryresult table is (Part1 in return fields) " +
847
          		               (endGetReturnValueFromQueryresultable-startGetReturnValueFromQueryresultable));
848
     MetacatUtil.writeDebugToFile("-----------------------------------------Get fields from xml_queryresult(Part1 in return fields) " +
849
               (endGetReturnValueFromQueryresultable-startGetReturnValueFromQueryresultable));
850
     MetacatUtil.writeDebugToDelimiteredFile(" " +
851
             (endGetReturnValueFromQueryresultable-startGetReturnValueFromQueryresultable),false);
852
     // backup the keys-elements in partOfDoclist to check later
853
     // if the doc entry is indexed yet
854
     Hashtable partOfDoclistBackup = new Hashtable();
855
     Iterator itt = partOfDoclist.getDocids();
856
     while (itt.hasNext()){
857
       Object key = itt.next();
858
         partOfDoclistBackup.put(key, partOfDoclist.get(key));
859
     }
860

    
861
     logMetacat.info("size of partOfDoclist after"
862
                             + " docidsInQueryresultTable(): "
863
                             + partOfDoclist.size());
864

    
865
     //add return fields for the documents in partOfDoclist
866
     partOfDoclist = addReturnfield(partOfDoclist, qspec, user, groups,
867
                                        dbconn, useXMLIndex);
868
     double endExtendedQuery = System.currentTimeMillis()/1000;
869
     logMetacat.warn("Get fields from index and node table (Part2 in return fields) "
870
        		                                          + (endExtendedQuery - endGetReturnValueFromQueryresultable));
871
     MetacatUtil.writeDebugToFile("-----------------------------------------Get fields from extened query(Part2 in return fields) "
872
             + (endExtendedQuery - endGetReturnValueFromQueryresultable));
873
     MetacatUtil.writeDebugToDelimiteredFile(" "
874
             + (endExtendedQuery - endGetReturnValueFromQueryresultable), false);
875
     //add relationship part part docid list for the documents in partOfDocList
876
     //partOfDoclist = addRelationship(partOfDoclist, qspec, dbconn, useXMLIndex);
877

    
878
     double startStoreReturnField = System.currentTimeMillis()/1000;
879
     Iterator keys = partOfDoclist.getDocids();
880
     String key = null;
881
     String element = null;
882
     String query = null;
883
     int offset = (new Integer(PropertyService
884
                               .getProperty("database.queryresultStringLength")))
885
                               .intValue();
886
     while (keys.hasNext())
887
     {
888
         key = (String) keys.next();
889
         element = (String)partOfDoclist.get(key);
890
         
891
	 // check if the enterRecords is true, elements is not null, element's
892
         // length is less than the limit of table column and if the document
893
         // has been indexed already
894
         if(enterRecords && element != null
895
		&& element.length() < offset
896
		&& element.compareTo((String) partOfDoclistBackup.get(key)) != 0){
897
             query = "INSERT INTO xml_queryresult (returnfield_id, docid, "
898
                 + "queryresult_string) VALUES (?, ?, ?)";
899

    
900
             PreparedStatement pstmt = null;
901
             pstmt = dbconn.prepareStatement(query);
902
             pstmt.setInt(1, returnfield_id);
903
             pstmt.setString(2, key);
904
             pstmt.setString(3, element);
905
            
906
             dbconn.increaseUsageCount(1);
907
             try
908
             {
909
            	 pstmt.execute();
910
             }
911
             catch(Exception e)
912
             {
913
            	 logMetacat.warn("couldn't insert the element to xml_queryresult table "+e.getLocalizedMessage());
914
             }
915
             finally
916
             {
917
                pstmt.close();
918
             }
919
         }
920
        
921
         // A string with element
922
         String xmlElement = "  <document>" + element + "</document>";
923

    
924
         //send single element to output
925
         if (out != null)
926
         {
927
             out.println(xmlElement);
928
         }
929
         resultset.append(xmlElement);
930
     }//while
931
     
932
     double endStoreReturnField = System.currentTimeMillis()/1000;
933
     logMetacat.warn("Time to store new return fields into xml_queryresult table (Part4 in return fields) "
934
                   + (endStoreReturnField -startStoreReturnField));
935
     MetacatUtil.writeDebugToFile("-----------------------------------------Insert new record to xml_queryresult(Part4 in return fields) "
936
             + (endStoreReturnField -startStoreReturnField));
937
     MetacatUtil.writeDebugToDelimiteredFile(" "
938
             + (endStoreReturnField -startStoreReturnField), false);
939
     
940
     Enumeration keysE = queryresultDocList.keys();
941
     while (keysE.hasMoreElements())
942
     {
943
         key = (String) keysE.nextElement();
944
         element = (String)queryresultDocList.get(key);
945
         // A string with element
946
         String xmlElement = "  <document>" + element + "</document>";
947
         //send single element to output
948
         if (out != null)
949
         {
950
             out.println(xmlElement);
951
         }
952
         resultset.append(xmlElement);
953
     }//while
954
     double returnFieldTime = System.currentTimeMillis() / 1000;
955
     logMetacat.warn("======Total time to get return fields is: "
956
                           + (returnFieldTime - startReturnField));
957
     MetacatUtil.writeDebugToFile("---------------------------------------------------------------------------------------------------------------"+
958
    		 "Total to get return fields  "
959
                                   + (returnFieldTime - startReturnField));
960
     MetacatUtil.writeDebugToDelimiteredFile(" "+ (returnFieldTime - startReturnField), false);
961
     return resultset;
962
 }
963

    
964
   /**
965
    * Get the docids already in xml_queryresult table and corresponding
966
    * queryresultstring as a hashtable
967
    */
968
   private Hashtable docidsInQueryresultTable(int returnfield_id,
969
                                              ResultDocumentSet partOfDoclist,
970
                                              DBConnection dbconn){
971

    
972
         Hashtable returnValue = new Hashtable();
973
         PreparedStatement pstmt = null;
974
         ResultSet rs = null;
975

    
976
         // get partOfDoclist as string for the query
977
         Iterator keylist = partOfDoclist.getDocids();
978
         StringBuffer doclist = new StringBuffer();
979
         while (keylist.hasNext())
980
         {
981
             doclist.append("'");
982
             doclist.append((String) keylist.next());
983
             doclist.append("',");
984
         }//while
985

    
986

    
987
         if (doclist.length() > 0)
988
         {
989
             doclist.deleteCharAt(doclist.length() - 1); //remove the last comma
990

    
991
             // the query to find out docids from xml_queryresult
992
             String query = "select docid, queryresult_string from "
993
                          + "xml_queryresult where returnfield_id = " +
994
                          returnfield_id +" and docid in ("+ doclist + ")";
995
             logMetacat.info("Query to get docids from xml_queryresult:"
996
                                      + query);
997

    
998
             try {
999
                 // prepare and execute the query
1000
                 pstmt = dbconn.prepareStatement(query);
1001
                 dbconn.increaseUsageCount(1);
1002
                 pstmt.execute();
1003
                 rs = pstmt.getResultSet();
1004
                 boolean tableHasRows = rs.next();
1005
                 while (tableHasRows) {
1006
                     // store the returned results in the returnValue hashtable
1007
                     String key = rs.getString(1);
1008
                     String element = rs.getString(2);
1009

    
1010
                     if(element != null){
1011
                         returnValue.put(key, element);
1012
                     } else {
1013
                         logMetacat.info("Null elment found ("
1014
                         + "DBQuery.docidsInQueryresultTable)");
1015
                     }
1016
                     tableHasRows = rs.next();
1017
                 }
1018
                 rs.close();
1019
                 pstmt.close();
1020
             } catch (Exception e){
1021
                 logMetacat.error("Error getting docids from "
1022
                                          + "queryresult in "
1023
                                          + "DBQuery.docidsInQueryresultTable: "
1024
                                          + e.getMessage());
1025
              }
1026
         }
1027
         return returnValue;
1028
     }
1029

    
1030

    
1031
   /**
1032
    * Method to get id from xml_returnfield table
1033
    * for a given query specification
1034
    */
1035
   private int returnfield_id;
1036
   private int getXmlReturnfieldsTableId(QuerySpecification qspec,
1037
                                           DBConnection dbconn){
1038
       int id = -1;
1039
       int count = 1;
1040
       PreparedStatement pstmt = null;
1041
       ResultSet rs = null;
1042
       String returnfield = qspec.getSortedReturnFieldString();
1043

    
1044
       // query for finding the id from xml_returnfield
1045
       String query = "SELECT returnfield_id, usage_count FROM xml_returnfield "
1046
            + "WHERE returnfield_string LIKE ?";
1047
       logMetacat.info("ReturnField Query:" + query);
1048

    
1049
       try {
1050
           // prepare and run the query
1051
           pstmt = dbconn.prepareStatement(query);
1052
           pstmt.setString(1,returnfield);
1053
           dbconn.increaseUsageCount(1);
1054
           pstmt.execute();
1055
           rs = pstmt.getResultSet();
1056
           boolean tableHasRows = rs.next();
1057

    
1058
           // if record found then increase the usage count
1059
           // else insert a new record and get the id of the new record
1060
           if(tableHasRows){
1061
               // get the id
1062
               id = rs.getInt(1);
1063
               count = rs.getInt(2) + 1;
1064
               rs.close();
1065
               pstmt.close();
1066

    
1067
               // increase the usage count
1068
               query = "UPDATE xml_returnfield SET usage_count ='" + count
1069
                   + "' WHERE returnfield_id ='"+ id +"'";
1070
               logMetacat.info("ReturnField Table Update:"+ query);
1071

    
1072
               pstmt = dbconn.prepareStatement(query);
1073
               dbconn.increaseUsageCount(1);
1074
               pstmt.execute();
1075
               pstmt.close();
1076

    
1077
           } else {
1078
               rs.close();
1079
               pstmt.close();
1080

    
1081
               // insert a new record
1082
               query = "INSERT INTO xml_returnfield (returnfield_string, usage_count)"
1083
                   + "VALUES (?, '1')";
1084
               logMetacat.info("ReturnField Table Insert:"+ query);
1085
               pstmt = dbconn.prepareStatement(query);
1086
               pstmt.setString(1, returnfield);
1087
               dbconn.increaseUsageCount(1);
1088
               pstmt.execute();
1089
               pstmt.close();
1090

    
1091
               // get the id of the new record
1092
               query = "SELECT returnfield_id FROM xml_returnfield "
1093
                   + "WHERE returnfield_string LIKE ?";
1094
               logMetacat.info("ReturnField query after Insert:" + query);
1095
               pstmt = dbconn.prepareStatement(query);
1096
               pstmt.setString(1, returnfield);
1097

    
1098
               dbconn.increaseUsageCount(1);
1099
               pstmt.execute();
1100
               rs = pstmt.getResultSet();
1101
               if(rs.next()){
1102
                   id = rs.getInt(1);
1103
               } else {
1104
                   id = -1;
1105
               }
1106
               rs.close();
1107
               pstmt.close();
1108
           }
1109

    
1110
       } catch (Exception e){
1111
           logMetacat.error("Error getting id from xml_returnfield in "
1112
                                     + "DBQuery.getXmlReturnfieldsTableId: "
1113
                                     + e.getMessage());
1114
           id = -1;
1115
       }
1116

    
1117
       returnfield_id = id;
1118
       return count;
1119
   }
1120

    
1121

    
1122
    /*
1123
     * A method to add return field to return doclist hash table
1124
     */
1125
    private ResultDocumentSet addReturnfield(ResultDocumentSet docListResult,
1126
                                      QuerySpecification qspec,
1127
                                      String user, String[]groups,
1128
                                      DBConnection dbconn, boolean useXMLIndex )
1129
                                      throws Exception
1130
    {
1131
      PreparedStatement pstmt = null;
1132
      ResultSet rs = null;
1133
      String docid = null;
1134
      String fieldname = null;
1135
      String fieldtype = null;
1136
      String fielddata = null;
1137
      String relation = null;
1138

    
1139
      if (qspec.containsExtendedSQL())
1140
      {
1141
        qspec.setUserName(user);
1142
        qspec.setGroup(groups);
1143
        Vector extendedFields = new Vector(qspec.getReturnFieldList());
1144
        Vector results = new Vector();
1145
        Iterator keylist = docListResult.getDocids();
1146
        StringBuffer doclist = new StringBuffer();
1147
        Vector parentidList = new Vector();
1148
        Hashtable returnFieldValue = new Hashtable();
1149
        while (keylist.hasNext())
1150
        {
1151
          doclist.append("'");
1152
          doclist.append((String) keylist.next());
1153
          doclist.append("',");
1154
        }
1155
        if (doclist.length() > 0)
1156
        {
1157
          Hashtable controlPairs = new Hashtable();
1158
          doclist.deleteCharAt(doclist.length() - 1); //remove the last comma
1159
          boolean tableHasRows = false;
1160
        
1161

    
1162
           String extendedQuery =
1163
               qspec.printExtendedSQL(doclist.toString(), useXMLIndex);
1164
           logMetacat.info("Extended query: " + extendedQuery);
1165

    
1166
           if(extendedQuery != null){
1167
        	   double extendedQueryStart = System.currentTimeMillis() / 1000;
1168
               pstmt = dbconn.prepareStatement(extendedQuery);
1169
               //increase dbconnection usage count
1170
               dbconn.increaseUsageCount(1);
1171
               pstmt.execute();
1172
               rs = pstmt.getResultSet();
1173
               double extendedQueryEnd = System.currentTimeMillis() / 1000;
1174
               logMetacat.warn(
1175
                   "Time to execute extended query: "
1176
                   + (extendedQueryEnd - extendedQueryStart));
1177
               MetacatUtil.writeDebugToFile(
1178
                       "Execute extended query "
1179
                       + (extendedQueryEnd - extendedQueryStart));
1180
               MetacatUtil.writeDebugToDelimiteredFile(" "+ (extendedQueryEnd - extendedQueryStart), false);
1181
               tableHasRows = rs.next();
1182
               while (tableHasRows) {
1183
                   ReturnFieldValue returnValue = new ReturnFieldValue();
1184
                   docid = rs.getString(1).trim();
1185
                   fieldname = rs.getString(2);
1186
                   fielddata = rs.getString(3);
1187
                   fielddata = MetacatUtil.normalize(fielddata);
1188
                   String parentId = rs.getString(4);
1189
                   fieldtype = rs.getString(5);
1190
                   StringBuffer value = new StringBuffer();
1191

    
1192
                   //handle case when usexmlindex is true differently
1193
                   //at one point merging the nodedata (for large text elements) was 
1194
                   //deemed unnecessary - but now it is needed.  but not for attribute nodes
1195
                   if (useXMLIndex || !containsKey(parentidList, parentId)) {
1196
                	   //merge node data only for non-ATTRIBUTEs
1197
                	   if (fieldtype != null && !fieldtype.equals("ATTRIBUTE")) {
1198
	                	   //try merging the data
1199
	                	   ReturnFieldValue existingRFV =
1200
	                		   getArrayValue(parentidList, parentId);
1201
	                	   if (existingRFV != null) {
1202
	                		   fielddata = existingRFV.getFieldValue() + fielddata;
1203
	                	   }
1204
                	   }
1205
                       value.append("<param name=\"");
1206
                       value.append(fieldname);
1207
                       value.append("\">");
1208
                       value.append(fielddata);
1209
                       value.append("</param>");
1210
                       //set returnvalue
1211
                       returnValue.setDocid(docid);
1212
                       returnValue.setFieldValue(fielddata);
1213
                       returnValue.setFieldType(fieldtype);
1214
                       returnValue.setXMLFieldValue(value.toString());
1215
                       // Store it in hastable
1216
                       putInArray(parentidList, parentId, returnValue);
1217
                   }
1218
                   else {
1219
                       // need to merge nodedata if they have same parent id and
1220
                       // node type is text
1221
                       fielddata = (String) ( (ReturnFieldValue)
1222
                                             getArrayValue(
1223
                           parentidList, parentId)).getFieldValue()
1224
                           + fielddata;
1225
                       value.append("<param name=\"");
1226
                       value.append(fieldname);
1227
                       value.append("\">");
1228
                       value.append(fielddata);
1229
                       value.append("</param>");
1230
                       returnValue.setDocid(docid);
1231
                       returnValue.setFieldValue(fielddata);
1232
                       returnValue.setFieldType(fieldtype);
1233
                       returnValue.setXMLFieldValue(value.toString());
1234
                       // remove the old return value from paretnidList
1235
                       parentidList.remove(parentId);
1236
                       // store the new return value in parentidlit
1237
                       putInArray(parentidList, parentId, returnValue);
1238
                   }
1239
                   tableHasRows = rs.next();
1240
               } //while
1241
               rs.close();
1242
               pstmt.close();
1243

    
1244
               // put the merger node data info into doclistReult
1245
               Enumeration xmlFieldValue = (getElements(parentidList)).
1246
                   elements();
1247
               while (xmlFieldValue.hasMoreElements()) {
1248
                   ReturnFieldValue object =
1249
                       (ReturnFieldValue) xmlFieldValue.nextElement();
1250
                   docid = object.getDocid();
1251
                   if (docListResult.containsDocid(docid)) {
1252
                       String removedelement = (String) docListResult.
1253
                           remove(docid);
1254
                       docListResult.
1255
                           addResultDocument(new ResultDocument(docid,
1256
                               removedelement + object.getXMLFieldValue()));
1257
                   }
1258
                   else {
1259
                       docListResult.addResultDocument(
1260
                         new ResultDocument(docid, object.getXMLFieldValue()));
1261
                   }
1262
               } //while
1263
               double docListResultEnd = System.currentTimeMillis() / 1000;
1264
               logMetacat.warn(
1265
                   "Time to prepare ResultDocumentSet after"
1266
                   + " execute extended query: "
1267
                   + (docListResultEnd - extendedQueryEnd));
1268
           }
1269

    
1270
         
1271
           
1272
           
1273
       }//if doclist lenght is great than zero
1274

    
1275
     }//if has extended query
1276

    
1277
      return docListResult;
1278
    }//addReturnfield
1279

    
1280
  
1281
  /**
1282
   * removes the <?xml version="1.0"?> tag from the beginning.  This takes a
1283
   * string as a param instead of a hashtable.
1284
   *
1285
   * @param xmlquery a string representing a query.
1286
   */
1287
   private  String transformQuery(String xmlquery)
1288
   {
1289
     xmlquery = xmlquery.trim();
1290
     int index = xmlquery.indexOf("?>");
1291
     if (index != -1)
1292
     {
1293
       return xmlquery.substring(index + 2, xmlquery.length());
1294
     }
1295
     else
1296
     {
1297
       return xmlquery;
1298
     }
1299
   }
1300
   
1301
   /*
1302
    * Method to store query string and result xml string into query result
1303
    * cache. If the size alreay reache the limitation, the cache will be
1304
    * cleared first, then store them.
1305
    */
1306
   private void storeQueryResultIntoCache(String query, String resultXML)
1307
   {
1308
	   synchronized (queryResultCache)
1309
	   {
1310
		   if (queryResultCache.size() >= QUERYRESULTCACHESIZE)
1311
		   {
1312
			   queryResultCache.clear();
1313
		   }
1314
		   queryResultCache.put(query, resultXML);
1315
		   
1316
	   }
1317
   }
1318
   
1319
   /*
1320
    * Method to get result xml string from query result cache. 
1321
    * Note: the returned string can be null.
1322
    */
1323
   private String getResultXMLFromCache(String query)
1324
   {
1325
	   String resultSet = null;
1326
	   synchronized (queryResultCache)
1327
	   {
1328
          try
1329
          {
1330
        	 logMetacat.info("Get query from cache ===");
1331
		     resultSet = (String)queryResultCache.get(query);
1332
		   
1333
          }
1334
          catch (Exception e)
1335
          {
1336
        	  resultSet = null;
1337
          }
1338
		   
1339
	   }
1340
	   return resultSet;
1341
   }
1342
   
1343
   /**
1344
    * Method to clear the query result cache.
1345
    */
1346
   public static void clearQueryResultCache()
1347
   {
1348
	   synchronized (queryResultCache)
1349
	   {
1350
		   queryResultCache.clear();
1351
	   }
1352
   }
1353

    
1354

    
1355
    /*
1356
     * A method to search if Vector contains a particular key string
1357
     */
1358
    private boolean containsKey(Vector parentidList, String parentId)
1359
    {
1360

    
1361
        Vector tempVector = null;
1362

    
1363
        for (int count = 0; count < parentidList.size(); count++) {
1364
            tempVector = (Vector) parentidList.get(count);
1365
            if (parentId.compareTo((String) tempVector.get(0)) == 0) { return true; }
1366
        }
1367
        return false;
1368
    }
1369
    
1370
    /*
1371
     * A method to put key and value in Vector
1372
     */
1373
    private void putInArray(Vector parentidList, String key,
1374
            ReturnFieldValue value)
1375
    {
1376

    
1377
        Vector tempVector = null;
1378
        //only filter if the field type is NOT an attribute (say, for text)
1379
        String fieldType = value.getFieldType();
1380
        if (fieldType != null && !fieldType.equals("ATTRIBUTE")) {
1381
        
1382
	        for (int count = 0; count < parentidList.size(); count++) {
1383
	            tempVector = (Vector) parentidList.get(count);
1384
	
1385
	            if (key.compareTo((String) tempVector.get(0)) == 0) {
1386
	                tempVector.remove(1);
1387
	                tempVector.add(1, value);
1388
	                return;
1389
	            }
1390
	        }
1391
        }
1392

    
1393
        tempVector = new Vector();
1394
        tempVector.add(0, key);
1395
        tempVector.add(1, value);
1396
        parentidList.add(tempVector);
1397
        return;
1398
    }
1399

    
1400
    /*
1401
     * A method to get value in Vector given a key
1402
     */
1403
    private ReturnFieldValue getArrayValue(Vector parentidList, String key)
1404
    {
1405

    
1406
        Vector tempVector = null;
1407

    
1408
        for (int count = 0; count < parentidList.size(); count++) {
1409
            tempVector = (Vector) parentidList.get(count);
1410

    
1411
            if (key.compareTo((String) tempVector.get(0)) == 0) { return (ReturnFieldValue) tempVector
1412
                    .get(1); }
1413
        }
1414
        return null;
1415
    }
1416

    
1417
    /*
1418
     * A method to get enumeration of all values in Vector
1419
     */
1420
    private Vector getElements(Vector parentidList)
1421
    {
1422
        Vector enumVector = new Vector();
1423
        Vector tempVector = null;
1424

    
1425
        for (int count = 0; count < parentidList.size(); count++) {
1426
            tempVector = (Vector) parentidList.get(count);
1427

    
1428
            enumVector.add(tempVector.get(1));
1429
        }
1430
        return enumVector;
1431
    }
1432

    
1433
  
1434

    
1435
    /*
1436
     * A method to create a query to get owner's docid list
1437
     */
1438
    private String getOwnerQuery(String owner)
1439
    {
1440
        if (owner != null) {
1441
            owner = owner.toLowerCase();
1442
        }
1443
        StringBuffer self = new StringBuffer();
1444

    
1445
        self.append("SELECT docid,docname,doctype,");
1446
        self.append("date_created, date_updated, rev ");
1447
        self.append("FROM xml_documents WHERE docid IN (");
1448
        self.append("(");
1449
        self.append("SELECT DISTINCT docid FROM xml_nodes WHERE \n");
1450
        self.append("nodedata LIKE '%%%' ");
1451
        self.append(") \n");
1452
        self.append(") ");
1453
        self.append(" AND (");
1454
        self.append(" lower(user_owner) = '" + owner + "'");
1455
        self.append(") ");
1456
        return self.toString();
1457
    }
1458

    
1459
    /**
1460
     * format a structured query as an XML document that conforms to the
1461
     * pathquery.dtd and is appropriate for submission to the DBQuery
1462
     * structured query engine
1463
     *
1464
     * @param params The list of parameters that should be included in the
1465
     *            query
1466
     */
1467
    public static String createSQuery(Hashtable params) throws PropertyNotFoundException
1468
    {
1469
        StringBuffer query = new StringBuffer();
1470
        Enumeration elements;
1471
        Enumeration keys;
1472
        String filterDoctype = null;
1473
        String casesensitive = null;
1474
        String searchmode = null;
1475
        Object nextkey;
1476
        Object nextelement;
1477
        //add the xml headers
1478
        query.append("<?xml version=\"1.0\"?>\n");
1479
        query.append("<pathquery version=\"1.2\">\n");
1480

    
1481

    
1482

    
1483
        if (params.containsKey("meta_file_id")) {
1484
            query.append("<meta_file_id>");
1485
            query.append(((String[]) params.get("meta_file_id"))[0]);
1486
            query.append("</meta_file_id>");
1487
        }
1488

    
1489
        if (params.containsKey("returndoctype")) {
1490
            String[] returnDoctypes = ((String[]) params.get("returndoctype"));
1491
            for (int i = 0; i < returnDoctypes.length; i++) {
1492
                String doctype = (String) returnDoctypes[i];
1493

    
1494
                if (!doctype.equals("any") && !doctype.equals("ANY")
1495
                        && !doctype.equals("")) {
1496
                    query.append("<returndoctype>").append(doctype);
1497
                    query.append("</returndoctype>");
1498
                }
1499
            }
1500
        }
1501

    
1502
        if (params.containsKey("filterdoctype")) {
1503
            String[] filterDoctypes = ((String[]) params.get("filterdoctype"));
1504
            for (int i = 0; i < filterDoctypes.length; i++) {
1505
                query.append("<filterdoctype>").append(filterDoctypes[i]);
1506
                query.append("</filterdoctype>");
1507
            }
1508
        }
1509

    
1510
        if (params.containsKey("returnfield")) {
1511
            String[] returnfield = ((String[]) params.get("returnfield"));
1512
            for (int i = 0; i < returnfield.length; i++) {
1513
                query.append("<returnfield>").append(returnfield[i]);
1514
                query.append("</returnfield>");
1515
            }
1516
        }
1517

    
1518
        if (params.containsKey("owner")) {
1519
            String[] owner = ((String[]) params.get("owner"));
1520
            for (int i = 0; i < owner.length; i++) {
1521
                query.append("<owner>").append(owner[i]);
1522
                query.append("</owner>");
1523
            }
1524
        }
1525

    
1526
        if (params.containsKey("site")) {
1527
            String[] site = ((String[]) params.get("site"));
1528
            for (int i = 0; i < site.length; i++) {
1529
                query.append("<site>").append(site[i]);
1530
                query.append("</site>");
1531
            }
1532
        }
1533

    
1534
        //allows the dynamic switching of boolean operators
1535
        if (params.containsKey("operator")) {
1536
            query.append("<querygroup operator=\""
1537
                    + ((String[]) params.get("operator"))[0] + "\">");
1538
        } else { //the default operator is UNION
1539
            query.append("<querygroup operator=\"UNION\">");
1540
        }
1541

    
1542
        if (params.containsKey("casesensitive")) {
1543
            casesensitive = ((String[]) params.get("casesensitive"))[0];
1544
        } else {
1545
            casesensitive = "false";
1546
        }
1547

    
1548
        if (params.containsKey("searchmode")) {
1549
            searchmode = ((String[]) params.get("searchmode"))[0];
1550
        } else {
1551
            searchmode = "contains";
1552
        }
1553

    
1554
        //anyfield is a special case because it does a
1555
        //free text search. It does not have a <pathexpr>
1556
        //tag. This allows for a free text search within the structured
1557
        //query. This is useful if the INTERSECT operator is used.
1558
        if (params.containsKey("anyfield")) {
1559
            String[] anyfield = ((String[]) params.get("anyfield"));
1560
            //allow for more than one value for anyfield
1561
            for (int i = 0; i < anyfield.length; i++) {
1562
                if (anyfield[i] != null && !anyfield[i].equals("")) {
1563
                    query.append("<queryterm casesensitive=\"" + casesensitive
1564
                            + "\" " + "searchmode=\"" + searchmode
1565
                            + "\"><value>" + anyfield[i]
1566
                            + "</value></queryterm>");
1567
                }
1568
            }
1569
        }
1570

    
1571
        //this while loop finds the rest of the parameters
1572
        //and attempts to query for the field specified
1573
        //by the parameter.
1574
        elements = params.elements();
1575
        keys = params.keys();
1576
        while (keys.hasMoreElements() && elements.hasMoreElements()) {
1577
            nextkey = keys.nextElement();
1578
            nextelement = elements.nextElement();
1579

    
1580
            //make sure we aren't querying for any of these
1581
            //parameters since the are already in the query
1582
            //in one form or another.
1583
            Vector ignoredParams = new Vector();
1584
            ignoredParams.add("returndoctype");
1585
            ignoredParams.add("filterdoctype");
1586
            ignoredParams.add("action");
1587
            ignoredParams.add("qformat");
1588
            ignoredParams.add("anyfield");
1589
            ignoredParams.add("returnfield");
1590
            ignoredParams.add("owner");
1591
            ignoredParams.add("site");
1592
            ignoredParams.add("operator");
1593
            ignoredParams.add("sessionid");
1594
            ignoredParams.add("pagesize");
1595
            ignoredParams.add("pagestart");
1596
            ignoredParams.add("searchmode");
1597

    
1598
            // Also ignore parameters listed in the properties file
1599
            // so that they can be passed through to stylesheets
1600
            String paramsToIgnore = PropertyService
1601
                    .getProperty("database.queryignoredparams");
1602
            StringTokenizer st = new StringTokenizer(paramsToIgnore, ",");
1603
            while (st.hasMoreTokens()) {
1604
                ignoredParams.add(st.nextToken());
1605
            }
1606
            if (!ignoredParams.contains(nextkey.toString())) {
1607
                //allow for more than value per field name
1608
                for (int i = 0; i < ((String[]) nextelement).length; i++) {
1609
                    if (!((String[]) nextelement)[i].equals("")) {
1610
                        query.append("<queryterm casesensitive=\""
1611
                                + casesensitive + "\" " + "searchmode=\""
1612
                                + searchmode + "\">" + "<value>" +
1613
                                //add the query value
1614
                                ((String[]) nextelement)[i]
1615
                                + "</value><pathexpr>" +
1616
                                //add the path to query by
1617
                                nextkey.toString() + "</pathexpr></queryterm>");
1618
                    }
1619
                }
1620
            }
1621
        }
1622
        query.append("</querygroup></pathquery>");
1623
        //append on the end of the xml and return the result as a string
1624
        return query.toString();
1625
    }
1626

    
1627
    /**
1628
     * format a simple free-text value query as an XML document that conforms
1629
     * to the pathquery.dtd and is appropriate for submission to the DBQuery
1630
     * structured query engine
1631
     *
1632
     * @param value the text string to search for in the xml catalog
1633
     * @param doctype the type of documents to include in the result set -- use
1634
     *            "any" or "ANY" for unfiltered result sets
1635
     */
1636
    public static String createQuery(String value, String doctype)
1637
    {
1638
        StringBuffer xmlquery = new StringBuffer();
1639
        xmlquery.append("<?xml version=\"1.0\"?>\n");
1640
        xmlquery.append("<pathquery version=\"1.0\">");
1641

    
1642
        if (!doctype.equals("any") && !doctype.equals("ANY")) {
1643
            xmlquery.append("<returndoctype>");
1644
            xmlquery.append(doctype).append("</returndoctype>");
1645
        }
1646

    
1647
        xmlquery.append("<querygroup operator=\"UNION\">");
1648
        //chad added - 8/14
1649
        //the if statement allows a query to gracefully handle a null
1650
        //query. Without this if a nullpointerException is thrown.
1651
        if (!value.equals("")) {
1652
            xmlquery.append("<queryterm casesensitive=\"false\" ");
1653
            xmlquery.append("searchmode=\"contains\">");
1654
            xmlquery.append("<value>").append(value).append("</value>");
1655
            xmlquery.append("</queryterm>");
1656
        }
1657
        xmlquery.append("</querygroup>");
1658
        xmlquery.append("</pathquery>");
1659

    
1660
        return (xmlquery.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
     */
1670
    public static String createQuery(String value)
1671
    {
1672
        return createQuery(value, "any");
1673
    }
1674

    
1675
    /**
1676
     * Check for "READ" permission on @docid for @user and/or @group from DB
1677
     * connection
1678
     */
1679
    private boolean hasPermission(String user, String[] groups, String docid)
1680
            throws SQLException, Exception
1681
    {
1682
        // Check for READ permission on @docid for @user and/or @groups
1683
        PermissionController controller = new PermissionController(docid);
1684
        return controller.hasPermission(user, groups,
1685
                AccessControlInterface.READSTRING);
1686
    }
1687

    
1688
    /**
1689
     * Get all docIds list for a data packadge
1690
     *
1691
     * @param dataPackageDocid, the string in docId field of xml_relation table
1692
     */
1693
    private Vector getCurrentDocidListForDataPackage(String dataPackageDocid)
1694
    {
1695
        DBConnection dbConn = null;
1696
        int serialNumber = -1;
1697
        Vector docIdList = new Vector();//return value
1698
        PreparedStatement pStmt = null;
1699
        ResultSet rs = null;
1700
        String docIdInSubjectField = null;
1701
        String docIdInObjectField = null;
1702

    
1703
        // Check the parameter
1704
        if (dataPackageDocid == null || dataPackageDocid.equals("")) { return docIdList; }//if
1705

    
1706
        //the query stirng
1707
        String query = "SELECT subject, object from xml_relation where docId = ?";
1708
        try {
1709
            dbConn = DBConnectionPool
1710
                    .getDBConnection("DBQuery.getCurrentDocidListForDataPackage");
1711
            serialNumber = dbConn.getCheckOutSerialNumber();
1712
            pStmt = dbConn.prepareStatement(query);
1713
            //bind the value to query
1714
            pStmt.setString(1, dataPackageDocid);
1715

    
1716
            //excute the query
1717
            pStmt.execute();
1718
            //get the result set
1719
            rs = pStmt.getResultSet();
1720
            //process the result
1721
            while (rs.next()) {
1722
                //In order to get the whole docIds in a data packadge,
1723
                //we need to put the docIds of subject and object field in
1724
                // xml_relation
1725
                //into the return vector
1726
                docIdInSubjectField = rs.getString(1);//the result docId in
1727
                                                      // subject field
1728
                docIdInObjectField = rs.getString(2);//the result docId in
1729
                                                     // object field
1730

    
1731
                //don't put the duplicate docId into the vector
1732
                if (!docIdList.contains(docIdInSubjectField)) {
1733
                    docIdList.add(docIdInSubjectField);
1734
                }
1735

    
1736
                //don't put the duplicate docId into the vector
1737
                if (!docIdList.contains(docIdInObjectField)) {
1738
                    docIdList.add(docIdInObjectField);
1739
                }
1740
            }//while
1741
            //close the pStmt
1742
            pStmt.close();
1743
        }//try
1744
        catch (SQLException e) {
1745
            logMetacat.error("Error in getDocidListForDataPackage: "
1746
                    + e.getMessage());
1747
        }//catch
1748
        finally {
1749
            try {
1750
                pStmt.close();
1751
            }//try
1752
            catch (SQLException ee) {
1753
                logMetacat.error(
1754
                        "Error in getDocidListForDataPackage: "
1755
                                + ee.getMessage());
1756
            }//catch
1757
            finally {
1758
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1759
            }//fianlly
1760
        }//finally
1761
        return docIdList;
1762
    }//getCurrentDocidListForDataPackadge()
1763

    
1764
    /**
1765
     * Get all docIds list for a data packadge
1766
     *
1767
     * @param dataPackageDocid, the string in docId field of xml_relation table
1768
     */
1769
    private Vector getOldVersionDocidListForDataPackage(String dataPackageDocidWithRev)
1770
    {
1771

    
1772
        Vector docIdList = new Vector();//return value
1773
        Vector tripleList = null;
1774
        String xml = null;
1775

    
1776
        // Check the parameter
1777
        if (dataPackageDocidWithRev == null || dataPackageDocidWithRev.equals("")) { return docIdList; }//if
1778

    
1779
        try {
1780
            //initial a documentImpl object
1781
            DocumentImpl packageDocument = new DocumentImpl(dataPackageDocidWithRev);
1782
            //transfer to documentImpl object to string
1783
            xml = packageDocument.toString();
1784

    
1785
            //create a tripcollection object
1786
            TripleCollection tripleForPackage = new TripleCollection(
1787
                    new StringReader(xml));
1788
            //get the vetor of triples
1789
            tripleList = tripleForPackage.getCollection();
1790

    
1791
            for (int i = 0; i < tripleList.size(); i++) {
1792
                //put subject docid into docIdlist without duplicate
1793
                if (!docIdList.contains(((Triple) tripleList.elementAt(i))
1794
                        .getSubject())) {
1795
                    //put subject docid into docIdlist
1796
                    docIdList.add(((Triple) tripleList.get(i)).getSubject());
1797
                }
1798
                //put object docid into docIdlist without duplicate
1799
                if (!docIdList.contains(((Triple) tripleList.elementAt(i))
1800
                        .getObject())) {
1801
                    docIdList.add(((Triple) (tripleList.get(i))).getObject());
1802
                }
1803
            }//for
1804
        }//try
1805
        catch (Exception e) {
1806
            logMetacat.error("Error in getOldVersionAllDocumentImpl: "
1807
                    + e.getMessage());
1808
        }//catch
1809

    
1810
        // return result
1811
        return docIdList;
1812
    }//getDocidListForPackageInXMLRevisions()
1813

    
1814
    /**
1815
     * Check if the docId is a data packadge id. If the id is a data packadage
1816
     * id, it should be store in the docId fields in xml_relation table. So we
1817
     * can use a query to get the entries which the docId equals the given
1818
     * value. If the result is null. The docId is not a packadge id. Otherwise,
1819
     * it is.
1820
     *
1821
     * @param docId, the id need to be checked
1822
     */
1823
    private boolean isDataPackageId(String docId)
1824
    {
1825
        boolean result = false;
1826
        PreparedStatement pStmt = null;
1827
        ResultSet rs = null;
1828
        String query = "SELECT docId from xml_relation where docId = ?";
1829
        DBConnection dbConn = null;
1830
        int serialNumber = -1;
1831
        try {
1832
            dbConn = DBConnectionPool
1833
                    .getDBConnection("DBQuery.isDataPackageId");
1834
            serialNumber = dbConn.getCheckOutSerialNumber();
1835
            pStmt = dbConn.prepareStatement(query);
1836
            //bind the value to query
1837
            pStmt.setString(1, docId);
1838
            //execute the query
1839
            pStmt.execute();
1840
            rs = pStmt.getResultSet();
1841
            //process the result
1842
            if (rs.next()) //There are some records for the id in docId fields
1843
            {
1844
                result = true;//It is a data packadge id
1845
            }
1846
            pStmt.close();
1847
        }//try
1848
        catch (SQLException e) {
1849
            logMetacat.error("Error in isDataPackageId: "
1850
                    + e.getMessage());
1851
        } finally {
1852
            try {
1853
                pStmt.close();
1854
            }//try
1855
            catch (SQLException ee) {
1856
                logMetacat.error("Error in isDataPackageId: "
1857
                        + ee.getMessage());
1858
            }//catch
1859
            finally {
1860
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1861
            }//finally
1862
        }//finally
1863
        return result;
1864
    }//isDataPackageId()
1865

    
1866
    /**
1867
     * Check if the user has the permission to export data package
1868
     *
1869
     * @param conn, the connection
1870
     * @param docId, the id need to be checked
1871
     * @param user, the name of user
1872
     * @param groups, the user's group
1873
     */
1874
    private boolean hasPermissionToExportPackage(String docId, String user,
1875
            String[] groups) throws Exception
1876
    {
1877
        //DocumentImpl doc=new DocumentImpl(conn,docId);
1878
        return DocumentImpl.hasReadPermission(user, groups, docId);
1879
    }
1880

    
1881
    /**
1882
     * Get the current Rev for a docid in xml_documents table
1883
     *
1884
     * @param docId, the id need to get version numb If the return value is -5,
1885
     *            means no value in rev field for this docid
1886
     */
1887
    private int getCurrentRevFromXMLDoumentsTable(String docId)
1888
            throws SQLException
1889
    {
1890
        int rev = -5;
1891
        PreparedStatement pStmt = null;
1892
        ResultSet rs = null;
1893
        String query = "SELECT rev from xml_documents where docId = ?";
1894
        DBConnection dbConn = null;
1895
        int serialNumber = -1;
1896
        try {
1897
            dbConn = DBConnectionPool
1898
                    .getDBConnection("DBQuery.getCurrentRevFromXMLDocumentsTable");
1899
            serialNumber = dbConn.getCheckOutSerialNumber();
1900
            pStmt = dbConn.prepareStatement(query);
1901
            //bind the value to query
1902
            pStmt.setString(1, docId);
1903
            //execute the query
1904
            pStmt.execute();
1905
            rs = pStmt.getResultSet();
1906
            //process the result
1907
            if (rs.next()) //There are some records for rev
1908
            {
1909
                rev = rs.getInt(1);
1910
                ;//It is the version for given docid
1911
            } else {
1912
                rev = -5;
1913
            }
1914

    
1915
        }//try
1916
        catch (SQLException e) {
1917
            logMetacat.error(
1918
                    "Error in getCurrentRevFromXMLDoumentsTable: "
1919
                            + e.getMessage());
1920
            throw e;
1921
        }//catch
1922
        finally {
1923
            try {
1924
                pStmt.close();
1925
            }//try
1926
            catch (SQLException ee) {
1927
                logMetacat.error(
1928
                        "Error in getCurrentRevFromXMLDoumentsTable: "
1929
                                + ee.getMessage());
1930
            }//catch
1931
            finally {
1932
                DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1933
            }//finally
1934
        }//finally
1935
        return rev;
1936
    }//getCurrentRevFromXMLDoumentsTable
1937

    
1938
    /**
1939
     * put a doc into a zip output stream
1940
     *
1941
     * @param docImpl, docmentImpl object which will be sent to zip output
1942
     *            stream
1943
     * @param zipOut, zip output stream which the docImpl will be put
1944
     * @param packageZipEntry, the zip entry name for whole package
1945
     */
1946
    private void addDocToZipOutputStream(DocumentImpl docImpl,
1947
            ZipOutputStream zipOut, String packageZipEntry)
1948
            throws ClassNotFoundException, IOException, SQLException,
1949
            McdbException, Exception
1950
    {
1951
        byte[] byteString = null;
1952
        ZipEntry zEntry = null;
1953

    
1954
        byteString = docImpl.toString().getBytes();
1955
        //use docId as the zip entry's name
1956
        zEntry = new ZipEntry(packageZipEntry + "/metadata/"
1957
                + docImpl.getDocID());
1958
        zEntry.setSize(byteString.length);
1959
        zipOut.putNextEntry(zEntry);
1960
        zipOut.write(byteString, 0, byteString.length);
1961
        zipOut.closeEntry();
1962

    
1963
    }//addDocToZipOutputStream()
1964

    
1965
    /**
1966
     * Transfer a docid vetor to a documentImpl vector. The documentImpl vetor
1967
     * only inlcudes current version. If a DocumentImple object couldn't find
1968
     * for a docid, then the String of this docid was added to vetor rather
1969
     * than DocumentImple object.
1970
     *
1971
     * @param docIdList, a vetor hold a docid list for a data package. In
1972
     *            docid, there is not version number in it.
1973
     */
1974

    
1975
    private Vector getCurrentAllDocumentImpl(Vector docIdList)
1976
            throws McdbException, Exception
1977
    {
1978
        //Connection dbConn=null;
1979
        Vector documentImplList = new Vector();
1980
        int rev = 0;
1981

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

    
1985
        //for every docid in vector
1986
        for (int i = 0; i < docIdList.size(); i++) {
1987
            try {
1988
                //get newest version for this docId
1989
                rev = getCurrentRevFromXMLDoumentsTable((String) docIdList
1990
                        .elementAt(i));
1991

    
1992
                // There is no record for this docId in xml_documents table
1993
                if (rev == -5) {
1994
                    // Rather than put DocumentImple object, put a String
1995
                    // Object(docid)
1996
                    // into the documentImplList
1997
                    documentImplList.add((String) docIdList.elementAt(i));
1998
                    // Skip other code
1999
                    continue;
2000
                }
2001

    
2002
                String docidPlusVersion = ((String) docIdList.elementAt(i))
2003
                        + PropertyService.getProperty("document.accNumSeparator") + rev;
2004

    
2005
                //create new documentImpl object
2006
                DocumentImpl documentImplObject = new DocumentImpl(
2007
                        docidPlusVersion);
2008
                //add them to vector
2009
                documentImplList.add(documentImplObject);
2010
            }//try
2011
            catch (Exception e) {
2012
                logMetacat.error("Error in getCurrentAllDocumentImpl: "
2013
                        + e.getMessage());
2014
                // continue the for loop
2015
                continue;
2016
            }
2017
        }//for
2018
        return documentImplList;
2019
    }
2020

    
2021
    /**
2022
     * Transfer a docid vetor to a documentImpl vector. If a DocumentImple
2023
     * object couldn't find for a docid, then the String of this docid was
2024
     * added to vetor rather than DocumentImple object.
2025
     *
2026
     * @param docIdList, a vetor hold a docid list for a data package. In
2027
     *            docid, t here is version number in it.
2028
     */
2029
    private Vector getOldVersionAllDocumentImpl(Vector docIdList)
2030
    {
2031
        //Connection dbConn=null;
2032
        Vector documentImplList = new Vector();
2033
        String siteCode = null;
2034
        String uniqueId = null;
2035
        int rev = 0;
2036

    
2037
        // Check the parameter
2038
        if (docIdList.isEmpty()) { return documentImplList; }//if
2039

    
2040
        //for every docid in vector
2041
        for (int i = 0; i < docIdList.size(); i++) {
2042

    
2043
            String docidPlusVersion = (String) (docIdList.elementAt(i));
2044

    
2045
            try {
2046
                //create new documentImpl object
2047
                DocumentImpl documentImplObject = new DocumentImpl(
2048
                        docidPlusVersion);
2049
                //add them to vector
2050
                documentImplList.add(documentImplObject);
2051
            }//try
2052
            catch (McdbDocNotFoundException notFoundE) {
2053
                logMetacat.error(
2054
                        "Error in DBQuery.getOldVersionAllDocument" + "Imple"
2055
                                + notFoundE.getMessage());
2056
                // Rather than add a DocumentImple object into vetor, a String
2057
                // object
2058
                // - the doicd was added to the vector
2059
                documentImplList.add(docidPlusVersion);
2060
                // Continue the for loop
2061
                continue;
2062
            }//catch
2063
            catch (Exception e) {
2064
                logMetacat.error(
2065
                        "Error in DBQuery.getOldVersionAllDocument" + "Imple"
2066
                                + e.getMessage());
2067
                // Continue the for loop
2068
                continue;
2069
            }//catch
2070

    
2071
        }//for
2072
        return documentImplList;
2073
    }//getOldVersionAllDocumentImple
2074

    
2075
    /**
2076
     * put a data file into a zip output stream
2077
     *
2078
     * @param docImpl, docmentImpl object which will be sent to zip output
2079
     *            stream
2080
     * @param zipOut, the zip output stream which the docImpl will be put
2081
     * @param packageZipEntry, the zip entry name for whole package
2082
     */
2083
    private void addDataFileToZipOutputStream(DocumentImpl docImpl,
2084
            ZipOutputStream zipOut, String packageZipEntry)
2085
            throws ClassNotFoundException, IOException, SQLException,
2086
            McdbException, Exception
2087
    {
2088
        byte[] byteString = null;
2089
        ZipEntry zEntry = null;
2090
        // this is data file; add file to zip
2091
        String filePath = PropertyService.getProperty("application.datafilepath");
2092
        if (!filePath.endsWith("/")) {
2093
            filePath += "/";
2094
        }
2095
        String fileName = filePath + docImpl.getDocID();
2096
        zEntry = new ZipEntry(packageZipEntry + "/data/" + docImpl.getDocID());
2097
        zipOut.putNextEntry(zEntry);
2098
        FileInputStream fin = null;
2099
        try {
2100
            fin = new FileInputStream(fileName);
2101
            byte[] buf = new byte[4 * 1024]; // 4K buffer
2102
            int b = fin.read(buf);
2103
            while (b != -1) {
2104
                zipOut.write(buf, 0, b);
2105
                b = fin.read(buf);
2106
            }//while
2107
            zipOut.closeEntry();
2108
        }//try
2109
        catch (IOException ioe) {
2110
            logMetacat.error("There is an exception: "
2111
                    + ioe.getMessage());
2112
        }//catch
2113
    }//addDataFileToZipOutputStream()
2114

    
2115
    /**
2116
     * create a html summary for data package and put it into zip output stream
2117
     *
2118
     * @param docImplList, the documentImpl ojbects in data package
2119
     * @param zipOut, the zip output stream which the html should be put
2120
     * @param packageZipEntry, the zip entry name for whole package
2121
     */
2122
    private void addHtmlSummaryToZipOutputStream(Vector docImplList,
2123
            ZipOutputStream zipOut, String packageZipEntry) throws Exception
2124
    {
2125
        StringBuffer htmlDoc = new StringBuffer();
2126
        ZipEntry zEntry = null;
2127
        byte[] byteString = null;
2128
        InputStream source;
2129
        DBTransform xmlToHtml;
2130

    
2131
        //create a DBTransform ojbect
2132
        xmlToHtml = new DBTransform();
2133
        //head of html
2134
        htmlDoc.append("<html><head></head><body>");
2135
        for (int i = 0; i < docImplList.size(); i++) {
2136
            // If this String object, this means it is missed data file
2137
            if ((((docImplList.elementAt(i)).getClass()).toString())
2138
                    .equals("class java.lang.String")) {
2139

    
2140
                htmlDoc.append("<a href=\"");
2141
                String dataFileid = (String) docImplList.elementAt(i);
2142
                htmlDoc.append("./data/").append(dataFileid).append("\">");
2143
                htmlDoc.append("Data File: ");
2144
                htmlDoc.append(dataFileid).append("</a><br>");
2145
                htmlDoc.append("<br><hr><br>");
2146

    
2147
            }//if
2148
            else if ((((DocumentImpl) docImplList.elementAt(i)).getDoctype())
2149
                    .compareTo("BIN") != 0) { //this is an xml file so we can
2150
                                              // transform it.
2151
                //transform each file individually then concatenate all of the
2152
                //transformations together.
2153

    
2154
                //for metadata xml title
2155
                htmlDoc.append("<h2>");
2156
                htmlDoc.append(((DocumentImpl) docImplList.elementAt(i))
2157
                        .getDocID());
2158
                //htmlDoc.append(".");
2159
                //htmlDoc.append(((DocumentImpl)docImplList.elementAt(i)).getRev());
2160
                htmlDoc.append("</h2>");
2161
                //do the actual transform
2162
                StringWriter docString = new StringWriter();
2163
                xmlToHtml.transformXMLDocument(((DocumentImpl) docImplList
2164
                        .elementAt(i)).toString(), "-//NCEAS//eml-generic//EN",
2165
                        "-//W3C//HTML//EN", "html", docString, null, null);
2166
                htmlDoc.append(docString.toString());
2167
                htmlDoc.append("<br><br><hr><br><br>");
2168
            }//if
2169
            else { //this is a data file so we should link to it in the html
2170
                htmlDoc.append("<a href=\"");
2171
                String dataFileid = ((DocumentImpl) docImplList.elementAt(i))
2172
                        .getDocID();
2173
                htmlDoc.append("./data/").append(dataFileid).append("\">");
2174
                htmlDoc.append("Data File: ");
2175
                htmlDoc.append(dataFileid).append("</a><br>");
2176
                htmlDoc.append("<br><hr><br>");
2177
            }//else
2178
        }//for
2179
        htmlDoc.append("</body></html>");
2180
        byteString = htmlDoc.toString().getBytes();
2181
        zEntry = new ZipEntry(packageZipEntry + "/metadata.html");
2182
        zEntry.setSize(byteString.length);
2183
        zipOut.putNextEntry(zEntry);
2184
        zipOut.write(byteString, 0, byteString.length);
2185
        zipOut.closeEntry();
2186
        //dbConn.close();
2187

    
2188
    }//addHtmlSummaryToZipOutputStream
2189

    
2190
    /**
2191
     * put a data packadge into a zip output stream
2192
     *
2193
     * @param docId, which the user want to put into zip output stream,it has version
2194
     * @param out, a servletoutput stream which the zip output stream will be
2195
     *            put
2196
     * @param user, the username of the user
2197
     * @param groups, the group of the user
2198
     */
2199
    public ZipOutputStream getZippedPackage(String docIdString,
2200
            ServletOutputStream out, String user, String[] groups,
2201
            String passWord) throws ClassNotFoundException, IOException,
2202
            SQLException, McdbException, NumberFormatException, Exception
2203
    {
2204
        ZipOutputStream zOut = null;
2205
        String elementDocid = null;
2206
        DocumentImpl docImpls = null;
2207
        //Connection dbConn = null;
2208
        Vector docIdList = new Vector();
2209
        Vector documentImplList = new Vector();
2210
        Vector htmlDocumentImplList = new Vector();
2211
        String packageId = null;
2212
        String rootName = "package";//the package zip entry name
2213

    
2214
        String docId = null;
2215
        int version = -5;
2216
        // Docid without revision
2217
        docId = DocumentUtil.getDocIdFromString(docIdString);
2218
        // revision number
2219
        version = DocumentUtil.getVersionFromString(docIdString);
2220

    
2221
        //check if the reqused docId is a data package id
2222
        if (!isDataPackageId(docId)) {
2223

    
2224
            /*
2225
             * Exception e = new Exception("The request the doc id "
2226
             * +docIdString+ " is not a data package id");
2227
             */
2228

    
2229
            //CB 1/6/03: if the requested docid is not a datapackage, we just
2230
            // zip
2231
            //up the single document and return the zip file.
2232
            if (!hasPermissionToExportPackage(docId, user, groups)) {
2233

    
2234
                Exception e = new Exception("User " + user
2235
                        + " does not have permission"
2236
                        + " to export the data package " + docIdString);
2237
                throw e;
2238
            }
2239

    
2240
            docImpls = new DocumentImpl(docIdString);
2241
            //checking if the user has the permission to read the documents
2242
            if (DocumentImpl.hasReadPermission(user, groups, docImpls
2243
                    .getDocID())) {
2244
                zOut = new ZipOutputStream(out);
2245
                //if the docImpls is metadata
2246
                if ((docImpls.getDoctype()).compareTo("BIN") != 0) {
2247
                    //add metadata into zip output stream
2248
                    addDocToZipOutputStream(docImpls, zOut, rootName);
2249
                }//if
2250
                else {
2251
                    //it is data file
2252
                    addDataFileToZipOutputStream(docImpls, zOut, rootName);
2253
                    htmlDocumentImplList.add(docImpls);
2254
                }//else
2255
            }//if
2256

    
2257
            zOut.finish(); //terminate the zip file
2258
            return zOut;
2259
        }
2260
        // Check the permission of user
2261
        else if (!hasPermissionToExportPackage(docId, user, groups)) {
2262

    
2263
            Exception e = new Exception("User " + user
2264
                    + " does not have permission"
2265
                    + " to export the data package " + docIdString);
2266
            throw e;
2267
        } else //it is a packadge id
2268
        {
2269
            //store the package id
2270
            packageId = docId;
2271
            //get current version in database
2272
            int currentVersion = getCurrentRevFromXMLDoumentsTable(packageId);
2273
            //If it is for current version (-1 means user didn't specify
2274
            // revision)
2275
            if ((version == -1) || version == currentVersion) {
2276
                //get current version number
2277
                version = currentVersion;
2278
                //get package zip entry name
2279
                //it should be docId.revsion.package
2280
                rootName = packageId + PropertyService.getProperty("document.accNumSeparator")
2281
                        + version + PropertyService.getProperty("document.accNumSeparator")
2282
                        + "package";
2283
                //get the whole id list for data packadge
2284
                docIdList = getCurrentDocidListForDataPackage(packageId);
2285
                //get the whole documentImple object
2286
                documentImplList = getCurrentAllDocumentImpl(docIdList);
2287

    
2288
            }//if
2289
            else if (version > currentVersion || version < -1) {
2290
                throw new Exception("The user specified docid: " + docId + "."
2291
                        + version + " doesn't exist");
2292
            }//else if
2293
            else //for an old version
2294
            {
2295

    
2296
                rootName = docIdString
2297
                        + PropertyService.getProperty("document.accNumSeparator") + "package";
2298
                //get the whole id list for data packadge
2299
                docIdList = getOldVersionDocidListForDataPackage(docIdString);
2300

    
2301
                //get the whole documentImple object
2302
                documentImplList = getOldVersionAllDocumentImpl(docIdList);
2303
            }//else
2304

    
2305
            // Make sure documentImplist is not empty
2306
            if (documentImplList.isEmpty()) { throw new Exception(
2307
                    "Couldn't find component for data package: " + packageId); }//if
2308

    
2309
            zOut = new ZipOutputStream(out);
2310
            //put every element into zip output stream
2311
            for (int i = 0; i < documentImplList.size(); i++) {
2312
                // if the object in the vetor is String, this means we couldn't
2313
                // find
2314
                // the document locally, we need find it remote
2315
                if ((((documentImplList.elementAt(i)).getClass()).toString())
2316
                        .equals("class java.lang.String")) {
2317
                    // Get String object from vetor
2318
                    String documentId = (String) documentImplList.elementAt(i);
2319
                    logMetacat.info("docid: " + documentId);
2320
                    // Get doicd without revision
2321
                    String docidWithoutRevision = 
2322
                    	DocumentUtil.getDocIdFromString(documentId);
2323
                    logMetacat.info("docidWithoutRevsion: "
2324
                            + docidWithoutRevision);
2325
                    // Get revision
2326
                    String revision = 
2327
                    	DocumentUtil.getRevisionStringFromString(documentId);
2328
                    logMetacat.info("revsion from docIdentifier: "
2329
                            + revision);
2330
                    // Zip entry string
2331
                    String zipEntryPath = rootName + "/data/";
2332
                    // Create a RemoteDocument object
2333
                    RemoteDocument remoteDoc = new RemoteDocument(
2334
                            docidWithoutRevision, revision, user, passWord,
2335
                            zipEntryPath);
2336
                    // Here we only read data file from remote metacat
2337
                    String docType = remoteDoc.getDocType();
2338
                    if (docType != null) {
2339
                        if (docType.equals("BIN")) {
2340
                            // Put remote document to zip output
2341
                            remoteDoc.readDocumentFromRemoteServerByZip(zOut);
2342
                            // Add String object to htmlDocumentImplList
2343
                            String elementInHtmlList = remoteDoc
2344
                                    .getDocIdWithoutRevsion()
2345
                                    + PropertyService.getProperty("document.accNumSeparator")
2346
                                    + remoteDoc.getRevision();
2347
                            htmlDocumentImplList.add(elementInHtmlList);
2348
                        }//if
2349
                    }//if
2350

    
2351
                }//if
2352
                else {
2353
                    //create a docmentImpls object (represent xml doc) base on
2354
                    // the docId
2355
                    docImpls = (DocumentImpl) documentImplList.elementAt(i);
2356
                    //checking if the user has the permission to read the
2357
                    // documents
2358
                    if (DocumentImpl.hasReadPermission(user, groups, docImpls
2359
                            .getDocID())) {
2360
                        //if the docImpls is metadata
2361
                        if ((docImpls.getDoctype()).compareTo("BIN") != 0) {
2362
                            //add metadata into zip output stream
2363
                            addDocToZipOutputStream(docImpls, zOut, rootName);
2364
                            //add the documentImpl into the vetor which will
2365
                            // be used in html
2366
                            htmlDocumentImplList.add(docImpls);
2367

    
2368
                        }//if
2369
                        else {
2370
                            //it is data file
2371
                            addDataFileToZipOutputStream(docImpls, zOut,
2372
                                    rootName);
2373
                            htmlDocumentImplList.add(docImpls);
2374
                        }//else
2375
                    }//if
2376
                }//else
2377
            }//for
2378

    
2379
            //add html summary file
2380
            addHtmlSummaryToZipOutputStream(htmlDocumentImplList, zOut,
2381
                    rootName);
2382
            zOut.finish(); //terminate the zip file
2383
            //dbConn.close();
2384
            return zOut;
2385
        }//else
2386
    }//getZippedPackage()
2387

    
2388
    private class ReturnFieldValue
2389
    {
2390

    
2391
        private String docid = null; //return field value for this docid
2392

    
2393
        private String fieldValue = null;
2394

    
2395
        private String xmlFieldValue = null; //return field value in xml
2396
                                             // format
2397
        private String fieldType = null; //ATTRIBUTE, TEXT...
2398

    
2399
        public void setDocid(String myDocid)
2400
        {
2401
            docid = myDocid;
2402
        }
2403

    
2404
        public String getDocid()
2405
        {
2406
            return docid;
2407
        }
2408

    
2409
        public void setFieldValue(String myValue)
2410
        {
2411
            fieldValue = myValue;
2412
        }
2413

    
2414
        public String getFieldValue()
2415
        {
2416
            return fieldValue;
2417
        }
2418

    
2419
        public void setXMLFieldValue(String xml)
2420
        {
2421
            xmlFieldValue = xml;
2422
        }
2423

    
2424
        public String getXMLFieldValue()
2425
        {
2426
            return xmlFieldValue;
2427
        }
2428
        
2429
        public void setFieldType(String myType)
2430
        {
2431
            fieldType = myType;
2432
        }
2433

    
2434
        public String getFieldType()
2435
        {
2436
            return fieldType;
2437
        }
2438

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