1 5 10 package com.opensymphony.workflow.spi.prevayler; 11 12 import com.opensymphony.util.DataUtil; 13 import com.opensymphony.util.TextUtils; 14 15 import com.opensymphony.workflow.StoreException; 16 import com.opensymphony.workflow.query.Expression; 17 import com.opensymphony.workflow.query.FieldExpression; 18 import com.opensymphony.workflow.query.NestedExpression; 19 import com.opensymphony.workflow.query.WorkflowExpressionQuery; 20 import com.opensymphony.workflow.query.WorkflowQuery; 21 import com.opensymphony.workflow.spi.SimpleStep; 22 import com.opensymphony.workflow.spi.SimpleWorkflowEntry; 23 import com.opensymphony.workflow.spi.WorkflowStore; 24 25 import java.security.InvalidParameterException ; 26 27 import java.util.Date ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 31 32 43 public class QueryLogic { 44 46 private WorkflowStore _store = null; 47 48 50 public QueryLogic(WorkflowStore store) { 51 _store = store; 52 } 53 54 private QueryLogic() { 55 super(); 56 } 57 58 60 public boolean query(Long entryId, WorkflowExpressionQuery query) throws StoreException { 61 Expression expression = query.getExpression(); 62 63 if (expression.isNested()) { 64 return this.checkNestedExpression(entryId.longValue(), (NestedExpression) expression); 65 } else { 66 return this.checkExpression(entryId.longValue(), (FieldExpression) expression); 67 } 68 } 69 70 public boolean query(Long entryId, WorkflowQuery query) throws StoreException { 71 if (query.getLeft() == null) { 72 return queryBasic(entryId, query); 73 } else { 74 int operator = query.getOperator(); 75 WorkflowQuery left = query.getLeft(); 76 WorkflowQuery right = query.getRight(); 77 78 switch (operator) { 79 case WorkflowQuery.AND: 80 return query(entryId, left) && query(entryId, right); 81 82 case WorkflowQuery.OR: 83 return query(entryId, left) || query(entryId, right); 84 85 case WorkflowQuery.XOR: 86 return query(entryId, left) ^ query(entryId, right); 87 } 88 } 89 90 return false; 91 } 92 93 private boolean checkExpression(long entryId, FieldExpression expression) throws StoreException { 94 Object value = expression.getValue(); 95 int operator = expression.getOperator(); 96 int field = expression.getField(); 97 int context = expression.getContext(); 98 99 Long id = new Long (entryId); 100 101 if (context == FieldExpression.ENTRY) { 102 SimpleWorkflowEntry theEntry = (SimpleWorkflowEntry) _store.findEntry(entryId); 103 104 if (field == FieldExpression.NAME) { 105 return this.compareText(theEntry.getWorkflowName(), (String ) value, operator); 106 } 107 108 if (field == FieldExpression.STATE) { 109 return this.compareLong(DataUtil.getInt((Integer ) value), theEntry.getState(), operator); 110 } 111 112 throw new InvalidParameterException ("unknown field"); 113 } 114 115 List steps; 116 117 if (context == FieldExpression.CURRENT_STEPS) { 118 steps = (List ) _store.findCurrentSteps(id.longValue()); 120 } else if (context == FieldExpression.HISTORY_STEPS) { 121 steps = (List ) _store.findHistorySteps(id.longValue()); 123 } else { 124 throw new InvalidParameterException ("unknown field context"); 125 } 126 127 if (steps == null) { 128 return false; 129 } 130 131 boolean expressionResult = false; 132 133 switch (field) { 134 case FieldExpression.ACTION: 135 136 long actionId = DataUtil.getInt((Integer ) value); 137 138 for (Iterator iterator = steps.iterator(); iterator.hasNext();) { 139 SimpleStep step = (SimpleStep) iterator.next(); 140 141 if (this.compareLong(step.getActionId(), actionId, operator)) { 142 expressionResult = true; 143 144 break; 145 } 146 } 147 148 break; 149 150 case FieldExpression.CALLER: 151 152 String caller = (String ) value; 153 154 for (Iterator iterator = steps.iterator(); iterator.hasNext();) { 155 SimpleStep step = (SimpleStep) iterator.next(); 156 157 if (this.compareText(step.getCaller(), caller, operator)) { 158 expressionResult = true; 159 160 break; 161 } 162 } 163 164 break; 165 166 case FieldExpression.FINISH_DATE: 167 168 Date finishDate = (Date ) value; 169 170 for (Iterator iterator = steps.iterator(); iterator.hasNext();) { 171 SimpleStep step = (SimpleStep) iterator.next(); 172 173 if (this.compareDate(step.getFinishDate(), finishDate, operator)) { 174 expressionResult = true; 175 176 break; 177 } 178 } 179 180 break; 181 182 case FieldExpression.OWNER: 183 184 String owner = (String ) value; 185 186 for (Iterator iterator = steps.iterator(); iterator.hasNext();) { 187 SimpleStep step = (SimpleStep) iterator.next(); 188 189 if (this.compareText(step.getOwner(), owner, operator)) { 190 expressionResult = true; 191 192 break; 193 } 194 } 195 196 break; 197 198 case FieldExpression.START_DATE: 199 200 Date startDate = (Date ) value; 201 202 for (Iterator iterator = steps.iterator(); iterator.hasNext();) { 203 SimpleStep step = (SimpleStep) iterator.next(); 204 205 if (this.compareDate(step.getStartDate(), startDate, operator)) { 206 expressionResult = true; 207 208 break; 209 } 210 } 211 212 break; 213 214 case FieldExpression.STEP: 215 216 int stepId = DataUtil.getInt((Integer ) value); 217 218 for (Iterator iterator = steps.iterator(); iterator.hasNext();) { 219 SimpleStep step = (SimpleStep) iterator.next(); 220 221 if (this.compareLong(step.getStepId(), stepId, operator)) { 222 expressionResult = true; 223 224 break; 225 } 226 } 227 228 break; 229 230 case FieldExpression.STATUS: 231 232 String status = (String ) value; 233 234 for (Iterator iterator = steps.iterator(); iterator.hasNext();) { 235 SimpleStep step = (SimpleStep) iterator.next(); 236 237 if (this.compareText(step.getStatus(), status, operator)) { 238 expressionResult = true; 239 240 break; 241 } 242 } 243 244 break; 245 } 246 247 if (expression.isNegate()) { 248 return !expressionResult; 249 } else { 250 return expressionResult; 251 } 252 } 253 254 private boolean checkNestedExpression(long entryId, NestedExpression nestedExpression) throws StoreException { 255 for (int i = 0; i < nestedExpression.getExpressionCount(); i++) { 256 boolean expressionResult; 257 Expression expression = nestedExpression.getExpression(i); 258 259 if (expression.isNested()) { 260 expressionResult = this.checkNestedExpression(entryId, (NestedExpression) expression); 261 } else { 262 expressionResult = this.checkExpression(entryId, (FieldExpression) expression); 263 } 264 265 if (nestedExpression.getExpressionOperator() == NestedExpression.AND) { 266 if (expressionResult == false) { 267 return nestedExpression.isNegate(); 268 } 269 } else if (nestedExpression.getExpressionOperator() == NestedExpression.OR) { 270 if (expressionResult == true) { 271 return !nestedExpression.isNegate(); 272 } 273 } 274 } 275 276 if (nestedExpression.getExpressionOperator() == NestedExpression.AND) { 277 return !nestedExpression.isNegate(); 278 } else if (nestedExpression.getExpressionOperator() == NestedExpression.OR) { 279 return nestedExpression.isNegate(); 280 } 281 282 throw new InvalidParameterException ("unknown operator"); 283 } 284 285 private boolean compareDate(Date value1, Date value2, int operator) { 286 switch (operator) { 287 case FieldExpression.EQUALS: 288 return value1.compareTo(value2) == 0; 289 290 case FieldExpression.NOT_EQUALS: 291 return value1.compareTo(value2) != 0; 292 293 case FieldExpression.GT: 294 return (value1.compareTo(value2) > 0); 295 296 case FieldExpression.LT: 297 return value1.compareTo(value2) < 0; 298 } 299 300 throw new InvalidParameterException ("unknown field operator"); 301 } 302 303 private boolean compareLong(long value1, long value2, int operator) { 304 switch (operator) { 305 case FieldExpression.EQUALS: 306 return value1 == value2; 307 308 case FieldExpression.NOT_EQUALS: 309 return value1 != value2; 310 311 case FieldExpression.GT: 312 return value1 > value2; 313 314 case FieldExpression.LT: 315 return value1 < value2; 316 } 317 318 throw new InvalidParameterException ("unknown field operator"); 319 } 320 321 private boolean compareText(String value1, String value2, int operator) { 322 switch (operator) { 323 case FieldExpression.EQUALS: 324 return TextUtils.noNull(value1).equals(value2); 325 326 case FieldExpression.NOT_EQUALS: 327 return !TextUtils.noNull(value1).equals(value2); 328 329 case FieldExpression.GT: 330 return TextUtils.noNull(value1).compareTo(value2) > 0; 331 332 case FieldExpression.LT: 333 return TextUtils.noNull(value1).compareTo(value2) < 0; 334 } 335 336 throw new InvalidParameterException ("unknown field operator"); 337 } 338 339 private boolean queryBasic(Long entryId, WorkflowQuery query) throws StoreException { 340 Object value = query.getValue(); 342 int operator = query.getOperator(); 343 int field = query.getField(); 344 int type = query.getType(); 345 346 switch (operator) { 347 case WorkflowQuery.EQUALS: 348 return queryEquals(entryId, field, type, value); 349 350 case WorkflowQuery.NOT_EQUALS: 351 return queryNotEquals(entryId, field, type, value); 352 353 case WorkflowQuery.GT: 354 return queryGreaterThan(entryId, field, type, value); 355 356 case WorkflowQuery.LT: 357 return queryLessThan(entryId, field, type, value); 358 } 359 360 return false; 361 } 362 363 private boolean queryEquals(Long entryId, int field, int type, Object value) throws StoreException { 364 List steps; 365 366 if (type == WorkflowQuery.CURRENT) { 367 steps = (List ) _store.findCurrentSteps(entryId.longValue()); 369 } else { 370 steps = (List ) _store.findCurrentSteps(entryId.longValue()); 372 } 373 374 switch (field) { 375 case WorkflowQuery.ACTION: 376 377 long actionId = DataUtil.getInt((Integer ) value); 378 379 for (Iterator iterator = steps.iterator(); iterator.hasNext();) { 380 SimpleStep step = (SimpleStep) iterator.next(); 381 382 if (step.getActionId() == actionId) { 383 return true; 384 } 385 } 386 387 return false; 388 389 case WorkflowQuery.CALLER: 390 391 String caller = (String ) value; 392 393 for (Iterator iterator = steps.iterator(); iterator.hasNext();) { 394 SimpleStep step = (SimpleStep) iterator.next(); 395 396 if (TextUtils.noNull(step.getCaller()).equals(caller)) { 397 return true; 398 } 399 } 400 401 return false; 402 403 case WorkflowQuery.FINISH_DATE: 404 405 Date finishDate = (Date ) value; 406 407 for (Iterator iterator = steps.iterator(); iterator.hasNext();) { 408 SimpleStep step = (SimpleStep) iterator.next(); 409 410 if (finishDate.equals(step.getFinishDate())) { 411 return true; 412 } 413 } 414 415 return false; 416 417 case WorkflowQuery.OWNER: 418 419 String owner = (String ) value; 420 421 for (Iterator iterator = steps.iterator(); iterator.hasNext();) { 422 SimpleStep step = (SimpleStep) iterator.next(); 423 424 if (TextUtils.noNull(step.getOwner()).equals(owner)) { 425 return true; 426 } 427 } 428 429 return false; 430 431 case WorkflowQuery.START_DATE: 432 433 Date startDate = (Date ) value; 434 435 for (Iterator iterator = steps.iterator(); iterator.hasNext();) { 436 SimpleStep step = (SimpleStep) iterator.next(); 437 438 if (startDate.equals(step.getStartDate())) { 439 return true; 440 } 441 } 442 443 return false; 444 445 case WorkflowQuery.STEP: 446 447 int stepId = DataUtil.getInt((Integer ) value); 448 449 for (Iterator iterator = steps.iterator(); iterator.hasNext();) { 450 SimpleStep step = (SimpleStep) iterator.next(); 451 452 if (stepId == step.getStepId()) { 453 return true; 454 } 455 } 456 457 return false; 458 459 case WorkflowQuery.STATUS: 460 461 String status = (String ) value; 462 463 for (Iterator iterator = steps.iterator(); iterator.hasNext();) { 464 SimpleStep step = (SimpleStep) iterator.next(); 465 466 if (TextUtils.noNull(step.getStatus()).equals(status)) { 467 return true; 468 } 469 } 470 471 return false; 472 } 473 474 return false; 475 } 476 477 private boolean queryGreaterThan(Long entryId, int field, int type, Object value) throws StoreException { 478 List steps; 479 480 if (type == WorkflowQuery.CURRENT) { 481 steps = (List ) _store.findCurrentSteps(entryId.longValue()); 483 } else { 484 steps = (List ) _store.findHistorySteps(entryId.longValue()); 486 } 487 488 switch (field) { 489 case WorkflowQuery.ACTION: 490 491 long actionId = DataUtil.getLong((Long ) value); 492 493 for (Iterator iterator = steps.iterator(); iterator.hasNext();) { 494 SimpleStep step = (SimpleStep) iterator.next(); 495 496 if (step.getActionId() > actionId) { 497 return true; 498 } 499 } 500 501 return false; 502 503 case WorkflowQuery.CALLER: 504 505 String caller = (String ) value; 506 507 for (Iterator iterator = steps.iterator(); iterator.hasNext();) { 508 SimpleStep step = (SimpleStep) iterator.next(); 509 510 if (TextUtils.noNull(step.getCaller()).compareTo(caller) > 0) { 511 return true; 512 } 513 } 514 515 return false; 516 517 case WorkflowQuery.FINISH_DATE: 518 519 Date finishDate = (Date ) value; 520 521 for (Iterator iterator = steps.iterator(); iterator.hasNext();) { 522 SimpleStep step = (SimpleStep) iterator.next(); 523 524 if (step.getFinishDate().compareTo(finishDate) > 0) { 525 return true; 526 } 527 } 528 529 return false; 530 531 case WorkflowQuery.OWNER: 532 533 String owner = (String ) value; 534 535 for (Iterator iterator = steps.iterator(); iterator.hasNext();) { 536 SimpleStep step = (SimpleStep) iterator.next(); 537 538 if (TextUtils.noNull(step.getOwner()).compareTo(owner) > 0) { 539 return true; 540 } 541 } 542 543 return false; 544 545 case WorkflowQuery.START_DATE: 546 547 Date startDate = (Date ) value; 548 549 for (Iterator iterator = steps.iterator(); iterator.hasNext();) { 550 SimpleStep step = (SimpleStep) iterator.next(); 551 552 if (step.getStartDate().compareTo(startDate) > 0) { 553 return true; 554 } 555 } 556 557 return false; 558 559 case WorkflowQuery.STEP: 560 561 int stepId = DataUtil.getInt((Integer ) value); 562 563 for (Iterator iterator = steps.iterator(); iterator.hasNext();) { 564 SimpleStep step = (SimpleStep) iterator.next(); 565 566 if (step.getStepId() > stepId) { 567 return true; 568 } 569 } 570 571 return false; 572 573 case WorkflowQuery.STATUS: 574 575 String status = (String ) value; 576 577 for (Iterator iterator = steps.iterator(); iterator.hasNext();) { 578 SimpleStep step = (SimpleStep) iterator.next(); 579 580 if (TextUtils.noNull(step.getStatus()).compareTo(status) > 0) { 581 return true; 582 } 583 } 584 585 return false; 586 } 587 588 return false; 589 } 590 591 private boolean queryLessThan(Long entryId, int field, int type, Object value) throws StoreException { 592 List steps; 593 594 if (type == WorkflowQuery.CURRENT) { 595 steps = (List ) _store.findCurrentSteps(entryId.longValue()); 597 } else { 598 steps = (List ) _store.findHistorySteps(entryId.longValue()); 600 } 601 602 switch (field) { 603 case WorkflowQuery.ACTION: 604 605 long actionId = DataUtil.getLong((Long ) value); 606 607 for (Iterator iterator = steps.iterator(); iterator.hasNext();) { 608 SimpleStep step = (SimpleStep) iterator.next(); 609 610 if (step.getActionId() < actionId) { 611 return true; 612 } 613 } 614 615 return false; 616 617 case WorkflowQuery.CALLER: 618 619 String caller = (String ) value; 620 621 for (Iterator iterator = steps.iterator(); iterator.hasNext();) { 622 SimpleStep step = (SimpleStep) iterator.next(); 623 624 if (TextUtils.noNull(step.getCaller()).compareTo(caller) < 0) { 625 return true; 626 } 627 } 628 629 return false; 630 631 case WorkflowQuery.FINISH_DATE: 632 633 Date finishDate = (Date ) value; 634 635 for (Iterator iterator = steps.iterator(); iterator.hasNext();) { 636 SimpleStep step = (SimpleStep) iterator.next(); 637 638 if (step.getFinishDate().compareTo(finishDate) < 0) { 639 return true; 640 } 641 } 642 643 return false; 644 645 case WorkflowQuery.OWNER: 646 647 String owner = (String ) value; 648 649 for (Iterator iterator = steps.iterator(); iterator.hasNext();) { 650 SimpleStep step = (SimpleStep) iterator.next(); 651 652 if (TextUtils.noNull(step.getOwner()).compareTo(owner) < 0) { 653 return true; 654 } 655 } 656 657 return false; 658 659 case WorkflowQuery.START_DATE: 660 661 Date startDate = (Date ) value; 662 663 for (Iterator iterator = steps.iterator(); iterator.hasNext();) { 664 SimpleStep step = (SimpleStep) iterator.next(); 665 666 if (step.getStartDate().compareTo(startDate) < 0) { 667 return true; 668 } 669 } 670 671 return false; 672 673 case WorkflowQuery.STEP: 674 675 int stepId = DataUtil.getInt((Integer ) value); 676 677 for (Iterator iterator = steps.iterator(); iterator.hasNext();) { 678 SimpleStep step = (SimpleStep) iterator.next(); 679 680 if (step.getStepId() < stepId) { 681 return true; 682 } 683 } 684 685 return false; 686 687 case WorkflowQuery.STATUS: 688 689 String status = (String ) value; 690 691 for (Iterator iterator = steps.iterator(); iterator.hasNext();) { 692 SimpleStep step = (SimpleStep) iterator.next(); 693 694 if (TextUtils.noNull(step.getStatus()).compareTo(status) < 0) { 695 return true; 696 } 697 } 698 699 return false; 700 } 701 702 return false; 703 } 704 705 private boolean queryNotEquals(Long entryId, int field, int type, Object value) throws StoreException { 706 List steps; 707 708 if (type == WorkflowQuery.CURRENT) { 709 steps = (List ) _store.findCurrentSteps(entryId.longValue()); 711 } else { 712 steps = (List ) _store.findHistorySteps(entryId.longValue()); 714 } 715 716 switch (field) { 717 case WorkflowQuery.ACTION: 718 719 long actionId = DataUtil.getLong((Long ) value); 720 721 for (Iterator iterator = steps.iterator(); iterator.hasNext();) { 722 SimpleStep step = (SimpleStep) iterator.next(); 723 724 if (step.getActionId() != actionId) { 725 return true; 726 } 727 } 728 729 return false; 730 731 case WorkflowQuery.CALLER: 732 733 String caller = (String ) value; 734 735 for (Iterator iterator = steps.iterator(); iterator.hasNext();) { 736 SimpleStep step = (SimpleStep) iterator.next(); 737 738 if (!TextUtils.noNull(step.getCaller()).equals(caller)) { 739 return true; 740 } 741 } 742 743 return false; 744 745 case WorkflowQuery.FINISH_DATE: 746 747 Date finishDate = (Date ) value; 748 749 for (Iterator iterator = steps.iterator(); iterator.hasNext();) { 750 SimpleStep step = (SimpleStep) iterator.next(); 751 752 if (!finishDate.equals(step.getFinishDate())) { 753 return true; 754 } 755 } 756 757 return false; 758 759 case WorkflowQuery.OWNER: 760 761 String owner = (String ) value; 762 763 for (Iterator iterator = steps.iterator(); iterator.hasNext();) { 764 SimpleStep step = (SimpleStep) iterator.next(); 765 766 if (!TextUtils.noNull(step.getOwner()).equals(owner)) { 767 return true; 768 } 769 } 770 771 return false; 772 773 case WorkflowQuery.START_DATE: 774 775 Date startDate = (Date ) value; 776 777 for (Iterator iterator = steps.iterator(); iterator.hasNext();) { 778 SimpleStep step = (SimpleStep) iterator.next(); 779 780 if (!startDate.equals(step.getStartDate())) { 781 return true; 782 } 783 } 784 785 return false; 786 787 case WorkflowQuery.STEP: 788 789 int stepId = DataUtil.getInt((Integer ) value); 790 791 for (Iterator iterator = steps.iterator(); iterator.hasNext();) { 792 SimpleStep step = (SimpleStep) iterator.next(); 793 794 if (stepId != step.getStepId()) { 795 return true; 796 } 797 } 798 799 return false; 800 801 case WorkflowQuery.STATUS: 802 803 String status = (String ) value; 804 805 for (Iterator iterator = steps.iterator(); iterator.hasNext();) { 806 SimpleStep step = (SimpleStep) iterator.next(); 807 808 if (!TextUtils.noNull(step.getStatus()).equals(status)) { 809 return true; 810 } 811 } 812 813 return false; 814 } 815 816 return false; 817 } 818 } 819 | Popular Tags |