KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > hittracker > SearchQuery


1 /*
2  * Created on Jul 19, 2006
3  */

4 package com.openedit.hittracker;
5
6 import java.util.ArrayList JavaDoc;
7 import java.util.Date JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.List JavaDoc;
10 import java.util.Map JavaDoc;
11
12
13 public class SearchQuery
14 {
15     protected List JavaDoc fieldTerms = new ArrayList JavaDoc();
16     protected boolean fieldAndTogether = true;
17     protected Map JavaDoc fieldInputs;
18     protected String JavaDoc fieldSortBy;
19     
20     public void addGroup( SearchQuery inGroup )
21     {
22         fieldTerms.add(inGroup);
23     }
24     protected List JavaDoc getTerms()
25     {
26         return fieldTerms;
27     }
28     public void addAfter(String JavaDoc inFieldId, Date JavaDoc inDate, String JavaDoc inFriendly)
29     {
30         //not supported
31
}
32     public void addBefore(String JavaDoc inFieldId, Date JavaDoc inDate, String JavaDoc inFriendly)
33     {
34         //throw exception
35
}
36     
37 // public void addAnd(String inString)
38
// {
39
// if (out.length() > 0)
40
// {
41
// out.append("AND ");
42
// }
43
// out.append(inString);
44
// out.append(" ");
45
// }
46

47     public void addOrsGroup(String JavaDoc inField, String JavaDoc inValue, String JavaDoc inFriendly)
48     {
49         Term term = new Term()
50         {
51             public String JavaDoc toQuery()
52             {
53                 StringBuffer JavaDoc orString = new StringBuffer JavaDoc();
54                 String JavaDoc[] orwords = getValue().split("\\s");
55                 if (orwords.length > 0)
56                 {
57                     orString.append("(");
58                     for (int i = 0; i < orwords.length - 1; i++)
59                     {
60                         orString.append(orwords[i]);
61                         orString.append(" OR ");
62                     }
63                     orString.append(orwords[orwords.length - 1]);
64                     orString.append(")");
65                 }
66                 return orString.toString();
67             }
68         };
69         term.setId(inField);
70         term.setValue(inValue);
71         term.setFriendly(inFriendly);
72         getTerms().add( term);
73     }
74     
75     public void addNots(String JavaDoc inId, String JavaDoc inNots, String JavaDoc inFriendly)
76     {
77         Term term = new Term()
78         {
79             public String JavaDoc toQuery()
80             {
81                 StringBuffer JavaDoc orString = new StringBuffer JavaDoc();
82
83                 String JavaDoc[] notwords = getValue().split("\\s");
84                 if (notwords.length > 0)
85                 {
86                     for (int i = 0; i < notwords.length; i++)
87                     {
88                         orString.append(" NOT " + notwords[i] );
89                     }
90                 }
91                 return orString.toString();
92             }
93         };
94         term.setId(inId);
95         term.setValue(inNots);
96         term.setFriendly(inFriendly);
97         getTerms().add( term);
98     }
99
100     public void addNot(String JavaDoc inNot)
101     {
102         
103         addNots(null, inNot, "not " + inNot);
104         //fieldTerms.add("NOT " + inNot);
105
}
106
107 // public void addDayRangeBeforeToday(String inDaterange)
108
// {
109
// GregorianCalendar d = new GregorianCalendar();
110
// SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
111
// String end = format.format(d.getTime());
112
// int less = Integer.parseInt(inDaterange);
113
// int m = 0 - less ;
114
// d.add(GregorianCalendar.DAY_OF_YEAR,m);
115
// //var start = d.getMonth() + '/' + d.getDate() + '/' + d.getFullYear();
116
// String start = format.format(d.getTime());
117
//
118
// String fin = "date:[" + start + " TO " + end + "]";
119
// //alert( );
120
// //query.value = query.value + ' AND ' + fin;
121
//
122
// fieldTerms.add(fin);
123
// }
124
//
125
// public void addMonthRangeBeforeToday(String inMonths)
126
// {
127
// int less = Integer.parseInt(inMonths);
128
// int m = 0 - less ;
129
// GregorianCalendar d = new GregorianCalendar();
130
// //var start = d.getMonth() + '/' + d.getDate() + '/' + d.getFullYear();
131
// SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
132
// String end = format.format(d.getTime());
133
// d.add(GregorianCalendar.MONTH,m);
134
// String start = format.format(d.getTime());
135
//
136
// //Is this right? Looks like wrong format
137
// String fin = "date:[" + start + " TO " + end + "]";
138
// fieldTerms.add(fin);
139
// }
140

141     
142     public void addFilter(String JavaDoc inFilter)
143     {
144         
145         if( inFilter != null && inFilter.length()> 1)
146         {
147             Term term = new Term();
148             term.setValue(inFilter);
149             fieldTerms.add(term);
150         }
151     }
152
153     public String JavaDoc toString()
154     {
155         return toQuery();
156     }
157     public String JavaDoc toQuery()
158     {
159         String JavaDoc op = " OR ";
160         if( isAndTogether())
161         {
162             op = "+";
163         }
164         StringBuffer JavaDoc done = new StringBuffer JavaDoc();
165         for (int i = 0; i < fieldTerms.size(); i++)
166         {
167             Term field= (Term)fieldTerms.get(i);
168             String JavaDoc q = field.toQuery();
169             if( i > 0 && !q.startsWith("+") && !q.startsWith("-"))
170             {
171                 done.append(op);
172             }
173             done.append(q);
174             if( i+1 < fieldTerms.size() )
175             {
176                 done.append(" ");
177             }
178         }
179         return done.toString();
180     }
181     
182     public String JavaDoc toFriendly()
183     {
184         String JavaDoc op = " or ";
185         if( isAndTogether())
186         {
187             op = " and ";
188         }
189         StringBuffer JavaDoc done = new StringBuffer JavaDoc();
190         for (int i = 0; i < fieldTerms.size(); i++)
191         {
192             Term field= (Term)fieldTerms.get(i);
193             String JavaDoc q = field.getFriendly();
194             if( i > 0 && !q.startsWith("+") && !q.startsWith("-"))
195             {
196                 done.append(op);
197             }
198             done.append(field);
199             if( i+1 < fieldTerms.size() )
200             {
201                 done.append(" ");
202             }
203         }
204         return done.toString();
205         
206     }
207     
208     public void addExact(String JavaDoc inId, String JavaDoc inValue, String JavaDoc inFriendly)
209     {
210         Term term = new Term()
211         {
212             public String JavaDoc toQuery()
213             {
214                 return getId() + ":\"" + getValue() + "\"";
215             }
216         };
217         term.setId(inId);
218         term.setValue(inValue);
219         term.setFriendly(inFriendly);
220         getTerms().add( term);
221         
222         
223         
224     }
225
226     public void addExact(String JavaDoc inValue, String JavaDoc inFriendly)
227     {
228         Term term = new Term()
229         {
230             public String JavaDoc toQuery()
231             {
232                 return "\"" + getValue() + "\"";
233             }
234         };
235     
236         term.setValue(inValue);
237         term.setFriendly(inFriendly);
238         getTerms().add( term);
239         
240     
241         
242     }
243     public boolean isAndTogether()
244     {
245         return fieldAndTogether;
246     }
247     public void setAndTogether(boolean inAndTogether)
248     {
249         fieldAndTogether = inAndTogether;
250     }
251     public void addMatches(String JavaDoc inValue)
252     {
253         addMatches(null, inValue, null);
254     }
255     public void addMatches(String JavaDoc inField, String JavaDoc inVal, String JavaDoc inFriendly)
256     {
257         Term term = new Term();
258         term.setId(inField);
259         term.setValue(inVal);
260         term.setFriendly(inFriendly);
261         fieldTerms.add(term);
262         //fieldTerms.add(inField + ":" + inVal);
263
}
264     public boolean isEmpty()
265     {
266         return fieldTerms.isEmpty();
267     }
268     public void addNot(String JavaDoc inField, String JavaDoc inVal)
269     {
270         Term term = new Term()
271         {
272             public String JavaDoc toQuery()
273             {
274                 return "-" + getId()+ ":" + getValue();
275             }
276         };
277         term.setId(inField);
278         term.setValue(inVal);
279         fieldTerms.add(term);
280         //fieldTerms.add("-" + inField + ":" + inVal);
281
}
282     public void addStartsWith(String JavaDoc inField, String JavaDoc inVal)
283     {
284         addStartsWith(inField, inVal,null);
285     }
286     public void addStartsWith(String JavaDoc inField, String JavaDoc inVal, String JavaDoc inFriendlyFieldName)
287     {
288         Term term = new Term();
289         term.setId(inField);
290         if( inFriendlyFieldName !=null)
291         {
292             String JavaDoc full = inFriendlyFieldName + " starts with " + inVal;
293             term.setFriendly(full);
294         }
295         if( !inVal.endsWith("*") && inVal.indexOf(":") == -1)
296         {
297             term.setValue(inVal + "*" );
298         }
299         else
300         {
301             term.setValue(inVal);
302             term.setFriendly(null);
303         }
304         fieldTerms.add(term);
305         
306     }
307     public void putInput( String JavaDoc inKey, String JavaDoc inValue)
308     {
309         getInputs().put(inKey, new String JavaDoc[] { inValue } );
310     }
311
312     public void putInput( String JavaDoc inKey, String JavaDoc[] inValues)
313     {
314         getInputs().put(inKey, inValues);
315     }
316
317     public String JavaDoc getInput( String JavaDoc inKey)
318     {
319         Object JavaDoc input = getInputs().get(inKey);
320         if( input != null)
321         {
322             if( input instanceof String JavaDoc)
323             {
324                 return (String JavaDoc)input;
325             }
326             else
327             {
328                 String JavaDoc[] vals = (String JavaDoc[])input;
329                 return vals[0];
330             }
331         }
332         return null;
333     }
334     protected Map JavaDoc getInputs()
335     {
336         if (fieldInputs == null)
337         {
338             fieldInputs = new HashMap JavaDoc();
339         }
340         return fieldInputs;
341     }
342     public String JavaDoc getSortBy()
343     {
344         return fieldSortBy;
345     }
346     public void setSortBy(String JavaDoc inSortBy)
347     {
348         fieldSortBy = inSortBy;
349     }
350
351 }
352
Popular Tags