KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > adminGui > feature > account > groups > GroupAccount


1 //// You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
package org.ozoneDB.adminGui.feature.account.groups;
8
9 import java.util.Iterator JavaDoc;
10 import java.util.Vector JavaDoc;
11
12 import org.ozoneDB.core.Group;
13 import org.ozoneDB.core.User;
14 import org.ozoneDB.core.UserManagerException;
15 import org.ozoneDB.DxLib.DxCollection;
16 import org.ozoneDB.DxLib.DxIterator;
17 import org.ozoneDB.adminGui.feature.account.users.UserItem;
18 import org.ozoneDB.adminGui.widget.MessageBox;
19 import org.ozoneDB.adminGui.main.AdminGui;
20
21
22 //#############################################################################
23
/**
24  * This class manages the groups accounts and takes care of communicating with
25  * the database.
26  *
27  * @author <p align=center>Ibsen Ramos-Bonilla
28  * <br>Copyright &copy 1997-@year@ by SMB GmbH. All Rights Reserved.</p>
29  *
30  * @version 1.0
31  */

32 //#############################################################################
33

34 public class GroupAccount implements IGroup {
35
36     /** Handle to the parent GroupPanel object. */
37     private GroupPanel parent = null;
38
39
40     /**
41      * Overloaded constructor with a handle to the parent.
42      *
43      * @param parent - a handle to the parent GroupPanel object.
44      */

45     public GroupAccount(GroupPanel parent) {
46         this.parent = parent;
47     }
48
49     /**
50      * This method retrieves an id that is not in use.
51      *
52      * @return int - unused group id.
53      */

54     private int fetchId() {
55
56         int id = -1;
57
58         //load the collection from the database
59
DxCollection groups = this.allGroups();
60
61         //didn't found any groups set id to one more than threshold
62
if (groups.isEmpty() | groups == null) {
63             id = ++AdminGui.instance().ID_THRESHOLD;
64         }
65
66         //found some groups, find the next empty id and use it.
67
else {
68             boolean match = false;
69             id = ++AdminGui.instance().ID_THRESHOLD;
70
71             while (true) {
72
73                 for (DxIterator it = groups.iterator(); it.next() != null;) {
74                     if (id == ((Group) it.object()).id().intValue()) {
75                         match = true;
76                         break;
77                     }
78                 }
79
80                 //found match increment id and test again
81
if (match) {
82                     match = false;
83                     ++id;
84                 }
85
86                 //didn't found a match get out and run like a chicken
87
else
88                     break;
89             }
90         }
91
92         return id;
93     }
94
95     /**
96      * This method creates a new group.
97      */

98     public void create() {
99         if (this.parent != null) {
100
101             String JavaDoc name = "";
102             Vector JavaDoc assignedUsers = null;
103
104             try {
105                 //display the work group dialog
106
WorkGroupDialog workGroupDialog = new WorkGroupDialog(parent.getAllUsers());
107                 workGroupDialog.show();
108
109                 //retrieve info from dialog
110
name = workGroupDialog.getName();
111                 assignedUsers = workGroupDialog.getUsers();
112                 boolean isOK = workGroupDialog.isOK();
113                 workGroupDialog.dispose();
114
115                 //get the group info
116
if (isOK) {
117                     int id = fetchId();
118                     createGroup(name, id, assignedUsers);
119                 }
120             } catch (Exception JavaDoc e) {
121                 e.printStackTrace();
122                 MessageBox.showError("Create Group", "Failed to create the " +
123                         name + " group.");
124             }
125         }
126     }
127
128     /**
129      * This method removes a group from the database.
130      */

131     public void remove() {
132         if (this.parent != null) {
133             //find the object that is selected
134
GroupItem gItem = (GroupItem) this.parent.getTree().
135                     getLastSelectedPathComponent();
136
137             if (gItem != null) {
138                 //find the group name and remove it
139
int id = gItem.getId();
140                 String JavaDoc name = gItem.toString();
141
142                 //remove the group if it's higher than the id threshold
143
try {
144                     if (id > AdminGui.instance().ID_THRESHOLD) {
145                         AdminGui.instance().getAdmin().removeGroup(name);
146                         this.parent.getTreeModel().getRootItem().removeChild(
147                                 gItem);
148                     } else
149                         MessageBox.showWarning("Remove Group", "This group " +
150                                 "cannot be removed!");
151                 } catch (Exception JavaDoc e) {
152                     e.printStackTrace();
153                     MessageBox.showError("Removing Group", "Failed to remove the "
154                             + name + " group.");
155                 } finally {
156                     this.parent.refreshTree();
157                 }
158             } else {
159                 MessageBox.showWarning("Remove Group", "Please select a " +
160                         "group from the Groups tree to be deleted.");
161             }
162
163         }
164     }
165
166     /**
167      * This method assigns an account to a selected group.
168      */

169     public void assign() {
170         if (this.parent != null) {
171             //find the object that is selected
172
GroupItem gItem = (GroupItem) this.parent.getTree().getLastSelectedPathComponent();
173
174             if (gItem != null) {
175                 //find the group name and assigned users
176
String JavaDoc name = gItem.toString();
177                 Vector JavaDoc users = gItem.getAssignedUsers();
178
179                 try {
180                     //display the work group dialog
181
WorkGroupDialog workGroupDialog = new WorkGroupDialog(name, this.parent.getAllUsers(), users);
182                     workGroupDialog.show();
183
184                     //retrieve info from dialog
185
users = workGroupDialog.getUsers();
186                     boolean isOK = workGroupDialog.isOK();
187                     workGroupDialog.dispose();
188
189                     //get the group info
190
if (isOK) {
191                         //if we have any users to add do it now
192
if (users != null)
193                             this.assignUsers(gItem, users);
194                         this.parent.refreshTree();
195                     }
196                 } catch (Exception JavaDoc e) {
197                     e.printStackTrace();
198                     MessageBox.showError("Assign User", "Failed to assign " +
199                             "the selected users.");
200                 }
201             }
202         }
203     }
204
205     /**
206      * This method unassigns an account from a selected group.
207      */

208     public void unassign() {
209         if (this.parent != null) {
210             //find the object that is selected
211
UserItem uItem = (UserItem) this.parent.getTree().getLastSelectedPathComponent();
212             this.unassign(uItem);
213         }
214     }
215
216     /**
217      * This method unassigns an account from a selected group.
218      */

219     public void unassign(UserItem uItem) {
220         if (this.parent != null) {
221             if (uItem != null) {
222 //find the account name and unassign it
223
int id = uItem.getId();
224                 String JavaDoc name = uItem.toString();
225                 int groupId = uItem.getGroup().getId();
226                 String JavaDoc groupName = uItem.getGroup().toString();
227
228 //remove the account if it's higher than the id threshold
229
try {
230                     if ((id > AdminGui.instance().ID_THRESHOLD) ||
231                             ((id <= AdminGui.instance().ID_THRESHOLD) &&
232                             (groupId > AdminGui.instance().ID_THRESHOLD))) {
233 //remove the account from group
234
AdminGui.instance().getAdmin().removeUserFromGroup(name, groupName);
235 //remove the account from the tree items
236
uItem.getGroup().removeChild(uItem);
237                         uItem.orphan();
238
239                         this.parent.getTreeModel().getRootItem().removeChild(uItem);
240                     } else
241                         MessageBox.showWarning("Unassign User", "This account " +
242                                 "cannot be unassign from this group!");
243                 } catch (Exception JavaDoc e) {
244                     e.printStackTrace();
245                     MessageBox.showError("Unassign account", "Failed to remove " +
246                             " account: " + name + ".");
247                 } finally {
248                     this.parent.refreshTree();
249                 }
250             } else {
251                 MessageBox.showWarning("Unassign User", "Please select an " +
252                         "account from a Group to be unassigned.");
253             }
254         }
255     }
256
257     /**
258      * This method lists all the groups in the database.
259      */

260     public void list() {
261         //load the collection from the database
262
DxCollection groups = this.allGroups();
263
264         //didn't found any users; do like a tree and leave.
265
if (groups.isEmpty() | groups == null) {
266             MessageBox.showInfo("List Groups", "No groups found.");
267         }
268
269         //found some users, return the list.
270
else {
271             if (this.parent != null) {
272                 this.parent.getTreeModel().clearGroups();
273                 this.populateGroups(groups);
274                 this.parent.refreshTree();
275             }
276         }
277     }
278
279     /**
280      * This method gets the collection of groups.
281      *
282      * @return DxCollection - list of groups in database.
283      */

284     public DxCollection allGroups() {
285         DxCollection groups = null;
286
287         //load the collection from the database
288
try {
289             groups = AdminGui.instance().getAdmin().allGroups();
290             return groups;
291         } catch (Exception JavaDoc e) {
292             e.printStackTrace();
293             MessageBox.showError("List Groups", "Unable to retrieve all groups: " + e.getMessage());
294             return groups;
295         }
296     }
297
298     /**
299      * This method creates a new group.
300      *
301      * @param name - the name for the new group.
302      * @param id - the id for the new group.
303      */

304     private void createGroup(String JavaDoc name, int id, Vector JavaDoc users)
305             throws Exception JavaDoc {
306
307         //first carete the group
308
AdminGui.instance().getAdmin().newGroup(name, id);
309         GroupItem gItem = new GroupItem(name, id);
310
311         //if we have any users to add do it now
312
if (users != null)
313             this.assignUsers(gItem, users);
314
315         this.parent.getTreeModel().getRootItem().addChild(gItem);
316         this.parent.refreshTree();
317     }
318
319     /**
320      * This is the method that actually populates the groups into the tree.
321      *
322      * @param groups - the collection of groups objects in the database.
323      */

324     public void populateGroups(DxCollection groups) {
325         //check that we have any groups
326
if (groups != null) {
327             Group group;
328
329             for (DxIterator it = groups.iterator(); it.next() != null;) {
330                 //get next group in the collection
331
group = (Group) it.object();
332
333                 //add the group
334
GroupItem item = new GroupItem(group.name(),
335                         group.id().intValue());
336
337                 this.parent.getTreeModel().getRootItem().addChild(item);
338
339                 //add the users under this group
340
populateUsers(group, item);
341             }
342         }
343     }
344
345     /**
346      * This is the method that actually populates each group in the tree.
347      *
348      * @param group - one group out of the groups collection.
349      * @param gItem - the group node having users added to.
350      */

351     private void populateUsers(Group group, GroupItem gItem) {
352         if (group != null) {
353             //add the users under this group
354
DxCollection ids = group.userIDs();
355
356             //check that we have any users
357
if (ids != null) {
358                 User user;
359                 int userId = 0;
360
361                 for (DxIterator it = ids.iterator(); it.next() != null;) {
362                     //get next account id in the collection
363

364                     userId = ((Integer JavaDoc) it.object()).intValue();
365                     try {
366                         user = AdminGui.instance().getAdmin().userForId(userId);
367
368                         //add the account
369
UserItem item = new UserItem(gItem, user.name(), userId);
370                         gItem.addChild(item);
371                     } catch (Exception JavaDoc e) {
372                         e.printStackTrace();
373                     }
374                 }
375             }
376         }
377     }
378
379     /**
380      * This is the method that assigns users to a new group in the tree.
381      *
382      * @param gItem - one group out of the groups collection.
383      * @param users - the list of users to add.
384      */

385     public void assignUsers(GroupItem gItem, Vector JavaDoc users) {
386
387         //check that we have any users
388
if (gItem != null && users != null) {
389             User user;
390             String JavaDoc userName = "";
391
392             //remove all the users from the group first
393
removeUnassignedUsers(gItem, users);
394
395             //proceed to add children again
396
Iterator JavaDoc it = users.iterator();
397             while (it.hasNext()) {
398                 //get next account id in the collection
399
userName = (String JavaDoc) it.next();
400
401                 try {
402                     user = AdminGui.instance().getAdmin().
403                             userForName(userName);
404
405                     //add the account
406
AdminGui.instance().getAdmin().addUser2Group(userName,
407                             gItem.toString());
408                     UserItem item = new UserItem(gItem, userName,
409                             user.id().intValue());
410                     gItem.addChild(item);
411                 } catch (UserManagerException ume) {
412                     //continue adding other users
413
} catch (Exception JavaDoc e) {
414                     e.printStackTrace();
415                 }
416             }
417         }
418     }
419
420     /**
421      * This method gets rid of all the users belonging to a group.
422      *
423      * @param users - list of assigned users.
424      */

425     private void removeUnassignedUsers(GroupItem gItem, Vector JavaDoc users) {
426         if (gItem != null) {
427             Vector JavaDoc originalUsers = gItem.getAssignedUsers();
428
429             Iterator JavaDoc it = originalUsers.iterator();
430             while (it.hasNext()) {
431                 Object JavaDoc user = it.next();
432
433                 if (!users.contains(user)) {
434                     unassign(gItem.getUser((String JavaDoc) user));
435                 }
436             }
437         }
438     }
439
440 } //--------------------------------- E O F -----------------------------------
441
Popular Tags