KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > shark > user > GenericUserGroupMgr


1 /*
2  * $Id: GenericUserGroupMgr.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.shark.user;
26
27 import java.util.List JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 import org.ofbiz.entity.GenericValue;
32 import org.ofbiz.entity.GenericDelegator;
33 import org.ofbiz.entity.GenericEntityException;
34 import org.ofbiz.shark.container.SharkContainer;
35 import org.ofbiz.base.util.UtilMisc;
36 import org.ofbiz.base.util.Debug;
37 import org.ofbiz.base.util.UtilValidate;
38
39 import org.enhydra.shark.api.internal.usergroup.UserGroupManager;
40 import org.enhydra.shark.api.internal.working.CallbackUtilities;
41 import org.enhydra.shark.api.RootException;
42 import org.enhydra.shark.api.UserTransaction;
43
44 /**
45  * Shark User/Group Manager
46  *
47  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
48  * @version $Rev: 5462 $
49  * @since 3.1
50  */

51 public class GenericUserGroupMgr implements UserGroupManager {
52
53     public static final String JavaDoc module = GenericUserGroupMgr.class.getName();
54     protected CallbackUtilities callBack = null;
55
56     public void configure(CallbackUtilities callbackUtilities) throws RootException {
57         this.callBack = callbackUtilities;
58     }
59
60     public List JavaDoc getAllGroupnames(UserTransaction trans) throws RootException {
61         GenericDelegator delegator = SharkContainer.getDelegator();
62         List JavaDoc groupNames = new ArrayList JavaDoc();
63         List JavaDoc groups = null;
64         try {
65             groups = delegator.findAll("SharkGroup");
66         } catch (GenericEntityException e) {
67             Debug.logError(e, module);
68             throw new RootException(e);
69         }
70         if (groups != null && groups.size() > 0) {
71             Iterator JavaDoc i = groups.iterator();
72             while (i.hasNext()) {
73                 GenericValue v = (GenericValue) i.next();
74                 groupNames.add(v.getString("groupName"));
75             }
76         }
77         return groupNames;
78     }
79
80     public List JavaDoc getAllUsers(UserTransaction trans) throws RootException {
81         GenericDelegator delegator = SharkContainer.getDelegator();
82         List JavaDoc userNames = new ArrayList JavaDoc();
83         List JavaDoc users = null;
84         try {
85             users = delegator.findAll("SharkUser");
86         } catch (GenericEntityException e) {
87             Debug.logError(e, module);
88             throw new RootException(e);
89         }
90         if (users != null && users.size() > 0) {
91             Iterator JavaDoc i = users.iterator();
92             while (i.hasNext()) {
93                 GenericValue v = (GenericValue) i.next();
94                 userNames.add(v.getString("userName"));
95             }
96         }
97         return userNames;
98     }
99
100     public List JavaDoc getAllUsers(UserTransaction trans, String JavaDoc groupName) throws RootException {
101         GenericDelegator delegator = SharkContainer.getDelegator();
102         List JavaDoc userNames = new ArrayList JavaDoc();
103         List JavaDoc members = null;
104         try {
105             members = delegator.findByAnd("SharkGroupMember", UtilMisc.toMap("groupName", groupName));
106         } catch (GenericEntityException e) {
107             Debug.logError(e, module);
108             throw new RootException(e);
109         }
110         if (members != null && members.size() > 0) {
111             Iterator JavaDoc i = members.iterator();
112             while (i.hasNext()) {
113                 GenericValue v = (GenericValue) i.next();
114                 userNames.add(v.getString("userName"));
115             }
116         }
117         return userNames;
118     }
119
120     public List JavaDoc getAllUsers(UserTransaction trans, List JavaDoc groupNames) throws RootException {
121         List JavaDoc userNames = new ArrayList JavaDoc();
122         if (groupNames != null && groupNames.size() > 0) {
123             Iterator JavaDoc i = groupNames.iterator();
124             while (i.hasNext()) {
125                 String JavaDoc groupName = (String JavaDoc) i.next();
126                 userNames.addAll(getAllUsers(trans, groupName));
127             }
128         }
129         return userNames;
130     }
131
132     public List JavaDoc getAllImmediateUsers(UserTransaction trans, String JavaDoc groupName) throws RootException {
133         return this.getAllUsers(trans, groupName);
134     }
135
136     public List JavaDoc getAllSubgroups(UserTransaction trans, String JavaDoc groupName) throws RootException {
137         GenericDelegator delegator = SharkContainer.getDelegator();
138         List JavaDoc subGroups = new ArrayList JavaDoc();
139         List JavaDoc rollups = null;
140         try {
141             rollups = delegator.findByAnd("SharkGroupRollup", UtilMisc.toMap("groupName", groupName));
142         } catch (GenericEntityException e) {
143             Debug.logError(e, module);
144             throw new RootException(e);
145         }
146         if (rollups != null && rollups.size() > 0) {
147             Iterator JavaDoc i = rollups.iterator();
148             while (i.hasNext()) {
149                 GenericValue v = (GenericValue) i.next();
150                 subGroups.add(v.getString("subGroupName"));
151             }
152         }
153         return subGroups;
154     }
155
156     public List JavaDoc getAllSubgroups(UserTransaction trans, List JavaDoc groupNames) throws RootException {
157         List JavaDoc subGroups = new ArrayList JavaDoc();
158         if (groupNames != null && groupNames.size() > 0) {
159             Iterator JavaDoc i = groupNames.iterator();
160             while (i.hasNext()) {
161                 String JavaDoc groupName = (String JavaDoc) i.next();
162                 subGroups.addAll(getAllSubgroups(trans, groupName));
163             }
164         }
165         return subGroups;
166     }
167
168     public List JavaDoc getAllImmediateSubgroups(UserTransaction trans, String JavaDoc groupName) throws RootException {
169         return this.getAllSubgroups(trans, groupName);
170     }
171
172     public void createGroup(UserTransaction trans, String JavaDoc groupName, String JavaDoc description) throws RootException {
173         GenericDelegator delegator = SharkContainer.getDelegator();
174         GenericValue group = delegator.makeValue("SharkGroup", null);
175         group.set("groupName", groupName);
176         group.set("description", description);
177         try {
178             delegator.create(group);
179         } catch (GenericEntityException e) {
180             Debug.logError(e, module);
181             throw new RootException(e);
182         }
183     }
184
185     public void removeGroup(UserTransaction trans, String JavaDoc groupName) throws RootException {
186         GenericValue group = getGroup(groupName);
187         if (group != null) {
188             try {
189                 group.remove();
190             } catch (GenericEntityException e) {
191                 Debug.logError(e, module);
192                 throw new RootException(e);
193             }
194         }
195     }
196
197     public boolean doesGroupExist(UserTransaction trans, String JavaDoc groupName) throws RootException {
198         GenericValue group = getGroup(groupName);
199         if (group != null) {
200             return true;
201         }
202         return false;
203     }
204
205     public boolean doesGroupBelongToGroup(UserTransaction trans, String JavaDoc groupName, String JavaDoc subGroupName) throws RootException {
206         GenericValue rollup = getGroupRollup(groupName, subGroupName);
207         if (rollup != null) {
208             return true;
209         }
210         return false;
211     }
212
213     public void updateGroup(UserTransaction trans, String JavaDoc groupName, String JavaDoc description) throws RootException {
214         GenericValue group = getGroup(groupName);
215         if (group != null) {
216             group.set("description", description);
217             try {
218                 group.store();
219             } catch (GenericEntityException e) {
220                 Debug.logError(e, module);
221                 throw new RootException(e);
222             }
223         }
224     }
225
226     public void addGroupToGroup(UserTransaction trans, String JavaDoc parentGroupName, String JavaDoc groupName) throws RootException {
227         GenericDelegator delegator = SharkContainer.getDelegator();
228         GenericValue rollup = delegator.makeValue("SharkGroupRollup", null);
229         rollup.set("parentGroupName", parentGroupName);
230         rollup.set("groupName", groupName);
231         try {
232             delegator.create(rollup);
233         } catch (GenericEntityException e) {
234             Debug.logError(e, module);
235             throw new RootException(e);
236         }
237     }
238
239     public void removeGroupFromGroup(UserTransaction trans, String JavaDoc parentGroup, String JavaDoc group) throws RootException {
240         GenericValue rollup = getGroupRollup(parentGroup, group);
241         if (rollup != null) {
242             try {
243                 rollup.remove();
244             } catch (GenericEntityException e) {
245                 Debug.logError(e, module);
246                 throw new RootException(e);
247             }
248         }
249     }
250
251     public void removeGroupTree(UserTransaction trans, String JavaDoc s) throws RootException {
252         // TODO: Implement Me!
253
}
254
255     public void removeUsersFromGroupTree(UserTransaction trans, String JavaDoc s) throws RootException {
256         // TODO: Implement Me!
257
}
258
259     public void moveGroup(UserTransaction trans, String JavaDoc currentParentGroup, String JavaDoc newParentGroup, String JavaDoc groupName) throws RootException {
260         this.removeGroupFromGroup(trans, currentParentGroup, groupName);
261         this.addGroupToGroup(trans, newParentGroup, groupName);
262     }
263
264     public String JavaDoc getGroupDescription(UserTransaction trans, String JavaDoc groupName) throws RootException {
265         GenericValue group = getGroup(groupName);
266         if (group != null) {
267             return group.getString("description");
268         }
269         return null;
270     }
271
272     public void addUserToGroup(UserTransaction trans, String JavaDoc groupName, String JavaDoc username) throws RootException {
273         GenericDelegator delegator = SharkContainer.getDelegator();
274         GenericValue member = delegator.makeValue("SharkGroupMember", null);
275         member.set("groupName", groupName);
276         member.set("userName", username);
277         try {
278             delegator.create(member);
279         } catch (GenericEntityException e) {
280             Debug.logError(e, module);
281             throw new RootException(e);
282         }
283     }
284
285     public void removeUserFromGroup(UserTransaction trans, String JavaDoc groupName, String JavaDoc username) throws RootException {
286         GenericValue member = getGroupMember(groupName, username);
287         if (member != null) {
288             try {
289                 member.remove();
290             } catch (GenericEntityException e) {
291                 Debug.logError(e, module);
292                 throw new RootException(e);
293             }
294         }
295     }
296
297     public void moveUser(UserTransaction trans, String JavaDoc currentGroup, String JavaDoc newGroup, String JavaDoc username) throws RootException {
298         this.removeUserFromGroup(trans, currentGroup, username);
299         this.addUserToGroup(trans, newGroup, username);
300     }
301
302     public boolean doesUserBelongToGroup(UserTransaction trans, String JavaDoc groupName, String JavaDoc username) throws RootException {
303         GenericValue member = getGroupMember(groupName, username);
304         if (member != null) {
305             return true;
306         }
307         return false;
308     }
309
310     public void createUser(UserTransaction trans, String JavaDoc groupName, String JavaDoc username, String JavaDoc password, String JavaDoc firstName, String JavaDoc lastName, String JavaDoc email) throws RootException {
311         GenericDelegator delegator = SharkContainer.getDelegator();
312         GenericValue user = delegator.makeValue("SharkUser", null);
313         user.set("userName", username);
314         user.set("firstName", firstName);
315         user.set("lastName", lastName);
316         user.set("passwd", password);
317         user.set("emailAddress", email);
318         try {
319             delegator.create(user);
320         } catch (GenericEntityException e) {
321             Debug.logError(e, module);
322             throw new RootException(e);
323         }
324         if (groupName != null) {
325             this.addUserToGroup(trans, groupName, username);
326         }
327     }
328
329     public void updateUser(UserTransaction trans, String JavaDoc username, String JavaDoc firstName, String JavaDoc lastName, String JavaDoc email) throws RootException {
330         GenericValue user = getUser(username);
331         if (user != null) {
332             user.set("firstName", firstName);
333             user.set("lastName", firstName);
334             user.set("emailAddress", email);
335             try {
336                 user.store();
337             } catch (GenericEntityException e) {
338                 Debug.logError(e, module);
339                 throw new RootException(e);
340             }
341         }
342     }
343
344     public void removeUser(UserTransaction trans, String JavaDoc username) throws RootException {
345         GenericValue user = getUser(username);
346         if (user != null) {
347             try {
348                 user.remove();
349             } catch (GenericEntityException e) {
350                 Debug.logError(e, module);
351                 throw new RootException(e);
352             }
353         }
354     }
355
356     public boolean doesUserExist(UserTransaction trans, String JavaDoc username) throws RootException {
357         GenericValue user = getUser(username);
358         if (user == null) {
359             return false;
360         }
361         return true;
362     }
363
364     public void setPassword(UserTransaction trans, String JavaDoc username, String JavaDoc password) throws RootException {
365         GenericValue user = getUser(username);
366         if (user != null) {
367             user.set("passwd", password);
368             try {
369                 user.store();
370             } catch (GenericEntityException e) {
371                 Debug.logError(e, module);
372                 throw new RootException(e);
373             }
374         }
375     }
376
377     public String JavaDoc getUserRealName(UserTransaction trans, String JavaDoc username) throws RootException {
378         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
379         GenericValue user = getUser(username);
380         if (!UtilValidate.isEmpty(user.getString("firstName"))) {
381             buf.append(user.getString("firstName"));
382
383         }
384         if (!UtilValidate.isEmpty(user.getString("lastName"))) {
385             if (buf.length() > 0) {
386                 buf.append(" ");
387             }
388             buf.append(user.getString("lastName"));
389         }
390         return buf.toString();
391     }
392
393     public String JavaDoc getUserFirstName(UserTransaction trans, String JavaDoc username) throws RootException {
394         GenericValue user = getUser(username);
395         return user.getString("firstName");
396     }
397
398     public String JavaDoc getUserLastName(UserTransaction trans, String JavaDoc username) throws RootException {
399         GenericValue user = getUser(username);
400         return user.getString("lastName");
401     }
402
403     public String JavaDoc getUserEMailAddress(UserTransaction trans, String JavaDoc username) throws RootException {
404         GenericValue user = getUser(username);
405         if (user != null) {
406             return user.getString("emailAddress");
407         }
408         return null;
409     }
410
411     private GenericValue getUser(String JavaDoc username) throws RootException {
412         GenericDelegator delegator = SharkContainer.getDelegator();
413         GenericValue value = null;
414         try {
415             value = delegator.findByPrimaryKey("SharkUser", UtilMisc.toMap("userName", username));
416         } catch (GenericEntityException e) {
417             Debug.logError(e, module);
418             throw new RootException(e);
419         }
420         return value;
421     }
422
423     private GenericValue getGroup(String JavaDoc groupName) throws RootException {
424         GenericDelegator delegator = SharkContainer.getDelegator();
425         GenericValue value = null;
426         try {
427             value = delegator.findByPrimaryKey("SharkGroup", UtilMisc.toMap("groupName", groupName));
428         } catch (GenericEntityException e) {
429             Debug.logError(e, module);
430             throw new RootException(e);
431         }
432         return value;
433     }
434
435     private GenericValue getGroupMember(String JavaDoc groupName, String JavaDoc username) throws RootException {
436         GenericDelegator delegator = SharkContainer.getDelegator();
437         GenericValue member = null;
438         try {
439             member = delegator.findByPrimaryKey("SharkGroupMember", UtilMisc.toMap("groupName", groupName, "userName", username));
440         } catch (GenericEntityException e) {
441             Debug.logError(e, module);
442             throw new RootException(e);
443         }
444         return member;
445     }
446
447     private GenericValue getGroupRollup(String JavaDoc parentGroup, String JavaDoc group) throws RootException {
448         GenericDelegator delegator = SharkContainer.getDelegator();
449         GenericValue rollup = null;
450         try {
451             rollup = delegator.findByPrimaryKey("SharkGroupRollup", UtilMisc.toMap("parentGroupName", parentGroup, "groupName", group));
452         } catch (GenericEntityException e) {
453             Debug.logError(e, module);
454             throw new RootException(e);
455         }
456         return rollup;
457     }
458 }
459
Popular Tags