1 53 54 106 107 package com.Yasna.forum.tags; 108 109 import java.util.*; 110 import javax.servlet.*; 111 import javax.servlet.jsp.*; 112 import javax.servlet.jsp.tagext.*; 113 import javax.servlet.http.*; 114 import com.Yasna.forum.*; 115 116 125 126 public class YazdState implements HttpSessionBindingListener 127 { 128 private Authorization authorization = null; 129 private int message_depth = 1; 131 private int thread_depth = 3; 132 private int items_per_page = 25; 133 private int forumID = -1; 136 private int threadID = -1; 137 private int messageID = -1; 138 private int parentID = -1; 139 private boolean logged_in = false; 141 private Date lastVisit = null; 143 private Date nextVisit = null; 145 private Map nextForumVisit = new HashMap(); 147 private Map lastForumVisit = new HashMap(); 148 private Map lastForumVisitCookie = new HashMap(); 149 private Map prevMessages = new HashMap(); 151 public final static String LAST_VISIT = "lastVisitDate"; 153 public final static String THREAD_DEPTH = "threadDepth"; 154 public final static String MESSAGE_DEPTH = "messageDepth"; 155 public final static String ITEMS_PER_PAGE = "itemsPerPage"; 156 public final static String LAST_FORUM_VISIT = "lastForumVisitDate_"; 157 public final static int COOKIE_EXPIRE = 60*60*24*365; 158 159 163 public final void valueBound(HttpSessionBindingEvent e) 164 { 165 } 166 167 174 public final void valueUnbound(HttpSessionBindingEvent e) 175 { 176 saveLastVisit(); 177 } 178 179 186 public final void saveLastVisit() 187 { 188 if( nextVisit == null )return; 189 User user; 190 ForumFactory ff = ForumFactory.getInstance(authorization); 191 if( ff == null )return; 192 ProfileManager pm = ff.getProfileManager(); 193 try { 194 user = pm.getUser(authorization.getUserID()); 195 } catch( UserNotFoundException ex ) { 196 return; 197 } 198 if( user.isAnonymous() ) { 199 return; 200 } 201 202 user.setProperty(LAST_VISIT,"" + nextVisit.getTime()); 203 String tmp; 205 Date last; 206 for( Iterator it=nextForumVisit.keySet().iterator(); it.hasNext(); ) { 207 tmp = (String )it.next(); 208 last = (Date)nextForumVisit.get(tmp); 209 user.setProperty( tmp, "" + last.getTime()); 210 } 211 } 212 213 217 public final void setAuthorization(Authorization auth) 218 { 219 authorization = auth; 220 } 221 222 228 public final Authorization getAuthorization() 229 { 230 return authorization; 231 } 232 233 237 public final void setForumID(int id) 238 { 239 forumID=id; 240 } 241 242 248 public final int getForumID() 249 { 250 return forumID; 251 } 252 253 257 public final void setThreadID(int id) 258 { 259 threadID=id; 260 } 261 262 268 public final int getThreadID() 269 { 270 return threadID; 271 } 272 273 277 public final void setMessageID(int id) 278 { 279 messageID=id; 280 } 281 282 288 public final int getMessageID() 289 { 290 return messageID; 291 } 292 293 296 public final void setParentID(int id) 297 { 298 parentID=id; 299 } 300 301 304 public final int getParentID() 305 { 306 return parentID; 307 } 308 309 312 public final void setLastVisit(Date d) 313 { 314 lastVisit = d; 315 } 316 317 322 public final Date getLastVisit() 323 { 324 return lastVisit; 325 } 326 327 335 public final void setNextForumVisitDate(PageContext pc, int forumid) 336 { 337 StringBuffer fid = new StringBuffer (); 338 fid.append(LAST_FORUM_VISIT + forumid); 339 if( nextForumVisit.get(fid.toString()) == null ) { 340 Date now = new Date(); 341 HttpServletRequest req = (HttpServletRequest)pc.getRequest(); 342 Cookie cookie = new Cookie(fid.toString(), "" + now.getTime()); 343 cookie.setPath(req.getContextPath()); 344 cookie.setMaxAge(COOKIE_EXPIRE); 345 HttpServletResponse res = (HttpServletResponse)pc.getResponse(); 346 res.addCookie(cookie); 347 nextForumVisit.put(fid.toString(), now); 348 } 349 } 350 351 360 public final void setLastForumVisitDate(String prop, Date date) 361 { 362 lastForumVisit.put(prop,date); 363 } 364 365 369 public final void resetLastForumVisitDate(YazdRequest jr) 370 { 371 try { 373 ProfileManager pm = jr.getProfileManager(); 374 User user = null; 375 try { 376 user = pm.getUser(getAuthorization().getUserID()); 377 } catch( UserNotFoundException ex ) { 378 } 379 if( user != null && !user.isAnonymous() ) { 380 String tmp; 381 String value; 382 Date last; 383 Enumeration enume = user.propertyNames(); 384 while( enume.hasMoreElements() ) { 385 tmp = (String )enume.nextElement(); 386 if( tmp.startsWith(LAST_FORUM_VISIT) ) { 387 value = user.getProperty(tmp); 388 if( value != null ) { 389 long last_time = Long.valueOf(value).longValue(); 390 last = new Date(last_time); 391 lastForumVisit.put(tmp,last); 392 } 393 } 394 } 395 } 396 } catch(JspException e) {} 397 } 398 399 404 public final Date getLastForumVisitDate(Forum cf, YazdRequest jr) 405 { 406 StringBuffer fid = new StringBuffer (); 407 fid.append(LAST_FORUM_VISIT + cf.getID()); 408 Date last = (Date)lastForumVisit.get(fid.toString()); 409 if( last == null ) { 410 last = cf.getCreationDate(); 412 } 413 return last; 414 } 415 416 426 public final void setNextVisit(PageContext pc) 427 { 428 if( nextVisit == null ) { 429 nextVisit = new Date(); 430 HttpServletRequest req = (HttpServletRequest)pc.getRequest(); 431 Cookie cookie = new Cookie(LAST_VISIT,"" + nextVisit.getTime()); 432 cookie.setPath(req.getContextPath()); 433 cookie.setMaxAge(COOKIE_EXPIRE); 434 HttpServletResponse res = (HttpServletResponse)pc.getResponse(); 435 res.addCookie(cookie); 436 } 437 } 438 439 442 public final void setLoggedIn(boolean l) 443 { 444 logged_in = l; 445 } 446 447 452 public final boolean getLoggedIn() 453 { 454 return logged_in; 455 } 456 457 462 public final int getMessageDepth() 463 { 464 return message_depth; 465 } 466 467 470 public final void setMessageDepth(int d) 471 { 472 message_depth = d; 473 } 474 475 480 public final int getThreadDepth() 481 { 482 if( thread_depth < message_depth ) 483 return message_depth; 484 return thread_depth; 485 } 486 487 490 public final void setThreadDepth(int d) 491 { 492 thread_depth = d; 493 } 494 495 500 public final int getItemsPerPage() 501 { 502 return items_per_page; 503 } 504 505 508 public final void setItemsPerPage(int d) 509 { 510 items_per_page = d; 511 } 512 513 517 public final void addMessagePage(String currentPage, String nextPage) 518 { 519 String key = "" + forumID + "_" + threadID; 520 Map map = (Map)prevMessages.get(key); 521 if( map == null ) { 522 map = new HashMap(); 523 prevMessages.put(key,map); 524 } 525 map.put(nextPage,currentPage); 526 } 527 528 533 public final String getMessagePage(String currentPage) 534 { 535 String key = "" + forumID + "_" + threadID; 536 Map map = (Map)prevMessages.get(key); 537 if( map != null ) { 538 String tmp = (String )map.get(currentPage); 539 if( tmp != null ) 540 return tmp; 541 } 542 return ""; 543 } 544 } 545 | Popular Tags |