1 package org.tigris.scarab.om; 2 3 48 49 import java.util.List ; 50 import java.util.ArrayList ; 51 import java.util.Iterator ; 52 import java.util.Properties ; 53 import java.io.InputStream ; 54 import java.io.IOException ; 55 56 import org.apache.torque.util.Criteria; 57 import org.apache.torque.om.Persistent; 58 import org.apache.torque.manager.MethodResultCache; 59 import org.apache.fulcrum.localization.Localization; 60 61 import org.tigris.scarab.services.cache.ScarabCache; 62 import org.tigris.scarab.tools.localization.L10NKeySet; 63 import org.tigris.scarab.om.Module; 64 import org.tigris.scarab.om.IssuePeer; 65 import org.tigris.scarab.util.ScarabException; 66 import org.tigris.scarab.util.ScarabConstants; 67 import org.tigris.scarab.util.Log; 68 import org.tigris.scarab.workflow.WorkflowFactory; 69 70 77 public class IssueType 78 extends org.tigris.scarab.om.BaseIssueType 79 implements Persistent 80 { 81 private static final String ISSUE_TYPE = 82 "IssueType"; 83 private static final String GET_TEMPLATE_ISSUE_TYPE = 84 "getTemplateIssueType"; 85 private static final String GET_INSTANCE = 86 "getInstance"; 87 protected static final String GET_ATTRIBUTE_GROUPS = 88 "getAttributeGroups"; 89 protected static final String GET_ATTRIBUTE_GROUP = 90 "getAttributeGroup"; 91 protected static final String GET_R_ISSUETYPE_ATTRIBUTES = 92 "getRIssueTypeAttributes"; 93 protected static final String GET_R_ISSUETYPE_OPTIONS = 94 "getRIssueTypeOptions"; 95 protected static final String GET_ALL_R_ISSUETYPE_OPTIONS = 96 "getAllRIssueTypeOptions"; 97 protected static final String GET_DEFAULT_TEXT_ATTRIBUTE = 98 "getDefaultTextAttribute"; 99 protected static final String GET_QUICK_SEARCH_ATTRIBUTES = 100 "getQuickSearchAttributes"; 101 protected static final String GET_REQUIRED_ATTRIBUTES = 102 "getRequiredAttributes"; 103 protected static final String GET_ACTIVE_ATTRIBUTES = 104 "getActiveAttributes"; 105 106 static final String USER = "user"; 107 static final String NON_USER = "non-user"; 108 109 private static final Properties SYSTEM_CONFIG = new Properties (); 110 111 static 113 { 114 InputStream in = IssueType.class 115 .getResourceAsStream("IssueTypeConfig.properties"); 116 if (in != null) 117 { 118 try 119 { 120 SYSTEM_CONFIG.load(in); 121 } 122 catch(IOException ioe) 123 { 124 Log.get().warn("Exception while loading the file: IssueTypeConfig.properties", ioe); 125 } 126 } 127 } 128 129 130 private IssueType templateIssueType; 132 133 private IssueType parentIssueType; 135 136 140 public IssueType getTemplateIssueType() 141 throws Exception 142 { 143 if (templateIssueType == null) 144 { 145 Criteria crit = new Criteria(); 146 crit.add(IssueTypePeer.PARENT_ID, getIssueTypeId()); 147 List results = IssueTypePeer.doSelect(crit); 148 if (results.isEmpty()) 149 { 150 throw new ScarabException(L10NKeySet.ExceptionTemplateTypeForIssueType); 151 } 152 else 153 { 154 templateIssueType = (IssueType)results.get(0); 155 } 156 } 157 return templateIssueType; 158 } 159 160 164 public IssueType getIssueTypeForTemplateType() 165 throws Exception 166 { 167 if (parentIssueType == null) 168 { 169 parentIssueType = getIssueTypeRelatedByParentId(); 170 } 171 return parentIssueType; 172 } 173 174 175 178 public Integer getTemplateId() 179 throws Exception 180 { 181 return getTemplateIssueType().getIssueTypeId(); 182 } 183 184 187 public boolean hasIssues() 188 throws Exception 189 { 190 return hasIssues((Module) null); 191 } 192 193 199 public String getDisplayName(Module module) 200 throws Exception 201 { 202 String moduleName = module.getRModuleIssueType(this).getDisplayName(); 203 String displayName = getName(); 204 if (!moduleName.equals(displayName)) 205 { 206 displayName = moduleName +" (" + displayName + ")"; 207 } 208 return displayName; 209 } 210 211 214 public boolean hasIssues(Module module) 215 throws Exception 216 { 217 Criteria crit = new Criteria(); 218 crit.add(IssuePeer.TYPE_ID, getIssueTypeId()); 219 if (module != null) 220 { 221 crit.add(IssuePeer.MODULE_ID, module.getModuleId()); 222 } 223 return (IssuePeer.count(crit) > 0); 224 } 225 226 229 public static IssueType getInstance(String issueTypeName) 230 throws Exception 231 { 232 IssueType result = null; 233 Object obj = ScarabCache.get(ISSUE_TYPE, GET_INSTANCE, issueTypeName); 234 if (obj == null) 235 { 236 Criteria crit = new Criteria(); 237 crit.add(IssueTypePeer.NAME, issueTypeName); 238 List issueTypes = IssueTypePeer.doSelect(crit); 239 if(issueTypes == null || issueTypes.size() == 0) 240 { 241 throw new ScarabException(L10NKeySet.ExceptionInvalidIssueType, 242 issueTypeName); 243 } 244 result = (IssueType)issueTypes.get(0); 245 ScarabCache.put(result, ISSUE_TYPE, GET_INSTANCE, issueTypeName); 246 } 247 else 248 { 249 result = (IssueType)obj; 250 } 251 return result; 252 } 253 254 257 public IssueType copyIssueType() 258 throws Exception 259 { 260 IssueType newIssueType = new IssueType(); 261 newIssueType.setName(getName() + " (copy)"); 262 newIssueType.setDescription(getDescription()); 263 newIssueType.setParentId(ScarabConstants.INTEGER_0); 264 newIssueType.save(); 265 Integer newId = newIssueType.getIssueTypeId(); 266 267 IssueType template = IssueTypePeer 269 .retrieveByPK(getTemplateId()); 270 IssueType newTemplate = new IssueType(); 271 newTemplate.setName(template.getName()); 272 newTemplate.setParentId(newId); 273 newTemplate.save(); 274 275 List userRIAs = getRIssueTypeAttributes(false, USER); 277 for (int m=0; m<userRIAs.size(); m++) 278 { 279 RIssueTypeAttribute userRia = (RIssueTypeAttribute)userRIAs.get(m); 280 RIssueTypeAttribute newUserRia = userRia.copyRia(); 281 newUserRia.setIssueTypeId(newId); 282 newUserRia.save(); 283 } 284 285 List attrGroups = getAttributeGroups(false); 287 for (int i = 0; i<attrGroups.size(); i++) 288 { 289 AttributeGroup group = (AttributeGroup)attrGroups.get(i); 290 AttributeGroup newGroup = group.copyGroup(); 291 newGroup.setIssueTypeId(newId); 292 newGroup.save(); 293 294 List attrs = group.getAttributes(); 296 if (attrs != null) 297 { 298 for (int j=0; j<attrs.size(); j++) 299 { 300 Attribute attr = (Attribute)attrs.get(j); 302 RAttributeAttributeGroup raag = group.getRAttributeAttributeGroup(attr); 303 RAttributeAttributeGroup newRaag = new RAttributeAttributeGroup(); 304 newRaag.setAttributeId(raag.getAttributeId()); 305 newRaag.setOrder(raag.getOrder()); 306 newRaag.setGroupId(newGroup.getAttributeGroupId()); 307 newRaag.save(); 308 309 RIssueTypeAttribute ria = getRIssueTypeAttribute(attr); 311 RIssueTypeAttribute newRia = ria.copyRia(); 312 newRia.setIssueTypeId(newId); 313 newRia.save(); 314 315 List rios = getRIssueTypeOptions(attr, false); 317 if (rios != null) 318 { 319 for (int k=0; k<rios.size(); k++) 320 { 321 RIssueTypeOption rio = (RIssueTypeOption)rios.get(k); 322 RIssueTypeOption newRio = rio.copyRio(); 323 newRio.setIssueTypeId(newId); 324 newRio.save(); 325 } 326 } 327 } 328 } 329 } 330 331 WorkflowFactory.getInstance().copyIssueTypeWorkflows(this, newIssueType); 333 334 return newIssueType; 335 } 336 337 340 public void deleteModuleMappings(ScarabUser user) 341 throws Exception 342 { 343 Criteria crit = new Criteria(); 344 crit.add(RModuleIssueTypePeer.ISSUE_TYPE_ID, 345 getIssueTypeId()); 346 List rmits = RModuleIssueTypePeer.doSelect(crit); 347 for (int i=0; i<rmits.size(); i++) 348 { 349 RModuleIssueType rmit = (RModuleIssueType)rmits.get(i); 350 rmit.delete(user); 351 } 352 ScarabCache.clear(); 353 } 354 355 356 359 public void createDefaultGroups() 360 throws Exception 361 { 362 AttributeGroup ag = createNewGroup(); 363 ag.setOrder(1); 364 ag.setDedupe(true); 365 ag.setDescription(null); 366 ag.save(); 367 AttributeGroup ag2 = createNewGroup(); 368 ag2.setOrder(3); 369 ag2.setDedupe(false); 370 ag2.setDescription(null); 371 ag2.save(); 372 } 373 374 public List getAttributeGroups(Module module) 375 throws Exception 376 { 377 return getAttributeGroups(module, false); 378 } 379 380 public List getAttributeGroups(boolean activeOnly) 381 throws Exception 382 { 383 return getAttributeGroups(null, activeOnly); 384 } 385 386 389 public List getAttributeGroups(Module module, boolean activeOnly) 390 throws Exception 391 { 392 List groups = null; 393 Boolean activeBool = activeOnly ? Boolean.TRUE : Boolean.FALSE; 394 Object obj = getMethodResult().get(this, GET_ATTRIBUTE_GROUPS, 395 module, activeBool); 396 if (obj == null) 397 { 398 Criteria crit = new Criteria() 399 .add(AttributeGroupPeer.ISSUE_TYPE_ID, getIssueTypeId()) 400 .addAscendingOrderByColumn(AttributeGroupPeer.PREFERRED_ORDER); 401 if (activeOnly) 402 { 403 crit.add(AttributeGroupPeer.ACTIVE, true); 404 } 405 if (module != null) 406 { 407 crit.add(AttributeGroupPeer.MODULE_ID, module.getModuleId()); 408 } 409 else 410 { 411 crit.add(AttributeGroupPeer.MODULE_ID, 413 (Object )(AttributeGroupPeer.MODULE_ID + " IS NULL"), 414 Criteria.CUSTOM); 415 } 416 groups = AttributeGroupPeer.doSelect(crit); 417 getMethodResult().put(groups, this, GET_ATTRIBUTE_GROUPS, 418 module, activeBool); 419 } 420 else 421 { 422 groups = (List )obj; 423 } 424 return groups; 425 } 426 427 428 public AttributeGroup createNewGroup() 429 throws Exception 430 { 431 return createNewGroup(null); 432 } 433 434 437 public AttributeGroup createNewGroup(Module module) 438 throws Exception 439 { 440 List groups = getAttributeGroups(module, false); 441 AttributeGroup ag = new AttributeGroup(); 442 443 ag.setName(Localization.getString("ScarabBundle", 445 ScarabConstants.DEFAULT_LOCALE, "NewAttributeGroup")); 446 ag.setActive(true); 447 ag.setIssueTypeId(getIssueTypeId()); 448 if (module != null) 449 { 450 ag.setModuleId(module.getModuleId()); 451 } 452 if (groups.size() == 0) 453 { 454 ag.setDedupe(true); 455 ag.setOrder(groups.size() +1); 456 } 457 else 458 { 459 ag.setDedupe(false); 460 ag.setOrder(groups.size() +2); 461 } 462 ag.save(); 463 groups.add(ag); 464 return ag; 465 } 466 467 472 public int getDedupeSequence() 473 throws Exception 474 { 475 return getDedupeSequence(null); 476 } 477 478 485 int getDedupeSequence(Module module) 486 throws Exception 487 { 488 List groups = getAttributeGroups(module, false); 489 int sequence = groups.size() + 1; 490 for (int i = 1; i <= groups.size(); i++) 491 { 492 int order; 493 int previousOrder; 494 try 495 { 496 order = ((AttributeGroup) groups.get(i)).getOrder(); 497 previousOrder = ((AttributeGroup) groups.get(i - 1)).getOrder(); 498 } 499 catch (Exception e) 500 { 501 Log.get().warn("Error accessing dedupe sequence for issue " 502 + "type '" + this + '\'', e); 503 return sequence; 504 } 505 506 if (order != previousOrder + 1) 507 { 508 sequence = order - 1; 509 break; 510 } 511 } 512 return sequence; 513 } 514 515 518 public List getRIssueTypeAttributes() 519 { 520 List rias = null; 521 try 522 { 523 rias = getRIssueTypeAttributes(false); 524 } 525 catch (Exception e) 526 { 527 Log.get().warn("Could not get RIA records for " + getName(), e); 528 } 529 return rias; 530 } 531 532 535 public List getRIssueTypeAttributes(boolean activeOnly) 536 throws Exception 537 { 538 return getRIssueTypeAttributes(activeOnly, "all"); 539 } 540 541 542 545 public List getRIssueTypeAttributes(boolean activeOnly, 546 String attributeType) 547 throws Exception 548 { 549 List rias = null; 550 Boolean activeBool = (activeOnly ? Boolean.TRUE : Boolean.FALSE); 551 Object obj = getMethodResult().get(this, GET_R_ISSUETYPE_ATTRIBUTES, 552 activeBool, attributeType); 553 if (obj == null) 554 { 555 Criteria crit = new Criteria(); 556 crit.add(RIssueTypeAttributePeer.ISSUE_TYPE_ID, 557 getIssueTypeId()); 558 crit.addAscendingOrderByColumn( 559 RIssueTypeAttributePeer.PREFERRED_ORDER); 560 561 if (activeOnly) 562 { 563 crit.add(RIssueTypeAttributePeer.ACTIVE, true); 564 } 565 566 if (USER.equals(attributeType)) 567 { 568 crit.add(AttributePeer.ATTRIBUTE_TYPE_ID, 569 AttributeTypePeer.USER_TYPE_KEY); 570 crit.addJoin(AttributePeer.ATTRIBUTE_ID, 571 RIssueTypeAttributePeer.ATTRIBUTE_ID); 572 } 573 else if (NON_USER.equals(attributeType)) 574 { 575 crit.addJoin(AttributePeer.ATTRIBUTE_ID, 576 RIssueTypeAttributePeer.ATTRIBUTE_ID); 577 crit.add(AttributePeer.ATTRIBUTE_TYPE_ID, 578 AttributeTypePeer.USER_TYPE_KEY, 579 Criteria.NOT_EQUAL); 580 } 581 582 rias = RIssueTypeAttributePeer.doSelect(crit); 583 getMethodResult().put(rias, this, GET_R_ISSUETYPE_ATTRIBUTES, 584 activeBool, attributeType); 585 } 586 else 587 { 588 rias = (List )obj; 589 } 590 return rias; 591 } 592 593 596 public List getAttributes(String attributeType) 597 throws Exception 598 { 599 ArrayList attrs = new ArrayList (); 600 List rias = getRIssueTypeAttributes(true, attributeType); 601 for (int i=0; i<rias.size(); i++) 602 { 603 attrs.add(((RIssueTypeAttribute)rias.get(i)).getAttribute()); 604 } 605 return attrs; 606 } 607 608 611 public RIssueTypeAttribute addRIssueTypeAttribute(Attribute attribute) 612 throws Exception 613 { 614 String attributeType = null; 615 attributeType = (attribute.isUserAttribute() ? USER : NON_USER); 616 617 RIssueTypeAttribute ria = new RIssueTypeAttribute(); 618 ria.setIssueTypeId(getIssueTypeId()); 619 ria.setAttributeId(attribute.getAttributeId()); 620 ria.setOrder(getLastAttribute(attributeType) + 1); 621 ria.save(); 622 getRIssueTypeAttributes(false, attributeType).add(ria); 623 return ria; 624 } 625 626 public RIssueTypeAttribute getRIssueTypeAttribute(Attribute attribute) 627 throws Exception 628 { 629 RIssueTypeAttribute ria = null; 630 List rias = null; 631 if (attribute.isUserAttribute()) 632 { 633 rias = getRIssueTypeAttributes(false, USER); 634 } 635 else 636 { 637 rias = getRIssueTypeAttributes(false, NON_USER); 638 } 639 Iterator i = rias.iterator(); 640 while (i.hasNext()) 641 { 642 RIssueTypeAttribute tempRia = (RIssueTypeAttribute)i.next(); 643 if (tempRia.getAttribute().equals(attribute)) 644 { 645 ria = tempRia; 646 break; 647 } 648 } 649 return ria; 650 } 651 652 655 public List getUserAttributes() 656 throws Exception 657 { 658 return getUserAttributes(true); 659 } 660 661 664 public List getUserAttributes(boolean activeOnly) 665 throws Exception 666 { 667 List rIssueTypeAttributes = getRIssueTypeAttributes(activeOnly, USER); 668 List userAttributes = new ArrayList (); 669 670 for (int i=0; i<rIssueTypeAttributes.size(); i++) 671 { 672 Attribute att = ((RIssueTypeAttribute)rIssueTypeAttributes.get(i)).getAttribute(); 673 userAttributes.add(att); 674 } 675 return userAttributes; 676 } 677 678 683 public int getLastAttribute(String attributeType) 684 throws Exception 685 { 686 List itAttributes = getRIssueTypeAttributes(false, attributeType); 687 int last = 0; 688 689 for (int i=0; i<itAttributes.size(); i++) 690 { 691 int order = ((RIssueTypeAttribute) itAttributes.get(i)) 692 .getOrder(); 693 if (order > last) 694 { 695 last = order; 696 } 697 } 698 return last; 699 } 700 701 702 707 public int getLastAttributeOption(Attribute attribute) 708 throws Exception 709 { 710 List issueTypeOptions = getRIssueTypeOptions(attribute); 711 int last = 0; 712 713 for (int i=0; i<issueTypeOptions.size(); i++) 714 { 715 int order = ((RIssueTypeOption) issueTypeOptions.get(i)) 716 .getOrder(); 717 if (order > last) 718 { 719 last = order; 720 } 721 } 722 return last; 723 } 724 725 728 public RIssueTypeOption addRIssueTypeOption(AttributeOption option) 729 throws Exception 730 { 731 RIssueTypeOption rio = new RIssueTypeOption(); 732 rio.setIssueTypeId(getIssueTypeId()); 733 rio.setOptionId(option.getOptionId()); 734 rio.setOrder(getLastAttributeOption(option.getAttribute()) + 1); 735 rio.save(); 736 getRIssueTypeOptions(option.getAttribute(), false).add(rio); 737 return rio; 738 } 739 740 743 public List getRIssueTypeOptions(Attribute attribute) 744 throws Exception 745 { 746 return getRIssueTypeOptions(attribute, true); 747 } 748 749 752 public List getRIssueTypeOptions(Attribute attribute, boolean activeOnly) 753 throws Exception 754 { 755 List allRIssueTypeOptions = null; 756 allRIssueTypeOptions = getAllRIssueTypeOptions(attribute); 757 758 if (allRIssueTypeOptions != null) 759 { 760 if (activeOnly) 761 { 762 List activeRIssueTypeOptions = 763 new ArrayList (allRIssueTypeOptions.size()); 764 for (int i=0; i<allRIssueTypeOptions.size(); i++) 765 { 766 RIssueTypeOption rio = 767 (RIssueTypeOption)allRIssueTypeOptions.get(i); 768 if (rio.getActive()) 769 { 770 activeRIssueTypeOptions.add(rio); 771 } 772 } 773 allRIssueTypeOptions = activeRIssueTypeOptions; 774 } 775 } 776 return allRIssueTypeOptions; 777 } 778 779 780 private List getAllRIssueTypeOptions(Attribute attribute) 781 throws Exception 782 { 783 List rIssueTypeOpts; 784 Object obj = ScarabCache.get(this, GET_ALL_R_ISSUETYPE_OPTIONS, 785 attribute); 786 if (obj == null) 787 { 788 List options = attribute.getAttributeOptions(false); 789 Integer [] optIds = null; 790 if (options == null) 791 { 792 optIds = new Integer [0]; 793 } 794 else 795 { 796 optIds = new Integer [options.size()]; 797 } 798 for (int i=optIds.length-1; i>=0; i--) 799 { 800 optIds[i] = ((AttributeOption)options.get(i)).getOptionId(); 801 } 802 803 if (optIds.length > 0) 804 { 805 Criteria crit = new Criteria(); 806 crit.add(RIssueTypeOptionPeer.ISSUE_TYPE_ID, getIssueTypeId()); 807 crit.addIn(RIssueTypeOptionPeer.OPTION_ID, optIds); 808 crit.addJoin(RIssueTypeOptionPeer.OPTION_ID, AttributeOptionPeer.OPTION_ID); 809 crit.addAscendingOrderByColumn(RIssueTypeOptionPeer.PREFERRED_ORDER); 810 crit.addAscendingOrderByColumn(AttributeOptionPeer.OPTION_NAME); 811 rIssueTypeOpts = RIssueTypeOptionPeer.doSelect(crit); 812 } 813 else 814 { 815 rIssueTypeOpts = new ArrayList (0); 816 } 817 ScarabCache.put(rIssueTypeOpts, this, GET_ALL_R_ISSUETYPE_OPTIONS, 818 attribute); 819 } 820 else 821 { 822 rIssueTypeOpts = (List )obj; 823 } 824 return rIssueTypeOpts; 825 } 826 827 public RIssueTypeOption getRIssueTypeOption(AttributeOption option) 828 throws Exception 829 { 830 RIssueTypeOption rio = null; 831 List rios = getRIssueTypeOptions(option.getAttribute(), false); 832 Iterator i = rios.iterator(); 833 while (i.hasNext()) 834 { 835 rio = (RIssueTypeOption)i.next(); 836 if (rio.getAttributeOption().equals(option)) 837 { 838 break; 839 } 840 } 841 842 return rio; 843 } 844 845 849 public List getAvailableAttributes(String attributeType) 850 throws Exception 851 { 852 List allAttributes = AttributePeer.getAttributes(attributeType); 853 List availAttributes = new ArrayList (); 854 List rIssueTypeAttributes = getRIssueTypeAttributes(false, 855 attributeType); 856 List attrs = new ArrayList (); 857 for (int i=0; i<rIssueTypeAttributes.size(); i++) 858 { 859 attrs.add( 860 ((RIssueTypeAttribute) rIssueTypeAttributes.get(i)).getAttribute()); 861 } 862 for (int i=0; i<allAttributes.size(); i++) 863 { 864 Attribute att = (Attribute)allAttributes.get(i); 865 if (!attrs.contains(att)) 866 { 867 availAttributes.add(att); 868 } 869 } 870 return availAttributes; 871 } 872 873 874 878 public List getAvailableAttributeOptions(Attribute attribute) 879 throws Exception 880 { 881 List rIssueTypeOptions = getRIssueTypeOptions(attribute, false); 882 List issueTypeOptions = new ArrayList (); 883 if (rIssueTypeOptions != null) 884 { 885 for (int i=0; i<rIssueTypeOptions.size(); i++) 886 { 887 issueTypeOptions.add( 888 ((RIssueTypeOption) rIssueTypeOptions.get(i)).getAttributeOption()); 889 } 890 } 891 892 List allOptions = attribute.getAttributeOptions(false); 893 List availOptions = new ArrayList (); 894 895 for (int i=0; i<allOptions.size(); i++) 896 { 897 AttributeOption option = (AttributeOption)allOptions.get(i); 898 if (!issueTypeOptions.contains(option)) 899 { 900 availOptions.add(option); 901 } 902 } 903 return availOptions; 904 } 905 906 private MethodResultCache getMethodResult() 907 { 908 return IssueTypeManager.getMethodResult(); 909 } 910 911 public String toString() 912 { 913 return '{' + super.toString() + ": name=" + getName() + '}'; 914 } 915 916 920 public List getMatchingAttributeValuesList(Module oldModule, Module newModule, 921 IssueType newIssueType) 922 throws Exception 923 { 924 List matchingAttributes = new ArrayList (); 925 List srcActiveAttrs = getActiveAttributes(oldModule); 926 List destActiveAttrs = newIssueType.getActiveAttributes(newModule); 927 for (int i = 0; i<srcActiveAttrs.size(); i++) 928 { 929 Attribute attr = (Attribute)srcActiveAttrs.get(i); 930 931 if (destActiveAttrs.contains(attr)) 932 { 933 matchingAttributes.add(attr); 934 } 935 } 936 return matchingAttributes; 937 } 938 939 943 public List getOrphanAttributeValuesList(Module oldModule, Module newModule, 944 IssueType newIssueType) 945 throws Exception 946 { 947 List orphanAttributes = new ArrayList (); 948 List srcActiveAttrs = getActiveAttributes(oldModule); 949 List destActiveAttrs = newIssueType.getActiveAttributes(newModule); 950 for (int i = 0; i<srcActiveAttrs.size(); i++) 951 { 952 Attribute attr = (Attribute)srcActiveAttrs.get(i); 953 if (!destActiveAttrs.contains(attr)) 954 { 955 orphanAttributes.add(attr); 956 } 957 } 958 return orphanAttributes; 959 } 960 961 968 public boolean isSystemDefined() 969 throws Exception 970 { 971 boolean systemDefined = false; 972 String name = getName(); 973 if (name != null) 974 { 975 systemDefined = "system".equalsIgnoreCase(SYSTEM_CONFIG.getProperty(name)); 976 } 977 return systemDefined; 978 } 979 980 981 988 public Attribute getDefaultTextAttribute(Module module) 989 throws Exception 990 { 991 Attribute result = null; 992 Object obj = ScarabCache.get(this, GET_DEFAULT_TEXT_ATTRIBUTE); 993 if (obj == null) 994 { 995 Criteria crit = new Criteria() 997 .add(RModuleAttributePeer.MODULE_ID, 998 module.getModuleId()); 999 crit.addAscendingOrderByColumn( 1000 RModuleAttributePeer.PREFERRED_ORDER); 1001 List rmas = getRModuleAttributes(crit); 1002 1003 Iterator i = rmas.iterator(); 1008 while (i.hasNext()) 1009 { 1010 RModuleAttribute rma = (RModuleAttribute)i.next(); 1011 if (rma.getDefaultTextFlag()) 1012 { 1013 result = rma.getAttribute(); 1014 break; 1015 } 1016 } 1017 1018 if (result == null) 1019 { 1020 i = rmas.iterator(); 1022 while (i.hasNext()) 1023 { 1024 RModuleAttribute rma = (RModuleAttribute)i.next(); 1025 Attribute testAttr = rma.getAttribute(); 1026 if (testAttr.isTextAttribute() && 1027 getAttributeGroup(module, testAttr).getActive()) 1028 { 1029 result = testAttr; 1030 break; 1031 } 1032 } 1033 } 1034 ScarabCache.put(result, this, GET_DEFAULT_TEXT_ATTRIBUTE); 1035 } 1036 else 1037 { 1038 result = (Attribute)obj; 1039 } 1040 return result; 1041 } 1042 1043 1048 public List getQuickSearchAttributes(Module module) 1049 throws Exception 1050 { 1051 List attributes = null; 1052 Object obj = ScarabCache.get(this, GET_QUICK_SEARCH_ATTRIBUTES, 1053 module); 1054 if (obj == null) 1055 { 1056 Criteria crit = new Criteria(3) 1057 .add(RModuleAttributePeer.QUICK_SEARCH, true); 1058 addOrderByClause(crit, module); 1059 attributes = getAttributes(crit); 1060 ScarabCache.put(attributes, this, GET_QUICK_SEARCH_ATTRIBUTES, 1061 module); 1062 } 1063 else 1064 { 1065 attributes = (List )obj; 1066 } 1067 return attributes; 1068 } 1069 1070 1073 private List getAttributes(Criteria criteria) 1074 throws Exception 1075 { 1076 List moduleAttributes = getRModuleAttributes(criteria); 1077 List attributes = new ArrayList (moduleAttributes.size()); 1078 for (int i=0; i<moduleAttributes.size(); i++) 1079 { 1080 attributes.add( 1081 ((RModuleAttribute) moduleAttributes.get(i)).getAttribute()); 1082 } 1083 return attributes; 1084 } 1085 1086 1091 public List getRequiredAttributes(Module module) 1092 throws Exception 1093 { 1094 1095 List attributes = null; 1096 Object obj = ScarabCache.get(this, GET_REQUIRED_ATTRIBUTES, 1097 module); 1098 if (obj == null) 1099 { 1100 Criteria crit = new Criteria(3) 1101 .add(RModuleAttributePeer.REQUIRED, true); 1102 crit.add(RModuleAttributePeer.ACTIVE, true); 1103 addOrderByClause(crit, module); 1104 List temp = getAttributes(crit); 1105 List requiredAttributes = new ArrayList (); 1106 for (int i=0; i <temp.size(); i++) 1107 { 1108 Attribute att = (Attribute)temp.get(i); 1109 AttributeGroup group = getAttributeGroup(module, att); 1110 if (group != null && group.getActive()) 1111 { 1112 requiredAttributes.add(att); 1113 } 1114 } 1115 attributes = requiredAttributes; 1116 ScarabCache.put(attributes, this, GET_REQUIRED_ATTRIBUTES, 1117 module); 1118 } 1119 else 1120 { 1121 attributes = (List )obj; 1122 } 1123 return attributes; 1124 1125 } 1126 1127 1132 public List getActiveAttributes(Module module) 1133 throws Exception 1134 { 1135 List attributes = null; 1136 Object obj = ScarabCache.get(this, GET_ACTIVE_ATTRIBUTES, module); 1137 if (obj == null) 1138 { 1139 Criteria crit = new Criteria(2); 1140 crit.add(RModuleAttributePeer.ACTIVE, true); 1141 addOrderByClause(crit, module); 1142 attributes = getAttributes(crit); 1143 ScarabCache.put(attributes, this, GET_ACTIVE_ATTRIBUTES, 1144 module); 1145 } 1146 else 1147 { 1148 attributes = (List )obj; 1149 } 1150 return attributes; 1151 } 1152 1153 private void addOrderByClause(Criteria crit, Module module) 1154 { 1155 crit.addAscendingOrderByColumn(RModuleAttributePeer.PREFERRED_ORDER); 1156 crit.addAscendingOrderByColumn(RModuleAttributePeer.DISPLAY_VALUE); 1157 crit.add(RModuleAttributePeer.MODULE_ID, module.getModuleId()); 1158 } 1159 1160 private AttributeGroup getAttributeGroup(Module module, 1161 Attribute attribute) 1162 throws Exception 1163 { 1164 AttributeGroup group = null; 1165 Object obj = ScarabCache.get(this, GET_ATTRIBUTE_GROUP, 1166 module, attribute); 1167 if (obj == null) 1168 { 1169 Criteria crit = new Criteria() 1170 .add(AttributeGroupPeer.ISSUE_TYPE_ID, getIssueTypeId()) 1171 .add(AttributeGroupPeer.MODULE_ID, 1172 module.getModuleId()) 1173 .addJoin(RAttributeAttributeGroupPeer.GROUP_ID, 1174 AttributeGroupPeer.ATTRIBUTE_GROUP_ID) 1175 .add(RAttributeAttributeGroupPeer.ATTRIBUTE_ID, 1176 attribute.getAttributeId()); 1177 List results = AttributeGroupPeer.doSelect(crit); 1178 if (results.size() > 0) 1179 { 1180 group = (AttributeGroup)results.get(0); 1181 ScarabCache.put(group, this, GET_ATTRIBUTE_GROUP, 1182 module, attribute); 1183 } 1184 } 1185 else 1186 { 1187 group = (AttributeGroup)obj; 1188 } 1189 return group; 1190 } 1191} 1192 | Popular Tags |