1 package org.tigris.scarab.om; 2 3 48 49 import java.util.Collections ; 50 import java.util.ArrayList ; 51 import java.util.Iterator ; 52 import java.util.List ; 53 import java.util.Locale ; 54 import java.util.Map ; 55 import java.util.HashMap ; 56 import java.util.Comparator ; 57 import java.sql.Connection ; 58 59 import org.apache.torque.TorqueException; 60 import org.apache.torque.util.Criteria; 61 import org.apache.torque.om.BaseObject; 62 63 import org.apache.fulcrum.localization.Localization; 64 65 import org.tigris.scarab.reports.ReportBridge; 66 import org.tigris.scarab.tools.localization.L10NKeySet; 67 import org.tigris.scarab.util.ScarabException; 68 import org.tigris.scarab.services.security.ScarabSecurity; 69 import org.tigris.scarab.services.cache.ScarabCache; 70 import org.tigris.scarab.util.Log; 71 import org.tigris.scarab.util.ScarabConstants; 72 73 81 public abstract class AbstractScarabUser 82 extends BaseObject 83 { 84 85 private static final String GET_R_MODULE_USERATTRIBUTES = 86 "getRModuleUserAttributes"; 87 88 private static final String GET_R_MODULE_USERATTRIBUTE = 89 "getRModuleUserAttribute"; 90 91 private static final String [] HOME_PAGES = {"home,EnterNew.vm", 92 "query", "Index.vm"}; 93 94 private static final int MAX_INDEPENDENT_WINDOWS = 10; 95 96 99 protected Locale locale = null; 100 101 105 private int issueCount = 0; 106 107 110 private Map issueMap; 111 112 116 private int reportCount = 0; 117 118 121 private Map reportMap; 122 123 126 private Map mostRecentQueryMap; 127 128 132 private Map mostRecentQueryMITMap; 133 134 137 private Map associatedUsersMap; 138 139 142 private Map selectedUsersMap; 143 144 148 private int enterIssueRedirect = 0; 149 150 154 private Map mitListMap; 155 156 159 private Map enterIssueMap; 160 161 165 private boolean showOtherModulesInIssueTypeList; 166 167 private Map activeKeys = new HashMap (); 168 private transient ThreadLocal threadKey = null; 169 170 175 private int threadCount = 0; 176 177 private transient ThreadLocal currentModule = null; 182 private transient ThreadLocal currentIssueType = null; 183 184 187 public AbstractScarabUser() 188 { 189 super(); 190 issueMap = new HashMap (); 191 reportMap = new HashMap (); 192 mitListMap = new HashMap (); 193 enterIssueMap = new HashMap (); 194 mostRecentQueryMap = new HashMap (); 195 mostRecentQueryMITMap = new HashMap (); 196 associatedUsersMap = new HashMap (); 197 selectedUsersMap = new HashMap (); 198 initThreadLocals(); 199 } 200 201 205 private void readObject(java.io.ObjectInputStream in) 206 throws java.io.IOException , ClassNotFoundException 207 { 208 try 209 { 210 in.defaultReadObject(); 211 } 212 catch (java.io.NotActiveException e) 213 { 214 } 215 initThreadLocals(); 216 } 217 218 private void initThreadLocals() 219 { 220 currentIssueType = new ThreadLocal (); 221 currentModule = new ThreadLocal (); 222 threadKey = new ThreadLocal (); 223 } 224 225 226 public abstract Integer getUserId(); 227 228 231 public abstract String getEmail(); 232 233 236 public abstract String getFirstName(); 237 238 241 public abstract String getLastName(); 242 243 246 public abstract boolean hasPermission(String perm, Module module); 247 248 251 public boolean hasPermission(String perm, List modules) 252 { 253 boolean hasPerm = false; 254 if (modules != null && !modules.isEmpty()) 255 { 256 hasPerm = true; 257 Iterator i = modules.iterator(); 258 while (i.hasNext() && hasPerm) 259 { 260 hasPerm = hasPermission(perm, (Module)i.next()); 261 } 262 } 263 return hasPerm; 264 } 265 266 270 public String getName() 271 { 272 String first = getFirstName(); 273 String last = getLastName(); 274 int firstlength = 0; 275 int lastlength = 0; 276 if (first != null) 277 { 278 firstlength = first.length(); 279 } 280 if (last != null) 281 { 282 lastlength = last.length(); 283 } 284 StringBuffer sb = new StringBuffer (firstlength + lastlength + 1); 285 if (firstlength > 0) 286 { 287 sb.append(first); 288 if (lastlength > 0) 289 { 290 sb.append(' '); 291 } 292 } 293 if (lastlength > 0) 294 { 295 sb.append(last); 296 } 297 298 return sb.toString(); 299 } 300 301 304 public abstract List getModules() throws Exception ; 305 306 309 public abstract Module[] getModules(String permission) throws Exception ; 310 311 314 public abstract List getModules(boolean showDeletedModules) 315 throws Exception ; 316 317 320 public List getEditableModules() 321 throws Exception 322 { 323 return getEditableModules(null); 324 } 325 326 327 333 public List getCopyToModules(Module currentModule, String action, 334 String searchString) 335 throws Exception 336 { 337 List copyToModules = new ArrayList (); 338 if (hasPermission(ScarabSecurity.ISSUE__MOVE, currentModule) 339 || "copy".equals(action)) 340 { 341 Module[] userModules = getModules(ScarabSecurity.ISSUE__ENTER); 342 for (int i=0; i<userModules.length; i++) 343 { 344 Module module = userModules[i]; 345 if (!module.isGlobalModule() && 346 (searchString == null || searchString.equals("") || 347 module.getName().indexOf(searchString) != -1)) 348 { 349 copyToModules.add(module); 350 } 351 } 352 } 353 else if (hasPermission(ScarabSecurity.ISSUE__EDIT, currentModule) 354 && currentModule.getIssueTypes().size() > 1) 355 { 356 copyToModules.add(currentModule); 357 } 358 return copyToModules; 359 } 360 361 364 public List getCopyToModules(Module currentModule) 365 throws Exception 366 { 367 return getCopyToModules(currentModule, "copy", null); 368 } 369 370 373 public List getCopyToModules(Module currentModule, String action) 374 throws Exception 375 { 376 return getCopyToModules(currentModule, action, null); 377 } 378 379 382 public List getEditableModules(Module currEditModule) 383 throws Exception 384 { 385 List userModules = getModules(true); 386 List editModules = new ArrayList (); 387 388 if (currEditModule != null) 389 { 390 editModules.add(currEditModule.getParent()); 391 } 392 for (int i=0; i<userModules.size(); i++) 393 { 394 Module module = (Module)userModules.get(i); 395 Module parent = module.getParent(); 396 397 if (!editModules.contains(module) && parent != currEditModule) 398 { 399 if (hasPermission(ScarabSecurity.MODULE__EDIT, module)) 400 { 401 editModules.add(module); 402 } 403 } 404 } 405 if (currEditModule != null && editModules.contains(currEditModule)) 407 { 408 editModules.remove(currEditModule); 409 } 410 411 return editModules; 412 } 413 414 417 public List getRModuleUserAttributes(Module module, 418 IssueType issueType) 419 throws Exception 420 { 421 List result = null; 422 Object obj = ScarabCache.get(this, GET_R_MODULE_USERATTRIBUTES, 423 module, issueType); 424 if (obj == null) 425 { 426 Criteria crit = new Criteria() 427 .add(RModuleUserAttributePeer.USER_ID, getUserId()) 428 .add(RModuleUserAttributePeer.MODULE_ID, module.getModuleId()) 429 .add(RModuleUserAttributePeer.ISSUE_TYPE_ID, 430 issueType.getIssueTypeId()) 431 .addAscendingOrderByColumn( 432 RModuleUserAttributePeer.PREFERRED_ORDER); 433 434 result = getRModuleUserAttributes(crit); 435 ScarabCache.put(result, this, GET_R_MODULE_USERATTRIBUTES, 436 module, issueType); 437 } 438 else 439 { 440 result = (List )obj; 441 } 442 return result; 443 } 444 445 449 protected abstract List getRModuleUserAttributes(Criteria crit) 450 throws TorqueException; 451 452 455 public RModuleUserAttribute getRModuleUserAttribute(Module module, 456 Attribute attribute, 457 IssueType issueType) 458 throws Exception 459 { 460 RModuleUserAttribute result = null; 461 Object obj = ScarabCache.get(this, GET_R_MODULE_USERATTRIBUTE, 462 module, attribute, issueType); 463 if (obj == null) 464 { 465 Criteria crit = new Criteria(4) 466 .add(RModuleUserAttributePeer.USER_ID, getUserId()) 467 .add(RModuleUserAttributePeer.ATTRIBUTE_ID, 468 attribute.getAttributeId()) 469 .add(RModuleUserAttributePeer.LIST_ID, null); 470 if (module == null) 471 { 472 crit.add(RModuleUserAttributePeer.MODULE_ID, null); 473 } 474 else 475 { 476 crit.add(RModuleUserAttributePeer.MODULE_ID, 477 module.getModuleId()); 478 479 } 480 if (issueType == null) 481 { 482 crit.add(RModuleUserAttributePeer.ISSUE_TYPE_ID, null); 483 } 484 else 485 { 486 crit.add(RModuleUserAttributePeer.ISSUE_TYPE_ID, 487 issueType.getIssueTypeId()); 488 } 489 490 List muas = RModuleUserAttributePeer.doSelect(crit); 491 if (muas.size() == 1) 492 { 493 result = (RModuleUserAttribute)muas.get(0); 494 } 495 else if (muas.isEmpty()) 496 { 497 result = 498 getNewRModuleUserAttribute(attribute, module, issueType); 499 } 500 else 501 { 502 throw new ScarabException(L10NKeySet.ExceptionMultipleJDMs); 503 } 504 ScarabCache.put(result, this, GET_R_MODULE_USERATTRIBUTE, 505 module, attribute, issueType); 506 } 507 else 508 { 509 result = (RModuleUserAttribute)obj; 510 } 511 return result; 512 } 513 514 protected RModuleUserAttribute getNewRModuleUserAttribute( 515 Attribute attribute, Module module, IssueType issueType) 516 throws Exception 517 { 518 RModuleUserAttribute result = RModuleUserAttributeManager.getInstance(); 519 result.setUserId(getUserId()); 520 result.setAttributeId(attribute.getAttributeId()); 521 if (module != null) 522 { 523 result.setModuleId(module.getModuleId()); 524 } 525 if (issueType != null) 526 { 527 result.setIssueTypeId(issueType.getIssueTypeId()); 528 } 529 return result; 530 } 531 532 535 public Issue getReportingIssue(String key) 536 { 537 return (Issue)issueMap.get(key); 538 } 539 540 541 544 public String setReportingIssue(Issue issue) 545 throws ScarabException 546 { 547 String key = null; 548 if (issue == null) 549 { 550 throw new ScarabException(L10NKeySet.ExceptionNullIssueForbidden); 551 } 552 else 553 { 554 key = String.valueOf(issueCount++); 555 setReportingIssue(key, issue); 556 } 557 return key; 558 } 559 560 561 564 public void setReportingIssue(String key, Issue issue) 565 { 566 if (issue == null) 567 { 568 issueMap.remove(key); 569 } 570 else 571 { 572 try 573 { 574 if (issueMap.size() >= MAX_INDEPENDENT_WINDOWS) 575 { 576 int intKey = Integer.parseInt(key); 579 int count = 0; 580 for (int i=intKey-1; i>=0; i--) 581 { 582 String testKey = String.valueOf(i); 583 if (getReportingIssue(testKey) != null) 584 { 585 if (++count >= MAX_INDEPENDENT_WINDOWS) 586 { 587 issueMap.remove(testKey); 588 } 589 } 590 } 591 } 592 } 593 catch (Exception e) 594 { 595 Log.get().error("Nonfatal error clearing old issues. " 596 + "This could be a memory leak.", e); 597 } 598 599 issueMap.put(key, issue); 600 } 601 } 602 603 604 605 608 public ReportBridge getCurrentReport(String key) 609 { 610 return (ReportBridge)reportMap.get(key); 611 } 612 613 614 617 public String setCurrentReport(ReportBridge report) 618 throws ScarabException 619 { 620 String key = null; 621 if (report == null) 622 { 623 throw new ScarabException(L10NKeySet.ExceptionNullReportForbidden); 624 } 625 else 626 { 627 key = String.valueOf(reportCount++); 628 setCurrentReport(key, report); 629 } 630 return key; 631 } 632 633 634 637 public void setCurrentReport(String key, ReportBridge report) 638 { 639 if (report == null) 640 { 641 reportMap.remove(key); 642 } 643 else 644 { 645 try 646 { 647 if (reportMap.size() >= MAX_INDEPENDENT_WINDOWS) 648 { 649 int intKey = Integer.parseInt(key); 652 int count = 0; 653 for (int i=intKey-1; i>=0; i--) 654 { 655 String testKey = String.valueOf(i); 656 if (getCurrentReport(testKey) != null) 657 { 658 if (++count >= MAX_INDEPENDENT_WINDOWS) 659 { 660 reportMap.remove(testKey); 661 } 662 } 663 } 664 } 665 } 666 catch (Exception e) 667 { 668 Log.get().error("Nonfatal error clearing old reports. " 669 + "This could be a memory leak.", e); 670 } 671 672 reportMap.put(String.valueOf(key), report); 673 } 674 } 675 676 677 681 public void save() throws Exception 682 { 683 throw new UnsupportedOperationException ("Not implemented"); } 685 686 690 public void save(String dbName) throws Exception 691 { 692 throw new UnsupportedOperationException ("Not implemented"); } 694 695 699 public void save(Connection dbCon) throws Exception 700 { 701 throw new UnsupportedOperationException ("Not implemented"); } 703 704 710 public int getEnterIssueRedirect() 711 throws TorqueException 712 { 713 if (enterIssueRedirect == 0) 714 { 715 UserPreference up = UserPreferenceManager.getInstance(getUserId()); 716 if (up != null && up.getEnterIssueRedirect() != 0) 717 { 718 enterIssueRedirect = up.getEnterIssueRedirect(); 719 } 720 } 721 return enterIssueRedirect; 722 } 723 724 725 731 public void setEnterIssueRedirect(int templateCode) 732 throws Exception 733 { 734 UserPreference up = UserPreferenceManager.getInstance(getUserId()); 735 up.setEnterIssueRedirect(templateCode); 736 up.save(); 737 enterIssueRedirect = templateCode; 738 } 739 740 743 public String getHomePage() 744 throws Exception 745 { 746 return getHomePage(getCurrentModule()); 747 } 748 749 752 public String getHomePage(Module module) 753 { 754 String homePage = null; 755 try 756 { 757 Integer uid = getUserId(); 761 if (uid != null) 762 { 763 UserPreference up = UserPreferenceManager.getInstance(uid); 764 homePage = up.getHomePage(); 765 766 if ("query".equals(homePage)) 767 { 768 homePage = getQueryTarget(); 769 } 770 else if (homePage != null && 772 (homePage.endsWith("ModuleQuery.vm") || 773 homePage.endsWith("XModuleList.vm"))) 774 { 775 homePage = getQueryTarget(); 776 } 777 778 int i = 0; 779 while (homePage == null || !isHomePageValid(homePage, module)) 780 { 781 homePage = HOME_PAGES[i++]; 782 if ("query".equals(homePage)) 783 { 784 homePage = getQueryTarget(); 785 } 786 } 787 } 788 } 789 catch (Exception e) 790 { 791 Log.get().warn("Error determining user homepage", e); 792 } 793 return (homePage != null ? homePage : "Index.vm"); 794 } 795 796 800 private boolean isHomePageValid(String homePage, Module module) 801 { 802 boolean result = true; 803 String perm = ScarabSecurity 804 .getScreenPermission(homePage.replace(',','.')); 805 if (perm != null && !hasPermission(perm, module)) 806 { 807 result = false; 808 } 809 return result; 810 } 811 812 815 public void setHomePage(String homePage) 816 throws Exception 817 { 818 if ("ModuleNotReady.vm".equals(homePage)) 819 { 820 throw new ScarabException(L10NKeySet.ExceptionForbiddenHomeModuleNotReady); 821 } 822 UserPreference up = UserPreferenceManager.getInstance(getUserId()); 823 up.setHomePage(homePage); 824 up.save(); 825 } 826 827 private final Map queryTargetMap = new HashMap (); 829 832 public String getQueryTarget() 833 { 834 MITList mitlist = getCurrentMITList(); 835 String target = null; 836 if (mitlist == null) 837 { 838 target = "IssueTypeList.vm"; 839 } 840 else if (mitlist.isSingleModuleIssueType()) 841 { 842 try 843 { 844 Integer issueTypeId = mitlist.getIssueType().getIssueTypeId(); 845 target = (String )queryTargetMap.get(issueTypeId); 846 } 847 catch (Exception e) 848 { 849 Log.get().warn("Could not determine query target.", e); 850 } 851 852 if (target == null) 853 { 854 target = "Search.vm"; 855 } 856 } 857 else 858 { 859 target = "AdvancedQuery.vm"; 860 } 861 return target; 862 } 863 864 867 public void setSingleIssueTypeQueryTarget(IssueType type, String target) 868 { 869 queryTargetMap.put(type.getIssueTypeId(), target); 870 } 871 872 878 public List getMITLists() 879 throws TorqueException 880 { 881 List result = null; 882 883 Criteria crit = new Criteria(); 884 crit.add(MITListPeer.ACTIVE, true); 885 Criteria.Criterion userCrit = crit.getNewCriterion( 886 MITListPeer.USER_ID, getUserId(), Criteria.EQUAL); 887 userCrit.or(crit.getNewCriterion( 888 MITListPeer.USER_ID, null, Criteria.EQUAL)); 889 crit.add(userCrit); 890 crit.add(MITListPeer.MODIFIABLE, true); 891 crit.add(MITListPeer.ACTIVE, true); 892 crit.add(MITListPeer.NAME, (Object )null, Criteria.NOT_EQUAL); 893 crit.addAscendingOrderByColumn(MITListPeer.NAME); 894 result = MITListPeer.doSelect(crit); 895 896 return result; 897 } 898 899 900 903 public boolean hasAnySearchableRMITs() 904 throws Exception 905 { 906 boolean result = false; 907 List moduleIds = getSearchableModuleIds(); 908 if (!moduleIds.isEmpty()) 909 { 910 Criteria crit = new Criteria(); 911 crit.addIn(RModuleIssueTypePeer.MODULE_ID, moduleIds); 912 result = (RModuleIssueTypePeer.count(crit) > 0); 913 } 914 return result; 915 } 916 917 private List getSearchableModuleIds() 918 throws Exception 919 { 920 Module[] userModules = getModules(ScarabSecurity.ISSUE__SEARCH); 921 List moduleIds; 922 if (userModules != null && (userModules.length > 1 || 923 userModules.length == 1 && !userModules[0].isGlobalModule()) 924 ) 925 { 926 moduleIds = new ArrayList (userModules.length); 927 for (int i=0; i<userModules.length; i++) 928 { 929 Module module = userModules[i]; 930 if (!module.isGlobalModule()) 931 { 932 moduleIds.add(module.getModuleId()); 933 } 934 } 935 } 936 else 937 { 938 moduleIds = Collections.EMPTY_LIST; 939 } 940 return moduleIds; 941 } 942 943 944 947 public List getUnusedRModuleIssueTypes(Module module) 948 throws Exception 949 { 950 Criteria crit = new Criteria(); 951 crit.add(RModuleIssueTypePeer.MODULE_ID, module.getModuleId()) 952 .addJoin(RModuleIssueTypePeer.ISSUE_TYPE_ID, 953 IssueTypePeer.ISSUE_TYPE_ID) 954 .add(IssueTypePeer.PARENT_ID, 0) 955 .add(IssueTypePeer.DELETED, false); 956 addCurrentMITListExclusion(crit); 957 return RModuleIssueTypePeer.doSelect(crit); 958 } 959 960 private void addCurrentMITListExclusion(Criteria crit) 961 throws Exception 962 { 963 MITList mitList = getCurrentMITList(getGenThreadKey()); 965 if (mitList != null && mitList.getMITListItems() != null 966 && !mitList.getMITListItems().isEmpty()) 967 { 968 boolean addAnd = false; 969 StringBuffer sb = new StringBuffer (); 970 Iterator mitItems = 971 mitList.getExpandedMITListItems().iterator(); 972 while (mitItems.hasNext()) 973 { 974 MITListItem item = (MITListItem)mitItems.next(); 975 if (mitList.getModule(item) != null 976 && item.getIssueType() != null) 977 { 978 if (addAnd) 979 { 980 sb.append(" AND "); 981 } 982 983 sb.append(" NOT (") 984 .append(RModuleIssueTypePeer.MODULE_ID) 985 .append('=') 986 .append(mitList.getModule(item).getModuleId()) 987 .append(" AND ") 988 .append(RModuleIssueTypePeer.ISSUE_TYPE_ID) 989 .append('=') 990 .append(item.getIssueType().getIssueTypeId()) 991 .append(')'); 992 addAnd = true; 993 } 994 } 995 crit.add(IssueTypePeer.ISSUE_TYPE_ID, 997 (Object )sb.toString(), Criteria.CUSTOM); 998 } 999 } 1000 1001 1006 public List getSearchableRMITs(String searchField, String searchString, 1007 String sortColumn, String sortPolarity, 1008 Module skipModule) 1009 throws Exception 1010 { 1011 List moduleIds = getSearchableModuleIds(); 1012 if (skipModule != null) 1013 { 1014 moduleIds.remove(skipModule.getModuleId()); 1015 } 1016 1017 List result; 1018 if (moduleIds.isEmpty()) 1019 { 1020 result = Collections.EMPTY_LIST; 1021 } 1022 else 1023 { 1024 Criteria crit = new Criteria(); 1025 crit.addIn(RModuleIssueTypePeer.MODULE_ID, moduleIds); 1026 crit.addJoin(RModuleIssueTypePeer.ISSUE_TYPE_ID, 1027 IssueTypePeer.ISSUE_TYPE_ID); 1028 crit.add(IssueTypePeer.PARENT_ID, 0); 1029 crit.add(IssueTypePeer.DELETED, false); 1030 addCurrentMITListExclusion(crit); 1031 1032 1039 result = RModuleIssueTypePeer.doSelect(crit); 1040 filterRMITList(result, searchField, searchString); 1041 sortRMITList(result, sortColumn, sortPolarity); 1042 } 1043 1044 return result; 1045 } 1046 1047 1050 protected void filterRMITList(List rmits, 1051 String searchField, String searchString) 1052 throws Exception 1053 { 1054 String moduleName = null; 1055 String issueTypeName = null; 1056 if ("issuetype".equals(searchField)) 1057 { 1058 issueTypeName = searchString; 1059 } 1060 else 1061 { 1062 moduleName = searchString; 1063 } 1064 1065 if (moduleName != null && moduleName.length() > 0) 1066 { 1067 for (int i=rmits.size()-1; i>=0; i--) 1068 { 1069 String name = ((RModuleIssueType)rmits.get(i)) 1070 .getModule().getRealName(); 1071 if (name == null || name.indexOf(moduleName) == -1) 1072 { 1073 rmits.remove(i); 1074 } 1075 } 1076 } 1077 if (issueTypeName != null && issueTypeName.length() > 0) 1078 { 1079 for (int i=rmits.size()-1; i>=0; i--) 1080 { 1081 String name = ((RModuleIssueType)rmits.get(i)) 1082 .getDisplayName(); 1083 if (name == null || name.indexOf(issueTypeName) == -1) 1084 { 1085 rmits.remove(i); 1086 } 1087 } 1088 } 1089 } 1090 1091 1094 protected void sortRMITList(List rmits, 1095 final String sortColumn, String sortPolarity) 1096 throws Exception 1097 { 1098 final int polarity = ("desc".equals(sortPolarity)) ? -1 : 1; 1099 Comparator c = new Comparator () 1100 { 1101 public int compare(Object o1, Object o2) 1102 { 1103 int i = 0; 1104 if (sortColumn != null && sortColumn.equals("issuetype")) 1105 { 1106 i = polarity * ((RModuleIssueType)o1).getDisplayName() 1107 .compareTo(((RModuleIssueType)o2).getDisplayName()); 1108 } 1109 else 1110 { 1111 try 1112 { 1113 i = polarity * 1114 ((RModuleIssueType)o1).getModule().getRealName() 1115 .compareTo(((RModuleIssueType)o2).getModule() 1116 .getRealName()); 1117 } 1118 catch (TorqueException e) 1119 { 1120 Log.get().error("Unable to sort on module names", e); 1121 } 1122 } 1123 return i; 1124 } 1125 }; 1126 Collections.sort(rmits, c); 1127 } 1128 1129 1130 public void addRMITsToCurrentMITList(List rmits) 1131 throws TorqueException 1132 { 1133 if (rmits != null && !rmits.isEmpty()) 1134 { 1135 MITList mitList = getCurrentMITList(getGenThreadKey()); 1136 if (mitList == null) 1137 { 1138 mitList = MITListManager.getInstance(); 1139 setCurrentMITList(mitList); 1140 } 1141 1142 Iterator i = rmits.iterator(); 1143 while (i.hasNext()) 1144 { 1145 RModuleIssueType rmit = (RModuleIssueType)i.next(); 1146 MITListItem item = MITListItemManager.getInstance(); 1147 item.setModuleId(rmit.getModuleId()); 1148 item.setIssueTypeId(rmit.getIssueTypeId()); 1149 if (!mitList.contains(item)) 1150 { 1151 mitList.addMITListItem(item); 1152 } 1153 } 1154 } 1155 } 1156 1157 private Object getGenThreadKey() 1158 { 1159 Object key = threadKey.get(); 1160 if (key == null) 1161 { 1162 key = getNewThreadKey(); 1163 setThreadKey((Integer )key); 1164 } 1165 return key; 1166 } 1167 1168 private synchronized Object getNewThreadKey() 1169 { 1170 Integer key = new Integer (threadCount++); 1173 activeKeys.put(key, null); 1174 Integer testKey = new Integer (key.intValue()-10); 1177 invalidateKey(testKey); 1178 return key; 1179 } 1180 1181 private void invalidateKey(Object key) 1182 { 1183 activeKeys.remove(key); 1184 mitListMap.remove(key); 1185 enterIssueMap.remove(key); 1186 } 1187 1188 1191 public Object getThreadKey() 1192 { 1193 return threadKey.get(); 1194 } 1195 1196 1199 public void setThreadKey(Integer key) 1200 { 1201 if (activeKeys.containsKey(key)) 1202 { 1203 threadKey.set(key); 1204 } 1205 } 1206 1207 public MITList getCurrentMITList() 1208 { 1209 return getCurrentMITList(getGenThreadKey()); 1210 } 1211 private MITList getCurrentMITList(Object key) 1212 { 1213 Log.get().debug("Getting mitlist for key " + key); 1214 return (MITList)mitListMap.get(key); 1215 } 1216 1217 1220 public void setCurrentMITList(MITList list) 1221 { 1222 if (list != null) 1223 { 1224 setCurrentMITList(getGenThreadKey(), list); 1225 } 1226 else if (getThreadKey() != null) 1227 { 1228 setCurrentMITList(getThreadKey(), list); 1229 } 1230 } 1231 private void setCurrentMITList(Object key, MITList list) 1232 { 1233 if (list == null) 1234 { 1235 mitListMap.remove(key); 1236 } 1237 else 1238 { 1239 try 1240 { 1241 if (mitListMap.size() >= MAX_INDEPENDENT_WINDOWS) 1242 { 1243 int intKey = Integer.parseInt(String.valueOf(key)); 1246 int count = 0; 1247 for (int i=intKey-1; i>=0; i--) 1248 { 1249 String testKey = String.valueOf(i); 1250 if (getCurrentMITList(testKey) != null) 1251 { 1252 if (++count >= MAX_INDEPENDENT_WINDOWS) 1253 { 1254 mitListMap.remove(testKey); 1255 } 1256 } 1257 } 1258 } 1259 } 1260 catch (Exception e) 1261 { 1262 Log.get().error("Nonfatal error clearing old MIT lists. " 1263 + "This could be a memory leak.", e); 1264 } 1265 Log.get().debug("Set mitList for key " + key + " to " + list); 1266 1267 mitListMap.put(key, list); 1268 } 1269 } 1270 1271 public void removeItemsFromCurrentMITList(String [] ids) 1272 { 1273 MITList mitList = getCurrentMITList(getGenThreadKey()); 1274 if (mitList != null && !mitList.isEmpty() 1275 && ids != null && ids.length > 0) 1276 { 1277 for (int i=0; i<ids.length; i++) 1278 { 1279 Iterator iter = mitList.iterator(); 1280 while (iter.hasNext()) 1281 { 1282 MITListItem item = (MITListItem)iter.next(); 1283 if (item.getQueryKey().equals(ids[i])) 1284 { 1285 iter.remove(); 1286 mitList.scheduleItemForDeletion(item); 1287 continue; 1288 } 1289 } 1290 1291 } 1292 } 1293 } 1294 1295 1298 public boolean hasMostRecentQuery() 1299 { 1300 return hasMostRecentQuery(getGenThreadKey()); 1301 } 1302 private boolean hasMostRecentQuery(Object key) 1303 { 1304 return mostRecentQueryMap.get(key) != null; 1305 } 1306 1307 1310 public String getMostRecentQuery() 1311 { 1312 return getMostRecentQuery(getGenThreadKey()); 1313 } 1314 private String getMostRecentQuery(Object key) 1315 { 1316 setCurrentMITList(key, (MITList)mostRecentQueryMITMap.get(key)); 1317 return (String )mostRecentQueryMap.get(key); 1318 } 1319 1320 1323 public void setMostRecentQuery(String queryString) 1324 { 1325 if (queryString != null) 1326 { 1327 setMostRecentQuery(getGenThreadKey(), queryString); 1328 } 1329 else if (getThreadKey() != null) 1330 { 1331 setMostRecentQuery(getThreadKey(), null); 1332 } 1333 } 1334 private void setMostRecentQuery(Object key, String queryString) 1335 { 1336 if (queryString == null) 1337 { 1338 mostRecentQueryMap.remove(key); 1339 mostRecentQueryMITMap.remove(key); 1340 } 1341 else 1342 { 1343 try 1344 { 1345 if (mostRecentQueryMap.size() >= MAX_INDEPENDENT_WINDOWS) 1346 { 1347 int intKey = Integer.parseInt(String.valueOf(key)); 1350 int count = 0; 1351 for (int i=intKey-1; i>=0; i--) 1352 { 1353 String testKey = String.valueOf(i); 1354 if (getMostRecentQuery(testKey) != null) 1355 { 1356 if (++count >= MAX_INDEPENDENT_WINDOWS) 1357 { 1358 mostRecentQueryMap.remove(testKey); 1359 mostRecentQueryMITMap.remove(testKey); 1360 } 1361 } 1362 } 1363 } 1364 } 1365 catch (Exception e) 1366 { 1367 Log.get().error("Nonfatal error clearing old queries. " 1368 + "This could be a memory leak.", e); 1369 } 1370 MITList list = getCurrentMITList(key); 1371 if (list != null) 1372 { 1373 mostRecentQueryMITMap.put(key, list); 1374 mostRecentQueryMap.put(key, queryString); 1375 } 1376 else 1377 { 1378 Log.get().warn( 1379 "Tried to set most recent query without any mitlist."); 1380 } 1381 } 1382 } 1383 1384 1385 public Object lastEnteredIssueTypeOrTemplate() 1386 { 1387 return lastEnteredIssueTypeOrTemplate(getGenThreadKey()); 1388 } 1389 private Object lastEnteredIssueTypeOrTemplate(Object key) 1390 { 1391 Log.get().debug("Getting last entered type for key " + key); 1392 return enterIssueMap.get(key); 1393 } 1394 1395 1396 1399 public void setLastEnteredIssueType(IssueType type) 1400 { 1401 setLastEnteredIssueTypeOrTemplate(type); 1402 } 1403 1406 public void setLastEnteredTemplate(Issue template) 1407 { 1408 setLastEnteredIssueTypeOrTemplate(template); 1409 } 1410 1413 private void setLastEnteredIssueTypeOrTemplate(Object obj) 1414 { 1415 if (obj != null) 1416 { 1417 setLastEnteredIssueTypeOrTemplate(getGenThreadKey(), obj); 1418 } 1419 else if (getThreadKey() != null) 1420 { 1421 setLastEnteredIssueTypeOrTemplate(getThreadKey(), null); 1422 } 1423 } 1424 private void setLastEnteredIssueTypeOrTemplate(Object key, Object obj) 1425 { 1426 if (obj == null) 1427 { 1428 enterIssueMap.remove(key); 1429 } 1430 else 1431 { 1432 try 1433 { 1434 if (enterIssueMap.size() >= MAX_INDEPENDENT_WINDOWS) 1435 { 1436 int intKey = Integer.parseInt(String.valueOf(key)); 1439 int count = 0; 1440 for (int i=intKey-1; i>=0; i--) 1441 { 1442 String testKey = String.valueOf(i); 1443 if (lastEnteredIssueTypeOrTemplate(testKey) != null) 1444 { 1445 if (++count >= MAX_INDEPENDENT_WINDOWS) 1446 { 1447 enterIssueMap.remove(testKey); 1448 } 1449 } 1450 } 1451 } 1452 } 1453 catch (Exception e) 1454 { 1455 Log.get().error("Nonfatal error clearing entered issue types. " 1456 + "This could be a memory leak.", e); 1457 } 1458 Log.get().debug("Set issue type for key " + key + " to " + obj); 1459 1460 enterIssueMap.put(key, obj); 1461 } 1462 } 1463 1464 1465 private void setUsersMap(Map map, Map users) 1466 throws Exception 1467 { 1468 Object key = (users != null ? getGenThreadKey() : getThreadKey()); 1469 if (key == null) 1470 { 1471 return; 1473 } 1474 1475 if (users != null && users.size() >= MAX_INDEPENDENT_WINDOWS) 1476 { 1477 try 1478 { 1479 int intKey = Integer.parseInt(String.valueOf(key)); 1481 int count = 0; 1482 for (int i = intKey - 1; i >= 0; i--) 1483 { 1484 String testKey = String.valueOf(i); 1485 if (map.get(testKey) != null) 1486 { 1487 if (++count >= MAX_INDEPENDENT_WINDOWS) 1488 { 1489 users.remove(testKey); 1490 } 1491 } 1492 } 1493 } 1494 catch (Exception e) 1495 { 1496 Log.get().warn("Error possibly resulting in memory leak", e); 1498 } 1499 } 1500 1501 map.put(key, users); 1502 } 1503 1504 1507 public Map getAssociatedUsersMap() 1508 throws Exception 1509 { 1510 return (Map ) associatedUsersMap.get(getGenThreadKey()); 1511 } 1512 1513 1516 public void setAssociatedUsersMap(Map associatedUsers) 1517 throws Exception 1518 { 1519 setUsersMap(associatedUsersMap, associatedUsers); 1520 } 1521 1522 1525 public Map getSelectedUsersMap() 1526 throws Exception 1527 { 1528 return (Map ) selectedUsersMap.get(getGenThreadKey()); 1529 } 1530 1531 1534 public void setSelectedUsersMap(Map selectedUsers) 1535 throws Exception 1536 { 1537 setUsersMap(selectedUsersMap, selectedUsers); 1538 } 1539 1540 1543 public Module getCurrentModule() 1544 { 1545 return (Module)currentModule.get(); 1546 } 1547 1548 1551 public void setCurrentModule(Module v) 1552 { 1553 this.currentModule.set(v); 1554 } 1555 1556 1559 public IssueType getCurrentIssueType() 1560 throws Exception 1561 { 1562 return (IssueType)currentIssueType.get(); 1563 } 1564 1565 1568 public void setCurrentIssueType(IssueType v) 1569 { 1570 this.currentIssueType.set(v); 1571 } 1572 1573 1576 public RModuleIssueType getCurrentRModuleIssueType() 1577 throws Exception 1578 { 1579 RModuleIssueType rmit = null; 1580 Module module = getCurrentModule(); 1581 if (module != null) 1582 { 1583 IssueType it = getCurrentIssueType(); 1584 if (it != null) 1585 { 1586 rmit = module.getRModuleIssueType(it); 1587 } 1588 } 1589 1590 return rmit; 1591 } 1592 1593 1594 1597 public void updateIssueListAttributes(List attributes) 1598 throws Exception 1599 { 1600 MITList mitList = getCurrentMITList(); 1601 1602 for (Iterator currentAttributes = mitList.getSavedRMUAs().iterator(); 1604 currentAttributes.hasNext();) 1605 { 1606 deleteRModuleUserAttribute( 1607 (RModuleUserAttribute)currentAttributes.next()); 1608 } 1609 1610 int i = 1; 1611 for (Iterator iter = attributes.iterator(); iter.hasNext();) 1612 { 1613 Attribute attribute = (Attribute)iter.next(); 1614 RModuleUserAttribute rmua = 1615 mitList.getNewRModuleUserAttribute(attribute); 1616 rmua.setOrder(i++); 1617 rmua.save(); 1618 } 1619 } 1620 1621 protected abstract void 1622 deleteRModuleUserAttribute(RModuleUserAttribute rmua) 1623 throws Exception ; 1624 1625 1626 1629 public String getStats() 1630 { 1631 return " IssueMap=" + issueMap.size() 1632 + "; ReportMap=" + reportMap.size() 1633 + "; MITListMap=" + mitListMap.size() 1634 + "; MostRecentQueryMap=" + mostRecentQueryMap.size() 1635 + "; MostRecentQueryMITMap=" + mostRecentQueryMITMap.size() 1636 + "; EnterIssueMap=" + enterIssueMap.size(); 1637 } 1638 1639 1642 public void setLocale(Locale newLocale) 1643 { 1644 locale = newLocale; 1645 } 1646 1647 1650 public Locale getLocale() 1651 { 1652 if (locale == null) 1653 { 1654 locale = getPreferredLocale(); 1655 } 1656 return locale; 1657 } 1658 1659 1663 public Locale getPreferredLocale() 1664 { 1665 Locale result; 1666 try 1667 { 1668 UserPreference up = 1669 UserPreferenceManager.getInstance(getUserId()); 1670 result = Localization.getLocale(up.getLocale()); 1671 } 1672 catch (Exception e) 1673 { 1674 result = ScarabConstants.DEFAULT_LOCALE; 1678 Log.get().warn( 1679 "AbstractScarabUser.getLocale() could not " 1680 + "retrieve locale for user id=" 1681 + getUserId() 1682 + "; Error message: " 1683 + e.getMessage()); 1684 } 1685 return result; 1686 } 1687 1688 1689 1692 public boolean isShowOtherModulesInIssueTypeList() 1693 { 1694 return showOtherModulesInIssueTypeList; 1695 } 1696 1697 1700 public void setShowOtherModulesInIssueTypeList( 1701 boolean newShowOtherModulesInIssueTypeList) 1702 { 1703 this.showOtherModulesInIssueTypeList = 1704 newShowOtherModulesInIssueTypeList; 1705 } 1706} 1707 | Popular Tags |