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-08-24 14:34:17 -0700 (Mon, 24 Aug 2009) $'
14
 * '$Revision: 5030 $'
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.database.DBConnection;
52
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
53
import edu.ucsb.nceas.metacat.properties.PropertyService;
54
import edu.ucsb.nceas.metacat.util.AuthUtil;
55
import edu.ucsb.nceas.metacat.util.DocumentUtil;
56
import edu.ucsb.nceas.metacat.util.MetacatUtil;
57
import edu.ucsb.nceas.morpho.datapackage.Triple;
58
import edu.ucsb.nceas.morpho.datapackage.TripleCollection;
59
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
60

    
61

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

    
71
    static final int ALL = 1;
72

    
73
    static final int WRITE = 2;
74

    
75
    static final int READ = 4;
76

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

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

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

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

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

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

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

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

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

    
145
                double connTime = System.currentTimeMillis();
146

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

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

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

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

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

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

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

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

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

    
296
  }
297

    
298

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

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

    
363

    
364

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

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

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

    
410
      }//else
411

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
795

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
985

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

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

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

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

    
1029

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

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

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

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

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

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

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

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

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

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

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

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

    
1120

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

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

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

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

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

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

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

    
1274
     }//if has extended query
1275

    
1276
      return docListResult;
1277
    }//addReturnfield
1278

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

    
1353

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

    
1360
        Vector tempVector = null;
1361

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

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

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

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

    
1405
        Vector tempVector = null;
1406

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

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

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

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

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

    
1432
  
1433

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

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

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

    
1480

    
1481

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1659
        return (xmlquery.toString());
1660
    }
1661

    
1662
    /**
1663
     * format a simple free-text value query as an XML document that conforms
1664
     * to the pathquery.dtd and is appropriate for submission to the DBQuery
1665
     * structured query engine
1666
     *
1667
     * @param value the text string to search for in the xml catalog
1668
     */
1669
    public static String createQuery(String value)
1670
    {
1671
        return createQuery(value, "any");
1672
    }
1673

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1962
    }//addDocToZipOutputStream()
1963

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
2187
    }//addHtmlSummaryToZipOutputStream
2188

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
2387
    private class ReturnFieldValue
2388
    {
2389

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

    
2392
        private String fieldValue = null;
2393

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

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

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

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

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

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

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

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

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