1 |
2741
|
costa
|
/**
|
2 |
|
|
* '$RCSfile$'
|
3 |
|
|
* Copyright: 2005 University of New Mexico and the
|
4 |
|
|
* Regents of the University of California and the
|
5 |
|
|
* National Center for Ecological Analysis and Synthesis
|
6 |
|
|
* '$Author$'
|
7 |
|
|
* '$Date$'
|
8 |
|
|
* '$Revision$'
|
9 |
|
|
*
|
10 |
|
|
* This program is free software; you can redistribute it and/or modify
|
11 |
|
|
* it under the terms of the GNU General Public License as published by
|
12 |
|
|
* the Free Software Foundation; either version 2 of the License, or
|
13 |
|
|
* (at your option) any later version.
|
14 |
|
|
*
|
15 |
|
|
* This program is distributed in the hope that it will be useful,
|
16 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18 |
|
|
* GNU General Public License for more details.
|
19 |
|
|
*
|
20 |
|
|
* You should have received a copy of the GNU General Public License
|
21 |
|
|
* along with this program; if not, write to the Free Software
|
22 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
23 |
|
|
*/
|
24 |
|
|
|
25 |
|
|
package edu.ucsb.nceas.metacat.advancedsearch;
|
26 |
|
|
|
27 |
|
|
import java.util.ArrayList;
|
28 |
|
|
|
29 |
|
|
/**
|
30 |
|
|
* @author dcosta
|
31 |
|
|
*
|
32 |
|
|
* AdvancedSearchQueryGroup holds the data needed to produce a valid querygroup
|
33 |
|
|
* string. A querygroup is composed of one or more querygroups and/or
|
34 |
|
|
* queryterms.
|
35 |
|
|
*/
|
36 |
|
|
public class AdvancedSearchQueryGroup {
|
37 |
|
|
|
38 |
|
|
// Object variables
|
39 |
|
|
private boolean includeOuterQueryGroup = true;
|
40 |
|
|
private String indent; // String of spaces for indenting output
|
41 |
|
|
private final int initialLength = 500; // Initial length of the stringBuffer
|
42 |
|
|
private String operator; // "INTERSECT" or "UNION" operator
|
43 |
|
|
private StringBuffer stringBuffer; // Holds the querygroup string
|
44 |
|
|
private ArrayList queryGroupList = new ArrayList(); // List of querygroups
|
45 |
|
|
private ArrayList queryTermList = new ArrayList(); // List of queryterms
|
46 |
|
|
|
47 |
|
|
|
48 |
|
|
/**
|
49 |
|
|
* Constructor. Initializes the operator and the indent.
|
50 |
|
|
*
|
51 |
|
|
* @param operator Must be either "INTERSECT" or "UNION"
|
52 |
|
|
* @param indent A string of spaces for indenting the xml output
|
53 |
|
|
*/
|
54 |
|
|
public AdvancedSearchQueryGroup(final String operator, final String indent) {
|
55 |
|
|
this.operator = operator;
|
56 |
|
|
this.indent = indent;
|
57 |
|
|
|
58 |
|
|
if (!((operator.equals("INTERSECT")) || (operator.equals("UNION")))) {
|
59 |
|
|
System.err.println("Invalid AdvancedSearchQueryGroup operator: " +
|
60 |
|
|
operator);
|
61 |
|
|
}
|
62 |
|
|
}
|
63 |
|
|
|
64 |
|
|
|
65 |
|
|
/**
|
66 |
|
|
* Adds a AdvancedSearchQueryGroup to this AdvancedSearchQueryGroup's list of
|
67 |
|
|
* querygroups.
|
68 |
|
|
*
|
69 |
|
|
* @param queryGroup The AdvancedSearchQueryGroup object to be added to
|
70 |
|
|
* the list.
|
71 |
|
|
*/
|
72 |
|
|
public void addQueryGroup(final AdvancedSearchQueryGroup queryGroup) {
|
73 |
|
|
queryGroupList.add(queryGroup);
|
74 |
|
|
}
|
75 |
|
|
|
76 |
|
|
|
77 |
|
|
/**
|
78 |
|
|
* Adds a AdvancedSearchQueryTerm to this AdvancedSearchQueryGroup's list of
|
79 |
|
|
* queryterms.
|
80 |
|
|
*
|
81 |
|
|
* @param queryTerm The AdvancedSearchQueryTerm object to be added to the
|
82 |
|
|
* list.
|
83 |
|
|
*/
|
84 |
|
|
public void addQueryTerm(final AdvancedSearchQueryTerm queryTerm) {
|
85 |
|
|
queryTermList.add(queryTerm);
|
86 |
|
|
}
|
87 |
|
|
|
88 |
|
|
|
89 |
|
|
/**
|
90 |
|
|
* Sets the boolean value of includeOuterQueryGroup. This enables an
|
91 |
|
|
* optimization. If the user enter search values for only one part of the
|
92 |
|
|
* advanced search form, then includeOuterQueryGroup can be set to false.
|
93 |
|
|
* When false, the QueryGroup object will omit the outer query group from
|
94 |
|
|
* the PathQuery, resulting in a less nested SQL statement.
|
95 |
|
|
*
|
96 |
|
|
* @param b When false, allows the outer QueryGroup to be stripped off,
|
97 |
|
|
* resulting in a less nested SQL statement.
|
98 |
|
|
*/
|
99 |
|
|
public void setIncludeOuterQueryGroup(boolean b) {
|
100 |
|
|
this.includeOuterQueryGroup = b;
|
101 |
|
|
}
|
102 |
|
|
|
103 |
|
|
|
104 |
|
|
/**
|
105 |
|
|
* Creates the XML string that represents this AdvancedSearchQueryGroup,
|
106 |
|
|
* including the querygroups and queryterms that are descendants of this
|
107 |
|
|
* querygroup.
|
108 |
|
|
*
|
109 |
|
|
* @return A XML string fragment representing this querygroup.
|
110 |
|
|
*/
|
111 |
|
|
public String toString() {
|
112 |
|
|
AdvancedSearchQueryGroup queryGroup;
|
113 |
|
|
AdvancedSearchQueryTerm queryTerm;
|
114 |
|
|
|
115 |
|
|
stringBuffer = new StringBuffer(initialLength);
|
116 |
|
|
|
117 |
|
|
if (includeOuterQueryGroup == true) {
|
118 |
|
|
stringBuffer.append(indent +
|
119 |
|
|
"<querygroup operator=\"" + operator + "\">\n");
|
120 |
|
|
}
|
121 |
|
|
|
122 |
|
|
for (int i = 0; i < queryGroupList.size(); i++) {
|
123 |
|
|
queryGroup = (AdvancedSearchQueryGroup) queryGroupList.get(i);
|
124 |
|
|
stringBuffer.append(queryGroup.toString());
|
125 |
|
|
}
|
126 |
|
|
|
127 |
|
|
for (int i = 0; i < queryTermList.size(); i++) {
|
128 |
|
|
queryTerm = (AdvancedSearchQueryTerm) queryTermList.get(i);
|
129 |
|
|
stringBuffer.append(queryTerm.toString());
|
130 |
|
|
}
|
131 |
|
|
|
132 |
|
|
if (includeOuterQueryGroup == true) {
|
133 |
|
|
stringBuffer.append(indent + "</querygroup>\n");
|
134 |
|
|
}
|
135 |
|
|
|
136 |
|
|
return stringBuffer.toString();
|
137 |
|
|
}
|
138 |
|
|
|
139 |
|
|
}
|