1 package org.apache.ojb.broker.query; 2 3 17 18 import java.util.Enumeration ; 19 import java.util.Hashtable ; 20 import java.util.Vector ; 21 22 31 public abstract class SearchFilter 32 { 33 public static final int AND = 0x200; public static final int OR = 0x201; public static final int NOT = 0x2; public static final int IN = 0x3; 40 public static final int NOT_IN = 0x4; 41 public static final int LIKE = 0x109; public static final int EQUAL = 0x10A; public static final int NOT_EQUAL = 0x10B; public static final int LESS_THAN = 0x10C; public static final int GREATER_THAN = 0x10D; public static final int GREATER_EQUAL = 0x10E; public static final int LESS_EQUAL = 0x10F; protected static final int BINARY_OPER_MASK = 0x100; 52 protected static final int LOGICAL_OPER_MASK = 0x200; 53 protected SearchBase m_filter = null; 55 56 60 public SearchFilter() 62 { 63 } 64 65 74 public void matchList(String ElementName, Vector values, int oper) 75 { 76 m_filter = null; 78 if (oper != NOT_IN) 81 { 82 oper = IN; 83 } 85 String [] value_string_array = new String [values.size()]; 86 values.copyInto(value_string_array); 87 m_filter = new SearchBaseLeaf(ElementName, oper, value_string_array); 89 } 90 91 100 public void matchList(String ElementName, String [] values, int oper) 101 { 102 m_filter = null; 104 if (oper != NOT_IN) 107 { 108 oper = IN; 109 } 111 m_filter = new SearchBaseLeaf(ElementName, oper, values); 112 } 113 114 123 public void matchList(String ElementName, int[] values, int oper) 124 { 125 m_filter = null; 127 if (oper != NOT_IN) 130 { 131 oper = IN; 132 } 134 m_filter = new SearchBaseLeafInt(ElementName, oper, values); 135 } 136 137 146 public void matchValue(String ElementName, String value, int oper) 147 { 148 m_filter = null; 150 if (oper != NOT_IN) 153 { 154 oper = IN; 155 } 158 String [] ValueArray = new String [1]; 159 ValueArray[0] = value; 160 m_filter = new SearchBaseLeaf(ElementName, oper, ValueArray); 162 } 163 164 170 public void matchValue(String ElementName, int value, int oper) 171 { 172 m_filter = null; 174 if (oper != NOT_IN) 177 { 178 oper = IN; 179 } 181 m_filter = new SearchBaseLeafInt(ElementName, oper, new int[] 182 { 183 value 184 }); 185 } 186 187 196 public void compareFilter(String ElementName, String value, int oper) throws DBException 197 { 198 m_filter = null; 200 if ((oper & BINARY_OPER_MASK) == 0) 202 { 203 throw new DBException(); 204 } 206 m_filter = new SearchBaseLeafComparison(ElementName, oper, value); 207 } 208 209 221 public void matchSet(Hashtable elements, int combine_op, int compare_op) throws DBException 222 { 223 m_filter = null; 225 if ((combine_op & LOGICAL_OPER_MASK) == 0) 227 { 228 throw new DBException(); 229 } 231 if ((compare_op & BINARY_OPER_MASK) == 0) 232 { 233 throw new DBException(); 234 } 236 Vector compareVector = new Vector (); 237 for (Enumeration e = elements.keys(); e.hasMoreElements();) 239 { 240 String elementName = (String ) e.nextElement(); 243 String elementValue = (String ) elements.get(elementName); 244 SearchBaseLeafComparison comparenode = new SearchBaseLeafComparison(elementName, 246 compare_op, elementValue); 247 compareVector.addElement(comparenode); 249 } 250 m_filter = new SearchBaseNode(combine_op, compareVector); 252 } 253 254 266 public void matchSet(String [] ElementNames, String [] ElementValues, int op) throws DBException 267 { 268 m_filter = null; 270 if ((op & LOGICAL_OPER_MASK) == 0) 272 { 273 throw new DBException(); 274 } 276 Vector leafVector = new Vector (); 277 int numnames = ElementNames.length; 279 for (int i = 0; i < numnames; i++) 280 { 281 SearchBaseLeaf leafnode = new SearchBaseLeaf(ElementNames[i], IN, ElementValues[i]); 283 leafVector.addElement(leafnode); 285 } 286 m_filter = new SearchBaseNode(op, leafVector); 288 } 289 290 297 public void combine(Vector new_filters, int op) throws DBException 298 { 299 if ((op & LOGICAL_OPER_MASK) == 0) 301 { 302 throw new DBException(); 303 } 306 Vector filters = new Vector (); 307 for (Enumeration e = new_filters.elements(); e.hasMoreElements();) 309 { 310 SearchFilter f = (SearchFilter) e.nextElement(); 312 filters.addElement(f.getFilter()); 313 } 314 m_filter = new SearchBaseNode(op, m_filter, filters); 316 } 317 318 325 public void combine(SearchFilter new_filter, int op) throws DBException 326 { 327 if ((op & LOGICAL_OPER_MASK) == 0) 329 { 330 throw new DBException(); 331 } 334 Vector filters = new Vector (); 335 filters.addElement(new_filter.getFilter()); 336 m_filter = new SearchBaseNode(op, m_filter, filters); 338 } 339 340 345 protected static String ConvertBinaryOperator(int oper) 346 { 347 String oper_string; 349 switch (oper) 350 { 351 default: 352 case EQUAL: 353 oper_string = "="; 354 break; 355 case LIKE: 356 oper_string = "LIKE"; 357 break; 358 case NOT_EQUAL: 359 oper_string = "!="; 360 break; 361 case LESS_THAN: 362 oper_string = "<"; 363 break; 364 case GREATER_THAN: 365 oper_string = ">"; 366 break; 367 case GREATER_EQUAL: 368 oper_string = ">="; 369 break; 370 case LESS_EQUAL: 371 oper_string = "<="; 372 break; 373 } 374 return oper_string; 375 } 376 377 385 protected SearchBase getFilter() 386 { 387 return m_filter; 388 } 389 390 394 395 399 public abstract String toString(); 400 401 406 protected class SearchBase 408 { 409 public int oper; 411 412 417 protected SearchBase() 419 { 420 } 421 422 } 423 424 428 protected class SearchBaseLeafComparison extends SearchBase 430 { 431 public String elementName; 433 public String value; 436 437 444 SearchBaseLeafComparison(String ElementName, int oper, String value) 445 { 446 this.elementName = ElementName; 447 this.oper = oper; 448 this.value = value; 449 } 450 451 } 452 453 460 protected class SearchBaseLeaf extends SearchBase 462 { 463 public String elementName; 465 public String [] matches; 468 469 476 SearchBaseLeaf(String ElementName, int oper, String [] matches) 477 { 478 this.elementName = ElementName; 479 this.oper = oper; 480 this.matches = matches; 481 } 482 483 490 SearchBaseLeaf(String ElementName, int oper, String match) 491 { 492 this.elementName = ElementName; 493 this.oper = oper; 494 this.matches = new String [1]; 495 this.matches[0] = match; 496 } 497 498 } 499 500 506 protected class SearchBaseLeafInt extends SearchBase 508 { 509 public String elementName; 511 public int[] matches; 514 515 522 SearchBaseLeafInt(String ElementName, int oper, int[] matches) 523 { 524 this.elementName = ElementName; 525 this.oper = oper; 526 this.matches = matches; 527 } 528 529 536 SearchBaseLeafInt(String ElementName, int oper, int match) 537 { 538 this.elementName = ElementName; 539 this.oper = oper; 540 this.matches = new int[1]; 541 this.matches[0] = match; 542 } 543 544 } 545 546 550 protected class SearchBaseNode extends SearchBase 552 { 553 public Vector nodes; 555 556 563 SearchBaseNode(int oper, Vector new_filters) 564 { 565 this.oper = oper; 566 this.nodes = new_filters; 567 } 568 569 577 SearchBaseNode(int oper, Object filter, Vector new_filters) 578 { 579 this.oper = oper; 581 nodes = new Vector (); 583 if (filter != null) 584 { 585 nodes.addElement(filter); 586 } 588 for (Enumeration e = new_filters.elements(); e.hasMoreElements();) 589 { 590 nodes.addElement(e.nextElement()); 591 } 592 } 593 594 } 595 } 596 | Popular Tags |