1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Purpose: A Class that represents a structured query, and can be
|
4
|
* constructed from an XML serialization conforming to
|
5
|
* pathquery.dtd. The printSQL() method can be used to print
|
6
|
* a SQL serialization of the query.
|
7
|
* Copyright: 2000 Regents of the University of California and the
|
8
|
* National Center for Ecological Analysis and Synthesis
|
9
|
* Authors: Matt Jones
|
10
|
* Release: @release@
|
11
|
*
|
12
|
* '$Author: sgarg $'
|
13
|
* '$Date: 2005-01-18 12:11:01 -0800 (Tue, 18 Jan 2005) $'
|
14
|
* '$Revision: 2376 $'
|
15
|
*
|
16
|
* This program is free software; you can redistribute it and/or modify
|
17
|
* it under the terms of the GNU General Public License as published by
|
18
|
* the Free Software Foundation; either version 2 of the License, or
|
19
|
* (at your option) any later version.
|
20
|
*
|
21
|
* This program is distributed in the hope that it will be useful,
|
22
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
23
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
24
|
* GNU General Public License for more details.
|
25
|
*
|
26
|
* You should have received a copy of the GNU General Public License
|
27
|
* along with this program; if not, write to the Free Software
|
28
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
29
|
*/
|
30
|
|
31
|
package edu.ucsb.nceas.metacat;
|
32
|
|
33
|
import java.io.IOException;
|
34
|
import java.io.Reader;
|
35
|
import java.io.StringReader;
|
36
|
import java.util.Enumeration;
|
37
|
import java.util.Hashtable;
|
38
|
import java.util.Stack;
|
39
|
import java.util.Vector;
|
40
|
|
41
|
import edu.ucsb.nceas.dbadapter.AbstractDatabase;
|
42
|
|
43
|
import org.xml.sax.Attributes;
|
44
|
import org.xml.sax.InputSource;
|
45
|
import org.xml.sax.SAXException;
|
46
|
import org.xml.sax.XMLReader;
|
47
|
import org.xml.sax.helpers.DefaultHandler;
|
48
|
import org.xml.sax.helpers.XMLReaderFactory;
|
49
|
|
50
|
/**
|
51
|
* A Class that represents a structured query, and can be constructed from an
|
52
|
* XML serialization conforming to
|
53
|
*
|
54
|
* @see pathquery.dtd. The printSQL() method can be used to print a SQL
|
55
|
* serialization of the query.
|
56
|
*/
|
57
|
public class QuerySpecification extends DefaultHandler
|
58
|
{
|
59
|
|
60
|
/** flag determining whether extended query terms are present */
|
61
|
private boolean containsExtendedSQL = false;
|
62
|
|
63
|
/** Identifier for this query document */
|
64
|
private String meta_file_id;
|
65
|
|
66
|
/** Title of this query */
|
67
|
private String queryTitle;
|
68
|
|
69
|
/** List of document types to be returned using package back tracing */
|
70
|
private Vector returnDocList;
|
71
|
|
72
|
/** List of document types to be searched */
|
73
|
private Vector filterDocList;
|
74
|
|
75
|
/** List of fields to be returned in result set */
|
76
|
private Vector returnFieldList;
|
77
|
|
78
|
/** List of users owning documents to be searched */
|
79
|
private Vector ownerList;
|
80
|
|
81
|
/** List of sites/scopes used to constrain search */
|
82
|
private Vector siteList;
|
83
|
|
84
|
/** The root query group that contains the recursive query constraints */
|
85
|
private QueryGroup query = null;
|
86
|
|
87
|
// Query data structures used temporarily during XML parsing
|
88
|
private Stack elementStack;
|
89
|
|
90
|
private Stack queryStack;
|
91
|
|
92
|
private String currentValue;
|
93
|
|
94
|
private String currentPathexpr;
|
95
|
|
96
|
private String parserName = null;
|
97
|
|
98
|
private String accNumberSeparator = null;
|
99
|
|
100
|
private static final AbstractDatabase dbAdapter = MetaCatUtil.dbAdapter;
|
101
|
|
102
|
private boolean percentageSearch = false;
|
103
|
|
104
|
private String userName = null;
|
105
|
|
106
|
private static final String PUBLIC = "public";
|
107
|
|
108
|
private String[] group = null;
|
109
|
|
110
|
public static final String ATTRIBUTESYMBOL = "@";
|
111
|
|
112
|
private boolean hasAttributeReturnField = false;
|
113
|
|
114
|
private Hashtable attributeReturnList = new Hashtable();
|
115
|
|
116
|
private int countAttributeReturnField = 0;
|
117
|
|
118
|
private StringBuffer textBuffer = new StringBuffer();
|
119
|
|
120
|
/**
|
121
|
* construct an instance of the QuerySpecification class
|
122
|
*
|
123
|
* @param queryspec
|
124
|
* the XML representation of the query (should conform to
|
125
|
* pathquery.dtd) as a Reader
|
126
|
* @param parserName
|
127
|
* the fully qualified name of a Java Class implementing the
|
128
|
* org.xml.sax.XMLReader interface
|
129
|
*/
|
130
|
public QuerySpecification(Reader queryspec, String parserName,
|
131
|
String accNumberSeparator) throws IOException
|
132
|
{
|
133
|
super();
|
134
|
|
135
|
// Initialize the class variables
|
136
|
returnDocList = new Vector();
|
137
|
filterDocList = new Vector();
|
138
|
elementStack = new Stack();
|
139
|
queryStack = new Stack();
|
140
|
returnFieldList = new Vector();
|
141
|
ownerList = new Vector();
|
142
|
siteList = new Vector();
|
143
|
this.parserName = parserName;
|
144
|
this.accNumberSeparator = accNumberSeparator;
|
145
|
|
146
|
// Initialize the parser and read the queryspec
|
147
|
XMLReader parser = initializeParser();
|
148
|
if (parser == null) {
|
149
|
System.err.println("SAX parser not instantiated properly.");
|
150
|
}
|
151
|
try {
|
152
|
parser.parse(new InputSource(queryspec));
|
153
|
} catch (SAXException e) {
|
154
|
System.err.println("error parsing data in "
|
155
|
+ "QuerySpecification.QuerySpecification");
|
156
|
System.err.println(e.getMessage());
|
157
|
}
|
158
|
}
|
159
|
|
160
|
/**
|
161
|
* construct an instance of the QuerySpecification class
|
162
|
*
|
163
|
* @param queryspec
|
164
|
* the XML representation of the query (should conform to
|
165
|
* pathquery.dtd) as a String
|
166
|
* @param parserName
|
167
|
* the fully qualified name of a Java Class implementing the
|
168
|
* org.xml.sax.Parser interface
|
169
|
*/
|
170
|
public QuerySpecification(String queryspec, String parserName,
|
171
|
String accNumberSeparator) throws IOException
|
172
|
{
|
173
|
this(new StringReader(queryspec), parserName, accNumberSeparator);
|
174
|
}
|
175
|
|
176
|
/**
|
177
|
* construct an instance of the QuerySpecification class which don't need
|
178
|
* to parser a xml document
|
179
|
*
|
180
|
* @param accNumberSeparator
|
181
|
* the separator between doc version
|
182
|
*/
|
183
|
public QuerySpecification(String accNumberSeparator) throws IOException
|
184
|
{
|
185
|
// Initialize the class variables
|
186
|
returnDocList = new Vector();
|
187
|
filterDocList = new Vector();
|
188
|
elementStack = new Stack();
|
189
|
queryStack = new Stack();
|
190
|
returnFieldList = new Vector();
|
191
|
ownerList = new Vector();
|
192
|
siteList = new Vector();
|
193
|
this.accNumberSeparator = accNumberSeparator;
|
194
|
}
|
195
|
|
196
|
/**
|
197
|
* Method to set user name
|
198
|
*
|
199
|
* @param myName
|
200
|
* the user name
|
201
|
*/
|
202
|
public void setUserName(String myName)
|
203
|
{
|
204
|
//to lower case
|
205
|
if (myName != null) {
|
206
|
this.userName = myName.toLowerCase();
|
207
|
} else {
|
208
|
this.userName = myName;
|
209
|
}
|
210
|
}
|
211
|
|
212
|
/**
|
213
|
* Method to set user group
|
214
|
*
|
215
|
* @param myGroup
|
216
|
* the user group
|
217
|
*/
|
218
|
public void setGroup(String[] myGroup)
|
219
|
{
|
220
|
this.group = myGroup;
|
221
|
}
|
222
|
|
223
|
/**
|
224
|
* Method to indicate this query is a percentage search
|
225
|
*/
|
226
|
public boolean isPercentageSearch()
|
227
|
{
|
228
|
return percentageSearch;
|
229
|
}
|
230
|
|
231
|
/*
|
232
|
* Method to get owner query. If it is owner it has all permission
|
233
|
*/
|
234
|
private String createOwerQuery()
|
235
|
{
|
236
|
String ownerQuery = null;
|
237
|
ownerQuery = "SELECT docid FROM xml_documents WHERE ";
|
238
|
if (userName != null && !userName.equals("")) {
|
239
|
ownerQuery = ownerQuery + "lower(user_owner) ='" + userName + "'";
|
240
|
}
|
241
|
|
242
|
MetaCatUtil.debugMessage("OwnerQuery: " + ownerQuery, 30);
|
243
|
return ownerQuery;
|
244
|
}
|
245
|
|
246
|
/*
|
247
|
* Method to create query for xml_access, this part is to get docid list
|
248
|
* which have a allow rule for a given user
|
249
|
*/
|
250
|
private String createAllowRuleQuery()
|
251
|
{
|
252
|
String allowQuery = null;
|
253
|
String allowString = constructAllowString();
|
254
|
allowQuery = "SELECT docid from xml_access WHERE( " + allowString;
|
255
|
allowQuery = allowQuery + ") AND subtreeid IS NULL";
|
256
|
MetaCatUtil.debugMessage("allow query is: " + allowQuery, 30);
|
257
|
return allowQuery;
|
258
|
|
259
|
}
|
260
|
|
261
|
/* Method to construct a allow rule string */
|
262
|
private String constructAllowString()
|
263
|
{
|
264
|
String allowQuery = "";
|
265
|
// add allow rule for user name
|
266
|
if (userName != null && !userName.equals("")) {
|
267
|
allowQuery = allowQuery + "(lower(principal_name) = '" + userName
|
268
|
+ "' AND perm_type = 'allow'"
|
269
|
+ " AND (permission='4' OR permission='7'))";
|
270
|
}
|
271
|
// add allow rule for public
|
272
|
allowQuery = allowQuery + "OR (lower(principal_name) = '" + PUBLIC
|
273
|
+ "' AND perm_type = 'allow'"
|
274
|
+ " AND (permission='4' OR permission='7'))";
|
275
|
|
276
|
// add allow rule for group
|
277
|
if (group != null) {
|
278
|
for (int i = 0; i < group.length; i++) {
|
279
|
String groupUint = group[i];
|
280
|
if (groupUint != null && !groupUint.equals("")) {
|
281
|
groupUint = groupUint.toLowerCase();
|
282
|
allowQuery = allowQuery + " OR (lower(principal_name) = '"
|
283
|
+ groupUint + "' AND perm_type = 'allow'"
|
284
|
+ " AND (permission='4' OR permission='7'))";
|
285
|
}//if
|
286
|
}//for
|
287
|
}//if
|
288
|
MetaCatUtil.debugMessage("allow string is: " + allowQuery, 40);
|
289
|
return allowQuery;
|
290
|
}
|
291
|
|
292
|
/*
|
293
|
* Method to create query for xml_access, this part is to get docid list
|
294
|
* which have a deny rule and perm_order is allowFirst for a given user.
|
295
|
* This means the user will be denied to read
|
296
|
*/
|
297
|
private String createDenyRuleQuery()
|
298
|
{
|
299
|
String denyQuery = null;
|
300
|
String denyString = constructDenyString();
|
301
|
denyQuery = "SELECT docid from xml_access WHERE( " + denyString;
|
302
|
denyQuery = denyQuery + ") AND subtreeid IS NULL ";
|
303
|
MetaCatUtil.debugMessage("denyquery is: " + denyQuery, 30);
|
304
|
return denyQuery;
|
305
|
|
306
|
}
|
307
|
|
308
|
/* Construct deny string */
|
309
|
private String constructDenyString()
|
310
|
{
|
311
|
String denyQuery = "";
|
312
|
// add deny rule for user name
|
313
|
if (userName != null && !userName.equals("")) {
|
314
|
denyQuery = denyQuery + "(lower(principal_name) = '" + userName
|
315
|
+ "' AND perm_type = 'deny' "
|
316
|
+ "AND perm_order ='allowFirst'"
|
317
|
+ " AND (permission='4' OR permission='7'))";
|
318
|
}
|
319
|
// add deny rule for public
|
320
|
denyQuery = denyQuery + "OR (lower(principal_name) = '" + PUBLIC
|
321
|
+ "' AND perm_type = 'deny' " + "AND perm_order ='allowFirst'"
|
322
|
+ " AND (permission='4' OR permission='7'))";
|
323
|
|
324
|
// add allow rule for group
|
325
|
if (group != null) {
|
326
|
for (int i = 0; i < group.length; i++) {
|
327
|
String groupUint = group[i];
|
328
|
if (groupUint != null && !groupUint.equals("")) {
|
329
|
groupUint = groupUint.toLowerCase();
|
330
|
denyQuery = denyQuery + " OR (lower(principal_name) = '"
|
331
|
+ groupUint + "' AND perm_type = 'deny' "
|
332
|
+ "AND perm_order ='allowFirst'"
|
333
|
+ " AND (permission='4' OR permission='7'))";
|
334
|
}//if
|
335
|
}//for
|
336
|
}//if
|
337
|
return denyQuery;
|
338
|
}
|
339
|
|
340
|
/**
|
341
|
* Method to append a access control query to SQL. So in DBQuery class, we
|
342
|
* can get docid from both user specified query and access control query.
|
343
|
* We don't need to checking permission after we get the doclist. It will
|
344
|
* be good to performance
|
345
|
*
|
346
|
*/
|
347
|
public String getAccessQuery()
|
348
|
{
|
349
|
String accessQuery = null;
|
350
|
String onwer = createOwerQuery();
|
351
|
String allow = createAllowRuleQuery();
|
352
|
String deny = createDenyRuleQuery();
|
353
|
accessQuery = " AND (docid IN(" + onwer + ")";
|
354
|
accessQuery = accessQuery + " OR (docid IN (" + allow + ")"
|
355
|
+ " AND docid NOT IN (" + deny + ")))";
|
356
|
MetaCatUtil.debugMessage("accessquery is: " + accessQuery, 30);
|
357
|
return accessQuery;
|
358
|
}
|
359
|
|
360
|
/**
|
361
|
* Returns true if the parsed query contains and extended xml query (i.e.
|
362
|
* there is at least one <returnfield> in the pathquery document)
|
363
|
*/
|
364
|
public boolean containsExtendedSQL()
|
365
|
{
|
366
|
if (containsExtendedSQL) {
|
367
|
return true;
|
368
|
} else {
|
369
|
return false;
|
370
|
}
|
371
|
}
|
372
|
|
373
|
/**
|
374
|
* A method to get if the query has an attribute return field
|
375
|
*/
|
376
|
public boolean containAttributeReturnField()
|
377
|
{
|
378
|
return hasAttributeReturnField;
|
379
|
}
|
380
|
|
381
|
/**
|
382
|
* Accessor method to return the identifier of this Query
|
383
|
*/
|
384
|
public String getIdentifier()
|
385
|
{
|
386
|
return meta_file_id;
|
387
|
}
|
388
|
|
389
|
/**
|
390
|
* method to set the identifier of this query
|
391
|
*/
|
392
|
public void setIdentifier(String id)
|
393
|
{
|
394
|
this.meta_file_id = id;
|
395
|
}
|
396
|
|
397
|
/**
|
398
|
* Accessor method to return the title of this Query
|
399
|
*/
|
400
|
public String getQueryTitle()
|
401
|
{
|
402
|
return queryTitle;
|
403
|
}
|
404
|
|
405
|
/**
|
406
|
* method to set the title of this query
|
407
|
*/
|
408
|
public void setQueryTitle(String title)
|
409
|
{
|
410
|
this.queryTitle = title;
|
411
|
}
|
412
|
|
413
|
/**
|
414
|
* Accessor method to return a vector of the return document types as
|
415
|
* defined in the <returndoctype> tag in the pathquery dtd.
|
416
|
*/
|
417
|
public Vector getReturnDocList()
|
418
|
{
|
419
|
return this.returnDocList;
|
420
|
}
|
421
|
|
422
|
/**
|
423
|
* method to set the list of return docs of this query
|
424
|
*/
|
425
|
public void setReturnDocList(Vector returnDocList)
|
426
|
{
|
427
|
this.returnDocList = returnDocList;
|
428
|
}
|
429
|
|
430
|
/**
|
431
|
* Accessor method to return a vector of the filter doc types as defined in
|
432
|
* the <filterdoctype> tag in the pathquery dtd.
|
433
|
*/
|
434
|
public Vector getFilterDocList()
|
435
|
{
|
436
|
return this.filterDocList;
|
437
|
}
|
438
|
|
439
|
/**
|
440
|
* method to set the list of filter docs of this query
|
441
|
*/
|
442
|
public void setFilterDocList(Vector filterDocList)
|
443
|
{
|
444
|
this.filterDocList = filterDocList;
|
445
|
}
|
446
|
|
447
|
/**
|
448
|
* Accessor method to return a vector of the extended return fields as
|
449
|
* defined in the <returnfield> tag in the pathquery dtd.
|
450
|
*/
|
451
|
public Vector getReturnFieldList()
|
452
|
{
|
453
|
return this.returnFieldList;
|
454
|
}
|
455
|
|
456
|
/**
|
457
|
* method to set the list of fields to be returned by this query
|
458
|
*/
|
459
|
public void setReturnFieldList(Vector returnFieldList)
|
460
|
{
|
461
|
this.returnFieldList = returnFieldList;
|
462
|
}
|
463
|
|
464
|
/**
|
465
|
* Accessor method to return a vector of the owner fields as defined in the
|
466
|
* <owner> tag in the pathquery dtd.
|
467
|
*/
|
468
|
public Vector getOwnerList()
|
469
|
{
|
470
|
return this.ownerList;
|
471
|
}
|
472
|
|
473
|
/**
|
474
|
* method to set the list of owners used to constrain this query
|
475
|
*/
|
476
|
public void setOwnerList(Vector ownerList)
|
477
|
{
|
478
|
this.ownerList = ownerList;
|
479
|
}
|
480
|
|
481
|
/**
|
482
|
* Accessor method to return a vector of the site fields as defined in the
|
483
|
* <site> tag in the pathquery dtd.
|
484
|
*/
|
485
|
public Vector getSiteList()
|
486
|
{
|
487
|
return this.siteList;
|
488
|
}
|
489
|
|
490
|
/**
|
491
|
* method to set the list of sites used to constrain this query
|
492
|
*/
|
493
|
public void setSiteList(Vector siteList)
|
494
|
{
|
495
|
this.siteList = siteList;
|
496
|
}
|
497
|
|
498
|
/**
|
499
|
* get the QueryGroup used to express query constraints
|
500
|
*/
|
501
|
public QueryGroup getQueryGroup()
|
502
|
{
|
503
|
return query;
|
504
|
}
|
505
|
|
506
|
/**
|
507
|
* set the querygroup
|
508
|
*/
|
509
|
public void setQueryGroup(QueryGroup group)
|
510
|
{
|
511
|
query = group;
|
512
|
}
|
513
|
|
514
|
/**
|
515
|
* set if this query sepcification has extendQuery(has return doc type or
|
516
|
* not)
|
517
|
*/
|
518
|
public void setContainsExtenedSQL(boolean hasExtenedQuery)
|
519
|
{
|
520
|
containsExtendedSQL = hasExtenedQuery;
|
521
|
}
|
522
|
|
523
|
/**
|
524
|
* Set up the SAX parser for reading the XML serialized query
|
525
|
*/
|
526
|
private XMLReader initializeParser()
|
527
|
{
|
528
|
XMLReader parser = null;
|
529
|
|
530
|
// Set up the SAX document handlers for parsing
|
531
|
try {
|
532
|
|
533
|
// Get an instance of the parser
|
534
|
parser = XMLReaderFactory.createXMLReader(parserName);
|
535
|
|
536
|
// Set the ContentHandler to this instance
|
537
|
parser.setContentHandler(this);
|
538
|
|
539
|
// Set the error Handler to this instance
|
540
|
parser.setErrorHandler(this);
|
541
|
|
542
|
} catch (Exception e) {
|
543
|
System.err.println("Error in QuerySpcecification.initializeParser "
|
544
|
+ e.toString());
|
545
|
}
|
546
|
|
547
|
return parser;
|
548
|
}
|
549
|
|
550
|
/**
|
551
|
* callback method used by the SAX Parser when the start tag of an element
|
552
|
* is detected. Used in this context to parse and store the query
|
553
|
* information in class variables.
|
554
|
*/
|
555
|
public void startElement(String uri, String localName, String qName,
|
556
|
Attributes atts) throws SAXException
|
557
|
{
|
558
|
BasicNode currentNode = new BasicNode(localName);
|
559
|
// add attributes to BasicNode here
|
560
|
if (atts != null) {
|
561
|
int len = atts.getLength();
|
562
|
for (int i = 0; i < len; i++) {
|
563
|
currentNode
|
564
|
.setAttribute(atts.getLocalName(i), atts.getValue(i));
|
565
|
}
|
566
|
}
|
567
|
|
568
|
elementStack.push(currentNode);
|
569
|
if (currentNode.getTagName().equals("querygroup")) {
|
570
|
QueryGroup currentGroup = new QueryGroup(currentNode
|
571
|
.getAttribute("operator"));
|
572
|
if (query == null) {
|
573
|
query = currentGroup;
|
574
|
} else {
|
575
|
QueryGroup parentGroup = (QueryGroup) queryStack.peek();
|
576
|
parentGroup.addChild(currentGroup);
|
577
|
}
|
578
|
queryStack.push(currentGroup);
|
579
|
}
|
580
|
}
|
581
|
|
582
|
/**
|
583
|
* callback method used by the SAX Parser when the end tag of an element is
|
584
|
* detected. Used in this context to parse and store the query information
|
585
|
* in class variables.
|
586
|
*/
|
587
|
public void endElement(String uri, String localName, String qName)
|
588
|
throws SAXException
|
589
|
{
|
590
|
BasicNode leaving = (BasicNode) elementStack.pop();
|
591
|
if (leaving.getTagName().equals("queryterm")) {
|
592
|
boolean isCaseSensitive = (new Boolean(leaving
|
593
|
.getAttribute("casesensitive"))).booleanValue();
|
594
|
QueryTerm currentTerm = null;
|
595
|
if (currentPathexpr == null) {
|
596
|
currentTerm = new QueryTerm(isCaseSensitive, leaving
|
597
|
.getAttribute("searchmode"), currentValue);
|
598
|
} else {
|
599
|
currentTerm = new QueryTerm(isCaseSensitive, leaving
|
600
|
.getAttribute("searchmode"), currentValue,
|
601
|
currentPathexpr);
|
602
|
}
|
603
|
QueryGroup currentGroup = (QueryGroup) queryStack.peek();
|
604
|
currentGroup.addChild(currentTerm);
|
605
|
currentValue = null;
|
606
|
currentPathexpr = null;
|
607
|
} else if (leaving.getTagName().equals("querygroup")) {
|
608
|
QueryGroup leavingGroup = (QueryGroup) queryStack.pop();
|
609
|
} else if (leaving.getTagName().equals("meta_file_id")) {
|
610
|
meta_file_id = textBuffer.toString().trim();
|
611
|
} else if (leaving.getTagName().equals("querytitle")) {
|
612
|
queryTitle = textBuffer.toString().trim();
|
613
|
} else if (leaving.getTagName().equals("value")) {
|
614
|
currentValue = textBuffer.toString().trim();
|
615
|
} else if (leaving.getTagName().equals("pathexpr")) {
|
616
|
currentPathexpr = textBuffer.toString().trim();
|
617
|
} else if (leaving.getTagName().equals("returndoctype")) {
|
618
|
returnDocList.add(textBuffer.toString().trim());
|
619
|
} else if (leaving.getTagName().equals("filterdoctype")) {
|
620
|
filterDocList.add(textBuffer.toString().trim());
|
621
|
} else if (leaving.getTagName().equals("returnfield")) {
|
622
|
handleReturnField(textBuffer.toString().trim());
|
623
|
} else if (leaving.getTagName().equals("filterdoctype")) {
|
624
|
filterDocList.add(textBuffer.toString().trim());
|
625
|
} else if (leaving.getTagName().equals("owner")) {
|
626
|
ownerList.add(textBuffer.toString().trim());
|
627
|
} else if (leaving.getTagName().equals("site")) {
|
628
|
siteList.add(textBuffer.toString().trim());
|
629
|
}
|
630
|
|
631
|
//rest textBuffer
|
632
|
textBuffer = null;
|
633
|
textBuffer = new StringBuffer();
|
634
|
|
635
|
}
|
636
|
|
637
|
/**
|
638
|
* callback method used by the SAX Parser when the text sequences of an xml
|
639
|
* stream are detected. Used in this context to parse and store the query
|
640
|
* information in class variables.
|
641
|
*/
|
642
|
public void characters(char ch[], int start, int length)
|
643
|
{
|
644
|
// buffer all text nodes for same element. This is for text was splited
|
645
|
// into different nodes
|
646
|
textBuffer.append(new String(ch, start, length));
|
647
|
|
648
|
}
|
649
|
|
650
|
/**
|
651
|
* Method to transfer string to return field
|
652
|
*/
|
653
|
public void handleReturnField(String inputString)
|
654
|
{
|
655
|
// make sure if return fields has an attribute or not
|
656
|
if (inputString.indexOf(ATTRIBUTESYMBOL) == -1) {
|
657
|
// no attribute value will be returned
|
658
|
returnFieldList.add(inputString);
|
659
|
containsExtendedSQL = true;
|
660
|
} else {
|
661
|
// has a attribute return field
|
662
|
// divied the return filed into two parts, one is path and the
|
663
|
// other is attribue name
|
664
|
String returnPath = newPathExpressionWithOutAttribute(inputString);
|
665
|
String attributeName = getAttributeName(inputString);
|
666
|
Vector pathInfo = new Vector();
|
667
|
// the vector has the information about return path and
|
668
|
// attributename
|
669
|
pathInfo.addElement(returnPath);
|
670
|
pathInfo.addElement(attributeName);
|
671
|
// put the vector into a hash table. The reseaon why don't put
|
672
|
// return path or attributename as a key is because they are not
|
673
|
// unique
|
674
|
attributeReturnList.put(new Integer(countAttributeReturnField),
|
675
|
pathInfo);
|
676
|
countAttributeReturnField++;
|
677
|
hasAttributeReturnField = true;
|
678
|
containsExtendedSQL = true;
|
679
|
}
|
680
|
}
|
681
|
|
682
|
/**
|
683
|
* create a SQL serialization of the query that this instance represents
|
684
|
*/
|
685
|
public String printSQL(boolean useXMLIndex)
|
686
|
{
|
687
|
|
688
|
StringBuffer self = new StringBuffer();
|
689
|
StringBuffer queryString = new StringBuffer();
|
690
|
|
691
|
queryString.append("SELECT docid,docname,doctype,");
|
692
|
queryString.append("date_created, date_updated, rev ");
|
693
|
queryString.append("FROM xml_documents WHERE");
|
694
|
|
695
|
// Get the query from the QueryGroup and check
|
696
|
// if no query has been returned
|
697
|
String queryFromQueryGroup = query.printSQL(useXMLIndex);
|
698
|
if(!queryFromQueryGroup.trim().equals("")){
|
699
|
self.append(" docid IN (");
|
700
|
self.append(queryFromQueryGroup);
|
701
|
self.append(") ");
|
702
|
}
|
703
|
|
704
|
// Add SQL to filter for doctypes requested in the query
|
705
|
// This is an implicit OR for the list of doctypes. Only doctypes in
|
706
|
// this
|
707
|
// list will be searched if the tag is present
|
708
|
if (!filterDocList.isEmpty()) {
|
709
|
boolean firstdoctype = true;
|
710
|
boolean emptyString = true;
|
711
|
|
712
|
if(!self.toString().equals("")){
|
713
|
self.append(" AND (");
|
714
|
emptyString = false;
|
715
|
}
|
716
|
|
717
|
Enumeration en = filterDocList.elements();
|
718
|
while (en.hasMoreElements()) {
|
719
|
String currentDoctype = (String) en.nextElement();
|
720
|
if (firstdoctype) {
|
721
|
firstdoctype = false;
|
722
|
self.append(" doctype = '" + currentDoctype + "'");
|
723
|
} else {
|
724
|
self.append(" OR doctype = '" + currentDoctype + "'");
|
725
|
}
|
726
|
}
|
727
|
|
728
|
if(!emptyString){
|
729
|
self.append(") ");
|
730
|
}
|
731
|
}
|
732
|
|
733
|
// Add SQL to filter for owners requested in the query
|
734
|
// This is an implicit OR for the list of owners
|
735
|
if (!ownerList.isEmpty()) {
|
736
|
boolean first = true;
|
737
|
boolean emptyString = true;
|
738
|
|
739
|
if(!self.toString().equals("")){
|
740
|
self.append(" AND (");
|
741
|
emptyString = false;
|
742
|
}
|
743
|
|
744
|
Enumeration en = ownerList.elements();
|
745
|
while (en.hasMoreElements()) {
|
746
|
String current = (String) en.nextElement();
|
747
|
if (current != null) {
|
748
|
current = current.toLowerCase();
|
749
|
}
|
750
|
if (first) {
|
751
|
first = false;
|
752
|
self.append(" lower(user_owner) = '" + current + "'");
|
753
|
} else {
|
754
|
self.append(" OR lower(user_owner) = '" + current + "'");
|
755
|
}
|
756
|
}
|
757
|
|
758
|
if(!emptyString){
|
759
|
self.append(") ");
|
760
|
}
|
761
|
}
|
762
|
|
763
|
// Add SQL to filter for sites requested in the query
|
764
|
// This is an implicit OR for the list of sites
|
765
|
if (!siteList.isEmpty()) {
|
766
|
boolean first = true;
|
767
|
boolean emptyString = true;
|
768
|
|
769
|
if(!self.toString().equals("")){
|
770
|
self.append(" AND (");
|
771
|
emptyString = false;
|
772
|
}
|
773
|
|
774
|
Enumeration en = siteList.elements();
|
775
|
while (en.hasMoreElements()) {
|
776
|
String current = (String) en.nextElement();
|
777
|
if (first) {
|
778
|
first = false;
|
779
|
self.append(" SUBSTR(docid, 1, INSTR(docid, '"
|
780
|
+ accNumberSeparator + "')-1) = '" + current + "'");
|
781
|
} else {
|
782
|
self.append(" OR SUBSTR(docid, 1, INSTR(docid, '"
|
783
|
+ accNumberSeparator + "')-1) = '" + current + "'");
|
784
|
}
|
785
|
}
|
786
|
|
787
|
if(!emptyString){
|
788
|
self.append(") ");
|
789
|
}
|
790
|
}
|
791
|
|
792
|
// if there is only one percentage search item, this query is a
|
793
|
// percentage
|
794
|
// search query
|
795
|
MetaCatUtil.debugMessage("percentage number: "
|
796
|
+ query.getPercentageSymbolCount(), 35);
|
797
|
if (query.getPercentageSymbolCount() == 1) {
|
798
|
MetaCatUtil.debugMessage("it is a percentage search", 30);
|
799
|
percentageSearch = true;
|
800
|
}
|
801
|
|
802
|
queryString.append(self.toString());
|
803
|
return queryString.toString();
|
804
|
}
|
805
|
|
806
|
/**
|
807
|
* This sql command will selecet startnodeid and endnodeid that user can
|
808
|
* NOT access
|
809
|
*/
|
810
|
public String printAccessControlSQLForReturnField(String doclist)
|
811
|
{
|
812
|
StringBuffer sql = new StringBuffer();
|
813
|
String allowString = constructAllowString();
|
814
|
String denyString = constructDenyString();
|
815
|
sql.append("SELECT distinct startnodeid, endnodeid from xml_access ");
|
816
|
sql.append("WHERE docid in (");
|
817
|
sql.append(doclist);
|
818
|
sql.append(") AND startnodeid IS NOT NULL AND ");
|
819
|
sql.append("(");
|
820
|
sql.append("(");
|
821
|
sql
|
822
|
.append("startnodeid NOT IN (SELECT startnodeid from xml_access, xml_documents ");
|
823
|
sql.append(" WHERE xml_access.docid = xml_documents.docid");
|
824
|
sql.append(" AND lower(xml_documents.user_owner) ='");
|
825
|
sql.append(userName);
|
826
|
sql.append("' AND xml_access.startnodeid IS NOT NULL)");
|
827
|
sql.append(")");
|
828
|
sql.append(" AND ");
|
829
|
sql.append("(");
|
830
|
sql
|
831
|
.append("(startnodeid NOT IN (SELECT startnodeid from xml_access where( ");
|
832
|
sql.append(allowString);
|
833
|
sql.append(") AND (startnodeid IS NOT NULL))");
|
834
|
sql.append(")");
|
835
|
sql
|
836
|
.append(" OR (startnodeid IN (SELECT startnodeid from xml_access where( ");
|
837
|
sql.append(denyString);
|
838
|
sql.append(") AND (startnodeid IS NOT NULL))");
|
839
|
sql.append(")");
|
840
|
sql.append(")");
|
841
|
sql.append(")");
|
842
|
MetaCatUtil.debugMessage("accessControlSQLForReturnField: "
|
843
|
+ sql.toString(), 30);
|
844
|
return sql.toString();
|
845
|
}
|
846
|
|
847
|
/**
|
848
|
* This method prints sql based upon the <returnfield> tag in the
|
849
|
* pathquery document. This allows for customization of the returned fields.
|
850
|
* If the boolean useXMLIndex paramter is false, it uses a recursive query on
|
851
|
* xml_nodes to find the fields to be included by their path expression, and
|
852
|
* avoids the use of the xml_index table.
|
853
|
*
|
854
|
* @param doclist the list of document ids to search
|
855
|
* @param unaccessableNodePair the node pairs (start id and end id) which
|
856
|
* this user should not access
|
857
|
* @param useXMLIndex a boolean flag indicating whether to search using
|
858
|
* xml_index
|
859
|
*/
|
860
|
public String printExtendedSQL(String doclist,
|
861
|
Hashtable unaccessableNodePair, boolean useXMLIndex)
|
862
|
{
|
863
|
if (useXMLIndex) {
|
864
|
return printExtendedSQL(doclist, unaccessableNodePair);
|
865
|
} else {
|
866
|
StringBuffer self = new StringBuffer();
|
867
|
|
868
|
boolean firstfield = true;
|
869
|
//put the returnfields into the query
|
870
|
//the for loop allows for multiple fields
|
871
|
for (int i = 0; i < returnFieldList.size(); i++) {
|
872
|
if (firstfield) {
|
873
|
firstfield = false;
|
874
|
} else {
|
875
|
self.append(" UNION ");
|
876
|
}
|
877
|
String path = (String) returnFieldList.elementAt(i);
|
878
|
self.append("select xml_nodes.docid, ");
|
879
|
self.append("'"+ path + "' as path, xml_nodes.nodedata, ");
|
880
|
self.append("xml_nodes.parentnodeid ");
|
881
|
self.append("from xml_nodes, xml_documents ");
|
882
|
self.append("where parentnodeid IN ");
|
883
|
self.append(QueryTerm.useNestedStatements(path));
|
884
|
|
885
|
self.append(" AND xml_nodes.docid in (");
|
886
|
self.append(doclist);
|
887
|
self.append(") AND xml_nodes.nodetype = 'TEXT'");
|
888
|
self.append(" AND xml_nodes.rootnodeid = xml_documents.rootnodeid");
|
889
|
|
890
|
addAccessRestrictionSQL(unaccessableNodePair, self);
|
891
|
}
|
892
|
|
893
|
return self.toString();
|
894
|
}
|
895
|
}
|
896
|
|
897
|
/**
|
898
|
* This method prints sql based upon the <returnfield> tag in the
|
899
|
* pathquery document. This allows for customization of the returned fields.
|
900
|
* It uses the xml_index table and so assumes that this table has been
|
901
|
* built.
|
902
|
*
|
903
|
* @param doclist the list of document ids to search
|
904
|
* @param unaccessableNodePair the node pairs (start id and end id)
|
905
|
* which this user should not access
|
906
|
*/
|
907
|
public String printExtendedSQL(String doclist,
|
908
|
Hashtable unaccessableNodePair)
|
909
|
{
|
910
|
StringBuffer self = new StringBuffer();
|
911
|
self.append("select path, docid, nodedata, ");
|
912
|
self.append("nodeid ");
|
913
|
self.append("from xml_index where (path like '");
|
914
|
boolean firstfield = true;
|
915
|
//put the returnfields into the query
|
916
|
//the for loop allows for multiple fields
|
917
|
for (int i = 0; i < returnFieldList.size(); i++) {
|
918
|
if (firstfield) {
|
919
|
firstfield = false;
|
920
|
self.append((String) returnFieldList.elementAt(i));
|
921
|
self.append("' ");
|
922
|
} else {
|
923
|
self.append("or path like '");
|
924
|
self.append((String) returnFieldList.elementAt(i));
|
925
|
self.append("' ");
|
926
|
}
|
927
|
}
|
928
|
self.append(") AND docid in (");
|
929
|
self.append(doclist);
|
930
|
self.append(")");
|
931
|
//self.append(") AND xml_nodes.nodetype = 'TEXT'");
|
932
|
|
933
|
addAccessRestrictionSQL(unaccessableNodePair, self);
|
934
|
|
935
|
return self.toString();
|
936
|
}
|
937
|
|
938
|
/**
|
939
|
* Create the SQl necessary to restrict access to allowed nodes. This is
|
940
|
* accomplished by restricting the nodes that are returned to include
|
941
|
* only those whose IDs fall outside of a set of start/stop pairs of
|
942
|
* nodeid values. These pairs are passed in as a hash, with the key
|
943
|
* containing the start nodeid and the value containing the end nodeid.
|
944
|
* Any nodes between these start and end nodeid values will be excluded
|
945
|
* from the results.
|
946
|
*
|
947
|
* @param unaccessableNodePair hash of start/end nodeid pairs to restrict
|
948
|
* @param self a stringbuffer to which the genrated SQL is appended
|
949
|
*/
|
950
|
private void addAccessRestrictionSQL(Hashtable unaccessableNodePair,
|
951
|
StringBuffer self)
|
952
|
{
|
953
|
// add control part for extended query
|
954
|
Enumeration en = unaccessableNodePair.keys();
|
955
|
|
956
|
while (en.hasMoreElements()) {
|
957
|
// Get control pairs in object
|
958
|
Long startNodeIdObject = (Long) en.nextElement();
|
959
|
Long endNodeIdObject = (Long) unaccessableNodePair
|
960
|
.get(startNodeIdObject);
|
961
|
// change it to long
|
962
|
long startNodeId = startNodeIdObject.longValue();
|
963
|
long endNodeId = endNodeIdObject.longValue();
|
964
|
// add into query
|
965
|
self.append(" AND ( xml_nodes.nodeid < ");
|
966
|
self.append(startNodeId);
|
967
|
self.append(" OR xml_nodes.nodeid > ");
|
968
|
self.append(endNodeId);
|
969
|
self.append(")");
|
970
|
}
|
971
|
}
|
972
|
|
973
|
/**
|
974
|
* This method prints sql that finds the values of attributes in the xml
|
975
|
* documents based upon the whether the returnfield tag in the pathquery
|
976
|
* document has an attribute symbol (@). This allows for customization of
|
977
|
* the returned fields.
|
978
|
*
|
979
|
* @param doclist the list of document ids to search
|
980
|
* @param useXMLIndex a boolean flag indicating whether to search using
|
981
|
* xml_index
|
982
|
*/
|
983
|
public String printAttributeQuery(String doclist, boolean useXMLIndex)
|
984
|
{
|
985
|
if (useXMLIndex) {
|
986
|
return printAttributeQuery(doclist);
|
987
|
} else {
|
988
|
StringBuffer self = new StringBuffer();
|
989
|
boolean firstfield = true;
|
990
|
//put the returnfields attributes into the query
|
991
|
//the for loop allows for multiple fields and attributes
|
992
|
Enumeration returnAttributes = attributeReturnList.elements();
|
993
|
while (returnAttributes.hasMoreElements()) {
|
994
|
Vector currentVector = (Vector) returnAttributes.nextElement();
|
995
|
String returnPath = (String) currentVector.elementAt(0);
|
996
|
String attributeName = (String) currentVector.elementAt(1);
|
997
|
if (firstfield) {
|
998
|
firstfield = false;
|
999
|
} else {
|
1000
|
self.append(" UNION ");
|
1001
|
}
|
1002
|
self.append("select xml_nodes.docid, '");
|
1003
|
self.append(returnPath);
|
1004
|
self.append("' as path, xml_nodes.nodedata, xml_nodes.nodename ");
|
1005
|
self.append("from xml_nodes, xml_documents ");
|
1006
|
self.append("where parentnodeid IN ");
|
1007
|
self.append(QueryTerm.useNestedStatements(returnPath));
|
1008
|
self.append(" AND xml_nodes.nodename like '");
|
1009
|
self.append(attributeName);
|
1010
|
self.append("' AND xml_nodes.docid in (");
|
1011
|
self.append(doclist);
|
1012
|
self.append(") AND xml_nodes.nodetype = 'ATTRIBUTE'");
|
1013
|
self.append(" AND xml_nodes.rootnodeid = xml_documents.rootnodeid");
|
1014
|
}
|
1015
|
|
1016
|
MetaCatUtil.debugMessage("Attribute query: " + self.toString(), 30);
|
1017
|
|
1018
|
return self.toString();
|
1019
|
}
|
1020
|
}
|
1021
|
|
1022
|
/**
|
1023
|
* This method prints sql that finds the values of attributes in the xml
|
1024
|
* documents based upon the whether the returnfield tag in the pathquery
|
1025
|
* document has an attribute symbol (@). This allows for customization of
|
1026
|
* the returned fields.
|
1027
|
*
|
1028
|
* @param doclist the list of document ids to search
|
1029
|
*/
|
1030
|
public String printAttributeQuery(String doclist)
|
1031
|
{
|
1032
|
StringBuffer self = new StringBuffer();
|
1033
|
self.append("select xml_nodes.docid, xml_index.path, ");
|
1034
|
self.append("xml_nodes.nodedata, xml_nodes.nodename ");
|
1035
|
self.append("from xml_index, xml_nodes where xml_index.nodeid=");
|
1036
|
self.append("xml_nodes.parentnodeid and (");
|
1037
|
boolean firstfield = true;
|
1038
|
//put the returnfields attributes into the query
|
1039
|
//the for loop allows for multiple fields and attributes
|
1040
|
Enumeration returnAttributes = attributeReturnList.elements();
|
1041
|
while (returnAttributes.hasMoreElements()) {
|
1042
|
Vector currentVector = (Vector) returnAttributes.nextElement();
|
1043
|
String returnPath = (String) currentVector.elementAt(0);
|
1044
|
String attributeName = (String) currentVector.elementAt(1);
|
1045
|
if (firstfield) {
|
1046
|
firstfield = false;
|
1047
|
self.append("( xml_index.path like '");
|
1048
|
self.append(returnPath);
|
1049
|
self.append("' AND xml_nodes.nodename like '");
|
1050
|
self.append(attributeName);
|
1051
|
self.append("') ");
|
1052
|
} else {
|
1053
|
self.append(" or ( xml_index.path like '");
|
1054
|
self.append(returnPath);
|
1055
|
self.append("' AND xml_nodes.nodename like '");
|
1056
|
self.append(attributeName);
|
1057
|
self.append("') ");
|
1058
|
}
|
1059
|
}
|
1060
|
self.append(") AND xml_nodes.docid in (");
|
1061
|
self.append(doclist);
|
1062
|
self.append(") AND xml_nodes.nodetype = 'ATTRIBUTE'");
|
1063
|
MetaCatUtil.debugMessage("Attribute query: " + self.toString(), 30);
|
1064
|
|
1065
|
return self.toString();
|
1066
|
}
|
1067
|
|
1068
|
public static String printRelationSQL(String docid)
|
1069
|
{
|
1070
|
StringBuffer self = new StringBuffer();
|
1071
|
self.append("select subject, relationship, object, subdoctype, ");
|
1072
|
self.append("objdoctype from xml_relation ");
|
1073
|
self.append("where docid like '").append(docid).append("'");
|
1074
|
return self.toString();
|
1075
|
}
|
1076
|
|
1077
|
public static String printGetDocByDoctypeSQL(String docid)
|
1078
|
{
|
1079
|
StringBuffer self = new StringBuffer();
|
1080
|
|
1081
|
self.append("SELECT docid,docname,doctype,");
|
1082
|
self.append("date_created, date_updated ");
|
1083
|
self.append("FROM xml_documents WHERE docid IN (");
|
1084
|
self.append(docid).append(")");
|
1085
|
return self.toString();
|
1086
|
}
|
1087
|
|
1088
|
/**
|
1089
|
* create a String description of the query that this instance represents.
|
1090
|
* This should become a way to get the XML serialization of the query.
|
1091
|
*/
|
1092
|
public String toString()
|
1093
|
{
|
1094
|
return "meta_file_id=" + meta_file_id + "\n" + query;
|
1095
|
//DOCTITLE attr cleared from the db
|
1096
|
//return "meta_file_id=" + meta_file_id + "\n" +
|
1097
|
//"querytitle=" + querytitle + "\n" + query;
|
1098
|
}
|
1099
|
|
1100
|
/** A method to get rid of attribute part in path expression */
|
1101
|
public static String newPathExpressionWithOutAttribute(String pathExpression)
|
1102
|
{
|
1103
|
if (pathExpression == null) { return null; }
|
1104
|
int index = pathExpression.lastIndexOf(ATTRIBUTESYMBOL);
|
1105
|
String newExpression = null;
|
1106
|
if (index != 1) {
|
1107
|
newExpression = pathExpression.substring(0, index - 1);
|
1108
|
}
|
1109
|
MetaCatUtil.debugMessage("The path expression without attributes: "
|
1110
|
+ newExpression, 30);
|
1111
|
return newExpression;
|
1112
|
}
|
1113
|
|
1114
|
/** A method to get attribute name from path */
|
1115
|
public static String getAttributeName(String path)
|
1116
|
{
|
1117
|
if (path == null) { return null; }
|
1118
|
int index = path.lastIndexOf(ATTRIBUTESYMBOL);
|
1119
|
int size = path.length();
|
1120
|
String attributeName = null;
|
1121
|
if (index != 1) {
|
1122
|
attributeName = path.substring(index + 1, size);
|
1123
|
}
|
1124
|
MetaCatUtil.debugMessage("The attirbute name from path: "
|
1125
|
+ attributeName, 30);
|
1126
|
return attributeName;
|
1127
|
}
|
1128
|
|
1129
|
}
|