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 34 public class OJBSearchFilter extends SearchFilter 35 { 36 Criteria criteria = new Criteria(); 37 38 42 43 52 public void matchList(String elementName, Vector values, int oper) 53 { 54 55 criteria = new Criteria(); 57 58 if (oper != NOT_IN) 59 { 60 for (int i = 0; i < values.size(); i++) 61 { 62 Criteria tempCrit = new Criteria(); 63 64 tempCrit.addEqualTo(elementName, values.elementAt(i)); 65 criteria.addOrCriteria(tempCrit); 66 } 67 } 68 else 69 { 70 for (int i = 0; i < values.size(); i++) 71 { 72 criteria.addNotEqualTo(elementName, values.elementAt(i)); 73 } 74 } 75 } 76 77 86 public void matchList(String elementName, String [] values, int oper) 87 { 88 89 criteria = new Criteria(); 92 93 if (oper != NOT_IN) 94 { 95 for (int i = 0; i < values.length; i++) 96 { 97 Criteria tempCrit = new Criteria(); 98 99 tempCrit.addEqualTo(elementName, values[i]); 100 criteria.addOrCriteria(tempCrit); 101 } 102 } 103 else 104 { 105 for (int i = 0; i < values.length; i++) 106 { 107 criteria.addNotEqualTo(elementName, values[i]); 108 } 109 } 110 } 111 112 121 public void matchList(String elementName, int[] values, int oper) 122 { 123 124 criteria = new Criteria(); 127 128 if (oper != NOT_IN) 129 { 130 for (int i = 0; i < values.length; i++) 131 { 132 Criteria tempCrit = new Criteria(); 133 134 tempCrit.addEqualTo(elementName, new Integer (values[i])); 135 criteria.addOrCriteria(tempCrit); 136 } 137 } 138 else 139 { 140 for (int i = 0; i < values.length; i++) 141 { 142 criteria.addNotEqualTo(elementName, new Integer (values[i])); 143 } 144 } 145 } 146 147 156 public void matchValue(String elementName, String value, int oper) 157 { 158 159 criteria = new Criteria(); 161 162 if (oper != NOT_IN) 163 { 164 criteria.addEqualTo(elementName, value); 165 } 166 else 167 { 168 criteria.addNotEqualTo(elementName, value); 169 } 170 } 171 172 178 public void matchValue(String elementName, int value, int oper) 179 { 180 181 criteria = new Criteria(); 183 184 if (oper != NOT_IN) 185 { 186 criteria.addEqualTo(elementName, new Integer (value)); 187 } 188 else 189 { 190 criteria.addNotEqualTo(elementName, new Integer (value)); 191 } 192 } 193 194 203 public void compareFilter(String elementName, String value, 204 int oper) throws DBException 205 { 206 207 criteria = new Criteria(); 209 210 if ((oper & BINARY_OPER_MASK) == 0) 212 { 213 throw new DBException(); 214 } 215 216 switch (oper) 217 { 218 219 case LIKE: 220 { 221 criteria.addLike(elementName, value); 222 223 break; 224 } 225 226 case EQUAL: 227 { 228 criteria.addEqualTo(elementName, value); 229 230 break; 231 } 232 233 case NOT_EQUAL: 234 { 235 criteria.addNotEqualTo(elementName, value); 236 237 break; 238 } 239 240 case LESS_THAN: 241 { 242 criteria.addLessThan(elementName, value); 243 244 break; 245 } 246 247 case GREATER_THAN: 248 { 249 criteria.addGreaterThan(elementName, value); 250 251 break; 252 } 253 254 case GREATER_EQUAL: 255 { 256 criteria.addGreaterOrEqualThan(elementName, value); 257 258 break; 259 } 260 261 case LESS_EQUAL: 262 { 263 criteria.addLessOrEqualThan(elementName, value); 264 265 break; 266 } 267 268 default: 269 { 270 throw new DBException("Unsupported binary operation in OJBSearchFilter!"); 271 } 272 } 273 } 274 275 287 public void matchSet(Hashtable elements, int combine_op, 288 int compare_op) throws DBException 289 { 290 291 criteria = new Criteria(); 293 294 if ((compare_op & BINARY_OPER_MASK) == 0) 296 { 297 throw new DBException(); 298 } 299 300 if (combine_op == AND) 301 { 302 304 for (Enumeration e = elements.keys(); e.hasMoreElements();) 306 { 307 308 String elementName = (String ) e.nextElement(); 311 String elementValue = (String ) elements.get(elementName); 312 313 switch (compare_op) 314 { 315 316 case LIKE: 317 { 318 criteria.addLike(elementName, elementValue); 319 320 break; 321 } 322 323 case EQUAL: 324 { 325 criteria.addEqualTo(elementName, elementValue); 326 327 break; 328 } 329 330 case NOT_EQUAL: 331 { 332 criteria.addNotEqualTo(elementName, elementValue); 333 334 break; 335 } 336 337 case LESS_THAN: 338 { 339 criteria.addLessThan(elementName, elementValue); 340 341 break; 342 } 343 344 case GREATER_THAN: 345 { 346 criteria.addGreaterThan(elementName, elementValue); 347 348 break; 349 } 350 351 case GREATER_EQUAL: 352 { 353 criteria.addGreaterOrEqualThan(elementName, elementValue); 354 355 break; 356 } 357 358 case LESS_EQUAL: 359 { 360 criteria.addLessOrEqualThan(elementName, elementValue); 361 362 break; 363 } 364 365 default: 366 { 367 throw new DBException("Unsupported binary operation in OJBSearchFilter!"); 368 } 369 } } } 372 else if (combine_op == OR) 373 { 374 for (Enumeration e = elements.keys(); e.hasMoreElements();) 377 { 378 379 String elementName = (String ) e.nextElement(); 382 String elementValue = (String ) elements.get(elementName); 383 384 switch (compare_op) 385 { 386 387 case LIKE: 388 { 389 Criteria tempCrit = new Criteria(); 390 391 tempCrit.addLike(elementName, elementValue); 392 criteria.addOrCriteria(tempCrit); 393 394 break; 395 } 396 397 case EQUAL: 398 { 399 Criteria tempCrit = new Criteria(); 400 401 tempCrit.addEqualTo(elementName, elementValue); 402 criteria.addOrCriteria(tempCrit); 403 404 break; 405 } 406 407 case NOT_EQUAL: 408 { 409 Criteria tempCrit = new Criteria(); 410 411 tempCrit.addNotEqualTo(elementName, elementValue); 412 criteria.addOrCriteria(tempCrit); 413 414 break; 415 } 416 417 case LESS_THAN: 418 { 419 Criteria tempCrit = new Criteria(); 420 421 tempCrit.addLessThan(elementName, elementValue); 422 criteria.addOrCriteria(tempCrit); 423 424 break; 425 } 426 427 case GREATER_THAN: 428 { 429 Criteria tempCrit = new Criteria(); 430 431 tempCrit.addGreaterThan(elementName, elementValue); 432 criteria.addOrCriteria(tempCrit); 433 434 break; 435 } 436 437 case GREATER_EQUAL: 438 { 439 Criteria tempCrit = new Criteria(); 440 441 tempCrit.addGreaterOrEqualThan(elementName, elementValue); 442 criteria.addOrCriteria(tempCrit); 443 444 break; 445 } 446 447 case LESS_EQUAL: 448 { 449 Criteria tempCrit = new Criteria(); 450 451 tempCrit.addLessOrEqualThan(elementName, elementValue); 452 criteria.addOrCriteria(tempCrit); 453 454 break; 455 } 456 457 default: 458 { 459 throw new DBException("Unsupported binary operation in OJBSearchFilter!"); 460 } 461 } } 464 } 465 else 466 { 467 468 throw new DBException(); 470 } 471 } 472 473 485 public void matchSet(String [] elementNames, String [] elementValues, 486 int op) throws DBException 487 { 488 489 criteria = new Criteria(); 491 492 if (op == OR) 493 { 494 for (int i = 0; i < elementNames.length; i++) 496 { 497 Criteria tempCrit = new Criteria(); 498 499 tempCrit.addEqualTo(elementNames[i], elementValues[i]); 500 criteria.addOrCriteria(tempCrit); 501 } 502 } 503 else if (op == AND) 504 { 505 for (int i = 0; i < elementNames.length; i++) 506 { 507 criteria.addEqualTo(elementNames[i], elementValues[i]); 508 } 509 } 510 else 511 { 512 throw new DBException(); 513 } 514 } 515 516 523 public void combine(Vector new_filters, int op) throws DBException 524 { 525 for (Enumeration elems = new_filters.elements(); elems.hasMoreElements();) 526 { 527 SearchFilter filter = (SearchFilter) elems.nextElement(); 528 529 combine(filter, op); 530 } 531 } 532 533 540 public void combine(SearchFilter new_filter, int op) throws DBException 541 { 542 543 OJBSearchFilter ojbFilter = (OJBSearchFilter) new_filter; 545 546 switch (op) 547 { 548 549 case OR: 550 { 551 criteria.addOrCriteria(ojbFilter.getCriteria()); 552 break; 553 } 554 555 case AND: 556 { 557 criteria.addAndCriteria(ojbFilter.getCriteria()); 558 break; 559 } 560 561 default: 562 { 563 throw new DBException(); 564 } 565 } 566 } 567 568 574 575 579 public String toString() 580 { 581 582 return criteria.toString(); 584 } 585 586 590 591 595 protected Criteria getCriteria() 596 { 597 598 return criteria; 600 } 601 602 } 603 | Popular Tags |