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