KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.enhydra.shark.usergroup;
2
3 import java.sql.SQLException JavaDoc;
4 import java.util.*;
5
6 import org.enhydra.dods.DODS;
7 import org.enhydra.shark.api.RootException;
8 import org.enhydra.shark.api.UserTransaction;
9 import org.enhydra.shark.api.internal.usergroup.UserGroupManager;
10 import org.enhydra.shark.api.internal.working.CallbackUtilities;
11 import org.enhydra.shark.usergroup.data.*;
12 import org.enhydra.shark.usertransaction.SharkDODSUserTransaction;
13 import org.enhydra.shark.utilities.MiscUtilities;
14 import org.enhydra.shark.utilities.dods.DODSUtilities;
15
16 import com.lutris.appserver.server.sql.*;
17
18 /**
19  * Used for managing users, groups and their relations.
20  *
21  * @author Sasa Bojanic, Vladimir Puskas, Tanja Jovanovic
22  * @version 0.1
23  */

24 public class DODSUserGroupManager implements UserGroupManager {
25
26    private static final String JavaDoc LDB_PARAM_NAME = "DODSUserGroupManager.DatabaseName";
27    private CallbackUtilities callback;
28    private LogicalDatabase db;
29
30    /**
31     * Public constructor ().
32     */

33    public DODSUserGroupManager () {
34       db = null;
35    }
36
37    /**
38     * Method configure is called at Shark start up, to configure
39     * implementation of DODSUserGroupManager.
40     *
41     * @param cus an instance of CallbackUtilities used to get
42     * properties for configuring usergroup manager in Shark.
43     *
44     * @exception RootException thrown if configuring doesn't succeed.
45     */

46    public void configure (CallbackUtilities cus) throws RootException {
47       if (null == cus)
48          throw new RootException("Cannot configure without call back impl.");
49       callback = cus;
50       DODSUtilities.init(callback.getProperties());
51       String JavaDoc dbName = callback
52          .getProperty(LDB_PARAM_NAME, DODS.getDatabaseManager().getDefaultDB());
53       try {
54          db = DODS.getDatabaseManager().findLogicalDatabase(dbName);
55       } catch (DatabaseManagerException e) {
56          throw new RootException("Couldn't find logical database.", e);
57       }
58       callback.debug("DODSUserGroupManager configured");
59    }
60
61    /* Returns Ids of all user groups.
62     *
63     * @param t user transaction.
64     * @return List of user group Ids.
65     *
66     * @throws RootException If something unexpected happens.
67     */

68    public List getAllGroupnames (UserTransaction t) throws RootException {
69       DBTransaction dbt = beginTransaction(t);
70       List ret = new ArrayList();
71       GroupQuery qry = new GroupQuery(dbt);
72       try {
73          while (true) {
74             GroupDO group = qry.getNextDO();
75             if (null == group)
76                break;
77             ret.add(group.getGroupid());
78          }
79       } catch (Exception JavaDoc e) {
80          throw new RootException(e);
81       } finally {
82          endTransaction(t, dbt, false);
83       }
84       return ret;
85    }
86
87    /**
88    * Returns Ids of all users.
89     *
90     * @param t user transaction.
91     * @return List of user Ids.
92     *
93     * @throws RootException If something unexpected happens.
94     */

95    public List getAllUsers (UserTransaction t) throws RootException {
96       DBTransaction dbt = beginTransaction(t);
97       List ret = new ArrayList();
98       UserQuery qry = new UserQuery(dbt);
99       try {
100          while (true) {
101             UserDO user = qry.getNextDO();
102             if (null == user) {
103                break;
104             }
105             ret.add(user.getUserid());
106          }
107       } catch (Exception JavaDoc e) {
108          throw new RootException(e);
109       } finally {
110          endTransaction(t, dbt, false);
111       }
112       return ret;
113    }
114
115    /**
116     * Returns all usernames that belong to the given group.
117     *
118     * @param t user transaction.
119     * @param groupName name of the given group.
120     * @return List of all usernames that belong to given group.
121     *
122     * @throws RootException If something unexpected happens.
123     */

124    public List getAllUsers (UserTransaction t,String JavaDoc groupName) throws RootException {
125       DBTransaction dbt = beginTransaction(t);
126       List ret = new ArrayList();
127       GroupQuery gqry = new GroupQuery(dbt);
128       try {
129          gqry.setQueryGroupid(groupName);
130          gqry.requireUniqueInstance();
131          GroupDO gd = gqry.getNextDO();
132
133          if (gd == null)
134            throw new RootException("Group " + groupName + "does not exist.");
135
136          UserLinkQuery lqry = new UserLinkQuery(dbt);
137          lqry.setQueryGroupid(gd);
138          while (true) {
139             UserLinkDO user = lqry.getNextDO();
140             if (null == user) {
141                break;
142             }
143             ret.add(user.getUserid().getUserid());
144          }
145
146          GroupLinkQuery groupLinkQ = new GroupLinkQuery(dbt);
147          groupLinkQ.setQueryGroupid(gd);
148          while (true) {
149             GroupLinkDO subGroup = groupLinkQ.getNextDO();
150             if (null == subGroup) {
151                break;
152             }
153             List l = getAllUsers(t,subGroup.getSub_gid().getGroupid());
154             if (!l.isEmpty()){
155                for (int i=0;i<l.size();i++){
156                   if (!ret.contains((String JavaDoc)l.get(i))){
157                      ret.add((String JavaDoc)l.get(i));
158                   }
159                }
160             }
161          }
162       } catch (Exception JavaDoc e) {
163          throw new RootException(e);
164       } finally {
165          endTransaction(t, dbt, false);
166       }
167       return ret;
168    }
169
170    /**
171     * Returns all users that belong to the given groups.
172     *
173     * @param t user transaction.
174     * @param groupNames names of the given groups.
175     * @return List of all users that belong to given groups.
176     *
177     * @throws RootException If something unexpected happens.
178     */

179    public List getAllUsers (UserTransaction t,List groupNames) throws RootException {
180       List ret = new ArrayList();
181       for (Iterator it = groupNames.iterator(); it.hasNext();) {
182          ret.addAll(getAllUsers(t, (String JavaDoc)it.next()));
183       }
184       return ret;
185    }
186
187    /**
188     * Returns all users that are immediate children of the given group.
189     *
190     * @param t user transaction.
191     * @param groupName name of the given group.
192     * @return List of all immediate (direct) users that belong to the given
193     * group.
194     *
195     * @throws RootException If something unexpected happens.
196     */

197    public List getAllImmediateUsers (UserTransaction t,String JavaDoc groupName) throws RootException {
198
199       DBTransaction dbt = beginTransaction(t);
200       List ret = new ArrayList();
201       GroupQuery gqry = new GroupQuery(dbt);
202       try {
203          gqry.setQueryGroupid(groupName);
204          gqry.requireUniqueInstance();
205          GroupDO gd = gqry.getNextDO();
206
207          if (gd == null)
208            throw new RootException("Group " + groupName + "does not exist.");
209
210          UserLinkQuery lqry = new UserLinkQuery(dbt);
211          lqry.setQueryGroupid(gd);
212          while (true) {
213             UserLinkDO user = lqry.getNextDO();
214             if (null == user) {
215                break;
216             }
217             ret.add(user.getUserid().getUserid());
218          }
219       } catch (Exception JavaDoc e) {
220          throw new RootException(e);
221       } finally {
222          endTransaction(t, dbt, false);
223       }
224       return ret;
225    }
226
227    /**
228     * Returns all groups that belong to the given group.
229     *
230     * @param t user transaction.
231     * @param groupName name of the given group.
232     * @return List of all groups that belong to the given group.
233     *
234     * @throws RootException If something unexpected happens.
235     */

236    public List getAllSubgroups (UserTransaction t, String JavaDoc groupName) throws RootException {
237       DBTransaction dbt = beginTransaction(t);
238       List ret = new ArrayList();
239       GroupQuery gqry = new GroupQuery(dbt);
240       try {
241           gqry.setQueryGroupid(groupName);
242           gqry.requireUniqueInstance();
243           GroupDO gd = gqry.getNextDO();
244
245           if (gd == null)
246            throw new RootException("Group " + groupName + "does not exist.");
247
248           GroupLinkQuery groupLinkQ = new GroupLinkQuery(dbt);
249           groupLinkQ.setQueryGroupid(gd);
250           while (true) {
251              GroupLinkDO subGroup = groupLinkQ.getNextDO();
252              if (null == subGroup) {
253                 break;
254              }
255              String JavaDoc subId = subGroup.getSub_gid().getGroupid();
256              if (!ret.contains(subId)) {
257                 ret.add(subId);
258                 List l = getAllSubgroups(t, subGroup.getSub_gid().getGroupid());
259                 if (!l.isEmpty()){
260                    for (int i=0;i<l.size();i++){
261                       if (!ret.contains((String JavaDoc)l.get(i))){
262                          ret.add((String JavaDoc)l.get(i));
263                       }
264                    }
265                 }
266              }
267           }
268        } catch (Exception JavaDoc e) {
269           throw new RootException(e);
270        } finally {
271           endTransaction(t, dbt, false);
272        }
273        return ret;
274     }
275
276    /**
277     * Returns all groups that belong to the given groups.
278     *
279     * @param t user transaction.
280     * @param groupNames names of the given groups.
281     * @return List of all groups that belong to the given groups.
282     *
283     * @throws RootException If something unexpected happens.
284     */

285    public List getAllSubgroups (UserTransaction t, List groupNames) throws RootException {
286       List ret = new ArrayList();
287       for (Iterator it = groupNames.iterator(); it.hasNext();) {
288          ret.addAll(getAllSubgroups(t, (String JavaDoc)it.next()));
289       }
290       return ret;
291    }
292    /**
293     * Returns all groups that are immediate children of the given group
294     * (which are on the first level bellow the level of the given group).
295     *
296     * @param t user transaction.
297     * @param groupName name of the given group.
298     * @return List of all groups that are immediate children of the given group.
299     *
300     * @throws RootException If something unexpected happens.
301     */

302    public List getAllImmediateSubgroups (UserTransaction t, String JavaDoc groupName) throws RootException {
303       DBTransaction dbt = beginTransaction(t);
304       List ret = new ArrayList();
305       GroupQuery gqry = new GroupQuery(dbt);
306       try {
307           gqry.setQueryGroupid(groupName);
308           gqry.requireUniqueInstance();
309           GroupDO gd = gqry.getNextDO();
310
311           if (gd == null)
312            throw new RootException("Group " + groupName + "does not exist.");
313
314           GroupLinkQuery groupLinkQ = new GroupLinkQuery(dbt);
315           groupLinkQ.setQueryGroupid(gd);
316           while (true) {
317              GroupLinkDO subGroup = groupLinkQ.getNextDO();
318              if (null == subGroup) {
319                 break;
320              }
321              ret.add(subGroup.getSub_gid().getGroupid());
322           }
323        } catch (Exception JavaDoc e) {
324           throw new RootException(e);
325        } finally {
326           endTransaction(t, dbt, false);
327        }
328        return ret;
329     }
330
331    /**
332     * Creates a new user group.
333     *
334     * @param t user transaction.
335     * @param groupName name of the given group.
336     * @param description group description.
337     *
338     * @throws RootException If something unexpected happens.
339     */

340    public void createGroup (UserTransaction t,
341                             String JavaDoc groupName,
342                             String JavaDoc description) throws RootException {
343
344       if (doesGroupExist(t,groupName)) {
345          throw new RootException("Group already exists");
346       }
347       DBTransaction dbt = beginTransaction(t);
348       try {
349          GroupDO group = GroupDO.createVirgin(dbt);
350          group.setGroupid(groupName);
351          group.setDescription(description);
352          group.save();
353          endTransaction(t, dbt, true);
354       } catch (Exception JavaDoc e) {
355          if (e instanceof RootException) {
356             throw (RootException)e;
357          }
358          throw new RootException(e);
359       }
360    }
361
362    /**
363     * Removes user group.
364     *
365     * @param t user transaction.
366     * @param groupName name of the given group.
367     *
368     * @throws RootException If something unexpected happens.
369     */

370    public void removeGroup (UserTransaction t,String JavaDoc groupName) throws RootException {
371       DBTransaction dbt = beginTransaction(t);
372       try {
373          GroupQuery qry = new GroupQuery(dbt);
374          qry.setQueryGroupid(groupName);
375          qry.requireUniqueInstance();
376          qry.getNextDO().delete();
377          endTransaction(t, dbt, true);
378       } catch (Exception JavaDoc e) {
379          if (e instanceof RootException) {
380             throw (RootException)e;
381          }
382          throw new RootException(e);
383       }
384    }
385
386    /**
387     * Returns true if user group with given name exists.
388     *
389     * @param t user transaction.
390     * @param groupName name of the given group.
391     * @return true if user group exists, otherwise false.
392     *
393     * @throws RootException If something unexpected happens.
394     */

395    public boolean doesGroupExist (UserTransaction t,String JavaDoc groupName) throws RootException {
396       boolean ret = false;
397       DBTransaction dbt = beginTransaction(t);
398       try {
399          GroupQuery qry = new GroupQuery(dbt);
400          qry.setQueryGroupid(groupName);
401          qry.requireUniqueInstance();
402          ret = null != qry.getNextDO();
403          endTransaction(t, dbt, false);
404       } catch (Exception JavaDoc e) {
405          if (e instanceof RootException) {
406             throw (RootException)e;
407          }
408          throw new RootException(e);
409       }
410       return ret;
411    }
412
413    /**
414     * Returns true if group <i>subgroupName</i> is subgroup of group <i>groupName</i>.
415     *
416     * @param t user transaction.
417     * @param groupName name of the given group.
418     * @param subgroupName name of the given subgroup.
419     * @return true if group <i>subgroupName</i> is subgroup of group <i>groupName</i>,
420     * otherwise false.
421     *
422     * @throws RootException If something unexpected happens.
423     */

424    public boolean doesGroupBelongToGroup (UserTransaction t,String JavaDoc groupName, String JavaDoc subgroupName) throws RootException {
425       boolean ret = false;
426       DBTransaction dbt = beginTransaction(t);
427       try {
428          GroupQuery gQry = new GroupQuery(dbt);
429          gQry.setQueryGroupid(groupName);
430          gQry.requireUniqueInstance();
431          GroupDO group = gQry.getNextDO();
432
433          if (group == null)
434            throw new RootException("Group " + groupName + "does not exist.");
435
436          GroupQuery gQry1 = new GroupQuery(dbt);
437          gQry1.setQueryGroupid(subgroupName);
438          gQry1.requireUniqueInstance();
439          GroupDO subgroup = gQry1.getNextDO();
440
441          if (subgroup == null)
442            throw new RootException("Group " + subgroupName + "does not exist.");
443
444          GroupLinkQuery lQry = new GroupLinkQuery(dbt);
445          lQry.setQueryGroupid(group);
446          lQry.setQuerySub_gid(subgroup);
447          lQry.requireUniqueInstance();
448          GroupLinkDO result = lQry.getNextDO();
449
450          if (result != null)
451            ret = true;
452          else {
453            GroupLinkQuery lQry1 = new GroupLinkQuery(dbt);
454            lQry1.setQueryGroupid(group);
455            result = lQry1.getNextDO();
456            while ((!ret) && (result != null)) {
457              ret = doesGroupBelongToGroup(t, result.getSub_gid(), subgroup);
458              result = lQry1.getNextDO();
459            }
460          }
461          endTransaction(t, dbt, false);
462       } catch (Exception JavaDoc e) {
463          if (e instanceof RootException) {
464             throw (RootException)e;
465          }
466          throw new RootException(e);
467       }
468       return ret;
469    }
470
471    private boolean doesGroupBelongToGroup (UserTransaction t,GroupDO group, GroupDO subgroup) throws RootException {
472       boolean ret = false;
473       DBTransaction dbt = beginTransaction(t);
474       try {
475          GroupLinkQuery lQry = new GroupLinkQuery(dbt);
476          lQry.setQueryGroupid(group);
477          lQry.setQuerySub_gid(subgroup);
478          lQry.requireUniqueInstance();
479          GroupLinkDO result = lQry.getNextDO();
480
481          if (result != null)
482            ret = true;
483          else {
484            GroupLinkQuery lQry1 = new GroupLinkQuery(dbt);
485            lQry1.setQueryGroupid(group);
486            result = lQry1.getNextDO();
487            while ((!ret) && (result != null)) {
488              ret = doesGroupBelongToGroup(t, result.getSub_gid(), subgroup);
489              result = lQry1.getNextDO();
490            }
491          }
492          endTransaction(t, dbt, false);
493       } catch (Exception JavaDoc e) {
494          if (e instanceof RootException) {
495             throw (RootException)e;
496          }
497          throw new RootException(e);
498       }
499       return ret;
500    }
501
502    /**
503     * Allows administrator to update data about group.
504     *
505     * @param t user transaction.
506     * @param groupName name of the given group.
507     * @param description group description.
508     *
509     * @throws RootException If something unexpected happens.
510     */

511    public void updateGroup (UserTransaction t,String JavaDoc groupName,String JavaDoc description) throws RootException {
512       DBTransaction dbt = beginTransaction(t);
513       try {
514          GroupQuery qry = new GroupQuery(dbt);
515          qry.setQueryGroupid(groupName);
516          qry.requireUniqueInstance();
517          GroupDO group = qry.getNextDO();
518
519          if (group == null)
520            throw new RootException("Group " + groupName + "does not exist.");
521
522          group.setDescription(description);
523          group.save();
524          endTransaction(t, dbt, true);
525       } catch (Exception JavaDoc e) {
526          if (e instanceof RootException) {
527             throw (RootException)e;
528          }
529          throw new RootException(e);
530       }
531    }
532
533    /**
534     * Adds an existing group <i>subgroupName</i> to the group <i>groupName</i>.
535     *
536     * @param t user transaction.
537     * @param groupName name of the given group.
538     * @param subgroupName name of the given subgroup to be added.
539     *
540     * @throws RootException If something unexpected happens.
541     */

542    public void addGroupToGroup (UserTransaction t,String JavaDoc groupName,String JavaDoc subgroupName) throws RootException {
543       DBTransaction dbt = beginTransaction(t);
544       try {
545          GroupQuery gQry = new GroupQuery(dbt);
546          gQry.setQueryGroupid(groupName);
547          gQry.requireUniqueInstance();
548          GroupDO group = gQry.getNextDO();
549
550          if (group == null)
551            throw new RootException("Group " + groupName + "does not exist.");
552
553          GroupQuery gQry1 = new GroupQuery(dbt);
554          gQry1.setQueryGroupid(subgroupName);
555          gQry1.requireUniqueInstance();
556          GroupDO subgroup = gQry1.getNextDO();
557
558          if (subgroup == null)
559            throw new RootException("Group " + subgroupName + "does not exist.");
560
561          GroupLinkDO GroupLink = GroupLinkDO.createVirgin(dbt);
562          GroupLink.setSub_gid(subgroup);
563          GroupLink.setGroupid(group);
564          GroupLink.save();
565
566          endTransaction(t, dbt, true);
567       } catch (Exception JavaDoc e) {
568          if (e instanceof RootException) {
569             throw (RootException)e;
570          }
571          throw new RootException(e);
572       }
573    }
574
575    /**
576     * Removes group <i>subgroupName</i> from the group <i>groupName</i>.
577     *
578     * @param t user transaction.
579     * @param groupName name of the given group.
580     * @param subgroupName name of the given subgroup to be removed.
581     *
582     * @throws RootException If something unexpected happens.
583     */

584    public void removeGroupFromGroup (UserTransaction t,String JavaDoc groupName,String JavaDoc subgroupName) throws RootException {
585       DBTransaction dbt = beginTransaction(t);
586       try {
587          GroupQuery gQry = new GroupQuery(dbt);
588          gQry.setQueryGroupid(groupName);
589          gQry.requireUniqueInstance();
590          GroupDO group = gQry.getNextDO();
591
592          if (group == null)
593            throw new RootException("Group " + groupName + "does not exist.");
594
595          GroupQuery gQry1 = new GroupQuery(dbt);
596          gQry1.setQueryGroupid(subgroupName);
597          gQry1.requireUniqueInstance();
598          GroupDO subgroup = gQry1.getNextDO();
599
600          if (subgroup == null)
601            throw new RootException("Group " + subgroupName + "does not exist.");
602
603          GroupLinkQuery lQry = new GroupLinkQuery(dbt);
604          lQry.setQuerySub_gid(subgroup);
605          lQry.setQueryGroupid(group);
606          lQry.requireUniqueInstance();
607          lQry.getNextDO().delete();
608
609          endTransaction(t, dbt, true);
610       } catch (Exception JavaDoc e) {
611          if (e instanceof RootException) {
612             throw (RootException)e;
613          }
614          throw new RootException(e);
615       }
616    }
617
618    /**
619     * Deletes group <i>groupName</i> and all its child groups that don't belong
620     * to any other group except this one.
621     *
622     * @param t user transaction.
623     * @param groupName name of the given group.
624     *
625     * @throws RootException If something unexpected happens.
626     */

627    public void removeGroupTree (UserTransaction t,String JavaDoc groupName) throws RootException {
628       DBTransaction dbt = beginTransaction(t);
629       try {
630          GroupQuery gQry = new GroupQuery(dbt);
631          gQry.setQueryGroupid(groupName);
632          gQry.requireUniqueInstance();
633          GroupDO group = gQry.getNextDO();
634
635          if (group == null)
636            throw new RootException("Group " + groupName + "does not exist.");
637
638          GroupLinkQuery groupLinkQ = new GroupLinkQuery(dbt);
639          groupLinkQ.setQueryGroupid(group);
640
641          while (true) {
642             GroupLinkDO groupLinkDo = groupLinkQ.getNextDO();
643             if (null == groupLinkDo) {
644                break;
645             }
646             removeChildTree(t, groupLinkDo);
647          }
648          removeUsersFromGroup(t, group);
649          group.delete();
650
651          endTransaction(t, dbt, true);
652       } catch (Exception JavaDoc e) {
653          if (e instanceof RootException) {
654             throw (RootException)e;
655          }
656          throw new RootException(e);
657       }
658    }
659
660    private void removeChildTree (UserTransaction t, GroupLinkDO groupLink) throws RootException {
661       DBTransaction dbt = beginTransaction(t);
662       try {
663          GroupDO group = groupLink.getSub_gid();
664
665          GroupLinkQuery gQry = new GroupLinkQuery(dbt);
666          gQry.setQueryGroupid(group);
667          GroupLinkDO subgroupLink = gQry.getNextDO();
668
669          while (subgroupLink != null) {
670             removeChildTree(t, subgroupLink);
671             subgroupLink = gQry.getNextDO();
672          }
673
674          GroupLinkQuery groupLinkQ = new GroupLinkQuery(dbt);
675          groupLinkQ.setQuerySub_gid(group);
676
677          int ii = groupLinkQ.getCount();
678
679          if (groupLinkQ.getCount() > 1) {
680             groupLink.delete();
681          }
682          else {
683             removeUsersFromGroup(t, group);
684             group.delete();
685          }
686
687          endTransaction(t, dbt, true);
688       } catch (Exception JavaDoc e) {
689          if (e instanceof RootException) {
690             throw (RootException)e;
691          }
692          throw new RootException(e);
693       }
694    }
695
696 /*
697    public void removeGroupTree (UserTransaction t,String groupName, boolean type) throws RootException {
698       DBTransaction dbt = beginTransaction(t);
699       try {
700
701          GroupQuery gQry = new GroupQuery(dbt);
702          gQry.setQueryGroupid(groupName);
703          gQry.requireUniqueInstance();
704          GroupDO group = gQry.getNextDO();
705
706          if (type) {
707             removeGroupTree(t, group, false);
708             removeUsersFromGroup(t, group);
709             group.delete();
710          }
711          else {
712             GroupLinkQuery groupLinkQ = new GroupLinkQuery(dbt);
713             groupLinkQ.setQueryGroupid(group);
714             while (true) {
715                GroupLinkDO groupLink_do = groupLinkQ.getNextDO();
716                if (null == groupLink_do) {
717                   break;
718                }
719                GroupLinkQuery subgroupLinkQ = new GroupLinkQuery(dbt);
720                subgroupLinkQ.setQuerySub_gid(groupLink_do.getSub_gid());
721                //GroupLinkDO subGroup = subgroupLinkQ.getNextDO();
722                if (subgroupLinkQ.getCount() > 1) {
723                   removeGroupTree(t, groupLink_do.getSub_gid().getGroupid(), false);
724                }
725                else {
726                   removeGroupTree(t, groupLink_do.getSub_gid().getGroupid(), true);
727                }
728             }
729          }
730
731          endTransaction(t, dbt, true);
732       } catch (Exception e) {
733          if (e instanceof RootException) {
734             throw (RootException)e;
735          }
736          throw new RootException(e);
737       }
738    }
739
740    private void removeGroupTree (UserTransaction t, GroupDO group, boolean type) throws RootException {
741       DBTransaction dbt = beginTransaction(t);
742       try {
743          if (type) {
744             removeGroupTree(t, group, false);
745             group.delete();
746          }
747          else {
748             GroupLinkQuery groupLinkQ = new GroupLinkQuery(dbt);
749             groupLinkQ.setQueryGroupid(group);
750             while (true) {
751                GroupLinkDO subGroup = groupLinkQ.getNextDO();
752                if (null == subGroup) {
753                   break;
754                }
755                GroupLinkQuery subgroupLinkQ = new GroupLinkQuery(dbt);
756                subgroupLinkQ.setQuerySub_gid(subGroup.getSub_gid());
757 //GroupLinkDO subGroup = subgroupLinkQ.getNextDO();
758                if (subgroupLinkQ.getCount() > 1) {
759                   removeGroupTree(t,subGroup.getSub_gid(), false);
760                }
761                else {
762                   removeGroupTree(t,subGroup.getSub_gid(), true);
763                }
764             } // while
765          }
766          endTransaction(t, dbt, true);
767       } catch (Exception e) {
768          if (e instanceof RootException) {
769             throw (RootException)e;
770          }
771          throw new RootException(e);
772       }
773    }
774 */

775
776    private void removeUsersFromGroup (UserTransaction t, GroupDO group) throws RootException {
777
778       DBTransaction dbt = beginTransaction(t);
779       try {
780          UserLinkQuery userLinkQ = new UserLinkQuery(dbt);
781          userLinkQ.setQueryGroupid(group);
782
783          UserLinkDO[] userLink_dos = userLinkQ.getDOArray();
784            for (int i = 0; i < userLink_dos.length; i++ ) {
785             UserLinkDO userLink_do = userLink_dos[i];
786             UserDO user_do = userLink_do.getUserid();
787
788             UserLinkQuery lQry = new UserLinkQuery(dbt);
789             lQry.setQueryUserid(user_do);
790
791             if (lQry.getCount() == 1) {
792                user_do.delete();
793             }
794             else {
795              userLink_do.delete();
796             }
797          }
798          endTransaction(t, dbt, true);
799       } catch (Exception JavaDoc e) {
800          if (e instanceof RootException) {
801             throw (RootException)e;
802          }
803          throw new RootException(e);
804       }
805    }
806
807
808
809    /**
810     * Removes all users from group <i>group</i> that don't belong to any other
811     * group except this one.
812     *
813     * @param t user transaction.
814     * @param groupName name of the given group.
815     *
816     * @throws RootException If something unexpected happens.
817     */

818    public void removeUsersFromGroupTree (UserTransaction t,String JavaDoc groupName) throws RootException {
819       DBTransaction dbt = beginTransaction(t);
820       try {
821          GroupQuery gQry = new GroupQuery(dbt);
822          gQry.setQueryGroupid(groupName);
823          gQry.requireUniqueInstance();
824          GroupDO group = gQry.getNextDO();
825
826          if (group == null)
827            throw new RootException("Group " + groupName + "does not exist.");
828
829          GroupLinkQuery groupLinkQ = new GroupLinkQuery(dbt);
830          groupLinkQ.setQueryGroupid(group);
831
832          while (true) {
833             GroupLinkDO groupLink_do = groupLinkQ.getNextDO();
834             if (null == groupLink_do) {
835                break;
836             }
837             removeUsersFromGroupTree(t, groupLink_do.getSub_gid());
838          }
839
840          removeUsersFromGroup(t, group);
841
842
843          endTransaction(t, dbt, true);
844       } catch (Exception JavaDoc e) {
845          if (e instanceof RootException) {
846             throw (RootException)e;
847          }
848          throw new RootException(e);
849       }
850    }
851
852    private void removeUsersFromGroupTree (UserTransaction t, GroupDO group) throws RootException {
853       DBTransaction dbt = beginTransaction(t);
854       try {
855
856          GroupLinkQuery groupLinkQ = new GroupLinkQuery(dbt);
857          groupLinkQ.setQueryGroupid(group);
858
859          while (true) {
860             GroupLinkDO groupLink_do = groupLinkQ.getNextDO();
861             if (null == groupLink_do) {
862                break;
863             }
864             removeUsersFromGroupTree(t, groupLink_do.getSub_gid());
865          }
866          removeUsersFromGroup(t, group);
867
868          endTransaction(t, dbt, true);
869       } catch (Exception JavaDoc e) {
870          if (e instanceof RootException) {
871             throw (RootException)e;
872          }
873          throw new RootException(e);
874       }
875    }
876
877    /**
878     * Moves group <i>subgroupName</i> from the group <i>currentParentGroup</i> to
879     * group <i>newParentGroup</i>.
880     *
881     * @param t user transaction.
882     * @param currentParentGroup current group that contains group subgroupName.
883     * @param newParentGroup new group where group subgroupName will be moved to.
884     * @param subgroupName subgroup that will be moved.
885     *
886     * @throws RootException If something unexpected happens.
887     */

888    public void moveGroup (UserTransaction t,String JavaDoc currentParentGroup,String JavaDoc newParentGroup,String JavaDoc subgroupName) throws RootException {
889       DBTransaction dbt = beginTransaction(t);
890       try {
891          GroupQuery gQry = new GroupQuery(dbt);
892          gQry.setQueryGroupid(subgroupName);
893          gQry.requireUniqueInstance();
894          GroupDO subgroup = gQry.getNextDO();
895
896          if (subgroup == null)
897            throw new RootException("Group " + subgroupName + "does not exist.");
898
899          GroupQuery gQry1 = new GroupQuery(dbt);
900          gQry1.setQueryGroupid(currentParentGroup);
901          gQry1.requireUniqueInstance();
902          GroupDO currentGroup = gQry1.getNextDO();
903
904          if (currentGroup == null)
905            throw new RootException("Group " + currentParentGroup + "does not exist.");
906
907          GroupQuery gQry2 = new GroupQuery(dbt);
908          gQry2.setQueryGroupid(newParentGroup);
909          gQry2.requireUniqueInstance();
910          GroupDO newGroup = gQry2.getNextDO();
911
912          if (newGroup == null)
913            throw new RootException("Group " + newParentGroup + "does not exist.");
914
915          GroupLinkQuery lQry = new GroupLinkQuery(dbt);
916          lQry.setQuerySub_gid(subgroup);
917          lQry.setQueryGroupid(currentGroup);
918          lQry.requireUniqueInstance();
919          lQry.getNextDO().delete();
920
921          GroupLinkDO GroupLink = GroupLinkDO.createVirgin(dbt);
922          GroupLink.setSub_gid(subgroup);
923          GroupLink.setGroupid(newGroup);
924          GroupLink.save();
925
926          endTransaction(t, dbt, true);
927       } catch (Exception JavaDoc e) {
928          if (e instanceof RootException) {
929             throw (RootException)e;
930          }
931          throw new RootException(e);
932       }
933    }
934
935    /**
936     * Returns a group description.
937     *
938     * @param t user transaction.
939     * @param groupName name of the given group.
940     * @return Group description.
941     *
942     * @throws RootException If something unexpected happens.
943     */

944    public String JavaDoc getGroupDescription (UserTransaction t,String JavaDoc groupName) throws RootException {
945       String JavaDoc ret = null;
946       DBTransaction dbt = beginTransaction(t);
947       try {
948          GroupQuery qry = new GroupQuery(dbt);
949          qry.setQueryGroupid(groupName);
950          qry.requireUniqueInstance();
951          ret = qry.getNextDO().getDescription();
952
953          endTransaction(t, dbt, false);
954       } catch (Exception JavaDoc e) {
955          if (e instanceof RootException) {
956             throw (RootException)e;
957          }
958          throw new RootException(e);
959       }
960       return ret;
961    }
962
963    /**
964     * Adds an existing user with a given username to the given group.
965     *
966     * @param t user transaction.
967     * @param groupName name of the given group.
968     * @param username username used to uniquely identify shark user.
969     *
970     * @throws RootException If something unexpected happens.
971     */

972    public void addUserToGroup (UserTransaction t,String JavaDoc groupName,String JavaDoc username) throws RootException {
973       DBTransaction dbt = beginTransaction(t);
974       try {
975          GroupQuery gQry = new GroupQuery(dbt);
976          gQry.setQueryGroupid(groupName);
977          gQry.requireUniqueInstance();
978          GroupDO group = gQry.getNextDO();
979
980          if (group == null)
981            throw new RootException("Group " + groupName + "does not exist.");
982
983          UserQuery uQry = new UserQuery(dbt);
984          uQry.setQueryUserid(username);
985          uQry.requireUniqueInstance();
986          UserDO user = uQry.getNextDO();
987
988          if (user == null)
989            throw new RootException("User " + username + "does not exist.");
990
991          UserLinkDO UserLink = UserLinkDO.createVirgin(dbt);
992          UserLink.setUserid(user);
993          UserLink.setGroupid(group);
994          UserLink.save();
995
996          endTransaction(t, dbt, true);
997       } catch (Exception JavaDoc e) {
998          if (e instanceof RootException) {
999             throw (RootException)e;
1000         }
1001         throw new RootException(e);
1002      }
1003   }
1004
1005   /**
1006    * Removes the user from the group.
1007    *
1008    * @param t user transaction.
1009    * @param groupName name of the given group.
1010    * @param username username used to uniquely identify shark user.
1011    *
1012    * @throws RootException If something unexpected happens.
1013    */

1014   public void removeUserFromGroup (UserTransaction t,String JavaDoc groupName,String JavaDoc username) throws RootException {
1015      DBTransaction dbt = beginTransaction(t);
1016      try {
1017         GroupQuery gQry = new GroupQuery(dbt);
1018         gQry.setQueryGroupid(groupName);
1019         gQry.requireUniqueInstance();
1020         GroupDO group = gQry.getNextDO();
1021
1022         if (group == null)
1023           throw new RootException("Group " + groupName + "does not exist.");
1024
1025         UserQuery uQry = new UserQuery(dbt);
1026         uQry.setQueryUserid(username);
1027         uQry.requireUniqueInstance();
1028         UserDO user = uQry.getNextDO();
1029
1030         if (user == null)
1031           throw new RootException("User " + username + "does not exist.");
1032
1033         UserLinkQuery lQry = new UserLinkQuery(dbt);
1034         lQry.setQueryUserid(user);
1035         lQry.setQueryGroupid(group);
1036         lQry.requireUniqueInstance();
1037         lQry.getNextDO().delete();
1038
1039         endTransaction(t, dbt, true);
1040      } catch (Exception JavaDoc e) {
1041         if (e instanceof RootException) {
1042            throw (RootException)e;
1043         }
1044         throw new RootException(e);
1045      }
1046   }
1047
1048   /**
1049    * Moves user <i>username</i> from the group <i>currentGroup</i> to group
1050    * <i>newGroup</i>.
1051    *
1052    * @param t user transaction.
1053    * @param currentGroup current group that contains the user.
1054    * @param newGroup new group where the user will be moved to.
1055    * @param username the user that will be moved.
1056    *
1057    * @throws RootException If something unexpected happens.
1058    */

1059   public void moveUser (UserTransaction t,String JavaDoc currentGroup,String JavaDoc newGroup,String JavaDoc username) throws RootException {
1060      DBTransaction dbt = beginTransaction(t);
1061      try {
1062         GroupQuery gQry1 = new GroupQuery(dbt);
1063         gQry1.setQueryGroupid(currentGroup);
1064         gQry1.requireUniqueInstance();
1065         GroupDO currentG = gQry1.getNextDO();
1066
1067         if (currentG == null)
1068           throw new RootException("Group " + currentGroup + "does not exist.");
1069
1070         GroupQuery gQry2 = new GroupQuery(dbt);
1071         gQry2.setQueryGroupid(newGroup);
1072         gQry2.requireUniqueInstance();
1073         GroupDO newG = gQry2.getNextDO();
1074
1075         if (newG == null)
1076           throw new RootException("Group " + newGroup + "does not exist.");
1077
1078         UserQuery uQry = new UserQuery(dbt);
1079         uQry.setQueryUserid(username);
1080         uQry.requireUniqueInstance();
1081         UserDO user = uQry.getNextDO();
1082
1083         if (user == null)
1084           throw new RootException("User " + username + "does not exist.");
1085
1086         UserLinkQuery lQry = new UserLinkQuery(dbt);
1087         lQry.setQueryUserid(user);
1088         lQry.setQueryGroupid(currentG);
1089         lQry.requireUniqueInstance();
1090         lQry.getNextDO().delete();
1091
1092         UserLinkDO UserLink = UserLinkDO.createVirgin(dbt);
1093         UserLink.setUserid(user);
1094         UserLink.setGroupid(newG);
1095         UserLink.save();
1096
1097         endTransaction(t, dbt, true);
1098      } catch (Exception JavaDoc e) {
1099         if (e instanceof RootException) {
1100            throw (RootException)e;
1101         }
1102         throw new RootException(e);
1103      }
1104
1105
1106   }
1107
1108   /**
1109    * Returns true if the given user belongs to the given group.
1110    *
1111    * @param t user transaction.
1112    * @param groupName name of the given group.
1113    * @param username username used to uniquely identify shark user.
1114    * @return true if the given user belongs to the given group, otherwise
1115    * false.
1116    *
1117    * @throws RootException If something unexpected happens.
1118    */

1119   public boolean doesUserBelongToGroup (UserTransaction t,String JavaDoc groupName,String JavaDoc username) throws RootException {
1120      boolean ret = false;
1121      DBTransaction dbt = beginTransaction(t);
1122      try {
1123         GroupQuery gQry = new GroupQuery(dbt);
1124         gQry.setQueryGroupid(groupName);
1125         gQry.requireUniqueInstance();
1126         GroupDO group = gQry.getNextDO();
1127
1128         if (group == null)
1129           throw new RootException("Group " + groupName + "does not exist.");
1130
1131         UserQuery uQry = new UserQuery(dbt);
1132         uQry.setQueryUserid(username);
1133         uQry.requireUniqueInstance();
1134         UserDO user = uQry.getNextDO();
1135
1136         if (user == null)
1137           throw new RootException("User " + username + "does not exist.");
1138
1139         UserLinkQuery lQry = new UserLinkQuery(dbt);
1140         lQry.setQueryUserid(user);
1141         lQry.setQueryGroupid(group);
1142         lQry.requireUniqueInstance();
1143// ret = null != lQry.getNextDO();
1144
UserLinkDO result = lQry.getNextDO();
1145
1146         if (result != null)
1147           ret = true;
1148         else {
1149           GroupLinkQuery lQry1 = new GroupLinkQuery(dbt);
1150           lQry1.setQueryGroupid(group);
1151           GroupLinkDO result1 = lQry1.getNextDO();
1152           while ((!ret) && (result1 != null)) {
1153             ret = doesUserBelongToGroup(t, result1.getSub_gid(), user);
1154             result1 = lQry1.getNextDO();
1155           }
1156         }
1157
1158         endTransaction(t, dbt, false);
1159      } catch (Exception JavaDoc e) {
1160         if (e instanceof RootException) {
1161            throw (RootException)e;
1162         }
1163         throw new RootException(e);
1164      }
1165      return ret;
1166   }
1167
1168   private boolean doesUserBelongToGroup (UserTransaction t,GroupDO group, UserDO user) throws RootException {
1169      boolean ret = false;
1170      DBTransaction dbt = beginTransaction(t);
1171      try {
1172         UserLinkQuery lQry = new UserLinkQuery(dbt);
1173         lQry.setQueryGroupid(group);
1174         lQry.setQueryUserid(user);
1175         lQry.requireUniqueInstance();
1176         UserLinkDO result = lQry.getNextDO();
1177
1178         if (result != null)
1179           ret = true;
1180         else {
1181           GroupLinkQuery lQry1 = new GroupLinkQuery(dbt);
1182           lQry1.setQueryGroupid(group);
1183           GroupLinkDO result1 = lQry1.getNextDO();
1184           while ((!ret) && (result1 != null)) {
1185             ret = doesUserBelongToGroup(t, result1.getSub_gid(), user);
1186             result1 = lQry1.getNextDO();
1187           }
1188         }
1189         endTransaction(t, dbt, false);
1190      } catch (Exception JavaDoc e) {
1191         if (e instanceof RootException) {
1192            throw (RootException)e;
1193         }
1194         throw new RootException(e);
1195      }
1196      return ret;
1197   }
1198
1199   /**
1200    * Allows administrator to create new user. After its creation, the client
1201    * application will always be able to log onto shark using username and
1202    * password defined for the user.
1203    *
1204    * @param t user transaction.
1205    * @param groupName groupName used to uniquely identify group -
1206    * this parameter is mandatory.
1207    * @param username username used to uniquely identify user -
1208    * this parameter is mandatory.
1209    * @param password password used to authenticate -
1210    * this parameter is mandatory.
1211    * @param firstName the user's first name.
1212    * @param lastName the user's last name.
1213    * @param emailAddress email address of the user.
1214    *
1215    * @throws RootException If something unexpected happens (i.e the user with
1216    * given username already exists).
1217    */

1218   public void createUser (UserTransaction t,
1219                           String JavaDoc groupName,
1220                           String JavaDoc username,
1221                           String JavaDoc password,
1222                           String JavaDoc firstName,
1223                           String JavaDoc lastName,
1224                           String JavaDoc emailAddress) throws RootException {
1225      if (doesUserExist(t,username)) {
1226         throw new RootException("User already exists");
1227      }
1228
1229      DBTransaction dbt = beginTransaction(t);
1230      try {
1231         GroupQuery gQry = new GroupQuery(dbt);
1232         gQry.setQueryGroupid(groupName);
1233         gQry.requireUniqueInstance();
1234         GroupDO group = gQry.getNextDO();
1235
1236         UserDO user = UserDO.createVirgin(dbt);
1237         user.setUserid(username);
1238         user.setPasswd(MiscUtilities.passwordDigest(password));
1239         user.setFirstname(firstName);
1240         user.setLastname(lastName);
1241         user.setEmail(emailAddress);
1242         user.save();
1243
1244         UserLinkDO UserLink = UserLinkDO.createVirgin(dbt);
1245         UserLink.setUserid(user);
1246         UserLink.setGroupid(group);
1247         UserLink.save();
1248
1249         endTransaction(t, dbt, true);
1250      } catch (Exception JavaDoc e) {
1251         if (e instanceof RootException) {
1252            throw (RootException)e;
1253         }
1254         throw new RootException(e);
1255      }
1256   }
1257
1258   /**
1259    * Allows administrator to update data about user.
1260    *
1261    * @param t user transaction.
1262    * @param username username used to uniquely identify user -
1263    * this parameter is mandatory.
1264    * @param firstName the user's first name.
1265    * @param lastName the user's last name.
1266    * @param emailAddress email address of the user.
1267    *
1268    * @throws RootException If something unexpected happens (i.e the user with
1269    * given username does not exist).
1270    */

1271   public void updateUser (UserTransaction t,
1272                           String JavaDoc username,
1273                           String JavaDoc firstName,
1274                           String JavaDoc lastName,
1275                           String JavaDoc emailAddress) throws RootException {
1276      DBTransaction dbt = beginTransaction(t);
1277      try {
1278         UserQuery uQry = new UserQuery(dbt);
1279         uQry.setQueryUserid(username);
1280         uQry.requireUniqueInstance();
1281         UserDO user = uQry.getNextDO();
1282
1283         if (user == null)
1284           throw new RootException("User " + username + "does not exist.");
1285
1286         user.setFirstname(firstName);
1287         user.setLastname(lastName);
1288         user.setEmail(emailAddress);
1289         user.save();
1290
1291         endTransaction(t, dbt, true);
1292      } catch (Exception JavaDoc e) {
1293         if (e instanceof RootException) {
1294            throw (RootException)e;
1295         }
1296         throw new RootException(e);
1297      }
1298   }
1299
1300   /**
1301    * Allows administrator to remove the user.
1302    *
1303    * @param t user transaction.
1304    * @param username username used to uniquely identify user.
1305    *
1306    * @throws RootException If something unexpected happens (i.e the user with
1307    * given username does not exist, or this is a user that can't be removed).
1308    */

1309   public void removeUser (UserTransaction t,String JavaDoc username) throws RootException {
1310      DBTransaction dbt = beginTransaction(t);
1311      try {
1312         UserQuery uQry = new UserQuery(dbt);
1313         uQry.setQueryUserid(username);
1314         uQry.requireUniqueInstance();
1315         uQry.getNextDO().delete();
1316
1317         endTransaction(t, dbt, true);
1318      } catch (Exception JavaDoc e) {
1319         if (e instanceof RootException) {
1320            throw (RootException)e;
1321         }
1322         throw new RootException(e);
1323      }
1324   }
1325
1326   /**
1327    * Returns true if user with given username exists.
1328    *
1329    * @param t user transaction.
1330    * @param username username of the shark user.
1331    * @return true if the user with the given username exists, otherwise false.
1332    *
1333    * @throws RootException If something unexpected happens.
1334    */

1335   public boolean doesUserExist (UserTransaction t,String JavaDoc username) throws RootException {
1336      boolean ret = false;
1337      DBTransaction dbt = beginTransaction(t);
1338      try {
1339         UserQuery uQry = new UserQuery(dbt);
1340         uQry.setQueryUserid(username);
1341         uQry.requireUniqueInstance();
1342         ret = null != uQry.getNextDO();
1343
1344         endTransaction(t, dbt, false);
1345      } catch (Exception JavaDoc e) {
1346         if (e instanceof RootException) {
1347            throw (RootException)e;
1348         }
1349         throw new RootException(e);
1350      }
1351      return ret;
1352   }
1353
1354   /**
1355    * Sets user password.
1356    *
1357    * @param t user transaction.
1358    * @param username username of the shark user.
1359    * @param password new password of the shark user.
1360    *
1361    * @throws RootException If something unexpected happens.
1362    */

1363   public void setPassword (UserTransaction t,String JavaDoc username,String JavaDoc password) throws RootException {
1364      DBTransaction dbt = beginTransaction(t);
1365      try {
1366         UserQuery uQry = new UserQuery(dbt);
1367         uQry.setQueryUserid(username);
1368         uQry.requireUniqueInstance();
1369         UserDO user = uQry.getNextDO();
1370         user.setPasswd(MiscUtilities.passwordDigest(password));
1371         user.save();
1372
1373         endTransaction(t, dbt, true);
1374      } catch (Exception JavaDoc e) {
1375         if (e instanceof RootException) {
1376            throw (RootException)e;
1377         }
1378         throw new RootException(e);
1379      }
1380   }
1381
1382   /**
1383    * Returns string representing the real name for the shark user with the
1384    * given username (first and last name).
1385    *
1386    * @param t user transaction.
1387    * @param username username of the shark user.
1388    * @return Real name of the shark user.
1389    *
1390    * @throws RootException If something unexpected happens.
1391    */

1392   public String JavaDoc getUserRealName (UserTransaction t,String JavaDoc username) throws RootException {
1393      String JavaDoc ret = null;
1394      DBTransaction dbt = beginTransaction(t);
1395      try {
1396         UserQuery uQry = new UserQuery(dbt);
1397         uQry.setQueryUserid(username);
1398         uQry.requireUniqueInstance();
1399         UserDO resultDO = uQry.getNextDO();
1400         ret = resultDO.getFirstname();
1401         String JavaDoc lastName = resultDO.getLastname();
1402         if (ret != null) {
1403            if (lastName != null)
1404              ret = ret + " " + lastName;
1405         }
1406         else {
1407          if (lastName != null)
1408              ret = lastName;
1409         }
1410
1411         endTransaction(t, dbt, false);
1412      } catch (Exception JavaDoc e) {
1413         if (e instanceof RootException) {
1414            throw (RootException)e;
1415         }
1416         throw new RootException(e);
1417      }
1418      return ret;
1419   }
1420
1421   /**
1422    * Returns string representing user's first name.
1423    *
1424    * @param t user transaction.
1425    * @param username username of the shark user.
1426    * @return First name of the shark user.
1427    *
1428    * @throws RootException If something unexpected happens.
1429    */

1430   public String JavaDoc getUserFirstName (UserTransaction t,String JavaDoc username) throws RootException {
1431      String JavaDoc ret = null;
1432      DBTransaction dbt = beginTransaction(t);
1433      try {
1434         UserQuery uQry = new UserQuery(dbt);
1435         uQry.setQueryUserid(username);
1436         uQry.requireUniqueInstance();
1437         ret = uQry.getNextDO().getFirstname();
1438
1439         endTransaction(t, dbt, false);
1440      } catch (Exception JavaDoc e) {
1441         if (e instanceof RootException) {
1442            throw (RootException)e;
1443         }
1444         throw new RootException(e);
1445      }
1446      return ret;
1447   }
1448
1449   /**
1450    * Returns string representing user's last name.
1451    *
1452    * @param t user transaction.
1453    * @param username username of the shark user.
1454    * @return Last name of the shark user.
1455    *
1456    * @throws RootException If something unexpected happens.
1457    */

1458   public String JavaDoc getUserLastName (UserTransaction t,String JavaDoc username) throws RootException {
1459      String JavaDoc ret = null;
1460      DBTransaction dbt = beginTransaction(t);
1461      try {
1462         UserQuery uQry = new UserQuery(dbt);
1463         uQry.setQueryUserid(username);
1464         uQry.requireUniqueInstance();
1465         ret = uQry.getNextDO().getLastname();
1466
1467         endTransaction(t, dbt, false);
1468      } catch (Exception JavaDoc e) {
1469         if (e instanceof RootException) {
1470            throw (RootException)e;
1471         }
1472         throw new RootException(e);
1473      }
1474      return ret;
1475   }
1476
1477   /**
1478    * Returns string representing email address for the user with the given
1479    * username.
1480    *
1481    * @param t user transaction.
1482    * @param username username of the shark user.
1483    * @return Email of the shark user.
1484    *
1485    * @throws RootException If something unexpected happens.
1486    */

1487   public String JavaDoc getUserEMailAddress (UserTransaction t,String JavaDoc username) throws RootException {
1488      String JavaDoc ret = null;
1489      DBTransaction dbt = beginTransaction(t);
1490      try {
1491         UserQuery uQry = new UserQuery(dbt);
1492         uQry.setQueryUserid(username);
1493         uQry.requireUniqueInstance();
1494         ret = uQry.getNextDO().getEmail();
1495
1496         endTransaction(t, dbt, false);
1497      } catch (Exception JavaDoc e) {
1498         if (e instanceof RootException) {
1499            throw (RootException)e;
1500         }
1501         throw new RootException(e);
1502      }
1503      return ret;
1504   }
1505
1506   private void endTransaction(UserTransaction t,
1507                               DBTransaction dbt,
1508                               boolean commitToo) throws RootException {
1509      if (!dbt.getDatabaseName().equals(db.getName())) {
1510         try {
1511            if (commitToo) {
1512               dbt.commit();
1513            }
1514         } catch (SQLException JavaDoc e) {
1515            throw new RootException(e);
1516         } finally {
1517            dbt.release();
1518         }
1519      }
1520   }
1521   private DBTransaction beginTransaction(UserTransaction t) throws RootException {
1522      DBTransaction dbt = ((SharkDODSUserTransaction)t).getDODSTransaction();
1523      if (!dbt.getDatabaseName().equals(db.getName())) {
1524         try {
1525            dbt = db.createTransaction();
1526         } catch (SQLException JavaDoc e) {
1527            throw new RootException(e);
1528         }
1529      }
1530      return dbt;
1531   }
1532}
1533
Popular Tags