1 16 package org.outerj.daisy.repository.commonimpl.user; 17 18 import java.util.Date ; 19 import java.util.GregorianCalendar ; 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 24 import org.outerj.daisy.repository.user.Role; 25 import org.outerj.daisy.repository.user.Roles; 26 import org.outerj.daisy.repository.user.User; 27 import org.outerj.daisy.repository.commonimpl.AuthenticatedUser; 28 import org.outerj.daisy.repository.RepositoryException; 29 import org.outerx.daisy.x10.RoleDocument; 30 import org.outerx.daisy.x10.RolesDocument; 31 import org.outerx.daisy.x10.UserDocument; 32 import org.outerx.daisy.x10.PublicUserInfoDocument; 33 34 public class UserImpl implements User { 35 private UserManagementStrategy userManagementStrategy; 36 54 private long userId=-1; 55 private String login; 56 private RoleImpl defaultRole; 57 private String email; 58 private String firstName; 59 private String lastName; 60 private String passWord; 61 private boolean updateableByUser = false; 62 private Date lastModified; 63 private long lastModifier; 64 private AuthenticatedUser requestingUser; 65 private Map roles = new HashMap (); 66 private long updateCount = 0; 67 private boolean confirmed = true; 68 private String confirmKey; 69 private boolean roleChanges=false; 70 private IntimateAccess intimateAccess = new IntimateAccess(); 71 private boolean readOnly = false; 72 private String authenticationScheme = "daisy"; 73 private static final String READ_ONLY_MESSAGE = "This User object is read-only."; 74 75 81 public UserImpl(UserManagementStrategy userManagementStrategy, String login, AuthenticatedUser requestingUser) { 82 this.userManagementStrategy = userManagementStrategy; 83 this.login = login; 84 this.requestingUser = requestingUser; 85 } 86 87 public void setDefaultRole(Role role) { 88 if (readOnly) 89 throw new RuntimeException (READ_ONLY_MESSAGE); 90 91 if (role == null) { 92 this.defaultRole = null; 93 } else { 94 preRoleAddChecks(role); 95 addToRole(role); 96 this.defaultRole = (RoleImpl)role; 97 } 98 } 99 100 private void preRoleAddChecks(Role role) { 101 if (role == null) 102 throw new IllegalArgumentException ("Role object cannot be null."); 103 104 if (!(role instanceof RoleImpl)) 105 throw new IllegalArgumentException ("Unsupported Role object supplied."); 106 107 if (((RoleImpl)role).getIntimateAccess(userManagementStrategy) == null) 108 throw new IllegalArgumentException ("Role object is not loaded from the same Repository as this User object."); 109 110 if (role.getId() == -1) 111 throw new IllegalArgumentException ("Only roles which have already been created in the repository can be added to a User."); 112 } 113 114 public Role getDefaultRole() { 115 return defaultRole; 116 } 117 118 public Roles getAllRoles() { 119 return getRolesFromMap(); 120 } 121 122 public long[] getAllRoleIds() { 123 long[] ids = new long[roles.size()]; 124 Iterator rolesIt = roles.values().iterator(); 125 int i = 0; 126 while (rolesIt.hasNext()) { 127 ids[i++] = ((Role)rolesIt.next()).getId(); 128 } 129 return ids; 130 } 131 132 public void setPassword(String s) { 133 if (readOnly) 134 throw new RuntimeException (READ_ONLY_MESSAGE); 135 136 passWord = s; 137 } 138 139 public void setEmail(String emailAddress) { 140 if (readOnly) 141 throw new RuntimeException (READ_ONLY_MESSAGE); 142 143 email = emailAddress; 144 } 145 146 public String getEmail() { 147 return email; 148 } 149 150 public long getId() { 151 return userId; 152 } 153 154 157 158 public void save() throws RepositoryException { 159 if (readOnly) 160 throw new RuntimeException (READ_ONLY_MESSAGE); 161 162 userManagementStrategy.store(this); 163 } 164 165 public UserDocument getXml() { 166 UserDocument userDocument = UserDocument.Factory.newInstance(); 167 UserDocument.User userXml = userDocument.addNewUser(); 168 169 if (defaultRole != null) { 170 RoleDocument.Role defaultRoleXml = defaultRole.getXml().getRole(); 171 userXml.setRole(defaultRoleXml); 172 } 173 174 if (email != null) 175 userXml.setEmail(email); 176 if (firstName != null) 177 userXml.setFirstName(firstName); 178 if (lastName != null) 179 userXml.setLastName(lastName); 180 userXml.setLogin(login); 181 if (passWord != null) 182 userXml.setPassword(passWord); 183 userXml.setUpdateCount(updateCount); 184 userXml.setUpdateableByUser(updateableByUser); 185 userXml.setConfirmed(confirmed); 186 if (confirmKey != null) 187 userXml.setConfirmKey(confirmKey); 188 userXml.setAuthenticationScheme(authenticationScheme); 189 190 Roles r = getRolesFromMap(); 191 RolesDocument.Roles rolesXml = r.getXml().getRoles(); 192 userXml.setRoles(rolesXml); 193 194 if (userId!=-1) { 195 GregorianCalendar lastModifiedCalendar = new GregorianCalendar (); 196 lastModifiedCalendar.setTime(lastModified); 197 198 userXml.setLastModified(lastModifiedCalendar); 199 userXml.setLastModifier(lastModifier); 200 201 userXml.setId(userId); 202 } 203 return userDocument; 204 } 205 206 private Roles getRolesFromMap() { 207 Role[] rolesArray = (Role[])roles.values().toArray(new Role[roles.size()]); 208 Roles r = new RolesImpl(rolesArray); 209 return r; 210 } 211 212 public void addToRole(Role role) { 213 if (readOnly) 214 throw new RuntimeException (READ_ONLY_MESSAGE); 215 216 preRoleAddChecks(role); 217 218 if (!roles.containsKey(new Long (role.getId()))) { 219 roleChanges = true; 220 roles.put(new Long (role.getId()), role); 221 } 222 } 223 224 public void removeFromRole(Role role) { 225 if (readOnly) 226 throw new RuntimeException (READ_ONLY_MESSAGE); 227 228 roleChanges = true; 229 roles.remove(new Long (role.getId())); 230 } 231 232 public String getLogin() { 233 return login; 234 } 235 236 public void setFirstName(String firstName) { 237 if (readOnly) 238 throw new RuntimeException (READ_ONLY_MESSAGE); 239 240 this.firstName = firstName; 241 } 242 243 public void setLastName(String lastName) { 244 if (readOnly) 245 throw new RuntimeException (READ_ONLY_MESSAGE); 246 247 this.lastName = lastName; 248 } 249 250 public String getFirstName() { 251 return firstName; 252 } 253 254 public String getLastName() { 255 return lastName; 256 } 257 258 public String getDisplayName() { 259 String name = null; 260 if (firstName == null && lastName == null) { 261 name = login; 262 } else if (firstName != null && lastName != null) { 263 name = firstName + " " + lastName; 264 } else if (lastName != null) { 265 name = lastName; 266 } else { 267 name = firstName; 268 } 269 return name; 270 } 271 272 275 public void makeReadOnly() { 276 this.readOnly = true; 277 278 if (defaultRole != null) 280 defaultRole.makeReadOnly(); 281 Iterator roleIt = roles.values().iterator(); 282 while (roleIt.hasNext()) { 283 ((RoleImpl)roleIt.next()).makeReadOnly(); 284 } 285 } 286 287 public IntimateAccess getIntimateAccess(UserManagementStrategy strategy) { 288 if (readOnly) 289 throw new RuntimeException (READ_ONLY_MESSAGE); 290 291 if (this.userManagementStrategy == strategy) 292 return intimateAccess; 293 else 294 return null; 295 } 296 297 public Date getLastModified() { 298 if (lastModified != null) 299 return (Date )lastModified.clone(); 300 else 301 return lastModified; 302 } 303 304 public long getLastModifier() { 305 return lastModifier; 306 } 307 308 319 public static boolean isValidPassword(String password) { 320 boolean isValid = false; 321 if (password!=null){ 322 if (password.length()>=1) isValid=true; 323 } 324 return isValid; 325 } 326 327 public void clearRoles() { 328 if (readOnly) 329 throw new RuntimeException (READ_ONLY_MESSAGE); 330 331 this.roles.clear(); 332 this.roleChanges = true; 333 this.defaultRole = null; 334 } 335 336 public void setLogin(String loginName) { 337 if (readOnly) 338 throw new RuntimeException (READ_ONLY_MESSAGE); 339 340 this.login = loginName; 341 } 342 343 public long getUpdateCount() { 344 return updateCount; 345 } 346 347 public boolean hasRole(long roleId) { 348 return roles.containsKey(new Long (roleId)); 349 } 350 351 public boolean isUpdateableByUser() { 352 return updateableByUser; 353 } 354 355 public void setUpdateableByUser(boolean updateableByUser) { 356 this.updateableByUser = updateableByUser; 357 } 358 359 public boolean isConfirmed() { 360 return confirmed; 361 } 362 363 public void setConfirmed(boolean confirmed) { 364 this.confirmed = confirmed; 365 } 366 367 public String getConfirmKey() { 368 return confirmKey; 369 } 370 371 public void setConfirmKey(String confirmKey) { 372 this.confirmKey = confirmKey; 373 } 374 375 public String getAuthenticationScheme() { 376 return authenticationScheme; 377 } 378 379 public void setAuthenticationScheme(String schemeName) { 380 if (schemeName == null) 381 throw new IllegalArgumentException ("schemeName cannot be null."); 382 if (schemeName.length() < 1) 383 throw new IllegalArgumentException ("schemeName cannot be an empty string."); 384 385 this.authenticationScheme = schemeName; 386 } 387 388 public PublicUserInfoDocument getPublicUserInfo() { 389 PublicUserInfoDocument publicInfoDocument = PublicUserInfoDocument.Factory.newInstance(); 390 PublicUserInfoDocument.PublicUserInfo publicInfoXml = publicInfoDocument.addNewPublicUserInfo(); 391 publicInfoXml.setId(getId()); 392 publicInfoXml.setLogin(getLogin()); 393 publicInfoXml.setDisplayName(getDisplayName()); 394 return publicInfoDocument; 395 } 396 397 400 public class IntimateAccess { 401 private IntimateAccess() { 402 } 403 404 public void setLastModified(Date lastModDate) { 405 lastModified = lastModDate; 406 } 407 public void setLastModifier(long lastMod) { 408 lastModifier = lastMod; 409 } 410 411 public AuthenticatedUser getCurrentUser() { 412 return requestingUser; 413 } 414 415 public void saved(long id, String firstName, String lastName, String email, Date lastModified, long lastModifier, long updateCount) { 416 userId = id; 417 UserImpl.this.firstName = firstName; 418 UserImpl.this.lastName = lastName; 419 UserImpl.this.email = email; 420 UserImpl.this.lastModified = lastModified; 421 UserImpl.this.lastModifier = lastModifier; 422 UserImpl.this.updateCount = updateCount; 423 roleChanges=false; 424 } 425 426 430 public String getPassword() { 431 return passWord; 432 } 433 434 public void addToRole(Role r) { 435 roles.put(new Long (r.getId()), r); 436 } 437 438 441 public boolean hasRoleChanges() { 442 return roleChanges; 443 } 444 } 445 } 446 | Popular Tags |