Project

General

Profile

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