1 package org.ozoneDB.adminGui.feature.account.users; 8 9 import java.util.Collections ; 10 import java.util.Vector ; 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 30 32 public class UserAccount implements IUser { 33 34 35 private UserPanel parent; 36 37 38 43 public UserAccount(UserPanel parent) { 44 this.parent = parent; 45 } 46 47 52 private int fetchId() { 53 54 int id = -1; 55 56 DxCollection users = this.allUsers(); 58 59 if (users.isEmpty() | users == null) { 61 id = ++AdminGui.instance().ID_THRESHOLD; 62 } 63 64 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 if (match) { 80 match = false; 81 ++id; 82 } 83 84 else 86 break; 87 } 88 } 89 90 return id; 91 } 92 93 96 public void create() { 97 if (this.parent != null) { 98 99 String name = ""; 100 String password = ""; 101 102 try { 103 WorkUserDialog workUserDialog = new WorkUserDialog(this); 105 workUserDialog.show(); 106 107 name = workUserDialog.getName(); 109 password = workUserDialog.getPassword(); 110 boolean isOK = workUserDialog.isOK(); 111 workUserDialog.dispose(); 112 113 if (isOK) { 115 int id = fetchId(); 116 createUser(name, password, id); 117 } 118 } catch (Exception e) { 119 e.printStackTrace(); 120 MessageBox.showError("Create User", "Failed to create the " + 121 "account: " + name + "."); 122 } 123 } 124 } 125 126 129 public void remove() { 130 if (this.parent != null) { 131 int row = this.parent.getTable().getSelectedRow(); 133 134 if (row != -1) { 135 String name = (String ) this.parent.getModel().getValueAt(row, 136 Account.COLUMN_POS_USER_NAME); 137 138 System.out.println("deleting : " + name); 139 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 if (answer == 0) { 148 try { 149 removeUser(name); 150 } catch (Exception 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 166 public void list() { 167 168 DxCollection users = this.allUsers(); 170 171 if (users.isEmpty() | users == null) { 173 MessageBox.showInfo("List Users", "No users found."); 174 } 175 176 else { 178 this.parent.getModel().setTableData(users); 179 this.parent.getModel().fireTableDataChanged(); 180 this.parent.getSorter().setModel(this.parent.getModel()); 181 } 182 } 183 184 189 public static DxCollection allUsers() { 190 DxCollection users = null; 191 192 try { 194 users = AdminGui.instance().getAdmin().allUsers(); 195 return users; 196 } catch (Exception e) { 197 e.printStackTrace(); 198 MessageBox.showError("List Users", "Unable to retrieve all " + 199 "users: " + e.getMessage()); 200 return users; 201 } 202 } 203 204 209 public static Vector allUsersNames() { 210 Vector userNames = new Vector (); 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 228 private void createUser(String name, String password, int id) 229 throws Exception { 230 AdminGui.instance().getAdmin().newUser(name, password, id); 232 this.list(); 233 } 234 235 240 private void removeUser(String name) throws Exception { 241 AdminGui.instance().getAdmin().removeUser(name); 242 this.list(); 243 } 244 245 } | Popular Tags |