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 |
|
|
/** a utility class that represents a group of terms in a query */
|
42 |
|
|
public class QueryGroup {
|
43 |
|
|
private String operator = null; // indicates how query terms are combined
|
44 |
|
|
private Vector children = null; // the list of query terms and groups
|
45 |
|
|
private int countPercentageSearchItem = 0;
|
46 |
2366
|
sgarg
|
/**
|
47 |
|
|
* construct a new QueryGroup
|
48 |
1831
|
tao
|
*
|
49 |
2366
|
sgarg
|
* @param operator the boolean conector used to connect query terms
|
50 |
1831
|
tao
|
* in this query group
|
51 |
|
|
*/
|
52 |
|
|
public QueryGroup(String operator) {
|
53 |
|
|
this.operator = operator;
|
54 |
|
|
children = new Vector();
|
55 |
|
|
}
|
56 |
|
|
|
57 |
2366
|
sgarg
|
/**
|
58 |
1831
|
tao
|
* Add a child QueryGroup to this QueryGroup
|
59 |
|
|
*
|
60 |
|
|
* @param qgroup the query group to be added to the list of terms
|
61 |
|
|
*/
|
62 |
|
|
public void addChild(QueryGroup qgroup) {
|
63 |
2366
|
sgarg
|
children.add((Object)qgroup);
|
64 |
1831
|
tao
|
}
|
65 |
|
|
|
66 |
|
|
/**
|
67 |
|
|
* Add a child QueryTerm to this QueryGroup
|
68 |
|
|
*
|
69 |
|
|
* @param qterm the query term to be added to the list of terms
|
70 |
|
|
*/
|
71 |
|
|
public void addChild(QueryTerm qterm) {
|
72 |
2366
|
sgarg
|
children.add((Object)qterm);
|
73 |
1831
|
tao
|
}
|
74 |
|
|
|
75 |
|
|
/**
|
76 |
|
|
* Retrieve an Enumeration of query terms for this QueryGroup
|
77 |
|
|
*/
|
78 |
|
|
public Enumeration getChildren() {
|
79 |
|
|
return children.elements();
|
80 |
|
|
}
|
81 |
2366
|
sgarg
|
|
82 |
1831
|
tao
|
public int getPercentageSymbolCount()
|
83 |
|
|
{
|
84 |
|
|
return countPercentageSearchItem;
|
85 |
|
|
}
|
86 |
2366
|
sgarg
|
|
87 |
1831
|
tao
|
/**
|
88 |
|
|
* create a SQL serialization of the query that this instance represents
|
89 |
|
|
*/
|
90 |
|
|
public String printSQL(boolean useXMLIndex) {
|
91 |
|
|
StringBuffer self = new StringBuffer();
|
92 |
2373
|
sgarg
|
StringBuffer queryString = new StringBuffer();
|
93 |
|
|
|
94 |
1831
|
tao
|
boolean first = true;
|
95 |
|
|
|
96 |
|
|
Enumeration en= getChildren();
|
97 |
|
|
while (en.hasMoreElements()) {
|
98 |
|
|
Object qobject = en.nextElement();
|
99 |
2503
|
sgarg
|
|
100 |
1831
|
tao
|
if (qobject instanceof QueryGroup) {
|
101 |
2503
|
sgarg
|
if (first) {
|
102 |
|
|
first = false;
|
103 |
|
|
} else {
|
104 |
|
|
if(!queryString.toString().equals("")){
|
105 |
|
|
queryString.append(" " + operator + " ");
|
106 |
|
|
}
|
107 |
|
|
}
|
108 |
1831
|
tao
|
QueryGroup qg = (QueryGroup)qobject;
|
109 |
2373
|
sgarg
|
queryString.append(qg.printSQL(useXMLIndex));
|
110 |
1831
|
tao
|
// count percerntage number
|
111 |
|
|
int count = qg.getPercentageSymbolCount();
|
112 |
|
|
countPercentageSearchItem = countPercentageSearchItem + count;
|
113 |
|
|
} else if (qobject instanceof QueryTerm) {
|
114 |
|
|
QueryTerm qt = (QueryTerm)qobject;
|
115 |
2373
|
sgarg
|
String termQueryString = qt.printSQL(useXMLIndex);
|
116 |
2366
|
sgarg
|
if(!(qt.getSearchMode().equals("contains") && qt.getValue().equals("%"))){
|
117 |
2503
|
sgarg
|
if (first) {
|
118 |
|
|
first = false;
|
119 |
|
|
} else {
|
120 |
|
|
if(!queryString.toString().equals("")){
|
121 |
|
|
queryString.append(" " + operator + " ");
|
122 |
|
|
}
|
123 |
|
|
}
|
124 |
2373
|
sgarg
|
queryString.append(termQueryString);
|
125 |
2366
|
sgarg
|
}
|
126 |
1831
|
tao
|
// count percerntage number
|
127 |
|
|
int count = qt.getPercentageSymbolCount();
|
128 |
|
|
countPercentageSearchItem = countPercentageSearchItem + count;
|
129 |
|
|
} else {
|
130 |
|
|
System.err.println("qobject wrong type: fatal error");
|
131 |
|
|
}
|
132 |
|
|
}
|
133 |
2373
|
sgarg
|
|
134 |
|
|
if(!queryString.toString().equals("")){
|
135 |
|
|
self.append("(");
|
136 |
|
|
self.append(queryString.toString());
|
137 |
|
|
self.append(")");
|
138 |
|
|
}
|
139 |
1831
|
tao
|
return self.toString();
|
140 |
|
|
}
|
141 |
|
|
|
142 |
|
|
/**
|
143 |
|
|
* create a String description of the query that this instance represents.
|
144 |
|
|
* This should become a way to get the XML serialization of the query.
|
145 |
|
|
*/
|
146 |
|
|
public String toString() {
|
147 |
|
|
StringBuffer self = new StringBuffer();
|
148 |
|
|
|
149 |
|
|
self.append(" (Query group operator=" + operator + "\n");
|
150 |
|
|
Enumeration en= getChildren();
|
151 |
|
|
while (en.hasMoreElements()) {
|
152 |
|
|
Object qobject = en.nextElement();
|
153 |
|
|
self.append(qobject);
|
154 |
|
|
}
|
155 |
|
|
self.append(" )\n");
|
156 |
|
|
return self.toString();
|
157 |
|
|
}
|
158 |
|
|
}
|