KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > ejb > client > util > IssueUtilities


1 /*
2  * This software was designed and created by Jason Carroll.
3  * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4  * The author can be reached at jcarroll@cowsultants.com
5  * ITracker website: http://www.cowsultants.com
6  * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it only under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  */

18
19 package cowsultants.itracker.ejb.client.util;
20
21 import java.text.SimpleDateFormat JavaDoc;
22 import java.util.*;
23
24 import cowsultants.itracker.ejb.client.models.*;
25 import cowsultants.itracker.ejb.client.resources.*;
26
27 /**
28   * Contains utilities used when displaying and processing issues.
29   */

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     // This marks the end of all status numbers. You can NOT add a status above this number or
38
// they will not be found.
39
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 JavaDoc componentsToString(IssueModel issue) {
66         String JavaDoc 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 JavaDoc versionsToString(IssueModel issue) {
76         String JavaDoc 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 JavaDoc historyToString(IssueModel issue, SimpleDateFormat JavaDoc sdf) {
86         String JavaDoc 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 JavaDoc getStatusName(int value) {
97         return getStatusName(value, ITrackerResources.getLocale());
98     }
99
100     public static String JavaDoc getStatusName(int value, Locale locale) {
101         return getStatusName(Integer.toString(value), locale);
102     }
103
104     public static String JavaDoc getStatusName(String JavaDoc 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 JavaDoc getSeverityName(int value) {
129         return getSeverityName(value, ITrackerResources.getLocale());
130     }
131
132     public static String JavaDoc getSeverityName(int value, Locale locale) {
133         return getSeverityName(Integer.toString(value), locale);
134     }
135
136     public static String JavaDoc getSeverityName(String JavaDoc value, Locale locale) {
137         return ITrackerResources.getString(ITrackerResources.KEY_BASE_SEVERITY + value, locale);
138     }
139
140     /**
141       * Returns the list of the defined issue severities in the system. The array returned
142       * is a cached list set from the setSeverities method. The actual values are stored
143       * in the database and and can be obtained from the SystemConfiguration bean.
144       * @param locale the locale to return the severities as
145       * @returns array of translated strings from the cached severities list
146       */

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     /**
164       * Compares the severity of two issues. The int returned will be negative if the
165       * the severity of issue A is less than the severity of issue B, positive if issue
166       * A is a higher severity than issue B, or 0 if the two issues have the same severity.
167       * @param ma IssueModel A
168       * @param mb IssueModel B
169       * @returns an int representing the compared severities
170       */

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 JavaDoc getResolutionName(int value) {
202         return getResolutionName(value, ITrackerResources.getLocale());
203     }
204
205     public static String JavaDoc getResolutionName(int value, Locale locale) {
206         return getResolutionName(Integer.toString(value), locale);
207     }
208
209     public static String JavaDoc getResolutionName(String JavaDoc value, Locale locale) {
210         return ITrackerResources.getString(ITrackerResources.KEY_BASE_RESOLUTION + value, locale);
211     }
212
213     public static String JavaDoc checkResolutionName(String JavaDoc value, Locale locale) throws MissingResourceException {
214         return ITrackerResources.getCheckForKey(ITrackerResources.KEY_BASE_RESOLUTION + value, locale);
215     }
216
217     /**
218       * Returns the list of predefined resolutions in the system. The array returned
219       * is a cached list set from the setResolutions method. The actual values are stored
220       * in the database and and can be obtained from the SystemConfiguration bean.
221       * @param locale the locale to return the resolutions as
222       * @returns array of translated strings from the cached resolution list
223       */

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     /**
233       * Sets the cached list of predefined resolutions.
234       */

235     public static void setResolutions(ConfigurationModel[] value) {
236         resolutions = (value == null ? new ConfigurationModel[0] : value);
237     }
238
239     public static String JavaDoc getActivityName(int value) {
240         return getActivityName(value, ITrackerResources.getLocale());
241     }
242
243     public static String JavaDoc getActivityName(int value, Locale locale) {
244         return ITrackerResources.getString("itracker.activity." + value, locale);
245     }
246
247     /**
248       * Returns the cached array of CustomFieldModels.
249       * @return an array of CustomFieldModels
250       */

251     public static CustomFieldModel[] getCustomFields() {
252         return (customFields == null ? new CustomFieldModel[0] : customFields);
253     }
254
255     /**
256       * Sets the cached array of CustomFieldModels.
257       * @return an array of CustomFieldModels
258       */

259     public static void setCustomFields(CustomFieldModel[] value) {
260         customFields = (value == null ? new CustomFieldModel[0] : value);
261     }
262
263     /**
264       * Returns an array of the cached custom fields. The fields labels will be initialized
265       * based on the Locale given. If no locale is given, the default locale of the
266       * system will be used.
267       * @param locale the locale to use to populate the field labels
268       * @return the cached array of CustomFieldModels
269       */

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 JavaDoc cnse) {
279                 Logger.logError("Error cloning CustomFieldModel: " + cnse.getMessage());
280             }
281         }
282         return localizedFields;
283     }
284
285     /**
286       * Returns the custom field with the supplied id. Any labels
287       * will be localized to the system default locale.
288       * @param bitValue the id of the field to return
289       * @return the requested CustomField object, or a new field if not found
290       */

291     public static CustomFieldModel getCustomField(Integer JavaDoc id) {
292         return getCustomField(id, ITrackerResources.getLocale());
293     }
294
295     /**
296       * Returns the custom field with the supplied id value. Any labels will
297       * be translated to the given locale.
298       * @param id the id of the field to return
299       * @param locale the locale to initialize any labels with
300       * @return the requested CustomField object, or a new field if not found
301       */

302     public static CustomFieldModel getCustomField(Integer JavaDoc 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 JavaDoc 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     /**
325       * Returns the total number of defined custom fields
326       */

327     public static int getNumberCustomFields() {
328         return customFields.length;
329     }
330
331     /**
332       * Returns true if the user has permission to view the requested issue.
333       * @param issue an IssueModel of the issue to check view permission for
334       * @param user a UserModel for the user to check permission for
335       * @param permissions a HashMap of the users permissions
336       */

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     /**
345       * Returns true if the user has permission to view the requested issue.
346       * @param issue an IssueModel of the issue to check view permission for
347       * @param userId the userId of the user to check permission for
348       * @param permissions a HashMap of the users permissions
349       */

350     public static boolean canViewIssue(IssueModel issue, Integer JavaDoc 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     /**
374       * Returns true if the user has permission to edit the requested issue.
375       * @param issue an IssueModel of the issue to check edit permission for
376       * @param userId the userId of the user to check permission for
377       * @param permissions a HashMap of the users permissions
378       */

379     public static boolean canEditIssue(IssueModel issue, Integer JavaDoc 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     /**
403       * Returns true if the user can be assigned to this issue.
404       * @param issue an IssueModel of the issue to check assign permission for
405       * @param userId the userId of the user to check permission for
406       * @param permissions a HashMap of the users permissions
407       */

408     public static boolean canBeAssignedIssue(IssueModel issue, Integer JavaDoc 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     /**
430       * Returns true if the user can unassign themselves from the issue.
431       * @param issue an IssueModel of the issue to check assign permission for
432       * @param userId the userId of the user to check permission for
433       * @param permissions a HashMap of the users permissions
434       */

435     public static boolean canUnassignIssue(IssueModel issue, Integer JavaDoc 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 JavaDoc userId) {
452         return hasIssueNotification(issue, issue.getProject(), userId);
453     }
454
455     public static boolean hasIssueNotification(IssueModel issue, ProjectModel project, Integer JavaDoc 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 JavaDoc a, Object JavaDoc b) {
486         if(a == null || b == null || ! (a instanceof String JavaDoc) || ! (b instanceof String JavaDoc)) {
487             return 0;
488         }
489
490         if(((String JavaDoc) a).equals((String JavaDoc) b)) {
491             return 0;
492         }
493
494         try {
495             if(new Integer JavaDoc((String JavaDoc) a).intValue() > new Integer JavaDoc((String JavaDoc) b).intValue()) {
496                 return 1;
497             } else {
498                 return -1;
499             }
500         } catch(NumberFormatException JavaDoc nfe) {
501             return 0;
502         }
503     }
504 }
505
506
Popular Tags