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-12-26 13:07:40 -0800 (Fri, 26 Dec 2008) $'
|
14
|
* '$Revision: 4698 $'
|
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.AuthUtil;
|
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("database.queryresultCacheSize"));
|
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("xml.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("database.appResultsetSize"))).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("xml.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("database.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("document.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(AuthUtil.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("database.webResultsetSize"))).intValue();
|
577
|
}
|
578
|
else
|
579
|
{
|
580
|
offset =
|
581
|
(new Integer(PropertyService.getProperty("database.appResultsetSize"))).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.debug("query: " + query);
|
607
|
logMetacat.debug("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.debug("============ 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("database.queryCacheOn").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("result 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.debug("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.debug("############getting result: " + currentIndex);
|
669
|
docid = rs.getString(1).trim();
|
670
|
logMetacat.debug("############processing: " + docid);
|
671
|
docname = rs.getString(2);
|
672
|
doctype = rs.getString(3);
|
673
|
logMetacat.debug("############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.debug("NOT Back tracing now...");
|
682
|
document = new StringBuffer();
|
683
|
|
684
|
String completeDocid = docid
|
685
|
+ PropertyService.getProperty("document.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.debug("currentIndex: " + currentIndex);
|
727
|
logMetacat.debug("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("database.queryCacheOn").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 database.xmlReturnfieldCount
|
811
|
int count = (new Integer(PropertyService
|
812
|
.getProperty("database.xmlReturnfieldCount")))
|
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("database.queryresultStringLength")))
|
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] != null && !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
|
ignoredParams.add("searchmode");
|
1593
|
|
1594
|
// Also ignore parameters listed in the properties file
|
1595
|
// so that they can be passed through to stylesheets
|
1596
|
String paramsToIgnore = PropertyService
|
1597
|
.getProperty("database.queryignoredparams");
|
1598
|
StringTokenizer st = new StringTokenizer(paramsToIgnore, ",");
|
1599
|
while (st.hasMoreTokens()) {
|
1600
|
ignoredParams.add(st.nextToken());
|
1601
|
}
|
1602
|
if (!ignoredParams.contains(nextkey.toString())) {
|
1603
|
//allow for more than value per field name
|
1604
|
for (int i = 0; i < ((String[]) nextelement).length; i++) {
|
1605
|
if (!((String[]) nextelement)[i].equals("")) {
|
1606
|
query.append("<queryterm casesensitive=\""
|
1607
|
+ casesensitive + "\" " + "searchmode=\""
|
1608
|
+ searchmode + "\">" + "<value>" +
|
1609
|
//add the query value
|
1610
|
((String[]) nextelement)[i]
|
1611
|
+ "</value><pathexpr>" +
|
1612
|
//add the path to query by
|
1613
|
nextkey.toString() + "</pathexpr></queryterm>");
|
1614
|
}
|
1615
|
}
|
1616
|
}
|
1617
|
}
|
1618
|
query.append("</querygroup></pathquery>");
|
1619
|
//append on the end of the xml and return the result as a string
|
1620
|
return query.toString();
|
1621
|
}
|
1622
|
|
1623
|
/**
|
1624
|
* format a simple free-text value query as an XML document that conforms
|
1625
|
* to the pathquery.dtd and is appropriate for submission to the DBQuery
|
1626
|
* structured query engine
|
1627
|
*
|
1628
|
* @param value the text string to search for in the xml catalog
|
1629
|
* @param doctype the type of documents to include in the result set -- use
|
1630
|
* "any" or "ANY" for unfiltered result sets
|
1631
|
*/
|
1632
|
public static String createQuery(String value, String doctype)
|
1633
|
{
|
1634
|
StringBuffer xmlquery = new StringBuffer();
|
1635
|
xmlquery.append("<?xml version=\"1.0\"?>\n");
|
1636
|
xmlquery.append("<pathquery version=\"1.0\">");
|
1637
|
|
1638
|
if (!doctype.equals("any") && !doctype.equals("ANY")) {
|
1639
|
xmlquery.append("<returndoctype>");
|
1640
|
xmlquery.append(doctype).append("</returndoctype>");
|
1641
|
}
|
1642
|
|
1643
|
xmlquery.append("<querygroup operator=\"UNION\">");
|
1644
|
//chad added - 8/14
|
1645
|
//the if statement allows a query to gracefully handle a null
|
1646
|
//query. Without this if a nullpointerException is thrown.
|
1647
|
if (!value.equals("")) {
|
1648
|
xmlquery.append("<queryterm casesensitive=\"false\" ");
|
1649
|
xmlquery.append("searchmode=\"contains\">");
|
1650
|
xmlquery.append("<value>").append(value).append("</value>");
|
1651
|
xmlquery.append("</queryterm>");
|
1652
|
}
|
1653
|
xmlquery.append("</querygroup>");
|
1654
|
xmlquery.append("</pathquery>");
|
1655
|
|
1656
|
return (xmlquery.toString());
|
1657
|
}
|
1658
|
|
1659
|
/**
|
1660
|
* format a simple free-text value query as an XML document that conforms
|
1661
|
* to the pathquery.dtd and is appropriate for submission to the DBQuery
|
1662
|
* structured query engine
|
1663
|
*
|
1664
|
* @param value the text string to search for in the xml catalog
|
1665
|
*/
|
1666
|
public static String createQuery(String value)
|
1667
|
{
|
1668
|
return createQuery(value, "any");
|
1669
|
}
|
1670
|
|
1671
|
/**
|
1672
|
* Check for "READ" permission on @docid for @user and/or @group from DB
|
1673
|
* connection
|
1674
|
*/
|
1675
|
private boolean hasPermission(String user, String[] groups, String docid)
|
1676
|
throws SQLException, Exception
|
1677
|
{
|
1678
|
// Check for READ permission on @docid for @user and/or @groups
|
1679
|
PermissionController controller = new PermissionController(docid);
|
1680
|
return controller.hasPermission(user, groups,
|
1681
|
AccessControlInterface.READSTRING);
|
1682
|
}
|
1683
|
|
1684
|
/**
|
1685
|
* Get all docIds list for a data packadge
|
1686
|
*
|
1687
|
* @param dataPackageDocid, the string in docId field of xml_relation table
|
1688
|
*/
|
1689
|
private Vector getCurrentDocidListForDataPackage(String dataPackageDocid)
|
1690
|
{
|
1691
|
DBConnection dbConn = null;
|
1692
|
int serialNumber = -1;
|
1693
|
Vector docIdList = new Vector();//return value
|
1694
|
PreparedStatement pStmt = null;
|
1695
|
ResultSet rs = null;
|
1696
|
String docIdInSubjectField = null;
|
1697
|
String docIdInObjectField = null;
|
1698
|
|
1699
|
// Check the parameter
|
1700
|
if (dataPackageDocid == null || dataPackageDocid.equals("")) { return docIdList; }//if
|
1701
|
|
1702
|
//the query stirng
|
1703
|
String query = "SELECT subject, object from xml_relation where docId = ?";
|
1704
|
try {
|
1705
|
dbConn = DBConnectionPool
|
1706
|
.getDBConnection("DBQuery.getCurrentDocidListForDataPackage");
|
1707
|
serialNumber = dbConn.getCheckOutSerialNumber();
|
1708
|
pStmt = dbConn.prepareStatement(query);
|
1709
|
//bind the value to query
|
1710
|
pStmt.setString(1, dataPackageDocid);
|
1711
|
|
1712
|
//excute the query
|
1713
|
pStmt.execute();
|
1714
|
//get the result set
|
1715
|
rs = pStmt.getResultSet();
|
1716
|
//process the result
|
1717
|
while (rs.next()) {
|
1718
|
//In order to get the whole docIds in a data packadge,
|
1719
|
//we need to put the docIds of subject and object field in
|
1720
|
// xml_relation
|
1721
|
//into the return vector
|
1722
|
docIdInSubjectField = rs.getString(1);//the result docId in
|
1723
|
// subject field
|
1724
|
docIdInObjectField = rs.getString(2);//the result docId in
|
1725
|
// object field
|
1726
|
|
1727
|
//don't put the duplicate docId into the vector
|
1728
|
if (!docIdList.contains(docIdInSubjectField)) {
|
1729
|
docIdList.add(docIdInSubjectField);
|
1730
|
}
|
1731
|
|
1732
|
//don't put the duplicate docId into the vector
|
1733
|
if (!docIdList.contains(docIdInObjectField)) {
|
1734
|
docIdList.add(docIdInObjectField);
|
1735
|
}
|
1736
|
}//while
|
1737
|
//close the pStmt
|
1738
|
pStmt.close();
|
1739
|
}//try
|
1740
|
catch (SQLException e) {
|
1741
|
logMetacat.error("Error in getDocidListForDataPackage: "
|
1742
|
+ e.getMessage());
|
1743
|
}//catch
|
1744
|
finally {
|
1745
|
try {
|
1746
|
pStmt.close();
|
1747
|
}//try
|
1748
|
catch (SQLException ee) {
|
1749
|
logMetacat.error(
|
1750
|
"Error in getDocidListForDataPackage: "
|
1751
|
+ ee.getMessage());
|
1752
|
}//catch
|
1753
|
finally {
|
1754
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
1755
|
}//fianlly
|
1756
|
}//finally
|
1757
|
return docIdList;
|
1758
|
}//getCurrentDocidListForDataPackadge()
|
1759
|
|
1760
|
/**
|
1761
|
* Get all docIds list for a data packadge
|
1762
|
*
|
1763
|
* @param dataPackageDocid, the string in docId field of xml_relation table
|
1764
|
*/
|
1765
|
private Vector getOldVersionDocidListForDataPackage(String dataPackageDocidWithRev)
|
1766
|
{
|
1767
|
|
1768
|
Vector docIdList = new Vector();//return value
|
1769
|
Vector tripleList = null;
|
1770
|
String xml = null;
|
1771
|
|
1772
|
// Check the parameter
|
1773
|
if (dataPackageDocidWithRev == null || dataPackageDocidWithRev.equals("")) { return docIdList; }//if
|
1774
|
|
1775
|
try {
|
1776
|
//initial a documentImpl object
|
1777
|
DocumentImpl packageDocument = new DocumentImpl(dataPackageDocidWithRev);
|
1778
|
//transfer to documentImpl object to string
|
1779
|
xml = packageDocument.toString();
|
1780
|
|
1781
|
//create a tripcollection object
|
1782
|
TripleCollection tripleForPackage = new TripleCollection(
|
1783
|
new StringReader(xml));
|
1784
|
//get the vetor of triples
|
1785
|
tripleList = tripleForPackage.getCollection();
|
1786
|
|
1787
|
for (int i = 0; i < tripleList.size(); i++) {
|
1788
|
//put subject docid into docIdlist without duplicate
|
1789
|
if (!docIdList.contains(((Triple) tripleList.elementAt(i))
|
1790
|
.getSubject())) {
|
1791
|
//put subject docid into docIdlist
|
1792
|
docIdList.add(((Triple) tripleList.get(i)).getSubject());
|
1793
|
}
|
1794
|
//put object docid into docIdlist without duplicate
|
1795
|
if (!docIdList.contains(((Triple) tripleList.elementAt(i))
|
1796
|
.getObject())) {
|
1797
|
docIdList.add(((Triple) (tripleList.get(i))).getObject());
|
1798
|
}
|
1799
|
}//for
|
1800
|
}//try
|
1801
|
catch (Exception e) {
|
1802
|
logMetacat.error("Error in getOldVersionAllDocumentImpl: "
|
1803
|
+ e.getMessage());
|
1804
|
}//catch
|
1805
|
|
1806
|
// return result
|
1807
|
return docIdList;
|
1808
|
}//getDocidListForPackageInXMLRevisions()
|
1809
|
|
1810
|
/**
|
1811
|
* Check if the docId is a data packadge id. If the id is a data packadage
|
1812
|
* id, it should be store in the docId fields in xml_relation table. So we
|
1813
|
* can use a query to get the entries which the docId equals the given
|
1814
|
* value. If the result is null. The docId is not a packadge id. Otherwise,
|
1815
|
* it is.
|
1816
|
*
|
1817
|
* @param docId, the id need to be checked
|
1818
|
*/
|
1819
|
private boolean isDataPackageId(String docId)
|
1820
|
{
|
1821
|
boolean result = false;
|
1822
|
PreparedStatement pStmt = null;
|
1823
|
ResultSet rs = null;
|
1824
|
String query = "SELECT docId from xml_relation where docId = ?";
|
1825
|
DBConnection dbConn = null;
|
1826
|
int serialNumber = -1;
|
1827
|
try {
|
1828
|
dbConn = DBConnectionPool
|
1829
|
.getDBConnection("DBQuery.isDataPackageId");
|
1830
|
serialNumber = dbConn.getCheckOutSerialNumber();
|
1831
|
pStmt = dbConn.prepareStatement(query);
|
1832
|
//bind the value to query
|
1833
|
pStmt.setString(1, docId);
|
1834
|
//execute the query
|
1835
|
pStmt.execute();
|
1836
|
rs = pStmt.getResultSet();
|
1837
|
//process the result
|
1838
|
if (rs.next()) //There are some records for the id in docId fields
|
1839
|
{
|
1840
|
result = true;//It is a data packadge id
|
1841
|
}
|
1842
|
pStmt.close();
|
1843
|
}//try
|
1844
|
catch (SQLException e) {
|
1845
|
logMetacat.error("Error in isDataPackageId: "
|
1846
|
+ e.getMessage());
|
1847
|
} finally {
|
1848
|
try {
|
1849
|
pStmt.close();
|
1850
|
}//try
|
1851
|
catch (SQLException ee) {
|
1852
|
logMetacat.error("Error in isDataPackageId: "
|
1853
|
+ ee.getMessage());
|
1854
|
}//catch
|
1855
|
finally {
|
1856
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
1857
|
}//finally
|
1858
|
}//finally
|
1859
|
return result;
|
1860
|
}//isDataPackageId()
|
1861
|
|
1862
|
/**
|
1863
|
* Check if the user has the permission to export data package
|
1864
|
*
|
1865
|
* @param conn, the connection
|
1866
|
* @param docId, the id need to be checked
|
1867
|
* @param user, the name of user
|
1868
|
* @param groups, the user's group
|
1869
|
*/
|
1870
|
private boolean hasPermissionToExportPackage(String docId, String user,
|
1871
|
String[] groups) throws Exception
|
1872
|
{
|
1873
|
//DocumentImpl doc=new DocumentImpl(conn,docId);
|
1874
|
return DocumentImpl.hasReadPermission(user, groups, docId);
|
1875
|
}
|
1876
|
|
1877
|
/**
|
1878
|
* Get the current Rev for a docid in xml_documents table
|
1879
|
*
|
1880
|
* @param docId, the id need to get version numb If the return value is -5,
|
1881
|
* means no value in rev field for this docid
|
1882
|
*/
|
1883
|
private int getCurrentRevFromXMLDoumentsTable(String docId)
|
1884
|
throws SQLException
|
1885
|
{
|
1886
|
int rev = -5;
|
1887
|
PreparedStatement pStmt = null;
|
1888
|
ResultSet rs = null;
|
1889
|
String query = "SELECT rev from xml_documents where docId = ?";
|
1890
|
DBConnection dbConn = null;
|
1891
|
int serialNumber = -1;
|
1892
|
try {
|
1893
|
dbConn = DBConnectionPool
|
1894
|
.getDBConnection("DBQuery.getCurrentRevFromXMLDocumentsTable");
|
1895
|
serialNumber = dbConn.getCheckOutSerialNumber();
|
1896
|
pStmt = dbConn.prepareStatement(query);
|
1897
|
//bind the value to query
|
1898
|
pStmt.setString(1, docId);
|
1899
|
//execute the query
|
1900
|
pStmt.execute();
|
1901
|
rs = pStmt.getResultSet();
|
1902
|
//process the result
|
1903
|
if (rs.next()) //There are some records for rev
|
1904
|
{
|
1905
|
rev = rs.getInt(1);
|
1906
|
;//It is the version for given docid
|
1907
|
} else {
|
1908
|
rev = -5;
|
1909
|
}
|
1910
|
|
1911
|
}//try
|
1912
|
catch (SQLException e) {
|
1913
|
logMetacat.error(
|
1914
|
"Error in getCurrentRevFromXMLDoumentsTable: "
|
1915
|
+ e.getMessage());
|
1916
|
throw e;
|
1917
|
}//catch
|
1918
|
finally {
|
1919
|
try {
|
1920
|
pStmt.close();
|
1921
|
}//try
|
1922
|
catch (SQLException ee) {
|
1923
|
logMetacat.error(
|
1924
|
"Error in getCurrentRevFromXMLDoumentsTable: "
|
1925
|
+ ee.getMessage());
|
1926
|
}//catch
|
1927
|
finally {
|
1928
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
1929
|
}//finally
|
1930
|
}//finally
|
1931
|
return rev;
|
1932
|
}//getCurrentRevFromXMLDoumentsTable
|
1933
|
|
1934
|
/**
|
1935
|
* put a doc into a zip output stream
|
1936
|
*
|
1937
|
* @param docImpl, docmentImpl object which will be sent to zip output
|
1938
|
* stream
|
1939
|
* @param zipOut, zip output stream which the docImpl will be put
|
1940
|
* @param packageZipEntry, the zip entry name for whole package
|
1941
|
*/
|
1942
|
private void addDocToZipOutputStream(DocumentImpl docImpl,
|
1943
|
ZipOutputStream zipOut, String packageZipEntry)
|
1944
|
throws ClassNotFoundException, IOException, SQLException,
|
1945
|
McdbException, Exception
|
1946
|
{
|
1947
|
byte[] byteString = null;
|
1948
|
ZipEntry zEntry = null;
|
1949
|
|
1950
|
byteString = docImpl.toString().getBytes();
|
1951
|
//use docId as the zip entry's name
|
1952
|
zEntry = new ZipEntry(packageZipEntry + "/metadata/"
|
1953
|
+ docImpl.getDocID());
|
1954
|
zEntry.setSize(byteString.length);
|
1955
|
zipOut.putNextEntry(zEntry);
|
1956
|
zipOut.write(byteString, 0, byteString.length);
|
1957
|
zipOut.closeEntry();
|
1958
|
|
1959
|
}//addDocToZipOutputStream()
|
1960
|
|
1961
|
/**
|
1962
|
* Transfer a docid vetor to a documentImpl vector. The documentImpl vetor
|
1963
|
* only inlcudes current version. If a DocumentImple object couldn't find
|
1964
|
* for a docid, then the String of this docid was added to vetor rather
|
1965
|
* than DocumentImple object.
|
1966
|
*
|
1967
|
* @param docIdList, a vetor hold a docid list for a data package. In
|
1968
|
* docid, there is not version number in it.
|
1969
|
*/
|
1970
|
|
1971
|
private Vector getCurrentAllDocumentImpl(Vector docIdList)
|
1972
|
throws McdbException, Exception
|
1973
|
{
|
1974
|
//Connection dbConn=null;
|
1975
|
Vector documentImplList = new Vector();
|
1976
|
int rev = 0;
|
1977
|
|
1978
|
// Check the parameter
|
1979
|
if (docIdList.isEmpty()) { return documentImplList; }//if
|
1980
|
|
1981
|
//for every docid in vector
|
1982
|
for (int i = 0; i < docIdList.size(); i++) {
|
1983
|
try {
|
1984
|
//get newest version for this docId
|
1985
|
rev = getCurrentRevFromXMLDoumentsTable((String) docIdList
|
1986
|
.elementAt(i));
|
1987
|
|
1988
|
// There is no record for this docId in xml_documents table
|
1989
|
if (rev == -5) {
|
1990
|
// Rather than put DocumentImple object, put a String
|
1991
|
// Object(docid)
|
1992
|
// into the documentImplList
|
1993
|
documentImplList.add((String) docIdList.elementAt(i));
|
1994
|
// Skip other code
|
1995
|
continue;
|
1996
|
}
|
1997
|
|
1998
|
String docidPlusVersion = ((String) docIdList.elementAt(i))
|
1999
|
+ PropertyService.getProperty("document.accNumSeparator") + rev;
|
2000
|
|
2001
|
//create new documentImpl object
|
2002
|
DocumentImpl documentImplObject = new DocumentImpl(
|
2003
|
docidPlusVersion);
|
2004
|
//add them to vector
|
2005
|
documentImplList.add(documentImplObject);
|
2006
|
}//try
|
2007
|
catch (Exception e) {
|
2008
|
logMetacat.error("Error in getCurrentAllDocumentImpl: "
|
2009
|
+ e.getMessage());
|
2010
|
// continue the for loop
|
2011
|
continue;
|
2012
|
}
|
2013
|
}//for
|
2014
|
return documentImplList;
|
2015
|
}
|
2016
|
|
2017
|
/**
|
2018
|
* Transfer a docid vetor to a documentImpl vector. If a DocumentImple
|
2019
|
* object couldn't find for a docid, then the String of this docid was
|
2020
|
* added to vetor rather than DocumentImple object.
|
2021
|
*
|
2022
|
* @param docIdList, a vetor hold a docid list for a data package. In
|
2023
|
* docid, t here is version number in it.
|
2024
|
*/
|
2025
|
private Vector getOldVersionAllDocumentImpl(Vector docIdList)
|
2026
|
{
|
2027
|
//Connection dbConn=null;
|
2028
|
Vector documentImplList = new Vector();
|
2029
|
String siteCode = null;
|
2030
|
String uniqueId = null;
|
2031
|
int rev = 0;
|
2032
|
|
2033
|
// Check the parameter
|
2034
|
if (docIdList.isEmpty()) { return documentImplList; }//if
|
2035
|
|
2036
|
//for every docid in vector
|
2037
|
for (int i = 0; i < docIdList.size(); i++) {
|
2038
|
|
2039
|
String docidPlusVersion = (String) (docIdList.elementAt(i));
|
2040
|
|
2041
|
try {
|
2042
|
//create new documentImpl object
|
2043
|
DocumentImpl documentImplObject = new DocumentImpl(
|
2044
|
docidPlusVersion);
|
2045
|
//add them to vector
|
2046
|
documentImplList.add(documentImplObject);
|
2047
|
}//try
|
2048
|
catch (McdbDocNotFoundException notFoundE) {
|
2049
|
logMetacat.error(
|
2050
|
"Error in DBQuery.getOldVersionAllDocument" + "Imple"
|
2051
|
+ notFoundE.getMessage());
|
2052
|
// Rather than add a DocumentImple object into vetor, a String
|
2053
|
// object
|
2054
|
// - the doicd was added to the vector
|
2055
|
documentImplList.add(docidPlusVersion);
|
2056
|
// Continue the for loop
|
2057
|
continue;
|
2058
|
}//catch
|
2059
|
catch (Exception e) {
|
2060
|
logMetacat.error(
|
2061
|
"Error in DBQuery.getOldVersionAllDocument" + "Imple"
|
2062
|
+ e.getMessage());
|
2063
|
// Continue the for loop
|
2064
|
continue;
|
2065
|
}//catch
|
2066
|
|
2067
|
}//for
|
2068
|
return documentImplList;
|
2069
|
}//getOldVersionAllDocumentImple
|
2070
|
|
2071
|
/**
|
2072
|
* put a data file into a zip output stream
|
2073
|
*
|
2074
|
* @param docImpl, docmentImpl object which will be sent to zip output
|
2075
|
* stream
|
2076
|
* @param zipOut, the zip output stream which the docImpl will be put
|
2077
|
* @param packageZipEntry, the zip entry name for whole package
|
2078
|
*/
|
2079
|
private void addDataFileToZipOutputStream(DocumentImpl docImpl,
|
2080
|
ZipOutputStream zipOut, String packageZipEntry)
|
2081
|
throws ClassNotFoundException, IOException, SQLException,
|
2082
|
McdbException, Exception
|
2083
|
{
|
2084
|
byte[] byteString = null;
|
2085
|
ZipEntry zEntry = null;
|
2086
|
// this is data file; add file to zip
|
2087
|
String filePath = PropertyService.getProperty("application.datafilepath");
|
2088
|
if (!filePath.endsWith("/")) {
|
2089
|
filePath += "/";
|
2090
|
}
|
2091
|
String fileName = filePath + docImpl.getDocID();
|
2092
|
zEntry = new ZipEntry(packageZipEntry + "/data/" + docImpl.getDocID());
|
2093
|
zipOut.putNextEntry(zEntry);
|
2094
|
FileInputStream fin = null;
|
2095
|
try {
|
2096
|
fin = new FileInputStream(fileName);
|
2097
|
byte[] buf = new byte[4 * 1024]; // 4K buffer
|
2098
|
int b = fin.read(buf);
|
2099
|
while (b != -1) {
|
2100
|
zipOut.write(buf, 0, b);
|
2101
|
b = fin.read(buf);
|
2102
|
}//while
|
2103
|
zipOut.closeEntry();
|
2104
|
}//try
|
2105
|
catch (IOException ioe) {
|
2106
|
logMetacat.error("There is an exception: "
|
2107
|
+ ioe.getMessage());
|
2108
|
}//catch
|
2109
|
}//addDataFileToZipOutputStream()
|
2110
|
|
2111
|
/**
|
2112
|
* create a html summary for data package and put it into zip output stream
|
2113
|
*
|
2114
|
* @param docImplList, the documentImpl ojbects in data package
|
2115
|
* @param zipOut, the zip output stream which the html should be put
|
2116
|
* @param packageZipEntry, the zip entry name for whole package
|
2117
|
*/
|
2118
|
private void addHtmlSummaryToZipOutputStream(Vector docImplList,
|
2119
|
ZipOutputStream zipOut, String packageZipEntry) throws Exception
|
2120
|
{
|
2121
|
StringBuffer htmlDoc = new StringBuffer();
|
2122
|
ZipEntry zEntry = null;
|
2123
|
byte[] byteString = null;
|
2124
|
InputStream source;
|
2125
|
DBTransform xmlToHtml;
|
2126
|
|
2127
|
//create a DBTransform ojbect
|
2128
|
xmlToHtml = new DBTransform();
|
2129
|
//head of html
|
2130
|
htmlDoc.append("<html><head></head><body>");
|
2131
|
for (int i = 0; i < docImplList.size(); i++) {
|
2132
|
// If this String object, this means it is missed data file
|
2133
|
if ((((docImplList.elementAt(i)).getClass()).toString())
|
2134
|
.equals("class java.lang.String")) {
|
2135
|
|
2136
|
htmlDoc.append("<a href=\"");
|
2137
|
String dataFileid = (String) docImplList.elementAt(i);
|
2138
|
htmlDoc.append("./data/").append(dataFileid).append("\">");
|
2139
|
htmlDoc.append("Data File: ");
|
2140
|
htmlDoc.append(dataFileid).append("</a><br>");
|
2141
|
htmlDoc.append("<br><hr><br>");
|
2142
|
|
2143
|
}//if
|
2144
|
else if ((((DocumentImpl) docImplList.elementAt(i)).getDoctype())
|
2145
|
.compareTo("BIN") != 0) { //this is an xml file so we can
|
2146
|
// transform it.
|
2147
|
//transform each file individually then concatenate all of the
|
2148
|
//transformations together.
|
2149
|
|
2150
|
//for metadata xml title
|
2151
|
htmlDoc.append("<h2>");
|
2152
|
htmlDoc.append(((DocumentImpl) docImplList.elementAt(i))
|
2153
|
.getDocID());
|
2154
|
//htmlDoc.append(".");
|
2155
|
//htmlDoc.append(((DocumentImpl)docImplList.elementAt(i)).getRev());
|
2156
|
htmlDoc.append("</h2>");
|
2157
|
//do the actual transform
|
2158
|
StringWriter docString = new StringWriter();
|
2159
|
xmlToHtml.transformXMLDocument(((DocumentImpl) docImplList
|
2160
|
.elementAt(i)).toString(), "-//NCEAS//eml-generic//EN",
|
2161
|
"-//W3C//HTML//EN", "html", docString);
|
2162
|
htmlDoc.append(docString.toString());
|
2163
|
htmlDoc.append("<br><br><hr><br><br>");
|
2164
|
}//if
|
2165
|
else { //this is a data file so we should link to it in the html
|
2166
|
htmlDoc.append("<a href=\"");
|
2167
|
String dataFileid = ((DocumentImpl) docImplList.elementAt(i))
|
2168
|
.getDocID();
|
2169
|
htmlDoc.append("./data/").append(dataFileid).append("\">");
|
2170
|
htmlDoc.append("Data File: ");
|
2171
|
htmlDoc.append(dataFileid).append("</a><br>");
|
2172
|
htmlDoc.append("<br><hr><br>");
|
2173
|
}//else
|
2174
|
}//for
|
2175
|
htmlDoc.append("</body></html>");
|
2176
|
byteString = htmlDoc.toString().getBytes();
|
2177
|
zEntry = new ZipEntry(packageZipEntry + "/metadata.html");
|
2178
|
zEntry.setSize(byteString.length);
|
2179
|
zipOut.putNextEntry(zEntry);
|
2180
|
zipOut.write(byteString, 0, byteString.length);
|
2181
|
zipOut.closeEntry();
|
2182
|
//dbConn.close();
|
2183
|
|
2184
|
}//addHtmlSummaryToZipOutputStream
|
2185
|
|
2186
|
/**
|
2187
|
* put a data packadge into a zip output stream
|
2188
|
*
|
2189
|
* @param docId, which the user want to put into zip output stream,it has version
|
2190
|
* @param out, a servletoutput stream which the zip output stream will be
|
2191
|
* put
|
2192
|
* @param user, the username of the user
|
2193
|
* @param groups, the group of the user
|
2194
|
*/
|
2195
|
public ZipOutputStream getZippedPackage(String docIdString,
|
2196
|
ServletOutputStream out, String user, String[] groups,
|
2197
|
String passWord) throws ClassNotFoundException, IOException,
|
2198
|
SQLException, McdbException, NumberFormatException, Exception
|
2199
|
{
|
2200
|
ZipOutputStream zOut = null;
|
2201
|
String elementDocid = null;
|
2202
|
DocumentImpl docImpls = null;
|
2203
|
//Connection dbConn = null;
|
2204
|
Vector docIdList = new Vector();
|
2205
|
Vector documentImplList = new Vector();
|
2206
|
Vector htmlDocumentImplList = new Vector();
|
2207
|
String packageId = null;
|
2208
|
String rootName = "package";//the package zip entry name
|
2209
|
|
2210
|
String docId = null;
|
2211
|
int version = -5;
|
2212
|
// Docid without revision
|
2213
|
docId = MetacatUtil.getDocIdFromString(docIdString);
|
2214
|
// revision number
|
2215
|
version = MetacatUtil.getVersionFromString(docIdString);
|
2216
|
|
2217
|
//check if the reqused docId is a data package id
|
2218
|
if (!isDataPackageId(docId)) {
|
2219
|
|
2220
|
/*
|
2221
|
* Exception e = new Exception("The request the doc id "
|
2222
|
* +docIdString+ " is not a data package id");
|
2223
|
*/
|
2224
|
|
2225
|
//CB 1/6/03: if the requested docid is not a datapackage, we just
|
2226
|
// zip
|
2227
|
//up the single document and return the zip file.
|
2228
|
if (!hasPermissionToExportPackage(docId, user, groups)) {
|
2229
|
|
2230
|
Exception e = new Exception("User " + user
|
2231
|
+ " does not have permission"
|
2232
|
+ " to export the data package " + docIdString);
|
2233
|
throw e;
|
2234
|
}
|
2235
|
|
2236
|
docImpls = new DocumentImpl(docIdString);
|
2237
|
//checking if the user has the permission to read the documents
|
2238
|
if (DocumentImpl.hasReadPermission(user, groups, docImpls
|
2239
|
.getDocID())) {
|
2240
|
zOut = new ZipOutputStream(out);
|
2241
|
//if the docImpls is metadata
|
2242
|
if ((docImpls.getDoctype()).compareTo("BIN") != 0) {
|
2243
|
//add metadata into zip output stream
|
2244
|
addDocToZipOutputStream(docImpls, zOut, rootName);
|
2245
|
}//if
|
2246
|
else {
|
2247
|
//it is data file
|
2248
|
addDataFileToZipOutputStream(docImpls, zOut, rootName);
|
2249
|
htmlDocumentImplList.add(docImpls);
|
2250
|
}//else
|
2251
|
}//if
|
2252
|
|
2253
|
zOut.finish(); //terminate the zip file
|
2254
|
return zOut;
|
2255
|
}
|
2256
|
// Check the permission of user
|
2257
|
else if (!hasPermissionToExportPackage(docId, user, groups)) {
|
2258
|
|
2259
|
Exception e = new Exception("User " + user
|
2260
|
+ " does not have permission"
|
2261
|
+ " to export the data package " + docIdString);
|
2262
|
throw e;
|
2263
|
} else //it is a packadge id
|
2264
|
{
|
2265
|
//store the package id
|
2266
|
packageId = docId;
|
2267
|
//get current version in database
|
2268
|
int currentVersion = getCurrentRevFromXMLDoumentsTable(packageId);
|
2269
|
//If it is for current version (-1 means user didn't specify
|
2270
|
// revision)
|
2271
|
if ((version == -1) || version == currentVersion) {
|
2272
|
//get current version number
|
2273
|
version = currentVersion;
|
2274
|
//get package zip entry name
|
2275
|
//it should be docId.revsion.package
|
2276
|
rootName = packageId + PropertyService.getProperty("document.accNumSeparator")
|
2277
|
+ version + PropertyService.getProperty("document.accNumSeparator")
|
2278
|
+ "package";
|
2279
|
//get the whole id list for data packadge
|
2280
|
docIdList = getCurrentDocidListForDataPackage(packageId);
|
2281
|
//get the whole documentImple object
|
2282
|
documentImplList = getCurrentAllDocumentImpl(docIdList);
|
2283
|
|
2284
|
}//if
|
2285
|
else if (version > currentVersion || version < -1) {
|
2286
|
throw new Exception("The user specified docid: " + docId + "."
|
2287
|
+ version + " doesn't exist");
|
2288
|
}//else if
|
2289
|
else //for an old version
|
2290
|
{
|
2291
|
|
2292
|
rootName = docIdString
|
2293
|
+ PropertyService.getProperty("document.accNumSeparator") + "package";
|
2294
|
//get the whole id list for data packadge
|
2295
|
docIdList = getOldVersionDocidListForDataPackage(docIdString);
|
2296
|
|
2297
|
//get the whole documentImple object
|
2298
|
documentImplList = getOldVersionAllDocumentImpl(docIdList);
|
2299
|
}//else
|
2300
|
|
2301
|
// Make sure documentImplist is not empty
|
2302
|
if (documentImplList.isEmpty()) { throw new Exception(
|
2303
|
"Couldn't find component for data package: " + packageId); }//if
|
2304
|
|
2305
|
zOut = new ZipOutputStream(out);
|
2306
|
//put every element into zip output stream
|
2307
|
for (int i = 0; i < documentImplList.size(); i++) {
|
2308
|
// if the object in the vetor is String, this means we couldn't
|
2309
|
// find
|
2310
|
// the document locally, we need find it remote
|
2311
|
if ((((documentImplList.elementAt(i)).getClass()).toString())
|
2312
|
.equals("class java.lang.String")) {
|
2313
|
// Get String object from vetor
|
2314
|
String documentId = (String) documentImplList.elementAt(i);
|
2315
|
logMetacat.info("docid: " + documentId);
|
2316
|
// Get doicd without revision
|
2317
|
String docidWithoutRevision = MetacatUtil
|
2318
|
.getDocIdFromString(documentId);
|
2319
|
logMetacat.info("docidWithoutRevsion: "
|
2320
|
+ docidWithoutRevision);
|
2321
|
// Get revision
|
2322
|
String revision = MetacatUtil
|
2323
|
.getRevisionStringFromString(documentId);
|
2324
|
logMetacat.info("revsion from docIdentifier: "
|
2325
|
+ revision);
|
2326
|
// Zip entry string
|
2327
|
String zipEntryPath = rootName + "/data/";
|
2328
|
// Create a RemoteDocument object
|
2329
|
RemoteDocument remoteDoc = new RemoteDocument(
|
2330
|
docidWithoutRevision, revision, user, passWord,
|
2331
|
zipEntryPath);
|
2332
|
// Here we only read data file from remote metacat
|
2333
|
String docType = remoteDoc.getDocType();
|
2334
|
if (docType != null) {
|
2335
|
if (docType.equals("BIN")) {
|
2336
|
// Put remote document to zip output
|
2337
|
remoteDoc.readDocumentFromRemoteServerByZip(zOut);
|
2338
|
// Add String object to htmlDocumentImplList
|
2339
|
String elementInHtmlList = remoteDoc
|
2340
|
.getDocIdWithoutRevsion()
|
2341
|
+ PropertyService.getProperty("document.accNumSeparator")
|
2342
|
+ remoteDoc.getRevision();
|
2343
|
htmlDocumentImplList.add(elementInHtmlList);
|
2344
|
}//if
|
2345
|
}//if
|
2346
|
|
2347
|
}//if
|
2348
|
else {
|
2349
|
//create a docmentImpls object (represent xml doc) base on
|
2350
|
// the docId
|
2351
|
docImpls = (DocumentImpl) documentImplList.elementAt(i);
|
2352
|
//checking if the user has the permission to read the
|
2353
|
// documents
|
2354
|
if (DocumentImpl.hasReadPermission(user, groups, docImpls
|
2355
|
.getDocID())) {
|
2356
|
//if the docImpls is metadata
|
2357
|
if ((docImpls.getDoctype()).compareTo("BIN") != 0) {
|
2358
|
//add metadata into zip output stream
|
2359
|
addDocToZipOutputStream(docImpls, zOut, rootName);
|
2360
|
//add the documentImpl into the vetor which will
|
2361
|
// be used in html
|
2362
|
htmlDocumentImplList.add(docImpls);
|
2363
|
|
2364
|
}//if
|
2365
|
else {
|
2366
|
//it is data file
|
2367
|
addDataFileToZipOutputStream(docImpls, zOut,
|
2368
|
rootName);
|
2369
|
htmlDocumentImplList.add(docImpls);
|
2370
|
}//else
|
2371
|
}//if
|
2372
|
}//else
|
2373
|
}//for
|
2374
|
|
2375
|
//add html summary file
|
2376
|
addHtmlSummaryToZipOutputStream(htmlDocumentImplList, zOut,
|
2377
|
rootName);
|
2378
|
zOut.finish(); //terminate the zip file
|
2379
|
//dbConn.close();
|
2380
|
return zOut;
|
2381
|
}//else
|
2382
|
}//getZippedPackage()
|
2383
|
|
2384
|
private class ReturnFieldValue
|
2385
|
{
|
2386
|
|
2387
|
private String docid = null; //return field value for this docid
|
2388
|
|
2389
|
private String fieldValue = null;
|
2390
|
|
2391
|
private String xmlFieldValue = null; //return field value in xml
|
2392
|
// format
|
2393
|
private String fieldType = null; //ATTRIBUTE, TEXT...
|
2394
|
|
2395
|
public void setDocid(String myDocid)
|
2396
|
{
|
2397
|
docid = myDocid;
|
2398
|
}
|
2399
|
|
2400
|
public String getDocid()
|
2401
|
{
|
2402
|
return docid;
|
2403
|
}
|
2404
|
|
2405
|
public void setFieldValue(String myValue)
|
2406
|
{
|
2407
|
fieldValue = myValue;
|
2408
|
}
|
2409
|
|
2410
|
public String getFieldValue()
|
2411
|
{
|
2412
|
return fieldValue;
|
2413
|
}
|
2414
|
|
2415
|
public void setXMLFieldValue(String xml)
|
2416
|
{
|
2417
|
xmlFieldValue = xml;
|
2418
|
}
|
2419
|
|
2420
|
public String getXMLFieldValue()
|
2421
|
{
|
2422
|
return xmlFieldValue;
|
2423
|
}
|
2424
|
|
2425
|
public void setFieldType(String myType)
|
2426
|
{
|
2427
|
fieldType = myType;
|
2428
|
}
|
2429
|
|
2430
|
public String getFieldType()
|
2431
|
{
|
2432
|
return fieldType;
|
2433
|
}
|
2434
|
|
2435
|
}
|
2436
|
|
2437
|
/**
|
2438
|
* a class to store one result document consisting of a docid and a document
|
2439
|
*/
|
2440
|
private class ResultDocument
|
2441
|
{
|
2442
|
public String docid;
|
2443
|
public String document;
|
2444
|
|
2445
|
public ResultDocument(String docid, String document)
|
2446
|
{
|
2447
|
this.docid = docid;
|
2448
|
this.document = document;
|
2449
|
}
|
2450
|
}
|
2451
|
|
2452
|
/**
|
2453
|
* a private class to handle a set of resultDocuments
|
2454
|
*/
|
2455
|
private class ResultDocumentSet
|
2456
|
{
|
2457
|
private Vector docids;
|
2458
|
private Vector documents;
|
2459
|
|
2460
|
public ResultDocumentSet()
|
2461
|
{
|
2462
|
docids = new Vector();
|
2463
|
documents = new Vector();
|
2464
|
}
|
2465
|
|
2466
|
/**
|
2467
|
* adds a result document to the set
|
2468
|
*/
|
2469
|
public void addResultDocument(ResultDocument rd)
|
2470
|
{
|
2471
|
if(rd.docid == null)
|
2472
|
return;
|
2473
|
if(rd.document == null)
|
2474
|
rd.document = "";
|
2475
|
|
2476
|
docids.addElement(rd.docid);
|
2477
|
documents.addElement(rd.document);
|
2478
|
|
2479
|
}
|
2480
|
|
2481
|
/**
|
2482
|
* gets an iterator of docids
|
2483
|
*/
|
2484
|
public Iterator getDocids()
|
2485
|
{
|
2486
|
return docids.iterator();
|
2487
|
}
|
2488
|
|
2489
|
/**
|
2490
|
* gets an iterator of documents
|
2491
|
*/
|
2492
|
public Iterator getDocuments()
|
2493
|
{
|
2494
|
return documents.iterator();
|
2495
|
}
|
2496
|
|
2497
|
/**
|
2498
|
* returns the size of the set
|
2499
|
*/
|
2500
|
public int size()
|
2501
|
{
|
2502
|
return docids.size();
|
2503
|
}
|
2504
|
|
2505
|
/**
|
2506
|
* tests to see if this set contains the given docid
|
2507
|
*/
|
2508
|
private boolean containsDocid(String docid)
|
2509
|
{
|
2510
|
for(int i=0; i<docids.size(); i++)
|
2511
|
{
|
2512
|
String docid0 = (String)docids.elementAt(i);
|
2513
|
if(docid0.trim().equals(docid.trim()))
|
2514
|
{
|
2515
|
return true;
|
2516
|
}
|
2517
|
}
|
2518
|
return false;
|
2519
|
}
|
2520
|
|
2521
|
/**
|
2522
|
* removes the element with the given docid
|
2523
|
*/
|
2524
|
public String remove(String docid)
|
2525
|
{
|
2526
|
for(int i=0; i<docids.size(); i++)
|
2527
|
{
|
2528
|
String docid0 = (String)docids.elementAt(i);
|
2529
|
if(docid0.trim().equals(docid.trim()))
|
2530
|
{
|
2531
|
String returnDoc = (String)documents.elementAt(i);
|
2532
|
documents.remove(i);
|
2533
|
docids.remove(i);
|
2534
|
return returnDoc;
|
2535
|
}
|
2536
|
}
|
2537
|
return null;
|
2538
|
}
|
2539
|
|
2540
|
/**
|
2541
|
* add a result document
|
2542
|
*/
|
2543
|
public void put(ResultDocument rd)
|
2544
|
{
|
2545
|
addResultDocument(rd);
|
2546
|
}
|
2547
|
|
2548
|
/**
|
2549
|
* add a result document by components
|
2550
|
*/
|
2551
|
public void put(String docid, String document)
|
2552
|
{
|
2553
|
addResultDocument(new ResultDocument(docid, document));
|
2554
|
}
|
2555
|
|
2556
|
/**
|
2557
|
* get the document part of the result document by docid
|
2558
|
*/
|
2559
|
public Object get(String docid)
|
2560
|
{
|
2561
|
for(int i=0; i<docids.size(); i++)
|
2562
|
{
|
2563
|
String docid0 = (String)docids.elementAt(i);
|
2564
|
if(docid0.trim().equals(docid.trim()))
|
2565
|
{
|
2566
|
return documents.elementAt(i);
|
2567
|
}
|
2568
|
}
|
2569
|
return null;
|
2570
|
}
|
2571
|
|
2572
|
/**
|
2573
|
* get the document part of the result document by an object
|
2574
|
*/
|
2575
|
public Object get(Object o)
|
2576
|
{
|
2577
|
return get((String)o);
|
2578
|
}
|
2579
|
|
2580
|
/**
|
2581
|
* get an entire result document by index number
|
2582
|
*/
|
2583
|
public ResultDocument get(int index)
|
2584
|
{
|
2585
|
return new ResultDocument((String)docids.elementAt(index),
|
2586
|
(String)documents.elementAt(index));
|
2587
|
}
|
2588
|
|
2589
|
/**
|
2590
|
* return a string representation of this object
|
2591
|
*/
|
2592
|
public String toString()
|
2593
|
{
|
2594
|
String s = "";
|
2595
|
for(int i=0; i<docids.size(); i++)
|
2596
|
{
|
2597
|
s += (String)docids.elementAt(i) + "\n";
|
2598
|
}
|
2599
|
return s;
|
2600
|
}
|
2601
|
/*
|
2602
|
* Set a new document value for a given docid
|
2603
|
*/
|
2604
|
public void set(String docid, String document)
|
2605
|
{
|
2606
|
for(int i=0; i<docids.size(); i++)
|
2607
|
{
|
2608
|
String docid0 = (String)docids.elementAt(i);
|
2609
|
if(docid0.trim().equals(docid.trim()))
|
2610
|
{
|
2611
|
documents.set(i, document);
|
2612
|
}
|
2613
|
}
|
2614
|
|
2615
|
}
|
2616
|
}
|
2617
|
}
|