1 package org.tigris.scarab.om; 2 3 48 49 import java.util.Collections ; 51 import java.util.HashMap ; 52 import java.util.Iterator ; 53 import java.util.List ; 54 import java.util.ArrayList ; 55 import java.util.Date ; 56 import com.workingdogs.village.Record; 57 58 import org.apache.torque.NoRowsException; 60 import org.apache.torque.TooManyRowsException; 61 import org.apache.torque.TorqueException; 62 import org.apache.torque.om.Persistent; 63 import org.apache.torque.om.ObjectKey; 64 import org.apache.torque.om.SimpleKey; 65 import org.apache.torque.util.Criteria; 66 67 import org.tigris.scarab.om.ScarabUserManager; 69 70 import org.tigris.scarab.services.cache.ScarabCache; 71 72 83 public class Attribute 84 extends BaseAttribute 85 implements Persistent, Conditioned 86 { 87 private static final String CLASS_NAME = "Attribute"; 88 89 90 private static final String ATTRIBUTE = 91 CLASS_NAME; 92 93 private static final String GET_INSTANCE = 94 "getInstance"; 95 96 private static final String GET_ALL_ATTRIBUTE_TYPES = 97 "getAllAttributeTypes"; 98 99 private static final String GET_COMPATIBLE_ATTRIBUTE_TYPES = 100 "getCompatibleAttributeTypes"; 101 102 private static final String GET_ATTRIBUTE_TYPE = 103 "getAttributeType"; 104 105 private static final String GET_ALL_ATTRIBUTES = 106 "getAllAttributes"; 107 108 private static final String GET_ALL_ATTRIBUTE_OPTIONS = 109 "getAllAttributeOptions"; 110 111 private static final String GET_ORDERED_ROPTIONOPTION_LIST = 112 "getOrderedROptionOptionList"; 113 114 private static final String SELECT_ONE = "select-one"; 115 private static final String USER_ATTRIBUTE = "user"; 116 private static final String [] TEXT_TYPES = {"string", "email", "long-string", "date"}; 117 118 private List orderedROptionOptionList = null; 119 private List orderedAttributeOptionList = null; 120 private List parentChildAttributeOptions = null; 121 122 private HashMap optionsMap; 123 private List attributeOptionsWithDeleted; 124 private List attributeOptionsWithoutDeleted; 125 126 129 protected Attribute() 130 { 131 } 132 133 static String getCacheKey(ObjectKey key) 134 { 135 String keyString = key.getValue().toString(); 136 return new StringBuffer (CLASS_NAME.length() + keyString.length()) 137 .append(CLASS_NAME).append(keyString).toString(); 138 } 139 140 141 145 public static Attribute getInstance(int id) 146 throws TorqueException 147 { 148 return AttributeManager.getInstance(new Integer (id)); 149 } 150 151 152 161 public static Attribute getInstance(String attributeName) 162 throws Exception 163 { 164 Attribute result = null; 165 Object obj = ScarabCache.get(ATTRIBUTE, GET_INSTANCE, attributeName.toLowerCase()); 167 if (obj == null) 168 { 169 Criteria crit = new Criteria(); 170 crit.add (AttributePeer.ATTRIBUTE_NAME, attributeName); 171 crit.setIgnoreCase(true); 172 List attributes = AttributePeer.doSelect(crit); 173 if (attributes.size() > 0) 174 { 175 result = (Attribute) attributes.get(0); 176 ScarabCache.put(result, ATTRIBUTE, GET_INSTANCE, attributeName.toLowerCase()); 177 } 178 } 179 else 180 { 181 result = (Attribute)obj; 182 } 183 return result; 184 } 185 186 191 public static boolean checkForDuplicate(String attributeName) 192 throws Exception 193 { 194 return (getInstance(attributeName) != null); 195 } 196 197 public static boolean checkForDuplicate(String attributeName, 198 Attribute attribute) 199 throws Exception 200 { 201 return (checkForDuplicate(attributeName) && 202 !attributeName.equals(attribute.getName())); 203 } 204 205 208 public String getCreatedUserName() throws Exception 209 { 210 Integer userId = getCreatedBy(); 211 String userName = null; 212 if (userId == null || userId.intValue() == 0) 213 { 214 userName = "Default"; 216 } 217 else 218 { 219 ScarabUser su = ScarabUserManager 220 .getInstance(SimpleKey.keyFor(userId)); 221 userName = su.getName(); 222 } 223 return userName; 224 } 225 226 229 void doRemoveCaches() 230 { 231 setOrderedROptionOptionList(null); 232 setOrderedAttributeOptionList(null); 233 setParentChildAttributeOptions(null); 234 } 235 236 237 242 public static List getAllAttributeTypes() 243 throws Exception 244 { 245 List result = null; 246 Object obj = ScarabCache.get(ATTRIBUTE, GET_ALL_ATTRIBUTE_TYPES); 247 if (obj == null) 248 { 249 result = AttributeTypePeer.doSelect(new Criteria()); 250 ScarabCache.put(result, ATTRIBUTE, GET_ALL_ATTRIBUTE_TYPES); 251 } 252 else 253 { 254 result = (List )obj; 255 } 256 return result; 257 } 258 259 267 public List getCompatibleAttributeTypes() 268 throws Exception 269 { 270 List result = null; 271 Object obj = ScarabCache.get(this, GET_COMPATIBLE_ATTRIBUTE_TYPES); 272 if (obj == null) 273 { 274 boolean inUse = !isNew(); 275 if (inUse) 276 { 277 Criteria crit = new Criteria(); 279 crit.add(AttributeValuePeer.ATTRIBUTE_ID, getAttributeId()); 280 inUse = AttributeValuePeer.count(crit) > 0; 281 } 282 if (inUse) 283 { 284 if (isTextAttribute()) 285 { 286 Criteria crit = new Criteria(); 287 crit.addIn(AttributeTypePeer.ATTRIBUTE_TYPE_ID, AttributeTypePeer.TEXT_PKS); 288 result = AttributeTypePeer.doSelect(crit); 289 } 290 else 291 { 292 result = Collections.EMPTY_LIST; 293 } 294 } 295 else 296 { 297 result = getAllAttributeTypes(); 298 } 299 ScarabCache.put(result, this, GET_COMPATIBLE_ATTRIBUTE_TYPES); 300 } 301 else 302 { 303 result = (List )obj; 304 } 305 return result; 306 } 307 308 312 public AttributeType getAttributeType() 313 throws TorqueException 314 { 315 AttributeType result = null; 316 Object obj = ScarabCache.get(this, GET_ATTRIBUTE_TYPE); 317 if (obj == null) 318 { 319 result = super.getAttributeType(); 320 ScarabCache.put(result, this, GET_ATTRIBUTE_TYPE); 321 } 322 else 323 { 324 result = (AttributeType)obj; 325 } 326 return result; 327 } 328 329 332 public static List getAllAttributes() 333 throws Exception 334 { 335 List result = null; 336 Object obj = ScarabCache.get(ATTRIBUTE, GET_ALL_ATTRIBUTES); 337 if (obj == null) 338 { 339 result = AttributePeer.doSelect(new Criteria()); 340 ScarabCache.put(result, ATTRIBUTE, GET_ALL_ATTRIBUTES); 341 } 342 else 343 { 344 result = (List )obj; 345 } 346 return result; 347 } 348 349 public boolean isOptionAttribute() 350 throws TorqueException 351 { 352 if (getTypeId() != null) 353 { 354 return getAttributeType().getAttributeClass().getName() 355 .equals(SELECT_ONE); 356 } 357 return false; 358 } 359 360 public boolean isUserAttribute() 361 throws TorqueException 362 { 363 if (getTypeId() != null) 364 { 365 return getAttributeType().getAttributeClass().getName() 366 .equals(USER_ATTRIBUTE); 367 } 368 return false; 369 } 370 371 public boolean isTextAttribute() 372 throws Exception 373 { 374 boolean isText = false; 375 if (getTypeId() != null) 376 { 377 for (int i=0; i<TEXT_TYPES.length && !isText; i++) 378 { 379 isText = TEXT_TYPES[i].equals(getAttributeType().getName()); 380 } 381 } 382 return isText; 383 } 384 385 public boolean isDateAttribute() 386 throws Exception 387 { 388 boolean isDate = false; 389 if(getTypeId() != null) 390 { 391 isDate = "date".equals(getAttributeType().getName()); 392 } 393 return isDate; 394 } 395 396 435 436 437 438 439 440 448 public AttributeOption getAttributeOption(Integer pk) 449 throws TorqueException 450 { 451 if (optionsMap == null) 452 { 453 buildOptionsMap(); 454 } 455 return (AttributeOption)optionsMap.get(pk); 456 } 457 458 462 public AttributeOption getAttributeOption(String optionID) 463 throws TorqueException 464 { 465 if (optionID == null || optionID.length() == 0) 466 { 467 throw new TorqueException("optionId is empty"); } 469 return getAttributeOption(new Integer (optionID)); 470 } 471 472 473 476 private List getAllAttributeOptions() 477 throws TorqueException 478 { 479 List result = null; 480 Object obj = ScarabCache.get(this, GET_ALL_ATTRIBUTE_OPTIONS); 481 if (obj == null) 482 { 483 Criteria crit = new Criteria(); 484 crit.addJoin(AttributeOptionPeer.OPTION_ID, 485 ROptionOptionPeer.OPTION2_ID); 486 crit.add(AttributeOptionPeer.ATTRIBUTE_ID, this.getAttributeId()); 487 crit.addAscendingOrderByColumn(ROptionOptionPeer.PREFERRED_ORDER); 488 result = AttributeOptionPeer.doSelect(crit); 489 ScarabCache.put(result, this, GET_ALL_ATTRIBUTE_OPTIONS); 490 } 491 else 492 { 493 result = (List )obj; 494 } 495 return result; 496 } 497 498 502 void setParentChildAttributeOptions(List value) 503 { 504 parentChildAttributeOptions = value; 505 } 506 507 514 public List getParentChildAttributeOptions() 515 throws TorqueException 516 { 517 if (parentChildAttributeOptions == null) 518 { 519 List rooList = getOrderedROptionOptionList(); 520 List aoList = getOrderedAttributeOptionList(); 521 parentChildAttributeOptions = new ArrayList (rooList.size()); 522 for (int i=0; i<rooList.size();i++) 523 { 524 ROptionOption roo = (ROptionOption)rooList.get(i); 525 AttributeOption ao = (AttributeOption)aoList.get(i); 526 527 ParentChildAttributeOption pcao = ParentChildAttributeOption 528 .getInstance(roo.getOption1Id(), roo.getOption2Id()); 529 pcao.setParentId(roo.getOption1Id()); 530 pcao.setOptionId(roo.getOption2Id()); 531 pcao.setPreferredOrder(roo.getPreferredOrder()); 532 pcao.setWeight(roo.getWeight()); 533 pcao.setName(ao.getName()); 534 pcao.setDeleted(ao.getDeleted()); 535 pcao.setAttributeId(this.getAttributeId()); 536 parentChildAttributeOptions.add(pcao); 537 } 538 } 539 return parentChildAttributeOptions; 540 } 541 542 546 void setOrderedROptionOptionList(List value) 547 { 548 orderedROptionOptionList = value; 549 } 550 551 552 559 public List getOrderedROptionOptionList() 560 throws TorqueException 561 { 562 List result = null; 563 Object obj = ScarabCache.get(this, GET_ORDERED_ROPTIONOPTION_LIST); 564 if (obj == null) 565 { 566 if (orderedROptionOptionList == null) 567 { 568 Criteria crit = new Criteria(); 569 crit.addJoin(AttributeOptionPeer.OPTION_ID, 570 ROptionOptionPeer.OPTION2_ID); 571 crit.add(AttributeOptionPeer.ATTRIBUTE_ID, getAttributeId()); 572 crit.addAscendingOrderByColumn( 573 ROptionOptionPeer.PREFERRED_ORDER); 574 orderedROptionOptionList = ROptionOptionPeer.doSelect(crit); 575 } 576 result = orderedROptionOptionList; 577 ScarabCache.put(result, this, GET_ORDERED_ROPTIONOPTION_LIST); 578 } 579 else 580 { 581 result = (List )obj; 582 } 583 return result; 584 } 585 586 590 void setOrderedAttributeOptionList(List value) 591 { 592 orderedAttributeOptionList = value; 593 } 594 595 602 public List getOrderedAttributeOptionList() 603 throws TorqueException 604 { 605 if (orderedAttributeOptionList == null) 606 { 607 Criteria crit = new Criteria(); 608 crit.addJoin(AttributeOptionPeer.OPTION_ID, ROptionOptionPeer.OPTION2_ID); 609 crit.add(AttributeOptionPeer.ATTRIBUTE_ID, this.getAttributeId()); 610 crit.addAscendingOrderByColumn(ROptionOptionPeer.PREFERRED_ORDER); 611 orderedAttributeOptionList = AttributeOptionPeer.doSelect(crit); 612 } 613 return orderedAttributeOptionList; 614 } 615 616 620 public List getAttributeOptions(boolean includeDeleted) 621 throws TorqueException 622 { 623 List allOptions = getAllAttributeOptions(); 624 List nonDeleted = new ArrayList (allOptions.size()); 625 if (includeDeleted) 626 { 627 return allOptions; 628 } 629 else 630 { 631 for (int i=0; i<allOptions.size(); i++) 632 { 633 AttributeOption option = (AttributeOption)allOptions.get(i); 634 if (!option.getDeleted()) 635 { 636 nonDeleted.add(option); 637 } 638 } 639 return nonDeleted; 640 } 641 } 642 643 646 public synchronized void buildOptionsMap() 647 throws TorqueException 648 { 649 if (getAttributeType().getAttributeClass().getName() 650 .equals(SELECT_ONE)) 651 { 652 attributeOptionsWithDeleted = this.getAllAttributeOptions(); 655 optionsMap = new HashMap ((int)(1.25*attributeOptionsWithDeleted.size()+1)); 656 657 attributeOptionsWithoutDeleted = new ArrayList (attributeOptionsWithDeleted.size()); 658 for (int i=0; i<attributeOptionsWithDeleted.size(); i++) 659 { 660 AttributeOption option = (AttributeOption)attributeOptionsWithDeleted.get(i); 661 optionsMap.put(option.getOptionId(), option); 662 if (!option.getDeleted()) 663 { 664 attributeOptionsWithoutDeleted.add(attributeOptionsWithDeleted.get(i)); 665 } 666 } 667 } 668 } 669 670 674 public List getActivitys() throws TorqueException 675 { 676 return null; 677 } 678 679 683 public Attribute copyAttribute(ScarabUser user) 684 throws Exception 685 { 686 Attribute newAttribute = new Attribute(); 687 newAttribute.setName(getName() + " (copy)"); 688 newAttribute.setDescription(getDescription()); 689 newAttribute.setTypeId(getTypeId()); 690 newAttribute.setPermission(getPermission()); 691 newAttribute.setRequiredOptionId(getRequiredOptionId()); 692 newAttribute.setConditionsArray(getConditionsArray()); 693 newAttribute.setAction(getAction()); 694 newAttribute.setCreatedBy(user.getUserId()); 695 newAttribute.setCreatedDate(new Date ()); 696 newAttribute.setDeleted(getDeleted()); 697 newAttribute.save(); 698 699 List attributeOptions = getAttributeOptions(); 700 for (int i=0;i<attributeOptions.size();i++) 701 { 702 AttributeOption option = (AttributeOption)attributeOptions.get(i); 703 AttributeOption newOption = new AttributeOption(); 704 newOption.setOptionId(option.getOptionId()); 705 newOption.setAttributeId(newAttribute.getAttributeId()); 706 newOption.setName(option.getName()); 707 newOption.setDeleted(option.getDeleted()); 708 newOption.save(); 709 710 List roos = option.getROptionOptionsRelatedByOption2Id(); 712 for (int j=0;j<roos.size();j++) 713 { 714 ROptionOption roo = (ROptionOption)roos.get(j); 715 ROptionOption newRoo = new ROptionOption(); 716 newRoo.setOption2Id(newOption.getOptionId()); 717 newRoo.setOption1Id(roo.getOption1Id()); 718 newRoo.setRelationshipId(roo.getRelationshipId()); 719 newRoo.setWeight(roo.getWeight()); 720 newRoo.setPreferredOrder(roo.getPreferredOrder()); 721 newRoo.save(); 722 } 723 } 724 return newAttribute; 725 } 726 727 730 public boolean hasModuleMappings() 731 throws Exception 732 { 733 return hasMapping((Module) null, (IssueType) null); 734 } 735 736 742 public boolean hasMapping(Module module, IssueType issueType) 743 throws Exception 744 { 745 Criteria crit = new Criteria(); 746 crit.add(RModuleAttributePeer.ATTRIBUTE_ID, 747 getAttributeId()); 748 if (module != null) 749 { 750 crit.add(RModuleAttributePeer.MODULE_ID, 751 module.getModuleId()); 752 } 753 if (issueType != null) 754 { 755 crit.add(RModuleAttributePeer.ISSUE_TYPE_ID, 756 issueType.getIssueTypeId()); 757 } 758 crit.addSelectColumn("count(" + RModuleAttributePeer.ATTRIBUTE_ID + ")"); 759 return ((Record)IssuePeer.doSelectVillageRecords(crit).get(0)) 760 .getValue(1).asInt() > 0; 761 } 762 763 769 public boolean hasGlobalIssueTypeMappings() 770 throws Exception 771 { 772 return hasGlobalMapping((IssueType) null); 773 } 774 775 783 public boolean hasGlobalMapping(IssueType issueType) 784 throws Exception 785 { 786 Criteria crit = new Criteria(); 787 crit.add(RIssueTypeAttributePeer.ATTRIBUTE_ID, 788 getAttributeId()); 789 if (issueType != null) 790 { 791 crit.add(RIssueTypeAttributePeer.ISSUE_TYPE_ID, 792 issueType.getIssueTypeId()); 793 } 794 crit.addSelectColumn("count(" + RIssueTypeAttributePeer.ATTRIBUTE_ID 795 + ')'); 796 return ((Record)IssuePeer.doSelectVillageRecords(crit).get(0)) 797 .getValue(1).asInt() > 0; 798 } 799 800 803 public void deleteModuleMappings() 804 throws Exception 805 { 806 Criteria crit = new Criteria(); 807 crit.add(RAttributeAttributeGroupPeer.ATTRIBUTE_ID, 808 getAttributeId()); 809 crit.addJoin(RAttributeAttributeGroupPeer.GROUP_ID, 810 AttributeGroupPeer.ATTRIBUTE_GROUP_ID); 811 crit.add(AttributeGroupPeer.MODULE_ID, (Object )null, Criteria.NOT_EQUAL); 812 List raags = RAttributeAttributeGroupPeer.doSelect(crit); 813 for (Iterator i = raags.iterator(); i.hasNext();) 814 { 815 ((RAttributeAttributeGroup)i.next()).delete(); 816 } 817 818 crit = new Criteria(); 819 crit.add(RModuleAttributePeer.ATTRIBUTE_ID, 820 getAttributeId()); 821 List rmas = RModuleAttributePeer.doSelect(crit); 822 for (int i=0; i<rmas.size(); i++) 823 { 824 RModuleAttribute rma = (RModuleAttribute)rmas.get(i); 825 rma.delete(true); 826 } 827 ScarabCache.clear(); 828 } 829 830 833 public void deleteIssueTypeMappings() 834 throws Exception 835 { 836 Criteria crit = new Criteria(); 837 crit.add(RAttributeAttributeGroupPeer.ATTRIBUTE_ID, 838 getAttributeId()); 839 crit.addJoin(RAttributeAttributeGroupPeer.GROUP_ID, 840 AttributeGroupPeer.ATTRIBUTE_GROUP_ID); 841 crit.add(AttributeGroupPeer.MODULE_ID, null); 842 List raags = RAttributeAttributeGroupPeer.doSelect(crit); 843 for (Iterator i = raags.iterator(); i.hasNext();) 844 { 845 ((RAttributeAttributeGroup)i.next()).delete(); 846 } 847 848 crit = new Criteria(); 849 crit.add(RIssueTypeAttributePeer.ATTRIBUTE_ID, 850 getAttributeId()); 851 List rias = RIssueTypeAttributePeer.doSelect(crit); 852 for (Iterator i = rias.iterator(); i.hasNext();) 853 { 854 ((RIssueTypeAttribute)i.next()).delete(); 855 } 856 857 ScarabCache.clear(); 858 } 859 860 864 private List getAssociatedIssueTypes() 865 throws Exception 866 { 867 Criteria crit = new Criteria(); 868 crit.add(RIssueTypeAttributePeer.ATTRIBUTE_ID, 869 getAttributeId()); 870 crit.addJoin(RIssueTypeAttributePeer.ISSUE_TYPE_ID, 871 IssueTypePeer.ISSUE_TYPE_ID); 872 List issueTypeList = IssueTypePeer.doSelect(crit); 873 return issueTypeList; 874 } 875 876 883 884 public boolean isSystemDefined() 885 throws Exception 886 { 887 boolean systemDefined = false; 888 List issueTypeList = getAssociatedIssueTypes(); 889 for (Iterator i = issueTypeList.iterator(); i.hasNext();) 890 { 891 if (((IssueType)i.next()).isSystemDefined()) 892 { 893 systemDefined = true; 894 break; 895 } 896 } 897 return systemDefined; 898 } 899 900 904 public AttributeOption getRequiredOption() 905 { 906 AttributeOption option = null; 907 try 908 { 909 option = AttributeOptionPeer.retrieveByPK(this.getRequiredOptionId()); 910 } 911 catch (NoRowsException e) 912 { 913 } 915 catch (TooManyRowsException e) 916 { 917 } 919 catch (TorqueException e) 920 { 921 e.printStackTrace(); 922 } 923 return option; 924 } 925 926 931 public Integer [] getConditionsArray() 932 { 933 List conditions = new ArrayList (); 934 Integer [] aIDs = null; 935 try 936 { 937 conditions = this.getConditions(); 938 aIDs = new Integer [conditions.size()]; 939 int i=0; 940 for (Iterator iter = conditions.iterator(); iter.hasNext(); i++) 941 { 942 Condition cond = (Condition)iter.next(); 943 aIDs[i] = cond.getOptionId(); 944 } 945 } 946 catch (TorqueException e) 947 { 948 this.getLog().error("getConditionsArray: " + e); 949 } 950 return aIDs; 951 } 952 953 public List getConditions() throws TorqueException 954 { 955 if (collConditions == null) 956 { 957 Criteria crit = new Criteria(); 958 crit.add(ConditionPeer.ATTRIBUTE_ID, this.getAttributeId()); 959 crit.add(ConditionPeer.MODULE_ID, null); 960 crit.add(ConditionPeer.TRANSITION_ID, null); 961 crit.add(ConditionPeer.ISSUE_TYPE_ID, null); 962 collConditions = getConditions(crit); 963 } 964 return collConditions; 965 } 966 967 972 public void setConditionsArray(Integer aOptionId[]) throws Exception 973 { 974 Criteria crit = new Criteria(); 975 crit.add(ConditionPeer.ATTRIBUTE_ID, this.getAttributeId()); 976 crit.add(ConditionPeer.MODULE_ID, null); 977 crit.add(ConditionPeer.ISSUE_TYPE_ID, null); 978 crit.add(ConditionPeer.TRANSITION_ID, null); 979 ConditionPeer.doDelete(crit); 980 this.save(); 981 this.getConditions().clear(); 982 ConditionManager.clear(); 983 if (aOptionId != null) 984 for (int i=0; i<aOptionId.length; i++) 985 { 986 if (aOptionId[i].intValue() != 0) 987 { 988 Condition cond = new Condition(); 989 cond.setAttributeId(this.getAttributeId()); 990 cond.setOptionId(aOptionId[i]); 991 cond.setModuleId(null); 992 cond.setIssueTypeId(null); 993 cond.setTransitionId(null); 994 this.addCondition(cond); 995 cond.save(); 996 } 997 } 998 } 999 1006 public boolean isRequiredIf(Integer optionID) throws TorqueException 1007 { 1008 Condition cond = new Condition(); 1009 cond.setAttributeId(this.getAttributeId()); 1010 cond.setOptionId(optionID); 1011 cond.setModuleId(null); 1012 cond.setIssueTypeId(null); 1013 cond.setTransitionId(null); 1014 return this.getConditions().contains(cond); 1015 } 1016 1017 public boolean isConditioned() 1018 { 1019 boolean bRdo = false; 1020 try { 1021 bRdo = this.getConditions().size()>0; 1022 } catch (TorqueException te) 1023 { 1024 } 1026 return bRdo; 1027 } 1028 1029} 1030 | Popular Tags |