1 18 19 package cowsultants.itracker.ejb.client.util; 20 21 import java.text.SimpleDateFormat ; 22 import java.util.*; 23 24 import cowsultants.itracker.ejb.client.models.*; 25 import cowsultants.itracker.ejb.client.resources.*; 26 27 30 public class IssueUtilities { 31 public static final int STATUS_NEW = 100; 32 public static final int STATUS_UNASSIGNED = 200; 33 public static final int STATUS_ASSIGNED = 300; 34 public static final int STATUS_RESOLVED = 400; 35 public static final int STATUS_CLOSED = 500; 36 37 public static final int STATUS_END = 600; 40 41 public static final int ACTIVITY_ISSUE_CREATED = 1; 42 public static final int ACTIVITY_STATUS_CHANGE = 2; 43 public static final int ACTIVITY_OWNER_CHANGE = 3; 44 public static final int ACTIVITY_SEVERITY_CHANGE = 4; 45 public static final int ACTIVITY_COMPONENTS_MODIFIED = 5; 46 public static final int ACTIVITY_VERSIONS_MODIFIED = 6; 47 public static final int ACTIVITY_REMOVE_HISTORY = 7; 48 public static final int ACTIVITY_ISSUE_MOVE = 8; 49 public static final int ACTIVITY_SYSTEM_UPDATE = 9; 50 public static final int ACTIVITY_TARGETVERSION_CHANGE = 10; 51 public static final int ACTIVITY_DESCRIPTION_CHANGE = 11; 52 public static final int ACTIVITY_RESOLUTION_CHANGE = 12; 53 54 public static final int HISTORY_STATUS_REMOVED = -1; 55 public static final int HISTORY_STATUS_AVAILABLE = 1; 56 57 private static ConfigurationModel[] resolutions = new ConfigurationModel[0]; 58 private static ConfigurationModel[] severities = new ConfigurationModel[0]; 59 private static ConfigurationModel[] statuses = new ConfigurationModel[0]; 60 private static CustomFieldModel[] customFields = new CustomFieldModel[0]; 61 62 public IssueUtilities() { 63 } 64 65 public static String componentsToString(IssueModel issue) { 66 String value = ""; 67 if(issue != null && issue.getComponents().length > 0) { 68 for(int i = 0; i < issue.getComponents().length; i++) { 69 value += (i != 0 ? ", " : "") + issue.getComponents()[i].getName(); 70 } 71 } 72 return value; 73 } 74 75 public static String versionsToString(IssueModel issue) { 76 String value = ""; 77 if(issue != null && issue.getVersions().length > 0) { 78 for(int i = 0; i < issue.getVersions().length; i++) { 79 value += (i != 0 ? ", " : "") + issue.getVersions()[i].getNumber(); 80 } 81 } 82 return value; 83 } 84 85 public static String historyToString(IssueModel issue, SimpleDateFormat sdf) { 86 String value = ""; 87 if(issue != null && issue.getHistory().length > 0 && sdf != null) { 88 for(int i = 0; i < issue.getHistory().length; i++) { 89 value += (i != 0 ? "," : "") + issue.getHistory()[i].getDescription() + "," + issue.getHistory()[i].getUserFirstName(); 90 value += " " + issue.getHistory()[i].getUserLastName() + "," + sdf.format(issue.getHistory()[i].getLastModifiedDate()); 91 } 92 } 93 return value; 94 } 95 96 public static String getStatusName(int value) { 97 return getStatusName(value, ITrackerResources.getLocale()); 98 } 99 100 public static String getStatusName(int value, Locale locale) { 101 return getStatusName(Integer.toString(value), locale); 102 } 103 104 public static String getStatusName(String value, Locale locale) { 105 return ITrackerResources.getString(ITrackerResources.KEY_BASE_STATUS + value, locale); 106 } 107 108 public static ConfigurationModel[] getStatuses() { 109 return statuses; 110 } 111 112 public static NameValuePairModel[] getStatuses(Locale locale) { 113 NameValuePairModel[] statusStrings = new NameValuePairModel[statuses.length]; 114 for(int i = 0; i < statuses.length; i++) { 115 statusStrings[i] = new NameValuePairModel(ITrackerResources.getString(ITrackerResources.KEY_BASE_STATUS + statuses[i].getValue(), locale), statuses[i].getValue()); 116 } 117 return statusStrings; 118 } 119 120 public static void setStatuses(ConfigurationModel[] value) { 121 statuses = (value == null ? new ConfigurationModel[0] : value); 122 } 123 124 public static int getNumberStatuses() { 125 return statuses.length; 126 } 127 128 public static String getSeverityName(int value) { 129 return getSeverityName(value, ITrackerResources.getLocale()); 130 } 131 132 public static String getSeverityName(int value, Locale locale) { 133 return getSeverityName(Integer.toString(value), locale); 134 } 135 136 public static String getSeverityName(String value, Locale locale) { 137 return ITrackerResources.getString(ITrackerResources.KEY_BASE_SEVERITY + value, locale); 138 } 139 140 147 public static NameValuePairModel[] getSeverities(Locale locale) { 148 NameValuePairModel[] severityStrings = new NameValuePairModel[severities.length]; 149 for(int i = 0; i < severities.length; i++) { 150 severityStrings[i] = new NameValuePairModel(ITrackerResources.getString(ITrackerResources.KEY_BASE_SEVERITY + severities[i].getValue(), locale), severities[i].getValue()); 151 } 152 return severityStrings; 153 } 154 155 public static void setSeverities(ConfigurationModel[] value) { 156 severities = (value == null ? new ConfigurationModel[0] : value); 157 } 158 159 public static int getNumberSeverities() { 160 return severities.length; 161 } 162 163 171 public static int compareSeverity(IssueModel ma, IssueModel mb) { 172 if(ma == null && mb == null) { 173 return 0; 174 } else if(ma == null && mb != null) { 175 return -1; 176 } else if(ma != null && mb == null) { 177 return 1; 178 } else { 179 int maIndex = Integer.MAX_VALUE; 180 int mbIndex = Integer.MAX_VALUE; 181 for(int i = 0; i < severities.length; i++) { 182 if(severities[i] != null) { 183 if(severities[i].getValue().equalsIgnoreCase(Integer.toString(ma.getSeverity()))) { 184 maIndex = i; 185 } 186 if(severities[i].getValue().equalsIgnoreCase(Integer.toString(mb.getSeverity()))) { 187 mbIndex = i; 188 } 189 } 190 } 191 if(maIndex > mbIndex) { 192 return -1; 193 } else if(maIndex < mbIndex) { 194 return 1; 195 } 196 } 197 198 return 0; 199 } 200 201 public static String getResolutionName(int value) { 202 return getResolutionName(value, ITrackerResources.getLocale()); 203 } 204 205 public static String getResolutionName(int value, Locale locale) { 206 return getResolutionName(Integer.toString(value), locale); 207 } 208 209 public static String getResolutionName(String value, Locale locale) { 210 return ITrackerResources.getString(ITrackerResources.KEY_BASE_RESOLUTION + value, locale); 211 } 212 213 public static String checkResolutionName(String value, Locale locale) throws MissingResourceException { 214 return ITrackerResources.getCheckForKey(ITrackerResources.KEY_BASE_RESOLUTION + value, locale); 215 } 216 217 224 public static NameValuePairModel[] getResolutions(Locale locale) { 225 NameValuePairModel[] resolutionStrings = new NameValuePairModel[resolutions.length]; 226 for(int i = 0; i < resolutions.length; i++) { 227 resolutionStrings[i] = new NameValuePairModel(ITrackerResources.getString(ITrackerResources.KEY_BASE_RESOLUTION + resolutions[i].getValue(), locale), resolutions[i].getValue()); 228 } 229 return resolutionStrings; 230 } 231 232 235 public static void setResolutions(ConfigurationModel[] value) { 236 resolutions = (value == null ? new ConfigurationModel[0] : value); 237 } 238 239 public static String getActivityName(int value) { 240 return getActivityName(value, ITrackerResources.getLocale()); 241 } 242 243 public static String getActivityName(int value, Locale locale) { 244 return ITrackerResources.getString("itracker.activity." + value, locale); 245 } 246 247 251 public static CustomFieldModel[] getCustomFields() { 252 return (customFields == null ? new CustomFieldModel[0] : customFields); 253 } 254 255 259 public static void setCustomFields(CustomFieldModel[] value) { 260 customFields = (value == null ? new CustomFieldModel[0] : value); 261 } 262 263 270 public static CustomFieldModel[] getCustomFields(Locale locale) { 271 CustomFieldModel[] localizedFields = new CustomFieldModel[customFields.length]; 272 for(int i = 0; i < customFields.length; i++) { 273 try { 274 localizedFields[i] = (CustomFieldModel) customFields[i].clone(); 275 if(localizedFields[i] != null) { 276 localizedFields[i].setLabels(locale); 277 } 278 } catch(CloneNotSupportedException cnse) { 279 Logger.logError("Error cloning CustomFieldModel: " + cnse.getMessage()); 280 } 281 } 282 return localizedFields; 283 } 284 285 291 public static CustomFieldModel getCustomField(Integer id) { 292 return getCustomField(id, ITrackerResources.getLocale()); 293 } 294 295 302 public static CustomFieldModel getCustomField(Integer id, Locale locale) { 303 CustomFieldModel retField = null; 304 305 try { 306 for(int i = 0; i < customFields.length; i++) { 307 if(customFields[i] != null && customFields[i].getId() != null && customFields[i].getId().equals(id)) { 308 retField = (CustomFieldModel) customFields[i].clone(); 309 break; 310 } 311 } 312 } catch(CloneNotSupportedException cnse) { 313 Logger.logError("Error cloning CustomFieldModel: " + cnse.getMessage()); 314 } 315 if(retField != null) { 316 retField.setLabels(locale); 317 } else { 318 retField = new CustomFieldModel(); 319 } 320 321 return retField; 322 } 323 324 327 public static int getNumberCustomFields() { 328 return customFields.length; 329 } 330 331 337 public static boolean canViewIssue(IssueModel issue, UserModel user, HashMap permissions) { 338 if(user == null) { 339 return false; 340 } 341 return canViewIssue(issue, user.getId(), permissions); 342 } 343 344 350 public static boolean canViewIssue(IssueModel issue, Integer userId, HashMap permissions) { 351 if(issue == null || userId == null || permissions == null) { 352 return false; 353 } 354 355 if(UserUtilities.hasPermission(permissions, issue.getProjectId(), UserUtilities.PERMISSION_VIEW_ALL)) { 356 return true; 357 } 358 if(! UserUtilities.hasPermission(permissions, issue.getProjectId(), UserUtilities.PERMISSION_VIEW_USERS)) { 359 return false; 360 } 361 362 if(issue.getCreatorId().equals(userId)) { 363 return true; 364 } 365 366 if(issue.getOwnerId().equals(userId)) { 367 return true; 368 } 369 370 return false; 371 } 372 373 379 public static boolean canEditIssue(IssueModel issue, Integer userId, HashMap permissions) { 380 if(issue == null || userId == null || permissions == null) { 381 return false; 382 } 383 384 if(UserUtilities.hasPermission(permissions, issue.getProjectId(), UserUtilities.PERMISSION_EDIT)) { 385 return true; 386 } 387 if(! UserUtilities.hasPermission(permissions, issue.getProjectId(), UserUtilities.PERMISSION_EDIT_USERS)) { 388 return false; 389 } 390 391 if(issue.getCreatorId().equals(userId)) { 392 return true; 393 } 394 395 if(issue.getOwnerId().equals(userId)) { 396 return true; 397 } 398 399 return false; 400 } 401 402 408 public static boolean canBeAssignedIssue(IssueModel issue, Integer userId, HashMap permissions) { 409 if(issue == null || userId == null || permissions == null) { 410 return false; 411 } 412 413 if(UserUtilities.hasPermission(permissions, issue.getProjectId(), UserUtilities.PERMISSION_EDIT)) { 414 return true; 415 } 416 if(UserUtilities.hasPermission(permissions, issue.getProjectId(), UserUtilities.PERMISSION_EDIT_USERS)) { 417 if(issue.getCreatorId().equals(userId)) { 418 return true; 419 } else if(UserUtilities.hasPermission(permissions, issue.getProjectId(), UserUtilities.PERMISSION_ASSIGNABLE)) { 420 return true; 421 } else if(issue.getOwnerId() != null && issue.getOwnerId().equals(userId)) { 422 return true; 423 } 424 } 425 426 return false; 427 } 428 429 435 public static boolean canUnassignIssue(IssueModel issue, Integer userId, HashMap permissions) { 436 if(issue == null || userId == null || permissions == null) { 437 return false; 438 } 439 440 if(UserUtilities.hasPermission(permissions, issue.getProjectId(), UserUtilities.PERMISSION_ASSIGN_OTHERS)) { 441 return true; 442 } 443 if(issue.getOwner() != null && userId.equals(issue.getOwner().getId()) && 444 UserUtilities.hasPermission(permissions, issue.getProjectId(), UserUtilities.PERMISSION_UNASSIGN_SELF)) { 445 return true; 446 } 447 448 return false; 449 } 450 451 public static boolean hasIssueNotification(IssueModel issue, Integer userId) { 452 return hasIssueNotification(issue, issue.getProject(), userId); 453 } 454 455 public static boolean hasIssueNotification(IssueModel issue, ProjectModel project, Integer userId) { 456 if(issue == null || userId == null) { 457 return false; 458 } 459 if(issue.getOwnerId().equals(userId) || issue.getCreatorId().equals(userId)) { 460 return true; 461 } 462 463 if(project != null && project.getOwners() != null) { 464 UserModel[] owners = project.getOwners(); 465 for(int i = 0; i < owners.length; i++) { 466 if(owners[i] != null && owners[i].getId().equals(userId)) { 467 return true; 468 } 469 } 470 } 471 472 NotificationModel [] notifications = issue.getNotifications(); 473 for(int i = 0; i < notifications.length; i++) { 474 if(notifications[i].getUserId().equals(userId)) { 475 return true; 476 } 477 } 478 479 return false; 480 } 481 } 482 483 class StatusComparator implements Comparator { 484 485 public int compare(Object a, Object b) { 486 if(a == null || b == null || ! (a instanceof String ) || ! (b instanceof String )) { 487 return 0; 488 } 489 490 if(((String ) a).equals((String ) b)) { 491 return 0; 492 } 493 494 try { 495 if(new Integer ((String ) a).intValue() > new Integer ((String ) b).intValue()) { 496 return 1; 497 } else { 498 return -1; 499 } 500 } catch(NumberFormatException nfe) { 501 return 0; 502 } 503 } 504 } 505 506 | Popular Tags |