📄 useraccount.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.users;
import java.util.Collections;
import java.util.Vector;
import org.ozoneDB.core.User;
import org.ozoneDB.DxLib.DxCollection;
import org.ozoneDB.DxLib.DxIterator;
import org.ozoneDB.adminGui.feature.account.Account;
import org.ozoneDB.adminGui.widget.MessageBox;
import org.ozoneDB.adminGui.main.AdminGui;
//#############################################################################
/**
* This class manages the users 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 UserAccount implements IUser {
/** Handle to the parent UserPanel object. */
private UserPanel parent;
/**
* Overloaded constructor with a handle to the parent.
*
* @param parent - a handle to the parent UserPanel object.
*/
public UserAccount(UserPanel parent) {
this.parent = parent;
}
/**
* This method retrieves an id that is not in use.
*
* @return int - unused account id.
*/
private int fetchId() {
int id = -1;
//load the collection from the database
DxCollection users = this.allUsers();
//didn't found any users set id to one more than threshold
if (users.isEmpty() | users == null) {
id = ++AdminGui.instance().ID_THRESHOLD;
}
//found some users, find the next empty id and use it.
else {
boolean match = false;
id = ++AdminGui.instance().ID_THRESHOLD;
while (true) {
for (DxIterator it = users.iterator(); it.next() != null;) {
if (id == ((User) 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 account.
*/
public void create() {
if (this.parent != null) {
String name = "";
String password = "";
try {
//display the add dialog
WorkUserDialog workUserDialog = new WorkUserDialog(this);
workUserDialog.show();
//retrieve info from dialog
name = workUserDialog.getName();
password = workUserDialog.getPassword();
boolean isOK = workUserDialog.isOK();
workUserDialog.dispose();
//get the account info
if (isOK) {
int id = fetchId();
createUser(name, password, id);
}
} catch (Exception e) {
e.printStackTrace();
MessageBox.showError("Create User", "Failed to create the " +
"account: " + name + ".");
}
}
}
/**
* This method removes the selected account from the database.
*/
public void remove() {
if (this.parent != null) {
//get the account name to delete
int row = this.parent.getTable().getSelectedRow();
if (row != -1) {
String name = (String) this.parent.getModel().getValueAt(row,
Account.COLUMN_POS_USER_NAME);
System.out.println("deleting : " + name);
//verify that is ok to delete
int answer = javax.swing.JOptionPane.showConfirmDialog(
AdminGui.instance(),
"Are you sure you want to remove " + name + "?",
"Remove User", javax.swing.JOptionPane.YES_NO_OPTION,
javax.swing.JOptionPane.QUESTION_MESSAGE);
//remove the account
if (answer == 0) {
try {
removeUser(name);
} catch (Exception e) {
e.printStackTrace();
MessageBox.showError("Remove User", "Failed to remove " +
"the account: " + name + ".");
}
}
} else {
MessageBox.showWarning("Remove User", "Please select an " +
"account from the Users list to be deleted.");
}
}
}
/**
* This method list all the users in the database.
*/
public void list() {
//load the collection from the database
DxCollection users = this.allUsers();
//didn't found any users; do like a tree and leave.
if (users.isEmpty() | users == null) {
MessageBox.showInfo("List Users", "No users found.");
}
//found some users, return the list.
else {
this.parent.getModel().setTableData(users);
this.parent.getModel().fireTableDataChanged();
this.parent.getSorter().setModel(this.parent.getModel());
}
}
/**
* This method gets the collection of users.
*
* @return DxCollection - list of users in database.
*/
public static DxCollection allUsers() {
DxCollection users = null;
//load the collection from the database
try {
users = AdminGui.instance().getAdmin().allUsers();
return users;
} catch (Exception e) {
e.printStackTrace();
MessageBox.showError("List Users", "Unable to retrieve all " +
"users: " + e.getMessage());
return users;
}
}
/**
* This method gets a vector of users names.
*
* @return Vector - list of users names in database.
*/
public static Vector allUsersNames() {
Vector userNames = new Vector();
DxCollection users = allUsers();
for (DxIterator it = users.iterator(); it.next() != null;) {
userNames.add(((User) it.object()).name());
}
Collections.sort(userNames);
return userNames;
}
/**
* This method creates a new account.
*
* @param name - the name for the new account.
* @param password - the password for the new account.
* @param id - the id for the new account.
*/
private void createUser(String name, String password, int id)
throws Exception {
//first create the account
AdminGui.instance().getAdmin().newUser(name, password, id);
this.list();
}
/**
* This method removes an existing account.
*
* @param name - the name of the.
*/
private void removeUser(String name) throws Exception {
AdminGui.instance().getAdmin().removeUser(name);
this.list();
}
} //--------------------------------- E O F -----------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -