KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > usergroup > HibernateUserGroupManager


1 /*
2  * Shark Hibernate UserGroup layer - Open Wide
3  */

4
5 package org.enhydra.shark.usergroup;
6
7 import java.util.ArrayList JavaDoc;
8 import java.util.HashSet JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.List JavaDoc;
11 import java.util.Set JavaDoc;
12
13 import net.sf.hibernate.Query;
14 import net.sf.hibernate.Session;
15
16 import org.enhydra.shark.api.RootException;
17 import org.enhydra.shark.api.UserTransaction;
18 import org.enhydra.shark.api.internal.usergroup.UserGroupManager;
19 import org.enhydra.shark.api.internal.working.CallbackUtilities;
20 import org.enhydra.shark.usertransaction.SharkHibernateUserTransaction;
21 import org.enhydra.shark.utilities.hibernate.HibernateUtilities;
22
23 /**
24  * Used for managing users.
25  * @author Vladislav Pernin
26  */

27 public class HibernateUserGroupManager implements UserGroupManager {
28
29     private CallbackUtilities callback;
30
31     //////////////////////////////////////////////////////////////////
32
// Initialization methods
33
//////////////////////////////////////////////////////////////////
34

35     public void configure(CallbackUtilities cus) throws RootException {
36         if (null == cus)
37             throw new RootException("Cannot configure without call back impl.");
38         callback = cus;
39         HibernateUtilities.init(callback.getProperties());
40         callback.debug("HibernateUserGroupManager configured");
41     }
42
43     //////////////////////////////////////////////////////////////////
44
// Persist methods
45
//////////////////////////////////////////////////////////////////
46

47     /**
48      * Creates a new user group.
49     *
50     * @param t user transaction.
51     * @param groupName name of the given group.
52     * @param description group description.
53     *
54     * @throws RootException If something unexpected happens.
55      */

56     public void createGroup(UserTransaction t, String JavaDoc groupName, String JavaDoc description) throws RootException {
57
58         if (doesGroupExistInternal(t, groupName)) {
59             throw new RootException("Group already exists");
60         }
61         try {
62             Session session = ((SharkHibernateUserTransaction) t).getSession();
63             HibernateGroup group = createGroup();
64             group.setGid(groupName);
65             group.setDescription(description);
66             session.save(group);
67             endTransaction(t, true);
68         } catch (Exception JavaDoc e) {
69             e.printStackTrace();
70             if (e instanceof RootException) {
71                 throw (RootException) e;
72             }
73             throw new RootException(e);
74         }
75     }
76
77     /**
78      * Allows administrator to update data about group.
79      *
80      * @param t user transaction.
81      * @param groupName group name.
82      * @param description group description.
83      *
84      * @throws RootException If something unexpected happens.
85      *
86      */

87     public void updateGroup(UserTransaction t, String JavaDoc groupName, String JavaDoc description) throws RootException {
88         try {
89             Session session = ((SharkHibernateUserTransaction) t).getSession();
90             HibernateGroup group = (HibernateGroup) session.get(HibernateGroup.class, groupName);
91             if (group == null)
92                 return;
93             group.setDescription(description);
94             session.update(group);
95             endTransaction(t, true);
96         } catch (Exception JavaDoc e) {
97             e.printStackTrace();
98             if (e instanceof RootException) {
99                 throw (RootException) e;
100             }
101             throw new RootException(e);
102         }
103     }
104
105     /**
106      * Allows administrator to create new user. After its creation, the client
107      * application will always be able to log onto shark using username and
108      * password defined for the user.
109      *
110      * @param t user transaction.
111      * @param groupName groupName used to uniquely identify group -
112      * this parameter is mandatory.
113      * @param username username used to uniquely identify user -
114      * this parameter is mandatory.
115      * @param password password used to authenticate -
116      * this parameter is mandatory.
117      * @param firstName user's first name.
118      * @param lastName user's last name.
119      * @param emailAddress email address of user.
120      *
121      * @throws RootException If something unexpected happens (i.e the user with
122      * given username already exists).
123      *
124      */

125     public void createUser (UserTransaction t,
126             String JavaDoc groupName,
127             String JavaDoc username,
128             String JavaDoc password,
129             String JavaDoc firstName,
130             String JavaDoc lastName,
131             String JavaDoc emailAddress) throws RootException {
132         if (doesUserExistInternal(t, username)) {
133             throw new RootException("User already exists");
134         }
135         try {
136             Session session = ((SharkHibernateUserTransaction) t).getSession();
137             HibernateGroup group = (HibernateGroup) session.get(HibernateGroup.class, groupName);
138             if (group == null)
139                 return;
140             HibernateUser user = createUser();
141             user.setUid(username);
142             user.setPasswd(passwordDigest(password));
143             user.setFirstName(firstName);
144             user.setLastName(lastName);
145             user.setEmail(emailAddress);
146             user.addGroup(group);
147             group.addUser(user);
148             session.save(user);
149             session.update(group);
150             endTransaction(t, true);
151         } catch (Exception JavaDoc e) {
152             e.printStackTrace();
153             if (e instanceof RootException) {
154                 throw (RootException) e;
155             }
156             throw new RootException(e);
157         }
158     }
159
160     /**
161      * Allows administrator to update data about user.
162      *
163      * @param t user transaction.
164      * @param username username used to uniquely identify user -
165      * this parameter is mandatory.
166      * @param firstName user's first name.
167      * @param lastName user's last name.
168      * @param emailAddress email address of user.
169      *
170      * @throws RootException If something unexpected happens (i.e the user with
171      * given username does not exist).
172      *
173      */

174     public void updateUser (UserTransaction t,
175             String JavaDoc username,
176             String JavaDoc firstName,
177             String JavaDoc lastName,
178             String JavaDoc emailAddress) throws RootException {
179         try {
180             Session session = ((SharkHibernateUserTransaction) t).getSession();
181             System.out.println("username to update : " + username);
182             HibernateUser user = null;
183             if (username != null) {
184                 user = (HibernateUser) session.get(HibernateUser.class, username);
185             }
186
187             if (user == null)
188                 return;
189
190             user.setFirstName(firstName);
191             user.setLastName(lastName);
192             user.setEmail(emailAddress);
193
194             session.update(user);
195
196             endTransaction(t, true);
197         } catch (Exception JavaDoc e) {
198             e.printStackTrace();
199             if (e instanceof RootException) {
200                 throw (RootException) e;
201             }
202             throw new RootException(e);
203         }
204     }
205
206     /**
207      * Adds an existing group <i>subgroupName</i> to a group <i>groupName</i>.
208      *
209      * @param t user transaction.
210      * @param groupName group name.
211      * @param subgroupName subgroup name.
212      *
213      * @throws RootException If something unexpected happens.
214      *
215      */

216     public void addGroupToGroup(UserTransaction t, String JavaDoc groupName, String JavaDoc subgroupName) throws RootException {
217         try {
218             Session session = ((SharkHibernateUserTransaction) t).getSession();
219
220             HibernateGroup group = (HibernateGroup) session.get(HibernateGroup.class, groupName);
221             HibernateGroup subGroup = (HibernateGroup) session.get(HibernateGroup.class, subgroupName);
222
223             System.out.println("Group :\n" + group.toString());
224             System.out.println("subGroup :\n" + subGroup.toString());
225
226             if ((group == null) || (subGroup == null))
227                 return;
228
229             group.addSubGroup(subGroup);
230
231             session.update(group);
232
233             endTransaction(t, true);
234         } catch (Exception JavaDoc e) {
235             e.printStackTrace();
236             if (e instanceof RootException) {
237                 throw (RootException) e;
238             }
239             throw new RootException(e);
240         }
241     }
242
243     /**
244      * Adds an existing user with a given username to a given group.
245      *
246      * @param t user transaction.
247      * @param groupName group name.
248      * @param username username used to uniquely identify shark user.
249      *
250      * @throws RootException If something unexpected happens.
251      *
252      */

253     public void addUserToGroup(UserTransaction t, String JavaDoc groupName, String JavaDoc username) throws RootException {
254         try {
255             Session session = ((SharkHibernateUserTransaction) t).getSession();
256
257             HibernateGroup group = (HibernateGroup) session.get(HibernateGroup.class, groupName);
258             HibernateUser user = (HibernateUser) session.get(HibernateUser.class, username);
259
260             if ((group == null) || (user == null))
261                 return;
262
263             group.addUser(user);
264             user.addGroup(group);
265
266             session.update(group);
267             // TODO voir si besoin d'updater user pour que le group apparaisse
268
session.update(user);
269
270             endTransaction(t, true);
271         } catch (Exception JavaDoc e) {
272             e.printStackTrace();
273             if (e instanceof RootException) {
274                 throw (RootException) e;
275             }
276             throw new RootException(e);
277         }
278     }
279
280     /**
281      * Moves group <i>subgroupName</i> from the group <i>currentParentGroup</i> to
282      * group <i>newParentGroup</i>.
283      *
284      * @param t user transaction.
285      * @param currentParentGroup current group that contains group subgroupName.
286      * @param newParentGroup new group where group subgroupName will be moved to.
287      * @param subgroupName subgroup that will be moved.
288      *
289      * @throws RootException If something unexpected happens.
290      *
291      */

292     public void moveGroup(UserTransaction t, String JavaDoc currentParentGroup, String JavaDoc newParentGroup, String JavaDoc subgroupName) throws RootException {
293         try {
294             Session session = ((SharkHibernateUserTransaction) t).getSession();
295
296             HibernateGroup subGroup = (HibernateGroup) session.get(HibernateGroup.class, subgroupName);
297             HibernateGroup currentGroup = (HibernateGroup) session.get(HibernateGroup.class, currentParentGroup);
298             HibernateGroup newGroup = (HibernateGroup) session.get(HibernateGroup.class, newParentGroup);
299
300             if ((subGroup == null) || (currentGroup == null) || (newGroup == null)){
301                 throw new RootException("Either currentParentGroup or newParentGroup or subgroupName doe not exists!");
302             }
303
304             currentGroup.removeSubGroup(subGroup);
305             newGroup.addSubGroup(subGroup);
306
307             session.update(subGroup);
308             session.update(currentGroup);
309             session.update(newGroup);
310
311             endTransaction(t, true);
312         } catch (Exception JavaDoc e) {
313             if (e instanceof RootException) {
314                 throw (RootException)e;
315             }
316             throw new RootException(e);
317         }
318     }
319
320     /**
321      * Moves user <i>username</i> from the group <i>currentGroup</i> to group
322      * <i>newGroup</i>.
323      *
324      * @param t user transaction.
325      * @param currentGroup current group that contains user.
326      * @param newGroup new group where user will be moved to.
327      * @param username user that will be moved.
328      *
329      * @throws RootException If something unexpected happens.
330      *
331      */

332     public void moveUser (UserTransaction t,String JavaDoc currentGroup,String JavaDoc newGroup,String JavaDoc username) throws RootException {
333         try {
334             Session session = ((SharkHibernateUserTransaction) t).getSession();
335             HibernateUser user = (HibernateUser) session.get(HibernateUser.class, username);
336             HibernateGroup currentGrp = (HibernateGroup) session.get(HibernateGroup.class, currentGroup);
337             HibernateGroup newGrp = (HibernateGroup) session.get(HibernateGroup.class, newGroup);
338
339             if ((user == null) || (currentGrp == null) || (newGrp == null)){
340                 throw new RootException("Either currentGroup or newGroup or user username doe not exists!");
341             }
342
343             currentGrp.removeUser(user);
344             newGrp.addUser(user);
345
346             session.update(user);
347             session.update(currentGrp);
348             session.update(newGrp);
349
350             endTransaction(t, true);
351         } catch (Exception JavaDoc e) {
352             if (e instanceof RootException) {
353                 throw (RootException)e;
354             }
355             throw new RootException(e);
356         }
357     }
358
359     /**
360      * Sets user password.
361      *
362      * @param t user transaction.
363      * @param username username of shark user.
364      * @param password new password of shark user.
365      *
366      * @throws RootException If something unexpected happens.
367      *
368      */

369     public void setPassword(UserTransaction t, String JavaDoc username, String JavaDoc password) throws RootException {
370         try {
371             Session session = ((SharkHibernateUserTransaction) t).getSession();
372             HibernateUser user = (HibernateUser) session.get(HibernateUser.class, username);
373
374             if (user == null)
375                 return;
376
377             user.setPasswd(passwordDigest(password));
378
379             session.update(user);
380
381             endTransaction(t, true);
382         } catch (Exception JavaDoc e) {
383             e.printStackTrace();
384             if (e instanceof RootException) {
385                 throw (RootException) e;
386             }
387             throw new RootException(e);
388         }
389     }
390
391     //////////////////////////////////////////////////////////////////
392
// Delete methods
393
//////////////////////////////////////////////////////////////////
394

395     /**
396      * Removes user group.
397      *
398      * @param t user transaction.
399      * @param groupName group name.
400      *
401      * @throws RootException If something unexpected happens.
402      *
403      */

404     public void removeGroup(UserTransaction t, String JavaDoc groupName) throws RootException {
405         try {
406             Session session = ((SharkHibernateUserTransaction) t).getSession();
407
408             if (groupName != "") {
409                 HibernateGroup group = (HibernateGroup) session.load(HibernateGroup.class, groupName);
410                 if ((group.getSubGroups().size() != 0) || (group.getUsers().size() != 0)) {
411                     return;
412                 }
413                 Set JavaDoc parentGroups = getParentGroups(t, groupName);
414                 if (parentGroups.size() != 0) {
415                     for (Iterator JavaDoc it = parentGroups.iterator(); it.hasNext();) {
416                         HibernateGroup groupTemp = (HibernateGroup) it.next();
417                         groupTemp.removeSubGroup(group);
418                     }
419                 }
420                 session.delete(group);
421             }
422
423             endTransaction(t, true);
424         } catch (Exception JavaDoc e) {
425             e.printStackTrace();
426             if (e instanceof RootException) {
427                 throw (RootException) e;
428             }
429             throw new RootException(e);
430         }
431     }
432
433     /**
434      * Removes group <i>subgroupName</i> from the group <i>groupName</i>.
435      *
436      * @param t user transaction.
437      * @param groupName group name.
438      * @param subgroupName subgroup name.
439      *
440      * @throws RootException If something unexpected happens.
441      *
442      */

443     public void removeGroupFromGroup(UserTransaction t, String JavaDoc groupName, String JavaDoc subgroupName) throws RootException {
444         try {
445             Session session = ((SharkHibernateUserTransaction) t).getSession();
446
447             HibernateGroup group = (HibernateGroup) session.get(HibernateGroup.class, groupName);
448             HibernateGroup subGroup = (HibernateGroup) session.get(HibernateGroup.class, subgroupName);
449
450             if ((group == null) || (subGroup == null))
451                 return;
452
453             group.removeSubGroup(subGroup);
454
455             session.update(group);
456
457             endTransaction(t, true);
458         } catch (Exception JavaDoc e) {
459             e.printStackTrace();
460             if (e instanceof RootException) {
461                 throw (RootException) e;
462             }
463             throw new RootException(e);
464         }
465     }
466      /**
467     * Deletes group <i>groupName</i> and all its child groups that don't belong
468     * to any other group except this one.
469     *
470     * @param t user transaction.
471     * @param groupName group name.
472     *
473     * @throws RootException If something unexpected happens.
474     */

475    public void removeGroupTree (UserTransaction t,String JavaDoc groupName) throws RootException {
476       throw new RootException("Not implemented");
477    }
478
479    /**
480     * Removes all users from group <i>group</i> that don't belong to any other
481     * group except this one.
482     *
483     * @param t user transaction.
484     * @param groupName group name.
485     *
486     * @throws RootException If something unexpected happens.
487     */

488    public void removeUsersFromGroupTree (UserTransaction t,String JavaDoc groupName) throws RootException {
489       throw new RootException("Not implemented");
490    }
491
492     /**
493      * Removes a user from a group.
494      *
495      * @param t user transaction.
496      * @param groupName group name.
497      * @param username username used to uniquely identify shark user.
498      *
499      * @throws RootException If something unexpected happens.
500      *
501      */

502     public void removeUserFromGroup(UserTransaction t, String JavaDoc groupName, String JavaDoc username) throws RootException {
503         try {
504             Session session = ((SharkHibernateUserTransaction) t).getSession();
505
506             HibernateGroup group = (HibernateGroup) session.get(HibernateGroup.class, groupName);
507             HibernateUser user = (HibernateUser) session.get(HibernateUser.class, username);
508
509             if ((group == null) || (user == null))
510                 return;
511
512             user.removeGroup(group);
513             session.update(user);
514
515             // TODO voir si il faut updater le group pour que le user s'en va
516

517             endTransaction(t, true);
518         } catch (Exception JavaDoc e) {
519             e.printStackTrace();
520             if (e instanceof RootException) {
521                 throw (RootException) e;
522             }
523             throw new RootException(e);
524         }
525     }
526
527     /**
528      * Allows administrator to remove user.
529      *
530      * @param t user transaction.
531      * @param username username used to uniquely identify user.
532      *
533      * @throws RootException If something unexpected happens (i.e the user with
534      * given username does not exist, or this is a user that can't be removed).
535      *
536      */

537     public void removeUser(UserTransaction t, String JavaDoc username) throws RootException {
538         try {
539             Session session = ((SharkHibernateUserTransaction) t).getSession();
540
541             if (username != "") {
542                 HibernateUser user = (HibernateUser) session.load(HibernateUser.class, username);
543                 List JavaDoc groups = user.getGroups();
544                 for (Iterator JavaDoc it = groups.iterator(); it.hasNext();) {
545                     user.removeGroup((HibernateGroup) it.next());
546                 }
547                 session.update(user);
548             }
549             endTransaction(t, true);
550         } catch (Exception JavaDoc e) {
551             if (e instanceof RootException) {
552                 throw (RootException) e;
553             }
554             throw new RootException(e);
555         }
556     }
557
558     //////////////////////////////////////////////////////////////////
559
// Query methods
560
//////////////////////////////////////////////////////////////////
561

562     /**
563      * Returns Ids of all user groups.
564      *
565      * @return an array of user group Ids.
566      *
567      * @throws RootException If something unexpected happens.
568      *
569      */

570     public List JavaDoc getAllGroupnames(UserTransaction t) throws RootException {
571         List JavaDoc ret = new ArrayList JavaDoc();
572         try {
573             Session session = ((SharkHibernateUserTransaction) t).getSession();
574             Query qGroupsQuery = session.createQuery("from HibernateGroup group");
575             List JavaDoc groups = qGroupsQuery.list();
576
577             if (groups.size() != 0) {
578                 for (Iterator JavaDoc it = groups.iterator(); it.hasNext();) {
579                     ret.add(((HibernateGroup) it.next()).getGid());
580                 }
581             }
582         } catch (Exception JavaDoc e) {
583             throw new RootException(e);
584         } finally {
585             endTransaction(t, false);
586         }
587         return ret;
588     }
589
590     private Set JavaDoc getAllGroupnamesInternal(UserTransaction t) throws RootException {
591         Set JavaDoc ret = new HashSet JavaDoc();
592         try {
593             Session session = ((SharkHibernateUserTransaction) t).getSession();
594             Query qGroupsQuery = session.createQuery("from HibernateGroup group");
595             List JavaDoc groups = qGroupsQuery.list();
596
597             if (groups.size() != 0) {
598                 for (Iterator JavaDoc it = groups.iterator(); it.hasNext();) {
599                     ret.add(((HibernateGroup) it.next()).getGid());
600                 }
601             }
602         } catch (Exception JavaDoc e) {
603             throw new RootException(e);
604         } finally {
605         }
606         return ret;
607     }
608
609     /**
610      * Returns Ids of all users.
611      *
612      * @return an array of user Ids.
613      *
614      * @throws RootException If something unexpected happens.
615      *
616      */

617     public List JavaDoc getAllUsers(UserTransaction t) throws RootException {
618         List JavaDoc ret = new ArrayList JavaDoc();
619         try {
620             Session session = ((SharkHibernateUserTransaction) t).getSession();
621             Query qUsersQuery = session.createQuery("from HibernateUser user");
622             List JavaDoc users = qUsersQuery.list();
623
624             if (users.size() != 0) {
625                 for (Iterator JavaDoc it = users.iterator(); it.hasNext();) {
626                     ret.add(((HibernateUser) it.next()).getUid());
627                 }
628             }
629         } catch (Exception JavaDoc e) {
630             throw new RootException(e);
631         } finally {
632             endTransaction(t, false);
633         }
634         return ret;
635     }
636
637     /**
638      * Returns all usernames that belong to given group.
639      *
640      * @param t user transaction.
641      * @param groupName group name.
642      *
643      * @return a List of all usernames that belong to given group.
644      *
645      * @throws RootException If something unexpected happens.
646      *
647      */

648     public List JavaDoc getAllUsers(UserTransaction t, String JavaDoc groupName) throws RootException {
649         List JavaDoc ret = new ArrayList JavaDoc();
650         Set JavaDoc retSet = new HashSet JavaDoc();
651         try {
652             Session session = ((SharkHibernateUserTransaction) t).getSession();
653             HibernateGroup group;
654             if (!groupName.equals("")) {
655                 group = (HibernateGroup) session.get(HibernateGroup.class, groupName);
656                 if (group == null)
657                     return ret;
658             } else {
659                 return ret;
660             }
661
662             List JavaDoc users = group.getUsers();
663             if (users.size() != 0) {
664                 for (Iterator JavaDoc it = users.iterator(); it.hasNext();) {
665                     retSet.add(((HibernateUser) it.next()).getUid());
666                 }
667             }
668
669             List JavaDoc subGroups = group.getSubGroups();
670             if (subGroups.size() != 0) {
671                 for (Iterator JavaDoc it = subGroups.iterator(); it.hasNext();) {
672                     retSet.addAll(getAllUsersInternal(t, ((HibernateGroup) it.next()).getGid()));
673                 }
674             }
675         } catch (Exception JavaDoc e) {
676             e.printStackTrace();
677             throw new RootException(e);
678         } finally {
679             endTransaction(t, false);
680         }
681         ret.addAll(retSet);
682         return ret;
683     }
684
685     private Set JavaDoc getAllUsersInternal(UserTransaction t, String JavaDoc groupName) throws RootException {
686         Set JavaDoc retSet = new HashSet JavaDoc();
687         try {
688             Session session = ((SharkHibernateUserTransaction) t).getSession();
689             HibernateGroup group;
690             if (!groupName.equals("")) {
691                 group = (HibernateGroup) session.get(HibernateGroup.class, groupName);
692                 if (group == null)
693                     return retSet;
694             } else {
695                 return retSet;
696             }
697
698             List JavaDoc users = group.getUsers();
699             if (users.size() != 0) {
700                 for (Iterator JavaDoc it = users.iterator(); it.hasNext();) {
701                     retSet.add(((HibernateUser) it.next()).getUid());
702                 }
703             }
704
705             List JavaDoc subGroups = group.getSubGroups();
706             if (subGroups.size() != 0) {
707                 for (Iterator JavaDoc it = subGroups.iterator(); it.hasNext();) {
708                     retSet.addAll(getAllUsersInternal(t, ((HibernateGroup) it.next()).getGid()));
709                 }
710             }
711         } catch (Exception JavaDoc e) {
712             e.printStackTrace();
713             throw new RootException(e);
714         } finally {
715         }
716         return retSet;
717     }
718
719     /**
720      * Returns all users that belong to given groups.
721      *
722      * @param t user transaction.
723      * @param groupName names of the given groups.
724      *
725      * @return a List of all users that belong to given groups.
726      *
727      * @throws RootException If something unexpected happens.
728      *
729      */

730     public List JavaDoc getAllUsers(UserTransaction t, List JavaDoc groupName) throws RootException {
731         List JavaDoc ret = new ArrayList JavaDoc();
732         for (Iterator JavaDoc it = groupName.iterator(); it.hasNext();) {
733             ret.addAll(getAllUsers(t, (String JavaDoc) it.next()));
734         }
735         return ret;
736     }
737
738     /**
739      * Returns all usernames that are immediate children of the given group.
740      *
741      * @param t user transaction.
742      * @param groupName group name.
743      *
744      * @return a List of aall immediate (direct) usernames that belong to given
745      *
746      * @throws RootException If something unexpected happens.
747      *
748      */

749     public List JavaDoc getAllImmediateUsers(UserTransaction t, String JavaDoc groupName) throws RootException {
750          throw new RootException("Not implemented");
751     }
752
753     /**
754      * Returns all groups that belong to given group.
755      *
756      * @param t user transaction.
757      * @param groupName group name.
758      *
759      * @return a List of all groups that belong to given group.
760      *
761      * @throws RootException If something unexpected happens.
762      *
763      */

764     public List JavaDoc getAllSubgroups(UserTransaction t, String JavaDoc groupName) throws RootException {
765         List JavaDoc ret = new ArrayList JavaDoc();
766         Set JavaDoc retSet = new HashSet JavaDoc();
767         try {
768             Session session = ((SharkHibernateUserTransaction) t).getSession();
769             HibernateGroup group;
770             if (!groupName.equals("")) {
771                 group = (HibernateGroup) session.get(HibernateGroup.class, groupName);
772                 if (group == null)
773                     return ret;
774             } else {
775                 return ret;
776             }
777
778             List JavaDoc subGroups = group.getSubGroups();
779             if (subGroups.size() != 0) {
780                 for (Iterator JavaDoc it = subGroups.iterator(); it.hasNext();) {
781                     String JavaDoc gid = ((HibernateGroup) it.next()).getGid();
782                     retSet.add(gid);
783                     retSet.addAll(getAllSubgroupsInternal(t, gid));
784                 }
785             }
786
787         } catch (Exception JavaDoc e) {
788             throw new RootException(e);
789         } finally {
790             endTransaction(t, false);
791         }
792         ret.addAll(retSet);
793         return ret;
794     }
795
796     private Set JavaDoc getAllSubgroupsInternal(UserTransaction t, String JavaDoc groupName) throws RootException {
797         Set JavaDoc retSet = new HashSet JavaDoc();
798         try {
799             Session session = ((SharkHibernateUserTransaction) t).getSession();
800             HibernateGroup group;
801             if (!groupName.equals("")) {
802                 group = (HibernateGroup) session.get(HibernateGroup.class, groupName);
803                 if (group == null)
804                     return retSet;
805             } else {
806                 return retSet;
807             }
808
809             List JavaDoc subGroups = group.getSubGroups();
810             System.out.println("subGroups : " + subGroups.toString());
811             if (subGroups.size() != 0) {
812                 for (Iterator JavaDoc it = subGroups.iterator(); it.hasNext();) {
813                     String JavaDoc gid = ((HibernateGroup) it.next()).getGid();
814                     retSet.add(gid);
815                     retSet.addAll(getAllSubgroupsInternal(t, gid));
816                 }
817             }
818
819         } catch (Exception JavaDoc e) {
820             throw new RootException(e);
821         } finally {
822         }
823         return retSet;
824     }
825
826      /**
827     * Returns all groups that are immediate children of the given group
828     * (which are on the first level bellow the level of the given group).
829     *
830     * @param t user transaction.
831     * @param groupName group name.
832     *
833     * @return List of all groups that are immediate children of the given group.
834     *
835     * @throws RootException If something unexpected happens.
836     *
837     */

838     public List JavaDoc getAllImmediateSubgroups(UserTransaction t, String JavaDoc groupName) throws RootException {
839         throw new RootException("Not implemented");
840     }
841
842     /**
843      * Returns all groups that belong to given groups.
844      *
845      * @param t user transaction.
846      * @param groupNames names of the given groups.
847      *
848      * @return a List of all groups that belong to given group.
849      *
850      * @throws RootException If something unexpected happens.
851      *
852      */

853     public List JavaDoc getAllSubgroups(UserTransaction t, List JavaDoc groupNames) throws RootException {
854         List JavaDoc ret = new ArrayList JavaDoc();
855         Set JavaDoc retSet = new HashSet JavaDoc();
856         for (Iterator JavaDoc it = groupNames.iterator(); it.hasNext();) {
857             retSet.addAll(getAllSubgroupsInternal(t, (String JavaDoc) it.next()));
858         }
859         ret.addAll(retSet);
860         return ret;
861     }
862
863     private Set JavaDoc getParentGroups(UserTransaction t, String JavaDoc groupName) throws RootException {
864         Set JavaDoc ret = new HashSet JavaDoc();
865         try {
866             Session session = ((SharkHibernateUserTransaction) t).getSession();
867             //TODO if this is to slow, it is possible to make a direct SQL
868
// search/query which should be quicker
869
Set JavaDoc allGroups = getAllGroupnamesInternal(t);
870             if (allGroups.size() != 0) {
871                 for (Iterator JavaDoc it = allGroups.iterator(); it.hasNext();) {
872                     String JavaDoc gid = (String JavaDoc) it.next();
873                     if (doesGroupBelongToGroupInternal(t, gid, groupName)) {
874                         ret.add((HibernateGroup) session.load(HibernateGroup.class, gid));
875                     }
876                 }
877             }
878         } catch (Exception JavaDoc e) {
879             e.printStackTrace();
880             if (e instanceof RootException) {
881                 throw (RootException) e;
882             }
883             throw new RootException(e);
884         }
885         return ret;
886     }
887
888     /**
889      * Returns true if user group with given name exists.
890      *
891      * @param t user transaction.
892      * @param groupName group name.
893      * @return true if user group exists, otherwise false.
894      *
895      * @throws RootException If something unexpected happens.
896      */

897     public boolean doesGroupExist(UserTransaction t, String JavaDoc groupName) throws RootException {
898         boolean ret = false;
899         try {
900             Session session = ((SharkHibernateUserTransaction) t).getSession();
901             if (!groupName.equals("") && ((HibernateGroup) session.get(HibernateGroup.class, groupName)) != null) {
902                 ret = true;
903             }
904             endTransaction(t, false);
905         } catch (Exception JavaDoc e) {
906             e.printStackTrace();
907             if (e instanceof RootException) {
908                 throw (RootException) e;
909             }
910             throw new RootException(e);
911         }
912         return ret;
913     }
914
915     private boolean doesGroupExistInternal(UserTransaction t, String JavaDoc groupName) throws RootException {
916         boolean ret = false;
917         try {
918             Session session = ((SharkHibernateUserTransaction) t).getSession();
919             if (!groupName.equals("") && ((HibernateGroup) session.get(HibernateGroup.class, groupName)) != null) {
920                 ret = true;
921             }
922         } catch (Exception JavaDoc e) {
923             e.printStackTrace();
924             if (e instanceof RootException) {
925                 throw (RootException) e;
926             }
927             throw new RootException(e);
928         }
929         return ret;
930     }
931
932     /**
933      * Returns true if group <i>subgroupName</i> is subgroup of group <i>groupName</i>.
934      *
935      * @param t user transaction.
936      * @param groupName group name.
937      * @param subgroupName subgroup name.
938      *
939      * @return true if group <i>subgroupName</i> is subgroup of group <i>groupName</i>,
940      * otherwise false.
941      * @throws RootException If something unexpected happens.
942      */

943     public boolean doesGroupBelongToGroup(UserTransaction t, String JavaDoc groupName, String JavaDoc subgroupName) throws RootException {
944         boolean ret = false;
945         try {
946             Session session = ((SharkHibernateUserTransaction) t).getSession();
947
948             HibernateGroup group = (HibernateGroup) session.get(HibernateGroup.class, groupName);
949             HibernateGroup subGroup = (HibernateGroup) session.get(HibernateGroup.class, subgroupName);
950
951             if ((group == null) || (subGroup == null))
952                 return ret;
953
954             ret = group.doesContainSubGroup(subGroup);
955
956             if (!ret) {
957                 List JavaDoc group_SubGroups = group.getSubGroups();
958                 if (group_SubGroups.size() != 0) {
959                     for (Iterator JavaDoc it = group_SubGroups.iterator(); it.hasNext() && !ret;) {
960                         ret = doesGroupBelongToGroupInternal(t, ((HibernateGroup) it.next()).getGid(), subGroup.getGid());
961                     }
962                 }
963             }
964             endTransaction(t, false);
965         } catch (Exception JavaDoc e) {
966             e.printStackTrace();
967             if (e instanceof RootException) {
968                 throw (RootException) e;
969             }
970             throw new RootException(e);
971         }
972         return ret;
973     }
974
975     private boolean doesGroupBelongToGroupInternal(UserTransaction t, String JavaDoc groupName, String JavaDoc subgroupName) throws RootException {
976         boolean ret = false;
977         try {
978             Session session = ((SharkHibernateUserTransaction) t).getSession();
979
980             HibernateGroup group = (HibernateGroup) session.get(HibernateGroup.class, groupName);
981             HibernateGroup subGroup = (HibernateGroup) session.get(HibernateGroup.class, subgroupName);
982
983             if ((group == null) || (subGroup == null))
984                 return ret;
985
986             ret = group.doesContainSubGroup(subGroup);
987
988             if (!ret) {
989                 List JavaDoc group_SubGroups = group.getSubGroups();
990                 if (group_SubGroups.size() != 0) {
991                     for (Iterator JavaDoc it = group_SubGroups.iterator(); it.hasNext() && !ret;) {
992                         ret = doesGroupBelongToGroupInternal(t, ((HibernateGroup) it.next()).getGid(), subGroup.getGid());
993                     }
994                 }
995             }
996         } catch (Exception JavaDoc e) {
997             e.printStackTrace();
998             if (e instanceof RootException) {
999                 throw (RootException) e;
1000            }
1001            throw new RootException(e);
1002        }
1003        return ret;
1004    }
1005
1006    /**
1007     * Returns a group description.
1008     *
1009     * @param t user transaction.
1010     * @param groupName group name.
1011     * @return group description.
1012     *
1013     * @throws RootException If something unexpected happens.
1014     */

1015    public String JavaDoc getGroupDescription(UserTransaction t, String JavaDoc groupName) throws RootException {
1016        String JavaDoc ret = null;
1017        try {
1018            Session session = ((SharkHibernateUserTransaction) t).getSession();
1019
1020            HibernateGroup group = (HibernateGroup) session.get(HibernateGroup.class, groupName);
1021            if (group == null)
1022                ret = "";
1023
1024            ret = group.getDescription();
1025
1026            endTransaction(t, false);
1027        } catch (Exception JavaDoc e) {
1028            e.printStackTrace();
1029            if (e instanceof RootException) {
1030                throw (RootException) e;
1031            }
1032            throw new RootException(e);
1033        }
1034        return ret;
1035    }
1036
1037    /**
1038     * Returns true if given user belongs to a given group.
1039     *
1040     * @param t user transaction.
1041     * @param groupName group name.
1042     * @param username username used to uniquely identify shark's user.
1043     * @return true if given user belongs to a given group, otherwise false.
1044     *
1045     * @throws RootException If something unexpected happens.
1046     */

1047    public boolean doesUserBelongToGroup(UserTransaction t, String JavaDoc groupName, String JavaDoc username) throws RootException {
1048        boolean ret = false;
1049        try {
1050            Session session = ((SharkHibernateUserTransaction) t).getSession();
1051
1052            HibernateGroup group = (HibernateGroup) session.get(HibernateGroup.class, groupName);
1053            HibernateUser user = (HibernateUser) session.get(HibernateUser.class, username);
1054
1055            if ((group == null) || (user == null))
1056                return ret;
1057
1058            ret = group.doesContainUser(user);
1059
1060            if (!ret) {
1061                List JavaDoc group_SubGroups = group.getSubGroups();
1062                if (group_SubGroups.size() != 0) {
1063                    for (Iterator JavaDoc it = group_SubGroups.iterator(); it.hasNext() && !ret;) {
1064                        ret = doesUserBelongToGroup(t, ((HibernateGroup) it.next()).getGid(), user.getUid());
1065                    }
1066                }
1067            }
1068            endTransaction(t, false);
1069        } catch (Exception JavaDoc e) {
1070            e.printStackTrace();
1071            if (e instanceof RootException) {
1072                throw (RootException) e;
1073            }
1074            throw new RootException(e);
1075        }
1076        return ret;
1077    }
1078
1079    /**
1080     * Returns true if user with given username exists.
1081     *
1082     * @param t user transaction.
1083     * @param username username of shark user.
1084     * @return true if user with given username exists, otherwise false.
1085     *
1086     * @throws RootException If something unexpected happens.
1087     */

1088    public boolean doesUserExist(UserTransaction t, String JavaDoc username) throws RootException {
1089        boolean ret = false;
1090        try {
1091            Session session = ((SharkHibernateUserTransaction) t).getSession();
1092            if (!username.equals("") && ((HibernateUser) session.get(HibernateUser.class, username)) != null) {
1093                ret = true;
1094            }
1095
1096            endTransaction(t, false);
1097        } catch (Exception JavaDoc e) {
1098            e.printStackTrace();
1099            if (e instanceof RootException) {
1100                throw (RootException) e;
1101            }
1102            throw new RootException(e);
1103        }
1104        return ret;
1105    }
1106
1107    private boolean doesUserExistInternal(UserTransaction t, String JavaDoc username) throws RootException {
1108        boolean ret = false;
1109        try {
1110            Session session = ((SharkHibernateUserTransaction) t).getSession();
1111            if (!username.equals("") && ((HibernateUser) session.get(HibernateUser.class, username)) != null) {
1112                ret = true;
1113            }
1114        } catch (Exception JavaDoc e) {
1115            e.printStackTrace();
1116            if (e instanceof RootException) {
1117                throw (RootException) e;
1118            }
1119            throw new RootException(e);
1120        }
1121        return ret;
1122    }
1123
1124    /**
1125     * Returns string representing the real name for the shark user with a given
1126     * username (first and last name).
1127     *
1128     * @param t user transaction.
1129     * @param username The username of shark user.
1130     * @return real name of shark user.
1131     *
1132     * @throws RootException If something unexpected happens.
1133     *
1134     */

1135    public String JavaDoc getUserRealName(UserTransaction t, String JavaDoc username) throws RootException {
1136        String JavaDoc ret = null;
1137        try {
1138            Session session = ((SharkHibernateUserTransaction) t).getSession();
1139            HibernateUser user = (HibernateUser) session.get(HibernateUser.class, username);
1140
1141            if (user == null)
1142                return ret;
1143
1144            String JavaDoc firstName = user.getFirstName();
1145            String JavaDoc lastName = user.getLastName();
1146            if (firstName != null) {
1147                if (lastName != null)
1148                    ret = firstName + " " + lastName;
1149            }
1150            else {
1151                if (lastName != null)
1152                    ret = lastName;
1153            }
1154
1155            endTransaction(t, false);
1156        } catch (Exception JavaDoc e) {
1157            e.printStackTrace();
1158            if (e instanceof RootException) {
1159                throw (RootException) e;
1160            }
1161            throw new RootException(e);
1162        }
1163        return ret;
1164    }
1165
1166    /**
1167     * Returns string representing the real name for the shark user with a given
1168     * username (first and last name).
1169     *
1170     * @param t user transaction.
1171     * @param username The username of shark user.
1172     * @return real name of shark user.
1173     *
1174     * @throws RootException If something unexpected happens.
1175     *
1176     */

1177    public String JavaDoc getUserFirstName (UserTransaction t,String JavaDoc username) throws RootException {
1178        String JavaDoc ret = null;
1179        try {
1180            Session session = ((SharkHibernateUserTransaction) t).getSession();
1181            HibernateUser user = (HibernateUser) session.get(HibernateUser.class, username);
1182
1183            if (user == null)
1184                return ret;
1185
1186            ret = user.getFirstName();
1187
1188            endTransaction(t, false);
1189        } catch (Exception JavaDoc e) {
1190            if (e instanceof RootException) {
1191                throw (RootException)e;
1192            }
1193            throw new RootException(e);
1194        }
1195        return ret;
1196    }
1197
1198    /**
1199     * Returns string representing user's last name.
1200     *
1201     * @param t user transaction.
1202     * @param username The username of shark user.
1203     * @return Last name of shark user.
1204     *
1205     * @throws RootException If something unexpected happens.
1206     *
1207     */

1208    public String JavaDoc getUserLastName (UserTransaction t,String JavaDoc username) throws RootException {
1209        String JavaDoc ret = null;
1210        try {
1211            Session session = ((SharkHibernateUserTransaction) t).getSession();
1212            HibernateUser user = (HibernateUser) session.get(HibernateUser.class, username);
1213
1214            if (user == null)
1215                return ret;
1216
1217            ret = user.getLastName();
1218
1219            endTransaction(t, false);
1220        } catch (Exception JavaDoc e) {
1221            if (e instanceof RootException) {
1222                throw (RootException)e;
1223            }
1224            throw new RootException(e);
1225        }
1226        return ret;
1227    }
1228
1229    /**
1230     * Returns string representing email address for the user with a given
1231     * username.
1232     *
1233     * @param t user transaction.
1234     * @param username The username of shark user.
1235     * @return email of shark user.
1236     *
1237     * @throws RootException If something unexpected happens.
1238     *
1239     */

1240    public String JavaDoc getUserEMailAddress(UserTransaction t, String JavaDoc username) throws RootException {
1241        String JavaDoc ret = null;
1242        try {
1243            Session session = ((SharkHibernateUserTransaction) t).getSession();
1244            HibernateUser user = (HibernateUser) session.get(HibernateUser.class, username);
1245
1246            if (user == null)
1247                return ret;
1248
1249            ret = user.getEmail();
1250
1251            endTransaction(t, false);
1252        } catch (Exception JavaDoc e) {
1253            e.printStackTrace();
1254            if (e instanceof RootException) {
1255                throw (RootException) e;
1256            }
1257            throw new RootException(e);
1258        }
1259        return ret;
1260    }
1261
1262    //////////////////////////////////////////////////////////////////
1263
// Factory methods
1264
//////////////////////////////////////////////////////////////////
1265

1266    private HibernateUser createUser() {
1267        return new HibernateUser();
1268    }
1269
1270    private HibernateGroup createGroup() {
1271        return new HibernateGroup();
1272    }
1273
1274    public HibernateUserGroupManager() {}
1275
1276    //////////////////////////////////////////////////////////////////
1277
// Utility methods
1278
//////////////////////////////////////////////////////////////////
1279

1280    private void endTransaction(UserTransaction t, boolean commitToo) throws RootException {
1281        try {
1282            if (commitToo) {
1283                t.commit();
1284            }
1285        } catch (Exception JavaDoc e) {
1286            throw new RootException(e);
1287        } finally {
1288            t.release();
1289        }
1290    }
1291
1292    private static String JavaDoc passwordDigest(String JavaDoc password) {
1293        // TODO: should we store plain text passwords, or ... ?
1294
// see HibernateAuthenticationManager, it must comply
1295
return password;
1296    }
1297
1298}
1299
Popular Tags