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
|
* Release: @release@
|
12
|
*
|
13
|
* '$Author: berkley $'
|
14
|
* '$Date: 2002-03-06 16:17:02 -0800 (Wed, 06 Mar 2002) $'
|
15
|
* '$Revision: 963 $'
|
16
|
*
|
17
|
* This program is free software; you can redistribute it and/or modify
|
18
|
* it under the terms of the GNU General Public License as published by
|
19
|
* the Free Software Foundation; either version 2 of the License, or
|
20
|
* (at your option) any later version.
|
21
|
*
|
22
|
* This program is distributed in the hope that it will be useful,
|
23
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
24
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
25
|
* GNU General Public License for more details.
|
26
|
*
|
27
|
* You should have received a copy of the GNU General Public License
|
28
|
* along with this program; if not, write to the Free Software
|
29
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
30
|
*/
|
31
|
|
32
|
package edu.ucsb.nceas.metacat;
|
33
|
|
34
|
import edu.ucsb.nceas.morpho.datapackage.*;
|
35
|
import java.io.*;
|
36
|
import java.util.Vector;
|
37
|
import java.util.zip.*;
|
38
|
import java.net.URL;
|
39
|
import java.net.MalformedURLException;
|
40
|
import java.sql.*;
|
41
|
import java.util.Stack;
|
42
|
import java.util.Hashtable;
|
43
|
import java.util.Enumeration;
|
44
|
import java.io.File;
|
45
|
import java.io.FileWriter;
|
46
|
import java.io.BufferedWriter;
|
47
|
import javax.servlet.ServletOutputStream;
|
48
|
|
49
|
/**
|
50
|
* A Class that searches a relational DB for elements and
|
51
|
* attributes that have free text matches a query string,
|
52
|
* or structured query matches to a path specified node in the
|
53
|
* XML hierarchy. It returns a result set consisting of the
|
54
|
* document ID for each document that satisfies the query
|
55
|
*/
|
56
|
public class DBQuery {
|
57
|
|
58
|
static final int ALL = 1;
|
59
|
static final int WRITE = 2;
|
60
|
static final int READ = 4;
|
61
|
|
62
|
private Connection conn = null;
|
63
|
private String parserName = null;
|
64
|
private MetaCatUtil util = new MetaCatUtil();
|
65
|
/**
|
66
|
* the main routine used to test the DBQuery utility.
|
67
|
* <p>
|
68
|
* Usage: java DBQuery <xmlfile>
|
69
|
*
|
70
|
* @param xmlfile the filename of the xml file containing the query
|
71
|
*/
|
72
|
static public void main(String[] args) {
|
73
|
|
74
|
if (args.length < 1)
|
75
|
{
|
76
|
System.err.println("Wrong number of arguments!!!");
|
77
|
System.err.println("USAGE: java DBQuery [-t] [-index] <xmlfile>");
|
78
|
return;
|
79
|
} else {
|
80
|
try {
|
81
|
|
82
|
int i = 0;
|
83
|
boolean showRuntime = false;
|
84
|
boolean useXMLIndex = false;
|
85
|
if ( args[i].equals( "-t" ) ) {
|
86
|
showRuntime = true;
|
87
|
i++;
|
88
|
}
|
89
|
if ( args[i].equals( "-index" ) ) {
|
90
|
useXMLIndex = true;
|
91
|
i++;
|
92
|
}
|
93
|
String xmlfile = args[i];
|
94
|
|
95
|
// Time the request if asked for
|
96
|
double startTime = System.currentTimeMillis();
|
97
|
|
98
|
// Open a connection to the database
|
99
|
MetaCatUtil util = new MetaCatUtil();
|
100
|
Connection dbconn = util.openDBConnection();
|
101
|
|
102
|
double connTime = System.currentTimeMillis();
|
103
|
|
104
|
// Execute the query
|
105
|
DBQuery queryobj = new DBQuery(dbconn, util.getOption("saxparser"));
|
106
|
FileReader xml = new FileReader(new File(xmlfile));
|
107
|
Hashtable nodelist = null;
|
108
|
nodelist = queryobj.findDocuments(xml, null, null, useXMLIndex);
|
109
|
|
110
|
// Print the reulting document listing
|
111
|
StringBuffer result = new StringBuffer();
|
112
|
String document = null;
|
113
|
String docid = null;
|
114
|
result.append("<?xml version=\"1.0\"?>\n");
|
115
|
result.append("<resultset>\n");
|
116
|
|
117
|
if (!showRuntime)
|
118
|
{
|
119
|
Enumeration doclist = nodelist.keys();
|
120
|
while (doclist.hasMoreElements()) {
|
121
|
docid = (String)doclist.nextElement();
|
122
|
document = (String)nodelist.get(docid);
|
123
|
result.append(" <document>\n " + document +
|
124
|
"\n </document>\n");
|
125
|
}
|
126
|
|
127
|
result.append("</resultset>\n");
|
128
|
}
|
129
|
// Time the request if asked for
|
130
|
double stopTime = System.currentTimeMillis();
|
131
|
double dbOpenTime = (connTime - startTime)/1000;
|
132
|
double readTime = (stopTime - connTime)/1000;
|
133
|
double executionTime = (stopTime - startTime)/1000;
|
134
|
if (showRuntime) {
|
135
|
System.out.print(" " + executionTime);
|
136
|
System.out.print(" " + dbOpenTime);
|
137
|
System.out.print(" " + readTime);
|
138
|
System.out.print(" " + nodelist.size());
|
139
|
System.out.println();
|
140
|
}
|
141
|
//System.out.println(result);
|
142
|
//write into a file "result.txt"
|
143
|
if (!showRuntime)
|
144
|
{
|
145
|
File f = new File("./result.txt");
|
146
|
FileWriter fw = new FileWriter(f);
|
147
|
BufferedWriter out = new BufferedWriter(fw);
|
148
|
out.write(result.toString());
|
149
|
out.flush();
|
150
|
out.close();
|
151
|
fw.close();
|
152
|
}
|
153
|
|
154
|
}
|
155
|
catch (Exception e) {
|
156
|
System.err.println("Error in DBQuery.main");
|
157
|
System.err.println(e.getMessage());
|
158
|
e.printStackTrace(System.err);
|
159
|
}
|
160
|
}
|
161
|
}
|
162
|
|
163
|
/**
|
164
|
* construct an instance of the DBQuery class
|
165
|
*
|
166
|
* <p>Generally, one would call the findDocuments() routine after creating
|
167
|
* an instance to specify the search query</p>
|
168
|
*
|
169
|
* @param conn the JDBC connection that we use for the query
|
170
|
* @param parserName the fully qualified name of a Java class implementing
|
171
|
* the org.xml.sax.XMLReader interface
|
172
|
*/
|
173
|
public DBQuery( Connection conn, String parserName )
|
174
|
throws IOException,
|
175
|
SQLException,
|
176
|
ClassNotFoundException {
|
177
|
this.conn = conn;
|
178
|
this.parserName = parserName;
|
179
|
}
|
180
|
|
181
|
/**
|
182
|
* routine to search the elements and attributes looking to match query
|
183
|
*
|
184
|
* @param xmlquery the xml serialization of the query (@see pathquery.dtd)
|
185
|
* @param user the username of the user
|
186
|
* @param group the group of the user
|
187
|
*/
|
188
|
public Hashtable findDocuments(Reader xmlquery, String user, String[] groups)
|
189
|
{
|
190
|
return findDocuments(xmlquery, user, groups, true);
|
191
|
}
|
192
|
|
193
|
/**
|
194
|
* routine to search the elements and attributes looking to match query
|
195
|
*
|
196
|
* @param xmlquery the xml serialization of the query (@see pathquery.dtd)
|
197
|
* @param user the username of the user
|
198
|
* @param group the group of the user
|
199
|
* @param useXMLIndex flag whether to search using the path index
|
200
|
*/
|
201
|
public Hashtable findDocuments(Reader xmlquery, String user, String[] groups,
|
202
|
boolean useXMLIndex)
|
203
|
{
|
204
|
Hashtable docListResult = new Hashtable();
|
205
|
PreparedStatement pstmt = null;
|
206
|
String docid = null;
|
207
|
String docname = null;
|
208
|
String doctype = null;
|
209
|
String createDate = null;
|
210
|
String updateDate = null;
|
211
|
String fieldname = null;
|
212
|
String fielddata = null;
|
213
|
String relation = null;
|
214
|
Connection dbconn = null;
|
215
|
Connection dbconn2 = null;
|
216
|
int rev = 0;
|
217
|
StringBuffer document = null;
|
218
|
|
219
|
try {
|
220
|
if (conn == null || conn.isClosed()) {
|
221
|
dbconn = util.openDBConnection();
|
222
|
} else {
|
223
|
dbconn = conn;
|
224
|
}
|
225
|
// problem with ODBC driver multi-threading
|
226
|
// dbconn2 = util.openDBConnection(); // for use by AccessControlList
|
227
|
|
228
|
// Get the XML query and covert it into a SQL statment
|
229
|
QuerySpecification qspec = new QuerySpecification(xmlquery,
|
230
|
parserName,
|
231
|
util.getOption("accNumSeparator"));
|
232
|
//System.out.println(qspec.printSQL(useXMLIndex));
|
233
|
pstmt = dbconn.prepareStatement( qspec.printSQL(useXMLIndex) );
|
234
|
|
235
|
// Execute the SQL query using the JDBC connection
|
236
|
pstmt.execute();
|
237
|
ResultSet rs = pstmt.getResultSet();
|
238
|
boolean tableHasRows = rs.next();
|
239
|
while (tableHasRows)
|
240
|
{
|
241
|
docid = rs.getString(1).trim();
|
242
|
if ( !hasPermission(dbconn, user, groups, docid) ) {
|
243
|
// Advance to the next record in the cursor
|
244
|
tableHasRows = rs.next();
|
245
|
continue;
|
246
|
}
|
247
|
docname = rs.getString(2);
|
248
|
doctype = rs.getString(3);
|
249
|
createDate = rs.getString(4);
|
250
|
updateDate = rs.getString(5);
|
251
|
rev = rs.getInt(6);
|
252
|
|
253
|
// if there are returndocs to match, backtracking can be performed
|
254
|
// otherwise, just return the document that was hit
|
255
|
Vector returndocVec = qspec.getReturnDocList();
|
256
|
if (returndocVec.size() != 0 && !returndocVec.contains(doctype))
|
257
|
{
|
258
|
MetaCatUtil.debugMessage("Back tracing now...");
|
259
|
String sep = util.getOption("accNumSeparator");
|
260
|
StringBuffer btBuf = new StringBuffer();
|
261
|
btBuf.append("select docid from xml_relation where ");
|
262
|
|
263
|
//build the doctype list for the backtracking sql statement
|
264
|
btBuf.append("packagetype in (");
|
265
|
for(int i=0; i<returndocVec.size(); i++)
|
266
|
{
|
267
|
btBuf.append("'").append((String)returndocVec.get(i)).append("'");
|
268
|
if (i != (returndocVec.size() - 1))
|
269
|
{
|
270
|
btBuf.append(", ");
|
271
|
}
|
272
|
}
|
273
|
btBuf.append(") ");
|
274
|
|
275
|
btBuf.append("and (subject like '");
|
276
|
btBuf.append(docid).append(sep).append(rev).append("'");
|
277
|
btBuf.append("or object like '");
|
278
|
btBuf.append(docid).append(sep).append(rev).append("')");
|
279
|
|
280
|
PreparedStatement npstmt = dbconn.
|
281
|
prepareStatement(btBuf.toString());
|
282
|
npstmt.execute();
|
283
|
ResultSet btrs = npstmt.getResultSet();
|
284
|
boolean hasBtRows = btrs.next();
|
285
|
while (hasBtRows)
|
286
|
{ //there was a backtrackable document found
|
287
|
DocumentImpl xmldoc = null;
|
288
|
String packageDocid = btrs.getString(1);
|
289
|
util.debugMessage("Getting document for docid: " + packageDocid);
|
290
|
try
|
291
|
{
|
292
|
// THIS CONSTRUCTOR BUILDS THE WHOLE XML doc not needed here
|
293
|
// xmldoc = new DocumentImpl(dbconn, packageDocid);
|
294
|
// thus use the following to get the doc info only
|
295
|
// xmldoc = new DocumentImpl(dbconn);
|
296
|
xmldoc = new DocumentImpl(dbconn, packageDocid, false);
|
297
|
if (xmldoc == null) {
|
298
|
util.debugMessage("Document was null for: " + packageDocid);
|
299
|
}
|
300
|
}
|
301
|
catch(Exception e)
|
302
|
{
|
303
|
System.out.println("Error getting document in " +
|
304
|
"DBQuery.findDocuments: " + e.getMessage());
|
305
|
}
|
306
|
|
307
|
String docid_org = xmldoc.getDocID();
|
308
|
if (docid_org == null) {
|
309
|
util.debugMessage("Docid_org was null.");
|
310
|
}
|
311
|
docid = docid_org.trim();
|
312
|
docname = xmldoc.getDocname();
|
313
|
doctype = xmldoc.getDoctype();
|
314
|
createDate = xmldoc.getCreateDate();
|
315
|
updateDate = xmldoc.getUpdateDate();
|
316
|
rev = xmldoc.getRev();
|
317
|
|
318
|
document = new StringBuffer();
|
319
|
|
320
|
String completeDocid = docid + util.getOption("accNumSeparator");
|
321
|
completeDocid += rev;
|
322
|
document.append("<docid>").append(completeDocid);
|
323
|
document.append("</docid>");
|
324
|
if (docname != null) {
|
325
|
document.append("<docname>" + docname + "</docname>");
|
326
|
}
|
327
|
if (doctype != null) {
|
328
|
document.append("<doctype>" + doctype + "</doctype>");
|
329
|
}
|
330
|
if (createDate != null) {
|
331
|
document.append("<createdate>" + createDate + "</createdate>");
|
332
|
}
|
333
|
if (updateDate != null) {
|
334
|
document.append("<updatedate>" + updateDate + "</updatedate>");
|
335
|
}
|
336
|
// Store the document id and the root node id
|
337
|
docListResult.put(docid,(String)document.toString());
|
338
|
|
339
|
// Get the next package document linked to our hit
|
340
|
hasBtRows = btrs.next();
|
341
|
}
|
342
|
npstmt.close();
|
343
|
btrs.close();
|
344
|
} else {
|
345
|
|
346
|
document = new StringBuffer();
|
347
|
|
348
|
String completeDocid = docid + util.getOption("accNumSeparator");
|
349
|
completeDocid += rev;
|
350
|
document.append("<docid>").append(completeDocid).append("</docid>");
|
351
|
if (docname != null) {
|
352
|
document.append("<docname>" + docname + "</docname>");
|
353
|
}
|
354
|
if (doctype != null) {
|
355
|
document.append("<doctype>" + doctype + "</doctype>");
|
356
|
}
|
357
|
if (createDate != null) {
|
358
|
document.append("<createdate>" + createDate + "</createdate>");
|
359
|
}
|
360
|
if (updateDate != null) {
|
361
|
document.append("<updatedate>" + updateDate + "</updatedate>");
|
362
|
}
|
363
|
// Store the document id and the root node id
|
364
|
docListResult.put(docid,(String)document.toString());
|
365
|
|
366
|
}
|
367
|
|
368
|
// Advance to the next record in the cursor
|
369
|
tableHasRows = rs.next();
|
370
|
}
|
371
|
rs.close();
|
372
|
pstmt.close();
|
373
|
|
374
|
if (qspec.containsExtendedSQL())
|
375
|
{
|
376
|
Vector extendedFields = new Vector(qspec.getReturnFieldList());
|
377
|
Vector results = new Vector();
|
378
|
Enumeration keylist = docListResult.keys();
|
379
|
StringBuffer doclist = new StringBuffer();
|
380
|
while(keylist.hasMoreElements())
|
381
|
{
|
382
|
doclist.append("'");
|
383
|
doclist.append((String)keylist.nextElement());
|
384
|
doclist.append("',");
|
385
|
}
|
386
|
if (doclist.length() > 0) {
|
387
|
doclist.deleteCharAt(doclist.length()-1); //remove the last comma
|
388
|
//pstmt.close();
|
389
|
pstmt = dbconn.prepareStatement(qspec.printExtendedSQL(
|
390
|
doclist.toString()));
|
391
|
pstmt.execute();
|
392
|
rs = pstmt.getResultSet();
|
393
|
tableHasRows = rs.next();
|
394
|
while(tableHasRows)
|
395
|
{
|
396
|
docid = rs.getString(1).trim();
|
397
|
if ( !hasPermission(dbconn, user, groups, docid) ) {
|
398
|
// Advance to the next record in the cursor
|
399
|
tableHasRows = rs.next();
|
400
|
continue;
|
401
|
}
|
402
|
fieldname = rs.getString(2);
|
403
|
fielddata = rs.getString(3);
|
404
|
|
405
|
document = new StringBuffer();
|
406
|
|
407
|
document.append("<param name=\"");
|
408
|
document.append(fieldname);
|
409
|
document.append("\">");
|
410
|
document.append(fielddata);
|
411
|
document.append("</param>");
|
412
|
|
413
|
tableHasRows = rs.next();
|
414
|
if (docListResult.containsKey(docid))
|
415
|
{
|
416
|
String removedelement = (String)docListResult.remove(docid);
|
417
|
docListResult.put(docid, removedelement + document.toString());
|
418
|
}
|
419
|
else
|
420
|
{
|
421
|
docListResult.put(docid, document.toString());
|
422
|
}
|
423
|
}
|
424
|
}
|
425
|
rs.close();
|
426
|
}
|
427
|
pstmt.close();
|
428
|
|
429
|
//this loop adds the relation data to the resultdoc
|
430
|
//this code might be able to be added to the backtracking code above
|
431
|
Enumeration docidkeys = docListResult.keys();
|
432
|
while(docidkeys.hasMoreElements())
|
433
|
{
|
434
|
//String connstring = "metacat://"+util.getOption("server")+"?docid=";
|
435
|
String connstring = "%docid=";
|
436
|
String docidkey = (String)docidkeys.nextElement();
|
437
|
pstmt = dbconn.prepareStatement(qspec.printRelationSQL(docidkey));
|
438
|
pstmt.execute();
|
439
|
rs = pstmt.getResultSet();
|
440
|
tableHasRows = rs.next();
|
441
|
while(tableHasRows)
|
442
|
{
|
443
|
String sub = rs.getString(1);
|
444
|
String rel = rs.getString(2);
|
445
|
String obj = rs.getString(3);
|
446
|
String subDT = rs.getString(4);
|
447
|
String objDT = rs.getString(5);
|
448
|
|
449
|
document = new StringBuffer();
|
450
|
document.append("<triple>");
|
451
|
document.append("<subject>").append(MetaCatUtil.normalize(sub));
|
452
|
document.append("</subject>");
|
453
|
if ( subDT != null ) {
|
454
|
document.append("<subjectdoctype>").append(subDT);
|
455
|
document.append("</subjectdoctype>");
|
456
|
}
|
457
|
document.append("<relationship>").
|
458
|
append(MetaCatUtil.normalize(rel));
|
459
|
document.append("</relationship>");
|
460
|
document.append("<object>").append(MetaCatUtil.normalize(obj));
|
461
|
document.append("</object>");
|
462
|
if ( objDT != null ) {
|
463
|
document.append("<objectdoctype>").append(objDT);
|
464
|
document.append("</objectdoctype>");
|
465
|
}
|
466
|
document.append("</triple>");
|
467
|
|
468
|
String removedelement = (String)docListResult.remove(docidkey);
|
469
|
docListResult.put(docidkey, removedelement +
|
470
|
document.toString());
|
471
|
tableHasRows = rs.next();
|
472
|
}
|
473
|
rs.close();
|
474
|
pstmt.close();
|
475
|
}
|
476
|
|
477
|
} catch (SQLException e) {
|
478
|
System.err.println("SQL Error in DBQuery.findDocuments: " +
|
479
|
e.getMessage());
|
480
|
} catch (IOException ioe) {
|
481
|
System.err.println("IO error in DBQuery.findDocuments:");
|
482
|
System.err.println(ioe.getMessage());
|
483
|
} catch (Exception ee) {
|
484
|
System.err.println("Exception in DBQuery.findDocuments: " +
|
485
|
ee.getMessage());
|
486
|
ee.printStackTrace(System.err);
|
487
|
}
|
488
|
finally {
|
489
|
try
|
490
|
{
|
491
|
dbconn.close();
|
492
|
// dbconn2.close();
|
493
|
}
|
494
|
catch(SQLException sqle)
|
495
|
{
|
496
|
System.out.println("error closing conn in DBQuery.findDocuments");
|
497
|
}
|
498
|
}
|
499
|
//System.out.println("docListResult: ");
|
500
|
//System.out.println(docListResult.toString());
|
501
|
return docListResult;
|
502
|
}
|
503
|
|
504
|
/**
|
505
|
* returns a string array of the contents of a particular node.
|
506
|
* If the node appears more than once, the contents are returned
|
507
|
* in the order in which they appearred in the document.
|
508
|
* @param nodename the name or path of the particular node.
|
509
|
* @param docid the docid of the document you want the node from.
|
510
|
* @param conn a database connection-this allows this method to be static
|
511
|
*/
|
512
|
public static Object[] getNodeContent(String nodename, String docid,
|
513
|
Connection conn)
|
514
|
{
|
515
|
StringBuffer query = new StringBuffer();
|
516
|
Vector result = new Vector();
|
517
|
PreparedStatement pstmt = null;
|
518
|
query.append("select nodedata from xml_nodes where parentnodeid in ");
|
519
|
query.append("(select nodeid from xml_index where path like '");
|
520
|
query.append(nodename);
|
521
|
query.append("' and docid like '").append(docid).append("')");
|
522
|
try
|
523
|
{
|
524
|
pstmt = conn.prepareStatement(query.toString());
|
525
|
|
526
|
// Execute the SQL query using the JDBC connection
|
527
|
pstmt.execute();
|
528
|
ResultSet rs = pstmt.getResultSet();
|
529
|
boolean tableHasRows = rs.next();
|
530
|
while (tableHasRows)
|
531
|
{
|
532
|
result.add(rs.getString(1));
|
533
|
System.out.println(rs.getString(1));
|
534
|
tableHasRows = rs.next();
|
535
|
}
|
536
|
}
|
537
|
catch (SQLException e)
|
538
|
{
|
539
|
System.err.println("Error in DBQuery.getNodeContent: " + e.getMessage());
|
540
|
} finally {
|
541
|
try
|
542
|
{
|
543
|
pstmt.close();
|
544
|
}
|
545
|
catch(SQLException sqle) {}
|
546
|
}
|
547
|
return result.toArray();
|
548
|
}
|
549
|
|
550
|
/**
|
551
|
* format a structured query as an XML document that conforms
|
552
|
* to the pathquery.dtd and is appropriate for submission to the DBQuery
|
553
|
* structured query engine
|
554
|
*
|
555
|
* @param params The list of parameters that should be included in the query
|
556
|
*/
|
557
|
public static String createSQuery(Hashtable params)
|
558
|
{
|
559
|
StringBuffer query = new StringBuffer();
|
560
|
Enumeration elements;
|
561
|
Enumeration keys;
|
562
|
String filterDoctype = null;
|
563
|
String casesensitive = null;
|
564
|
String searchmode = null;
|
565
|
Object nextkey;
|
566
|
Object nextelement;
|
567
|
//add the xml headers
|
568
|
query.append("<?xml version=\"1.0\"?>\n");
|
569
|
query.append("<pathquery version=\"1.0\">\n");
|
570
|
|
571
|
if (params.containsKey("meta_file_id"))
|
572
|
{
|
573
|
query.append("<meta_file_id>");
|
574
|
query.append( ((String[])params.get("meta_file_id"))[0]);
|
575
|
query.append("</meta_file_id>");
|
576
|
}
|
577
|
|
578
|
if (params.containsKey("returndoctype"))
|
579
|
{
|
580
|
String[] returnDoctypes = ((String[])params.get("returndoctype"));
|
581
|
for(int i=0; i<returnDoctypes.length; i++)
|
582
|
{
|
583
|
String doctype = (String)returnDoctypes[i];
|
584
|
|
585
|
if (!doctype.equals("any") &&
|
586
|
!doctype.equals("ANY") &&
|
587
|
!doctype.equals("") )
|
588
|
{
|
589
|
query.append("<returndoctype>").append(doctype);
|
590
|
query.append("</returndoctype>");
|
591
|
}
|
592
|
}
|
593
|
}
|
594
|
|
595
|
if (params.containsKey("filterdoctype"))
|
596
|
{
|
597
|
String[] filterDoctypes = ((String[])params.get("filterdoctype"));
|
598
|
for(int i=0; i<filterDoctypes.length; i++)
|
599
|
{
|
600
|
query.append("<filterdoctype>").append(filterDoctypes[i]);
|
601
|
query.append("</filterdoctype>");
|
602
|
}
|
603
|
}
|
604
|
|
605
|
if (params.containsKey("returnfield"))
|
606
|
{
|
607
|
String[] returnfield = ((String[])params.get("returnfield"));
|
608
|
for(int i=0; i<returnfield.length; i++)
|
609
|
{
|
610
|
query.append("<returnfield>").append(returnfield[i]);
|
611
|
query.append("</returnfield>");
|
612
|
}
|
613
|
}
|
614
|
|
615
|
if (params.containsKey("owner"))
|
616
|
{
|
617
|
String[] owner = ((String[])params.get("owner"));
|
618
|
for(int i=0; i<owner.length; i++)
|
619
|
{
|
620
|
query.append("<owner>").append(owner[i]);
|
621
|
query.append("</owner>");
|
622
|
}
|
623
|
}
|
624
|
|
625
|
if (params.containsKey("site"))
|
626
|
{
|
627
|
String[] site = ((String[])params.get("site"));
|
628
|
for(int i=0; i<site.length; i++)
|
629
|
{
|
630
|
query.append("<site>").append(site[i]);
|
631
|
query.append("</site>");
|
632
|
}
|
633
|
}
|
634
|
|
635
|
//allows the dynamic switching of boolean operators
|
636
|
if (params.containsKey("operator"))
|
637
|
{
|
638
|
query.append("<querygroup operator=\"" +
|
639
|
((String[])params.get("operator"))[0] + "\">");
|
640
|
}
|
641
|
else
|
642
|
{ //the default operator is UNION
|
643
|
query.append("<querygroup operator=\"UNION\">");
|
644
|
}
|
645
|
|
646
|
if (params.containsKey("casesensitive"))
|
647
|
{
|
648
|
casesensitive = ((String[])params.get("casesensitive"))[0];
|
649
|
}
|
650
|
else
|
651
|
{
|
652
|
casesensitive = "false";
|
653
|
}
|
654
|
|
655
|
if (params.containsKey("searchmode"))
|
656
|
{
|
657
|
searchmode = ((String[])params.get("searchmode"))[0];
|
658
|
}
|
659
|
else
|
660
|
{
|
661
|
searchmode = "contains";
|
662
|
}
|
663
|
|
664
|
//anyfield is a special case because it does a
|
665
|
//free text search. It does not have a <pathexpr>
|
666
|
//tag. This allows for a free text search within the structured
|
667
|
//query. This is useful if the INTERSECT operator is used.
|
668
|
if (params.containsKey("anyfield"))
|
669
|
{
|
670
|
String[] anyfield = ((String[])params.get("anyfield"));
|
671
|
//allow for more than one value for anyfield
|
672
|
for(int i=0; i<anyfield.length; i++)
|
673
|
{
|
674
|
if (!anyfield[i].equals(""))
|
675
|
{
|
676
|
query.append("<queryterm casesensitive=\"" + casesensitive +
|
677
|
"\" " + "searchmode=\"" + searchmode + "\"><value>" +
|
678
|
anyfield[i] +
|
679
|
"</value></queryterm>");
|
680
|
}
|
681
|
}
|
682
|
}
|
683
|
|
684
|
//this while loop finds the rest of the parameters
|
685
|
//and attempts to query for the field specified
|
686
|
//by the parameter.
|
687
|
elements = params.elements();
|
688
|
keys = params.keys();
|
689
|
while(keys.hasMoreElements() && elements.hasMoreElements())
|
690
|
{
|
691
|
nextkey = keys.nextElement();
|
692
|
nextelement = elements.nextElement();
|
693
|
|
694
|
//make sure we aren't querying for any of these
|
695
|
//parameters since the are already in the query
|
696
|
//in one form or another.
|
697
|
if (!nextkey.toString().equals("returndoctype") &&
|
698
|
!nextkey.toString().equals("filterdoctype") &&
|
699
|
!nextkey.toString().equals("action") &&
|
700
|
!nextkey.toString().equals("qformat") &&
|
701
|
!nextkey.toString().equals("anyfield") &&
|
702
|
!nextkey.toString().equals("returnfield") &&
|
703
|
!nextkey.toString().equals("owner") &&
|
704
|
!nextkey.toString().equals("site") &&
|
705
|
!nextkey.toString().equals("operator") )
|
706
|
{
|
707
|
//allow for more than value per field name
|
708
|
for(int i=0; i<((String[])nextelement).length; i++)
|
709
|
{
|
710
|
if (!((String[])nextelement)[i].equals(""))
|
711
|
{
|
712
|
query.append("<queryterm casesensitive=\"" + casesensitive +"\" " +
|
713
|
"searchmode=\"" + searchmode + "\">" +
|
714
|
"<value>" +
|
715
|
//add the query value
|
716
|
((String[])nextelement)[i] +
|
717
|
"</value><pathexpr>" +
|
718
|
//add the path to query by
|
719
|
nextkey.toString() +
|
720
|
"</pathexpr></queryterm>");
|
721
|
}
|
722
|
}
|
723
|
}
|
724
|
}
|
725
|
query.append("</querygroup></pathquery>");
|
726
|
//append on the end of the xml and return the result as a string
|
727
|
return query.toString();
|
728
|
}
|
729
|
|
730
|
/**
|
731
|
* format a simple free-text value query as an XML document that conforms
|
732
|
* to the pathquery.dtd and is appropriate for submission to the DBQuery
|
733
|
* structured query engine
|
734
|
*
|
735
|
* @param value the text string to search for in the xml catalog
|
736
|
* @param doctype the type of documents to include in the result set -- use
|
737
|
* "any" or "ANY" for unfiltered result sets
|
738
|
*/
|
739
|
public static String createQuery(String value, String doctype) {
|
740
|
StringBuffer xmlquery = new StringBuffer();
|
741
|
xmlquery.append("<?xml version=\"1.0\"?>\n");
|
742
|
xmlquery.append("<pathquery version=\"1.0\">");
|
743
|
|
744
|
if (!doctype.equals("any") && !doctype.equals("ANY")) {
|
745
|
xmlquery.append("<returndoctype>");
|
746
|
xmlquery.append(doctype).append("</returndoctype>");
|
747
|
}
|
748
|
|
749
|
xmlquery.append("<querygroup operator=\"UNION\">");
|
750
|
//chad added - 8/14
|
751
|
//the if statement allows a query to gracefully handle a null
|
752
|
//query. Without this if a nullpointerException is thrown.
|
753
|
if (!value.equals(""))
|
754
|
{
|
755
|
xmlquery.append("<queryterm casesensitive=\"false\" ");
|
756
|
xmlquery.append("searchmode=\"contains\">");
|
757
|
xmlquery.append("<value>").append(value).append("</value>");
|
758
|
xmlquery.append("</queryterm>");
|
759
|
}
|
760
|
xmlquery.append("</querygroup>");
|
761
|
xmlquery.append("</pathquery>");
|
762
|
|
763
|
|
764
|
return (xmlquery.toString());
|
765
|
}
|
766
|
|
767
|
/**
|
768
|
* format a simple free-text value query as an XML document that conforms
|
769
|
* to the pathquery.dtd and is appropriate for submission to the DBQuery
|
770
|
* structured query engine
|
771
|
*
|
772
|
* @param value the text string to search for in the xml catalog
|
773
|
*/
|
774
|
public static String createQuery(String value) {
|
775
|
return createQuery(value, "any");
|
776
|
}
|
777
|
|
778
|
/**
|
779
|
* Check for "READ" permission on @docid for @user and/or @group
|
780
|
* from DB connection
|
781
|
*/
|
782
|
private boolean hasPermission ( Connection conn, String user,
|
783
|
String[] groups, String docid )
|
784
|
throws SQLException, Exception
|
785
|
{
|
786
|
// Check for READ permission on @docid for @user and/or @groups
|
787
|
AccessControlList aclobj = new AccessControlList(conn);
|
788
|
return aclobj.hasPermission("READ", user, groups, docid);
|
789
|
}
|
790
|
|
791
|
/**
|
792
|
* Get all docIds list for a data packadge
|
793
|
* @param dataPackageDocid, the string in docId field of xml_relation table
|
794
|
*/
|
795
|
private Vector getCurrentDocidListForDataPackage(String dataPackageDocid)
|
796
|
{
|
797
|
Vector docIdList=new Vector();//return value
|
798
|
PreparedStatement pStmt;
|
799
|
ResultSet rs=null;
|
800
|
String docIdInSubjectField=null;
|
801
|
String docIdInObjectField=null;
|
802
|
//the query stirng
|
803
|
String query="SELECT subject, object from xml_relation where docId = ?";
|
804
|
try
|
805
|
{
|
806
|
pStmt=conn.prepareStatement(query);
|
807
|
//bind the value to query
|
808
|
pStmt.setString(1, dataPackageDocid);
|
809
|
|
810
|
//excute the query
|
811
|
pStmt.execute();
|
812
|
//get the result set
|
813
|
rs=pStmt.getResultSet();
|
814
|
//process the result
|
815
|
while (rs.next())
|
816
|
{
|
817
|
//In order to get the whole docIds in a data packadge,
|
818
|
//we need to put the docIds of subject and object field in xml_relation
|
819
|
//into the return vector
|
820
|
docIdInSubjectField=rs.getString(1);//the result docId in subject field
|
821
|
docIdInObjectField=rs.getString(2);//the result docId in object field
|
822
|
|
823
|
//don't put the duplicate docId into the vector
|
824
|
if (!docIdList.contains(docIdInSubjectField))
|
825
|
{
|
826
|
docIdList.add(docIdInSubjectField);
|
827
|
}
|
828
|
|
829
|
//don't put the duplicate docId into the vector
|
830
|
if (!docIdList.contains(docIdInObjectField))
|
831
|
{
|
832
|
docIdList.add(docIdInObjectField);
|
833
|
}
|
834
|
}//while
|
835
|
//close the pStmt
|
836
|
pStmt.close();
|
837
|
}//try
|
838
|
catch (SQLException e)
|
839
|
{
|
840
|
util.debugMessage("Error in getDocidListForDataPackage: "
|
841
|
+e.getMessage());
|
842
|
}//catch
|
843
|
return docIdList;
|
844
|
}//getCurrentDocidListForDataPackadge()
|
845
|
|
846
|
/**
|
847
|
* Get all docIds list for a data packadge
|
848
|
* @param dataPackageDocid, the string in docId field of xml_relation table
|
849
|
*/
|
850
|
private Vector getOldVersionDocidListForDataPackage(String dataPackageDocid)
|
851
|
throws SQLException, McdbException,Exception
|
852
|
{
|
853
|
|
854
|
Vector docIdList=new Vector();//return value
|
855
|
Vector tripleList=null;
|
856
|
String xml=null;
|
857
|
Connection dbConn=null;
|
858
|
|
859
|
if (conn == null || conn.isClosed())
|
860
|
{
|
861
|
dbConn = util.openDBConnection();
|
862
|
}
|
863
|
else
|
864
|
{
|
865
|
dbConn=conn;
|
866
|
}
|
867
|
//initial a documentImpl object
|
868
|
DocumentImpl packageDocument = new DocumentImpl(dbConn, dataPackageDocid);
|
869
|
//transfer to documentImpl object to string
|
870
|
xml=packageDocument.toString();
|
871
|
|
872
|
//create a tripcollection object
|
873
|
TripleCollection tripleForPackage = new
|
874
|
TripleCollection(new StringReader(xml));
|
875
|
//get the vetor of triples
|
876
|
tripleList=tripleForPackage.getCollection();
|
877
|
|
878
|
for (int i= 0; i<tripleList.size(); i++)
|
879
|
{
|
880
|
//put subject docid into docIdlist without duplicate
|
881
|
if (!docIdList.contains(((Triple)tripleList.elementAt(i)).getSubject()))
|
882
|
{
|
883
|
//put subject docid into docIdlist
|
884
|
docIdList.add(((Triple)tripleList.get(i)).getSubject());
|
885
|
}
|
886
|
//put object docid into docIdlist without duplicate
|
887
|
if (!docIdList.contains(((Triple)tripleList.elementAt(i)).getObject()))
|
888
|
{
|
889
|
docIdList.add(((Triple)(tripleList.get(i))).getObject());
|
890
|
}
|
891
|
}//for
|
892
|
|
893
|
return docIdList;
|
894
|
}//getDocidListForPackageInXMLRevisions()
|
895
|
|
896
|
/**
|
897
|
* Check if the docId is a data packadge id. If the id is a data packadage
|
898
|
*id, it should be store in the docId fields in xml_relation table.
|
899
|
*So we can use a query to get the entries which the docId equals the given
|
900
|
*value. If the result is null. The docId is not a packadge id. Otherwise,
|
901
|
* it is.
|
902
|
* @param docId, the id need to be checked
|
903
|
*/
|
904
|
private boolean isDataPackageId(String docId)
|
905
|
{
|
906
|
boolean result=false;
|
907
|
PreparedStatement pStmt;
|
908
|
ResultSet rs=null;
|
909
|
String query="SELECT docId from xml_relation where docId = ?";
|
910
|
try
|
911
|
{
|
912
|
pStmt=conn.prepareStatement(query);
|
913
|
//bind the value to query
|
914
|
pStmt.setString(1, docId);
|
915
|
//execute the query
|
916
|
pStmt.execute();
|
917
|
rs=pStmt.getResultSet();
|
918
|
//process the result
|
919
|
if (rs.next()) //There are some records for the id in docId fields
|
920
|
{
|
921
|
result=true;//It is a data packadge id
|
922
|
}
|
923
|
pStmt.close();
|
924
|
}//try
|
925
|
catch (SQLException e)
|
926
|
{
|
927
|
util.debugMessage("Error in getDocidListForDataPackadge: "
|
928
|
+e.getMessage());
|
929
|
}
|
930
|
return result;
|
931
|
}//isDataPackageId()
|
932
|
|
933
|
/**
|
934
|
* Check if the user has the permission to export data package
|
935
|
* @param conn, the connection
|
936
|
* @param docId, the id need to be checked
|
937
|
* @param user, the name of user
|
938
|
* @param groups, the user's group
|
939
|
*/
|
940
|
private boolean hasPermissionToExportPackage(Connection conn, String docId,
|
941
|
String user, String[] groups)
|
942
|
throws Exception
|
943
|
{
|
944
|
DocumentImpl doc=new DocumentImpl(conn,docId);
|
945
|
return doc.hasReadPermission(conn, user, groups,docId);
|
946
|
}
|
947
|
|
948
|
/**
|
949
|
*Get the current Rev for a docid in xml_documents table
|
950
|
* @param docId, the id need to get version numb
|
951
|
* If the return value is -5, means no value in rev field for this docid
|
952
|
*/
|
953
|
private int getCurrentRevFromXMLDoumentsTable(String docId)
|
954
|
{
|
955
|
int rev=-5;
|
956
|
PreparedStatement pStmt;
|
957
|
ResultSet rs=null;
|
958
|
String query="SELECT rev from xml_documents where docId = ?";
|
959
|
try
|
960
|
{
|
961
|
pStmt=conn.prepareStatement(query);
|
962
|
//bind the value to query
|
963
|
pStmt.setString(1, docId);
|
964
|
//execute the query
|
965
|
pStmt.execute();
|
966
|
rs=pStmt.getResultSet();
|
967
|
//process the result
|
968
|
if (rs.next()) //There are some records for rev
|
969
|
{
|
970
|
rev=rs.getInt(1);;//It is the version for given docid
|
971
|
}
|
972
|
else
|
973
|
{
|
974
|
rev=-5;
|
975
|
}
|
976
|
pStmt.close();
|
977
|
}//try
|
978
|
catch (SQLException e)
|
979
|
{
|
980
|
util.debugMessage("Error in getDocidListForDataPackadge: "
|
981
|
+e.getMessage());
|
982
|
}
|
983
|
return rev;
|
984
|
}//getCurrentRevFromXMLDoumentsTable
|
985
|
|
986
|
/**
|
987
|
*put a doc into a zip output stream
|
988
|
*@param docImpl, docmentImpl object which will be sent to zip output stream
|
989
|
*@param zipOut, zip output stream which the docImpl will be put
|
990
|
*@param packageZipEntry, the zip entry name for whole package
|
991
|
*/
|
992
|
private void addDocToZipOutputStream(DocumentImpl docImpl,
|
993
|
ZipOutputStream zipOut, String packageZipEntry)
|
994
|
throws ClassNotFoundException, IOException, SQLException,
|
995
|
McdbException, Exception
|
996
|
{
|
997
|
byte[] byteString = null;
|
998
|
ZipEntry zEntry = null;
|
999
|
|
1000
|
byteString = docImpl.toString().getBytes();
|
1001
|
//use docId as the zip entry's name
|
1002
|
zEntry = new ZipEntry(packageZipEntry+"/metadata/"+docImpl.getDocID());
|
1003
|
zEntry.setSize(byteString.length);
|
1004
|
zipOut.putNextEntry(zEntry);
|
1005
|
zipOut.write(byteString, 0, byteString.length);
|
1006
|
zipOut.closeEntry();
|
1007
|
|
1008
|
}//addDocToZipOutputStream()
|
1009
|
|
1010
|
|
1011
|
/**
|
1012
|
*transfer a docid vetor to a documentImpl vector. The documentImpl vetor
|
1013
|
*only inlcudes current version
|
1014
|
*@param docIdList, a vetor hold a docid list for a data package. In docid,
|
1015
|
*there is not version number in it.
|
1016
|
*/
|
1017
|
|
1018
|
private Vector getCurrentAllDocumentImpl( Vector docIdList)
|
1019
|
throws McdbException,Exception
|
1020
|
{
|
1021
|
Connection dbConn=null;
|
1022
|
Vector documentImplList=new Vector();
|
1023
|
int rev=0;
|
1024
|
|
1025
|
if (conn == null || conn.isClosed())
|
1026
|
{
|
1027
|
dbConn = util.openDBConnection();
|
1028
|
}
|
1029
|
else
|
1030
|
{
|
1031
|
dbConn=conn;
|
1032
|
}
|
1033
|
//for every docid in vector
|
1034
|
for (int i=0;i<docIdList.size();i++)
|
1035
|
{
|
1036
|
//get newest version for this docId
|
1037
|
rev=getCurrentRevFromXMLDoumentsTable((String)docIdList.elementAt(i));
|
1038
|
|
1039
|
String docidPlusVersion=((String)docIdList.elementAt(i))
|
1040
|
+util.getOption("accNumSeparator")+rev;
|
1041
|
//create new documentImpl object
|
1042
|
DocumentImpl documentImplObject =
|
1043
|
new DocumentImpl(dbConn,docidPlusVersion);
|
1044
|
//add them to vector
|
1045
|
documentImplList.add(documentImplObject);
|
1046
|
|
1047
|
}//for
|
1048
|
return documentImplList;
|
1049
|
}
|
1050
|
|
1051
|
/**
|
1052
|
*transfer a docid vetor to a documentImpl vector. The documentImpl vetor
|
1053
|
*does not inlcude old version
|
1054
|
*@param docIdList, a vetor hold a docid list for a data package. In docid,
|
1055
|
*there is version number in it.
|
1056
|
*/
|
1057
|
private Vector getOldVersionAllDocumentImpl( Vector docIdList)
|
1058
|
throws McdbException,Exception
|
1059
|
{
|
1060
|
Connection dbConn=null;
|
1061
|
Vector documentImplList=new Vector();
|
1062
|
String siteCode=null;
|
1063
|
String uniqueId=null;
|
1064
|
int rev=0;
|
1065
|
|
1066
|
if (conn == null || conn.isClosed())
|
1067
|
{
|
1068
|
dbConn = util.openDBConnection();
|
1069
|
}
|
1070
|
else
|
1071
|
{
|
1072
|
dbConn=conn;
|
1073
|
}
|
1074
|
//for every docid in vector
|
1075
|
for (int i=0;i<docIdList.size();i++)
|
1076
|
{
|
1077
|
|
1078
|
String docidPlusVersion=(String)(docIdList.elementAt(i));
|
1079
|
//create new documentImpl object
|
1080
|
DocumentImpl documentImplObject =
|
1081
|
new DocumentImpl(dbConn,docidPlusVersion);
|
1082
|
//add them to vector
|
1083
|
documentImplList.add(documentImplObject);
|
1084
|
|
1085
|
}//for
|
1086
|
return documentImplList;
|
1087
|
}
|
1088
|
/**
|
1089
|
*put a data file into a zip output stream
|
1090
|
*@param docImpl, docmentImpl object which will be sent to zip output stream
|
1091
|
*@param zipOut, the zip output stream which the docImpl will be put
|
1092
|
*@param packageZipEntry, the zip entry name for whole package
|
1093
|
*/
|
1094
|
private void addDataFileToZipOutputStream(DocumentImpl docImpl,
|
1095
|
ZipOutputStream zipOut, String packageZipEntry)
|
1096
|
throws ClassNotFoundException, IOException, SQLException,
|
1097
|
McdbException, Exception
|
1098
|
{
|
1099
|
byte[] byteString = null;
|
1100
|
ZipEntry zEntry = null;
|
1101
|
// this is data file; add file to zip
|
1102
|
String filePath = util.getOption("datafilepath");
|
1103
|
if (!filePath.endsWith("/"))
|
1104
|
{
|
1105
|
filePath += "/";
|
1106
|
}
|
1107
|
String fileName = filePath + docImpl.getDocID();
|
1108
|
zEntry = new ZipEntry(packageZipEntry+"/data/"+docImpl.getDocID());
|
1109
|
zipOut.putNextEntry(zEntry);
|
1110
|
FileInputStream fin = null;
|
1111
|
try
|
1112
|
{
|
1113
|
fin = new FileInputStream(fileName);
|
1114
|
byte[] buf = new byte[4 * 1024]; // 4K buffer
|
1115
|
int b = fin.read(buf);
|
1116
|
while (b != -1)
|
1117
|
{
|
1118
|
zipOut.write(buf, 0, b);
|
1119
|
b = fin.read(buf);
|
1120
|
}//while
|
1121
|
zipOut.closeEntry();
|
1122
|
}//try
|
1123
|
catch (IOException ioe)
|
1124
|
{
|
1125
|
util.debugMessage("There is an exception: "+ioe.getMessage());
|
1126
|
}//catch
|
1127
|
}//addDataFileToZipOutputStream()
|
1128
|
|
1129
|
/**
|
1130
|
*create a html summary for data package and put it into zip output stream
|
1131
|
*@param docImplList, the documentImpl ojbects in data package
|
1132
|
*@param zipOut, the zip output stream which the html should be put
|
1133
|
*@param packageZipEntry, the zip entry name for whole package
|
1134
|
*/
|
1135
|
private void addHtmlSummaryToZipOutputStream(Vector docImplList,
|
1136
|
ZipOutputStream zipOut, String packageZipEntry)
|
1137
|
throws Exception
|
1138
|
{
|
1139
|
StringBuffer htmlDoc = new StringBuffer();
|
1140
|
ZipEntry zEntry = null;
|
1141
|
byte[] byteString=null;
|
1142
|
InputStream source;
|
1143
|
DBTransform xmlToHtml;
|
1144
|
Connection dbConn;
|
1145
|
|
1146
|
//get the connection to the database
|
1147
|
if (conn == null || conn.isClosed())
|
1148
|
{
|
1149
|
dbConn = util.openDBConnection();
|
1150
|
}
|
1151
|
else
|
1152
|
{
|
1153
|
dbConn=conn;
|
1154
|
}
|
1155
|
|
1156
|
//create a DBTransform ojbect
|
1157
|
xmlToHtml = new DBTransform(dbConn);
|
1158
|
//head of html
|
1159
|
htmlDoc.append("<html><head></head><body>");
|
1160
|
for (int i=0; i<docImplList.size(); i++)
|
1161
|
{
|
1162
|
if ((((DocumentImpl)docImplList.elementAt(i)).getDoctype()).
|
1163
|
compareTo("BIN")!=0)
|
1164
|
{ //this is an xml file so we can transform it.
|
1165
|
//transform each file individually then concatenate all of the
|
1166
|
//transformations together.
|
1167
|
|
1168
|
//for metadata xml title
|
1169
|
htmlDoc.append("<h2>");
|
1170
|
htmlDoc.append(((DocumentImpl)docImplList.elementAt(i)).getDocID());
|
1171
|
//htmlDoc.append(".");
|
1172
|
//htmlDoc.append(((DocumentImpl)docImplList.elementAt(i)).getRev());
|
1173
|
htmlDoc.append("</h2>");
|
1174
|
//do the actual transform
|
1175
|
StringWriter docString = new StringWriter();
|
1176
|
xmlToHtml.transformXMLDocument(
|
1177
|
((DocumentImpl)docImplList.elementAt(i)).toString(),
|
1178
|
"-//NCEAS//eml-generic//EN", "-//W3C//HTML//EN", "html", docString);
|
1179
|
htmlDoc.append(docString.toString());
|
1180
|
htmlDoc.append("<br><br><hr><br><br>");
|
1181
|
}//if
|
1182
|
else
|
1183
|
{ //this is a data file so we should link to it in the html
|
1184
|
htmlDoc.append("<a href=\"");
|
1185
|
String dataFileName = null;
|
1186
|
String dataFileid =((DocumentImpl)docImplList.elementAt(i)).getDocID();
|
1187
|
htmlDoc.append("./data/").append(dataFileid).append("\">");
|
1188
|
htmlDoc.append("Data File: ");
|
1189
|
htmlDoc.append(dataFileid).append("</a><br>");
|
1190
|
htmlDoc.append("<br><hr><br>");
|
1191
|
}//else
|
1192
|
}//for
|
1193
|
htmlDoc.append("</body></html>");
|
1194
|
byteString = htmlDoc.toString().getBytes();
|
1195
|
zEntry = new ZipEntry(packageZipEntry+"/metadata.html");
|
1196
|
zEntry.setSize(byteString.length);
|
1197
|
zipOut.putNextEntry(zEntry);
|
1198
|
zipOut.write(byteString, 0, byteString.length);
|
1199
|
zipOut.closeEntry();
|
1200
|
dbConn.close();
|
1201
|
|
1202
|
}//addHtmlSummaryToZipOutputStream
|
1203
|
|
1204
|
|
1205
|
|
1206
|
/**
|
1207
|
* put a data packadge into a zip output stream
|
1208
|
* @param docId, which the user want to put into zip output stream
|
1209
|
* @param out, a servletoutput stream which the zip output stream will be put
|
1210
|
* @param user, the username of the user
|
1211
|
* @param groups, the group of the user
|
1212
|
*/
|
1213
|
public ZipOutputStream getZippedPackage(String docIdString,
|
1214
|
ServletOutputStream out, String user, String[] groups)
|
1215
|
throws ClassNotFoundException, IOException, SQLException,
|
1216
|
McdbException, NumberFormatException, Exception
|
1217
|
{
|
1218
|
ZipOutputStream zOut = null;
|
1219
|
String elementDocid=null;
|
1220
|
DocumentImpl docImpls=null;
|
1221
|
Connection dbConn = null;
|
1222
|
Vector docIdList=new Vector();
|
1223
|
Vector documentImplList=new Vector();
|
1224
|
Vector htmlDocumentImplList=new Vector();
|
1225
|
String packageId=null;
|
1226
|
String rootName="package";//the package zip entry name
|
1227
|
|
1228
|
String docId=null;
|
1229
|
int version=-5;
|
1230
|
docId=MetaCatUtil.getDocIdFromString(docIdString);
|
1231
|
version=MetaCatUtil.getVersionFromString(docIdString);
|
1232
|
|
1233
|
//get the connection to the database
|
1234
|
if (conn == null || conn.isClosed())
|
1235
|
{
|
1236
|
dbConn = util.openDBConnection();
|
1237
|
}
|
1238
|
else
|
1239
|
{
|
1240
|
dbConn=conn;
|
1241
|
}
|
1242
|
|
1243
|
//check if the reqused docId is a data package id
|
1244
|
if (!isDataPackageId(docId))//if it is not, throw a exception
|
1245
|
{
|
1246
|
Exception e = new Exception("The request the doc id " +docIdString+
|
1247
|
" is not a data package id");
|
1248
|
dbConn.close();
|
1249
|
throw e;
|
1250
|
}
|
1251
|
else if(!hasPermissionToExportPackage(dbConn, docId, user, groups))
|
1252
|
{
|
1253
|
Exception e = new Exception("User " + user + " does not have permission"
|
1254
|
+" to export the data package " + docIdString);
|
1255
|
dbConn.close();
|
1256
|
throw e;
|
1257
|
}
|
1258
|
else //it is a packadge id
|
1259
|
{
|
1260
|
//store the package id
|
1261
|
packageId=docId;
|
1262
|
//If it is for current version
|
1263
|
if ((version ==-1)||version==getCurrentRevFromXMLDoumentsTable(packageId))
|
1264
|
{
|
1265
|
//get current version number
|
1266
|
version=getCurrentRevFromXMLDoumentsTable(packageId);
|
1267
|
//get package zip entry name
|
1268
|
//it should be docId.revsion.package
|
1269
|
rootName=packageId+util.getOption("accNumSeparator")+version+
|
1270
|
util.getOption("accNumSeparator")+"package";
|
1271
|
//get the whole id list for data packadge
|
1272
|
docIdList=getCurrentDocidListForDataPackage(packageId);
|
1273
|
//get the whole documentImple object
|
1274
|
documentImplList=getCurrentAllDocumentImpl(docIdList);
|
1275
|
|
1276
|
}//if
|
1277
|
else //for an old version
|
1278
|
{
|
1279
|
|
1280
|
rootName=docIdString+util.getOption("accNumSeparator")+"package";
|
1281
|
//get the whole id list for data packadge
|
1282
|
docIdList=getOldVersionDocidListForDataPackage(docIdString);
|
1283
|
|
1284
|
//get the whole documentImple object
|
1285
|
documentImplList=getOldVersionAllDocumentImpl(docIdList);
|
1286
|
}//else
|
1287
|
|
1288
|
|
1289
|
zOut = new ZipOutputStream(out);
|
1290
|
|
1291
|
//put every element into zip output stream
|
1292
|
for (int i=0; i < documentImplList.size(); i++ )
|
1293
|
{
|
1294
|
//create a docmentImpls object (represent xml doc) base on the docId
|
1295
|
docImpls=(DocumentImpl)documentImplList.elementAt(i);
|
1296
|
|
1297
|
//checking if the user has the permission to read the documents
|
1298
|
if (docImpls.hasReadPermission(dbConn,user,groups,docImpls.getDocID()))
|
1299
|
{
|
1300
|
//if the docImpls is metadata
|
1301
|
if ((docImpls.getDoctype()).compareTo("BIN")!=0)
|
1302
|
{
|
1303
|
//add metadata into zip output stream
|
1304
|
addDocToZipOutputStream(docImpls, zOut, rootName);
|
1305
|
//add the documentImpl into the vetor which will be used in html
|
1306
|
htmlDocumentImplList.add(docImpls);
|
1307
|
|
1308
|
}//if
|
1309
|
|
1310
|
else
|
1311
|
{
|
1312
|
|
1313
|
//it is data file
|
1314
|
addDataFileToZipOutputStream(docImpls, zOut, rootName);
|
1315
|
htmlDocumentImplList.add(docImpls);
|
1316
|
}//else
|
1317
|
}//if
|
1318
|
|
1319
|
}//for
|
1320
|
|
1321
|
//add html summary file
|
1322
|
addHtmlSummaryToZipOutputStream(htmlDocumentImplList, zOut, rootName);
|
1323
|
zOut.finish(); //terminate the zip file
|
1324
|
dbConn.close();
|
1325
|
return zOut;
|
1326
|
}//else
|
1327
|
}//getZippedPackage()
|
1328
|
|
1329
|
}
|