1 package org.ozoneDB.adminGui.feature.account.groups; 8 9 import java.util.Iterator ; 10 import java.util.Vector ; 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 32 34 public class GroupAccount implements IGroup { 35 36 37 private GroupPanel parent = null; 38 39 40 45 public GroupAccount(GroupPanel parent) { 46 this.parent = parent; 47 } 48 49 54 private int fetchId() { 55 56 int id = -1; 57 58 DxCollection groups = this.allGroups(); 60 61 if (groups.isEmpty() | groups == null) { 63 id = ++AdminGui.instance().ID_THRESHOLD; 64 } 65 66 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 if (match) { 82 match = false; 83 ++id; 84 } 85 86 else 88 break; 89 } 90 } 91 92 return id; 93 } 94 95 98 public void create() { 99 if (this.parent != null) { 100 101 String name = ""; 102 Vector assignedUsers = null; 103 104 try { 105 WorkGroupDialog workGroupDialog = new WorkGroupDialog(parent.getAllUsers()); 107 workGroupDialog.show(); 108 109 name = workGroupDialog.getName(); 111 assignedUsers = workGroupDialog.getUsers(); 112 boolean isOK = workGroupDialog.isOK(); 113 workGroupDialog.dispose(); 114 115 if (isOK) { 117 int id = fetchId(); 118 createGroup(name, id, assignedUsers); 119 } 120 } catch (Exception e) { 121 e.printStackTrace(); 122 MessageBox.showError("Create Group", "Failed to create the " + 123 name + " group."); 124 } 125 } 126 } 127 128 131 public void remove() { 132 if (this.parent != null) { 133 GroupItem gItem = (GroupItem) this.parent.getTree(). 135 getLastSelectedPathComponent(); 136 137 if (gItem != null) { 138 int id = gItem.getId(); 140 String name = gItem.toString(); 141 142 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 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 169 public void assign() { 170 if (this.parent != null) { 171 GroupItem gItem = (GroupItem) this.parent.getTree().getLastSelectedPathComponent(); 173 174 if (gItem != null) { 175 String name = gItem.toString(); 177 Vector users = gItem.getAssignedUsers(); 178 179 try { 180 WorkGroupDialog workGroupDialog = new WorkGroupDialog(name, this.parent.getAllUsers(), users); 182 workGroupDialog.show(); 183 184 users = workGroupDialog.getUsers(); 186 boolean isOK = workGroupDialog.isOK(); 187 workGroupDialog.dispose(); 188 189 if (isOK) { 191 if (users != null) 193 this.assignUsers(gItem, users); 194 this.parent.refreshTree(); 195 } 196 } catch (Exception e) { 197 e.printStackTrace(); 198 MessageBox.showError("Assign User", "Failed to assign " + 199 "the selected users."); 200 } 201 } 202 } 203 } 204 205 208 public void unassign() { 209 if (this.parent != null) { 210 UserItem uItem = (UserItem) this.parent.getTree().getLastSelectedPathComponent(); 212 this.unassign(uItem); 213 } 214 } 215 216 219 public void unassign(UserItem uItem) { 220 if (this.parent != null) { 221 if (uItem != null) { 222 int id = uItem.getId(); 224 String name = uItem.toString(); 225 int groupId = uItem.getGroup().getId(); 226 String groupName = uItem.getGroup().toString(); 227 228 try { 230 if ((id > AdminGui.instance().ID_THRESHOLD) || 231 ((id <= AdminGui.instance().ID_THRESHOLD) && 232 (groupId > AdminGui.instance().ID_THRESHOLD))) { 233 AdminGui.instance().getAdmin().removeUserFromGroup(name, groupName); 235 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 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 260 public void list() { 261 DxCollection groups = this.allGroups(); 263 264 if (groups.isEmpty() | groups == null) { 266 MessageBox.showInfo("List Groups", "No groups found."); 267 } 268 269 else { 271 if (this.parent != null) { 272 this.parent.getTreeModel().clearGroups(); 273 this.populateGroups(groups); 274 this.parent.refreshTree(); 275 } 276 } 277 } 278 279 284 public DxCollection allGroups() { 285 DxCollection groups = null; 286 287 try { 289 groups = AdminGui.instance().getAdmin().allGroups(); 290 return groups; 291 } catch (Exception e) { 292 e.printStackTrace(); 293 MessageBox.showError("List Groups", "Unable to retrieve all groups: " + e.getMessage()); 294 return groups; 295 } 296 } 297 298 304 private void createGroup(String name, int id, Vector users) 305 throws Exception { 306 307 AdminGui.instance().getAdmin().newGroup(name, id); 309 GroupItem gItem = new GroupItem(name, id); 310 311 if (users != null) 313 this.assignUsers(gItem, users); 314 315 this.parent.getTreeModel().getRootItem().addChild(gItem); 316 this.parent.refreshTree(); 317 } 318 319 324 public void populateGroups(DxCollection groups) { 325 if (groups != null) { 327 Group group; 328 329 for (DxIterator it = groups.iterator(); it.next() != null;) { 330 group = (Group) it.object(); 332 333 GroupItem item = new GroupItem(group.name(), 335 group.id().intValue()); 336 337 this.parent.getTreeModel().getRootItem().addChild(item); 338 339 populateUsers(group, item); 341 } 342 } 343 } 344 345 351 private void populateUsers(Group group, GroupItem gItem) { 352 if (group != null) { 353 DxCollection ids = group.userIDs(); 355 356 if (ids != null) { 358 User user; 359 int userId = 0; 360 361 for (DxIterator it = ids.iterator(); it.next() != null;) { 362 364 userId = ((Integer ) it.object()).intValue(); 365 try { 366 user = AdminGui.instance().getAdmin().userForId(userId); 367 368 UserItem item = new UserItem(gItem, user.name(), userId); 370 gItem.addChild(item); 371 } catch (Exception e) { 372 e.printStackTrace(); 373 } 374 } 375 } 376 } 377 } 378 379 385 public void assignUsers(GroupItem gItem, Vector users) { 386 387 if (gItem != null && users != null) { 389 User user; 390 String userName = ""; 391 392 removeUnassignedUsers(gItem, users); 394 395 Iterator it = users.iterator(); 397 while (it.hasNext()) { 398 userName = (String ) it.next(); 400 401 try { 402 user = AdminGui.instance().getAdmin(). 403 userForName(userName); 404 405 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 } catch (Exception e) { 414 e.printStackTrace(); 415 } 416 } 417 } 418 } 419 420 425 private void removeUnassignedUsers(GroupItem gItem, Vector users) { 426 if (gItem != null) { 427 Vector originalUsers = gItem.getAssignedUsers(); 428 429 Iterator it = originalUsers.iterator(); 430 while (it.hasNext()) { 431 Object user = it.next(); 432 433 if (!users.contains(user)) { 434 unassign(gItem.getUser((String ) user)); 435 } 436 } 437 } 438 } 439 440 } | Popular Tags |