1 4 package com.openedit.hittracker; 5 6 import java.util.ArrayList ; 7 import java.util.Date ; 8 import java.util.HashMap ; 9 import java.util.List ; 10 import java.util.Map ; 11 12 13 public class SearchQuery 14 { 15 protected List fieldTerms = new ArrayList (); 16 protected boolean fieldAndTogether = true; 17 protected Map fieldInputs; 18 protected String fieldSortBy; 19 20 public void addGroup( SearchQuery inGroup ) 21 { 22 fieldTerms.add(inGroup); 23 } 24 protected List getTerms() 25 { 26 return fieldTerms; 27 } 28 public void addAfter(String inFieldId, Date inDate, String inFriendly) 29 { 30 } 32 public void addBefore(String inFieldId, Date inDate, String inFriendly) 33 { 34 } 36 37 47 public void addOrsGroup(String inField, String inValue, String inFriendly) 48 { 49 Term term = new Term() 50 { 51 public String toQuery() 52 { 53 StringBuffer orString = new StringBuffer (); 54 String [] 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 inId, String inNots, String inFriendly) 76 { 77 Term term = new Term() 78 { 79 public String toQuery() 80 { 81 StringBuffer orString = new StringBuffer (); 82 83 String [] 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 inNot) 101 { 102 103 addNots(null, inNot, "not " + inNot); 104 } 106 107 141 142 public void addFilter(String 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 toString() 154 { 155 return toQuery(); 156 } 157 public String toQuery() 158 { 159 String op = " OR "; 160 if( isAndTogether()) 161 { 162 op = "+"; 163 } 164 StringBuffer done = new StringBuffer (); 165 for (int i = 0; i < fieldTerms.size(); i++) 166 { 167 Term field= (Term)fieldTerms.get(i); 168 String 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 toFriendly() 183 { 184 String op = " or "; 185 if( isAndTogether()) 186 { 187 op = " and "; 188 } 189 StringBuffer done = new StringBuffer (); 190 for (int i = 0; i < fieldTerms.size(); i++) 191 { 192 Term field= (Term)fieldTerms.get(i); 193 String 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 inId, String inValue, String inFriendly) 209 { 210 Term term = new Term() 211 { 212 public String 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 inValue, String inFriendly) 227 { 228 Term term = new Term() 229 { 230 public String 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 inValue) 252 { 253 addMatches(null, inValue, null); 254 } 255 public void addMatches(String inField, String inVal, String inFriendly) 256 { 257 Term term = new Term(); 258 term.setId(inField); 259 term.setValue(inVal); 260 term.setFriendly(inFriendly); 261 fieldTerms.add(term); 262 } 264 public boolean isEmpty() 265 { 266 return fieldTerms.isEmpty(); 267 } 268 public void addNot(String inField, String inVal) 269 { 270 Term term = new Term() 271 { 272 public String toQuery() 273 { 274 return "-" + getId()+ ":" + getValue(); 275 } 276 }; 277 term.setId(inField); 278 term.setValue(inVal); 279 fieldTerms.add(term); 280 } 282 public void addStartsWith(String inField, String inVal) 283 { 284 addStartsWith(inField, inVal,null); 285 } 286 public void addStartsWith(String inField, String inVal, String inFriendlyFieldName) 287 { 288 Term term = new Term(); 289 term.setId(inField); 290 if( inFriendlyFieldName !=null) 291 { 292 String 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 inKey, String inValue) 308 { 309 getInputs().put(inKey, new String [] { inValue } ); 310 } 311 312 public void putInput( String inKey, String [] inValues) 313 { 314 getInputs().put(inKey, inValues); 315 } 316 317 public String getInput( String inKey) 318 { 319 Object input = getInputs().get(inKey); 320 if( input != null) 321 { 322 if( input instanceof String ) 323 { 324 return (String )input; 325 } 326 else 327 { 328 String [] vals = (String [])input; 329 return vals[0]; 330 } 331 } 332 return null; 333 } 334 protected Map getInputs() 335 { 336 if (fieldInputs == null) 337 { 338 fieldInputs = new HashMap (); 339 } 340 return fieldInputs; 341 } 342 public String getSortBy() 343 { 344 return fieldSortBy; 345 } 346 public void setSortBy(String inSortBy) 347 { 348 fieldSortBy = inSortBy; 349 } 350 351 } 352 | Popular Tags |