1 16 package dlog4j.formbean; 17 18 import java.sql.SQLException ; 19 import java.text.SimpleDateFormat ; 20 import java.util.ArrayList ; 21 import java.util.Date ; 22 import java.util.List ; 23 import java.util.StringTokenizer ; 24 25 import javax.servlet.http.HttpServletRequest ; 26 27 import net.sf.hibernate.HibernateException; 28 import net.sf.hibernate.Session; 29 30 import dlog4j.SiteManager; 31 import dlog4j.UserManager; 32 import dlog4j.security.DlogRole; 33 import dlog4j.util.StringUtils; 34 35 43 public class UserForm extends DlogActionForm implements Cloneable { 44 45 public final static String KEY = "dlog4j.loginUser"; 46 public final static SimpleDateFormat DATE_FORMAT = new SimpleDateFormat ("yyyy-MM-dd"); 47 public final static int MODE_LOGIN = 0; 48 public final static int MODE_REG = 1; 49 public final static int MODE_EDIT = 2; 50 public final static int MODE_CREATE = 4; public final static int MODE_DELETE = 8; 52 53 55 56 private int id; 57 58 59 private String email; 60 61 62 private String loginName; 63 64 private String password; 65 private String displayName; 66 private String homePage; 67 private String resume; 68 private String portrait; 69 private int loginCount; 70 private Date regTime; 71 private Date lastTime; 72 private int userRole = DlogRole.ROLE_GUEST; 73 private int mode = MODE_LOGIN; 74 75 private List logs; 76 private int logCount; 77 private List replies; 78 private int replyCount; 79 private int bookMarkCount; 81 SiteForm site; 82 83 DlogRole role; 84 85 90 private String cats; 91 92 public static UserForm getLoginUser(HttpServletRequest request){ 93 SiteForm sf = SiteManager.getCurrentSite(request); 94 if(sf!=null) 95 return (UserForm) request.getSession().getAttribute(sf.getName()+'_'+UserForm.KEY); 96 return null; 97 } 98 99 public void saveLoginUser(HttpServletRequest request){ 100 SiteForm sf = SiteManager.getCurrentSite(request); 101 if(sf!=null) 102 request.getSession().setAttribute(sf.getName()+'_'+UserForm.KEY, this); 103 } 104 105 public static void removeFromSession(HttpServletRequest request){ 106 SiteForm sf = SiteManager.getCurrentSite(request); 107 if(sf!=null) 108 request.getSession().removeAttribute(sf.getName()+'_'+UserForm.KEY); 109 } 110 111 public boolean isLogin(){ 112 return regTime!=null; 113 } 114 115 public boolean isAdmin(){ 116 return userRole == DlogRole.ROLE_MANAGER; 117 } 118 119 public boolean isFriend(){ 120 return userRole == DlogRole.ROLE_FRIEND; 121 } 122 123 125 public String toString(){ 126 StringBuffer user = new StringBuffer (100); 127 user.append("Name:"); 128 user.append(loginName); 129 user.append(",displayName:"); 130 user.append(displayName); 131 user.append(",email:"); 132 user.append(email); 133 user.append(",password:"); 134 user.append(password); 135 return user.toString(); 136 } 137 142 protected UserForm checkLoginName(SiteForm site, String name){ 143 Session ssn = null; 144 UserForm user = null; 145 try{ 146 ssn = getSession(); 147 user = UserManager.getUser(ssn,site,name); 148 }catch(HibernateException e){ 149 }catch(SQLException e){ 150 }finally{ 151 try{ 152 closeSession(ssn); 153 }catch(Exception e){} 154 } 155 return user; 156 } 157 158 161 public String getDisplayName() { 162 return displayName; 163 } 164 165 168 public String getEmail() { 169 return email; 170 } 171 172 175 public String getHomePage() { 176 return homePage; 177 } 178 179 182 public int getId() { 183 return id; 184 } 185 186 189 public Date getLastTime() { 190 return lastTime; 191 } 192 193 196 public int getLoginCount() { 197 return loginCount; 198 } 199 200 203 public String getPassword() { 204 return password; 205 } 206 207 public String getCryptPassword() { 208 return StringUtils.encrypt(password); 209 } 210 211 public void setCryptPassword(String pwd) { 212 password = StringUtils.decrypt(pwd); 213 } 214 215 218 public Date getRegTime() { 219 return regTime; 220 } 221 222 225 public String getResume() { 226 return resume; 227 } 228 229 232 public int getUserRole() { 233 return userRole; 234 } 235 236 public String getRoleDesc(){ 237 String desc = null; 238 switch(userRole){ 239 case DlogRole.ROLE_MANAGER: 240 desc = "管理员"; 241 break; 242 case DlogRole.ROLE_GUEST: 243 desc = "过客"; 244 break; 245 case DlogRole.ROLE_COMMON: 246 desc = "普通用户"; 247 break; 248 case DlogRole.ROLE_FRIEND: 249 desc = "我的好友"; 250 break; 251 case DlogRole.ROLE_BUDDY: 252 desc = "密友"; 253 break; 254 default: 255 desc = "未知"; 256 } 257 return desc; 258 } 259 260 263 public void setDisplayName(String string) { 264 displayName = string; 265 } 266 267 270 public void setEmail(String string) { 271 email = string; 272 } 273 274 277 public void setHomePage(String string) { 278 homePage = string; 279 } 280 281 284 public void setId(int i) { 285 id = i; 286 } 287 288 291 public void setLastTime(Date date) { 292 lastTime = date; 293 } 294 295 298 public void setLoginCount(int i) { 299 loginCount = i; 300 } 301 302 305 public void setPassword(String string) { 306 password = string; 307 } 308 309 312 public void setRegTime(Date date) { 313 regTime = date; 314 } 315 316 public String getRegDate(){ 317 if(regTime==null) 318 return null; 319 return DATE_FORMAT.format(regTime); 320 } 321 322 public String getLastDate(){ 323 if(lastTime==null) 324 return null; 325 return DATE_FORMAT.format(lastTime); 326 } 327 328 331 public void setResume(String string) { 332 resume = string; 333 } 334 335 338 public void setUserRole(int i) { 339 userRole = i; 340 } 341 342 345 public List getLogs() { 346 return logs; 347 } 348 349 public int getLogCount(){ 350 return logCount; 351 } 352 353 356 public List getReplies() { 357 return replies; 358 } 359 360 363 public void setLogs(List list) { 364 logs = list; 365 } 366 367 370 public void setReplies(List list) { 371 replies = list; 372 } 373 374 377 public String getLoginName() { 378 return loginName; 379 } 380 381 384 public void setLoginName(String string) { 385 loginName = string; 386 } 387 388 391 public int getMode() { 392 return mode; 393 } 394 395 398 public void setMode(int i) { 399 mode = i; 400 } 401 402 405 public int getReplyCount() { 406 return replyCount; 407 } 408 409 412 public void setLogCount(int i) { 413 logCount = i; 414 } 415 416 419 public void setReplyCount(int i) { 420 replyCount = i; 421 } 422 423 426 public SiteForm getSite() { 427 return site; 428 } 429 430 433 public void setSite(SiteForm form) { 434 site = form; 435 } 436 437 public String getPortrait() { 438 return portrait; 439 } 440 public void setPortrait(String portrait) { 441 this.portrait = portrait; 442 } 443 public DlogRole getRole() { 444 return role; 445 } 446 public void setRole(DlogRole role) { 447 this.role = role; 448 } 449 public String getCats() { 450 return cats; 451 } 452 public void setCats(String cats) { 453 this.cats = cats; 454 } 455 456 public int getBookMarkCount() { 457 return bookMarkCount; 458 } 459 public void setBookMarkCount(int bookMarkCount) { 460 this.bookMarkCount = bookMarkCount; 461 } 462 466 public int[] getOwnerCatids(){ 467 List ids = new ArrayList (); 468 if(cats!=null){ 469 StringTokenizer st = new StringTokenizer (cats,","); 470 while(st.hasMoreElements()){ 471 String sId = st.nextToken(); 472 try{ 473 Integer IId = new Integer (Integer.parseInt(sId)); 474 if(!ids.contains(IId)) 475 ids.add(IId); 476 }catch(Exception e){} 477 } 478 } 479 int[] nIds = new int[ids.size()]; 480 for(int i=0;i<ids.size();i++) 481 nIds[i] = ((Integer )ids.get(i)).intValue(); 482 return nIds; 483 } 484 } 485 | Popular Tags |