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