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: jones $'
12
 *     '$Date: 2006-11-10 10:25:38 -0800 (Fri, 10 Nov 2006) $'
13
 * '$Revision: 3077 $'
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 edu.ucsb.nceas.dbadapter.*;
33

    
34
import java.io.*;
35
import java.util.Hashtable;
36
import java.util.Stack;
37
import java.util.Vector;
38
import java.util.Enumeration;
39

    
40
import org.apache.log4j.Logger;
41

    
42
 /** a utility class that represents a group of terms in a query */
43
public  class QueryGroup {
44
    private String operator = null;  // indicates how query terms are combined
45
    private Vector children = null;  // the list of query terms and groups
46
    private int countPercentageSearchItem = 0;
47
    private static Logger logMetacat = Logger.getLogger(QueryGroup.class);
48

    
49
    /**
50
     * construct a new QueryGroup
51
     *
52
     * @param operator the boolean conector used to connect query terms
53
     *                    in this query group
54
     */
55
    public QueryGroup(String operator) {
56
      this.operator = operator;
57
      children = new Vector();
58
    }
59

    
60
    /**
61
     * Add a child QueryGroup to this QueryGroup
62
     *
63
     * @param qgroup the query group to be added to the list of terms
64
     */
65
    public void addChild(QueryGroup qgroup) {
66
      children.add((Object)qgroup);
67
    }
68

    
69
    /**
70
     * Add a child QueryTerm to this QueryGroup
71
     *
72
     * @param qterm the query term to be added to the list of terms
73
     */
74
    public void addChild(QueryTerm qterm) {
75
      children.add((Object)qterm);
76
    }
77

    
78
    /**
79
     * Retrieve an Enumeration of query terms for this QueryGroup
80
     */
81
    public Enumeration getChildren() {
82
      return children.elements();
83
    }
84

    
85
    public int getPercentageSymbolCount()
86
    {
87
      return countPercentageSearchItem;
88
    }
89

    
90
    /**
91
     * create a SQL serialization of the query that this instance represents
92
     */
93
    public String printSQL(boolean useXMLIndex) {
94
      StringBuffer self = new StringBuffer();
95
      StringBuffer queryString = new StringBuffer();
96

    
97
      boolean first = true;
98
      
99
      Enumeration en= getChildren();
100
      while (en.hasMoreElements()) {
101
        Object qobject = en.nextElement();
102
        if (qobject instanceof QueryGroup) {
103
            QueryGroup qg = (QueryGroup)qobject;
104
        	String queryGroupSQL = qg.printSQL(useXMLIndex);
105
        	logMetacat.info("In QueryGroup.printSQL.. found a QueryGroup: " 
106
        			+ queryGroupSQL);
107
        	
108
        	if (first) {
109
        		first = false;
110
        	} else {
111
        		if(!queryString.toString().equals("") && queryGroupSQL != null &&!queryGroupSQL.equals("")){
112
                    queryString.append(" " + operator + " ");
113
        		}
114
        	}
115
   		  	queryString.append(queryGroupSQL);
116
   		  	
117
   		  	// count percerntage number
118
   		  	int count = qg.getPercentageSymbolCount();
119
   		  	countPercentageSearchItem = countPercentageSearchItem + count;
120
        } else if (qobject instanceof QueryTerm) {
121
           QueryTerm qt = (QueryTerm)qobject;
122
           String termQueryString = qt.printSQL(useXMLIndex);
123
       	   logMetacat.info("In QueryGroup.printSQL.. found a QueryGroup: " 
124
        			+ termQueryString);
125
           if(!(qt.getSearchMode().equals("contains") && qt.getValue().equals("%"))){
126
        	   if (first) {
127
                   first = false;
128
               } else {
129
                   if(!queryString.toString().equals("")){
130
                       queryString.append(" " + operator + " ");
131
                   }
132
               }
133
               queryString.append(termQueryString);
134
           }
135
           // count percerntage number
136
           int count = qt.getPercentageSymbolCount();
137
           countPercentageSearchItem = countPercentageSearchItem + count;
138
        } else {
139
          System.err.println("qobject wrong type: fatal error");
140
        }
141
      }
142

    
143
      if(!queryString.toString().equals("")){
144
          self.append("(");
145
          self.append(queryString.toString());
146
          self.append(")");
147
      }
148
      
149
      logMetacat.info("In QueryGroup.printSQL.. final query returned is: " 
150
			+ self.toString());
151
      return self.toString();
152
    }
153

    
154
    /**
155
     * create a String description of the query that this instance represents.
156
     * This should become a way to get the XML serialization of the query.
157
     */
158
    public String toString() {
159
      StringBuffer self = new StringBuffer();
160

    
161
      self.append("  (Query group operator=" + operator + "\n");
162
      Enumeration en= getChildren();
163
      while (en.hasMoreElements()) {
164
        Object qobject = en.nextElement();
165
        self.append(qobject);
166
      }
167
      self.append("  )\n");
168
      return self.toString();
169
    }
170
  }
(53-53/65)