1 18 19 package cowsultants.itracker.ejb.client.models; 20 21 import java.util.Comparator ; 22 23 import cowsultants.itracker.ejb.client.models.PermissionModel; 24 import cowsultants.itracker.ejb.client.models.UserPreferencesModel; 25 import cowsultants.itracker.ejb.client.util.UserUtilities; 26 27 public class UserModel extends GenericModel implements Comparator { 28 private String login; 29 private String password; 30 private String firstName; 31 private String lastName; 32 private String email; 33 private int status; 34 private int registrationType; 35 private boolean loggedIn; 36 private boolean superUser; 37 38 private PermissionModel[] permissions; 39 private UserPreferencesModel preferences; 40 41 public UserModel() { 42 login = ""; 43 password = ""; 44 firstName = ""; 45 lastName = ""; 46 email = ""; 47 status = 0; 48 registrationType = 0; 49 superUser = false; 50 loggedIn = false; 51 } 52 53 public UserModel(String login, String password, String firstName, String lastName, String email, boolean superUser) { 54 this(login, password, firstName, lastName, email, UserUtilities.REGISTRATION_TYPE_ADMIN, superUser); 55 } 56 57 public UserModel(String login, String password, String firstName, String lastName, String email, int registrationType, boolean superUser) { 58 this.login = login; 59 this.password = password; 60 this.firstName = firstName; 61 this.lastName = lastName; 62 this.email = email; 63 this.registrationType = registrationType; 64 this.superUser = superUser; 65 } 66 67 public String getFirstInitial() { 68 return (getFirstName().length() > 0 ? getFirstName().substring(0,1).toUpperCase() + "." : ""); 69 } 70 71 public boolean hasRequiredData() { 72 return hasRequiredData(true); 73 } 74 75 public boolean hasRequiredData(boolean passwordRequired) { 76 if(this.getLogin() == null || this.getLogin().equals("") || 77 this.getFirstName() == null || this.getFirstName().equals("") || 78 this.getLastName() == null || this.getLastName().equals("") || 79 this.getEmail() == null || this.getEmail().equals("")) { 80 return false; 81 } 82 if(passwordRequired && (this.getPassword() == null || this.getPassword().equals(""))) { 83 return false; 84 } 85 return true; 86 } 87 88 public String getLogin() { 89 return login; 90 } 91 92 public void setLogin(String value) { 93 login = value; 94 } 95 96 public String getPassword() { 97 return password; 98 } 99 100 public void setPassword(String value) { 101 password = value; 102 } 103 104 public String getFirstName() { 105 return firstName; 106 } 107 108 public void setFirstName(String value) { 109 firstName = value; 110 } 111 112 public String getLastName() { 113 return lastName; 114 } 115 116 public void setLastName(String value) { 117 lastName = value; 118 } 119 120 public String getEmail() { 121 return (email == null ? "" : email); 122 } 123 124 public void setEmail(String value) { 125 email = value; 126 } 127 128 public int getStatus() { 129 return status; 130 } 131 132 public void setStatus(int value) { 133 status = value; 134 } 135 136 public int getRegistrationType() { 137 return registrationType; 138 } 139 140 public void setRegistrationType(int value) { 141 registrationType = value; 142 } 143 144 public boolean isLoggedIn() { 145 return loggedIn; 146 } 147 148 public void setLoggedIn(boolean value) { 149 loggedIn = value; 150 } 151 152 public boolean isSuperUser() { 153 return superUser; 154 } 155 156 public void setSuperUser(boolean value) { 157 superUser = value; 158 } 159 160 public PermissionModel[] getPermissions() { 161 return (permissions == null ? new PermissionModel[0] : permissions); 162 } 163 164 public void setPermissions(PermissionModel[] value) { 165 permissions = value; 166 } 167 168 public UserPreferencesModel getPreferences() { 169 return (preferences == null ? new UserPreferencesModel() : preferences); 170 } 171 172 public void setPreferences(UserPreferencesModel value) { 173 preferences = value; 174 } 175 176 public String toString() { 177 return "User " + this.getLogin() + "(" + this.getId() + ")" + " Name: " + this.getFirstName() + " " + this.getLastName() + 178 " Email: " + this.getEmail() + " Status: " + this.getStatus() + " RegType: " + this.getRegistrationType() + " Super-User: " + this.isSuperUser(); 179 } 180 181 public int compare(Object a, Object b) { 182 return this.new CompareByLogin().compare(a, b); 183 } 184 185 public boolean equals(Object obj) { 186 return this.new CompareByLogin().equals(obj); 187 } 188 189 public int hashCode() { 190 return this.new CompareByLogin().hashCode(); 191 } 192 193 public abstract class UserModelComparator implements Comparator { 194 protected boolean isAscending = true; 195 196 public UserModelComparator() { 197 } 198 199 public UserModelComparator(boolean isAscending) { 200 setAscending(isAscending); 201 } 202 203 public void setAscending(boolean value) { 204 this.isAscending = value; 205 } 206 207 protected abstract int doComparison(UserModel ma, UserModel mb); 208 209 public final boolean equals(Object obj) { 210 if(! (obj instanceof UserModel)) { 211 return false; 212 } 213 214 try { 215 UserModel mo = (UserModel) obj; 216 if(UserModel.this.getLogin().equals(mo.getLogin())) { 217 return true; 218 } 219 } catch(ClassCastException cce) { 220 } 221 222 return false; 223 } 224 225 public final int hashCode() { 226 return (UserModel.this.getLogin() == null ? 1 : UserModel.this.getLogin().hashCode()); 227 } 228 229 public final int compare(Object a, Object b) { 230 if(! (a instanceof UserModel) || ! (b instanceof UserModel)) { 231 throw new ClassCastException (); 232 } 233 234 UserModel ma = (UserModel) a; 235 UserModel mb = (UserModel) b; 236 237 int result = doComparison(ma, mb); 238 if(! isAscending) { 239 result = result * -1; 240 } 241 return result; 242 } 243 } 244 245 public class CompareByLogin extends UserModelComparator { 246 public CompareByLogin(){ 247 super(); 248 } 249 250 public CompareByLogin(boolean isAscending) { 251 super(isAscending); 252 } 253 254 protected int doComparison(UserModel ma, UserModel mb) { 255 if(ma.getLogin() == null && mb.getLogin() == null) { 256 return 0; 257 } else if(ma.getLogin() == null) { 258 return 1; 259 } else if(mb.getLogin() == null) { 260 return -1; 261 } 262 263 return ma.getLogin().compareTo(mb.getLogin()); 264 } 265 } 266 267 public class CompareByName extends UserModelComparator { 268 public CompareByName(){ 269 super(); 270 } 271 272 public CompareByName(boolean isAscending) { 273 super(isAscending); 274 } 275 276 protected int doComparison(UserModel ma, UserModel mb) { 277 if(ma.getLastName() == null && mb.getLastName() == null) { 278 return 0; 279 } else if(ma.getLastName() == null) { 280 return 1; 281 } else if(mb.getLastName() == null) { 282 return -1; 283 } 284 285 if(ma.getLastName().equals(mb.getLastName())) { 286 if(ma.getFirstName() == null && mb.getFirstName() == null) { 287 return 0; 288 } else if(ma.getFirstName() == null) { 289 return 1; 290 } else if(mb.getFirstName() == null) { 291 return -1; 292 } 293 return ma.getFirstName().compareTo(mb.getFirstName()); 294 } 295 return ma.getLastName().compareTo(mb.getLastName()); 296 } 297 } 298 } | Popular Tags |