KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.*;
110 import java.util.*;
111 import javax.servlet.*;
112 import javax.servlet.jsp.*;
113 import javax.servlet.jsp.tagext.*;
114 import javax.servlet.http.*;
115 import com.Yasna.forum.*;
116 import com.Yasna.forum.tags.*;
117
118 /**
119  * JSP Tag <b>forum</b>, used to get information about current forum.
120  * <p>
121  * Requires that attribute <b>id</b> be set to the name of a
122  * script variable for later use in JSP to retrieve Forum data
123  * using &lt;jsp:getProperty/&gt;.
124  * <p>
125  * If optional attribute <b>trackVisits</b>="true", the date of the
126  * users last visit to the forum will be saved when the user logs out
127  * or their current session ends.
128  * <p>
129  * Optional attribute <b>match</b> can be set to the <b>id</b> of
130  * a <b>match</b> tag to select only those forums that match the
131  * forum extended properties specified by the <b>match</b> tag.
132  * <p>
133  * Gets the current forum from within a <b>forum_loop</b> tag or
134  * the current forum from the user state information.
135  * <p>
136  * JSP Tag Lib Descriptor
137  * <p><pre>
138  * &lt;name&gt;forum&lt;/name&gt;
139  * &lt;tagclass&gt;com.Yasna.forum.tags.ForumTag&lt;/tagclass&gt;
140  * &lt;teiclass&gt;com.Yasna.forum.tags.ForumTEI&lt;/teiclass&gt;
141  * &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
142  * &lt;info&gt;Get the current forum data&lt;/info&gt;
143  * &lt;attribute&gt;
144  * &lt;name&gt;id&lt;/name&gt;
145  * &lt;required&gt;true&lt;/required&gt;
146  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
147  * &lt;/attribute&gt;
148  * &lt;attribute&gt;
149  * &lt;name&gt;trackVisits&lt;/name&gt;
150  * &lt;required&gt;false&lt;/required&gt;
151  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
152  * &lt;/attribute&gt;
153  * &lt;attribute&gt;
154  * &lt;name&gt;match&lt;/name&gt;
155  * &lt;required&gt;false&lt;/required&gt;
156  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
157  * &lt;/attribute&gt;
158  * </pre>
159  *
160  * @see ForumLoopTag
161  * @see MatchTag
162  * @see YazdProperty
163  * @see GetYazdPropertyTag
164  * @see SetYazdPropertyTag
165  * @see YazdRequest
166  *
167  * @author Glenn Nielsen
168  */

169
170 public class ForumTag extends TagSupport implements YazdProperty,
171     ChangeForum, NewMessages
172 {
173   private YazdState js = null;
174   private YazdRequest jr = null;
175   private Forum cf = null;
176   // ID's of previous and next forum for use as a navigation aide
177
private int prevForumID = -1;
178   private int nextForumID = -1;
179   // Flag whether date and time of last visit to this forum should be saved
180
private boolean trackVisits = false;
181   // Hash of forum extended properties to match on when selecting forums
182
private Map match = new HashMap();
183   private ForumsLoopTag forumsLoop;
184
185   /**
186    * Method called at start of forum Tag to get Forum
187    *
188    * @return EVAL_BODY_INCLUDE if there is a forum or SKIP_BODY if there is not a forum to view
189    */

190   public final int doStartTag() throws JspException
191   {
192       forumsLoop = null;
193     try {
194       forumsLoop = (ForumsLoopTag)this.findAncestorWithClass(this,
195         Class.forName("com.Yasna.forum.tags.ForumsLoopTag"));
196     } catch(Exception JavaDoc e) {
197     }
198
199     if (forumsLoop != null) {
200         cf = forumsLoop.getForum();
201         pageContext.setAttribute(id, this, PageContext.PAGE_SCOPE);
202         return EVAL_BODY_INCLUDE;
203     } else {
204
205         // Get the user state information
206
js = (YazdState) pageContext.getAttribute("yazdUserState",
207             PageContext.SESSION_SCOPE);
208         if (js == null) {
209             throw new JspException(
210                 "Yazd forum tag could not get yazd state.");
211         }
212
213         // Get the user request information
214
jr = (YazdRequest) pageContext.getAttribute("yazdUserRequest",
215             PageContext.REQUEST_SCOPE);
216         if (jr == null)
217             throw new JspException("Yazd forum tag, could not find request");
218
219         // See if we are nested inside a forum_loop tag
220
ForumLoopTag fl = null;
221         try {
222             fl = (ForumLoopTag)this.findAncestorWithClass(this,
223                 Class.forName("com.Yasna.forum.tags.ForumLoopTag"));
224         }
225         catch (Exception JavaDoc e) {
226         }
227
228         if (fl != null) {
229             cf = fl.getForum();
230             if (fl.getMatch() != null)
231                 match = fl.getMatch();
232         }
233         else {
234             // Get the forum from user state information
235
cf = jr.getForum();
236         }
237
238         if (cf == null || !cf.hasPermission(ForumPermissions.READ))
239             return SKIP_BODY;
240
241         // Get the next and previous forum ID's just in case they are needed
242
List forums = jr.getForums(match);
243         int nextid;
244         int previd = -1;
245         int size = forums.size();
246         for (int i = 0; i < size; i++) {
247             nextid = ( (Forum) forums.get(i)).getID();
248             if (nextid == cf.getID()) {
249                 prevForumID = previd;
250                 if ( (i + 1) < size)
251                     nextForumID = ( (Forum) forums.get(i + 1)).getID();
252                 break;
253             }
254             previd = nextid;
255         }
256
257         // Set the date and time of last visit to this forum
258
if (trackVisits) {
259             js.setNextForumVisitDate(pageContext, cf.getID());
260         }
261
262         // Save the script variable so JSP author can access forum data
263
pageContext.setAttribute(id, this, PageContext.PAGE_SCOPE);
264         return EVAL_BODY_INCLUDE;
265     }
266   }
267
268   /**
269    * Set a flag indicating whether date and time of users last visit
270    * to this forum should be saved (Optional attribute).
271    */

272   public final void setTrackVisits(String JavaDoc a)
273   {
274     if( a.equals("true") )
275       trackVisits=true;
276   }
277
278   /**
279    * Forum ID property which can be obtained by the JSP page
280    * using &lt;jsp:getProperty name=<i>"id"</i> property="forumid"/&gt;
281    *
282    * @return String - forum ID
283    */

284   public final String JavaDoc getForumid()
285   {
286     return "" + cf.getID();
287   }
288
289   /**
290    * Forum Description property which can be obtained by the JSP page
291    * using &lt;jsp:getProperty name=<i>"id"</i> property="description"/&gt;
292    *
293    * @return String - forum Description
294    */

295   public final String JavaDoc getDescription()
296   {
297     return cf.getDescription();
298   }
299
300   /**
301    * Forum MessageCount property which can be obtained by the JSP page
302    * using &lt;jsp:getProperty name=<i>"id"</i> property="messagecount"/&gt;
303    *
304    * @return String - forum MessageCount
305    */

306   public final String JavaDoc getMessagecount()
307   {
308     return "" + cf.getMessageCount();
309   }
310
311   /**
312    * Forum ThreadCount property which can be obtained by the JSP page
313    * using &lt;jsp:getProperty name=<i>"id"</i> property="threadcount"/&gt;
314    *
315    * @return String - forum ThreadCount
316    */

317   public final String JavaDoc getThreadcount()
318   {
319     return "" + cf.getThreadCount();
320   }
321
322   /**
323    * Forum Name property which can be obtained by the JSP page
324    * using &lt;jsp:getProperty name=<i>"id"</i> property="name"/&gt;
325    *
326    * @return String - forum Name
327    */

328   public final String JavaDoc getName()
329   {
330     return cf.getName();
331   }
332
333   /**
334    * Date and time of Forum CreationDate (integer) property which can be
335    * obtained by the JSP page using
336    * &lt;jsp:getProperty name=<i>"id"</i> property="creationDate"/&gt;
337    *
338    * @return date and time of Forum CreationDate as an integer
339    */

340   public final String JavaDoc getCreationDate()
341   {
342     return "" + cf.getCreationDate().getTime();
343   }
344
345   /**
346    * Date and time of Forum ModifiedDate (integer) property which can be
347    * obtained by the JSP page using
348    * &lt;jsp:getProperty name=<i>"id"</i> property="modifiedDate"/&gt;
349    *
350    * @return date and time of Forum ModifiedDate as an integer
351    */

352   public final String JavaDoc getModifiedDate()
353   {
354     return "" + cf.getModifiedDate().getTime();
355   }
356
357   /**
358    * Date and time Forum was last visited (integer) property which can be
359    * obtained by the JSP page using
360    * &lt;jsp:getProperty name=<i>"id"</i> property="lastForumVisitDate"/&gt;
361    *
362    * @return date and time of lastForumVisitDate as an integer
363    */

364   public final String JavaDoc getLastForumVisitDate()
365   {
366     return "" + js.getLastForumVisitDate(cf,jr).getTime();
367   }
368
369   /**
370    * Next Forum ID property which can be obtained by the JSP page
371    * using &lt;jsp:getProperty name=<i>"id"</i> property="nextForumid"/&gt;
372    *
373    * @return String - next forum ID
374    */

375   public final String JavaDoc getNextForumid()
376   {
377     return "" + nextForumID;
378   }
379
380   /**
381    * Previous Forum ID property which can be obtained by the JSP page
382    * using &lt;jsp:getProperty name=<i>"id"</i> property="prevForumid"/&gt;
383    *
384    * @return String - previous forum ID
385    */

386   public final String JavaDoc getPrevForumid()
387   {
388     return "" + prevForumID;
389   }
390
391   /**
392    * Used by ThreadLoopTag and ThreadTag to get current Forum
393    *
394    * @return Forum
395    */

396   public final Forum getForum()
397   {
398     return cf;
399   }
400
401   /**
402    * Method used by the getYazdProperty tag to get an extended Forum
403    * property from the forum tag script variable.
404    *
405    * @return String - value of the property
406    */

407   public final String JavaDoc getProperty(String JavaDoc name)
408   {
409     String JavaDoc tmp = cf.getProperty(name);
410     if( tmp != null )return tmp;
411     return "";
412   }
413
414   /**
415    * Method used by the setYazdProperty tag to set an extended Forum
416    * property from the forum tag script variable.
417    */

418   public final void setProperty(String JavaDoc name, String JavaDoc value)
419   {
420     try {
421       cf.setProperty(name,value);
422     } catch(UnauthorizedException ue) {
423     }
424   }
425
426   /**
427    * Set the <b>id</b> of a <b>match</b> tag
428    */

429   public final void setMatch(String JavaDoc mtch)
430   {
431     MatchTag fmt =
432       (MatchTag)pageContext.getAttribute(mtch,PageContext.PAGE_SCOPE);
433     if( fmt != null && fmt.getMatch() != null )
434       match = fmt.getMatch();
435   }
436
437   /**
438    * Set a different forum
439    */

440   public final void changeForum(Forum apf)
441   {
442     cf = apf;
443   }
444
445   /**
446    * Determine if forum has new messages since users last visit.
447    *
448    * @return boolean - true or false
449    */

450   public final boolean newMessages()
451   {
452     if( js.getLastForumVisitDate(cf,jr).getTime() <
453         cf.getModifiedDate().getTime() )
454         return true;
455     return false;
456   }
457
458   /**
459    * Determine if there is a forum preceding this one
460    *
461    * @return boolean - true or false
462    */

463   public final boolean prevForum()
464   {
465     if( prevForumID > 0 )
466       return true;
467     return false;
468   }
469
470   /**
471    * Determine if there is a forum following this one
472    *
473    * @return boolean - true or false
474    */

475   public final boolean nextForum()
476   {
477     if( nextForumID > 0 )
478       return true;
479     return false;
480   }
481
482   /**
483    * Remove the script variable after forum tag closed out
484    */

485   public final void release()
486   {
487     if( id != null && id.length() > 0 )
488       pageContext.removeAttribute(id,PageContext.PAGE_SCOPE);
489   }
490
491 }
492
Popular Tags