1 |
1831
|
tao
|
/**
|
2 |
|
|
* '$RCSfile$'
|
3 |
2366
|
sgarg
|
* 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 |
1831
|
tao
|
* 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 |
|
|
* Release: @release@
|
11 |
|
|
*
|
12 |
|
|
* '$Author$'
|
13 |
|
|
* '$Date$'
|
14 |
|
|
* '$Revision$'
|
15 |
|
|
*
|
16 |
|
|
* This program is free software; you can redistribute it and/or modify
|
17 |
|
|
* it under the terms of the GNU General Public License as published by
|
18 |
|
|
* the Free Software Foundation; either version 2 of the License, or
|
19 |
|
|
* (at your option) any later version.
|
20 |
|
|
*
|
21 |
|
|
* This program is distributed in the hope that it will be useful,
|
22 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
23 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
24 |
|
|
* GNU General Public License for more details.
|
25 |
|
|
*
|
26 |
|
|
* You should have received a copy of the GNU General Public License
|
27 |
|
|
* along with this program; if not, write to the Free Software
|
28 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
29 |
|
|
*/
|
30 |
|
|
|
31 |
|
|
package edu.ucsb.nceas.metacat;
|
32 |
|
|
|
33 |
|
|
import edu.ucsb.nceas.dbadapter.*;
|
34 |
|
|
|
35 |
|
|
import java.io.*;
|
36 |
|
|
import java.util.Hashtable;
|
37 |
|
|
import java.util.Stack;
|
38 |
|
|
import java.util.Vector;
|
39 |
|
|
import java.util.Enumeration;
|
40 |
|
|
|
41 |
2677
|
sgarg
|
import org.apache.log4j.Logger;
|
42 |
|
|
|
43 |
1831
|
tao
|
/** a utility class that represents a group of terms in a query */
|
44 |
|
|
public class QueryGroup {
|
45 |
|
|
private String operator = null; // indicates how query terms are combined
|
46 |
|
|
private Vector children = null; // the list of query terms and groups
|
47 |
|
|
private int countPercentageSearchItem = 0;
|
48 |
2677
|
sgarg
|
private static Logger logMetacat = Logger.getLogger(QueryGroup.class);
|
49 |
|
|
|
50 |
2366
|
sgarg
|
/**
|
51 |
|
|
* construct a new QueryGroup
|
52 |
1831
|
tao
|
*
|
53 |
2366
|
sgarg
|
* @param operator the boolean conector used to connect query terms
|
54 |
1831
|
tao
|
* in this query group
|
55 |
|
|
*/
|
56 |
|
|
public QueryGroup(String operator) {
|
57 |
|
|
this.operator = operator;
|
58 |
|
|
children = new Vector();
|
59 |
|
|
}
|
60 |
|
|
|
61 |
2366
|
sgarg
|
/**
|
62 |
1831
|
tao
|
* Add a child QueryGroup to this QueryGroup
|
63 |
|
|
*
|
64 |
|
|
* @param qgroup the query group to be added to the list of terms
|
65 |
|
|
*/
|
66 |
|
|
public void addChild(QueryGroup qgroup) {
|
67 |
2366
|
sgarg
|
children.add((Object)qgroup);
|
68 |
1831
|
tao
|
}
|
69 |
|
|
|
70 |
|
|
/**
|
71 |
|
|
* Add a child QueryTerm to this QueryGroup
|
72 |
|
|
*
|
73 |
|
|
* @param qterm the query term to be added to the list of terms
|
74 |
|
|
*/
|
75 |
|
|
public void addChild(QueryTerm qterm) {
|
76 |
2366
|
sgarg
|
children.add((Object)qterm);
|
77 |
1831
|
tao
|
}
|
78 |
|
|
|
79 |
|
|
/**
|
80 |
|
|
* Retrieve an Enumeration of query terms for this QueryGroup
|
81 |
|
|
*/
|
82 |
|
|
public Enumeration getChildren() {
|
83 |
|
|
return children.elements();
|
84 |
|
|
}
|
85 |
2366
|
sgarg
|
|
86 |
1831
|
tao
|
public int getPercentageSymbolCount()
|
87 |
|
|
{
|
88 |
|
|
return countPercentageSearchItem;
|
89 |
|
|
}
|
90 |
2366
|
sgarg
|
|
91 |
1831
|
tao
|
/**
|
92 |
|
|
* create a SQL serialization of the query that this instance represents
|
93 |
|
|
*/
|
94 |
|
|
public String printSQL(boolean useXMLIndex) {
|
95 |
|
|
StringBuffer self = new StringBuffer();
|
96 |
2373
|
sgarg
|
StringBuffer queryString = new StringBuffer();
|
97 |
|
|
|
98 |
1831
|
tao
|
boolean first = true;
|
99 |
2677
|
sgarg
|
|
100 |
1831
|
tao
|
Enumeration en= getChildren();
|
101 |
|
|
while (en.hasMoreElements()) {
|
102 |
|
|
Object qobject = en.nextElement();
|
103 |
|
|
if (qobject instanceof QueryGroup) {
|
104 |
2635
|
sgarg
|
QueryGroup qg = (QueryGroup)qobject;
|
105 |
2634
|
sgarg
|
String queryGroupSQL = qg.printSQL(useXMLIndex);
|
106 |
2677
|
sgarg
|
logMetacat.info("In QueryGroup.printSQL.. found a QueryGroup: "
|
107 |
|
|
+ queryGroupSQL);
|
108 |
2635
|
sgarg
|
|
109 |
2634
|
sgarg
|
if (first) {
|
110 |
|
|
first = false;
|
111 |
|
|
} else {
|
112 |
|
|
if(!queryString.toString().equals("") && queryGroupSQL != null &&!queryGroupSQL.equals("")){
|
113 |
2503
|
sgarg
|
queryString.append(" " + operator + " ");
|
114 |
2677
|
sgarg
|
}
|
115 |
|
|
}
|
116 |
|
|
queryString.append(queryGroupSQL);
|
117 |
|
|
|
118 |
|
|
// count percerntage number
|
119 |
|
|
int count = qg.getPercentageSymbolCount();
|
120 |
|
|
countPercentageSearchItem = countPercentageSearchItem + count;
|
121 |
1831
|
tao
|
} else if (qobject instanceof QueryTerm) {
|
122 |
|
|
QueryTerm qt = (QueryTerm)qobject;
|
123 |
2373
|
sgarg
|
String termQueryString = qt.printSQL(useXMLIndex);
|
124 |
2677
|
sgarg
|
logMetacat.info("In QueryGroup.printSQL.. found a QueryGroup: "
|
125 |
|
|
+ termQueryString);
|
126 |
2366
|
sgarg
|
if(!(qt.getSearchMode().equals("contains") && qt.getValue().equals("%"))){
|
127 |
2634
|
sgarg
|
if (first) {
|
128 |
2503
|
sgarg
|
first = false;
|
129 |
|
|
} else {
|
130 |
|
|
if(!queryString.toString().equals("")){
|
131 |
|
|
queryString.append(" " + operator + " ");
|
132 |
|
|
}
|
133 |
|
|
}
|
134 |
2373
|
sgarg
|
queryString.append(termQueryString);
|
135 |
2366
|
sgarg
|
}
|
136 |
1831
|
tao
|
// count percerntage number
|
137 |
|
|
int count = qt.getPercentageSymbolCount();
|
138 |
|
|
countPercentageSearchItem = countPercentageSearchItem + count;
|
139 |
|
|
} else {
|
140 |
|
|
System.err.println("qobject wrong type: fatal error");
|
141 |
|
|
}
|
142 |
|
|
}
|
143 |
2373
|
sgarg
|
|
144 |
|
|
if(!queryString.toString().equals("")){
|
145 |
|
|
self.append("(");
|
146 |
|
|
self.append(queryString.toString());
|
147 |
|
|
self.append(")");
|
148 |
|
|
}
|
149 |
2677
|
sgarg
|
|
150 |
|
|
logMetacat.info("In QueryGroup.printSQL.. final query returned is: "
|
151 |
|
|
+ self.toString());
|
152 |
1831
|
tao
|
return self.toString();
|
153 |
|
|
}
|
154 |
|
|
|
155 |
|
|
/**
|
156 |
|
|
* create a String description of the query that this instance represents.
|
157 |
|
|
* This should become a way to get the XML serialization of the query.
|
158 |
|
|
*/
|
159 |
|
|
public String toString() {
|
160 |
|
|
StringBuffer self = new StringBuffer();
|
161 |
|
|
|
162 |
|
|
self.append(" (Query group operator=" + operator + "\n");
|
163 |
|
|
Enumeration en= getChildren();
|
164 |
|
|
while (en.hasMoreElements()) {
|
165 |
|
|
Object qobject = en.nextElement();
|
166 |
|
|
self.append(qobject);
|
167 |
|
|
}
|
168 |
|
|
self.append(" )\n");
|
169 |
|
|
return self.toString();
|
170 |
|
|
}
|
171 |
|
|
}
|