1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package org.astrogrid.community.common.policy.manager ;
26
27 import org.apache.commons.logging.Log ;
28 import org.apache.commons.logging.LogFactory ;
29
30 import java.util.Map ;
31 import java.util.HashMap ;
32
33 import org.astrogrid.community.common.policy.data.GroupData ;
34 import org.astrogrid.community.common.service.CommunityServiceMock ;
35
36 import org.astrogrid.community.common.exception.CommunityPolicyException ;
37 import org.astrogrid.community.common.exception.CommunityIdentifierException ;
38
39 /***
40 * Mock implementation of the GroupManager service.
41 *
42 */
43 public class GroupManagerMock
44 extends CommunityServiceMock
45 implements GroupManager
46 {
47 /***
48 * Our debug logger.
49 *
50 */
51 private static Log log = LogFactory.getLog(GroupManagerMock.class);
52
53 /***
54 * Public constructor.
55 *
56 */
57 public GroupManagerMock()
58 {
59 super() ;
60 }
61
62 /***
63 * Our hash table of values.
64 *
65 */
66 private static Map map = new HashMap() ;
67
68 /***
69 * Reset our map.
70 *
71 */
72 public static void reset()
73 {
74 log.debug("") ;
75 log.debug("----\"----") ;
76 log.debug("GroupManagerMock.reset()") ;
77 map.clear() ;
78 }
79
80 /***
81 * Add a new Group, given the Group ident.
82 * @param ident The Group identifier.
83 * @return An GroupData for the Group.
84 * @throws CommunityIdentifierException If the identifier is not valid.
85 * @throws CommunityPolicyException If the identifier is already in the database.
86 *
87 */
88 public GroupData addGroup(String ident)
89 throws CommunityIdentifierException, CommunityPolicyException
90 {
91 log.debug("") ;
92 log.debug("----\"----") ;
93 log.debug("GroupManagerMock.addGroup()") ;
94 log.debug(" Ident : " + ident) ;
95
96
97 if (null == ident)
98 {
99 throw new CommunityIdentifierException(
100 "Null identifier"
101 ) ;
102 }
103
104
105 if (map.containsKey(ident))
106 {
107 throw new CommunityPolicyException(
108 "Duplicate group",
109 ident
110 ) ;
111 }
112
113
114 GroupData group = new GroupData(ident) ;
115
116
117 map.put(group.getIdent(), group) ;
118
119
120 return group ;
121 }
122
123 /***
124 * Add a new Group, given the Group data.
125 * @param data The GroupData to add.
126 * @return A new GroupData for the Group.
127 * @throws CommunityIdentifierException If the identifier is not valid.
128 * @throws CommunityPolicyException If the identifier is already in the database.
129 *
130 */
131 public GroupData addGroup(GroupData group)
132 throws CommunityIdentifierException, CommunityPolicyException
133 {
134 log.debug("") ;
135 log.debug("----\"----") ;
136 log.debug("GroupManagerMock.addGroup()") ;
137 log.debug(" Group : " + ((null != group) ? group.getIdent() : null)) ;
138
139
140 if (null == group)
141 {
142 throw new CommunityIdentifierException(
143 "Null group"
144 ) ;
145 }
146
147
148 if (null == group.getIdent())
149 {
150 throw new CommunityIdentifierException(
151 "Null identifier"
152 ) ;
153 }
154
155
156 if (map.containsKey(group.getIdent()))
157 {
158 throw new CommunityPolicyException(
159 "Duplicate group",
160 group.getIdent()
161 ) ;
162 }
163
164
165 map.put(group.getIdent(), group) ;
166
167
168 return group ;
169 }
170
171 /***
172 * Request a Group details, given the Group ident.
173 * @param ident The Group identifier.
174 * @return An GroupData for the Group.
175 * @throws CommunityIdentifierException If the identifier is not valid.
176 * @throws CommunityPolicyException If the identifier is not in the database.
177 *
178 */
179 public GroupData getGroup(String ident)
180 throws CommunityIdentifierException, CommunityPolicyException
181 {
182 log.debug("") ;
183 log.debug("----\"----") ;
184 log.debug("GroupManagerMock.getGroup()") ;
185 log.debug(" Ident : " + ident) ;
186
187
188 if (null == ident)
189 {
190 throw new CommunityIdentifierException(
191 "Null identifier"
192 ) ;
193 }
194
195
196 GroupData group = (GroupData) map.get(ident) ;
197
198
199 if (null != group)
200 {
201 return group ;
202 }
203
204
205 else {
206 throw new CommunityPolicyException(
207 "Group not found",
208 ident
209 ) ;
210 }
211 }
212
213 /***
214 * Update a Group.
215 * @param update The new Group data to update.
216 * @return A new GroupData for the Group.
217 * @throws CommunityIdentifierException If the identifier is not valid.
218 * @throws CommunityPolicyException If the identifier is not in the database.
219 *
220 */
221 public GroupData setGroup(GroupData update)
222 throws CommunityIdentifierException, CommunityPolicyException
223 {
224 log.debug("") ;
225 log.debug("----\"----") ;
226 log.debug("GroupManagerMock.setGroup()") ;
227 log.debug(" Ident : " + ((null != update) ? update.getIdent() : null)) ;
228
229
230 if (null == update)
231 {
232 throw new CommunityIdentifierException(
233 "Null inout data"
234 ) ;
235 }
236
237
238 if (null == update.getIdent())
239 {
240 throw new CommunityIdentifierException(
241 "Null inout data"
242 ) ;
243 }
244
245
246 GroupData found = this.getGroup(update.getIdent()) ;
247
248
249 map.put(found.getIdent(), update) ;
250
251
252 return update ;
253 }
254
255 /***
256 * Delete a Group.
257 * @param ident The Group identifier.
258 * @return The GroupData for the old Group.
259 * @throws CommunityIdentifierException If the identifier is not valid.
260 * @throws CommunityPolicyException If the identifier is not in the database.
261 *
262 */
263 public GroupData delGroup(String ident)
264 throws CommunityIdentifierException, CommunityPolicyException
265 {
266 log.debug("") ;
267 log.debug("----\"----") ;
268 log.debug("GroupManagerMock.delGroup()") ;
269 log.debug(" Ident : " + ident) ;
270
271
272 if (null == ident)
273 {
274 throw new CommunityIdentifierException(
275 "Null identifier"
276 ) ;
277 }
278
279
280 GroupData group = this.getGroup(ident) ;
281
282
283 if (null != group)
284 {
285
286
287 map.remove(group.getIdent()) ;
288
289
290 return group ;
291 }
292
293
294 else {
295 throw new CommunityPolicyException(
296 "Group not found",
297 ident
298 ) ;
299 }
300 }
301
302 /***
303 * Request a list of local Groups.
304 * @return An array of GroupData objects.
305 *
306 */
307 public Object[] getLocalGroups()
308 {
309 log.debug("") ;
310 log.debug("----\"----") ;
311 log.debug("GroupManagerMock.getLocalGroups()") ;
312 return map.values().toArray() ;
313 }
314
315 /***
316 * Get a list of local Groups that an Account belongs to.
317 * @return An array of GroupData objects.
318 * @todo Filter the results for matches.
319 *
320 */
321 public Object[] getLocalAccountGroups(String account)
322 {
323 log.debug("") ;
324 log.debug("----\"----") ;
325 log.debug("GroupManagerMock.getAccountGroups()") ;
326 return map.values().toArray() ;
327 }
328 }