View Javadoc

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 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  	 * @deprecated use enum NotificationUtilities.Role.ANY instead
41  	 */
42  	public static final int ROLE_ANY = Notification.Role.ANY.getCode();
43  	/**
44  	 * @deprecated use enum NotificationUtilities.Role.CREATOR instead
45  	 */
46  	public static final int ROLE_CREATOR = Notification.Role.CREATOR.getCode();
47  	/**
48  	 * @deprecated use enum NotificationUtilities.Role.OWNER instead
49  	 */
50  	public static final int ROLE_OWNER = Notification.Role.OWNER.getCode();
51  	/**
52  	 * @deprecated use enum NotificationUtilities.Role.CONTRIBUTOR instead
53  	 */
54  	public static final int ROLE_CONTRIBUTER = Notification.Role.CONTRIBUTER.getCode();
55  	/**
56  	 * @deprecated use enum NotificationUtilities.Role.QA instead
57  	 */
58  	public static final int ROLE_QA = Notification.Role.QA.getCode();
59  	/**
60  	 * @deprecated use enum NotificationUtilities.Role.PM instead
61  	 */
62  	public static final int ROLE_PM = Notification.Role.PM.getCode();
63  	/**
64  	 * @deprecated use enum NotificationUtilities.Role.PO instead
65  	 */
66  	public static final int ROLE_PO = Notification.Role.PO.getCode();
67  	/**
68  	 * @deprecated use enum NotificationUtilities.Role.CO instead
69  	 */
70  	public static final int ROLE_CO = Notification.Role.CO.getCode();
71  	/**
72  	 * @deprecated use enum NotificationUtilities.Role.VO instead
73  	 */
74  	public static final int ROLE_VO = Notification.Role.VO.getCode();
75  	/**
76  	 * @deprecated use enum NotificationUtilities.Role.IP instead
77  	 */
78  	public static final int ROLE_IP = Notification.Role.IP.getCode();
79  
80  	/**
81  	 * @deprecated use enum NotificationUtilities.Type.CREATED instead
82  	 */
83  	public static final int TYPE_CREATED = Notification.Type.CREATED.getCode();
84  	/**
85  	 * @deprecated use enum NotificationUtilities.Type.UPDATED instead
86  	 */
87  	public static final int TYPE_UPDATED = Notification.Type.UPDATED.getCode();
88  	/**
89  	 * @deprecated use enum NotificationUtilities.Type.ASSIGNED instead
90  	 */
91  	public static final int TYPE_ASSIGNED = Notification.Type.ASSIGNED.getCode();
92  	/**
93  	 * @deprecated use enum NotificationUtilities.Type.CLOSED instead
94  	 */
95  	public static final int TYPE_CLOSED = Notification.Type.CLOSED.getCode();
96  	/**
97  	 * @deprecated use enum NotificationUtilities.Type.SELF_REGISTER instead
98  	 */
99  	public static final int TYPE_SELF_REGISTER = Notification.Type.SELF_REGISTER.getCode();
100 	/**
101 	 * @deprecated use enum NotificationUtilities.Type.ISSUE_REMINDER instead
102 	 */
103 	public static final int TYPE_ISSUE_REMINDER = Notification.Type.ISSUE_REMINDER.getCode();
104 
105 	/**
106 	 * @deprecated, use enumeration Notification.Role instead
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 	 * @deprecated
116 	 * @param value
117 	 * @return
118 	 */
119 	public static String getRoleName(int value) {
120 		return getRoleName(value, ITrackerResources.getLocale());
121 	}
122 
123 	/**
124 	 * @deprecated
125 	 * @param value
126 	 * @param locale
127 	 * @return
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 	 * @deprecated prefer direct use of Role enumeration
151 	 * @return
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 	 * @deprecated
179 	 * @param value
180 	 * @return
181 	 */
182 	public static String getTypeName(int value) {
183 		return getTypeName(value, ITrackerResources.getLocale());
184 	}
185 
186 	/**
187 	 * @deprecated
188 	 * @param value
189 	 * @param locale
190 	 * @return
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 }