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: 2008-07-06 21:25:34 -0700 (Sun, 06 Jul 2008) $'
14
 * '$Revision: 4080 $'
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.service.PropertyService;
52
import edu.ucsb.nceas.metacat.util.LDAPUtil;
53
import edu.ucsb.nceas.metacat.util.MetaCatUtil;
54
import edu.ucsb.nceas.morpho.datapackage.Triple;
55
import edu.ucsb.nceas.morpho.datapackage.TripleCollection;
56
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
57

    
58

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

    
68
    static final int ALL = 1;
69

    
70
    static final int WRITE = 2;
71

    
72
    static final int READ = 4;
73

    
74
    //private Connection conn = null;
75
    private String parserName = null;
76

    
77
    private Logger logMetacat = Logger.getLogger(DBQuery.class);
78

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

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

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

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

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

    
136
                // Time the request if asked for
137
                double startTime = System.currentTimeMillis();
138

    
139
                // Open a connection to the database
140
                //Connection dbconn = util.openDBConnection();
141

    
142
                double connTime = System.currentTimeMillis();
143

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

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

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

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

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

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

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

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

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

    
293
  }
294

    
295

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

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

    
360

    
361

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

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

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

    
407
      }//else
408

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

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

    
451
    //try to get the cached version first    
452
    // Hashtable sessionHash = MetaCatServlet.getSessionHash();
453
    // HttpSession sess = (HttpSession)sessionHash.get(sessionid);
454

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

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

    
474
        //checkout the dbconnection
475
        dbconn = DBConnectionPool.getDBConnection("DBQuery.findDocuments");
476
        serialNumber = dbconn.getCheckOutSerialNumber();
477

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

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

    
535
    //default to returning the whole resultset
536
    return resultset;
537
  }//createResultDocuments
538

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

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

    
650
      double queryExecuteTime = System.currentTimeMillis() / 1000;
651
      logMetacat.warn("Time to execute select docid query is "
652
                    + (queryExecuteTime - startTime));
653
      MetaCatUtil.writeDebugToFile("\n\n\n\n\n\nExecute selection query  "
654
              + (queryExecuteTime - startTime));
655
      MetaCatUtil.writeDebugToDelimiteredFile(""+(queryExecuteTime - startTime), false);
656

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

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

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

    
792

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

    
810
     // get value of xml_returnfield_count
811
     int count = (new Integer(PropertyService
812
                            .getProperty("xml_returnfield_count")))
813
                            .intValue();
814

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

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

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

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

    
857
     logMetacat.info("size of partOfDoclist after"
858
                             + " docidsInQueryresultTable(): "
859
                             + partOfDoclist.size());
860

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

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

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

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

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

    
968
         Hashtable returnValue = new Hashtable();
969
         PreparedStatement pstmt = null;
970
         ResultSet rs = null;
971

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

    
982

    
983
         if (doclist.length() > 0)
984
         {
985
             doclist.deleteCharAt(doclist.length() - 1); //remove the last comma
986

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

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

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

    
1026

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

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

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

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

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

    
1068
               pstmt = dbconn.prepareStatement(query);
1069
               dbconn.increaseUsageCount(1);
1070
               pstmt.execute();
1071
               pstmt.close();
1072

    
1073
           } else {
1074
               rs.close();
1075
               pstmt.close();
1076

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

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

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

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

    
1113
       returnfield_id = id;
1114
       return count;
1115
   }
1116

    
1117

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

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

    
1158
           String extendedQuery =
1159
               qspec.printExtendedSQL(doclist.toString(), useXMLIndex);
1160
           logMetacat.info("Extended query: " + extendedQuery);
1161

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

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

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

    
1266
         
1267
           
1268
           
1269
       }//if doclist lenght is great than zero
1270

    
1271
     }//if has extended query
1272

    
1273
      return docListResult;
1274
    }//addReturnfield
1275

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

    
1350

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

    
1357
        Vector tempVector = null;
1358

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

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

    
1389
        tempVector = new Vector();
1390
        tempVector.add(0, key);
1391
        tempVector.add(1, value);
1392
        parentidList.add(tempVector);
1393
        return;
1394
    }
1395

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

    
1402
        Vector tempVector = null;
1403

    
1404
        for (int count = 0; count < parentidList.size(); count++) {
1405
            tempVector = (Vector) parentidList.get(count);
1406

    
1407
            if (key.compareTo((String) tempVector.get(0)) == 0) { return (ReturnFieldValue) tempVector
1408
                    .get(1); }
1409
        }
1410
        return null;
1411
    }
1412

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

    
1421
        for (int count = 0; count < parentidList.size(); count++) {
1422
            tempVector = (Vector) parentidList.get(count);
1423

    
1424
            enumVector.add(tempVector.get(1));
1425
        }
1426
        return enumVector;
1427
    }
1428

    
1429
  
1430

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

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

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

    
1477

    
1478

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

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

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

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

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

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

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

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

    
1538
        if (params.containsKey("casesensitive")) {
1539
            casesensitive = ((String[]) params.get("casesensitive"))[0];
1540
        } else {
1541
            casesensitive = "false";
1542
        }
1543

    
1544
        if (params.containsKey("searchmode")) {
1545
            searchmode = ((String[]) params.get("searchmode"))[0];
1546
        } else {
1547
            searchmode = "contains";
1548
        }
1549

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

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

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

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

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

    
1637
        if (!doctype.equals("any") && !doctype.equals("ANY")) {
1638
            xmlquery.append("<returndoctype>");
1639
            xmlquery.append(doctype).append("</returndoctype>");
1640
        }
1641

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

    
1655
        return (xmlquery.toString());
1656
    }
1657

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

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

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

    
1698
        // Check the parameter
1699
        if (dataPackageDocid == null || dataPackageDocid.equals("")) { return docIdList; }//if
1700

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

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

    
1726
                //don't put the duplicate docId into the vector
1727
                if (!docIdList.contains(docIdInSubjectField)) {
1728
                    docIdList.add(docIdInSubjectField);
1729
                }
1730

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

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

    
1767
        Vector docIdList = new Vector();//return value
1768
        Vector tripleList = null;
1769
        String xml = null;
1770

    
1771
        // Check the parameter
1772
        if (dataPackageDocidWithRev == null || dataPackageDocidWithRev.equals("")) { return docIdList; }//if
1773

    
1774
        try {
1775
            //initial a documentImpl object
1776
            DocumentImpl packageDocument = new DocumentImpl(dataPackageDocidWithRev);
1777
            //transfer to documentImpl object to string
1778
            xml = packageDocument.toString();
1779

    
1780
            //create a tripcollection object
1781
            TripleCollection tripleForPackage = new TripleCollection(
1782
                    new StringReader(xml));
1783
            //get the vetor of triples
1784
            tripleList = tripleForPackage.getCollection();
1785

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

    
1805
        // return result
1806
        return docIdList;
1807
    }//getDocidListForPackageInXMLRevisions()
1808

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

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

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

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

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

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

    
1958
    }//addDocToZipOutputStream()
1959

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

    
1970
    private Vector getCurrentAllDocumentImpl(Vector docIdList)
1971
            throws McdbException, Exception
1972
    {
1973
        //Connection dbConn=null;
1974
        Vector documentImplList = new Vector();
1975
        int rev = 0;
1976

    
1977
        // Check the parameter
1978
        if (docIdList.isEmpty()) { return documentImplList; }//if
1979

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

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

    
1997
                String docidPlusVersion = ((String) docIdList.elementAt(i))
1998
                        + PropertyService.getProperty("accNumSeparator") + rev;
1999

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

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

    
2032
        // Check the parameter
2033
        if (docIdList.isEmpty()) { return documentImplList; }//if
2034

    
2035
        //for every docid in vector
2036
        for (int i = 0; i < docIdList.size(); i++) {
2037

    
2038
            String docidPlusVersion = (String) (docIdList.elementAt(i));
2039

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

    
2066
        }//for
2067
        return documentImplList;
2068
    }//getOldVersionAllDocumentImple
2069

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

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

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

    
2135
                htmlDoc.append("<a href=\"");
2136
                String dataFileid = (String) docImplList.elementAt(i);
2137
                htmlDoc.append("./data/").append(dataFileid).append("\">");
2138
                htmlDoc.append("Data File: ");
2139
                htmlDoc.append(dataFileid).append("</a><br>");
2140
                htmlDoc.append("<br><hr><br>");
2141

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

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

    
2183
    }//addHtmlSummaryToZipOutputStream
2184

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

    
2209
        String docId = null;
2210
        int version = -5;
2211
        // Docid without revision
2212
        docId = MetaCatUtil.getDocIdFromString(docIdString);
2213
        // revision number
2214
        version = MetaCatUtil.getVersionFromString(docIdString);
2215

    
2216
        //check if the reqused docId is a data package id
2217
        if (!isDataPackageId(docId)) {
2218

    
2219
            /*
2220
             * Exception e = new Exception("The request the doc id "
2221
             * +docIdString+ " is not a data package id");
2222
             */
2223

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

    
2229
                Exception e = new Exception("User " + user
2230
                        + " does not have permission"
2231
                        + " to export the data package " + docIdString);
2232
                throw e;
2233
            }
2234

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

    
2252
            zOut.finish(); //terminate the zip file
2253
            return zOut;
2254
        }
2255
        // Check the permission of user
2256
        else if (!hasPermissionToExportPackage(docId, user, groups)) {
2257

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

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

    
2291
                rootName = docIdString
2292
                        + PropertyService.getProperty("accNumSeparator") + "package";
2293
                //get the whole id list for data packadge
2294
                docIdList = getOldVersionDocidListForDataPackage(docIdString);
2295

    
2296
                //get the whole documentImple object
2297
                documentImplList = getOldVersionAllDocumentImpl(docIdList);
2298
            }//else
2299

    
2300
            // Make sure documentImplist is not empty
2301
            if (documentImplList.isEmpty()) { throw new Exception(
2302
                    "Couldn't find component for data package: " + packageId); }//if
2303

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

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

    
2363
                        }//if
2364
                        else {
2365
                            //it is data file
2366
                            addDataFileToZipOutputStream(docImpls, zOut,
2367
                                    rootName);
2368
                            htmlDocumentImplList.add(docImpls);
2369
                        }//else
2370
                    }//if
2371
                }//else
2372
            }//for
2373

    
2374
            //add html summary file
2375
            addHtmlSummaryToZipOutputStream(htmlDocumentImplList, zOut,
2376
                    rootName);
2377
            zOut.finish(); //terminate the zip file
2378
            //dbConn.close();
2379
            return zOut;
2380
        }//else
2381
    }//getZippedPackage()
2382

    
2383
    private class ReturnFieldValue
2384
    {
2385

    
2386
        private String docid = null; //return field value for this docid
2387

    
2388
        private String fieldValue = null;
2389

    
2390
        private String xmlFieldValue = null; //return field value in xml
2391
                                             // format
2392
        private String fieldType = null; //ATTRIBUTE, TEXT...
2393

    
2394
        public void setDocid(String myDocid)
2395
        {
2396
            docid = myDocid;
2397
        }
2398

    
2399
        public String getDocid()
2400
        {
2401
            return docid;
2402
        }
2403

    
2404
        public void setFieldValue(String myValue)
2405
        {
2406
            fieldValue = myValue;
2407
        }
2408

    
2409
        public String getFieldValue()
2410
        {
2411
            return fieldValue;
2412
        }
2413

    
2414
        public void setXMLFieldValue(String xml)
2415
        {
2416
            xmlFieldValue = xml;
2417
        }
2418

    
2419
        public String getXMLFieldValue()
2420
        {
2421
            return xmlFieldValue;
2422
        }
2423
        
2424
        public void setFieldType(String myType)
2425
        {
2426
            fieldType = myType;
2427
        }
2428

    
2429
        public String getFieldType()
2430
        {
2431
            return fieldType;
2432
        }
2433

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