1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.itracker.services.util;
20
21 import java.util.Comparator;
22 import java.util.HashMap;
23 import java.util.Hashtable;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.Locale;
27 import java.util.Map;
28 import java.util.Set;
29 import java.util.TreeSet;
30
31 import org.apache.commons.lang.builder.CompareToBuilder;
32 import org.itracker.core.resources.ITrackerResources;
33 import org.itracker.model.Notification;
34 import org.itracker.model.User;
35 import org.itracker.model.Notification.Role;
36
37 public class NotificationUtilities {
38
39
40
41
42 public static final int ROLE_ANY = Notification.Role.ANY.getCode();
43
44
45
46 public static final int ROLE_CREATOR = Notification.Role.CREATOR.getCode();
47
48
49
50 public static final int ROLE_OWNER = Notification.Role.OWNER.getCode();
51
52
53
54 public static final int ROLE_CONTRIBUTER = Notification.Role.CONTRIBUTER.getCode();
55
56
57
58 public static final int ROLE_QA = Notification.Role.QA.getCode();
59
60
61
62 public static final int ROLE_PM = Notification.Role.PM.getCode();
63
64
65
66 public static final int ROLE_PO = Notification.Role.PO.getCode();
67
68
69
70 public static final int ROLE_CO = Notification.Role.CO.getCode();
71
72
73
74 public static final int ROLE_VO = Notification.Role.VO.getCode();
75
76
77
78 public static final int ROLE_IP = Notification.Role.IP.getCode();
79
80
81
82
83 public static final int TYPE_CREATED = Notification.Type.CREATED.getCode();
84
85
86
87 public static final int TYPE_UPDATED = Notification.Type.UPDATED.getCode();
88
89
90
91 public static final int TYPE_ASSIGNED = Notification.Type.ASSIGNED.getCode();
92
93
94
95 public static final int TYPE_CLOSED = Notification.Type.CLOSED.getCode();
96
97
98
99 public static final int TYPE_SELF_REGISTER = Notification.Type.SELF_REGISTER.getCode();
100
101
102
103 public static final int TYPE_ISSUE_REMINDER = Notification.Type.ISSUE_REMINDER.getCode();
104
105
106
107
108 private static HashMap<Locale, HashMap<Role, String>> roleNames = new HashMap<Locale, HashMap<Role, String>>();
109
110 public NotificationUtilities() {
111 super();
112 }
113
114
115
116
117
118
119 public static String getRoleName(int value) {
120 return getRoleName(value, ITrackerResources.getLocale());
121 }
122
123
124
125
126
127
128
129 public static String getRoleName(int value, Locale locale) {
130 return ITrackerResources.getString("itracker.notification.role."
131 + value, locale);
132 }
133
134 public static String getRoleName(Role role) {
135 return getRoleName(role, ITrackerResources.getLocale());
136 }
137
138 public static String getRoleName(Role role, Locale locale) {
139 String s;
140 if (null != (s = ITrackerResources.getString(
141 "itracker.notification.role." + role, locale))) {
142 return s;
143 }
144
145 return ITrackerResources.getString("itracker.notification.role."
146 + role.getCode(), locale);
147 }
148
149
150
151
152
153 public static HashMap<Role, String> getRoleNames() {
154 return getRoleNames(ITrackerResources.getLocale());
155 }
156
157 public static HashMap<Role, String> getRoleNames(Locale locale) {
158 HashMap<Role, String> roles = roleNames.get(locale);
159 if (roles == null) {
160 roles = new HashMap<Role, String>();
161 roles.put(Notification.Role.CREATOR,
162 getRoleName(Notification.Role.CREATOR, locale));
163 roles.put(Notification.Role.OWNER, getRoleName(Role.OWNER, locale));
164 roles.put(Notification.Role.CONTRIBUTER, getRoleName(
165 Notification.Role.CONTRIBUTER, locale));
166 roles.put(Notification.Role.QA, getRoleName(Role.QA, locale));
167 roles.put(Notification.Role.PM, getRoleName(Role.PM, locale));
168 roles.put(Notification.Role.PO, getRoleName(Role.PO, locale));
169 roles.put(Notification.Role.CO, getRoleName(Role.CO, locale));
170 roles.put(Notification.Role.VO, getRoleName(Role.VO, locale));
171 roles.put(Notification.Role.IP, getRoleName(Role.IP, locale));
172 }
173 roleNames.put(locale, roles);
174 return roles;
175 }
176
177
178
179
180
181
182 public static String getTypeName(int value) {
183 return getTypeName(value, ITrackerResources.getLocale());
184 }
185
186
187
188
189
190
191
192 public static String getTypeName(int value, Locale locale) {
193 return ITrackerResources.getString("itracker.notification.type."
194 + value, locale);
195 }
196
197 public static final Map<User, Set<Notification.Role>> mappedRoles(List<Notification> notifications) {
198
199 Map<User, Set<Role>> mapping = new Hashtable<User, Set<Role>>();
200 Iterator<Notification> notificationIt = notifications.iterator();
201 while (notificationIt.hasNext()) {
202 Notification notification = (Notification) notificationIt.next();
203 Set<Role> roles;
204 if (mapping.keySet().contains(notification.getUser())) {
205 roles = mapping.get(notification.getUser());
206 roles.add(notification.getRole());
207 } else {
208 roles = new TreeSet<Role>(new Comparator<Role>() {
209 public int compare(Role o1, Role o2) {
210 return new CompareToBuilder().append(o1.getCode(), o2.getCode()).toComparison();
211 }
212 });
213 roles.add(notification.getRole());
214 mapping.put(notification.getUser(), roles);
215 }
216 }
217
218
219 return mapping;
220 }
221
222 }