1 package org.apache.fulcrum.security.impl.db.entity; 2 3 56 57 import java.io.ByteArrayOutputStream ; 58 import java.io.PrintWriter ; 59 import java.util.Date ; 60 import java.util.Hashtable ; 61 import org.apache.fulcrum.security.TurbineSecurity; 62 import org.apache.fulcrum.security.entity.User; 63 import org.apache.fulcrum.security.session.SessionBindingEvent; 64 65 80 public class TurbineUser 81 extends org.apache.fulcrum.security.impl.db.entity.BaseTurbineUser 82 implements User 83 { 84 85 private Date lastAccessDate = null; 86 87 88 private Hashtable permStorage = null; 89 90 91 private Hashtable tempStorage = null; 92 93 97 public TurbineUser() 98 { 99 setCreateDate(new Date ()); 100 tempStorage = new Hashtable (10); 101 setHasLoggedIn(Boolean.FALSE); 102 } 103 104 109 public int getAccessCounter() 110 { 111 try 112 { 113 return ((Integer ) getPerm(User.ACCESS_COUNTER)).intValue(); 114 } 115 catch (Exception e) 116 { 117 return 0; 118 } 119 } 120 121 126 public int getAccessCounterForSession() 127 { 128 try 129 { 130 return ((Integer ) getTemp(User.SESSION_ACCESS_COUNTER)).intValue(); 131 } 132 catch (Exception e) 133 { 134 return 0; 135 } 136 } 137 138 141 public void incrementAccessCounter() 142 { 143 setAccessCounter(getAccessCounter() + 1); 144 } 145 146 149 public void incrementAccessCounterForSession() 150 { 151 setAccessCounterForSession(getAccessCounterForSession() + 1); 152 } 153 154 159 public void setAccessCounter(int cnt) 160 { 161 setPerm(User.ACCESS_COUNTER, new Integer (cnt)); 162 } 163 164 170 public void setAccessCounterForSession(int cnt) 171 { 172 setTemp(User.SESSION_ACCESS_COUNTER, new Integer (cnt)); 173 } 174 175 183 public boolean isConfirmed() 184 { 185 String value = getConfirmed(); 186 return (value != null && value.equals(User.CONFIRM_DATA)); 187 } 188 189 194 public boolean hasLoggedIn() 195 { 196 Boolean loggedIn = getHasLoggedIn(); 197 return (loggedIn != null && loggedIn.booleanValue()); 198 } 199 200 206 public void setHasLoggedIn(Boolean value) 207 { 208 setTemp(User.HAS_LOGGED_IN, value); 209 } 210 211 218 private Boolean getHasLoggedIn() 219 { 220 return (Boolean ) getTemp(User.HAS_LOGGED_IN); 221 } 222 223 229 public java.util.Date getLastAccessDate() 230 { 231 if (lastAccessDate == null) 232 { 233 setLastAccessDate(); 234 } 235 return lastAccessDate; 236 } 237 238 242 public void setLastAccessDate() 243 { 244 lastAccessDate = new java.util.Date (); 245 } 246 247 253 public Object getPerm(String name) 254 { 255 return permStorage.get(name); 256 } 257 258 266 public Object getPerm(String name, Object def) 267 { 268 try 269 { 270 Object val = permStorage.get(name); 271 return (val == null ? def : val); 272 } 273 catch (Exception e) 274 { 275 return def; 276 } 277 } 278 279 285 public Hashtable getPermStorage() 286 { 287 if (this.permStorage == null) 288 { 289 this.permStorage = new Hashtable (); 290 } 291 292 return this.permStorage; 293 } 294 295 304 public void setPerm(String name, Object value) 305 { 306 safeAddToHashtable(getPermStorage(), name, value); 307 } 308 309 315 public void setPermStorage(Hashtable stuff) 316 { 317 this.permStorage = stuff; 318 } 319 320 326 public Object getTemp(String name) 327 { 328 return tempStorage.get(name); 329 } 330 331 339 public Object getTemp(String name, Object def) 340 { 341 Object val; 342 try 343 { 344 val = tempStorage.get(name); 345 if (val == null) 346 { 347 val = def; 348 } 349 } 350 catch (Exception e) 351 { 352 val = def; 353 } 354 return val; 355 } 356 357 363 public Hashtable getTempStorage() 364 { 365 if (this.tempStorage == null) 366 { 367 this.tempStorage = new Hashtable (); 368 } 369 return this.tempStorage; 370 } 371 372 378 public Object removeTemp(String name) 379 { 380 return tempStorage.remove(name); 381 } 382 383 392 public void setTemp(String name, Object value) 393 { 394 safeAddToHashtable(tempStorage, name, value); 395 } 396 397 403 public void setTempStorage(Hashtable storage) 404 { 405 this.tempStorage = storage; 406 } 407 408 413 public void updateLastLogin() 414 throws Exception 415 { 416 setPerm( User.LAST_LOGIN, new java.util.Date () ); 417 } 418 419 425 public void valueBound(SessionBindingEvent event) 426 { 427 } 429 430 436 public void valueUnbound(SessionBindingEvent event) 437 { 438 try 439 { 440 if (hasLoggedIn()) 441 { 442 TurbineSecurity.saveUser(this); 443 } 444 } 445 catch (Exception e) 446 { 447 449 ByteArrayOutputStream ostr = new ByteArrayOutputStream (); 453 e.printStackTrace(new PrintWriter (ostr,true)); 454 String stackTrace = ostr.toString(); 455 System.out.println(stackTrace); 456 } 457 } 458 459 462 public void setName(String v) 463 { 464 setUserName(v); 465 } 466 467 470 public String getName() 471 { 472 return getUserName(); 473 } 474 475 487 public static final void safeAddToHashtable(Hashtable hash, Object key, Object value) 488 throws NullPointerException 489 { 490 if (value == null) 491 { 492 hash.put ( key, "" ); 493 } 494 else 495 { 496 hash.put ( key, value ); 497 } 498 } 499 } 500 | Popular Tags |