1 package org.tigris.scarab.om; 2 3 48 49 import java.io.Serializable ; 51 import java.sql.Connection ; 52 import java.util.ArrayList ; 53 import java.util.Arrays ; 54 import java.util.Date ; 55 import java.util.HashMap ; 56 import java.util.HashSet ; 57 import java.util.Iterator ; 58 import java.util.List ; 59 import java.util.Locale ; 60 import java.util.Map ; 61 import java.util.Set ; 62 import java.util.TreeSet ; 63 64 import org.apache.commons.collections.MapIterator; 65 import org.apache.commons.collections.map.LinkedMap; 66 import org.apache.commons.lang.ObjectUtils; 67 import org.apache.commons.lang.StringUtils; 68 import org.apache.fulcrum.localization.Localization; 69 import org.apache.torque.TorqueException; 70 import org.apache.torque.manager.MethodResultCache; 71 import org.apache.torque.map.DatabaseMap; 72 import org.apache.torque.oid.IDBroker; 73 import org.apache.torque.om.Persistent; 74 import org.apache.torque.util.BasePeer; 75 import org.apache.torque.util.Criteria; 76 import org.apache.turbine.Turbine; 77 import org.tigris.scarab.attribute.OptionAttribute; 78 import org.tigris.scarab.attribute.TotalVotesAttribute; 79 import org.tigris.scarab.attribute.UserAttribute; 80 import org.tigris.scarab.services.cache.ScarabCache; 81 import org.tigris.scarab.services.security.ScarabSecurity; 82 import org.tigris.scarab.tools.ScarabGlobalTool; 83 import org.tigris.scarab.tools.localization.L10NKeySet; 84 import org.tigris.scarab.tools.localization.L10NMessage; 85 import org.tigris.scarab.tools.localization.Localizable; 86 import org.tigris.scarab.util.Log; 87 import org.tigris.scarab.util.MutableBoolean; 88 import org.tigris.scarab.util.ScarabConstants; 89 import org.tigris.scarab.util.ScarabException; 90 import org.tigris.scarab.workflow.WorkflowFactory; 91 92 import com.workingdogs.village.Record; 93 94 102 public class Issue 103 extends BaseIssue 104 implements Persistent 105 { 106 protected static final String GET_ATTRIBUTE_VALUES_MAP = 108 "getAttributeValuesMap"; 109 protected static final String GET_ASSOCIATED_USERS = 110 "getAssociatedUsers"; 111 protected static final String GET_MODULE_ATTRVALUES_MAP = 112 "getModuleAttributeValuesMap"; 113 protected static final String GET_ATTRVALUE = 114 "getAttributeValue"; 115 protected static final String GET_ATTRVALUES = 116 "getAttributeValues"; 117 protected static final String GET_ALL_USERS_TO_EMAIL = 118 "getAllUsersToEmail"; 119 protected static final String GET_USER_ATTRIBUTEVALUE = 120 "getUserAttributeValue"; 121 protected static final String GET_USER_ATTRIBUTEVALUES = 122 "getUserAttributeValues"; 123 protected static final String GET_CREATED_DATE = 124 "getCreatedDate"; 125 protected static final String GET_CREATED_BY = 126 "getCreatedBy"; 127 protected static final String GET_LAST_TRANSACTION = 128 "getLastActivitySet"; 129 protected static final String GET_MODIFIED_BY = 130 "getModifiedBy"; 131 protected static final String GET_MODIFIED_DATE = 132 "getModifiedDate"; 133 protected static final String GET_COMMENTS = 134 "getComments"; 135 protected static final String GET_URLS = 136 "getUrls"; 137 protected static final String GET_EXISTING_ATTACHMENTS = 138 "getExistingAttachments"; 139 protected static final String GET_ACTIVITY = 140 "getActivity"; 141 protected static final String GET_TRANSACTIONS = 142 "getActivitySets"; 143 protected static final String GET_CHILDREN = 144 "getChildren"; 145 protected static final String GET_PARENTS = 146 "getParents"; 147 protected static final String GET_ALL_DEPENDENCY_TYPES = 148 "getAllDependencyTypes"; 149 protected static final String GET_DEPENDENCY = 150 "getDependency"; 151 protected static final String GET_TEMPLATE_TYPES = 152 "getTemplateTypes"; 153 protected static final String GET_TEMPLATEINFO = 154 "getTemplateInfo"; 155 protected static final String GET_CLOSED_DATE = 156 "getClosedDate"; 157 protected static final String GET_ORPHAN_ATTRIBUTEVALUES_LIST = 158 "getNonMatchingAttributeValuesList"; 159 protected static final String GET_DEFAULT_TEXT_ATTRIBUTEVALUE = 160 "getDefaultTextAttributeValue"; 161 protected static final String GET_DEFAULT_TEXT = 162 "getDefaultText"; 163 protected static final String GET_NULL_END_DATE = 164 "getActivitiesWithNullEndDate"; 165 protected static final String GET_INITIAL_ACTIVITYSET = 166 "getInitialActivitySet"; 167 protected static final String GET_HISTORY_LIMIT = 168 "getHistoryLimit"; 169 170 private static final Integer NUMBERKEY_0 = new Integer (0); 171 private static final Integer COPIED = new Integer (1); 172 private static final Integer MOVED = new Integer (2); 173 174 175 private List unSavedAttachments = null; 176 177 181 protected Issue() 182 { 183 } 184 185 protected Issue(Module module, IssueType issueType) 186 throws Exception 187 { 188 this(); 189 setModule(module); 190 setIssueType(issueType); 191 } 192 193 196 public static Issue getNewInstance(Module module, 197 IssueType issueType) 198 throws Exception 199 { 200 Issue issue = new Issue(module, issueType); 201 return issue; 202 } 203 204 205 208 public static Issue getIssueById(String id) 209 { 210 return IssueManager.getIssueById(id); 211 } 212 213 216 public static Issue getIssueById(Issue.FederatedId fid) 217 { 218 return IssueManager.getIssueByIdImpl(fid); 219 } 220 221 222 223 226 public String getUniqueId() 227 throws TorqueException 228 { 229 if (getIdPrefix() == null) 230 { 231 setIdPrefix(getModule().getCode()); 232 } 233 return getIdPrefix() + getIdCount(); 234 } 235 236 239 public void setUniqueId(String id) 240 { 241 } 242 243 public String getFederatedId() 244 throws TorqueException 245 { 246 if (getIdDomain() != null) 247 { 248 return getIdDomain() + '-' + getUniqueId(); 249 } 250 return getUniqueId(); 251 } 252 253 public void setFederatedId(String id) 254 { 255 FederatedId fid = new FederatedId(id); 256 setIdDomain(fid.getDomain()); 257 setIdPrefix(fid.getPrefix()); 258 setIdCount(fid.getCount()); 259 } 260 261 266 public static class FederatedId 267 implements Serializable 268 { 269 private String domainId; 270 private String prefix; 271 private int count; 272 273 public FederatedId(String id) 274 { 275 int dash = id.indexOf('-'); 276 if (dash > 0) 277 { 278 domainId = id.substring(0, dash); 279 setUniqueId(id.substring(dash+1)); 280 } 281 else 282 { 283 setUniqueId(id); 284 } 285 } 286 287 public FederatedId(String domain, String prefix, int count) 288 { 289 this.domainId = domain; 290 setPrefix(prefix); 291 this.count = count; 292 } 293 294 298 public void setUniqueId(String id) 299 { 300 int codeLength = ScarabGlobalTool.getModuleCodeLength(); 301 StringBuffer code = new StringBuffer (codeLength); 304 int max = id.length() < codeLength ? id.length() : codeLength; 305 for (int i = 0; i < max; i++) 306 { 307 char c = id.charAt(i); 308 if (c < '0' || c > '9') 309 { 310 code.append(c); 311 } 312 } 313 if (code.length() != 0) 314 { 315 setPrefix(code.toString()); 316 } 317 count = Integer.parseInt(id.substring(code.length())); 318 } 319 320 323 public String getDomain() 324 { 325 return domainId; 326 } 327 328 331 public String getPrefix() 332 { 333 return prefix; 334 } 335 336 339 public int getCount() 340 { 341 return count; 342 } 343 344 347 public void setDomain(String domainId) 348 { 349 this.domainId = domainId; 350 } 351 352 355 public void setPrefix(String prefix) 356 { 357 if (prefix != null) 358 { 359 this.prefix = prefix.toUpperCase(); 360 } 361 } 362 363 366 public void setCount(int count) 367 { 368 this.count = count; 369 } 370 371 public boolean equals(Object obj) 372 { 373 boolean b = false; 374 if (obj instanceof FederatedId) 375 { 376 FederatedId fid = (FederatedId)obj; 377 b = fid.count == this.count; 378 b &= ObjectUtils.equals(fid.domainId, domainId); 379 b &= ObjectUtils.equals(fid.prefix, prefix); 380 } 381 return b; 382 } 383 384 public int hashCode() 385 { 386 int hc = count; 387 if (domainId != null) 388 { 389 hc += domainId.hashCode(); 390 } 391 if (prefix != null) 392 { 393 hc += prefix.hashCode(); 394 } 395 return hc; 396 } 397 } 398 399 405 public static List parseIssueList(Module module, String theList) 406 throws Exception 407 { 408 String [] issues = StringUtils.split(theList, ","); 409 List results = new ArrayList (); 410 for (int i = 0; i < issues.length; i++) 411 { 412 if (issues[i].indexOf('*') != -1) 413 { 414 String sql = "SELECT CONCAT(" + IssuePeer.ID_PREFIX + ',' + 418 IssuePeer.ID_COUNT + ") FROM " + IssuePeer.TABLE_NAME + 419 " WHERE " + IssuePeer.ID_PREFIX + " = '" + 420 module.getCode() + '\''; 421 List records = BasePeer.executeQuery(sql); 422 for (Iterator j = records.iterator(); j.hasNext();) 423 { 424 Record rec = (Record)j.next(); 425 results.add(rec.getValue(1).asString()); 426 } 427 } 428 else if (issues[i].indexOf('-') == -1) 430 { 431 FederatedId fid = createFederatedId(module, issues[i]); 434 if (!fid.getPrefix().equalsIgnoreCase(module.getCode())) 435 { 436 String [] args = { fid.getPrefix(), module.getCode() }; 437 throw new Exception (Localization.format 438 (ScarabConstants.DEFAULT_BUNDLE_NAME, 439 module.getLocale(), 440 "IssueIDPrefixNotForModule", args)); } 442 results.add(issues[i]); 443 } 444 else 445 { 446 String [] issue = StringUtils.split(issues[i], "-"); 447 if (issue.length != 2) 448 { 449 throw new Exception (Localization.format 450 (ScarabConstants.DEFAULT_BUNDLE_NAME, 451 module.getLocale(), 452 "IssueIDRangeNotValid", issues[i])); } 454 FederatedId fidStart = createFederatedId(module, issue[0]); 455 FederatedId fidStop = createFederatedId(module, issue[1]); 456 if (!fidStart.getPrefix().equalsIgnoreCase(module.getCode()) || 457 !fidStop.getPrefix().equalsIgnoreCase(module.getCode())) 458 { 459 throw new Exception (Localization.format 460 (ScarabConstants.DEFAULT_BUNDLE_NAME, 461 module.getLocale(), 462 "IssueIDPrefixesNotForModule", 463 module.getCode())); } 465 else if (!fidStart.getPrefix() 466 .equalsIgnoreCase(fidStop.getPrefix())) 467 { 468 String [] args = { fidStart.getPrefix(), 469 fidStop.getPrefix() }; 470 throw new Exception (Localization.format 471 (ScarabConstants.DEFAULT_BUNDLE_NAME, 472 module.getLocale(), 473 "IssueIDPrefixesDoNotMatch", args)); } 475 else if (fidStart.getCount() > fidStop.getCount()) 476 { 477 FederatedId swap = fidStart; 478 fidStart = fidStop; 479 fidStop = swap; 480 } 481 482 for (int j = fidStart.getCount(); j <= fidStop.getCount();j++) 483 { 484 results.add(fidStart.getPrefix() + j); 485 } 486 } 487 } 488 return results; 489 } 490 491 494 private static FederatedId createFederatedId(Module module, String id) 495 throws Exception 496 { 497 FederatedId fid = null; 498 try 499 { 500 fid = new FederatedId(id.trim()); 501 if (fid.getPrefix() == null || fid.getPrefix().length() == 0) 502 { 503 fid.setPrefix(module.getCode()); 504 } 505 } 506 catch (Exception e) 507 { 508 throw new Exception ("Invalid federated id: " + id); } 510 return fid; 511 } 512 513 516 public boolean isTemplate() 517 { 518 boolean isTemplate = false; 519 try 520 { 521 isTemplate = !getIssueType().getParentId().equals(NUMBERKEY_0); 522 } 523 catch (Exception e) 524 { 525 getLog().error("Problem determining whether issue is template"); 526 } 527 return isTemplate; 528 } 529 530 534 public ActivitySet addUrl(Attachment attachment, ScarabUser user) 535 throws Exception 536 { 537 return addUrl(null, attachment, user); 538 } 539 540 543 public ActivitySet addUrl(ActivitySet activitySet, 544 Attachment attachment, ScarabUser user) 545 throws Exception 546 { 547 attachment.setTextFields(user, this, Attachment.URL__PK); 548 attachment.save(); 549 550 String nameFieldString = attachment.getName(); 551 int length = nameFieldString.length() + 12; 553 if (length > 254) 555 { 556 nameFieldString = nameFieldString.substring(0, 238) + "..."; 557 } 558 String desc = Localization.format( 559 ScarabConstants.DEFAULT_BUNDLE_NAME, 560 getLocale(), 561 "UrlAddedDesc", nameFieldString); 562 563 if (activitySet == null) 565 { 566 activitySet = getActivitySet(user, ActivitySetTypePeer.EDIT_ISSUE__PK); 567 activitySet.save(); 568 } 569 ActivityManager 571 .createTextActivity(this, activitySet, desc, attachment); 572 573 return activitySet; 574 } 575 576 private Locale getLocale() 577 throws TorqueException 578 { 579 return getModule().getLocale(); 580 } 581 582 586 public ActivitySet addComment(Attachment attachment, ScarabUser user) 587 throws Exception 588 { 589 return addComment(null, attachment, user); 590 } 591 592 595 public ActivitySet addComment(ActivitySet activitySet, 596 Attachment attachment, ScarabUser user) 597 throws Exception 598 { 599 String comment = attachment.getData(); 600 if (comment == null || comment.length() == 0) 601 { 602 throw new ScarabException(L10NKeySet.NoDataInComment); 603 } 604 if (activitySet == null) 605 { 606 activitySet = getActivitySet(user, 607 ActivitySetTypePeer.EDIT_ISSUE__PK); 608 } 609 activitySet.save(); 610 611 String desc = Localization.getString( 613 ScarabConstants.DEFAULT_BUNDLE_NAME, 614 getLocale(), 615 "AddedCommentToIssue"); 616 int total = 248 - desc.length(); 617 if (comment.length() > total) 618 { 619 comment = comment.substring(0,total) + "..."; 620 } 621 comment = desc + " '" + comment + "'"; 622 623 attachment = AttachmentManager 625 .getComment(attachment, this, user); 626 627 ActivityManager 628 .createTextActivity(this, activitySet, 629 comment, attachment); 630 631 try 632 { 633 activitySet.sendEmail(this); 634 } 635 catch (Exception e) 636 { Localizable commentSaved = new L10NMessage(L10NKeySet.CommentSaved); 637 Localizable sendFailed = new L10NMessage(L10NKeySet.CouldNotSendEmail,e); 638 throw new ScarabException(L10NKeySet.SavedButErrors, 639 commentSaved, 640 sendFailed); 641 } 642 643 return activitySet; 644 } 645 646 652 public synchronized void addFile(Attachment attachment, 653 ScarabUser user) 654 throws Exception 655 { 656 attachment.setTypeId(Attachment.FILE__PK); 657 attachment.setCreatedBy(user.getUserId()); 658 if (unSavedAttachments == null) 659 { 660 unSavedAttachments = new ArrayList (); 661 } 662 unSavedAttachments.add(attachment); 663 } 664 665 669 public synchronized List getAttachments() 670 throws TorqueException 671 { 672 if (unSavedAttachments != null && 673 unSavedAttachments.size() > 0) 674 { 675 return unSavedAttachments; 676 } 677 else 678 { 679 return super.getAttachments(); 680 } 681 } 682 683 689 public synchronized ActivitySet doSaveFileAttachments(ScarabUser user) 690 throws Exception 691 { 692 return doSaveFileAttachments(null, user); 693 } 694 695 701 public synchronized ActivitySet doSaveFileAttachments(ActivitySet activitySet, 702 ScarabUser user) 703 throws Exception 704 { 705 if (unSavedAttachments == null) 706 { 707 return activitySet; 708 } 709 if (activitySet == null) 710 { 711 activitySet = getActivitySet(user, ActivitySetTypePeer.EDIT_ISSUE__PK); 713 activitySet.save(); 714 } 715 Iterator itr = unSavedAttachments.iterator(); 716 while (itr.hasNext()) 717 { 718 Attachment attachment = (Attachment)itr.next(); 719 attachment.setIssue(this); 721 attachment.save(); 722 723 String name = attachment.getFileName(); 725 Object [] args = {name}; 726 String description = Localization.format( 727 ScarabConstants.DEFAULT_BUNDLE_NAME, 728 getLocale(), 729 "FileAddedDesc", args); 730 731 ActivityManager 733 .createTextActivity(this, activitySet, description, attachment); 734 735 736 } 737 super.collAttachments = null; 740 this.unSavedAttachments = null; 742 return activitySet; 743 } 744 745 750 public void removeFile(String index) 751 throws Exception 752 { 753 int indexInt = Integer.parseInt(index) - 1; 754 if (indexInt >= 0) 755 { 756 if (unSavedAttachments != null && unSavedAttachments.size() > 0) 757 { 758 unSavedAttachments.remove(indexInt); 759 } 760 else 761 { 762 List attachList = getAttachments(); 763 if (attachList != null && attachList.size() > 0) 764 { 765 attachList.remove(indexInt); 766 } 767 } 768 } 769 } 770 771 777 public ScarabModule getScarabModule() 778 { 779 throw new UnsupportedOperationException ( 780 "Should use getModule"); } 782 783 788 public void setScarabModule(ScarabModule module) 789 { 790 throw new UnsupportedOperationException ( 791 "Should use setModule(Module). Note module cannot be new."); } 793 794 797 public void setModule(Module me) 798 throws TorqueException 799 { 800 Integer id = me.getModuleId(); 801 if (id == null) 802 { 803 throw new TorqueException("Modules must be saved prior to " + 804 "being associated with other objects."); } 806 setModuleId(id); 807 } 808 809 814 public Module getModule() 815 throws TorqueException 816 { 817 Module module = null; 818 Integer id = getModuleId(); 819 if ( id != null ) 820 { 821 module = ModuleManager.getInstance(id); 822 } 823 824 return module; 825 } 826 827 833 public RModuleIssueType getRModuleIssueType() 834 throws Exception 835 { 836 RModuleIssueType rmit = null; 837 Module module = getModule(); 838 IssueType issueType = getIssueType(); 839 if (module != null && issueType != null) 840 { 841 rmit = module.getRModuleIssueType(issueType); 842 } 843 return rmit; 844 } 845 846 851 public LinkedMap getModuleAttributeValuesMap() 852 throws Exception 853 { 854 return getModuleAttributeValuesMap(true); 855 } 856 857 866 public LinkedMap getModuleAttributeValuesMap(boolean isActive) 867 throws Exception 868 { 869 LinkedMap result = null; 870 Object obj = getCachedObject(GET_MODULE_ATTRVALUES_MAP, isActive ? Boolean.TRUE : Boolean.FALSE); 871 if (obj == null) 872 { 873 List attributes = null; 874 if (isActive) 875 { 876 attributes = getIssueType().getActiveAttributes(getModule()); 877 } 878 else 879 { 880 attributes = getModule().getAttributes(getIssueType()); 881 } 882 Map siaValuesMap = getAttributeValuesMap(); 883 result = new LinkedMap((int)(1.25*attributes.size() + 1)); 884 for (int i=0; i<attributes.size(); i++) 885 { 886 String key = ((Attribute)attributes.get(i)).getName().toUpperCase(); 887 if (siaValuesMap.containsKey(key)) 888 { 889 result.put(key, siaValuesMap.get(key)); 890 } 891 else 892 { 893 AttributeValue aval = AttributeValue 894 .getNewInstance(((Attribute)attributes.get(i)), this); 895 addAttributeValue(aval); 896 siaValuesMap.put( 897 aval.getAttribute().getName().toUpperCase(), aval); 898 result.put(key, aval); 899 } 900 } 901 putCachedObject(result, GET_MODULE_ATTRVALUES_MAP, isActive ? Boolean.TRUE : Boolean.FALSE); 902 } 903 else 904 { 905 result = (LinkedMap)obj; 906 } 907 return result; 908 } 909 910 public void addAttributeValue(AttributeValue aval) 911 throws TorqueException 912 { 913 List avals = getAttributeValues(); 914 if (!avals.contains(aval)) 915 { 916 super.addAttributeValue(aval); 917 } 918 } 919 920 928 public String getEmailShortInfo() 929 throws Exception 930 { 931 String result = ""; 932 933 String key = "status_attribute_"+this.getTypeId(); 934 935 String statusId = GlobalParameterManager.getString(key,this.getModule()); 936 if(!statusId.equals("")) 937 { 938 int sid = Integer.parseInt(statusId); 939 AttributeValue av = getAttributeValue(sid); 940 if(av != null) 941 { 942 result=av.getValue(); 943 } 944 } 945 return result; 946 } 947 948 public AttributeValue getAttributeValue(String attributeName) 949 throws Exception 950 { 951 Attribute attribute = Attribute.getInstance(attributeName); 952 return getAttributeValue(attribute); 953 } 954 955 public AttributeValue getAttributeValue(int id) 956 throws Exception 957 { 958 Attribute attribute = Attribute.getInstance(id); 959 return getAttributeValue(attribute); 960 } 961 962 public AttributeValue getAttributeValue(Attribute attribute) 963 throws Exception 964 { 965 AttributeValue result = null; 966 Object obj = ScarabCache.get(this, GET_ATTRVALUE, attribute); 967 if (obj == null) 968 { 969 if (isNew()) 970 { 971 List avals = getAttributeValues(); 972 if (avals != null) 973 { 974 Iterator i = avals.iterator(); 975 while (i.hasNext()) 976 { 977 AttributeValue tempAval = (AttributeValue)i.next(); 978 if (tempAval.getAttribute().equals(attribute)) 979 { 980 result = tempAval; 981 break; 982 } 983 } 984 } 985 } 986 else 987 { 988 Criteria crit = new Criteria(2) 989 .add(AttributeValuePeer.ISSUE_ID, getIssueId()) 990 .add(AttributeValuePeer.DELETED, false) 991 .add(AttributeValuePeer.ATTRIBUTE_ID, 992 attribute.getAttributeId()); 993 994 List avals = getAttributeValues(crit); 995 if (avals.size() > 0) 996 { 997 result = (AttributeValue)avals.get(0); 998 } 999 if (avals.size() > 1) 1000 { 1001 getLog().error("getAttributeValue(): Error when retrieving attribute values of attribute. Expected 1 and found " + avals.size() + ". List follows: " + avals); 1002 } 1003 } 1004 ScarabCache.put(result, this, GET_ATTRVALUE, attribute); 1005 } 1006 else 1007 { 1008 result = (AttributeValue)obj; 1009 } 1010 return result; 1011 } 1012 1013 1016 public List getAttributeValues(Attribute attribute) 1017 throws Exception 1018 { 1019 List result = null; 1020 Object obj = ScarabCache.get(this, GET_ATTRVALUES, attribute); 1021 if (obj == null) 1022 { 1023 if (isNew()) 1024 { 1025 List avals = getAttributeValues(); 1026 result = new ArrayList (); 1027 if (avals != null) 1028 { 1029 Iterator i = avals.iterator(); 1030 while (i.hasNext()) 1031 { 1032 AttributeValue tempAval = (AttributeValue)i.next(); 1033 if (tempAval.getAttribute().equals(attribute)) 1034 { 1035 result.add(tempAval); 1036 } 1037 } 1038 } 1039 } 1040 else 1041 { 1042 Criteria crit = new Criteria(2) 1043 .add(AttributeValuePeer.DELETED, false) 1044 .add(AttributeValuePeer.ATTRIBUTE_ID, 1045 attribute.getAttributeId()); 1046 1047 result = getAttributeValues(crit); 1048 ScarabCache.put(result, this, GET_ATTRVALUES, attribute); 1049 } 1050 } 1051 else 1052 { 1053 result = (List )obj; 1054 } 1055 return result; 1056 } 1057 1058 public boolean isAttributeValue(AttributeValue attVal) 1059 throws Exception 1060 { 1061 boolean isValue = false; 1062 List attValues = getAttributeValues(attVal.getAttribute()); 1063 if (attValues.contains(attVal)) 1064 { 1065 isValue = true; 1066 } 1067 return isValue; 1068 } 1069 1070 1080 private AttributeValue getAttributeValueWithValue(Attribute att, String strVal, Integer numVal) 1081 throws Exception 1082 { 1083 AttributeValue val = null; 1084 boolean bFound = false; 1085 List attValues = getAttributeValues(att); 1086 for (Iterator it = attValues.iterator(); !bFound && it.hasNext(); ) 1087 { 1088 val = (AttributeValue)it.next(); 1089 if (strVal != null) 1090 bFound = val.getValue().equals(strVal); 1091 else if (!bFound && numVal != null) 1092 bFound = val.getNumericValue().equals(numVal); 1093 } 1094 return val; 1095 } 1096 1097 1098 1101 public Map getAttributeValuesMap() throws Exception 1102 { 1103 Map result = null; 1104 Object obj = ScarabCache.get(this, GET_ATTRIBUTE_VALUES_MAP); 1105 if (obj == null) 1106 { 1107 Criteria crit = new Criteria(2) 1108 .add(AttributeValuePeer.DELETED, false); 1109 List siaValues = getAttributeValues(crit); 1110 result = new HashMap ((int)(1.25*siaValues.size() + 1)); 1111 for (Iterator i = siaValues.iterator(); i.hasNext(); ) 1112 { 1113 AttributeValue att = (AttributeValue) i.next(); 1114 result.put(att.getAttribute().getName().toUpperCase(), att); 1115 } 1116 1117 ScarabCache.put(result, this, GET_ATTRIBUTE_VALUES_MAP); 1118 } 1119 else 1120 { 1121 result = (Map )obj; 1122 } 1123 return result; 1124 } 1125 1126 1131 public Map getAllAttributeValuesMap() 1132 throws Exception 1133 { 1134 Map moduleAtts = getModuleAttributeValuesMap(); 1135 Map issueAtts = getAttributeValuesMap(); 1136 Map allValuesMap = new HashMap ((int)(1.25*(moduleAtts.size() + 1137 issueAtts.size())+1)); 1138 1139 allValuesMap.putAll(moduleAtts); 1140 allValuesMap.putAll(issueAtts); 1141 return allValuesMap; 1142 } 1143 1144 1150 public boolean containsMinimumAttributeValues() 1151 throws Exception 1152 { 1153 List attributes = getIssueType() 1154 .getRequiredAttributes(getModule()); 1155 1156 boolean result = true; 1157 LinkedMap avMap = getModuleAttributeValuesMap(); 1158 MapIterator i = avMap.mapIterator(); 1159 while (i.hasNext()) 1160 { 1161 AttributeValue aval = (AttributeValue)avMap.get(i.next()); 1162 1163 if (aval.getOptionId() == null && aval.getValue() == null) 1164 { 1165 for (int j=attributes.size()-1; j>=0; j--) 1166 { 1167 if (aval.getAttribute().getPrimaryKey().equals( 1168 ((Attribute)attributes.get(j)).getPrimaryKey())) 1169 { 1170 result = false; 1171 break; 1172 } 1173 } 1174 if (!result) 1175 { 1176 break; 1177 } 1178 } 1179 } 1180 return result; 1181 } 1182 1183 1191 public List getEligibleUsers(Attribute attribute) 1192 throws Exception 1193 { 1194 ScarabUser[] users = getModule().getEligibleUsers(attribute); 1195 List assigneeAVs = getAttributeValues(attribute); 1197 if (users != null && assigneeAVs != null) 1198 { 1199 for (int i=users.length-1; i>=0; i--) 1200 { 1201 for (int j=assigneeAVs.size()-1; j>=0; j--) 1202 { 1203 AttributeValue av = (AttributeValue)assigneeAVs.get(j); 1204 Integer avUserId = av.getUserId(); 1205 Integer userUserId = users[i].getUserId(); 1206 if ( av != null && avUserId != null && 1207 userUserId != null && 1208 avUserId.equals(userUserId)) 1209 { 1210 users[i] = null; 1211 break; 1212 } 1213 } 1214 } 1215 } 1216 1217 List eligibleUsers = new ArrayList (users.length); 1218 for (int i=0; i<users.length; i++) 1219 { 1220 if (users[i] != null) 1221 { 1222 eligibleUsers.add(users[i]); 1223 } 1224 } 1225 1226 return eligibleUsers; 1227 } 1228 1229 1239 protected Set getUsersToEmail(String action, Issue issue, Set users) 1240 throws Exception 1241 { 1242 if (users == null) 1243 { 1244 users = new HashSet (1); 1245 } 1246 1247 Module module = getModule(); 1248 1249 ScarabUser createdBy = issue.getCreatedBy(); 1250 if (createdBy != null && !users.contains(createdBy) && 1251 AttributePeer.EMAIL_TO.equals(action) && 1252 createdBy.hasPermission(ScarabSecurity.ISSUE__ENTER, module)) 1253 { 1254 users.add(createdBy); 1255 } 1256 1257 Criteria crit = new Criteria() 1258 .add(AttributeValuePeer.ISSUE_ID, issue.getIssueId()) 1259 .addJoin(AttributeValuePeer.ATTRIBUTE_ID, 1260 AttributePeer.ATTRIBUTE_ID) 1261 .add(AttributePeer.ACTION, action) 1262 .add(RModuleAttributePeer.MODULE_ID, getModuleId()) 1263 .add(RModuleAttributePeer.ISSUE_TYPE_ID, getTypeId()) 1264 .add(AttributeValuePeer.DELETED, 0) 1265 .add(RModuleAttributePeer.ACTIVE, true) 1266 .addJoin(RModuleAttributePeer.ATTRIBUTE_ID, 1267 AttributeValuePeer.ATTRIBUTE_ID); 1268 List userAttVals = AttributeValuePeer.doSelect(crit); 1269 for (Iterator i = userAttVals.iterator(); i.hasNext(); ) 1270 { 1271 AttributeValue attVal = (AttributeValue) i.next(); 1272 try 1273 { 1274 ScarabUser su = ScarabUserManager 1275 .getInstance(attVal.getUserId()); 1276 if (!users.contains(su) 1277 && su.hasPermission(attVal.getAttribute().getPermission(), 1278 module)) 1279 { 1280 users.add(su); 1281 } 1282 } 1283 catch (Exception e) 1284 { 1285 throw new Exception ("Error retrieving users to email"); } 1287 } 1288 return users; 1289 } 1290 1291 1298 public Set getAllUsersToEmail(String action) throws Exception 1299 { 1300 Set result = null; 1301 Object obj = ScarabCache.get(this, GET_ALL_USERS_TO_EMAIL, action); 1302 if (obj == null) 1303 { 1304 Set users = new HashSet (); 1305 try 1306 { 1307 users = getUsersToEmail(action, this, users); 1308 List children = getChildren(); 1309 for (int i=0;i<children.size();i++) 1310 { 1311 Issue depIssue = IssueManager.getInstance 1312 (((Depend) children.get(i)).getObserverId()); 1313 users = getUsersToEmail(action, depIssue, users); 1314 } 1315 result = users; 1316 } 1317 catch (Exception e) 1318 { 1319 getLog().error("Issue.getUsersToEmail(): ", e); 1320 throw new Exception ("Error in retrieving users."); } 1322 ScarabCache.put(result, this, GET_ALL_USERS_TO_EMAIL, action); 1323 } 1324 else 1325 { 1326 result = (Set )obj; 1327 } 1328 return result; 1329 } 1330 1331 1334 public AttributeValue getUserAttributeValue(ScarabUser user, Attribute attribute) 1335 throws Exception 1336 { 1337 AttributeValue result = null; 1338 Object obj = getCachedObject(GET_USER_ATTRIBUTEVALUE, 1339 attribute.getAttributeId(), user.getUserId()); 1340 if (obj == null) 1341 { 1342 Criteria crit = new Criteria() 1343 .add(AttributeValuePeer.ATTRIBUTE_ID, attribute.getAttributeId()) 1344 .add(AttributeValuePeer.ISSUE_ID, getIssueId()) 1345 .add(AttributeValuePeer.USER_ID, user.getUserId()) 1346 .add(AttributeValuePeer.DELETED, 0); 1347 List resultList = AttributeValuePeer.doSelect(crit); 1348 if (resultList != null && resultList.size() == 1) 1349 { 1350 result = (AttributeValue)resultList.get(0); 1351 } 1352 putCachedObject(result, GET_USER_ATTRIBUTEVALUE, 1353 attribute.getAttributeId(), user.getUserId()); 1354 } 1355 else 1356 { 1357 result = (AttributeValue)obj; 1358 } 1359 return result; 1360 } 1361 1362 1365 public List getUserAttributeValues() throws Exception 1366 { 1367 List result = null; 1368 Object obj = getCachedObject(GET_USER_ATTRIBUTEVALUES); 1369 if (obj == null) 1370 { 1371 List attributeList = getModule().getUserAttributes(getIssueType(), true); 1372 List attributeIdList = new ArrayList (); 1373 1374 for (int i=0; i<attributeList.size(); i++) 1375 { 1376 Attribute att = (Attribute) attributeList.get(i); 1377 attributeIdList.add(att.getAttributeId()); 1378 } 1379 1380 if(!attributeIdList.isEmpty()) 1381 { 1382 Criteria crit = new Criteria() 1383 .addIn(AttributeValuePeer.ATTRIBUTE_ID, attributeIdList) 1384 .add(AttributeValuePeer.ISSUE_ID, getIssueId()) 1385 .add(AttributeValuePeer.DELETED, 0); 1386 result = AttributeValuePeer.doSelect(crit); 1387 } 1388 else 1389 { 1390 result = new ArrayList (0); 1391 } 1392 putCachedObject(result, GET_USER_ATTRIBUTEVALUES); 1393 } 1394 else 1395 { 1396 result = (List )obj; 1397 } 1398 return result; 1399 } 1400 1401 1402 1408 public ActivitySet getInitialActivitySet() 1409 throws Exception 1410 { 1411 ActivitySet activitySet = getActivitySet(); 1412 if (activitySet == null) 1413 { 1414 Log.get().warn("Creation ActivitySet is null for " + this); 1415 } 1416 1417 return activitySet; 1418 } 1419 1420 1426 public Date getCreatedDate() 1427 throws TorqueException 1428 { 1429 ActivitySet creationSet = getActivitySet(); 1430 Date result = null; 1431 if (creationSet == null) 1432 { 1433 getLog().warn("Issue " + getUniqueId() + " (pk=" + getIssueId() + 1434 ") does not have a creation ActivitySet"); 1435 } 1436 else 1437 { 1438 result = creationSet.getCreatedDate(); 1439 } 1440 return result; 1441 } 1442 1443 1447 public ScarabUser getCreatedBy() 1448 throws TorqueException 1449 { 1450 ActivitySet creationSet = getActivitySet(); 1451 ScarabUser result = null; 1452 if (creationSet == null) 1453 { 1454 getLog().warn("Issue " + getUniqueId() + " (pk=" + getIssueId() + 1455 ") does not have a creation ActivitySet"); 1456 } 1457 else 1458 { 1459 result = creationSet.getScarabUser(); 1460 } 1461 return result; 1462 } 1463 1464 public boolean isCreatingUser(ScarabUser user) 1465 throws Exception 1466 { 1467 ActivitySet creationSet = getActivitySet(); 1468 boolean result = false; 1469 if (creationSet == null) 1470 { 1471 getLog().warn("Issue " + getUniqueId() + " (pk=" + getIssueId() + 1472 ") does not have a creation ActivitySet"); 1473 } 1474 else 1475 { 1476 result = creationSet.getCreatedBy().equals(user.getUserId()); 1477 } 1478 return result; 1479 } 1480 1481 1486 public ActivitySet getLastActivitySet() 1487 throws Exception 1488 { 1489 ActivitySet t = null; 1490 if (!isNew()) 1491 { 1492 Object obj = ScarabCache.get(this, GET_LAST_TRANSACTION); 1493 if (obj == null) 1494 { 1495 Criteria crit = new Criteria(); 1496 crit.addJoin(ActivitySetPeer.TRANSACTION_ID, 1497 ActivityPeer.TRANSACTION_ID); 1498 crit.add(ActivityPeer.ISSUE_ID, getIssueId()); 1499 Integer [] typeIds = {ActivitySetTypePeer.EDIT_ISSUE__PK, 1500 ActivitySetTypePeer.MOVE_ISSUE__PK}; 1501 crit.addIn(ActivitySetPeer.TYPE_ID, typeIds); 1502 crit.setDistinct(); 1505 crit.addDescendingOrderByColumn(ActivitySetPeer.CREATED_DATE); 1506 List activitySets = ActivitySetPeer.doSelect(crit); 1507 if (activitySets.size() > 0) 1508 { 1509 t = (ActivitySet)activitySets.get(0); 1510 } 1511 ScarabCache.put(t, this, GET_LAST_TRANSACTION); 1512 } 1513 else 1514 { 1515 t = (ActivitySet)obj; 1516 } 1517 } 1518 return t; 1519 } 1520 1521 1526 public Date getModifiedDate() 1527 throws Exception 1528 { 1529 Date result = null; 1530 if (!isNew()) 1531 { 1532 ActivitySet t = getLastActivitySet(); 1533 if (t == null) 1534 { 1535 result = getCreatedDate(); 1536 } 1537 else 1538 { 1539 result = t.getCreatedDate(); 1540 } 1541 } 1542 return result; 1543 } 1544 1545 1550 public ScarabUser getModifiedBy() 1551 throws Exception 1552 { 1553 ScarabUser result = null; 1554 if (!isNew()) 1555 { 1556 ActivitySet t = getLastActivitySet(); 1557 if (t == null) 1558 { 1559 result = getCreatedBy(); 1560 } 1561 else 1562 { 1563 result = ScarabUserManager 1564 .getInstance(t.getCreatedBy()); 1565 } 1566 } 1567 return result; 1568 } 1569 1570 1571 1574 public int getCommentsCount() throws Exception 1575 { 1576 return getComments(true).size(); 1577 } 1578 1579 1583 public boolean isCommentsLong() throws Exception 1584 { 1585 return (getCommentsCount() > getCommentsLimit()); 1586 } 1587 1588 1591 public int getCommentsLimit() throws Exception 1592 { 1593 int limit=0; 1594 try 1595 { 1596 limit = getModule().getRModuleIssueType(getIssueType()) 1597 .getComments(); 1598 } 1599 catch (Exception e) 1600 { 1601 } 1603 return limit; 1604 } 1605 1606 1610 public List getComments(boolean full) throws Exception 1611 { 1612 List result = null; 1613 Boolean fullBool = (full ? Boolean.TRUE : Boolean.FALSE); 1614 Object obj = getCachedObject(GET_COMMENTS, fullBool); 1615 if (obj == null) 1616 { 1617 Criteria crit = new Criteria() 1618 .add(AttachmentPeer.ISSUE_ID, getIssueId()) 1619 .addJoin(AttachmentTypePeer.ATTACHMENT_TYPE_ID, 1620 AttachmentPeer.ATTACHMENT_TYPE_ID) 1621 .add(AttachmentTypePeer.ATTACHMENT_TYPE_ID, 1622 Attachment.COMMENT__PK) 1623 .addDescendingOrderByColumn(AttachmentPeer.CREATED_DATE); 1624 if (!full) 1625 { 1626 crit.setLimit(getCommentsLimit()); 1627 } 1628 result = AttachmentPeer.doSelect(crit); 1629 putCachedObject(result, GET_COMMENTS, fullBool); 1630 } 1631 else 1632 { 1633 result = (List )obj; 1634 } 1635 return result; 1636 } 1637 1638 1639 1643 public List getUrls() throws Exception 1644 { 1645 List result = null; 1646 Object obj = getCachedObject(GET_URLS); 1647 if (obj == null) 1648 { 1649 Criteria crit = new Criteria() 1650 .add(AttachmentPeer.ISSUE_ID, getIssueId()) 1651 .addJoin(AttachmentTypePeer.ATTACHMENT_TYPE_ID, 1652 AttachmentPeer.ATTACHMENT_TYPE_ID) 1653 .add(AttachmentTypePeer.ATTACHMENT_TYPE_ID, 1654 Attachment.URL__PK) 1655 .add(AttachmentPeer.DELETED, 0); 1656 result = AttachmentPeer.doSelect(crit); 1657 putCachedObject(result, GET_URLS); 1658 } 1659 else 1660 { 1661 result = (List )obj; 1662 } 1663 return result; 1664 } 1665 1666 1667 1670 public List getExistingAttachments() throws Exception 1671 { 1672 List result = null; 1673 Object obj = getCachedObject(GET_EXISTING_ATTACHMENTS); 1674 if (obj == null) 1675 { 1676 Criteria crit = new Criteria() 1677 .add(AttachmentPeer.ISSUE_ID, getIssueId()) 1678 .addJoin(AttachmentTypePeer.ATTACHMENT_TYPE_ID, 1679 AttachmentPeer.ATTACHMENT_TYPE_ID) 1680 .add(AttachmentTypePeer.ATTACHMENT_TYPE_ID, 1681 Attachment.FILE__PK) 1682 .add(AttachmentPeer.DELETED, 0); 1683 result = AttachmentPeer.doSelect(crit); 1684 putCachedObject(result, GET_EXISTING_ATTACHMENTS); 1685 } 1686 else 1687 { 1688 result = (List )obj; 1689 } 1690 return result; 1691 } 1692 1693 public List getActivitiesWithNullEndDate(Attribute attribute) 1694 throws TorqueException 1695 { 1696 List result = null; 1697 Object obj = ScarabCache.get(this, GET_NULL_END_DATE, attribute); 1698 if (obj == null) 1699 { 1700 Criteria crit = new Criteria(); 1701 crit.add(ActivityPeer.ISSUE_ID, this.getIssueId()); 1702 crit.add(ActivityPeer.ATTRIBUTE_ID, attribute.getAttributeId()); 1703 crit.add(ActivityPeer.END_DATE, null); 1704 result = ActivityPeer.doSelect(crit); 1705 ScarabCache.put(result, this, GET_NULL_END_DATE, attribute); 1706 } 1707 else 1708 { 1709 result = (List )obj; 1710 } 1711 return result; 1712 } 1713 1714 1718 public int getHistoryLimit() throws Exception 1719 { 1720 RModuleIssueType rmit = getModule().getRModuleIssueType(getIssueType()); 1721 if (rmit != null) 1722 { 1723 return rmit.getHistory(); 1724 } 1725 else 1726 { 1727 return 5; 1728 } 1729 } 1730 1731 1735 public boolean isHistoryLong() throws Exception 1736 { 1737 return isHistoryLong(getHistoryLimit()); 1738 } 1739 1740 1744 public boolean isHistoryLong(int limit) throws Exception 1745 { 1746 return (getActivity(true).size() > limit); 1747 } 1748 1749 1752 public List getActivity() throws Exception 1753 { 1754 return getActivity(false, getHistoryLimit()); 1755 } 1756 1757 1760 public List getActivity(int limit) throws Exception 1761 { 1762 return getActivity(false, limit); 1763 } 1764 1765 1770 public List getActivity(boolean fullHistory) throws Exception 1771 { 1772 return getActivity(fullHistory, getHistoryLimit()); 1773 } 1774 1775 1778 private List getActivity(boolean fullHistory, int limit) throws Exception 1779 { 1780 List result = null; 1781 Boolean fullHistoryObj = fullHistory ? Boolean.TRUE : Boolean.FALSE; 1782 Object obj = getCachedObject(GET_ACTIVITY, fullHistoryObj, 1783 new Integer (limit)); 1784 if (obj == null) 1785 { 1786 Criteria crit = new Criteria() 1787 .add(ActivityPeer.ISSUE_ID, getIssueId()) 1788 .addAscendingOrderByColumn(ActivityPeer.TRANSACTION_ID); 1789 if (!fullHistory) 1790 { 1791 crit.setLimit(limit); 1792 } 1793 result = ActivityPeer.doSelect(crit); 1794 putCachedObject(result, GET_ACTIVITY, 1795 fullHistoryObj, new Integer (limit)); 1796 } 1797 else 1798 { 1799 result = (List )obj; 1800 } 1801 return result; 1802 } 1803 1804 1807 public void addActivity(Activity activity) throws TorqueException 1808 { 1809 List activityList = null; 1810 try 1811 { 1812 activityList = getActivity(true); 1813 } 1814 catch (Exception e) 1815 { 1816 throw new TorqueException(e); } 1818 super.addActivity(activity); 1819 if (!activityList.contains(activity)) 1820 { 1821 activityList.add(activity); 1822 } 1823 } 1824 1825 1828 public List getActivitySets() 1829 throws Exception 1830 { 1831 List result = null; 1832 Object obj = ScarabCache.get(this, GET_TRANSACTIONS); 1833 if (obj == null) 1834 { 1835 Criteria crit = new Criteria(); 1836 crit.add(ActivityPeer.ISSUE_ID, getIssueId()); 1837 crit.addJoin(ActivitySetPeer.TRANSACTION_ID, ActivityPeer.TRANSACTION_ID); 1838 crit.setDistinct(); 1839 result = ActivitySetPeer.doSelect(crit); 1840 ScarabCache.put(result, this, GET_TRANSACTIONS); 1841 } 1842 else 1843 { 1844 result = (List )obj; 1845 } 1846 return result; 1847 } 1848 1849 1852 public ActivitySet getActivitySet(ScarabUser user, Attachment attachment, 1853 Integer type) 1854 throws Exception 1855 { 1856 ActivitySet activitySet = null; 1857 if (attachment == null) 1858 { 1859 activitySet = ActivitySetManager 1860 .getInstance(type, user); 1861 } 1862 else 1863 { 1864 activitySet = ActivitySetManager 1865 .getInstance(type, user, attachment); 1866 } 1867 return activitySet; 1868 } 1869 1870 1873 public ActivitySet getActivitySet(ScarabUser user, Integer type) 1874 throws Exception 1875 { 1876 return getActivitySet(user, null, type); 1877 } 1878 1879 1882 public List getAllDependencies() 1883 throws Exception 1884 { 1885 List dependencies = new ArrayList (); 1886 dependencies.addAll(getChildren()); 1887 dependencies.addAll(getParents()); 1888 return dependencies; 1889 } 1890 1891 1895 public List getChildren() throws Exception 1896 { 1897 return getChildren(true); 1898 } 1899 1900 1904 public List getChildren(boolean hideDeleted) throws Exception 1905 { 1906 List result = null; 1907 Boolean hide = hideDeleted ? Boolean.TRUE : Boolean.FALSE; 1908 Object obj = getCachedObject(GET_CHILDREN, hide); 1909 if (obj == null) 1910 { 1911 Criteria crit = new Criteria() 1912 .add(DependPeer.OBSERVED_ID, getIssueId()); 1913 if (hideDeleted) 1914 { 1915 crit.add(DependPeer.DELETED, false); 1916 } 1917 result = DependPeer.doSelect(crit); 1918 putCachedObject(result, GET_CHILDREN, hide); 1919 } 1920 else 1921 { 1922 result = (List )obj; 1923 } 1924 return result; 1925 } 1926 1927 1931 public List getParents() throws Exception 1932 { 1933 return getParents(true); 1934 } 1935 1936 1940 public List getParents(boolean hideDeleted) throws Exception 1941 { 1942 List result = null; 1943 Boolean hide = hideDeleted ? Boolean.TRUE : Boolean.FALSE; 1944 Object obj = getCachedObject(GET_PARENTS, hide); 1945 if (obj == null) 1946 { 1947 Criteria crit = new Criteria() 1948 .add(DependPeer.OBSERVER_ID, getIssueId()); 1949 if (hideDeleted) 1950 { 1951 crit.add(DependPeer.DELETED, false); 1952 } 1953 result = DependPeer.doSelect(crit); 1954 putCachedObject(result, GET_PARENTS, hide); 1955 } 1956 else 1957 { 1958 result = (List )obj; 1959 } 1960 return result; 1961 } 1962 1963 1964 1969 public List getAllDependencyTypes() throws Exception 1970 { 1971 return DependTypeManager.getAll(); 1972 } 1973 1974 public ActivitySet doAddDependency(ActivitySet activitySet, Depend depend, 1975 Issue childIssue, ScarabUser user) 1976 throws Exception 1977 { 1978 Depend prevDepend = this.getDependency(childIssue, true); 1982 if (prevDepend != null) 1983 { 1984 throw new ScarabException(L10NKeySet.DependencyExists); 1985 } 1986 1987 depend.setNew(true); 1989 depend.setDeleted(false); 1990 depend.save(); 1991 1992 if (activitySet == null) 1993 { 1994 Attachment comment = depend.getDescriptionAsAttachment(user, this); 1996 activitySet = getActivitySet(user, comment, 1998 ActivitySetTypePeer.EDIT_ISSUE__PK); 1999 activitySet.save(); 2000 } 2001 2002 Object [] args = { 2003 this.getUniqueId(), 2004 depend.getAction(), 2005 childIssue.getUniqueId() 2006 }; 2007 2008 String desc = Localization.format( 2009 ScarabConstants.DEFAULT_BUNDLE_NAME, 2010 getLocale(), 2011 "AddDependency", args); 2012 2013 ActivityManager 2015 .createAddDependencyActivity(this, activitySet, depend, desc); 2016 2017 ActivityManager 2019 .createAddDependencyActivity(childIssue, activitySet, depend, desc); 2020 2021 return activitySet; 2022 } 2023 2027 public Depend getDependency(Issue potentialDependency) throws Exception 2028 { 2029 return getDependency(potentialDependency, true); 2030 } 2031 2032 2041 public Depend getDependency(Issue potentialDependency, boolean hideDeleted) throws Exception 2042 { 2043 Depend result = null; 2044 Object obj = ScarabCache.get(this, GET_DEPENDENCY, potentialDependency); 2045 if (obj == null) 2046 { 2047 2048 Criteria crit = new Criteria(2) 2050 .add(DependPeer.OBSERVED_ID, getIssueId()) 2051 .add(DependPeer.OBSERVER_ID, potentialDependency.getIssueId()); 2052 if (hideDeleted) 2053 { 2054 crit.add(DependPeer.DELETED, false); 2055 } 2056 2057 List childIssues = DependPeer.doSelect(crit); 2058 if (!childIssues.isEmpty()) 2061 { 2062 result = (Depend)childIssues.get(0); 2063 } 2064 else 2065 { 2066 Criteria crit2 = new Criteria(2) 2068 .add(DependPeer.OBSERVER_ID, getIssueId()) 2069 .add(DependPeer.OBSERVED_ID, potentialDependency.getIssueId()); 2070 if (hideDeleted) 2071 { 2072 crit2.add(DependPeer.DELETED, false); 2073 } 2074 List parentIssues = DependPeer.doSelect(crit2); 2075 if (!parentIssues.isEmpty()) 2076 { 2077 result = (Depend)parentIssues.get(0); 2078 } 2079 } 2080 2081 if (result != null) 2082 { 2083 ScarabCache.put(result, this, GET_DEPENDENCY, potentialDependency); 2084 } 2085 } 2086 else 2087 { 2088 result = (Depend)obj; 2089 } 2090 return result; 2091 } 2092 2093 2100 public void save(Connection dbCon) 2101 throws TorqueException 2102 { 2103 Module module = getModule(); 2104 if (!module.allowsIssues() || (isNew() && !module.allowsNewIssues())) 2105 { 2106 throw new UnsupportedOperationException (module.getName() + 2107 " does not allow issues."); } 2109 2110 List attValues = getAttributeValues(); 2112 for (int i=attValues.size()-1; i>=0; i--) 2114 { 2115 AttributeValue attVal = (AttributeValue) attValues.get(i); 2116 if (!attVal.isSet()) 2117 { 2118 attValues.remove(i); 2119 } 2120 } 2121 2122 if (isNew()) 2123 { 2124 setIdDomain(module.getScarabInstanceId()); 2126 setIdPrefix(module.getCode()); 2127 2128 if (isTemplate()) 2131 { 2132 setIdCount(-1); 2133 } 2134 else 2135 { 2136 try 2137 { 2138 setIdCount(getNextIssueId(dbCon)); 2139 } 2140 catch (Exception e) 2141 { 2142 throw new TorqueException(e); } 2144 } 2145 } 2146 super.save(dbCon); 2147 } 2148 2149 2150 private int getNextIssueId(Connection con) 2151 throws Exception 2152 { 2153 int id = -1; 2154 String key = getIdTableKey(); 2155 DatabaseMap dbMap = IssuePeer.getTableMap().getDatabaseMap(); 2156 IDBroker idbroker = dbMap.getIDBroker(); 2157 try 2158 { 2159 id = idbroker.getIdAsInt(con, key); 2160 } 2161 catch (Exception e) 2162 { 2163 synchronized (idbroker) 2164 { 2165 try 2166 { 2167 id = idbroker.getIdAsInt(con, key); 2168 } 2169 catch (Exception idRetrievalErr) 2170 { 2171 try 2174 { 2175 saveIdTableKey(con); 2176 id = 1; 2177 } 2178 catch (Exception badException) 2179 { 2180 getLog().error("Could not get an id, even after " 2181 +"trying to add a module entry into the ID_TABLE", 2182 e); 2183 getLog() 2184 .error("Error trying to create ID_TABLE entry for " 2185 + getIdTableKey(), badException); 2186 throw new ScarabException( 2188 L10NKeySet.ExceptionRetrievingIssueId, 2189 badException); 2190 } 2191 } 2192 } 2193 } 2194 return id; 2195 } 2196 2197 private String getIdTableKey() 2198 throws Exception 2199 { 2200 Module module = getModule(); 2201 String prefix = module.getCode(); 2202 2203 String domain = module.getScarabInstanceId(); 2204 if (domain != null && domain.length() > 0) 2205 { 2206 prefix = domain + "-" + prefix; 2207 } 2208 return prefix; 2209 } 2210 2211 private void saveIdTableKey(Connection dbCon) 2212 throws Exception 2213 { 2214 int id = 0; 2215 DatabaseMap dbMap = IssuePeer.getTableMap().getDatabaseMap(); 2216 IDBroker idbroker = dbMap.getIDBroker(); 2217 String idTable = IDBroker.TABLE_NAME.substring(0, 2218 IDBroker.TABLE_NAME.indexOf('.')); 2219 id = idbroker.getIdAsInt(dbCon, idTable); 2220 2221 String key = getIdTableKey(); 2222 2223 String sql = "insert into " + idTable 2225 + " (ID_TABLE_ID,TABLE_NAME,NEXT_ID,QUANTITY) " 2226 + " VALUES (" + id + ",'" + key + "',2,1)" ; 2227 BasePeer.executeStatement(sql, dbCon); 2228 } 2229 2230 2251 2252 2253 2256 public IssueTemplateInfo getTemplateInfo() 2257 throws Exception 2258 { 2259 IssueTemplateInfo result = null; 2260 Object obj = ScarabCache.get(this, GET_TEMPLATEINFO); 2261 if (obj == null) 2262 { 2263 Criteria crit = new Criteria(1); 2264 crit.add(IssueTemplateInfoPeer.ISSUE_ID, getIssueId()); 2265 result = (IssueTemplateInfo)IssueTemplateInfoPeer 2266 .doSelect(crit).get(0); 2267 ScarabCache.put(result, this, GET_TEMPLATEINFO); 2268 } 2269 else 2270 { 2271 result = (IssueTemplateInfo)obj; 2272 } 2273 return result; 2274 } 2275 2276 2279 public List getUnsetRequiredAttrs(Module newModule, IssueType newIssueType) 2280 throws Exception 2281 { 2282 List attrs = new ArrayList (); 2283 if (!getIssueType().getIssueTypeId() 2284 .equals(newIssueType.getIssueTypeId()) 2285 || !getModule().getModuleId().equals(newModule.getModuleId())) 2286 { 2287 List requiredAttributes = 2288 newIssueType.getRequiredAttributes(newModule); 2289 Map attrValues = getAttributeValuesMap(); 2290 2291 for (Iterator i = requiredAttributes.iterator(); i.hasNext(); ) 2292 { 2293 Attribute attr = (Attribute)i.next(); 2294 if (!attrValues.containsKey(attr.getName().toUpperCase())) 2295 { 2296 attrs.add(attr); 2297 } 2298 } 2299 } 2300 return attrs; 2301 } 2302 2303 2311 private boolean isNonMatchingAttribute(List nonmatching, AttributeValue value) 2312 { 2313 boolean bRdo = false; 2314 if (value instanceof UserAttribute) 2315 { 2316 for (Iterator it = nonmatching.iterator(); !bRdo && it.hasNext(); ) 2317 { 2318 UserAttribute userAttr = (UserAttribute)it.next(); 2319 bRdo = userAttr.getUserName().equals(((UserAttribute)value).getUserName()); 2320 } 2321 } 2322 else 2323 { 2324 bRdo = nonmatching.contains(value); 2325 } 2326 return bRdo; 2327 } 2328 2329 2332 public Issue move(Module newModule, IssueType newIssueType, 2333 String action, ScarabUser user, String reason, 2334 List commentAttrs, List commentUserValues) 2335 throws Exception 2336 { 2337 Issue newIssue; 2338 2339 Attachment attachment = new Attachment(); 2340 2341 Module oldModule = getModule(); 2342 2343 if (getModule().getModuleId().equals(newModule.getModuleId()) 2346 && !getIssueType().getIssueTypeId().equals(newIssueType.getIssueTypeId()) 2347 && action.equals("move")) 2348 { 2349 newIssue = this; 2350 newIssue.setIssueType(newIssueType); 2351 } 2352 else 2353 { 2354 newIssue = newModule.getNewIssue(newIssueType); 2355 } 2356 newIssue.save(); 2357 2358 if (newIssue != this) 2359 { 2360 if (action.equals("move")) 2362 { 2363 setDeleted(true); 2364 save(); 2365 } 2366 2367 ActivitySet createActivitySet = ActivitySetManager.getInstance( 2368 ActivitySetTypePeer.CREATE_ISSUE__PK, getCreatedBy()); 2369 createActivitySet.setCreatedDate(getCreatedDate()); 2370 createActivitySet.save(); 2371 newIssue.setCreatedTransId(createActivitySet.getActivitySetId()); 2372 newIssue.save(); 2373 2374 2375 List children = getChildren(); 2378 for (Iterator i = children.iterator(); i.hasNext();) 2379 { 2380 Depend depend = (Depend)i.next(); 2381 if (action.equals("move")) 2382 { 2383 doDeleteDependency(null, depend, user); 2384 } 2385 Issue child = IssueManager.getInstance(depend.getObserverId()); 2386 Depend newDepend = new Depend(); 2387 newDepend.setObserverId(child.getIssueId()); 2388 newDepend.setObservedId(newIssue.getIssueId()); 2389 newDepend.setTypeId(depend.getTypeId()); 2390 newIssue.doAddDependency(null, newDepend, child, user); 2391 } 2392 List parents = getParents(); 2393 for (Iterator j = parents.iterator(); j.hasNext();) 2394 { 2395 Depend depend = (Depend)j.next(); 2396 if (action.equals("move")) 2397 { 2398 doDeleteDependency(null, depend, user); 2399 } 2400 Issue parent = IssueManager.getInstance(depend.getObservedId()); 2401 Depend newDepend = new Depend(); 2402 newDepend.setObserverId(newIssue.getIssueId()); 2403 newDepend.setObservedId(parent.getIssueId()); 2404 newDepend.setTypeId(depend.getTypeId()); 2405 parent.doAddDependency(null, newDepend, newIssue, user); 2406 } 2407 2408 Iterator attachments = getAttachments().iterator(); 2410 while (attachments.hasNext()) 2411 { 2412 Attachment oldA = (Attachment)attachments.next(); 2413 Attachment newA = oldA.copy(); 2414 newA.setIssueId(newIssue.getIssueId()); 2415 newA.save(); 2416 Activity oldAct = oldA.getActivity(); 2417 if (oldAct != null) 2418 { 2419 ActivitySet activitySet = getActivitySet( 2420 user, ActivitySetTypePeer.EDIT_ISSUE__PK); 2421 activitySet.save(); 2422 ActivityManager.createTextActivity(newIssue, activitySet, 2423 oldA.getActivity().getDescription(), newA); 2424 } 2425 if (Attachment.FILE__PK.equals(newA.getTypeId())) 2426 { 2427 oldA.copyFileTo(newA.getFullPath()); 2428 } 2429 } 2430 2431 List activitySets = getActivitySets(); 2434 List nonMatchingAttributes = getNonMatchingAttributeValuesList 2435 (newModule, newIssueType); 2436 List alreadyAssociatedUsers = new ArrayList (); 2437 for (Iterator i = activitySets.iterator(); i.hasNext();) 2438 { 2439 ActivitySet as = (ActivitySet)i.next(); 2440 ActivitySet newAS = null; 2441 Attachment newAtt = null; 2442 if (as.getAttachmentId() != null) 2444 { 2445 newAtt = as.getAttachment().copy(); 2446 newAtt.save(); 2447 } 2448 List activities = as.getActivityListForIssue(this); 2450 for (Iterator j = activities.iterator(); j.hasNext();) 2451 { 2452 Activity a = (Activity)j.next(); 2453 if (as.getTypeId().equals((ActivitySetTypePeer.MOVE_ISSUE__PK)) 2458 || !a.getAttributeId().equals(new Integer ("0"))) 2459 { 2460 newAS = new ActivitySet(); 2461 newAS.setTypeId(as.getTypeId()); 2462 if (newAtt != null) 2463 { 2464 newAS.setAttachmentId(newAtt.getAttachmentId()); 2465 } 2466 newAS.setCreatedBy(as.getCreatedBy()); 2467 newAS.setCreatedDate(as.getCreatedDate()); 2468 newAS.save(); 2469 2470 Activity newA = a.copy(newIssue, newAS); 2472 newIssue.getActivity(true).add(newA); 2473 2474 AttributeValue attVal = getAttributeValueWithValue(a.getAttribute(), 2478 a.getNewValue(), a.getNewNumericValue()); 2479 if (a.getEndDate() == null && attVal != null) 2480 { 2481 List values = getAttributeValues(a.getAttribute()); 2482 for (Iterator it = values.iterator(); it.hasNext(); ) 2483 { 2484 AttributeValue att = (AttributeValue)it.next(); 2485 if (attVal != null && !isNonMatchingAttribute(nonMatchingAttributes, att)) 2488 { 2489 boolean isUser = (att instanceof UserAttribute); 2490 if (!isUser || !alreadyAssociatedUsers.contains(((UserAttribute)att).getUserName()+att.getAttribute().getName())) 2491 { 2492 AttributeValue newAttVal = att.copy(); 2493 newAttVal.setIssueId(newIssue.getIssueId()); 2494 newAttVal.setActivity(newA); 2495 newAttVal.startActivitySet(newAS); 2496 newAttVal.save(); 2497 if (isUser) 2498 { 2499 alreadyAssociatedUsers.add(((UserAttribute)att).getUserName()+att.getAttribute().getName()); 2500 } 2501 } 2502 } 2503 } 2504 } 2505 } 2506 } 2507 } 2508 } 2509 2510 StringBuffer attachmentBuf = new StringBuffer (); 2513 StringBuffer delAttrsBuf = new StringBuffer (); 2514 if (reason != null && reason.length() > 0) 2515 { 2516 attachmentBuf.append(reason).append(". "); 2517 } 2518 if (commentAttrs.size() > 0 || commentUserValues.size() > 0 ) 2519 { 2520 attachmentBuf.append(Localization.format( 2521 ScarabConstants.DEFAULT_BUNDLE_NAME, 2522 getLocale(), "DidNotCopyAttributes", newIssueType.getName() + "/" + newModule.getName())); 2523 attachmentBuf.append("\n"); 2524 for (int i = 0; i < commentAttrs.size(); i++) 2525 { 2526 List attVals = getAttributeValues((Attribute) commentAttrs 2527 .get(i)); 2528 for (int j = 0; j < attVals.size(); j++) 2529 { 2530 AttributeValue attVal = (AttributeValue) attVals.get(j); 2531 String field = null; 2532 delAttrsBuf.append(attVal.getAttribute().getName()); 2533 field = attVal.getValue(); 2534 delAttrsBuf.append("=").append(field).append(". ").append( 2535 "\n"); 2536 } 2537 } 2538 for (int i=0; i < commentUserValues.size(); i++) 2539 { 2540 UserAttribute useratt = (UserAttribute)commentUserValues.get(i); 2541 delAttrsBuf.append(useratt.getAttribute().getName() + ": " + 2542 useratt.getUserName() + "\n"); 2543 } 2544 String delAttrs = delAttrsBuf.toString(); 2545 attachmentBuf.append(delAttrs); 2546 2547 Attachment comment = new Attachment(); 2549 comment.setTextFields(user, newIssue, Attachment.COMMENT__PK); 2550 2551 Object [] args = {this.getUniqueId(), newIssueType.getName() + " / " + newModule.getName()}; 2552 StringBuffer commentBuf = new StringBuffer (Localization.format( 2553 ScarabConstants.DEFAULT_BUNDLE_NAME, 2554 getLocale(), 2555 "DidNotCopyAttributesFromArtifact", args)); 2556 commentBuf.append("\n").append(delAttrs); 2557 comment.setData(commentBuf.toString()); 2558 comment.setName(Localization.getString( 2559 ScarabConstants.DEFAULT_BUNDLE_NAME, 2560 getLocale(), 2561 "Comment")); 2562 comment.save(); 2563 } 2564 else 2565 { 2566 attachmentBuf.append(Localization.getString( 2567 ScarabConstants.DEFAULT_BUNDLE_NAME, 2568 getLocale(), 2569 "AllCopied")); 2570 } 2571 attachment.setData(attachmentBuf.toString()); 2572 2573 if (action.equals("move")) 2574 { 2575 attachment.setName(Localization.getString( 2576 ScarabConstants.DEFAULT_BUNDLE_NAME, 2577 getLocale(), 2578 "MovedIssueNote")); 2579 } 2580 else 2581 { 2582 attachment.setName(Localization.getString( 2583 ScarabConstants.DEFAULT_BUNDLE_NAME, 2584 getLocale(), 2585 "CopiedIssueNote")); 2586 } 2587 attachment.setTextFields(user, newIssue, Attachment.MODIFICATION__PK); 2588 attachment.save(); 2589 2590 2591 ActivitySet activitySet2 = ActivitySetManager 2593 .getInstance(ActivitySetTypePeer.MOVE_ISSUE__PK, user, attachment); 2594 activitySet2.save(); 2595 2596 Integer actionChoice = (action.equals("copy")) ? COPIED : MOVED; 2598 Object [] args = { 2599 actionChoice, 2600 getUniqueId(), 2601 oldModule.getName(), 2602 getIssueType().getName() 2603 }; 2604 String desc = Localization.format( 2605 ScarabConstants.DEFAULT_BUNDLE_NAME, 2606 getLocale(), 2607 "MovedFromIssueDescription", args); 2608 2609 Attribute zeroAttribute = AttributeManager 2611 .getInstance(NUMBERKEY_0); 2612 ActivityManager 2613 .createTextActivity(newIssue, zeroAttribute, activitySet2, 2614 desc, null, 2615 getUniqueId(), newIssue.getUniqueId()); 2616 2617 if (newIssue != this) 2619 { 2620 args[1] = newIssue.getUniqueId(); 2621 args[2] = newModule.getName(); 2622 args[3] = newIssueType.getName(); 2623 desc = Localization.format( 2624 ScarabConstants.DEFAULT_BUNDLE_NAME, 2625 getLocale(), 2626 "MovedToIssueDescription", args); 2627 2628 ActivityManager 2629 .createTextActivity(this, zeroAttribute, activitySet2, 2630 desc, null, 2631 getUniqueId(), newIssue.getUniqueId()); 2632 } 2633 2634 2635 return newIssue; 2636 } 2637 2638 public void addVote(ScarabUser user) 2639 throws ScarabException, Exception 2640 { 2641 int previousVotes = 0; 2643 IssueVote issueVote = null; 2644 Criteria crit = new Criteria() 2645 .add(IssueVotePeer.ISSUE_ID, getIssueId()) 2646 .add(IssueVotePeer.USER_ID, user.getUserId()); 2647 List votes = IssueVotePeer.doSelect(crit); 2648 if (votes != null && votes.size() != 0) 2649 { 2650 issueVote = (IssueVote)votes.get(0); 2651 previousVotes = issueVote.getVotes(); 2652 } 2653 else 2654 { 2655 issueVote = new IssueVote(); 2656 issueVote.setIssueId(getIssueId()); 2657 issueVote.setUserId(user.getUserId()); 2658 } 2659 2660 if (!getModule().allowsMultipleVoting() && previousVotes > 0) 2662 { 2663 throw new ScarabException(L10NKeySet.ExceptionMultipleVoteForUnallowed, 2664 user.getUserName(), 2665 getUniqueId()); 2666 } 2667 2668 issueVote.setVotes(previousVotes+1); 2670 issueVote.save(); 2671 2672 crit = new Criteria() 2674 .add(AttributeValuePeer.ATTRIBUTE_ID, 2675 AttributePeer.TOTAL_VOTES__PK); 2676 List voteValues = getAttributeValues(crit); 2677 TotalVotesAttribute voteValue = null; 2678 if (voteValues.size() == 0) 2679 { 2680 voteValue = new TotalVotesAttribute(); 2681 voteValue.setIssue(this); 2682 voteValue.setAttributeId(AttributePeer.TOTAL_VOTES__PK); 2683 } 2684 else 2685 { 2686 voteValue = (TotalVotesAttribute)voteValues.get(0); 2687 } 2688 ActivitySet activitySet = ActivitySetManager 2690 .getInstance(ActivitySetTypePeer.RETOTAL_ISSUE_VOTE__PK, user); 2691 activitySet.save(); 2692 voteValue.startActivitySet(activitySet); 2693 voteValue.addVote(); 2694 voteValue.save(); 2695 } 2696 2697 2701 public List getMatchingAttributeValuesList(Module newModule, 2702 IssueType newIssueType) 2703 throws Exception 2704 { 2705 List matchingAttributes = new ArrayList (); 2706 Map setMap = this.getAttributeValuesMap(); 2707 for (Iterator iter = setMap.keySet().iterator(); iter.hasNext();) 2708 { 2709 AttributeValue aval = (AttributeValue)setMap.get(iter.next()); 2710 List values = getAttributeValues(aval.getAttribute()); 2711 for (int i = 0; i<values.size(); i++) 2713 { 2714 AttributeValue attVal = (AttributeValue)values.get(i); 2715 RModuleAttribute modAttr = newModule. 2716 getRModuleAttribute(aval.getAttribute(), newIssueType); 2717 2718 if (modAttr != null && modAttr.getActive()) 2721 { 2722 if (aval instanceof OptionAttribute) 2725 { 2726 Criteria crit2 = new Criteria(4) 2728 .add(RModuleOptionPeer.ACTIVE, true) 2729 .add(RModuleOptionPeer.OPTION_ID, attVal.getOptionId()) 2730 .add(RModuleOptionPeer.MODULE_ID, newModule.getModuleId()) 2731 .add(RModuleOptionPeer.ISSUE_TYPE_ID, newIssueType.getIssueTypeId()); 2732 List modOpt = RModuleOptionPeer.doSelect(crit2); 2733 2734 if (!modOpt.isEmpty()) 2735 { 2736 matchingAttributes.add(attVal); 2737 } 2738 } 2739 else if (attVal instanceof UserAttribute) 2740 { 2741 ScarabUser user = null; 2742 try 2743 { 2744 user = ScarabUserManager.getInstance(attVal.getUserId()); 2745 } 2746 catch (Exception e) 2747 { 2748 getLog().error(e); 2749 e.printStackTrace(); 2750 } 2751 Attribute attr = attVal.getAttribute(); 2752 ScarabUser[] userArray = newModule.getUsers(attr.getPermission()); 2753 if (Arrays.asList(userArray).contains(user)) 2756 { 2757 matchingAttributes.add(attVal); 2758 } 2759 } 2760 else 2761 { 2762 matchingAttributes.add(attVal); 2763 } 2764 } 2765 } 2766 } 2767 return matchingAttributes; 2768 } 2769 2770 public List getMatchingAttributeValuesList(String moduleId, String issueTypeId) 2771 throws Exception 2772 { 2773 Module module = ModuleManager.getInstance(new Integer (moduleId)); 2774 IssueType issueType = IssueTypeManager.getInstance(new Integer (issueTypeId)); 2775 return getMatchingAttributeValuesList(module, issueType); 2776 } 2777 2778 2783 public List getNonMatchingAttributeValuesList(Module newModule, 2784 IssueType newIssueType) 2785 throws Exception 2786 { 2787 List nonMatchingAttributes = new ArrayList (); 2788 AttributeValue aval = null; 2789 2790 Map setMap = this.getAttributeValuesMap(); 2791 for (Iterator iter = setMap.values().iterator(); iter.hasNext();) 2792 { 2793 aval = (AttributeValue) iter.next(); 2794 List values = getAttributeValues(aval.getAttribute()); 2795 for (Iterator i = values.iterator(); i.hasNext(); ) 2797 { 2798 AttributeValue attVal = (AttributeValue) i.next(); 2799 RModuleAttribute modAttr = newModule. 2800 getRModuleAttribute(aval.getAttribute(), newIssueType); 2801 2802 if (modAttr == null || !modAttr.getActive()) 2805 { 2806 nonMatchingAttributes.add(attVal); 2807 } 2808 else 2809 { 2810 if (attVal instanceof OptionAttribute) 2813 { 2814 Criteria crit2 = new Criteria(1) 2815 .add(RModuleOptionPeer.ACTIVE, true) 2816 .add(RModuleOptionPeer.OPTION_ID, attVal.getOptionId()) 2817 .add(RModuleOptionPeer.MODULE_ID, newModule.getModuleId()) 2818 .add(RModuleOptionPeer.ISSUE_TYPE_ID, newIssueType.getIssueTypeId()); 2819 List modOpt = RModuleOptionPeer.doSelect(crit2); 2820 2821 if ( modOpt.isEmpty()) 2822 { 2823 nonMatchingAttributes.add(attVal); 2824 } 2825 } 2826 else if (attVal instanceof UserAttribute) 2827 { 2828 ScarabUser user = null; 2829 try 2830 { 2831 user = ScarabUserManager.getInstance(attVal.getUserId()); 2832 } 2833 catch (Exception e) 2834 { 2835 Log.get().error("Unable to retrieve user for " 2836 + "attribute", e); 2837 } 2838 Attribute attr = attVal.getAttribute(); 2839 ScarabUser[] userArray = 2840 newModule.getUsers(attr.getPermission()); 2841 if (!Arrays.asList(userArray).contains(user)) 2844 { 2845 nonMatchingAttributes.add(attVal); 2846 } 2847 } 2848 } 2849 } 2850 } 2851 return nonMatchingAttributes; 2852 } 2853 2854 2855 public List getNonMatchingAttributeValuesList(String moduleId, String issueTypeId) 2856 throws Exception 2857 { 2858 Module module = ModuleManager.getInstance(new Integer (moduleId)); 2859 IssueType issueType = IssueTypeManager.getInstance(new Integer (issueTypeId)); 2860 return getNonMatchingAttributeValuesList(module, issueType); 2861 } 2862 2863 2868 public void delete(ScarabUser user) 2869 throws Exception , ScarabException 2870 { 2871 Module module = getModule(); 2872 if (user.hasPermission(ScarabSecurity.ITEM__DELETE, module) 2873 || (user.getUserId().equals(getCreatedBy().getUserId()) && isTemplate())) 2874 { 2875 setDeleted(true); 2876 save(); 2877 } 2878 else 2879 { 2880 throw new ScarabException(L10NKeySet.YouDoNotHavePermissionToAction); 2881 } 2882 } 2883 2884 2885 2892 public AttributeValue getDefaultTextAttributeValue() 2893 throws Exception 2894 { 2895 AttributeValue result = null; 2896 Object obj = ScarabCache.get(this, GET_DEFAULT_TEXT_ATTRIBUTEVALUE); 2897 if (obj == null) 2898 { 2899 Attribute defaultTextAttribute = 2900 getIssueType().getDefaultTextAttribute(getModule()); 2901 if (defaultTextAttribute != null) 2902 { 2903 result = getAttributeValue(defaultTextAttribute); 2904 } 2905 ScarabCache.put(result, this, GET_DEFAULT_TEXT_ATTRIBUTEVALUE); 2906 } 2907 else 2908 { 2909 result = (AttributeValue)obj; 2910 } 2911 return result; 2912 } 2913 2914 2920 public String getDefaultText() 2921 throws Exception 2922 { 2923 String result = null; 2924 Object obj = ScarabCache.get(this, GET_DEFAULT_TEXT); 2925 if (obj == null) 2926 { 2927 AttributeValue emailAV = getDefaultTextAttributeValue(); 2928 if (emailAV != null) 2929 { 2930 result = emailAV.getValue(); 2931 } 2932 if (result == null) 2933 { 2934 ActivitySet activitySet = getInitialActivitySet(); 2935 if (activitySet != null) 2936 { 2937 Attachment reason = activitySet.getAttachment(); 2938 if (reason != null && reason.getData() != null 2939 && reason.getData().trim().length() > 0) 2940 { 2941 result = reason.getData(); 2942 } 2943 } 2944 } 2945 result = (result == null) ? 2946 Localization.getString(ScarabConstants.DEFAULT_BUNDLE_NAME, 2947 getLocale(), "NoIssueSummaryAvailable") 2948 : result; 2949 ScarabCache.put(result, this, GET_DEFAULT_TEXT); 2950 } 2951 else 2952 { 2953 result = (String )obj; 2954 } 2955 return result; 2956 } 2957 2958 2959 private MethodResultCache getMethodResult() 2960 { 2961 return IssueManager.getMethodResult(); 2962 } 2963 2964 2969 private Object getCachedObject(String methodName) 2970 { 2971 Object obj = null; 2972 if (getIssueId() == null) 2978 { 2979 obj = ScarabCache.get(this, methodName); 2980 } 2981 else 2982 { 2983 obj = getMethodResult().get(this, methodName); 2984 } 2985 return obj; 2986 } 2987 2988 2993 private void putCachedObject(Object obj, String methodName) 2994 { 2995 if (getIssueId() == null) 2997 { 2998 ScarabCache.put(obj, this, methodName); 2999 } 3000 else 3001 { 3002 getMethodResult().put(obj, this, methodName); 3003 } 3004 } 3005 3006 3011 private Object getCachedObject(String methodName, Serializable arg1) 3012 { 3013 Object obj = null; 3014 if (getIssueId() == null) 3020 { 3021 obj = ScarabCache.get(this, methodName, arg1); 3022 } 3023 else 3024 { 3025 obj = getMethodResult().get(this, methodName, arg1); 3026 } 3027 return obj; 3028 } 3029 3030 3035 private void putCachedObject(Object obj, String methodName, 3036 Serializable arg1) 3037 { 3038 if (getIssueId() == null) 3040 { 3041 ScarabCache.put(obj, this, methodName, arg1); 3042 } 3043 else 3044 { 3045 getMethodResult().put(obj, this, methodName, arg1); 3046 } 3047 } 3048 3049 3054 private Object getCachedObject(String methodName, 3055 Serializable arg1, Serializable arg2) 3056 { 3057 Object obj = null; 3058 if (getIssueId() == null) 3064 { 3065 obj = ScarabCache.get(this, methodName, arg1, arg2); 3066 } 3067 else 3068 { 3069 obj = getMethodResult().get(this, methodName, arg1, arg2); 3070 } 3071 return obj; 3072 } 3073 3074 3079 private void putCachedObject(Object obj, String methodName, 3080 Serializable arg1, Serializable arg2) 3081 { 3082 if (getIssueId() == null) 3084 { 3085 ScarabCache.put(obj, this, methodName, arg1, arg2); 3086 } 3087 else 3088 { 3089 getMethodResult().put(obj, this, methodName, arg1, arg2); 3090 } 3091 } 3092 3093 3094 3098 3102 public boolean hasEnterPermission(ScarabUser user, Module module) 3103 throws Exception 3104 { 3105 boolean hasPerm = false; 3106 3107 if (user.hasPermission(ScarabSecurity.ISSUE__ENTER, module)) 3108 { 3109 hasPerm = true; 3110 } 3111 return hasPerm; 3112 } 3113 3114 3115 3119 public boolean hasEditPermission(ScarabUser user, Module module) 3120 throws Exception 3121 { 3122 boolean hasPerm = false; 3123 3124 if (user.hasPermission(ScarabSecurity.ISSUE__EDIT, module) 3125 || user.equals(getCreatedBy())) 3126 { 3127 hasPerm = true; 3128 } 3129 return hasPerm; 3130 } 3131 3132 3136 public boolean hasMovePermission(ScarabUser user, Module module) 3137 throws Exception 3138 { 3139 boolean hasPerm = false; 3140 3141 if (user.hasPermission(ScarabSecurity.ISSUE__EDIT, module) 3142 || user.equals(getCreatedBy())) 3143 { 3144 hasPerm = true; 3145 } 3146 return hasPerm; 3147 } 3148 3149 3152 public ActivitySet assignUser(ActivitySet activitySet, String description, 3153 ScarabUser assignee, ScarabUser assigner, 3154 Attribute attribute, Attachment attachment) 3155 throws Exception 3156 { 3157 UserAttribute attVal = new UserAttribute(); 3158 3159 if (activitySet == null) 3161 { 3162 activitySet = ActivitySetManager 3163 .getInstance(ActivitySetTypePeer.EDIT_ISSUE__PK, assigner, 3164 attachment); 3165 activitySet.save(); 3166 attVal.startActivitySet(activitySet); 3167 } 3168 3169 if (description == null) 3170 { 3171 description = getAssignUserChangeString(assigner, assignee, 3173 attribute); 3174 } 3175 ActivityManager 3176 .createUserActivity(this, attribute, activitySet, 3177 description, null, 3178 null, assignee.getUserId()); 3179 3180 attVal.setIssue(this); 3182 attVal.setAttributeId(attribute.getAttributeId()); 3183 attVal.setUserId(assignee.getUserId()); 3184 attVal.setValue(assignee.getUserName()); 3185 attVal.save(); 3186 3187 return activitySet; 3188 } 3189 3190 3193 public ActivitySet assignUser(ActivitySet activitySet, 3194 ScarabUser assignee, ScarabUser assigner, 3195 Attribute attribute, Attachment attachment) 3196 throws Exception 3197 { 3198 return assignUser(activitySet, null, 3199 assignee, assigner, 3200 attribute, attachment); 3201 } 3202 3203 3208 private String getAssignUserChangeString(ScarabUser assigner, 3209 ScarabUser assignee, 3210 Attribute attr) 3211 throws Exception 3212 { 3213 String attrDisplayName = getModule() 3214 .getRModuleAttribute(attr, getIssueType()).getDisplayValue(); 3215 Object [] args = { 3216 assigner.getUserName(), 3217 assignee.getUserName(), 3218 attrDisplayName 3219 }; 3220 String actionString = Localization.format( 3221 ScarabConstants.DEFAULT_BUNDLE_NAME, 3222 getLocale(), 3223 "AssignIssueEmailAddedUserAction", args); 3224 return actionString; 3225 } 3226 3227 3228 3232 public ActivitySet changeUserAttributeValue(ActivitySet activitySet, 3233 ScarabUser assignee, 3234 ScarabUser assigner, 3235 AttributeValue oldAttVal, 3236 Attribute newAttr, 3237 Attachment attachment) 3238 throws Exception 3239 { 3240 3241 String actionString = null; 3242 if (activitySet == null) 3244 { 3245 activitySet = ActivitySetManager 3246 .getInstance(ActivitySetTypePeer.EDIT_ISSUE__PK, assigner, attachment); 3247 activitySet.save(); 3248 oldAttVal.startActivitySet(activitySet); 3249 } 3250 3251 actionString = getUserDeleteString(assigner, assignee, 3253 oldAttVal.getAttribute()); 3254 if (actionString == null) 3255 { 3256 Log.get().debug("User attribute '"+oldAttVal.getAttribute() 3257 .getName()+"' removed from the artifact type"); 3258 } 3259 ActivityManager 3260 .createUserActivity(this, oldAttVal.getAttribute(), 3261 activitySet, 3262 actionString, null, 3263 assignee.getUserId(), null); 3264 3265 3266 actionString = getAssignUserChangeString(assigner, assignee, 3268 newAttr); 3269 ActivityManager 3270 .createUserActivity(this, newAttr, activitySet, 3271 actionString, null, 3272 null, assignee.getUserId()); 3273 3274 oldAttVal.setAttributeId(newAttr.getAttributeId()); 3276 oldAttVal.save(); 3277 3278 return activitySet; 3279 } 3280 3281 3286 private String getUserAttributeChangeString(ScarabUser assigner, 3287 ScarabUser assignee, 3288 Attribute oldAttr, 3289 Attribute newAttr) 3290 throws Exception 3291 { 3292 String oldAttrDisplayName = getModule() 3293 .getRModuleAttribute(oldAttr, getIssueType()).getDisplayValue(); 3294 String newAttrDisplayName = getModule() 3295 .getRModuleAttribute(newAttr, getIssueType()).getDisplayValue(); 3296 Object [] args = { 3297 assignee.getUserName(), assigner.getUserName(), 3298 oldAttrDisplayName, newAttrDisplayName 3299 }; 3300 String actionString = Localization.format( 3301 ScarabConstants.DEFAULT_BUNDLE_NAME, 3302 getLocale(), 3303 "AssignIssueEmailChangedUserAttributeAction", args); 3304 return actionString; 3305 } 3306 3307 3308 3311 public ActivitySet deleteUser(ActivitySet activitySet, ScarabUser assignee, 3312 ScarabUser assigner, 3313 AttributeValue attVal, Attachment attachment) 3314 throws Exception 3315 { 3316 Attribute attr = attVal.getAttribute(); 3317 String actionString = null; 3318 if (activitySet == null) 3320 { 3321 activitySet = ActivitySetManager 3322 .getInstance(ActivitySetTypePeer.EDIT_ISSUE__PK, assigner, attachment); 3323 activitySet.save(); 3324 attVal.startActivitySet(activitySet); 3325 } 3326 3327 actionString = getUserDeleteString(assigner, assignee, attr); 3329 if (actionString == null) 3330 { 3331 Log.get().debug("User attribute '"+attr.getName()+ 3332 "' removed from the artifact type." ); 3333 } 3334 3335 ActivityManager 3336 .createUserActivity(this, attVal.getAttribute(), 3337 activitySet, 3338 actionString, null, 3339 assignee.getUserId(), null); 3340 3341 attVal.setDeleted(true); 3343 attVal.save(); 3344 3345 return activitySet; 3346 } 3347 3348 3353 private String getUserDeleteString(ScarabUser assigner, 3354 ScarabUser assignee, 3355 Attribute attr) 3356 throws Exception 3357 { 3358 String actionString = null; 3359 RModuleAttribute rma = getModule() 3360 .getRModuleAttribute(attr, getIssueType()); 3361 if (rma != null) 3362 { 3363 String attrDisplayName = rma.getDisplayValue(); 3364 Object [] args = { 3365 assigner.getUserName(), assignee.getUserName(), 3366 attrDisplayName 3367 }; 3368 actionString = Localization.format( 3369 ScarabConstants.DEFAULT_BUNDLE_NAME, getLocale(), 3370 "AssignIssueEmailRemovedUserAction", args); 3371 } 3372 return actionString; 3373 } 3374 3375 3378 public ActivitySet doDeleteDependency(ActivitySet activitySet, 3379 Depend oldDepend, ScarabUser user) 3380 throws Exception 3381 { 3382 Issue otherIssue = IssueManager 3383 .getInstance(oldDepend.getObserverId(), false); 3384 3390 Issue thisIssue = IssueManager 3391 .getInstance(oldDepend.getObservedId(), false); 3392 3393 Object [] args = { 3394 oldDepend.getDependType().getName(), 3395 thisIssue.getUniqueId(), 3396 otherIssue.getUniqueId() 3397 }; 3398 String desc = Localization.format( 3399 ScarabConstants.DEFAULT_BUNDLE_NAME, 3400 getLocale(), 3401 "DependencyDeletedDesc", args); 3402 3403 oldDepend = thisIssue.getDependency(otherIssue); 3405 oldDepend.setNew(false); 3406 oldDepend.setDeleted(true); 3407 oldDepend.save(); 3408 3409 ScarabCache.put(null, thisIssue, GET_DEPENDENCY, otherIssue); 3412 3413 if (activitySet == null) 3414 { 3415 Attachment comment = oldDepend.getDescriptionAsAttachment(user, thisIssue); 3417 3418 activitySet = getActivitySet(user, comment, 3419 ActivitySetTypePeer.EDIT_ISSUE__PK); 3420 activitySet.save(); 3422 } 3423 3424 ActivityManager 3425 .createDeleteDependencyActivity(thisIssue, activitySet, oldDepend, 3426 desc); 3427 ActivityManager 3428 .createDeleteDependencyActivity(otherIssue, activitySet, oldDepend, 3429 desc); 3430 3431 3432 return activitySet; 3433 } 3434 3435 3440 public ActivitySet doChangeUrlDescription(ActivitySet activitySet, 3441 ScarabUser user, 3442 Attachment attachment, 3443 String oldDescription) 3444 throws Exception 3445 { 3446 String newDescription = attachment.getName(); 3447 if (!oldDescription.equals(newDescription)) 3448 { 3449 Object [] args = { 3450 oldDescription, 3451 newDescription, 3452 }; 3453 String desc = Localization.format( 3454 ScarabConstants.DEFAULT_BUNDLE_NAME, 3455 getLocale(), 3456 "UrlDescChangedDesc", args); 3457 3458 if (desc.length() > 248) 3459 { 3460 desc = desc.substring(0,248) + "..."; 3461 } 3462 if (activitySet == null) 3463 { 3464 activitySet = getActivitySet(user, ActivitySetTypePeer.EDIT_ISSUE__PK); 3466 activitySet.save(); 3467 } 3468 ActivityManager 3470 .createTextActivity(this, activitySet, 3471 desc, attachment, 3472 oldDescription, newDescription); 3473 try 3474 { 3475 activitySet.sendEmail(this); 3476 } 3477 catch (Exception e) 3478 { 3479 Localizable urlDescSaved = new L10NMessage(L10NKeySet.UrlDescChangedDesc); 3480 Localizable emailError = new L10NMessage(L10NKeySet.CouldNotSendEmail,e); 3481 throw new ScarabException(L10NKeySet.SavedButErrors, 3482 urlDescSaved, 3483 emailError); 3484 } 3485 } 3486 return activitySet; 3487 } 3488 3489 3494 public ActivitySet doChangeUrlUrl(ActivitySet activitySet, ScarabUser user, 3495 Attachment attachment, String oldUrl) 3496 throws Exception 3497 { 3498 String newUrl = attachment.getData(); 3499 if (!oldUrl.equals(newUrl)) 3500 { 3501 Object [] args = { 3502 oldUrl, newUrl 3503 }; 3504 String desc = Localization.format( 3505 ScarabConstants.DEFAULT_BUNDLE_NAME, 3506 getLocale(), 3507 "UrlChangedDesc", args); 3508 3509 if (desc.length() > 248) 3510 { 3511 desc = desc.substring(0,248) + "..."; 3512 } 3513 if (activitySet == null) 3514 { 3515 activitySet = getActivitySet(user, ActivitySetTypePeer.EDIT_ISSUE__PK); 3517 activitySet.save(); 3518 } 3519 ActivityManager 3521 .createTextActivity(this, activitySet, 3522 desc, attachment, 3523 oldUrl, newUrl); 3524 try 3525 { 3526 activitySet.sendEmail(this); 3527 } 3528 catch (Exception e) 3529 { 3530 Localizable urlChanged = new L10NMessage(L10NKeySet.UrlChangedDesc, oldUrl, newUrl); 3531 Localizable emailError = new L10NMessage(L10NKeySet.CouldNotSendEmail, e); 3532 throw new ScarabException(L10NKeySet.SavedButErrors, 3533 urlChanged, 3534 emailError); 3535 } 3536 } 3537 return activitySet; 3538 } 3539 3540 3544 public ActivitySet doChangeDependencyType(ActivitySet activitySet, 3545 Depend oldDepend, 3546 Depend newDepend, 3547 ScarabUser user) 3548 throws Exception 3549 { 3550 String oldName = oldDepend.getDependType().getName(); 3551 String newName = newDepend.getDependType().getName(); 3552 if (!newName.equals(oldName) && !newDepend.getDeleted()) 3555 { 3556 Issue otherIssue = IssueManager 3557 .getInstance(newDepend.getObserverId(), false); 3558 3559 oldDepend.setDeleted(true); 3561 oldDepend.save(); 3562 newDepend.setNew(true); 3564 newDepend.save(); 3565 3566 Object [] args = { 3567 this.getUniqueId(), 3568 otherIssue.getUniqueId(), 3569 oldName, 3570 newName 3571 }; 3572 String desc = Localization.format( 3573 ScarabConstants.DEFAULT_BUNDLE_NAME, 3574 getLocale(), 3575 "DependencyTypeChangedDesc", args); 3576 3577 ScarabCache.put(null, this, GET_DEPENDENCY, otherIssue); 3580 3581 if (activitySet == null) 3582 { 3583 Attachment comment = newDepend.getDescriptionAsAttachment(user, this); 3585 3586 activitySet = getActivitySet(user, comment, 3587 ActivitySetTypePeer.EDIT_ISSUE__PK); 3588 activitySet.save(); 3590 } 3591 3592 ActivityManager 3593 .createChangeDependencyActivity(this, activitySet, newDepend, 3594 desc, oldName, newName); 3595 ActivityManager 3596 .createChangeDependencyActivity(otherIssue, activitySet, newDepend, 3597 desc, oldName, newName); 3598 } 3599 return activitySet; 3600 } 3601 3602 3609 public ActivitySet setInitialAttributeValues(ActivitySet activitySet, 3610 Attachment attachment, HashMap newValues, ScarabUser user) 3611 throws Exception 3612 { 3613 String msg = doCheckInitialAttributeValueWorkflow(newValues, user); 3615 if (msg != null) 3616 { 3617 throw new Exception (msg); } 3619 3620 if (activitySet == null) 3621 { 3622 activitySet = ActivitySetManager 3624 .getInstance(ActivitySetTypePeer.CREATE_ISSUE__PK, user); 3625 activitySet.save(); 3626 } 3627 setActivitySet(activitySet); 3628 3629 LinkedMap avMap = getModuleAttributeValuesMap(); 3631 MapIterator iter = avMap.mapIterator(); 3632 while (iter.hasNext()) 3633 { 3634 AttributeValue aval = (AttributeValue)avMap.get(iter.next()); 3635 try 3636 { 3637 aval.startActivitySet(activitySet); 3638 } 3639 catch (ScarabException se) 3640 { 3641 throw new Exception ("Fatal Error: " + 3642 se.getMessage() + " Please start over."); } 3644 } 3645 this.save(); 3646 3647 ActivityManager.createReportIssueActivity(this, activitySet, 3649 Localization.getString( 3650 ScarabConstants.DEFAULT_BUNDLE_NAME, 3651 getLocale(), 3652 "IssueCreated")); 3653 3654 String attachmentData = attachment.getData(); 3657 if (attachmentData != null && 3658 attachmentData.length() > 0) 3659 { 3660 attachment = AttachmentManager.getReason(attachment, this, user); 3661 activitySet.setAttachment(attachment); 3662 } 3663 activitySet.save(); 3664 3665 ScarabCache.clear(); 3669 return activitySet; 3670 } 3671 3672 3683 public ActivitySet setAttributeValues(ActivitySet activitySet, 3684 HashMap newAttVals, 3685 Attachment attachment, 3686 ScarabUser user) 3687 throws Exception 3688 { 3689 if (!isTemplate()) 3690 { 3691 String msg = doCheckAttributeValueWorkflow(newAttVals, user); 3692 if (msg != null) 3693 { 3694 throw new Exception (msg); } 3696 } 3697 if (attachment != null) 3699 { 3700 attachment.setTextFields(user, this, 3701 Attachment.MODIFICATION__PK); 3702 attachment.save(); 3703 } 3704 3705 if (activitySet == null) 3707 { 3708 activitySet = getActivitySet(user, attachment, 3709 ActivitySetTypePeer.EDIT_ISSUE__PK); 3710 activitySet.save(); 3711 ScarabCache.clear(); 3712 } 3713 3714 LinkedMap avMap = getModuleAttributeValuesMap(); 3715 AttributeValue oldAttVal = null; 3716 AttributeValue newAttVal = null; 3717 Iterator iter = newAttVals.keySet().iterator(); 3718 boolean attValDeleted = false; 3719 while (iter.hasNext()) 3720 { 3721 Integer attrId = (Integer )iter.next(); 3722 Attribute attr = AttributeManager.getInstance(attrId); 3723 oldAttVal = (AttributeValue)avMap.get(attr.getName().toUpperCase()); 3724 newAttVal = (AttributeValue)newAttVals.get(attrId); 3725 String newAttValValue = newAttVal.getValue(); 3726 if (Log.get().isDebugEnabled()) 3727 { 3728 Log.get().debug("Attribute: " + attr.getName() + 3729 " has newAttValValue = " + newAttValValue); 3730 } 3731 if (newAttValValue != null && newAttValValue.length() > 0) 3732 { 3733 oldAttVal.setProperties(newAttVal); 3734 } 3735 else 3736 { 3737 oldAttVal.setDeleted(true); 3738 Log.get().debug("setDeleted(true)"); 3739 attValDeleted = true; 3740 } 3741 oldAttVal.startActivitySet(activitySet); 3742 oldAttVal.save(); 3743 } 3744 if (attValDeleted) 3745 { 3746 getMethodResult().remove(this, GET_MODULE_ATTRVALUES_MAP, 3748 Boolean.TRUE); 3749 } 3750 return activitySet; 3751 } 3752 3753 3759 public String doCheckInitialAttributeValueWorkflow(HashMap newValues, 3760 ScarabUser user) 3761 throws Exception 3762 { 3763 String msg = null; 3764 Iterator iter = newValues.keySet().iterator(); 3765 while (iter.hasNext()) 3766 { 3767 Integer attrId = (Integer )iter.next(); 3768 Attribute attr = AttributeManager.getInstance(attrId); 3769 if (attr.isOptionAttribute()) 3770 { 3771 AttributeOption toOption = AttributeOptionManager 3772 .getInstance(new Integer ((String )newValues.get(attrId))); 3773 msg = WorkflowFactory.getInstance().checkInitialTransition( 3774 toOption, this, 3775 newValues, user); 3776 } 3777 if (msg != null) 3778 { 3779 break; 3780 } 3781 } 3782 return msg; 3783 } 3784 3785 3790 public String doCheckAttributeValueWorkflow(HashMap newAttVals, 3791 ScarabUser user) 3792 throws Exception 3793 { 3794 LinkedMap avMap = getModuleAttributeValuesMap(); 3795 AttributeValue oldAttVal = null; 3796 AttributeValue newAttVal = null; 3797 String msg = null; 3798 Iterator iter = newAttVals.keySet().iterator(); 3799 while (iter.hasNext()) 3800 { 3801 Integer attrId = (Integer )iter.next(); 3802 Attribute attr = AttributeManager.getInstance(attrId); 3803 oldAttVal = (AttributeValue)avMap.get(attr.getName().toUpperCase()); 3804 newAttVal = (AttributeValue)newAttVals.get(attrId); 3805 AttributeOption fromOption = null; 3806 AttributeOption toOption = null; 3807 3808 if (newAttVal.getValue() != null) 3809 { 3810 if (newAttVal.getAttribute().isOptionAttribute()) 3811 { 3812 if (oldAttVal.getOptionId() == null) 3813 { 3814 fromOption = AttributeOptionManager.getInstance(ScarabConstants.INTEGER_0); 3815 } 3816 else 3817 { 3818 fromOption = oldAttVal.getAttributeOption(); 3819 } 3820 toOption = newAttVal.getAttributeOption(); 3821 msg = WorkflowFactory.getInstance().checkTransition( 3822 fromOption, 3823 toOption, this, 3824 newAttVals, user); 3825 } 3826 if (msg != null) 3827 { 3828 break; 3829 } 3830 } 3831 } 3832 return msg; 3833 } 3834 3835 3842 public String doCheckAttributeValueWorkflow(HashMap newAttVals, 3843 Attachment attachment, 3844 ScarabUser user) 3845 throws Exception 3846 { 3847 return doCheckAttributeValueWorkflow(newAttVals, user); 3848 } 3849 3850 3851 3852 3856 public ActivitySet doEditComment(ActivitySet activitySet, String newComment, 3857 Attachment attachment, ScarabUser user) 3858 throws Exception 3859 { 3860 String oldComment = attachment.getData(); 3861 if (!newComment.equals(oldComment)) 3862 { 3863 attachment.setData(newComment); 3864 attachment.save(); 3865 3866 Object [] args = { 3868 oldComment, 3869 newComment 3870 }; 3871 String desc = Localization.format( 3872 ScarabConstants.DEFAULT_BUNDLE_NAME, 3873 getLocale(), 3874 "ChangedComment", args); 3875 3876 if (activitySet == null) 3877 { 3878 activitySet = getActivitySet(user, 3880 ActivitySetTypePeer.EDIT_ISSUE__PK); 3881 activitySet.save(); 3882 } 3883 ActivityManager 3885 .createTextActivity(this, null, activitySet, 3886 desc, attachment, 3887 oldComment, newComment); 3888 3889 try 3890 { 3891 activitySet.sendEmail(this); 3892 } 3893 catch (Exception e) 3894 { 3895 Localizable commentSaved = new L10NMessage( L10NKeySet.CommentSaved,oldComment,newComment); 3896 Localizable emailError = new L10NMessage( L10NKeySet.CouldNotSendEmail,e); 3897 throw new ScarabException(L10NKeySet.SavedButErrors, 3898 commentSaved, 3899 emailError); 3900 } 3901 } 3902 return activitySet; 3903 } 3904 3905 3909 public ActivitySet doDeleteUrl(ActivitySet activitySet, 3910 Attachment attachment, ScarabUser user) 3911 throws Exception 3912 { 3913 String oldUrl = attachment.getData(); 3914 attachment.setDeleted(true); 3915 attachment.save(); 3916 3917 String name = attachment.getName(); 3919 String desc = Localization.format( 3920 ScarabConstants.DEFAULT_BUNDLE_NAME, 3921 getLocale(), 3922 "UrlDeletedDesc", name); 3923 3924 if (activitySet == null) 3925 { 3926 activitySet = getActivitySet(user, 3928 ActivitySetTypePeer.EDIT_ISSUE__PK); 3929 activitySet.save(); 3930 } 3931 ActivityManager 3933 .createTextActivity(this, null, activitySet, 3934 desc, attachment, oldUrl, null); 3935 return activitySet; 3936 } 3937 3938 3948 public ActivitySet doRemoveAttachment(ActivitySet activitySet, 3949 MutableBoolean physicallyDeleted, 3950 Attachment attachment, 3951 ScarabUser user) 3952 throws Exception 3953 { 3954 boolean attachmentPhysicallyDeleted = false; 3955 boolean physicalDeletionAllowed = Turbine.getConfiguration() 3956 .getBoolean("scarab.attachment.remove.permanent",false); 3957 3958 if(physicalDeletionAllowed) 3959 { 3960 attachmentPhysicallyDeleted = attachment.deletePhysicalAttachment(); 3961 physicallyDeleted.set(attachmentPhysicallyDeleted); 3962 } 3963 3964 attachment.setDeleted(true); 3965 attachment.save(); 3966 3967 String name = new String (); 3969 Localizable l10nMessage; 3970 if (!physicalDeletionAllowed) 3971 { 3972 name = attachment.getFileName(); 3973 l10nMessage = new L10NMessage(L10NKeySet.AttachmentDeletedDesc, name); 3974 } 3975 else if (attachmentPhysicallyDeleted) 3976 { 3977 name = attachment.getFileName(); 3978 l10nMessage = new L10NMessage(L10NKeySet.FileDeletedDesc, name ); 3979 } 3980 else 3981 { 3982 name = attachment.getFullPath(); 3983 l10nMessage = new L10NMessage(L10NKeySet.FileNotDeletedDesc, name); 3984 } 3985 3986 if (activitySet == null) 3987 { 3988 activitySet = getActivitySet(user, 3990 ActivitySetTypePeer.EDIT_ISSUE__PK); 3991 activitySet.save(); 3992 } 3993 3994 String desc = l10nMessage.getMessage(); 3999 ActivityManager 4000 .createTextActivity(this, null, activitySet, 4001 desc, attachment, name, null); 4002 4003 return activitySet; 4004 } 4005 4006 4007 4010 public HashSet getAssociatedUsers() throws Exception 4011 { 4012 HashSet users = null; 4013 Object obj = ScarabCache.get(this, GET_ASSOCIATED_USERS); 4014 if (obj == null) 4015 { 4016 List attributeList = getModule() 4017 .getUserAttributes(getIssueType(), true); 4018 List attributeIdList = new ArrayList (); 4019 4020 for (int i=0; i<attributeList.size(); i++) 4021 { 4022 Attribute att = (Attribute) attributeList.get(i); 4023 RModuleAttribute modAttr = getModule(). 4024 getRModuleAttribute(att, getIssueType()); 4025 if (modAttr.getActive()) 4026 { 4027 attributeIdList.add(att.getAttributeId()); 4028 } 4029 } 4030 4031 if (!attributeIdList.isEmpty()) 4032 { 4033 users = new HashSet (); 4034 Criteria crit = new Criteria() 4035 .addIn(AttributeValuePeer.ATTRIBUTE_ID, attributeIdList) 4036 .add(AttributeValuePeer.DELETED, false); 4037 crit.setDistinct(); 4038 4039 List attValues = getAttributeValues(crit); 4040 for (int i=0; i<attValues.size(); i++) 4041 { 4042 List item = new ArrayList (2); 4043 AttributeValue attVal = (AttributeValue) attValues.get(i); 4044 ScarabUser su = ScarabUserManager.getInstance(attVal.getUserId()); 4045 Attribute attr = AttributeManager.getInstance(attVal.getAttributeId()); 4046 item.add(attr); 4047 item.add(su); 4048 users.add(item); 4049 } 4050 } 4051 ScarabCache.put(users, this, GET_ASSOCIATED_USERS); 4052 } 4053 else 4054 { 4055 users = (HashSet )obj; 4056 } 4057 return users; 4058 } 4059 4060 4061 public String toString() 4062 { 4063 String id = null; 4064 try 4065 { 4066 id = isNew() ? "New issue" : getUniqueId(); 4067 } 4068 catch (Exception e) 4069 { 4070 id = "Error in getting unique id"; 4071 Log.get().warn(id, e); 4072 } 4073 4074 return super.toString() + '{' + id + '}'; 4075 } 4076 4077 4082 public boolean isBlockingConditionTrue() throws Exception 4083 { 4084 boolean isBlockingConditionTrue = false; 4085 List blockingConditions = this.getRModuleIssueType().getConditions(); 4086 for (Iterator it = blockingConditions.iterator(); !isBlockingConditionTrue && it.hasNext(); ) 4087 { 4088 Condition cond = (Condition)it.next(); 4089 Integer conditionOptionId = cond.getOptionId(); 4090 Attribute attr = cond.getAttributeOption().getAttribute(); 4091 AttributeValue attrVal = this.getAttributeValue(attr); 4092 if (attrVal != null) 4093 { 4094 Integer issueOptionId = attrVal.getOptionId(); 4095 if (issueOptionId != null && issueOptionId.equals(conditionOptionId)) 4096 { 4097 isBlockingConditionTrue = true; 4098 } 4099 } 4100 } 4101 return isBlockingConditionTrue; 4102 } 4103 4104 4109 public boolean isBlockingAnyIssue() throws Exception 4110 { 4111 return this.getBlockedIssues().size() > 0; 4112 } 4113 4114 4120 public boolean isBlocked() throws Exception 4121 { 4122 return (getBlockingIssues().size()>0); 4123 } 4124 4125 4130 public List getBlockingIssues() throws Exception 4131 { 4132 List blockingIssues = new ArrayList (); 4133 List blockingDependantIssues = this.getBlockingDependantIssues(); 4134 for (Iterator it = blockingDependantIssues.iterator(); it.hasNext(); ) 4135 { 4136 Issue is = (Issue)it.next(); 4137 if (is.isBlockingConditionTrue()) 4138 blockingIssues.add(is); 4139 } 4140 return blockingIssues; 4141 } 4142 4143 4149 public List getBlockingDependantIssues() throws Exception 4150 { 4151 List blockingIssues = new ArrayList (); 4152 List parentIssues = this.getParents(); 4153 for (Iterator it = parentIssues.iterator(); it.hasNext(); ) 4154 { 4155 Depend depend = (Depend)it.next(); 4156 if (depend.getDependType().getDependTypeId().equals(DependTypePeer.BLOCKING__PK)) 4157 { 4158 blockingIssues.add(IssuePeer.retrieveByPK(depend.getObservedId())); 4159 } 4160 } 4161 return blockingIssues; 4162 } 4163 4164 4170 public List getBlockedIssues() throws Exception 4171 { 4172 if (this.isBlockingConditionTrue()) 4173 return this.getBlockedDependantIssues(); 4174 else 4175 return new ArrayList (); 4176 } 4177 4178 4185 public List getBlockedDependantIssues() throws Exception 4186 { 4187 List blockableIssues = new ArrayList (); 4188 List childIssues = this.getChildren(); 4189 for (Iterator it = childIssues.iterator(); it.hasNext(); ) 4190 { 4191 Depend depend = (Depend)it.next(); 4192 if (depend.getDependType().getDependTypeId().equals(DependTypePeer.BLOCKING__PK)) 4193 { 4194 blockableIssues.add(IssuePeer.retrieveByPK(depend.getObserverId())); 4195 } 4196 } 4197 return blockableIssues; 4198 } 4199} 4200 | Popular Tags |