KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > adminGui > feature > account > users > UserAccount


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.users;
8
9 import java.util.Collections JavaDoc;
10 import java.util.Vector JavaDoc;
11
12 import org.ozoneDB.core.User;
13 import org.ozoneDB.DxLib.DxCollection;
14 import org.ozoneDB.DxLib.DxIterator;
15 import org.ozoneDB.adminGui.feature.account.Account;
16 import org.ozoneDB.adminGui.widget.MessageBox;
17 import org.ozoneDB.adminGui.main.AdminGui;
18
19
20 //#############################################################################
21
/**
22  * This class manages the users accounts and takes care of communicating with
23  * the database.
24  *
25  * @author <p align=center>Ibsen Ramos-Bonilla
26  * <br>Copyright &copy 1997-@year@ by SMB GmbH. All Rights Reserved.</p>
27  *
28  * @version 1.0
29  */

30 //#############################################################################
31

32 public class UserAccount implements IUser {
33
34     /** Handle to the parent UserPanel object. */
35     private UserPanel parent;
36
37
38     /**
39      * Overloaded constructor with a handle to the parent.
40      *
41      * @param parent - a handle to the parent UserPanel object.
42      */

43     public UserAccount(UserPanel parent) {
44         this.parent = parent;
45     }
46
47     /**
48      * This method retrieves an id that is not in use.
49      *
50      * @return int - unused account id.
51      */

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

96     public void create() {
97         if (this.parent != null) {
98
99             String JavaDoc name = "";
100             String JavaDoc password = "";
101
102             try {
103                 //display the add dialog
104
WorkUserDialog workUserDialog = new WorkUserDialog(this);
105                 workUserDialog.show();
106
107                 //retrieve info from dialog
108
name = workUserDialog.getName();
109                 password = workUserDialog.getPassword();
110                 boolean isOK = workUserDialog.isOK();
111                 workUserDialog.dispose();
112
113                 //get the account info
114
if (isOK) {
115                     int id = fetchId();
116                     createUser(name, password, id);
117                 }
118             } catch (Exception JavaDoc e) {
119                 e.printStackTrace();
120                 MessageBox.showError("Create User", "Failed to create the " +
121                         "account: " + name + ".");
122             }
123         }
124     }
125
126     /**
127      * This method removes the selected account from the database.
128      */

129     public void remove() {
130         if (this.parent != null) {
131             //get the account name to delete
132
int row = this.parent.getTable().getSelectedRow();
133
134             if (row != -1) {
135                 String JavaDoc name = (String JavaDoc) this.parent.getModel().getValueAt(row,
136                         Account.COLUMN_POS_USER_NAME);
137
138                 System.out.println("deleting : " + name);
139                 //verify that is ok to delete
140
int answer = javax.swing.JOptionPane.showConfirmDialog(
141                         AdminGui.instance(),
142                         "Are you sure you want to remove " + name + "?",
143                         "Remove User", javax.swing.JOptionPane.YES_NO_OPTION,
144                         javax.swing.JOptionPane.QUESTION_MESSAGE);
145
146                 //remove the account
147
if (answer == 0) {
148                     try {
149                         removeUser(name);
150                     } catch (Exception JavaDoc e) {
151                         e.printStackTrace();
152                         MessageBox.showError("Remove User", "Failed to remove " +
153                                 "the account: " + name + ".");
154                     }
155                 }
156             } else {
157                 MessageBox.showWarning("Remove User", "Please select an " +
158                         "account from the Users list to be deleted.");
159             }
160         }
161     }
162
163     /**
164      * This method list all the users in the database.
165      */

166     public void list() {
167
168         //load the collection from the database
169
DxCollection users = this.allUsers();
170
171         //didn't found any users; do like a tree and leave.
172
if (users.isEmpty() | users == null) {
173             MessageBox.showInfo("List Users", "No users found.");
174         }
175
176         //found some users, return the list.
177
else {
178             this.parent.getModel().setTableData(users);
179             this.parent.getModel().fireTableDataChanged();
180             this.parent.getSorter().setModel(this.parent.getModel());
181         }
182     }
183
184     /**
185      * This method gets the collection of users.
186      *
187      * @return DxCollection - list of users in database.
188      */

189     public static DxCollection allUsers() {
190         DxCollection users = null;
191
192         //load the collection from the database
193
try {
194             users = AdminGui.instance().getAdmin().allUsers();
195             return users;
196         } catch (Exception JavaDoc e) {
197             e.printStackTrace();
198             MessageBox.showError("List Users", "Unable to retrieve all " +
199                     "users: " + e.getMessage());
200             return users;
201         }
202     }
203
204     /**
205      * This method gets a vector of users names.
206      *
207      * @return Vector - list of users names in database.
208      */

209     public static Vector JavaDoc allUsersNames() {
210         Vector JavaDoc userNames = new Vector JavaDoc();
211         DxCollection users = allUsers();
212
213         for (DxIterator it = users.iterator(); it.next() != null;) {
214             userNames.add(((User) it.object()).name());
215         }
216
217         Collections.sort(userNames);
218         return userNames;
219     }
220
221     /**
222      * This method creates a new account.
223      *
224      * @param name - the name for the new account.
225      * @param password - the password for the new account.
226      * @param id - the id for the new account.
227      */

228     private void createUser(String JavaDoc name, String JavaDoc password, int id)
229             throws Exception JavaDoc {
230         //first create the account
231
AdminGui.instance().getAdmin().newUser(name, password, id);
232         this.list();
233     }
234
235     /**
236      * This method removes an existing account.
237      *
238      * @param name - the name of the.
239      */

240     private void removeUser(String JavaDoc name) throws Exception JavaDoc {
241         AdminGui.instance().getAdmin().removeUser(name);
242         this.list();
243     }
244
245 } //--------------------------------- E O F -----------------------------------
246
Popular Tags