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: tao $'
12
 *     '$Date: 2008-03-20 17:32:01 -0700 (Thu, 20 Mar 2008) $'
13
 * '$Revision: 3771 $'
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.Enumeration;
36
import java.util.Hashtable;
37
import java.util.Stack;
38
import java.util.Vector;
39

    
40
import edu.ucsb.nceas.dbadapter.AbstractDatabase;
41

    
42
import org.apache.log4j.Logger;
43
import org.xml.sax.Attributes;
44
import org.xml.sax.InputSource;
45
import org.xml.sax.SAXException;
46
import org.xml.sax.XMLReader;
47
import org.xml.sax.helpers.DefaultHandler;
48
import org.xml.sax.helpers.XMLReaderFactory;
49
import java.util.Iterator;
50

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

    
61
    /** flag determining whether extended query terms are present */
62
    private boolean containsExtendedSQL = false;
63

    
64
    /** flag determining whether predicates are present */
65
    private boolean containsPredicates = false;
66

    
67
    /** Identifier for this query document */
68
    private String meta_file_id;
69

    
70
    /** Title of this query */
71
    private String queryTitle;
72

    
73
    /** List of document types to be returned using package back tracing */
74
    private Vector returnDocList;
75

    
76
    /** List of document types to be searched */
77
    private Vector filterDocList;
78

    
79
    /** List of fields to be returned in result set */
80
    private Vector returnFieldList;
81
    
82
    /** List of fields with "[" and "]" in result set. This is a subset of returnFieldList.
83
     *   If some of return fields have [,  those fields will be stored this vector (we have different query for those return fields */
84
    private Vector returnFieldListWithPredicates;
85

    
86
    /** List of users owning documents to be searched */
87
    private Vector ownerList;
88

    
89
    /** The root query group that contains the recursive query constraints */
90
    private QueryGroup query = null;
91
    
92
    /** A string buffer to stored normalized query (Sometimes, the query have 
93
     * a value like "&", it will cause problem in html transform). So we need a
94
     * normalized query xml string.
95
     */
96
    private StringBuffer xml = new StringBuffer();
97

    
98
    // Query data structures used temporarily during XML parsing
99
    private Stack elementStack;
100

    
101
    private Stack queryStack;
102

    
103
    private String currentValue;
104

    
105
    private String currentPathexpr;
106

    
107
    private String parserName = null;
108

    
109
    private String accNumberSeparator = null;
110

    
111
    private static final AbstractDatabase dbAdapter = MetaCatUtil.dbAdapter;
112

    
113
    private boolean percentageSearch = false;
114

    
115
    private String userName = null;
116

    
117
    private static final String PUBLIC = "public";
118

    
119
    private String[] group = null;
120

    
121
    public static final String ATTRIBUTESYMBOL = "@";
122

    
123
    public static final char PREDICATE_START = '[';
124

    
125
    public static final char PREDICATE_END = ']';
126

    
127
    //private boolean hasAttributeReturnField = false;
128

    
129
    //private Hashtable attributeReturnList = new Hashtable();
130

    
131
    //private int countAttributeReturnField = 0;
132

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

    
138
    /**
139
     * construct an instance of the QuerySpecification class
140
     *
141
     * @param queryspec
142
     *            the XML representation of the query (should conform to
143
     *            pathquery.dtd) as a Reader
144
     * @param parserName
145
     *            the fully qualified name of a Java Class implementing the
146
     *            org.xml.sax.XMLReader interface
147
     */
148
    public QuerySpecification(Reader queryspec, String parserName,
149
            String accNumberSeparator) throws IOException
150
    {
151
        super();
152

    
153
        // Initialize the class variables
154
        returnDocList = new Vector();
155
        filterDocList = new Vector();
156
        elementStack = new Stack();
157
        queryStack = new Stack();
158
        returnFieldList = new Vector();
159
        returnFieldListWithPredicates = new Vector();
160
        ownerList = new Vector();
161
        this.parserName = parserName;
162
        this.accNumberSeparator = accNumberSeparator;
163

    
164
        // Initialize the parser and read the queryspec
165
        XMLReader parser = initializeParser();
166
        if (parser == null) {
167
            System.err.println("SAX parser not instantiated properly.");
168
        }
169
        try {
170
            parser.parse(new InputSource(queryspec));
171
        } catch (SAXException e) {
172
            System.err.println("error parsing data in "
173
                    + "QuerySpecification.QuerySpecification");
174
            System.err.println(e.getMessage());
175
        }
176
    }
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 createOwerQuery()
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("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 docid from xml_access WHERE( " + allowString;
276
        allowQuery = allowQuery + ")";
277
        logMetacat.info("allow query is: " + allowQuery);
278
        return allowQuery;
279

    
280
    }
281

    
282
    /* Method to construct a allow rule string */
283
    private String constructAllowString()
284
    {
285
        String allowQuery = "";
286
        
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
        }
296
        // add  group
297
        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
                    allowQuery = allowQuery + " OR lower(principal_name) = '"
303
                            + groupUint + "'";
304
                }//if
305
            }//for
306
        }//if
307
        // add allow rule
308
        allowQuery = allowQuery + ") AND perm_type = 'allow'" + " AND permission > 3";
309
        logMetacat.info("allow string is: " + allowQuery);
310
        return allowQuery;
311
    }
312

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

    
327
    }
328

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

    
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
     *
367
     */
368
    public String getAccessQuery()
369
    {
370
        String accessQuery = null;
371
        String onwer = createOwerQuery();
372
        String allow = createAllowRuleQuery();
373
        String deny = createDenyRuleQuery();
374
        //logMetacat.warn("onwer " +onwer);
375
        //logMetacat.warn("allow "+allow);
376
        //logMetacat.warn("deny "+deny);
377
        if (onwer != null)
378
        {
379
          accessQuery = " AND (docid IN(" + onwer + ")";
380
          accessQuery = accessQuery + " OR (docid IN (" + allow + ")"
381
                + " AND docid NOT IN (" + deny + ")))";
382
        }
383
        else
384
        {
385
        	accessQuery = " AND (docid IN (" + allow + ")"
386
                + " AND docid NOT IN (" + deny + "))";
387
        }
388
        logMetacat.warn("accessquery 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
            System.err.println("Error in QuerySpcecification.initializeParser "
552
                    + e.toString());
553
        }
554

    
555
        return parser;
556
    }
557

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

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

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

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

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

    
686
    }
687

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

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

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

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

    
709
            hasPredicate = true;
710

    
711
            if (attributePos < predicateStart)
712
                break;
713

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

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

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

    
727
                if (attributePos == -1)
728
                    break;
729
            }
730
        }
731

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

    
738
        containsExtendedSQL = true;
739

    
740
     
741
        // no attribute value will be returned
742
        logMetacat.info("QuerySpecification.handleReturnField(): " );
743
        logMetacat.info("  there are no attributes in the XPATH statement" );
744
        returnFieldList.add(inputString);
745
       
746

    
747
       
748
    }
749

    
750
    /**
751
     * create a SQL serialization of the query that this instance represents
752
     */
753
    public String printSQL(boolean useXMLIndex)
754
    {
755

    
756
        StringBuffer self = new StringBuffer();
757
        StringBuffer queryString = new StringBuffer();
758

    
759
        queryString.append("SELECT docid,docname,doctype,");
760
        queryString.append("date_created, date_updated, rev ");
761
        queryString.append("FROM xml_documents WHERE");
762

    
763
        // Get the query from the QueryGroup and check
764
        // if no query has been returned
765
        String queryFromQueryGroup = query.printSQL(useXMLIndex);
766
        logMetacat.info("Query from query in QuerySpec.printSQL: " 
767
        		+ queryFromQueryGroup);
768
        
769
        if(!queryFromQueryGroup.trim().equals("")){
770
            self.append(" docid IN (");
771
            self.append(queryFromQueryGroup);
772
            self.append(") ");
773
        }
774

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

    
783
            if(!self.toString().equals("")){
784
                self.append(" AND (");
785
                emptyString = false;
786
            }
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 = '" + currentDoctype + "'");
794
                } else {
795
                    self.append(" OR doctype = '" + currentDoctype + "'");
796
                }
797
            }
798

    
799
            if(!emptyString){
800
                self.append(") ");
801
            }
802
        }
803

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

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

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

    
829
            if(!emptyString){
830
                self.append(") ");
831
            }
832
        }
833

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

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

    
848
   
849

    
850
    /**
851
     * This method prints sql based upon the &lt;returnfield&gt; tag in the
852
     * pathquery document. This allows for customization of the returned fields.
853
     * If the boolean useXMLIndex paramter is false, it uses a recursive query on
854
     * xml_nodes to find the fields to be included by their path expression, and
855
     * avoids the use of the xml_index table.
856
     *
857
     * @param doclist the list of document ids to search
858
     * @param unaccessableNodePair the node pairs (start id and end id) which
859
     *            this user should not access
860
     * @param useXMLIndex a boolean flag indicating whether to search using
861
     *            xml_index
862
     */
863
    public String printExtendedSQL(String doclist, boolean useXMLIndex)
864
    {
865
        if (useXMLIndex && !containsPredicates)
866
        {
867
            return printExtendedSQL(doclist);
868
        }
869
        else
870
        {
871
            StringBuffer self = new StringBuffer();
872
            boolean firstfield = true;
873
            // first part comes from fields without  predicates 
874
            String queryFromWithoutPrecidates = printExtendedSQL(doclist);
875
             if (queryFromWithoutPrecidates != null)
876
             {
877
            	 // it has return fields without predicate
878
            	 self.append(queryFromWithoutPrecidates);
879
            	 firstfield = false;
880
             }
881
            //put the returnfields into the query
882
            //the for loop allows for multiple fields
883
            for (int i = 0; i <   returnFieldListWithPredicates.size(); i++)
884
            {
885
                if (firstfield)
886
                {
887
                    firstfield = false;
888
                }
889
                else
890
                {
891
                    self.append(" UNION ");
892
                }
893
                String path  = (String)  returnFieldListWithPredicates.elementAt(i);
894
                self.append("select xml_nodes.docid, ");
895
                self.append("'"+ path.replaceAll("'", "''") + "' as path, ");
896
                self.append("xml_nodes.nodedata, ");
897
                self.append("xml_nodes.parentnodeid, ");
898
                self.append("xml_nodes.nodetype ");
899
                //self.append("from xml_nodes, xml_documents ");
900
                self.append("from xml_nodes ");
901
                self.append("where ");
902
                self.append(QueryTerm.useNestedStatements(path));
903

    
904
                self.append(" AND xml_nodes.docid in (");
905
                self.append(doclist);
906
                if (returnFieldIsAttribute(path))
907
                {
908
                    self.append(")");
909
                }
910
                else
911
                {
912
                     self.append(") AND xml_nodes.nodetype = 'TEXT'");
913
                }
914
                //self.append(" AND xml_nodes.rootnodeid = xml_documents.rootnodeid");
915

    
916
                //addAccessRestrictionSQL(unaccessableNodePair, self);
917
            }
918

    
919
            return self.toString();
920
        }
921
    }
922
    
923
    /*
924
     * Determines the returnfield is an attribute of not. 
925
     * For given returnfield, this programm will cut the part of path after last slash.
926
     * If no slash in the path, the original string will be considered as last part.
927
     * If first character of last part is @ it will retrun true. 
928
     */
929
    private boolean returnFieldIsAttribute(String path)
930
    {
931
    	boolean isAttribute = false;
932
    	if (path != null)
933
    	{
934
    	    int slashIndex = path.lastIndexOf("/");
935
    	    if (slashIndex !=-1)
936
    	    {
937
    	    	// if there is slash in the path, path should be replace by the last part
938
    	    	path = path.substring(slashIndex+1);
939
    	    }
940
    	    logMetacat.debug("In QuerySpecification.returnFieldIsAttribute method, final path is "+path);
941
    	    // if first of character of path is @, the path is attribute
942
    	    if (path.charAt(0) == '@')
943
    	    {
944
    	    	logMetacat.debug("it is attribute");
945
    	    	isAttribute = true;
946
    	    }
947
    	}
948
    	return isAttribute;
949
    }
950

    
951
    /**
952
     * This method prints sql based upon the &lt;returnfield&gt; tag in the
953
     * pathquery document. This allows for customization of the returned fields.
954
     * It uses the xml_index table and so assumes that this table has been
955
     * built.
956
     *
957
     * @param doclist the list of document ids to search
958
     * @param unaccessableNodePair the node pairs (start id and end id)
959
     *            which this user should not access
960
     */
961
    private String printExtendedSQL(String doclist)
962
    {
963
        logMetacat.info("querySpecification.printExtendedSQL called\n");
964
        StringBuffer self = new StringBuffer();
965
        Vector elementVector = new Vector();
966
        Vector attributeVector = new Vector();
967

    
968
        boolean usePathIndex = true;
969

    
970
        // test if the are elements in the return fields
971
        if ( returnFieldList.size() == 0 ) {
972
            return null;
973
        }
974

    
975
        for (int i = 0; i < returnFieldList.size(); i++) {
976
        	String path = (String)returnFieldList.elementAt(i);
977
        	// Since return fileds having preicates will be handle in another path,
978
        	// we should skip it.
979
        	if (returnFieldListWithPredicates.contains(path))
980
        	{
981
        		continue;
982
        	}
983
        	if (path != null && path.indexOf(ATTRIBUTESYMBOL) != -1)
984
        	{
985
        		attributeVector.add(path);
986
        	}
987
        	else 
988
        	{
989
        		elementVector.add(path);
990
        	}       	
991
            if(!MetaCatUtil.pathsForIndexing.contains(path)){
992
                usePathIndex = false;              
993
            }
994
         
995
        }
996
        // check if has return field
997
        if (elementVector.size() == 0 && attributeVector.size()==0)
998
        {
999
        	return null;
1000
        }
1001

    
1002
        if(usePathIndex){
1003
            self.append("select docid, path, nodedata, parentnodeid, null as nodetype ");
1004
            self.append("from xml_path_index where path in( '");
1005

    
1006
            boolean firstfield = true;
1007
            //put the returnfields into the query
1008
            //the for loop allows for multiple fields
1009
            for (int i = 0; i < returnFieldList.size(); i++) {
1010
                if (firstfield) {
1011
                    firstfield = false;
1012
                    self.append( (String) returnFieldList.elementAt(i));
1013
                    self.append("' ");
1014
                }
1015
                else {
1016
                    self.append(", '");
1017
                    self.append( (String) returnFieldList.elementAt(i));
1018
                    self.append("' ");
1019
                }
1020
            }
1021
            self.append(") AND docid in (");
1022
            self.append(doclist);
1023
            self.append(")");
1024

    
1025
        } else {
1026
            self.append("select xml_nodes.docid, xml_index.path, xml_nodes.nodedata,  ");
1027
            self.append("xml_nodes.parentnodeid, ");
1028
            self.append("xml_nodes.nodetype ");
1029
            self.append("FROM xml_index, xml_nodes WHERE (");
1030
           
1031

    
1032
            boolean firstElement = true;
1033
            boolean firstAttribute = true;
1034
            //put the returnfields into the query
1035
            //the for loop allows for multiple fields
1036
            if (elementVector.size() != 0)
1037
            {
1038
	            for (int i = 0; i < elementVector.size(); i++) {
1039
	            	String path = (String) elementVector.elementAt(i);
1040
	                if (firstElement) {
1041
	                	firstElement = false;
1042
	                	self.append(" (xml_index.nodeid=xml_nodes.parentnodeid AND xml_index.path IN ('");
1043
	                    self.append(path);
1044
	                    self.append("'");
1045
	                 }
1046
	                else 
1047
	                {
1048
	                    self.append(", '");
1049
	                    self.append(path);
1050
	                    self.append("' ");
1051
	                }
1052
	            }
1053
	            self.append(") AND xml_nodes.nodetype = 'TEXT')");
1054
            }
1055
            
1056
            if (attributeVector.size() != 0)
1057
            {
1058
            	for (int j=0; j<attributeVector.size(); j++)
1059
            	{
1060
            		String path = (String) attributeVector.elementAt(j);
1061
            		if (firstAttribute)
1062
            		{
1063
            			firstAttribute = false;
1064
            			if (!firstElement)
1065
                		{
1066
                			self.append(" OR ");
1067
                		}
1068
            			self.append(" (xml_index.nodeid=xml_nodes.nodeid AND ( xml_index.path IN ( '");
1069
	                    self.append(path);
1070
	                    self.append("'");
1071
            		}
1072
            		else 
1073
	                {
1074
	                    self.append(", '");
1075
	                    self.append(path);
1076
	                    self.append("' ");
1077
	                }
1078
            	}
1079
            	self.append(") AND xml_nodes.nodetype = 'ATTRIBUTE'))");
1080
            }
1081
            
1082
          
1083
            self.append(") AND xml_nodes.docid in (");
1084
            self.append(doclist);
1085
            self.append(")");
1086

    
1087
        }
1088

    
1089
        return self.toString();
1090
    }
1091

    
1092

    
1093
    /**
1094
     * Method to return a String generated after sorting the returnFieldList
1095
     * Vector
1096
     */
1097
    public String getSortedReturnFieldString(){
1098
        String returnFields = "";
1099

    
1100
        // Create a temporary vector and copy returnFieldList into it
1101
        Vector tempVector = new Vector();
1102

    
1103
        Iterator it = returnFieldList.iterator();
1104
        while(it.hasNext()){
1105
            tempVector.add(it.next());
1106
        }
1107

    
1108
        /*Enumeration attEnum = attributeReturnList.elements();
1109
        while(attEnum.hasMoreElements()){
1110
            Iterator tempIt = ((Vector)attEnum.nextElement()).iterator();
1111
	    String rfield = "";
1112
            if(tempIt.hasNext()){
1113
		String element = (String)tempIt.next();
1114
		if(element != null) {
1115
		    rfield +=element;
1116
		}
1117
	    }
1118
            if(tempIt.hasNext()){
1119
		String attribute = (String)tempIt.next();
1120
		if(attribute != null) {
1121
  		    rfield = rfield + "@" + attribute;
1122
                }
1123
	    }
1124
            tempVector.add(rfield);
1125
        }*/
1126

    
1127
        // Sort the temporary vector
1128
        java.util.Collections.sort(tempVector);
1129

    
1130
        // Generate the string and return it
1131
        it = tempVector.iterator();
1132
        while(it.hasNext()){
1133
            returnFields = returnFields + it.next() + "|";
1134
        }
1135
        return returnFields;
1136
    }
1137

    
1138

    
1139
  
1140

    
1141

    
1142
    public static String printRelationSQL(String docid)
1143
    {
1144
        StringBuffer self = new StringBuffer();
1145
        self.append("select subject, relationship, object, subdoctype, ");
1146
        self.append("objdoctype from xml_relation ");
1147
        self.append("where docid like '").append(docid).append("'");
1148
        return self.toString();
1149
    }
1150

    
1151
    public static String printGetDocByDoctypeSQL(String docid)
1152
    {
1153
        StringBuffer self = new StringBuffer();
1154

    
1155
        self.append("SELECT docid,docname,doctype,");
1156
        self.append("date_created, date_updated ");
1157
        self.append("FROM xml_documents WHERE docid IN (");
1158
        self.append(docid).append(")");
1159
        return self.toString();
1160
    }
1161

    
1162
    /**
1163
     * create a String description of the query that this instance represents.
1164
     * This should become a way to get the XML serialization of the query.
1165
     */
1166
    public String toString()
1167
    {
1168
        return "meta_file_id=" + meta_file_id + "\n" + query;
1169
        //DOCTITLE attr cleared from the db
1170
        //return "meta_file_id=" + meta_file_id + "\n" +
1171
        //"querytitle=" + querytitle + "\n" + query;
1172
    }
1173

    
1174
    /** A method to get rid of attribute part in path expression */
1175
    public static String newPathExpressionWithOutAttribute(String pathExpression)
1176
    {
1177
        if (pathExpression == null) { return null; }
1178
        int index = pathExpression.lastIndexOf(ATTRIBUTESYMBOL);
1179
        String newExpression = null;
1180
        if (index != 0) {
1181
            newExpression = pathExpression.substring(0, index - 1);
1182
        }
1183
        logMetacat.info("The path expression without attributes: "
1184
                + newExpression);
1185
        return newExpression;
1186
    }
1187

    
1188
    /** A method to get attribute name from path */
1189
    public static String getAttributeName(String path)
1190
    {
1191
        if (path == null) { return null; }
1192
        int index = path.lastIndexOf(ATTRIBUTESYMBOL);
1193
        int size = path.length();
1194
        String attributeName = null;
1195
        if (index != 1) {
1196
            attributeName = path.substring(index + 1, size);
1197
        }
1198
        logMetacat.info("The attirbute name from path: "
1199
                + attributeName);
1200
        return attributeName;
1201
    }
1202

    
1203
}
(54-54/66)