1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.oodt.cas.filemgr.tools;
19
20
21 import java.net.MalformedURLException;
22 import java.net.URL;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.Vector;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28
29
30 import org.apache.oodt.cas.filemgr.structs.Product;
31 import org.apache.oodt.cas.filemgr.structs.ProductType;
32 import org.apache.oodt.cas.filemgr.structs.RangeQueryCriteria;
33 import org.apache.oodt.cas.filemgr.structs.TermQueryCriteria;
34 import org.apache.oodt.cas.filemgr.structs.exceptions.CatalogException;
35 import org.apache.oodt.cas.filemgr.structs.exceptions.ConnectionException;
36 import org.apache.oodt.cas.filemgr.structs.exceptions.QueryFormulationException;
37 import org.apache.oodt.cas.filemgr.structs.exceptions.RepositoryManagerException;
38 import org.apache.oodt.cas.filemgr.structs.query.ComplexQuery;
39 import org.apache.oodt.cas.filemgr.structs.query.QueryResult;
40 import org.apache.oodt.cas.filemgr.system.XmlRpcFileManagerClient;
41 import org.apache.oodt.cas.filemgr.util.SqlParser;
42
43
44 import org.apache.lucene.index.Term;
45 import org.apache.lucene.queryParser.ParseException;
46 import org.apache.lucene.queryParser.QueryParser;
47 import org.apache.lucene.search.BooleanClause;
48 import org.apache.lucene.search.BooleanQuery;
49 import org.apache.lucene.search.PhraseQuery;
50 import org.apache.lucene.search.Query;
51 import org.apache.lucene.search.RangeQuery;
52 import org.apache.lucene.search.TermQuery;
53
54
55
56
57
58
59
60
61
62
63 @Deprecated
64 public final class QueryTool {
65
66 private static String freeTextBlock = "__FREE__";
67
68 private XmlRpcFileManagerClient client = null;
69
70 private static enum QueryType { LUCENE, SQL };
71
72
73 private static final Logger LOG = Logger.getLogger(QueryTool.class.getName());
74
75 public QueryTool(URL fmUrl) throws InstantiationException {
76 try {
77 client = new XmlRpcFileManagerClient(fmUrl);
78 } catch (ConnectionException e) {
79 throw new InstantiationException(e.getMessage());
80 }
81 }
82
83 public static Query parseQuery(String query) {
84 QueryParser parser;
85
86 parser = new QueryParser(freeTextBlock, new CASAnalyzer());
87 Query luceneQ = null;
88 try {
89 luceneQ = (Query) parser.parse(query);
90 } catch (ParseException e) {
91 System.out.println("Error parsing query text.");
92 System.exit(-1);
93 }
94 return luceneQ;
95 }
96
97 public List query(org.apache.oodt.cas.filemgr.structs.Query query) {
98 List prodIds = new Vector();
99 List products = new Vector();
100
101 List productTypes = safeGetProductTypes();
102
103 if (productTypes != null && productTypes.size() > 0) {
104 for (Iterator i = productTypes.iterator(); i.hasNext();) {
105 ProductType type = (ProductType) i.next();
106 try {
107 products = client.query(query, type);
108 if (products != null && products.size() > 0) {
109 for (Iterator j = products.iterator(); j.hasNext();) {
110 Product product = (Product) j.next();
111 prodIds.add(product.getProductId());
112 }
113 }
114 } catch (CatalogException e) {
115 LOG.log(Level.WARNING, "Exception querying for: ["
116 + type.getName() + "] products: Message: "
117 + e.getMessage());
118 }
119
120 }
121
122 }
123
124 return prodIds;
125
126 }
127
128 public void generateCASQuery(
129 org.apache.oodt.cas.filemgr.structs.Query casQuery,
130 Query luceneQuery) {
131 if (luceneQuery instanceof TermQuery) {
132 Term t = ((TermQuery) luceneQuery).getTerm();
133 if (t.field().equals(freeTextBlock)) {
134
135 } else {
136 casQuery.addCriterion(new TermQueryCriteria(t.field(),
137 t.text()));
138 }
139 } else if (luceneQuery instanceof PhraseQuery) {
140 Term[] t = ((PhraseQuery) luceneQuery).getTerms();
141 if (t[0].field().equals(freeTextBlock)) {
142
143 } else {
144 for (int i = 0; i < t.length; i++)
145 casQuery.addCriterion(new TermQueryCriteria(
146 t[i].field(), t[i].text()));
147 }
148 } else if (luceneQuery instanceof RangeQuery) {
149 Term startT = ((RangeQuery) luceneQuery).getLowerTerm();
150 Term endT = ((RangeQuery) luceneQuery).getUpperTerm();
151 casQuery.addCriterion(new RangeQueryCriteria(startT
152 .field(), startT.text(), endT.text()));
153 } else if (luceneQuery instanceof BooleanQuery) {
154 BooleanClause[] clauses = ((BooleanQuery) luceneQuery).getClauses();
155 for (int i = 0; i < clauses.length; i++) {
156 generateCASQuery(casQuery, (clauses[i]).getQuery());
157 }
158 } else {
159 throw new RuntimeException(
160 "Error parsing query! Cannot determine clause type: ["
161 + luceneQuery.getClass().getName() + "] !");
162 }
163 }
164
165 private List safeGetProductTypes() {
166 List prodTypes = null;
167
168 try {
169 prodTypes = client.getProductTypes();
170 } catch (RepositoryManagerException e) {
171 LOG.log(Level.WARNING,
172 "Error obtaining product types from file manager: ["
173 + client.getFileManagerUrl() + "]: Message: "
174 + e.getMessage());
175 }
176
177 return prodTypes;
178 }
179
180 public static void main(String[] args) throws Exception {
181 String usage = "Usage: QueryTool [options] \n"
182 + "options: \n"
183 + "--url <fm url> \n"
184 + " Lucene like query options: \n"
185 + " --lucene \n"
186 + " -query <query> \n"
187 + " SQL like query options: \n"
188 + " --sql \n"
189 + " -query <query> \n"
190 + " -sortBy <metadata-key> \n"
191 + " -outputFormat <output-format-string> \n";
192
193 String fmUrlStr = null, queryStr = null, sortBy = null, outputFormat = null, delimiter = null;
194 QueryType queryType = null;
195 for (int i = 0; i < args.length; i++) {
196 if (args[i].equals("--lucene")) {
197 if (queryType != null)
198 exit("ERROR: Can only perform one query at a time! \n" + usage);
199 if (args[++i].equals("-query"))
200 queryStr = args[++i];
201 else
202 exit("ERROR: Must specify a query! \n" + usage);
203 queryType = QueryType.LUCENE;
204 }else if (args[i].equals("--sql")) {
205 if (queryType != null)
206 exit("ERROR: Can only perform one query at a time! \n" + usage);
207 if (args[++i].equals("-query"))
208 queryStr = args[++i];
209 else
210 exit("ERROR: Must specify a query! \n" + usage);
211 for (; i < args.length; i++) {
212 if (args[i].equals("-sortBy"))
213 sortBy = args[++i];
214 else if (args[i].equals("-outputFormat"))
215 outputFormat = args[++i];
216 else if (args[i].equals("-delimiter"))
217 delimiter = args[++i];
218 }
219 queryType = QueryType.SQL;
220 }else if (args[i].equals("--url")) {
221 fmUrlStr = args[++i];
222 }
223 }
224
225 if (queryStr == null || fmUrlStr == null)
226 exit("Must specify a query and filemgr url! \n" + usage);
227
228 if (queryType == QueryType.LUCENE) {
229 URL fmUrl = new URL(fmUrlStr);
230 QueryTool queryTool = new QueryTool(fmUrl);
231 org.apache.oodt.cas.filemgr.structs.Query casQuery = new org.apache.oodt.cas.filemgr.structs.Query();
232 queryTool.generateCASQuery(casQuery, parseQuery(queryStr));
233
234 List prodIds = queryTool.query(casQuery);
235 if (prodIds != null && prodIds.size() > 0) {
236 for (Iterator i = prodIds.iterator(); i.hasNext();) {
237 String prodId = (String) i.next();
238 System.out.println(prodId);
239 }
240 }
241 }else {
242 System.out.println(performSqlQuery(queryStr, sortBy, outputFormat, delimiter != null ? delimiter : "\n", fmUrlStr));
243 }
244
245 }
246
247 private static String performSqlQuery(String query, String sortBy, String outputFormat, String delimiter, String filemgrUrl)
248 throws MalformedURLException, CatalogException, ConnectionException, QueryFormulationException {
249 ComplexQuery complexQuery = SqlParser.parseSqlQuery(query);
250 complexQuery.setSortByMetKey(sortBy);
251 complexQuery.setToStringResultFormat(outputFormat);
252 List<QueryResult> results = new XmlRpcFileManagerClient(new URL(filemgrUrl)).complexQuery(complexQuery);
253 StringBuffer returnString = new StringBuffer("");
254 for (QueryResult qr : results)
255 returnString.append(qr.toString() + delimiter);
256 return returnString.substring(0, returnString.length() - delimiter.length());
257 }
258
259 private static void exit(String msg) {
260 System.err.println(msg);
261 System.exit(1);
262 }
263 }