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: 2001-08-02 10:29:27 -0700 (Thu, 02 Aug 2001) $'
|
15
|
* '$Revision: 818 $'
|
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 java.io.*;
|
35
|
import java.util.Vector;
|
36
|
import java.net.URL;
|
37
|
import java.net.MalformedURLException;
|
38
|
import java.sql.*;
|
39
|
import java.util.Stack;
|
40
|
import java.util.Hashtable;
|
41
|
import java.util.Enumeration;
|
42
|
import java.io.File;
|
43
|
import java.io.FileWriter;
|
44
|
import java.io.BufferedWriter;
|
45
|
|
46
|
/**
|
47
|
* A Class that searches a relational DB for elements and
|
48
|
* attributes that have free text matches a query string,
|
49
|
* or structured query matches to a path specified node in the
|
50
|
* XML hierarchy. It returns a result set consisting of the
|
51
|
* document ID for each document that satisfies the query
|
52
|
*/
|
53
|
public class DBQuery {
|
54
|
|
55
|
static final int ALL = 1;
|
56
|
static final int WRITE = 2;
|
57
|
static final int READ = 4;
|
58
|
|
59
|
private Connection conn = null;
|
60
|
private String parserName = null;
|
61
|
private MetaCatUtil util = new MetaCatUtil();
|
62
|
/**
|
63
|
* the main routine used to test the DBQuery utility.
|
64
|
* <p>
|
65
|
* Usage: java DBQuery <xmlfile>
|
66
|
*
|
67
|
* @param xmlfile the filename of the xml file containing the query
|
68
|
*/
|
69
|
static public void main(String[] args) {
|
70
|
|
71
|
if (args.length < 1)
|
72
|
{
|
73
|
System.err.println("Wrong number of arguments!!!");
|
74
|
System.err.println("USAGE: java DBQuery [-t] [-index] <xmlfile>");
|
75
|
return;
|
76
|
} else {
|
77
|
try {
|
78
|
|
79
|
int i = 0;
|
80
|
boolean showRuntime = false;
|
81
|
boolean useXMLIndex = false;
|
82
|
if ( args[i].equals( "-t" ) ) {
|
83
|
showRuntime = true;
|
84
|
i++;
|
85
|
}
|
86
|
if ( args[i].equals( "-index" ) ) {
|
87
|
useXMLIndex = true;
|
88
|
i++;
|
89
|
}
|
90
|
String xmlfile = args[i];
|
91
|
|
92
|
// Time the request if asked for
|
93
|
double startTime = System.currentTimeMillis();
|
94
|
|
95
|
// Open a connection to the database
|
96
|
MetaCatUtil util = new MetaCatUtil();
|
97
|
Connection dbconn = util.openDBConnection();
|
98
|
|
99
|
double connTime = System.currentTimeMillis();
|
100
|
|
101
|
// Execute the query
|
102
|
DBQuery queryobj = new DBQuery(dbconn, util.getOption("saxparser"));
|
103
|
FileReader xml = new FileReader(new File(xmlfile));
|
104
|
Hashtable nodelist = null;
|
105
|
nodelist = queryobj.findDocuments(xml, null, null, useXMLIndex);
|
106
|
|
107
|
// Print the reulting document listing
|
108
|
StringBuffer result = new StringBuffer();
|
109
|
String document = null;
|
110
|
String docid = null;
|
111
|
result.append("<?xml version=\"1.0\"?>\n");
|
112
|
result.append("<resultset>\n");
|
113
|
// following line removed by Dan Higgins to avoid insertion of query XML inside returned XML doc
|
114
|
// result.append(" <query>" + xmlfile + "</query>\n");
|
115
|
if (!showRuntime)
|
116
|
{
|
117
|
Enumeration doclist = nodelist.keys();
|
118
|
while (doclist.hasMoreElements()) {
|
119
|
docid = (String)doclist.nextElement();
|
120
|
document = (String)nodelist.get(docid);
|
121
|
result.append(" <document>\n " + document +
|
122
|
"\n </document>\n");
|
123
|
}
|
124
|
|
125
|
result.append("</resultset>\n");
|
126
|
}
|
127
|
// Time the request if asked for
|
128
|
double stopTime = System.currentTimeMillis();
|
129
|
double dbOpenTime = (connTime - startTime)/1000;
|
130
|
double readTime = (stopTime - connTime)/1000;
|
131
|
double executionTime = (stopTime - startTime)/1000;
|
132
|
if (showRuntime) {
|
133
|
System.out.print(" " + executionTime);
|
134
|
System.out.print(" " + dbOpenTime);
|
135
|
System.out.print(" " + readTime);
|
136
|
System.out.print(" " + nodelist.size());
|
137
|
System.out.println();
|
138
|
}
|
139
|
//System.out.println(result);
|
140
|
//write into a file "result.txt"
|
141
|
if (!showRuntime)
|
142
|
{
|
143
|
File f = new File("./result.txt");
|
144
|
FileWriter fw = new FileWriter(f);
|
145
|
BufferedWriter out = new BufferedWriter(fw);
|
146
|
out.write(result.toString());
|
147
|
out.flush();
|
148
|
out.close();
|
149
|
fw.close();
|
150
|
}
|
151
|
|
152
|
}
|
153
|
catch (Exception e) {
|
154
|
System.err.println("Error in DBQuery.main");
|
155
|
System.err.println(e.getMessage());
|
156
|
e.printStackTrace(System.err);
|
157
|
}
|
158
|
}
|
159
|
}
|
160
|
|
161
|
/**
|
162
|
* construct an instance of the DBQuery class
|
163
|
*
|
164
|
* <p>Generally, one would call the findDocuments() routine after creating
|
165
|
* an instance to specify the search query</p>
|
166
|
*
|
167
|
* @param conn the JDBC connection that we use for the query
|
168
|
* @param parserName the fully qualified name of a Java class implementing
|
169
|
* the org.xml.sax.XMLReader interface
|
170
|
*/
|
171
|
public DBQuery( Connection conn, String parserName )
|
172
|
throws IOException,
|
173
|
SQLException,
|
174
|
ClassNotFoundException {
|
175
|
this.conn = conn;
|
176
|
this.parserName = parserName;
|
177
|
}
|
178
|
|
179
|
/**
|
180
|
* routine to search the elements and attributes looking to match query
|
181
|
*
|
182
|
* @param xmlquery the xml serialization of the query (@see pathquery.dtd)
|
183
|
* @param user the username of the user
|
184
|
* @param group the group of the user
|
185
|
*/
|
186
|
public Hashtable findDocuments(Reader xmlquery, String user, String[] groups)
|
187
|
{
|
188
|
return findDocuments(xmlquery, user, groups, true);
|
189
|
}
|
190
|
|
191
|
/**
|
192
|
* routine to search the elements and attributes looking to match query
|
193
|
*
|
194
|
* @param xmlquery the xml serialization of the query (@see pathquery.dtd)
|
195
|
* @param user the username of the user
|
196
|
* @param group the group of the user
|
197
|
* @param useXMLIndex flag whether to search using the path index
|
198
|
*/
|
199
|
public Hashtable findDocuments(Reader xmlquery, String user, String[] groups,
|
200
|
boolean useXMLIndex)
|
201
|
{
|
202
|
Hashtable docListResult = new Hashtable();
|
203
|
PreparedStatement pstmt = null;
|
204
|
String docid = null;
|
205
|
String docname = null;
|
206
|
String doctype = null;
|
207
|
String createDate = null;
|
208
|
String updateDate = null;
|
209
|
String fieldname = null;
|
210
|
String fielddata = null;
|
211
|
String relation = null;
|
212
|
Connection dbconn = null;
|
213
|
Connection dbconn2 = null;
|
214
|
int rev = 0;
|
215
|
StringBuffer document = null;
|
216
|
|
217
|
try {
|
218
|
if (conn == null || conn.isClosed()) {
|
219
|
dbconn = util.openDBConnection();
|
220
|
} else {
|
221
|
dbconn = conn;
|
222
|
}
|
223
|
// problem with ODBC driver multi-threading
|
224
|
// dbconn2 = util.openDBConnection(); // for use by AccessControlList
|
225
|
|
226
|
// Get the XML query and covert it into a SQL statment
|
227
|
QuerySpecification qspec = new QuerySpecification(xmlquery,
|
228
|
parserName,
|
229
|
util.getOption("accNumSeparator"));
|
230
|
//System.out.println(qspec.printSQL(useXMLIndex));
|
231
|
pstmt = dbconn.prepareStatement( qspec.printSQL(useXMLIndex) );
|
232
|
|
233
|
// Execute the SQL query using the JDBC connection
|
234
|
pstmt.execute();
|
235
|
ResultSet rs = pstmt.getResultSet();
|
236
|
boolean tableHasRows = rs.next();
|
237
|
while (tableHasRows)
|
238
|
{
|
239
|
docid = rs.getString(1).trim();
|
240
|
if ( !hasPermission(dbconn, user, groups, docid) ) {
|
241
|
// Advance to the next record in the cursor
|
242
|
tableHasRows = rs.next();
|
243
|
continue;
|
244
|
}
|
245
|
docname = rs.getString(2);
|
246
|
doctype = rs.getString(3);
|
247
|
createDate = rs.getString(4);
|
248
|
updateDate = rs.getString(5);
|
249
|
rev = rs.getInt(6);
|
250
|
|
251
|
// if there are returndocs to match, backtracking can be performed
|
252
|
// otherwise, just return the document that was hit
|
253
|
Vector returndocVec = qspec.getReturnDocList();
|
254
|
if (returndocVec.size() != 0 && !returndocVec.contains(doctype))
|
255
|
{
|
256
|
MetaCatUtil.debugMessage("Back tracing now...");
|
257
|
String sep = util.getOption("accNumSeparator");
|
258
|
StringBuffer btBuf = new StringBuffer();
|
259
|
btBuf.append("select docid from xml_relation where ");
|
260
|
|
261
|
//build the doctype list for the backtracking sql statement
|
262
|
btBuf.append("packagetype in (");
|
263
|
for(int i=0; i<returndocVec.size(); i++)
|
264
|
{
|
265
|
btBuf.append("'").append((String)returndocVec.get(i)).append("'");
|
266
|
if (i != (returndocVec.size() - 1))
|
267
|
{
|
268
|
btBuf.append(", ");
|
269
|
}
|
270
|
}
|
271
|
btBuf.append(") ");
|
272
|
|
273
|
btBuf.append("and (subject like '");
|
274
|
btBuf.append(docid).append(sep).append(rev).append("'");
|
275
|
btBuf.append("or object like '");
|
276
|
btBuf.append(docid).append(sep).append(rev).append("')");
|
277
|
|
278
|
PreparedStatement npstmt = dbconn.
|
279
|
prepareStatement(btBuf.toString());
|
280
|
npstmt.execute();
|
281
|
ResultSet btrs = npstmt.getResultSet();
|
282
|
boolean hasBtRows = btrs.next();
|
283
|
while (hasBtRows)
|
284
|
{ //there was a backtrackable document found
|
285
|
DocumentImpl xmldoc = null;
|
286
|
String packageDocid = btrs.getString(1);
|
287
|
util.debugMessage("Getting document for docid: " + packageDocid);
|
288
|
try
|
289
|
{
|
290
|
// THIS CONSTRUCTOR BUILDS THE WHOLE XML doc not needed here
|
291
|
// xmldoc = new DocumentImpl(dbconn, packageDocid);
|
292
|
// thus use the following to get the doc info only
|
293
|
// xmldoc = new DocumentImpl(dbconn);
|
294
|
xmldoc = new DocumentImpl(dbconn, packageDocid, false);
|
295
|
if (xmldoc == null) {
|
296
|
util.debugMessage("Document was null for: " + packageDocid);
|
297
|
}
|
298
|
}
|
299
|
catch(Exception e)
|
300
|
{
|
301
|
System.out.println("Error getting document in " +
|
302
|
"DBQuery.findDocuments: " + e.getMessage());
|
303
|
}
|
304
|
|
305
|
String docid_org = xmldoc.getDocID();
|
306
|
if (docid_org == null) {
|
307
|
util.debugMessage("Docid_org was null.");
|
308
|
}
|
309
|
docid = docid_org.trim();
|
310
|
docname = xmldoc.getDocname();
|
311
|
doctype = xmldoc.getDoctype();
|
312
|
createDate = xmldoc.getCreateDate();
|
313
|
updateDate = xmldoc.getUpdateDate();
|
314
|
rev = xmldoc.getRev();
|
315
|
|
316
|
document = new StringBuffer();
|
317
|
|
318
|
String completeDocid = docid + util.getOption("accNumSeparator");
|
319
|
completeDocid += rev;
|
320
|
document.append("<docid>").append(completeDocid);
|
321
|
document.append("</docid>");
|
322
|
if (docname != null) {
|
323
|
document.append("<docname>" + docname + "</docname>");
|
324
|
}
|
325
|
if (doctype != null) {
|
326
|
document.append("<doctype>" + doctype + "</doctype>");
|
327
|
}
|
328
|
if (createDate != null) {
|
329
|
document.append("<createdate>" + createDate + "</createdate>");
|
330
|
}
|
331
|
if (updateDate != null) {
|
332
|
document.append("<updatedate>" + updateDate + "</updatedate>");
|
333
|
}
|
334
|
// Store the document id and the root node id
|
335
|
docListResult.put(docid,(String)document.toString());
|
336
|
|
337
|
// Get the next package document linked to our hit
|
338
|
hasBtRows = btrs.next();
|
339
|
}
|
340
|
npstmt.close();
|
341
|
btrs.close();
|
342
|
} else {
|
343
|
|
344
|
document = new StringBuffer();
|
345
|
|
346
|
String completeDocid = docid + util.getOption("accNumSeparator");
|
347
|
completeDocid += rev;
|
348
|
document.append("<docid>").append(completeDocid).append("</docid>");
|
349
|
if (docname != null) {
|
350
|
document.append("<docname>" + docname + "</docname>");
|
351
|
}
|
352
|
if (doctype != null) {
|
353
|
document.append("<doctype>" + doctype + "</doctype>");
|
354
|
}
|
355
|
if (createDate != null) {
|
356
|
document.append("<createdate>" + createDate + "</createdate>");
|
357
|
}
|
358
|
if (updateDate != null) {
|
359
|
document.append("<updatedate>" + updateDate + "</updatedate>");
|
360
|
}
|
361
|
// Store the document id and the root node id
|
362
|
docListResult.put(docid,(String)document.toString());
|
363
|
|
364
|
}
|
365
|
|
366
|
// Advance to the next record in the cursor
|
367
|
tableHasRows = rs.next();
|
368
|
}
|
369
|
rs.close();
|
370
|
pstmt.close();
|
371
|
|
372
|
if (qspec.containsExtendedSQL())
|
373
|
{
|
374
|
Vector extendedFields = new Vector(qspec.getReturnFieldList());
|
375
|
Vector results = new Vector();
|
376
|
Enumeration keylist = docListResult.keys();
|
377
|
StringBuffer doclist = new StringBuffer();
|
378
|
while(keylist.hasMoreElements())
|
379
|
{
|
380
|
doclist.append("'");
|
381
|
doclist.append((String)keylist.nextElement());
|
382
|
doclist.append("',");
|
383
|
}
|
384
|
doclist.deleteCharAt(doclist.length()-1); //remove the last comma
|
385
|
//pstmt.close();
|
386
|
pstmt = dbconn.prepareStatement(qspec.printExtendedSQL(
|
387
|
doclist.toString()));
|
388
|
pstmt.execute();
|
389
|
rs = pstmt.getResultSet();
|
390
|
tableHasRows = rs.next();
|
391
|
while(tableHasRows)
|
392
|
{
|
393
|
docid = rs.getString(1).trim();
|
394
|
if ( !hasPermission(dbconn, user, groups, docid) ) {
|
395
|
// Advance to the next record in the cursor
|
396
|
tableHasRows = rs.next();
|
397
|
continue;
|
398
|
}
|
399
|
fieldname = rs.getString(2);
|
400
|
fielddata = rs.getString(3);
|
401
|
|
402
|
document = new StringBuffer();
|
403
|
|
404
|
document.append("<param name=\"");
|
405
|
document.append(fieldname);
|
406
|
document.append("\">");
|
407
|
document.append(fielddata);
|
408
|
document.append("</param>");
|
409
|
|
410
|
tableHasRows = rs.next();
|
411
|
if (docListResult.containsKey(docid))
|
412
|
{
|
413
|
String removedelement = (String)docListResult.remove(docid);
|
414
|
docListResult.put(docid, removedelement + document.toString());
|
415
|
}
|
416
|
else
|
417
|
{
|
418
|
docListResult.put(docid, document.toString());
|
419
|
}
|
420
|
}
|
421
|
rs.close();
|
422
|
}
|
423
|
pstmt.close();
|
424
|
|
425
|
//this loop adds the relation data to the resultdoc
|
426
|
//this code might be able to be added to the backtracking code above
|
427
|
Enumeration docidkeys = docListResult.keys();
|
428
|
while(docidkeys.hasMoreElements())
|
429
|
{
|
430
|
//String connstring = "metacat://"+util.getOption("server")+"?docid=";
|
431
|
String connstring = "%docid=";
|
432
|
String docidkey = (String)docidkeys.nextElement();
|
433
|
pstmt = dbconn.prepareStatement(qspec.printRelationSQL(docidkey));
|
434
|
pstmt.execute();
|
435
|
rs = pstmt.getResultSet();
|
436
|
tableHasRows = rs.next();
|
437
|
while(tableHasRows)
|
438
|
{
|
439
|
String sub = rs.getString(1);
|
440
|
String rel = rs.getString(2);
|
441
|
String obj = rs.getString(3);
|
442
|
String subDT = rs.getString(4);
|
443
|
String objDT = rs.getString(5);
|
444
|
|
445
|
//MetacatURL murl = new MetacatURL(sub);
|
446
|
//we only want to process metacat urls here.
|
447
|
//if (murl.getProtocol().equals("metacat")) {
|
448
|
//String[] tempparam = murl.getParam(0);
|
449
|
//if (tempparam[0].equals("docid") && tempparam[1].equals(docidkey))
|
450
|
//{
|
451
|
document = new StringBuffer();
|
452
|
document.append("<triple>");
|
453
|
document.append("<subject>").append(sub);
|
454
|
document.append("</subject>");
|
455
|
if (!subDT.equals("null")) {
|
456
|
document.append("<subjectdoctype>").append(subDT);
|
457
|
document.append("</subjectdoctype>");
|
458
|
}
|
459
|
document.append("<relationship>").append(rel);
|
460
|
document.append("</relationship>");
|
461
|
document.append("<object>").append(obj);
|
462
|
document.append("</object>");
|
463
|
if (!objDT.equals("null")) {
|
464
|
document.append("<objectdoctype>").append(objDT);
|
465
|
document.append("</objectdoctype>");
|
466
|
}
|
467
|
document.append("</triple>");
|
468
|
|
469
|
String removedelement = (String)docListResult.remove(docidkey);
|
470
|
docListResult.put(docidkey, removedelement +
|
471
|
document.toString());
|
472
|
|
473
|
//}
|
474
|
//}
|
475
|
tableHasRows = rs.next();
|
476
|
}
|
477
|
rs.close();
|
478
|
pstmt.close();
|
479
|
}
|
480
|
|
481
|
} catch (SQLException e) {
|
482
|
System.err.println("SQL Error in DBQuery.findDocuments: " +
|
483
|
e.getMessage());
|
484
|
} catch (IOException ioe) {
|
485
|
System.err.println("IO error in DBQuery.findDocuments:");
|
486
|
System.err.println(ioe.getMessage());
|
487
|
} catch (Exception ee) {
|
488
|
System.err.println("Exception in DBQuery.findDocuments: " +
|
489
|
ee.getMessage());
|
490
|
ee.printStackTrace(System.err);
|
491
|
}
|
492
|
finally {
|
493
|
try
|
494
|
{
|
495
|
dbconn.close();
|
496
|
// dbconn2.close();
|
497
|
}
|
498
|
catch(SQLException sqle)
|
499
|
{
|
500
|
System.out.println("error closing conn in DBQuery.findDocuments");
|
501
|
}
|
502
|
}
|
503
|
//System.out.println("docListResult: ");
|
504
|
//System.out.println(docListResult.toString());
|
505
|
return docListResult;
|
506
|
}
|
507
|
|
508
|
/**
|
509
|
* returns a string array of the contents of a particular node.
|
510
|
* If the node appears more than once, the contents are returned
|
511
|
* in the order in which they appearred in the document.
|
512
|
* @param nodename the name or path of the particular node.
|
513
|
* @param docid the docid of the document you want the node from.
|
514
|
* @param conn a database connection-this allows this method to be static
|
515
|
*/
|
516
|
public static Object[] getNodeContent(String nodename, String docid,
|
517
|
Connection conn)
|
518
|
{
|
519
|
StringBuffer query = new StringBuffer();
|
520
|
Vector result = new Vector();
|
521
|
PreparedStatement pstmt = null;
|
522
|
query.append("select nodedata from xml_nodes where parentnodeid in ");
|
523
|
query.append("(select nodeid from xml_index where path like '");
|
524
|
query.append(nodename);
|
525
|
query.append("' and docid like '").append(docid).append("')");
|
526
|
try
|
527
|
{
|
528
|
pstmt = conn.prepareStatement(query.toString());
|
529
|
|
530
|
// Execute the SQL query using the JDBC connection
|
531
|
pstmt.execute();
|
532
|
ResultSet rs = pstmt.getResultSet();
|
533
|
boolean tableHasRows = rs.next();
|
534
|
while (tableHasRows)
|
535
|
{
|
536
|
result.add(rs.getString(1));
|
537
|
System.out.println(rs.getString(1));
|
538
|
tableHasRows = rs.next();
|
539
|
}
|
540
|
}
|
541
|
catch (SQLException e)
|
542
|
{
|
543
|
System.err.println("Error in DBQuery.getNodeContent: " + e.getMessage());
|
544
|
} finally {
|
545
|
try
|
546
|
{
|
547
|
pstmt.close();
|
548
|
}
|
549
|
catch(SQLException sqle) {}
|
550
|
}
|
551
|
return result.toArray();
|
552
|
}
|
553
|
|
554
|
/**
|
555
|
* format a structured query as an XML document that conforms
|
556
|
* to the pathquery.dtd and is appropriate for submission to the DBQuery
|
557
|
* structured query engine
|
558
|
*
|
559
|
* @param params The list of parameters that should be included in the query
|
560
|
*/
|
561
|
public static String createSQuery(Hashtable params)
|
562
|
{
|
563
|
StringBuffer query = new StringBuffer();
|
564
|
Enumeration elements;
|
565
|
Enumeration keys;
|
566
|
String filterDoctype = null;
|
567
|
String casesensitive = null;
|
568
|
String searchmode = null;
|
569
|
Object nextkey;
|
570
|
Object nextelement;
|
571
|
//add the xml headers
|
572
|
query.append("<?xml version=\"1.0\"?>\n");
|
573
|
query.append("<pathquery version=\"1.0\">\n");
|
574
|
|
575
|
if (params.containsKey("meta_file_id"))
|
576
|
{
|
577
|
query.append("<meta_file_id>");
|
578
|
query.append( ((String[])params.get("meta_file_id"))[0]);
|
579
|
query.append("</meta_file_id>");
|
580
|
}
|
581
|
|
582
|
if (params.containsKey("returndoctype"))
|
583
|
{
|
584
|
String[] returnDoctypes = ((String[])params.get("returndoctype"));
|
585
|
for(int i=0; i<returnDoctypes.length; i++)
|
586
|
{
|
587
|
String doctype = (String)returnDoctypes[i];
|
588
|
|
589
|
if (!doctype.equals("any") &&
|
590
|
!doctype.equals("ANY") &&
|
591
|
!doctype.equals("") )
|
592
|
{
|
593
|
query.append("<returndoctype>").append(doctype);
|
594
|
query.append("</returndoctype>");
|
595
|
}
|
596
|
}
|
597
|
}
|
598
|
|
599
|
if (params.containsKey("filterdoctype"))
|
600
|
{
|
601
|
String[] filterDoctypes = ((String[])params.get("filterdoctype"));
|
602
|
for(int i=0; i<filterDoctypes.length; i++)
|
603
|
{
|
604
|
query.append("<filterdoctype>").append(filterDoctypes[i]);
|
605
|
query.append("</filterdoctype>");
|
606
|
}
|
607
|
}
|
608
|
|
609
|
if (params.containsKey("returnfield"))
|
610
|
{
|
611
|
String[] returnfield = ((String[])params.get("returnfield"));
|
612
|
for(int i=0; i<returnfield.length; i++)
|
613
|
{
|
614
|
query.append("<returnfield>").append(returnfield[i]);
|
615
|
query.append("</returnfield>");
|
616
|
}
|
617
|
}
|
618
|
|
619
|
if (params.containsKey("owner"))
|
620
|
{
|
621
|
String[] owner = ((String[])params.get("owner"));
|
622
|
for(int i=0; i<owner.length; i++)
|
623
|
{
|
624
|
query.append("<owner>").append(owner[i]);
|
625
|
query.append("</owner>");
|
626
|
}
|
627
|
}
|
628
|
|
629
|
if (params.containsKey("site"))
|
630
|
{
|
631
|
String[] site = ((String[])params.get("site"));
|
632
|
for(int i=0; i<site.length; i++)
|
633
|
{
|
634
|
query.append("<site>").append(site[i]);
|
635
|
query.append("</site>");
|
636
|
}
|
637
|
}
|
638
|
|
639
|
//allows the dynamic switching of boolean operators
|
640
|
if (params.containsKey("operator"))
|
641
|
{
|
642
|
query.append("<querygroup operator=\"" +
|
643
|
((String[])params.get("operator"))[0] + "\">");
|
644
|
}
|
645
|
else
|
646
|
{ //the default operator is UNION
|
647
|
query.append("<querygroup operator=\"UNION\">");
|
648
|
}
|
649
|
|
650
|
if (params.containsKey("casesensitive"))
|
651
|
{
|
652
|
casesensitive = ((String[])params.get("casesensitive"))[0];
|
653
|
}
|
654
|
else
|
655
|
{
|
656
|
casesensitive = "false";
|
657
|
}
|
658
|
|
659
|
if (params.containsKey("searchmode"))
|
660
|
{
|
661
|
searchmode = ((String[])params.get("searchmode"))[0];
|
662
|
}
|
663
|
else
|
664
|
{
|
665
|
searchmode = "contains";
|
666
|
}
|
667
|
|
668
|
//anyfield is a special case because it does a
|
669
|
//free text search. It does not have a <pathexpr>
|
670
|
//tag. This allows for a free text search within the structured
|
671
|
//query. This is useful if the INTERSECT operator is used.
|
672
|
if (params.containsKey("anyfield"))
|
673
|
{
|
674
|
String[] anyfield = ((String[])params.get("anyfield"));
|
675
|
//allow for more than one value for anyfield
|
676
|
for(int i=0; i<anyfield.length; i++)
|
677
|
{
|
678
|
if (!anyfield[i].equals(""))
|
679
|
{
|
680
|
query.append("<queryterm casesensitive=\"" + casesensitive +
|
681
|
"\" " + "searchmode=\"" + searchmode + "\"><value>" +
|
682
|
anyfield[i] +
|
683
|
"</value></queryterm>");
|
684
|
}
|
685
|
}
|
686
|
}
|
687
|
|
688
|
//this while loop finds the rest of the parameters
|
689
|
//and attempts to query for the field specified
|
690
|
//by the parameter.
|
691
|
elements = params.elements();
|
692
|
keys = params.keys();
|
693
|
while(keys.hasMoreElements() && elements.hasMoreElements())
|
694
|
{
|
695
|
nextkey = keys.nextElement();
|
696
|
nextelement = elements.nextElement();
|
697
|
|
698
|
//make sure we aren't querying for any of these
|
699
|
//parameters since the are already in the query
|
700
|
//in one form or another.
|
701
|
if (!nextkey.toString().equals("returndoctype") &&
|
702
|
!nextkey.toString().equals("filterdoctype") &&
|
703
|
!nextkey.toString().equals("action") &&
|
704
|
!nextkey.toString().equals("qformat") &&
|
705
|
!nextkey.toString().equals("anyfield") &&
|
706
|
!nextkey.toString().equals("returnfield") &&
|
707
|
!nextkey.toString().equals("owner") &&
|
708
|
!nextkey.toString().equals("site") &&
|
709
|
!nextkey.toString().equals("operator") )
|
710
|
{
|
711
|
//allow for more than value per field name
|
712
|
for(int i=0; i<((String[])nextelement).length; i++)
|
713
|
{
|
714
|
if (!((String[])nextelement)[i].equals(""))
|
715
|
{
|
716
|
query.append("<queryterm casesensitive=\"" + casesensitive +"\" " +
|
717
|
"searchmode=\"" + searchmode + "\">" +
|
718
|
"<value>" +
|
719
|
//add the query value
|
720
|
((String[])nextelement)[i] +
|
721
|
"</value><pathexpr>" +
|
722
|
//add the path to query by
|
723
|
nextkey.toString() +
|
724
|
"</pathexpr></queryterm>");
|
725
|
}
|
726
|
}
|
727
|
}
|
728
|
}
|
729
|
query.append("</querygroup></pathquery>");
|
730
|
//append on the end of the xml and return the result as a string
|
731
|
return query.toString();
|
732
|
}
|
733
|
|
734
|
/**
|
735
|
* format a simple free-text value query as an XML document that conforms
|
736
|
* to the pathquery.dtd and is appropriate for submission to the DBQuery
|
737
|
* structured query engine
|
738
|
*
|
739
|
* @param value the text string to search for in the xml catalog
|
740
|
* @param doctype the type of documents to include in the result set -- use
|
741
|
* "any" or "ANY" for unfiltered result sets
|
742
|
*/
|
743
|
public static String createQuery(String value, String doctype) {
|
744
|
StringBuffer xmlquery = new StringBuffer();
|
745
|
xmlquery.append("<?xml version=\"1.0\"?>\n");
|
746
|
xmlquery.append("<pathquery version=\"1.0\">");
|
747
|
|
748
|
if (!doctype.equals("any") && !doctype.equals("ANY")) {
|
749
|
xmlquery.append("<returndoctype>");
|
750
|
xmlquery.append(doctype).append("</returndoctype>");
|
751
|
}
|
752
|
|
753
|
xmlquery.append("<querygroup operator=\"UNION\">");
|
754
|
//chad added - 8/14
|
755
|
//the if statement allows a query to gracefully handle a null
|
756
|
//query. Without this if a nullpointerException is thrown.
|
757
|
if (!value.equals(""))
|
758
|
{
|
759
|
xmlquery.append("<queryterm casesensitive=\"false\" ");
|
760
|
xmlquery.append("searchmode=\"contains\">");
|
761
|
xmlquery.append("<value>").append(value).append("</value>");
|
762
|
xmlquery.append("</queryterm>");
|
763
|
}
|
764
|
xmlquery.append("</querygroup>");
|
765
|
xmlquery.append("</pathquery>");
|
766
|
|
767
|
|
768
|
return (xmlquery.toString());
|
769
|
}
|
770
|
|
771
|
/**
|
772
|
* format a simple free-text value query as an XML document that conforms
|
773
|
* to the pathquery.dtd and is appropriate for submission to the DBQuery
|
774
|
* structured query engine
|
775
|
*
|
776
|
* @param value the text string to search for in the xml catalog
|
777
|
*/
|
778
|
public static String createQuery(String value) {
|
779
|
return createQuery(value, "any");
|
780
|
}
|
781
|
|
782
|
/**
|
783
|
* Check for "READ" permission on @docid for @user and/or @group
|
784
|
* from DB connection
|
785
|
*/
|
786
|
private boolean hasPermission ( Connection conn, String user,
|
787
|
String[] groups, String docid )
|
788
|
throws SQLException
|
789
|
{
|
790
|
// Check for READ permission on @docid for @user and/or @groups
|
791
|
AccessControlList aclobj = new AccessControlList(conn);
|
792
|
return aclobj.hasPermission("READ", user, groups, docid);
|
793
|
}
|
794
|
|
795
|
}
|