1 package org.tigris.scarab.om; 2 3 48 49 import java.util.List ; 51 import java.util.ArrayList ; 52 import java.util.Locale ; 53 import java.sql.Connection ; 54 55 import org.apache.commons.lang.ObjectUtils; 56 57 import org.apache.torque.TorqueException; 59 import org.apache.torque.om.Persistent; 60 import org.apache.fulcrum.localization.Localization; 61 62 import org.tigris.scarab.tools.localization.L10NKeySet; 63 import org.tigris.scarab.tools.localization.LocalizationKey; 64 import org.tigris.scarab.util.ScarabConstants; 65 import org.tigris.scarab.util.ScarabException; 66 import org.tigris.scarab.util.Log; 67 import org.tigris.scarab.om.ScarabUserManager; 68 import org.tigris.scarab.om.Module; 69 70 78 public abstract class AttributeValue 79 extends BaseAttributeValue 80 implements Persistent 81 { 82 private ActivitySet activitySet; 83 private Integer oldOptionId; 84 private Integer oldUserId; 85 private String oldValue; 86 private Integer oldNumericValue; 87 private boolean oldOptionIdIsSet; 88 private boolean oldUserIdIsSet; 89 private boolean oldValueIsSet; 90 private boolean oldNumericValueIsSet; 91 private AttributeValue chainedValue; 92 93 private String activityDescription = null; 94 private Activity saveActivity = null; 95 96 private static String className = "AttributeValue"; 97 98 99 102 protected AttributeValue() 103 { 104 oldOptionIdIsSet = false; 105 oldUserIdIsSet = false; 106 oldValueIsSet = false; 107 oldNumericValueIsSet = false; 108 } 109 110 114 public AttributeValue getChainedValue() 115 { 116 return chainedValue; 117 } 118 119 123 public void setChainedValue(AttributeValue v) 124 throws Exception 125 { 126 if (v == null) 127 { 128 this.chainedValue = null; 129 } 130 else 131 { 132 if (v.getAttributeId() == null && getAttributeId() != null) 133 { 134 v.setAttributeId(getAttributeId()); 135 } 136 else if (v.getAttribute() != null 137 && !v.getAttribute().equals(getAttribute())) 138 { 139 throw new ScarabException(L10NKeySet.ExceptionCantChainAttributeValues, 140 v.getAttributeId(), 141 getAttributeId()); 142 } 143 144 if (v.getIssueId() == null && getIssueId() != null) 145 { 146 v.setIssueId(getIssueId()); 147 } 148 else if (v.getIssue() != null 149 && !v.getIssue().equals(getIssue())) 150 { 151 throw new ScarabException(L10NKeySet.ExceptionCantChainIssues, 152 v.getIssueId(), 153 getIssueId()); 154 } 155 156 if (this.chainedValue == null) 157 { 158 this.chainedValue = v; 159 } 160 else 161 { 162 chainedValue.setChainedValue(v); 163 } 164 165 if (activitySet != null) 166 { 167 v.startActivitySet(activitySet); 168 } 169 } 170 } 171 172 178 public List getValueList() 179 { 180 List list = new ArrayList (); 181 list.add(this); 182 AttributeValue av = getChainedValue(); 183 while (av != null) 184 { 185 list.add(av); 186 av = av.getChainedValue(); 187 } 188 return list; 189 } 190 191 194 public void setAttributeId(Integer nk) 195 throws TorqueException 196 { 197 super.setAttributeId(nk); 198 if (chainedValue != null) 199 { 200 chainedValue.setAttributeId(nk); 201 } 202 } 203 204 207 public void setIssueId(Long nk) 208 throws TorqueException 209 { 210 super.setIssueId(nk); 211 if (chainedValue != null) 212 { 213 chainedValue.setIssueId(nk); 214 } 215 } 216 217 226 public void startActivitySet(ActivitySet activitySet) 227 throws ScarabException, Exception 228 { 229 if (activitySet == null) 230 { 231 throw new ScarabException(L10NKeySet.ExceptionCanNotStartActivitySet); 232 } 233 234 if (this.activitySet == null) 235 { 236 this.activitySet = activitySet; 237 } 238 else 239 { 240 throw new ScarabException(L10NKeySet.ExceptionActivitySetInProgress); 241 } 242 255 256 List result = null; 259 Issue issue = getIssue(); 260 if (issue != null) 261 { 262 result = issue 263 .getActivitiesWithNullEndDate(getAttribute()); 264 } 265 if (result != null && result.size() > 0) 266 { 267 for (int i=0; i<result.size(); i++) 268 { 269 Activity a = (Activity)result.get(i); 270 oldOptionId = a.getNewOptionId(); 271 oldValue = a.getNewValue(); 272 } 273 } 274 if (chainedValue != null) 275 { 276 chainedValue.startActivitySet(activitySet); 277 } 278 } 279 280 private void endActivitySet() 281 { 282 this.activitySet = null; 283 this.saveActivity = null; 284 oldOptionId = null; 285 oldValue = null; 286 oldOptionIdIsSet = false; 287 oldValueIsSet = false; 288 if (chainedValue != null) 289 { 290 chainedValue.endActivitySet(); 291 } 292 } 293 294 private void checkActivitySet(LocalizationKey l10nKey) 295 throws ScarabException 296 { 297 if (activitySet == null) 298 { 299 throw new ScarabException(l10nKey); 300 } 301 } 302 303 public String getQueryKey() 304 { 305 String key = super.getQueryKey(); 306 if (key == null || key.length() == 0) 307 { 308 try 309 { 310 key = "__" + getAttribute().getQueryKey(); 311 } 312 catch (Exception e) 313 { 314 key = ""; 315 } 316 } 317 318 return key; 319 } 320 321 public boolean equals(Object obj) 322 { 323 boolean b = false; 324 if (obj instanceof AttributeValue) 325 { 326 b = super.equals(obj); 327 if (!b) 328 { 329 AttributeValue aval = (AttributeValue)obj; 332 b = (getChainedValue() == null) && 333 ObjectUtils.equals(aval.getAttributeId(), getAttributeId()) 334 && ObjectUtils.equals(aval.getIssueId(), getIssueId()); 335 } 336 } 337 return b; 338 } 339 340 public int hashCode() 341 { 342 int retVal = 0; 343 344 if (getChainedValue() != null || getPrimaryKey() != null) 345 { 346 retVal = super.hashCode(); 349 } 350 else 351 { 352 int issueHashCode = 0; 353 if (getIssueId() != null) 354 { 355 issueHashCode = getIssueId().hashCode(); 356 } 357 retVal = getAttributeId().hashCode() ^ issueHashCode; 358 } 359 return retVal; 360 } 361 362 public String toString() 363 { 364 try 365 { 366 String s = '{' + super.toString() + ": " + getAttribute().getName(); 367 if (getOptionId() != null) 368 { 369 s += " optionId=" + getOptionId(); 370 } 371 if (getUserId() != null) 372 { 373 s += " userId=" + getUserId(); 374 } 375 if (getValue() != null) 376 { 377 s += " value=" + getValue(); 378 } 379 380 return s + '}'; 381 } 382 catch (Exception e) 383 { 384 return super.toString(); 385 } 386 } 387 388 392 public String getOptionIdAsString() 393 { 394 String optionIdString = ""; 395 if (getOptionId() != null) 396 { 397 optionIdString = getOptionId().toString(); 398 } 399 return optionIdString; 400 } 401 402 408 public void setOptionId(Integer optionId) 409 throws TorqueException 410 { 411 if ( optionId != null ) 412 { 413 Module module = getIssue().getModule(); 414 IssueType issueType = getIssue().getIssueType(); 415 if (module == null || issueType == null) 416 { 417 AttributeOption option = AttributeOptionManager 418 .getInstance(optionId); 419 setValueOnly(option.getName()); 420 } 421 else 422 { 423 List options = null; 426 try 427 { 428 options = module 429 .getRModuleOptions(getAttribute(), issueType); 430 } 431 catch (Exception e) 432 { 433 if (e instanceof TorqueException) 434 { 435 throw (TorqueException)e; } 437 else 438 { 439 throw new TorqueException(e); } 441 } 442 for (int i=options.size()-1; i>=0; i--) 443 { 444 RModuleOption option = (RModuleOption)options.get(i); 445 if (option.getOptionId().equals(optionId)) 446 { 447 setValueOnly(option.getDisplayValue()); 448 break; 449 } 450 } 451 } 452 } 453 else 454 { 455 setValueOnly(null); 457 } 458 459 setOptionIdOnly(optionId); 460 } 461 462 467 public void setNumericValue(Integer v) 468 { 469 setValueOnly(String.valueOf(v)); 470 if (v != getNumericValue()) 471 { 472 if (!isNew() && !oldNumericValueIsSet) 475 { 476 oldNumericValue = getNumericValue(); 477 oldNumericValueIsSet = true; 478 } 479 super.setNumericValue(v); 480 } 481 } 482 483 protected void setOptionIdOnly(Integer optionId) 484 throws TorqueException 485 { 486 if (!ObjectUtils.equals(optionId, getOptionId())) 487 { 488 if (!isNew() && !oldOptionIdIsSet && getOptionId() != null) 491 { 492 oldOptionId = getOptionId(); 493 oldOptionIdIsSet = true; 494 } 495 super.setOptionId(optionId); 496 } 497 } 498 499 505 public void setUserId(Integer userId) 506 throws TorqueException 507 { 508 if (userId != null) 509 { 510 ScarabUser user = ScarabUserManager.getInstance(userId); 511 setValueOnly(user.getUserName()); 512 } 513 else 514 { 515 setValueOnly(null); 517 } 518 519 setUserIdOnly(userId); 520 } 521 522 protected void setUserIdOnly(Integer value) 523 throws TorqueException 524 { 525 if (!ObjectUtils.equals(value, getUserId())) 526 { 527 if (!isNew() && !oldUserIdIsSet) 530 { 531 oldUserId = getUserId(); 532 oldUserIdIsSet = true; 533 } 534 super.setUserId(value); 535 } 536 } 537 538 544 public Integer [] getOptionIds() 545 throws Exception 546 { 547 List optionIds = new ArrayList (); 548 if (getOptionId() != null) 549 { 550 optionIds.add(getOptionId()); 551 } 552 AttributeValue chainedAV = getChainedValue(); 553 while (chainedAV != null) 554 { 555 if (chainedAV.getOptionId() != null) 556 { 557 optionIds.add(chainedAV.getOptionId()); 558 } 559 chainedAV = chainedAV.getChainedValue(); 560 } 561 if (Log.get().isDebugEnabled()) 562 { 563 Log.get().debug(this + " optionIds: " + optionIds); 564 } 565 566 return (Integer [])optionIds.toArray(new Integer [optionIds.size()]); 567 } 568 569 public void setOptionIds(Integer [] ids) 570 throws Exception 571 { 572 if (ids != null && ids.length > 0) 573 { 574 setOptionId(ids[0]); 575 } 576 if (ids != null && ids.length > 1) 577 { 578 for (int i=1; i<ids.length; i++) 579 { 580 AttributeValue av = AttributeValue 581 .getNewInstance(getAttributeId(), getIssue()); 582 setChainedValue(av); 583 av.setOptionId(ids[i]); 584 } 585 } 586 } 587 588 594 public Integer [] getUserIds() 595 throws Exception 596 { 597 throw new ScarabException(L10NKeySet.ExceptionGetUserIdsNotImplemented); 598 } 599 600 public void setUserIds(Integer [] ids) 601 throws Exception 602 { 603 if (ids != null && ids.length > 0) 604 { 605 setUserId(ids[0]); 606 } 607 if (ids != null && ids.length > 1) 608 { 609 for (int i=1; i<ids.length; i++) 610 { 611 AttributeValue av = AttributeValue 612 .getNewInstance(getAttributeId(), getIssue()); 613 setChainedValue(av); 614 av.setUserId(ids[i]); 615 } 616 } 617 } 618 619 public void setValue(String value) 620 { 621 setValueOnly(value); 622 } 623 624 protected void setValueOnly(String value) 625 { 626 if (!ObjectUtils.equals(value, getValue())) 627 { 628 if (!isNew() && !oldValueIsSet) 631 { 632 oldValue = getValue(); 633 oldValueIsSet = true; 634 } 635 super.setValue(value); 636 } 637 } 638 639 public boolean isSet() 640 { 641 return !(getOptionId() == null && getValue() == null 642 && getUserId() == null); 643 } 644 645 public boolean isRequired() 646 throws Exception 647 { 648 return getRModuleAttribute().getRequired(); 649 } 650 651 public RModuleAttribute getRModuleAttribute() 652 throws Exception 653 { 654 Issue issue = getIssue(); 655 RModuleAttribute rma = null; 656 if (issue != null) 657 { 658 Module module = issue.getModule(); 659 if (module != null) 660 { 661 rma = module.getRModuleAttribute( 662 getAttribute(), getIssue().getIssueType()); 663 } 664 else 665 { 666 throw new Exception ("Module is null: Please report this issue."); } 668 } 669 else 670 { 671 throw new Exception ("Issue is null: Please report this issue."); } 673 return rma; 674 } 675 676 public AttributeOption getAttributeOption() 677 throws TorqueException 678 { 679 return getAttribute() 680 .getAttributeOption(getOptionId()); 681 } 682 683 690 public boolean isQuickSearchAttribute() 691 throws Exception 692 { 693 boolean result = false; 694 List qsAttributes = getIssue().getIssueType() 695 .getQuickSearchAttributes(getIssue().getModule()); 696 for (int i=qsAttributes.size()-1; i>=0; i--) 697 { 698 if (((Attribute)qsAttributes.get(i)).equals(getAttribute())) 699 { 700 result = true; 701 break; 702 } 703 } 704 return result; 705 } 706 707 713 public static AttributeValue getNewInstance( 714 RModuleAttribute rma, Issue issue) throws TorqueException 715 { 716 return getNewInstance(rma.getAttributeId(), issue); 717 } 718 719 725 public static AttributeValue getNewInstance( 726 Integer attId, Issue issue) throws TorqueException 727 { 728 Attribute attribute = AttributeManager.getInstance(attId); 729 return getNewInstance(attribute, issue); 730 } 731 732 738 public static synchronized AttributeValue getNewInstance( 739 Attribute attribute, Issue issue) throws TorqueException 740 { 741 AttributeValue attv = null; 742 try 743 { 744 String className = attribute 745 .getAttributeType().getJavaClassName(); 746 attv = (AttributeValue) 747 Class.forName(className).newInstance(); 748 attv.setAttribute(attribute); 749 attv.setIssue(issue); 750 751 attv.init(); 752 } 753 catch (Exception e) 754 { 755 throw new TorqueException(e); } 757 return attv; 758 } 759 760 767 protected abstract Object loadResources() throws Exception ; 768 769 779 protected abstract void setResources(Object resources); 780 781 784 public abstract void init() throws Exception ; 785 786 public boolean supportsVoting() 787 { 788 return false; 789 } 790 791 public AttributeValue copy() throws TorqueException 792 { 793 AttributeValue copyObj = AttributeValue 794 .getNewInstance(getAttributeId(), getIssue()); 795 return copyInto(copyObj); 796 } 797 798 public void save(Connection dbcon) 799 throws TorqueException 800 { 801 if (isModified() && !getAttribute().isUserAttribute()) 802 { 803 String desc = null; 804 try 805 { 806 checkActivitySet(L10NKeySet.ExceptionCanNotSaveAttributeValue); 807 desc = getActivityDescription(); 808 } 809 catch (Exception e) 810 { 811 throw new TorqueException(e); } 813 if (saveActivity == null) 816 { 817 if (getDeleted()) 818 { 819 saveActivity = ActivityManager 820 .create(getIssue(), getAttribute(), activitySet, 821 desc, null, getNumericValue(), ScarabConstants.INTEGER_0, 822 getUserId(), null, getOptionId(), null, 823 getValue(), null, dbcon); 824 } 825 else 826 { 827 saveActivity = ActivityManager 828 .create(getIssue(), getAttribute(), activitySet, 829 desc, null, oldNumericValue, getNumericValue(), 830 oldUserId, getUserId(), oldOptionId, getOptionId(), 831 oldValue, getValue(), dbcon); 832 } 833 } 834 } 835 super.save(dbcon); 836 if (chainedValue != null) 837 { 838 chainedValue.save(dbcon); 839 } 840 endActivitySet(); 841 } 842 843 848 public Activity getActivity() 849 { 850 return this.saveActivity; 851 } 852 853 public void setActivity(Activity activity) 854 { 855 this.saveActivity = activity; 856 } 857 858 863 public void setActivityDescription(String string) 864 { 865 this.activityDescription = string; 866 } 867 868 872 private String getActivityDescription() 873 throws Exception 874 { 875 if (activityDescription != null) 876 { 877 return activityDescription; 878 } 879 String attributeName = getRModuleAttribute().getDisplayValue(); 880 String newValue = getValue(); 881 882 String result = null; 883 if (getDeleted()) 884 { 885 result = Localization.format( 886 ScarabConstants.DEFAULT_BUNDLE_NAME, 887 getLocale(), 888 "AttributeHasBeenUndefined", attributeName); 889 } 890 else 891 { 892 if (newValue.length() > 30) 893 { 894 newValue = newValue.substring(0,30) + "..."; 895 } 896 if (oldValue == null) 897 { 898 Object [] args = { 899 attributeName, 900 newValue 901 }; 902 result = Localization.format( 903 ScarabConstants.DEFAULT_BUNDLE_NAME, 904 getLocale(), 905 "AttributeSetToNewValue", args); 906 } 907 else 908 { 909 String tmpOldValue = null; 911 if (oldValue.length() > 30) 912 { 913 tmpOldValue = oldValue.substring(0,30) + "..."; 914 } 915 else 916 { 917 tmpOldValue = oldValue; 918 } 919 Object [] args = { 920 attributeName, 921 tmpOldValue, 922 newValue 923 }; 924 result = Localization.format( 925 ScarabConstants.DEFAULT_BUNDLE_NAME, 926 getLocale(), 927 "AttributeChangedFromToNewValue", args); 928 } 929 } 930 return result; 931 } 932 933 937 public void setProperties(AttributeValue attVal1) 938 throws Exception 939 { 940 setAttribute(attVal1.getAttribute()); 941 setIssue(attVal1.getIssue()); 942 setNumericValue(attVal1.getNumericValue()); 943 setOptionId(attVal1.getOptionId()); 944 setUserId(attVal1.getUserId()); 945 setValue(attVal1.getValue()); 946 } 947 948 953 private Locale getLocale() 954 { 955 return ScarabConstants.DEFAULT_LOCALE; 956 } 957 } 958 959 960 961 | Popular Tags |