Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that represents a structured query, and can be
4
 *             constructed from an XML serialization conforming to
5
 *             pathquery.dtd. The printSQL() method can be used to print
6
 *             a SQL serialization of the query.
7
 *  Copyright: 2000 Regents of the University of California and the
8
 *             National Center for Ecological Analysis and Synthesis
9
 *    Authors: Matt Jones
10
 *
11
 *   '$Author: leinfelder $'
12
 *     '$Date: 2013-02-22 11:07:31 -0800 (Fri, 22 Feb 2013) $'
13
 * '$Revision: 7495 $'
14
 *
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
 */
29

    
30
package edu.ucsb.nceas.metacat;
31

    
32
import java.io.IOException;
33
import java.io.Reader;
34
import java.io.StringReader;
35
import java.util.ArrayList;
36
import java.util.Enumeration;
37
import java.util.List;
38
import java.util.Stack;
39
import java.util.Vector;
40

    
41
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
42
import edu.ucsb.nceas.metacat.util.MetacatUtil;
43
import edu.ucsb.nceas.metacat.util.SystemUtil;
44
//import edu.ucsb.nceas.utilities.UtilException;
45

    
46
import org.apache.log4j.Logger;
47
import org.xml.sax.Attributes;
48
import org.xml.sax.InputSource;
49
import org.xml.sax.SAXException;
50
import org.xml.sax.XMLReader;
51
import org.xml.sax.helpers.DefaultHandler;
52
import org.xml.sax.helpers.XMLReaderFactory;
53
import java.util.Iterator;
54

    
55
/**
56
 * A Class that represents a structured query, and can be constructed from an
57
 * XML serialization conforming to
58
 *
59
 * @see pathquery.dtd. The printSQL() method can be used to print a SQL
60
 *      serialization of the query.
61
 */
62
public class QuerySpecification extends DefaultHandler
63
{
64

    
65
    /** flag determining whether extended query terms are present */
66
    private boolean containsExtendedSQL = false;
67

    
68
    /** flag determining whether predicates are present */
69
    private boolean containsPredicates = false;
70

    
71
    /** Identifier for this query document */
72
    private String meta_file_id;
73

    
74
    /** 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
    
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

    
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
    
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

    
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
    public static final char PREDICATE_START = '[';
126

    
127
    public static final char PREDICATE_END = ']';
128

    
129
    //private boolean hasAttributeReturnField = false;
130

    
131
    //private Hashtable attributeReturnList = new Hashtable();
132

    
133
    //private int countAttributeReturnField = 0;
134

    
135
    private StringBuffer textBuffer = new StringBuffer();
136
    
137
   
138
    private static Logger logMetacat = Logger.getLogger(QuerySpecification.class);
139

    
140
    /**
141
     * construct an instance of the QuerySpecification class
142
     *
143
     * @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
        returnFieldListWithPredicates = new Vector();
162
        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
        	logMetacat.error("QuerySpecification() - SAX parser not instantiated properly.");
170
        }
171
        try {
172
            parser.parse(new InputSource(queryspec));
173
        } catch (SAXException se) {
174
            logMetacat.error("QuerySpecification() - SAX error parsing data: " + se.getMessage());
175
        }
176
    }
177

    
178
    /**
179
     * construct an instance of the QuerySpecification class
180
     *
181
     * @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
    }
193

    
194
    /**
195
     * construct an instance of the QuerySpecification class which don't need
196
     * to parser a xml document
197
     *
198
     * @param accNumberSeparator
199
     *            the separator between doc version
200
     */
201
    public QuerySpecification(String accNumberSeparator) throws IOException
202
    {
203
        // 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
        returnFieldListWithPredicates = new Vector();
210
        ownerList = new Vector();
211
        this.accNumberSeparator = accNumberSeparator;
212
    }
213

    
214
    /**
215
     * Method to set user name
216
     *
217
     * @param myName
218
     *            the user name
219
     */
220
    public void setUserName(String myName)
221
    {
222
        //to lower case
223
        if (myName != null) {
224
            this.userName = myName.toLowerCase();
225
        } else {
226
            this.userName = myName;
227
        }
228
    }
229

    
230
    /**
231
     * Method to set user group
232
     *
233
     * @param myGroup
234
     *            the user group
235
     */
236
    public void setGroup(String[] myGroup)
237
    {
238
        this.group = myGroup;
239
    }
240

    
241
    /**
242
     * Method to indicate this query is a percentage search
243
     */
244
    public boolean isPercentageSearch()
245
    {
246
        return percentageSearch;
247
    }
248

    
249
    /*
250
     * Method to get owner query. If it is owner it has all permission
251
     */
252
    private String createOwnerQuery()
253
    {
254
        String ownerQuery = null;
255
        //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
        }
263
        logMetacat.info("QuerySpecification.createOwerQuery - OwnerQuery: " + ownerQuery);
264
        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
    {
273
        String allowQuery = null;
274
        String allowString = constructAllowString();
275
        allowQuery = "SELECT guid from xml_access  " +
276
        		"WHERE ( " + allowString;
277
        allowQuery = allowQuery + ")";
278
        logMetacat.info("QuerySpecification.createAllowRuleQuery - allow query is: " + allowQuery);
279
        return allowQuery;
280

    
281
    }
282

    
283
    /* Method to construct a allow rule string */
284
    private String constructAllowString()
285
    {
286
        String allowQuery = "";
287
        
288
       // add public
289
        allowQuery = "(lower(principal_name) = '" + PUBLIC
290
                + "'";
291
                
292
        // add user name
293
        if (userName != null && !userName.equals("") && !userName.equalsIgnoreCase(PUBLIC)) {
294
            allowQuery = allowQuery + "OR lower(principal_name) = '" + userName +"'";
295
                    
296
        }
297
        // add  group
298
        if (group != null) {
299
            for (int i = 0; i < group.length; i++) {
300
                String groupUint = group[i];
301
                if (groupUint != null && !groupUint.equals("")) {
302
                    groupUint = groupUint.toLowerCase();
303
                    allowQuery = allowQuery + " OR lower(principal_name) = '"
304
                            + groupUint + "'";
305
                }//if
306
            }//for
307
        }//if
308
        // add allow rule
309
        allowQuery = allowQuery + ") AND perm_type = 'allow'" + " AND permission > 3";
310
        logMetacat.info("QuerySpecification.constructAllowString - allow string is: " + allowQuery);
311
        return allowQuery;
312
    }
313

    
314
    /*
315
     * Method to create query for xml_access, this part is to get docid list
316
     * which have a deny rule and perm_order is allowFirst for a given user.
317
     * This means the user will be denied to read
318
     */
319
    private String createDenyRuleQuery()
320
    {
321
        String denyQuery = null;
322
        String denyString = constructDenyString();
323
        denyQuery = "SELECT guid from xml_access " +
324
        		"WHERE ( " + denyString;
325
        denyQuery = denyQuery + ") ";
326
        logMetacat.info("QuerySpecification.createDenyRuleQuery - denyquery is: " + denyQuery);
327
        return denyQuery;
328

    
329
    }
330

    
331
    /* Construct deny string */
332
    private String constructDenyString()
333
    {
334
        String denyQuery = "";
335
         
336
        // add public
337
        denyQuery = "(lower(principal_name) = '" + PUBLIC
338
                 + "'";
339
                 
340
         // add user name
341
         if (userName != null && !userName.equals("") && !userName.equalsIgnoreCase(PUBLIC)) {
342
        	 denyQuery = denyQuery + "OR lower(principal_name) = '" + userName +"'";
343
                     
344
         }
345
         // add  groups
346
         if (group != null) {
347
             for (int i = 0; i < group.length; i++) {
348
                 String groupUint = group[i];
349
                 if (groupUint != null && !groupUint.equals("")) {
350
                     groupUint = groupUint.toLowerCase();
351
                     denyQuery = denyQuery + " OR lower(principal_name) = '"
352
                             + groupUint + "'";
353
                 }//if
354
             }//for
355
         }//if
356
         // add deny rules
357
         denyQuery = denyQuery + ") AND perm_type = 'deny'" +  " AND perm_order ='allowFirst'" +" AND permission > 3";
358
         logMetacat.info("QuerySpecification.constructDenyString - deny string is: " + denyQuery);
359
         return denyQuery;
360
        
361
    }
362

    
363
    /**
364
     * Method to append a access control query to SQL. So in DBQuery class, we
365
     * can get docid from both user specified query and access control query.
366
     * We don't need to checking permission after we get the doclist. It will
367
     * be good to performance
368
     *
369
     */
370
    public String getAccessQuery()
371
    {
372
        String accessQuery = null;
373
        String owner = createOwnerQuery();
374
        String allow = createAllowRuleQuery();
375
        String deny = createDenyRuleQuery();
376

    
377
        if (owner != null)
378
        {
379
          accessQuery = " AND (xml_documents.docid IN (" + owner + ")";
380
          accessQuery = accessQuery + " OR (identifier.guid IN (" + allow + ")"
381
                + " AND identifier.guid NOT IN (" + deny + ")))";
382
        }
383
        else
384
        {
385
        	accessQuery = " AND (identifier.guid IN (" + allow + ")"
386
                + " AND identifier.guid NOT IN (" + deny + "))";
387
        }
388
        logMetacat.info("QuerySpecification.getAccessQuery - access query is: " + accessQuery);
389
        return accessQuery;
390
    }
391

    
392
    /**
393
     * Returns true if the parsed query contains and extended xml query (i.e.
394
     * there is at least one &lt;returnfield&gt; in the pathquery document)
395
     */
396
    public boolean containsExtendedSQL()
397
    {
398
        if (containsExtendedSQL) {
399
            return true;
400
        } else {
401
            return false;
402
        }
403
    }
404

    
405
  
406
    /**
407
     * Accessor method to return the identifier of this Query
408
     */
409
    public String getIdentifier()
410
    {
411
        return meta_file_id;
412
    }
413

    
414
    /**
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

    
422
    /**
423
     * Accessor method to return the title of this Query
424
     */
425
    public String getQueryTitle()
426
    {
427
        return queryTitle;
428
    }
429

    
430
    /**
431
     * method to set the title of this query
432
     */
433
    public void setQueryTitle(String title)
434
    {
435
        this.queryTitle = title;
436
    }
437

    
438
    /**
439
     * Accessor method to return a vector of the return document types as
440
     * defined in the &lt;returndoctype&gt; tag in the pathquery dtd.
441
     */
442
    public Vector getReturnDocList()
443
    {
444
        return this.returnDocList;
445
    }
446

    
447
    /**
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

    
455
    /**
456
     * Accessor method to return a vector of the filter doc types as defined in
457
     * the &lt;filterdoctype&gt; tag in the pathquery dtd.
458
     */
459
    public Vector getFilterDocList()
460
    {
461
        return this.filterDocList;
462
    }
463

    
464
    /**
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

    
472
    /**
473
     * Accessor method to return a vector of the extended return fields as
474
     * defined in the &lt;returnfield&gt; tag in the pathquery dtd.
475
     */
476
    public Vector getReturnFieldList()
477
    {
478
        return this.returnFieldList;
479
    }
480

    
481
    /**
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

    
489
    /**
490
     * Accessor method to return a vector of the owner fields as defined in the
491
     * &lt;owner&gt; tag in the pathquery dtd.
492
     */
493
    public Vector getOwnerList()
494
    {
495
        return this.ownerList;
496
    }
497

    
498
    /**
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
    }
505

    
506
    /**
507
     * get the QueryGroup used to express query constraints
508
     */
509
    public QueryGroup getQueryGroup()
510
    {
511
        return query;
512
    }
513

    
514
    /**
515
     * set the querygroup
516
     */
517
    public void setQueryGroup(QueryGroup group)
518
    {
519
        query = group;
520
    }
521

    
522
    /**
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

    
531
    /**
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
            logMetacat.error("QuerySpecification.getAccessQuery - Error: " + e.getMessage());
552
        }
553

    
554
        return parser;
555
    }
556

    
557
    /**
558
     * callback method used by the SAX Parser when the start tag of an element
559
     * is detected. Used in this context to parse and store the query
560
     * information in class variables.
561
     */
562
    public void startElement(String uri, String localName, String qName,
563
            Attributes atts) throws SAXException
564
    {
565
        logMetacat.debug("QuerySpecification.startElement - start element " + localName);
566
        BasicNode currentNode = new BasicNode(localName);
567
        //write element name into xml buffer.
568
        xml.append("<");
569
        xml.append(localName);
570
        // add attributes to BasicNode here
571
        if (atts != null) {
572
            int len = atts.getLength();
573
            for (int i = 0; i < len; i++) {
574
                currentNode
575
                        .setAttribute(atts.getLocalName(i), atts.getValue(i));
576
                xml.append(" ");
577
                xml.append(atts.getLocalName(i));
578
                xml.append("=\"");
579
                xml.append(atts.getValue(i));
580
                xml.append("\"");
581
            }
582
        }
583
        xml.append(">");
584

    
585
        elementStack.push(currentNode);
586
        if (currentNode.getTagName().equals("querygroup")) {
587
            QueryGroup currentGroup = new QueryGroup(currentNode
588
                    .getAttribute("operator"));
589
            if (query == null) {
590
                query = currentGroup;
591
            } else {
592
                QueryGroup parentGroup = (QueryGroup) queryStack.peek();
593
                parentGroup.addChild(currentGroup);
594
            }
595
            queryStack.push(currentGroup);
596
        }
597
        logMetacat.debug("QuerySpecification.startElement - ending startElement " + localName);
598
    }
599

    
600
    /**
601
     * callback method used by the SAX Parser when the end tag of an element is
602
     * detected. Used in this context to parse and store the query information
603
     * in class variables.
604
     */
605
    public void endElement(String uri, String localName, String qName)
606
            throws SAXException
607
    {
608
    	 logMetacat.debug("QuerySpecification.endElement - endElement "+localName);
609
        BasicNode leaving = (BasicNode) elementStack.pop();
610
        if (leaving.getTagName().equals("queryterm")) {
611
            boolean isCaseSensitive = (new Boolean(leaving
612
                    .getAttribute("casesensitive"))).booleanValue();
613
            QueryTerm currentTerm = null;
614
            if (currentPathexpr == null) {
615
                currentTerm = new QueryTerm(isCaseSensitive, leaving
616
                        .getAttribute("searchmode"), currentValue);
617
            } else {
618
                currentTerm = new QueryTerm(isCaseSensitive, leaving
619
                        .getAttribute("searchmode"), currentValue,
620
                        currentPathexpr);
621
            }
622
            QueryGroup currentGroup = (QueryGroup) queryStack.peek();
623
            currentGroup.addChild(currentTerm);
624
            currentValue = null;
625
            currentPathexpr = null;
626
        } else if (leaving.getTagName().equals("querygroup")) {
627
            QueryGroup leavingGroup = (QueryGroup) queryStack.pop();
628
        } else if (leaving.getTagName().equals("meta_file_id")) {
629
              meta_file_id = textBuffer.toString().trim();
630
        } else if (leaving.getTagName().equals("querytitle")) {
631
              queryTitle = textBuffer.toString().trim();
632
        } else if (leaving.getTagName().equals("value")) {
633
              currentValue = textBuffer.toString().trim();
634
              currentValue = MetacatUtil.normalize(currentValue);
635
        } else if (leaving.getTagName().equals("pathexpr")) {
636
              currentPathexpr = textBuffer.toString().trim();
637
        } else if (leaving.getTagName().equals("returndoctype")) {
638
              returnDocList.add(textBuffer.toString().trim());
639
        } else if (leaving.getTagName().equals("filterdoctype")) {
640
              filterDocList.add(textBuffer.toString().trim());
641
        } else if (leaving.getTagName().equals("returnfield")) {
642
              handleReturnField(textBuffer.toString().trim());
643
        } else if (leaving.getTagName().equals("filterdoctype")) {
644
              filterDocList.add(textBuffer.toString().trim());
645
        } else if (leaving.getTagName().equals("owner")) {
646
              ownerList.add(textBuffer.toString().trim());
647
        }
648
        String normalizedXML = textBuffer.toString().trim();
649
        logMetacat.debug("QuerySpecification.endElement - before normalize: " + normalizedXML);
650
        normalizedXML =  MetacatUtil.normalize(normalizedXML);
651
        logMetacat.debug("QuerySpecification.endElement - after normalize " + normalizedXML);
652
        xml.append(normalizedXML);
653
        xml.append("</");
654
        xml.append(localName);
655
        xml.append(">");
656
        //rest textBuffer
657
        textBuffer = new StringBuffer();
658

    
659
    }
660
    
661
    /**
662
     * Gets normailized query string in xml format, which can be transformed
663
     * to html
664
     */
665
    public String getNormalizedXMLQuery()
666
    {
667
    	//System.out.println("normailized xml \n"+xml.toString());
668
    	return xml.toString();
669
    }
670
    
671

    
672
    /**
673
     * callback method used by the SAX Parser when the text sequences of an xml
674
     * stream are detected. Used in this context to parse and store the query
675
     * information in class variables.
676
     */
677
    public void characters(char ch[], int start, int length)
678
    {
679
      // buffer all text nodes for same element. This is for text was splited
680
      // into different nodes
681
      String text = new String(ch, start, length);
682
      logMetacat.debug("QuerySpecification.characters - the text in characters " + text);
683
      textBuffer.append(text);
684

    
685
    }
686

    
687
   /**
688
    * Method to handle return field. It will be callied in ecogrid part
689
    * @param inputString
690
    */
691
    public void handleReturnField(String inputString)
692
    {
693
        int attributePos = inputString.indexOf(ATTRIBUTESYMBOL);
694
        int predicateStart = -1;
695
        int predicateEnd;
696
        boolean hasPredicate = false;
697

    
698
        while (true)
699
        {
700
            predicateStart = inputString.indexOf(PREDICATE_START, predicateStart + 1);
701

    
702
            if (attributePos == -1)
703
                break;
704

    
705
            if (predicateStart == -1)
706
                break;
707

    
708
            hasPredicate = true;
709

    
710
            if (attributePos < predicateStart)
711
                break;
712

    
713
            predicateEnd = inputString.indexOf(PREDICATE_END, predicateStart);
714

    
715
            if (predicateEnd == -1)
716
            {
717
                logMetacat.warn("QuerySpecification.handleReturnField - Invalid path: " + inputString);
718
                return;
719
            }
720

    
721
            while (attributePos < predicateEnd)
722
            {
723
                attributePos = inputString.indexOf(ATTRIBUTESYMBOL, attributePos + 1);
724

    
725
                if (attributePos == -1)
726
                    break;
727
            }
728
        }
729

    
730
        if (hasPredicate)
731
        {
732
            containsPredicates = true;
733
            returnFieldListWithPredicates.add(inputString);
734
        }
735

    
736
        containsExtendedSQL = true;
737
   
738
        // no attribute value will be returned
739
        logMetacat.info("QuerySpecification.handleReturnField - there are no attributes in the XPATH statement" );
740
        returnFieldList.add(inputString);       
741
    }
742

    
743
    /**
744
     * create a SQL serialization of the query that this instance represents
745
     */
746
    public String printSQL(boolean useXMLIndex, List<Object> parameterValues)
747
    {
748

    
749
        StringBuffer self = new StringBuffer();
750
        StringBuffer queryString = new StringBuffer();
751

    
752
        queryString.append("SELECT xml_documents.docid, identifier.guid, docname, doctype, date_created, date_updated, xml_documents.rev ");
753
        queryString.append("FROM xml_documents, identifier ");
754
        queryString.append("WHERE xml_documents.docid = identifier.docid AND xml_documents.rev = identifier.rev AND");
755

    
756
        // Get the query from the QueryGroup and check
757
        // if no query has been returned
758
        String queryFromQueryGroup;
759
        // keep track of the values we add as prepared statement question marks (?)
760
        List<Object> groupValues = new ArrayList<Object>();
761
        if (query != null) {
762
        	queryFromQueryGroup = query.printSQL(useXMLIndex, groupValues);
763
        } else {
764
        	queryFromQueryGroup = "";
765
        }
766
        logMetacat.info("QuerySpecification.printSQL - Query : " + queryFromQueryGroup);
767
        
768
        if(!queryFromQueryGroup.trim().equals("")){
769
            self.append(" xml_documents.docid IN (");
770
            self.append(queryFromQueryGroup);
771
            self.append(") ");
772
            // add the parameter values
773
            parameterValues.addAll(groupValues);
774
        }
775

    
776
        // Add SQL to filter for doctypes requested in the query
777
        // This is an implicit OR for the list of doctypes. Only doctypes in
778
        // this
779
        // list will be searched if the tag is present
780
        if (!filterDocList.isEmpty()) {
781
            boolean firstdoctype = true;
782

    
783
            if (!self.toString().equals("")){
784
                self.append(" AND ");
785
            }
786
            self.append(" (");
787

    
788
            Enumeration en = filterDocList.elements();
789
            while (en.hasMoreElements()) {
790
                String currentDoctype = (String) en.nextElement();
791
                if (firstdoctype) {
792
                    firstdoctype = false;
793
                    self.append(" doctype = ?");
794
                } else {
795
                    self.append(" OR doctype = ?");
796
                }
797
                parameterValues.add(currentDoctype);
798

    
799
            }
800

    
801
            self.append(") ");
802
            
803
        }
804

    
805
        // Add SQL to filter for owners requested in the query
806
        // This is an implicit OR for the list of owners
807
        if (!ownerList.isEmpty()) {
808
            boolean first = true;
809

    
810
            if (!self.toString().equals("")){
811
                self.append(" AND ");
812
            }
813
            self.append(" (");
814
            
815

    
816
            Enumeration en = ownerList.elements();
817
            while (en.hasMoreElements()) {
818
                String current = (String) en.nextElement();
819
                if (current != null) {
820
                    current = current.toLowerCase();
821
                }
822
                if (first) {
823
                    first = false;
824
                    self.append(" lower(user_owner) = ?");
825
                } else {
826
                    self.append(" OR lower(user_owner) = ?");
827
                }
828
                parameterValues.add(current);
829
            }
830

    
831
            self.append(") ");
832
            
833
        }
834

    
835
        // if there is only one percentage search item, this query is a
836
        // percentage search query
837
        if (query != null) {
838
        	logMetacat.info("QuerySpecification.printSQL - percentage number: " + query.getPercentageSymbolCount());
839
			if (query.getPercentageSymbolCount() == 1) {
840
				logMetacat.info("QuerySpecification.printSQL - It is a percentage search");
841
				percentageSearch = true;
842
			}
843
        }
844

    
845
        queryString.append(self.toString());
846
        return queryString.toString();
847
    }
848

    
849
   
850

    
851
    /**
852
     * This method prints sql based upon the &lt;returnfield&gt; tag in the
853
     * pathquery document. This allows for customization of the returned fields.
854
     * If the boolean useXMLIndex paramter is false, it uses a recursive query on
855
     * xml_nodes to find the fields to be included by their path expression, and
856
     * avoids the use of the xml_index table.
857
     *
858
     * @param doclist the list of document ids to search
859
     * @param unaccessableNodePair the node pairs (start id and end id) which
860
     *            this user should not access
861
     * @param useXMLIndex a boolean flag indicating whether to search using
862
     *            xml_index
863
     */
864
    public String printExtendedSQL(String doclist, boolean useXMLIndex, List<Object> allValues, List<Object> docListValues)
865
    {
866
    	
867
    	// keep track of the values we add as prepared statement question marks (?)
868
    	//List<Object> allValues = new ArrayList<Object>();
869
    	
870
        if (useXMLIndex && !containsPredicates) {
871
        	// keep track of the values we add as prepared statement question marks (?)
872
        	List<Object> parameterValues = new ArrayList<Object>();
873
        	String query = printExtendedSQL(doclist, parameterValues, docListValues);
874
        	// add parameter values to our running list
875
        	allValues.addAll(parameterValues);
876
        	return query;
877
        }
878
        else
879
        {
880
            StringBuffer self = new StringBuffer();
881
            boolean firstfield = true;
882
            // keep track of the values we add as prepared statement question marks (?)
883
        	List<Object> parameterValues = new ArrayList<Object>();
884
            // first part comes from fields without  predicates 
885
            String queryFromWithoutPrecidates = printExtendedSQL(doclist, parameterValues, docListValues);
886
            // add parameter values to our running list
887
        	allValues.addAll(parameterValues);
888
        	if (queryFromWithoutPrecidates != null) {
889
            	 // it has return fields without predicate
890
            	 self.append(queryFromWithoutPrecidates);
891
            	 firstfield = false;
892
        	}
893
            //put the returnfields into the query
894
            //the for loop allows for multiple fields
895
            for (int i = 0; i <   returnFieldListWithPredicates.size(); i++)
896
            {
897
                if (firstfield)
898
                {
899
                    firstfield = false;
900
                }
901
                else
902
                {
903
                    self.append(" UNION ");
904
                }
905
                String path  = (String)  returnFieldListWithPredicates.elementAt(i);
906
                //path = path.replaceAll("'", "''");
907
                // TODO: can we use prepared statements for this?
908
                allValues.add(path);
909
                self.append("select xml_nodes.docid, ");
910
                self.append("? as path, ");
911
                self.append("xml_nodes.nodedata, ");
912
                self.append("xml_nodes.parentnodeid, ");
913
                self.append("xml_nodes.nodetype ");
914
                //self.append("from xml_nodes, xml_documents ");
915
                self.append("from xml_nodes ");
916
                self.append("where ");
917
                // keep track of the values we add as prepared statement question marks (?)
918
            	List<Object> nestedParameterValues = new ArrayList<Object>();
919
                String nestedQuery = QueryTerm.useNestedStatements(path, nestedParameterValues);
920
                self.append(nestedQuery);
921
                // add to the running total
922
                allValues.addAll(nestedParameterValues);
923

    
924
                self.append(" AND xml_nodes.docid in (");
925
                self.append(doclist);
926
                allValues.addAll(docListValues);
927

    
928
                if (returnFieldIsAttribute(path))
929
                {
930
                    self.append(")");
931
                }
932
                else
933
                {
934
                     self.append(") AND xml_nodes.nodetype = 'TEXT'");
935
                }
936
                //self.append(" AND xml_nodes.rootnodeid = xml_documents.rootnodeid");
937

    
938
                //addAccessRestrictionSQL(unaccessableNodePair, self);
939
            }
940

    
941
            return self.toString();
942
        }
943
    }
944
    
945
    /*
946
     * Determines the returnfield is an attribute of not. 
947
     * For given returnfield, this programm will cut the part of path after last slash.
948
     * If no slash in the path, the original string will be considered as last part.
949
     * If first character of last part is @ it will retrun true. 
950
     */
951
    private boolean returnFieldIsAttribute(String path)
952
    {
953
    	boolean isAttribute = false;
954
    	if (path != null)
955
    	{
956
    	    int slashIndex = path.lastIndexOf("/");
957
    	    if (slashIndex !=-1)
958
    	    {
959
    	    	// if there is slash in the path, path should be replace by the last part
960
    	    	path = path.substring(slashIndex+1);
961
    	    }
962
    	    logMetacat.debug("QuerySpecification.returnFieldIsAttribute - final path is " + path);
963
    	    // if first of character of path is @, the path is attribute
964
    	    if (path.charAt(0) == '@')
965
    	    {
966
    	    	logMetacat.debug("QuerySpecification.returnFieldIsAttribute - it is an attribute");
967
    	    	isAttribute = true;
968
    	    }
969
    	}
970
    	return isAttribute;
971
    }
972

    
973
    /**
974
     * This method prints sql based upon the &lt;returnfield&gt; tag in the
975
     * pathquery document. This allows for customization of the returned fields.
976
     * It uses the xml_index table and so assumes that this table has been
977
     * built.
978
     *
979
     * @param doclist the list of document ids to search
980
     * @param unaccessableNodePair the node pairs (start id and end id)
981
     *            which this user should not access
982
     */
983
    private String printExtendedSQL(String doclist, List<Object> values, List<Object> docListValues) {
984
    	
985
    	// keep track of the values we add as prepared statement question marks (?)
986
    	//List<Object> values = new ArrayList<Object>();
987
    	
988
        logMetacat.debug("QuerySpecification.printExtendedSQL - in printExtendedSQL");
989
        StringBuffer self = new StringBuffer();
990
        Vector<String> elementVector = new Vector<String>();
991
        Vector<String> attributeVector = new Vector<String>();
992

    
993
        boolean usePathIndex = true;
994

    
995
        // test if the are elements in the return fields
996
        if ( returnFieldList.size() == 0 ) {
997
            return null;
998
        }
999

    
1000
        for (int i = 0; i < returnFieldList.size(); i++) {
1001
        	String path = (String)returnFieldList.elementAt(i);
1002
        	// Since return fileds having preicates will be handle in another path,
1003
        	// we should skip it.
1004
        	if (returnFieldListWithPredicates.contains(path)) {
1005
        		continue;
1006
        	}
1007
        	
1008
        	if (path != null && path.indexOf(ATTRIBUTESYMBOL) != -1) {
1009
        		attributeVector.add(path);
1010
        	} else {
1011
        		elementVector.add(path);
1012
        	} 
1013
        	
1014

    
1015
        	try {
1016
				if (!SystemUtil.getPathsForIndexing().contains(path)) {
1017
					usePathIndex = false;   
1018
				}
1019
			} catch (MetacatUtilException mue) {
1020
				logMetacat.warn("QuerySpecification.printExtendedSQL - Could not get index paths: "  + mue.getMessage());
1021
			}
1022
         
1023
        }
1024
        // check if has return field
1025
        if (elementVector.size() == 0 && attributeVector.size()==0)
1026
        {
1027
        	return null;
1028
        }
1029

    
1030
        if (usePathIndex){
1031
            self.append("select docid, path, nodedata, parentnodeid, null as nodetype ");
1032
            self.append("from xml_path_index where path in ( ");
1033

    
1034
            boolean firstfield = true;
1035
            //put the returnfields into the query
1036
            //the for loop allows for multiple fields
1037
            for (int i = 0; i < returnFieldList.size(); i++) {
1038
            	String returnField = (String) returnFieldList.elementAt(i);
1039
            	// in case we have predicate conditions with quotes
1040
            	returnField = returnField.replaceAll("'", "''");
1041
                if (firstfield) {
1042
                    firstfield = false;
1043
                    self.append("? ");
1044
                	values.add(returnField);
1045
                }
1046
                else {
1047
                    self.append(", ? ");
1048
                    values.add(returnField);
1049
                }
1050
            }
1051
            self.append(") AND docid in (");
1052
            self.append(doclist);
1053
            values.addAll(docListValues);
1054
            self.append(")");
1055

    
1056
        } else {
1057
            self.append("select xml_nodes.docid, xml_index.path, xml_nodes.nodedata,  ");
1058
            self.append("xml_nodes.parentnodeid, ");
1059
            self.append("xml_nodes.nodetype ");
1060
            self.append("FROM xml_index, xml_nodes WHERE (");
1061
           
1062
            boolean firstElement = true;
1063
            boolean firstAttribute = true;
1064
            //put the returnfields into the query
1065
            //the for loop allows for multiple fields
1066
            if (elementVector.size() != 0)
1067
            {
1068
	            for (int i = 0; i < elementVector.size(); i++) {
1069
	            	String path = (String) elementVector.elementAt(i);
1070
	                if (firstElement) {
1071
	                	firstElement = false;
1072
	                	self.append(" (xml_index.nodeid=xml_nodes.parentnodeid AND xml_index.path IN ( ");
1073
	                    self.append("?");
1074
	                    values.add(path);
1075
	                 }
1076
	                else 
1077
	                {
1078
	                    self.append(", ? ");
1079
	                    values.add(path);
1080
	                }
1081
	            }
1082
	            self.append(") AND xml_nodes.nodetype = 'TEXT')");
1083
            }
1084
            
1085
            if (attributeVector.size() != 0)
1086
            {
1087
            	for (int j=0; j<attributeVector.size(); j++)
1088
            	{
1089
            		String path = (String) attributeVector.elementAt(j);
1090
            		if (firstAttribute)
1091
            		{
1092
            			firstAttribute = false;
1093
            			if (!firstElement)
1094
                		{
1095
                			self.append(" OR ");
1096
                		}
1097
            			self.append(" (xml_index.nodeid=xml_nodes.nodeid AND ( xml_index.path IN ( ");
1098
	                    self.append("?");
1099
	                    values.add(path);
1100
            		}
1101
            		else 
1102
	                {
1103
	                    self.append(", ? ");
1104
	                    values.add(path);
1105
	                }
1106
            	}
1107
            	self.append(") AND xml_nodes.nodetype = 'ATTRIBUTE'))");
1108
            }
1109
            
1110
          
1111
            self.append(") AND xml_nodes.docid in (");
1112
            self.append(doclist);
1113
            values.addAll(docListValues);
1114
            self.append(")");
1115

    
1116
        }
1117

    
1118
        return self.toString();
1119
    }
1120

    
1121

    
1122
    /**
1123
     * Method to return a String generated after sorting the returnFieldList
1124
     * Vector
1125
     */
1126
    public String getSortedReturnFieldString(){
1127
        String returnFields = "";
1128

    
1129
        // Create a temporary vector and copy returnFieldList into it
1130
        Vector tempVector = new Vector();
1131

    
1132
        Iterator it = returnFieldList.iterator();
1133
        while(it.hasNext()){
1134
            tempVector.add(it.next());
1135
        }
1136

    
1137
        /*Enumeration attEnum = attributeReturnList.elements();
1138
        while(attEnum.hasMoreElements()){
1139
            Iterator tempIt = ((Vector)attEnum.nextElement()).iterator();
1140
	    String rfield = "";
1141
            if(tempIt.hasNext()){
1142
		String element = (String)tempIt.next();
1143
		if(element != null) {
1144
		    rfield +=element;
1145
		}
1146
	    }
1147
            if(tempIt.hasNext()){
1148
		String attribute = (String)tempIt.next();
1149
		if(attribute != null) {
1150
  		    rfield = rfield + "@" + attribute;
1151
                }
1152
	    }
1153
            tempVector.add(rfield);
1154
        }*/
1155

    
1156
        // Sort the temporary vector
1157
        java.util.Collections.sort(tempVector);
1158

    
1159
        // Generate the string and return it
1160
        it = tempVector.iterator();
1161
        while(it.hasNext()){
1162
            returnFields = returnFields + it.next() + "|";
1163
        }
1164
        return returnFields;
1165
    }
1166

    
1167

    
1168
  
1169

    
1170

    
1171
    public static String printRelationSQL(String docid)
1172
    {
1173
        StringBuffer self = new StringBuffer();
1174
        self.append("select subject, relationship, object, subdoctype, ");
1175
        self.append("objdoctype from xml_relation ");
1176
        self.append("where docid like '").append(docid).append("'");
1177
        return self.toString();
1178
    }
1179

    
1180
    public static String printGetDocByDoctypeSQL(String docid)
1181
    {
1182
        StringBuffer self = new StringBuffer();
1183

    
1184
        self.append("SELECT docid,docname,doctype,");
1185
        self.append("date_created, date_updated ");
1186
        self.append("FROM xml_documents WHERE docid IN (");
1187
        self.append(docid).append(")");
1188
        return self.toString();
1189
    }
1190

    
1191
    /**
1192
     * create a String description of the query that this instance represents.
1193
     * This should become a way to get the XML serialization of the query.
1194
     */
1195
    public String toString()
1196
    {
1197
        return "meta_file_id=" + meta_file_id + "\n" + query;
1198
        //DOCTITLE attr cleared from the db
1199
        //return "meta_file_id=" + meta_file_id + "\n" +
1200
        //"querytitle=" + querytitle + "\n" + query;
1201
    }
1202

    
1203
    /** A method to get rid of attribute part in path expression */
1204
    public static String newPathExpressionWithOutAttribute(String pathExpression)
1205
    {
1206
        if (pathExpression == null) { return null; }
1207
        int index = pathExpression.lastIndexOf(ATTRIBUTESYMBOL);
1208
        String newExpression = null;
1209
        if (index != 0) {
1210
            newExpression = pathExpression.substring(0, index - 1);
1211
        }
1212
        logMetacat.info("QuerySpecification.newPathExpressionWithOutAttribute - The path expression without attributes: "
1213
                + newExpression);
1214
        return newExpression;
1215
    }
1216

    
1217
    /** A method to get attribute name from path */
1218
    public static String getAttributeName(String path)
1219
    {
1220
        if (path == null) { return null; }
1221
        int index = path.lastIndexOf(ATTRIBUTESYMBOL);
1222
        int size = path.length();
1223
        String attributeName = null;
1224
        if (index != 1) {
1225
            attributeName = path.substring(index + 1, size);
1226
        }
1227
        logMetacat.info("QuerySpecification.getAttributeName - The attirbute name from path: " + attributeName);
1228
        return attributeName;
1229
    }
1230

    
1231
}
(54-54/64)