KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Yasna > forum > tags > YazdState


1 /**
2  * Copyright (C) 2001 Yasna.com. All rights reserved.
3  *
4  * ===================================================================
5  * The Apache Software License, Version 1.1
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution,
20  * if any, must include the following acknowledgment:
21  * "This product includes software developed by
22  * Yasna.com (http://www.yasna.com)."
23  * Alternately, this acknowledgment may appear in the software itself,
24  * if and wherever such third-party acknowledgments normally appear.
25  *
26  * 4. The names "Yazd" and "Yasna.com" must not be used to
27  * endorse or promote products derived from this software without
28  * prior written permission. For written permission, please
29  * contact yazd@yasna.com.
30  *
31  * 5. Products derived from this software may not be called "Yazd",
32  * nor may "Yazd" appear in their name, without prior written
33  * permission of Yasna.com.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL YASNA.COM OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of Yasna.com. For more information
51  * on Yasna.com, please see <http://www.yasna.com>.
52  */

53
54 /**
55  * Copyright (C) 2000 CoolServlets.com. All rights reserved.
56  *
57  * ===================================================================
58  * The Apache Software License, Version 1.1
59  *
60  * Redistribution and use in source and binary forms, with or without
61  * modification, are permitted provided that the following conditions
62  * are met:
63  *
64  * 1. Redistributions of source code must retain the above copyright
65  * notice, this list of conditions and the following disclaimer.
66  *
67  * 2. Redistributions in binary form must reproduce the above copyright
68  * notice, this list of conditions and the following disclaimer in
69  * the documentation and/or other materials provided with the
70  * distribution.
71  *
72  * 3. The end-user documentation included with the redistribution,
73  * if any, must include the following acknowledgment:
74  * "This product includes software developed by
75  * CoolServlets.com (http://www.coolservlets.com)."
76  * Alternately, this acknowledgment may appear in the software itself,
77  * if and wherever such third-party acknowledgments normally appear.
78  *
79  * 4. The names "Jive" and "CoolServlets.com" must not be used to
80  * endorse or promote products derived from this software without
81  * prior written permission. For written permission, please
82  * contact webmaster@coolservlets.com.
83  *
84  * 5. Products derived from this software may not be called "Jive",
85  * nor may "Jive" appear in their name, without prior written
86  * permission of CoolServlets.com.
87  *
88  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
89  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
90  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
91  * DISCLAIMED. IN NO EVENT SHALL COOLSERVLETS.COM OR
92  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
93  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
94  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
95  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
96  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
97  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
98  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
99  * SUCH DAMAGE.
100  * ====================================================================
101  *
102  * This software consists of voluntary contributions made by many
103  * individuals on behalf of CoolServlets.com. For more information
104  * on CoolServlets.com, please see <http://www.coolservlets.com>.
105  */

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  /**
117  * Maintains Yazd state information for the user session.
118  * Provides methods required for maintaining the
119  * user session state information.
120  *
121  * @see YazdRequest
122  *
123  * @author Glenn Nielsen
124  */

125
126 public class YazdState implements HttpSessionBindingListener
127 {
128   private Authorization authorization = null;
129   // Keep track of user display preferences
130
private int message_depth = 1;
131   private int thread_depth = 3;
132   private int items_per_page = 25;
133   // Keep track of currently selected forum, thread, and message
134
// for forum navigation
135
private int forumID = -1;
136   private int threadID = -1;
137   private int messageID = -1;
138   private int parentID = -1;
139   // Flag whether the user is logged in
140
private boolean logged_in = false;
141   // Date of users last session
142
private Date lastVisit = null;
143   // Date of users last use of current session
144
private Date nextVisit = null;
145   // Variables for tracking last visist to a Forum
146
private Map nextForumVisit = new HashMap();
147   private Map lastForumVisit = new HashMap();
148   private Map lastForumVisitCookie = new HashMap();
149   // Variables for tracking previous messages page
150
private Map prevMessages = new HashMap();
151   // And some constants for use with extended user properties
152
public final static String JavaDoc LAST_VISIT = "lastVisitDate";
153   public final static String JavaDoc THREAD_DEPTH = "threadDepth";
154   public final static String JavaDoc MESSAGE_DEPTH = "messageDepth";
155   public final static String JavaDoc ITEMS_PER_PAGE = "itemsPerPage";
156   public final static String JavaDoc LAST_FORUM_VISIT = "lastForumVisitDate_";
157   public final static int COOKIE_EXPIRE = 60*60*24*365;
158
159   /**
160    * Required for implementing interface
161    * HttpSessionBindingListener
162    */

163   public final void valueBound(HttpSessionBindingEvent e)
164   {
165   }
166
167   /**
168    * Required for implementing interface
169    * HttpSessionBindingListener, used to save the
170    * date and time of users lastVisit to Yazd.
171    * If forums were visited, save nextForumVisit date
172    * and time as an extended user property.
173    */

174   public final void valueUnbound(HttpSessionBindingEvent e)
175   {
176     saveLastVisit();
177   }
178
179   /**
180    * If user is logged in save the
181    * date and time user last used current session
182    * as their lastVisist to Yazd.
183    * If forums were visited, save nextForumVisit date
184    * and time as an extended user property.
185    */

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     // Save next lastForumVisit dates to extended user properties
204
String JavaDoc tmp;
205     Date last;
206     for( Iterator it=nextForumVisit.keySet().iterator(); it.hasNext(); ) {
207       tmp = (String JavaDoc)it.next();
208       last = (Date)nextForumVisit.get(tmp);
209       user.setProperty( tmp, "" + last.getTime());
210     }
211   }
212
213   /**
214    * Update users Authorization state information
215    * for current session.
216    */

217   public final void setAuthorization(Authorization auth)
218   {
219     authorization = auth;
220   }
221
222   /**
223    * Get users Authorization state information
224    * for current session.
225    *
226    * @return users Authorization
227    */

228   public final Authorization getAuthorization()
229   {
230     return authorization;
231   }
232
233   /**
234    * Set the users currently selected forum
235    * as session state information
236    */

237   public final void setForumID(int id)
238   {
239     forumID=id;
240   }
241
242   /**
243    * Get the users currently selected forum
244    * from session state information
245    *
246    * @return forumID
247    */

248   public final int getForumID()
249   {
250     return forumID;
251   }
252
253   /**
254    * Set the users currently selected thread
255    * as session state information
256    */

257   public final void setThreadID(int id)
258   {
259     threadID=id;
260   }
261
262   /**
263    * Get the users currently selected thread
264    * from session state information
265    *
266    * @return threadID
267    */

268   public final int getThreadID()
269   {
270     return threadID;
271   }
272
273   /**
274    * Set the users currently selected message
275    * as session state information
276    */

277   public final void setMessageID(int id)
278   {
279     messageID=id;
280   }
281
282   /**
283    * Get the users currently selected message
284    * from session state information
285    *
286    * @return messageID
287    */

288   public final int getMessageID()
289   {
290     return messageID;
291   }
292
293   /**
294    * Under construction.
295    */

296   public final void setParentID(int id)
297   {
298     parentID=id;
299   }
300
301   /**
302    * Under construction.
303    */

304   public final int getParentID()
305   {
306     return parentID;
307   }
308
309   /**
310    * Set the Date of the users last visit to Yazd
311    */

312   public final void setLastVisit(Date d)
313   {
314     lastVisit = d;
315   }
316
317   /**
318    * Get the Date of the users last visit to Yazd
319    *
320    * @return Date of users last Yazd visit
321    */

322   public final Date getLastVisit()
323   {
324     return lastVisit;
325   }
326
327   /**
328    * Used by <b>forum</b> tag to set the Date of the users last visit to a forum.
329    * This is saved as an extended user property when the
330    * user logs out or the users session times out. If the
331    * users nextForumVisit setting is new, send a cookie.
332    *
333    * @param PageContext for request
334    */

335   public final void setNextForumVisitDate(PageContext pc, int forumid)
336   {
337     StringBuffer JavaDoc fid = new StringBuffer JavaDoc();
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   /**
352    * Set the Date of the users last visit to a forum in the users
353    * YazdState information.
354    *
355    * Used by <b>authorize</b> and <b>login</b> tag.
356    *
357    * @param String lastForumVisit property name
358    * @param Date of last visit to forum
359    */

360   public final void setLastForumVisitDate(String JavaDoc prop, Date date)
361   {
362     lastForumVisit.put(prop,date);
363   }
364
365   /**
366    * Used by <b>login</b> tag to reset the users lastForumVisit
367    * based on their user extended properties.
368    */

369   public final void resetLastForumVisitDate(YazdRequest jr)
370   {
371     // Get the User data
372
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 JavaDoc tmp;
381         String JavaDoc value;
382     Date last;
383     Enumeration enume = user.propertyNames();
384     while( enume.hasMoreElements() ) {
385       tmp = (String JavaDoc)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   /**
400    * Used by forum tag to get the Date of the users last visit to a forum.
401    *
402    * @return Date of users last forum visit
403    */

404   public final Date getLastForumVisitDate(Forum cf, YazdRequest jr)
405   {
406     StringBuffer JavaDoc fid = new StringBuffer JavaDoc();
407     fid.append(LAST_FORUM_VISIT + cf.getID());
408     Date last = (Date)lastForumVisit.get(fid.toString());
409     if( last == null ) {
410       // If all else fails, use the forum creation date
411
last = cf.getCreationDate();
412     }
413     return last;
414   }
415
416   /**
417    * Set the Date of the users next lastVisit to the forum.
418    * This is saved as an extended user property when the
419    * user logs out or the users session times out. If the
420    * users nextVisit setting is new, send a cookie.
421    *
422    * Used by the <b>authorize</b> and <b>logout</b> tags.
423    *
424    * @param PageContext for request
425    */

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   /**
440    * Set the users login status flag.
441    */

442   public final void setLoggedIn(boolean l)
443   {
444     logged_in = l;
445   }
446
447   /**
448    * Get the users login status flag.
449    *
450    * @return true if user is logged in, false if user is anonymous
451    */

452   public final boolean getLoggedIn()
453   {
454     return logged_in;
455   }
456
457   /**
458    * Get the users current session messageDepth display preference.
459    *
460    * @return messageDepth
461    */

462   public final int getMessageDepth()
463   {
464     return message_depth;
465   }
466
467   /**
468    * Set the users current session messageDepth display preference.
469    */

470   public final void setMessageDepth(int d)
471   {
472     message_depth = d;
473   }
474
475   /**
476    * Get the users current session threadDepth display preference.
477    *
478    * @return threadDepth
479    */

480   public final int getThreadDepth()
481   {
482     if( thread_depth < message_depth )
483       return message_depth;
484     return thread_depth;
485   }
486
487   /**
488    * Set the users current session threadDepth display preference.
489    */

490   public final void setThreadDepth(int d)
491   {
492     thread_depth = d;
493   }
494
495   /**
496    * Get the users current session itemsPerPage display preference.
497    *
498    * @return itemsPerPage
499    */

500   public final int getItemsPerPage()
501   {
502     return items_per_page;
503   }
504
505   /**
506    * Set the users current session itemsPerPage display preference.
507    */

508   public final void setItemsPerPage(int d)
509   {
510     items_per_page = d;
511   }
512
513   /**
514    * Add an HREF to the message paging stack for a forum/thread.
515    *
516    */

517   public final void addMessagePage(String JavaDoc currentPage, String JavaDoc nextPage)
518   {
519     String JavaDoc 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   /**
529    * Get the HREF of a previous message page list.
530    *
531    * @return an HREF string
532    */

533   public final String JavaDoc getMessagePage(String JavaDoc currentPage)
534   {
535     String JavaDoc key = "" + forumID + "_" + threadID;
536     Map map = (Map)prevMessages.get(key);
537     if( map != null ) {
538       String JavaDoc tmp = (String JavaDoc)map.get(currentPage);
539       if( tmp != null )
540         return tmp;
541     }
542     return "";
543   }
544 }
545
Popular Tags