📄 groupaccount.java
字号:
//// You can redistribute this software and/or modify it under the terms of
// the Ozone Library License version 1 published by ozone-db.org.
//
// The original code and portions created by SMB are
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
//
package org.ozoneDB.adminGui.feature.account.groups;
import java.util.Iterator;
import java.util.Vector;
import org.ozoneDB.core.Group;
import org.ozoneDB.core.User;
import org.ozoneDB.core.UserManagerException;
import org.ozoneDB.DxLib.DxCollection;
import org.ozoneDB.DxLib.DxIterator;
import org.ozoneDB.adminGui.feature.account.users.UserItem;
import org.ozoneDB.adminGui.widget.MessageBox;
import org.ozoneDB.adminGui.main.AdminGui;
//#############################################################################
/**
* This class manages the groups accounts and takes care of communicating with
* the database.
*
* @author <p align=center>Ibsen Ramos-Bonilla
* <br>Copyright © 1997-@year@ by SMB GmbH. All Rights Reserved.</p>
*
* @version 1.0
*/
//#############################################################################
public class GroupAccount implements IGroup {
/** Handle to the parent GroupPanel object. */
private GroupPanel parent = null;
/**
* Overloaded constructor with a handle to the parent.
*
* @param parent - a handle to the parent GroupPanel object.
*/
public GroupAccount(GroupPanel parent) {
this.parent = parent;
}
/**
* This method retrieves an id that is not in use.
*
* @return int - unused group id.
*/
private int fetchId() {
int id = -1;
//load the collection from the database
DxCollection groups = this.allGroups();
//didn't found any groups set id to one more than threshold
if (groups.isEmpty() | groups == null) {
id = ++AdminGui.instance().ID_THRESHOLD;
}
//found some groups, find the next empty id and use it.
else {
boolean match = false;
id = ++AdminGui.instance().ID_THRESHOLD;
while (true) {
for (DxIterator it = groups.iterator(); it.next() != null;) {
if (id == ((Group) it.object()).id().intValue()) {
match = true;
break;
}
}
//found match increment id and test again
if (match) {
match = false;
++id;
}
//didn't found a match get out and run like a chicken
else
break;
}
}
return id;
}
/**
* This method creates a new group.
*/
public void create() {
if (this.parent != null) {
String name = "";
Vector assignedUsers = null;
try {
//display the work group dialog
WorkGroupDialog workGroupDialog = new WorkGroupDialog(parent.getAllUsers());
workGroupDialog.show();
//retrieve info from dialog
name = workGroupDialog.getName();
assignedUsers = workGroupDialog.getUsers();
boolean isOK = workGroupDialog.isOK();
workGroupDialog.dispose();
//get the group info
if (isOK) {
int id = fetchId();
createGroup(name, id, assignedUsers);
}
} catch (Exception e) {
e.printStackTrace();
MessageBox.showError("Create Group", "Failed to create the " +
name + " group.");
}
}
}
/**
* This method removes a group from the database.
*/
public void remove() {
if (this.parent != null) {
//find the object that is selected
GroupItem gItem = (GroupItem) this.parent.getTree().
getLastSelectedPathComponent();
if (gItem != null) {
//find the group name and remove it
int id = gItem.getId();
String name = gItem.toString();
//remove the group if it's higher than the id threshold
try {
if (id > AdminGui.instance().ID_THRESHOLD) {
AdminGui.instance().getAdmin().removeGroup(name);
this.parent.getTreeModel().getRootItem().removeChild(
gItem);
} else
MessageBox.showWarning("Remove Group", "This group " +
"cannot be removed!");
} catch (Exception e) {
e.printStackTrace();
MessageBox.showError("Removing Group", "Failed to remove the "
+ name + " group.");
} finally {
this.parent.refreshTree();
}
} else {
MessageBox.showWarning("Remove Group", "Please select a " +
"group from the Groups tree to be deleted.");
}
}
}
/**
* This method assigns an account to a selected group.
*/
public void assign() {
if (this.parent != null) {
//find the object that is selected
GroupItem gItem = (GroupItem) this.parent.getTree().getLastSelectedPathComponent();
if (gItem != null) {
//find the group name and assigned users
String name = gItem.toString();
Vector users = gItem.getAssignedUsers();
try {
//display the work group dialog
WorkGroupDialog workGroupDialog = new WorkGroupDialog(name, this.parent.getAllUsers(), users);
workGroupDialog.show();
//retrieve info from dialog
users = workGroupDialog.getUsers();
boolean isOK = workGroupDialog.isOK();
workGroupDialog.dispose();
//get the group info
if (isOK) {
//if we have any users to add do it now
if (users != null)
this.assignUsers(gItem, users);
this.parent.refreshTree();
}
} catch (Exception e) {
e.printStackTrace();
MessageBox.showError("Assign User", "Failed to assign " +
"the selected users.");
}
}
}
}
/**
* This method unassigns an account from a selected group.
*/
public void unassign() {
if (this.parent != null) {
//find the object that is selected
UserItem uItem = (UserItem) this.parent.getTree().getLastSelectedPathComponent();
this.unassign(uItem);
}
}
/**
* This method unassigns an account from a selected group.
*/
public void unassign(UserItem uItem) {
if (this.parent != null) {
if (uItem != null) {
//find the account name and unassign it
int id = uItem.getId();
String name = uItem.toString();
int groupId = uItem.getGroup().getId();
String groupName = uItem.getGroup().toString();
//remove the account if it's higher than the id threshold
try {
if ((id > AdminGui.instance().ID_THRESHOLD) ||
((id <= AdminGui.instance().ID_THRESHOLD) &&
(groupId > AdminGui.instance().ID_THRESHOLD))) {
//remove the account from group
AdminGui.instance().getAdmin().removeUserFromGroup(name, groupName);
//remove the account from the tree items
uItem.getGroup().removeChild(uItem);
uItem.orphan();
this.parent.getTreeModel().getRootItem().removeChild(uItem);
} else
MessageBox.showWarning("Unassign User", "This account " +
"cannot be unassign from this group!");
} catch (Exception e) {
e.printStackTrace();
MessageBox.showError("Unassign account", "Failed to remove " +
" account: " + name + ".");
} finally {
this.parent.refreshTree();
}
} else {
MessageBox.showWarning("Unassign User", "Please select an " +
"account from a Group to be unassigned.");
}
}
}
/**
* This method lists all the groups in the database.
*/
public void list() {
//load the collection from the database
DxCollection groups = this.allGroups();
//didn't found any users; do like a tree and leave.
if (groups.isEmpty() | groups == null) {
MessageBox.showInfo("List Groups", "No groups found.");
}
//found some users, return the list.
else {
if (this.parent != null) {
this.parent.getTreeModel().clearGroups();
this.populateGroups(groups);
this.parent.refreshTree();
}
}
}
/**
* This method gets the collection of groups.
*
* @return DxCollection - list of groups in database.
*/
public DxCollection allGroups() {
DxCollection groups = null;
//load the collection from the database
try {
groups = AdminGui.instance().getAdmin().allGroups();
return groups;
} catch (Exception e) {
e.printStackTrace();
MessageBox.showError("List Groups", "Unable to retrieve all groups: " + e.getMessage());
return groups;
}
}
/**
* This method creates a new group.
*
* @param name - the name for the new group.
* @param id - the id for the new group.
*/
private void createGroup(String name, int id, Vector users)
throws Exception {
//first carete the group
AdminGui.instance().getAdmin().newGroup(name, id);
GroupItem gItem = new GroupItem(name, id);
//if we have any users to add do it now
if (users != null)
this.assignUsers(gItem, users);
this.parent.getTreeModel().getRootItem().addChild(gItem);
this.parent.refreshTree();
}
/**
* This is the method that actually populates the groups into the tree.
*
* @param groups - the collection of groups objects in the database.
*/
public void populateGroups(DxCollection groups) {
//check that we have any groups
if (groups != null) {
Group group;
for (DxIterator it = groups.iterator(); it.next() != null;) {
//get next group in the collection
group = (Group) it.object();
//add the group
GroupItem item = new GroupItem(group.name(),
group.id().intValue());
this.parent.getTreeModel().getRootItem().addChild(item);
//add the users under this group
populateUsers(group, item);
}
}
}
/**
* This is the method that actually populates each group in the tree.
*
* @param group - one group out of the groups collection.
* @param gItem - the group node having users added to.
*/
private void populateUsers(Group group, GroupItem gItem) {
if (group != null) {
//add the users under this group
DxCollection ids = group.userIDs();
//check that we have any users
if (ids != null) {
User user;
int userId = 0;
for (DxIterator it = ids.iterator(); it.next() != null;) {
//get next account id in the collection
userId = ((Integer) it.object()).intValue();
try {
user = AdminGui.instance().getAdmin().userForId(userId);
//add the account
UserItem item = new UserItem(gItem, user.name(), userId);
gItem.addChild(item);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
/**
* This is the method that assigns users to a new group in the tree.
*
* @param gItem - one group out of the groups collection.
* @param users - the list of users to add.
*/
public void assignUsers(GroupItem gItem, Vector users) {
//check that we have any users
if (gItem != null && users != null) {
User user;
String userName = "";
//remove all the users from the group first
removeUnassignedUsers(gItem, users);
//proceed to add children again
Iterator it = users.iterator();
while (it.hasNext()) {
//get next account id in the collection
userName = (String) it.next();
try {
user = AdminGui.instance().getAdmin().
userForName(userName);
//add the account
AdminGui.instance().getAdmin().addUser2Group(userName,
gItem.toString());
UserItem item = new UserItem(gItem, userName,
user.id().intValue());
gItem.addChild(item);
} catch (UserManagerException ume) {
//continue adding other users
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* This method gets rid of all the users belonging to a group.
*
* @param users - list of assigned users.
*/
private void removeUnassignedUsers(GroupItem gItem, Vector users) {
if (gItem != null) {
Vector originalUsers = gItem.getAssignedUsers();
Iterator it = originalUsers.iterator();
while (it.hasNext()) {
Object user = it.next();
if (!users.contains(user)) {
unassign(gItem.getUser((String) user));
}
}
}
}
} //--------------------------------- E O F -----------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -