KickJava   Java API By Example, From Geeks To Geeks.

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


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>message</b>, used to get information about current message.
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 ForumMessage data
123  * using &lt;jsp:getProperty/&gt;.
124  * <p>
125  * Gets the current message from the user state information.
126  * <p>
127  * If optional attribute <b>nested</b>="true" the message is obtained
128  * from the closest enclosing <b>walk</b> or <b>thread</b> tag.
129  * <p>
130  * JSP Tag Lib Descriptor
131  * <p><pre>
132  * &lt;name&gt;message&lt;/name&gt;
133  * &lt;tagclass&gt;com.Yasna.forum.tags.MessageTag&lt;/tagclass&gt;
134  * &lt;teiclass&gt;com.Yasna.forum.tags.MessageTEI&lt;/teiclass&gt;
135  * &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
136  * &lt;info&gt;Get the current message data&lt;/info&gt;
137  * &lt;attribute&gt;
138  * &lt;name&gt;id&lt;/name&gt;
139  * &lt;required&gt;true&lt;/required&gt;
140  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
141  * &lt;/attribute&gt;
142  * &lt;attribute&gt;
143  * &lt;name&gt;nested&lt;/name&gt;
144  * &lt;required&gt;false&lt;/required&gt;
145  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
146  * &lt;/attribute&gt;
147  * </pre>
148  *
149  * @see WalkTag
150  * @see ThreadTag
151  * @see YazdRequest
152  * @see YazdState
153  * @see YazdProperty
154  * @see NewMessages
155  *
156  * @author Glenn Nielsen
157  */

158 public class MessageTag extends TagSupport implements YazdProperty,
159     NewMessages
160 {
161   private YazdState js = null;
162   private YazdRequest jr = null;
163   private ForumMessage cm = null;
164   private GetNestedMessage gnm = null;
165   // Flag to indicate the message is from a parent walk or thread tag
166
private boolean nested = false;
167
168   /**
169    * Method called at start of message Tag to get ForumMessage
170    *
171    * @return EVAL_BODY_INCLUDE if there is a message or SKIP_BODY if there is not a message to view
172    */

173   public final int doStartTag() throws JspException
174   {
175     // Get the user state information
176
js = (YazdState)pageContext.getAttribute("yazdUserState",
177                 PageContext.SESSION_SCOPE);
178     if( js == null ) {
179       throw new JspException("Yazd message tag could not get yazd state.");
180     }
181
182     // Get the user request information
183
jr = (YazdRequest)pageContext.getAttribute("yazdUserRequest",
184                 PageContext.REQUEST_SCOPE);
185     if( jr == null )
186       throw new JspException("Yazd message tag, could not find request");
187
188     // See if we are nested inside a walk or thread tag
189
if( !nested ) {
190         // Get the message from user state information
191
cm = jr.getMessage();
192     } else {
193       // Get the closest enclosing walk or thread tag.
194
try {
195         gnm = (GetNestedMessage)this.findAncestorWithClass(this,
196           Class.forName("com.Yasna.forum.tags.GetNestedMessage"));
197     cm = gnm.getMessage();
198       } catch(Exception JavaDoc e) {
199       }
200     }
201
202     if( cm == null )
203       return SKIP_BODY;
204
205     // Save the script variable so JSP author can access message data
206
pageContext.setAttribute(id,this,PageContext.PAGE_SCOPE);
207     return EVAL_BODY_INCLUDE;
208   }
209
210   /**
211    * Set a flag indicating whether message is nested within a walk or thread tag
212    * Optional attribute).
213    */

214   public final void setNested(String JavaDoc a)
215   {
216     if( a.equals("true") )nested=true;
217   }
218
219   /**
220    * Used by UserTag to get the User data for the person
221    * who posted this message.
222    *
223    * @return User who posted message being viewed
224    */

225   public final User getUser()
226   {
227     return cm.getUser();
228   }
229
230   /**
231    * Message ID property which can be obtained by the JSP page
232    * using &lt;jsp:getProperty name=<i>"id"</i> property="messageid"/&gt;
233    *
234    * @return String - message ID
235    */

236   public final String JavaDoc getMessageid()
237   {
238     return "" + cm.getID();
239   }
240
241   /**
242    * Message Body property which can be obtained by the JSP page
243    * using &lt;jsp:getProperty name=<i>"id"</i> property="body"/&gt;
244    *
245    * @return String - message body
246    */

247   public final String JavaDoc getBody()
248   {
249     return cm.getBody();
250   }
251
252   /**
253    * Message Subject property which can be obtained by the JSP page
254    * using &lt;jsp:getProperty name=<i>"id"</i> property="subject"/&gt;
255    *
256    * @return String - message subject
257    */

258   public final String JavaDoc getSubject()
259   {
260     return cm.getSubject();
261   }
262
263   /**
264    * Date and time of Message CreationDate (integer) property which can be
265    * obtained by the JSP page using
266    * &lt;jsp:getProperty name=<i>"id"</i> property="creationDate"/&gt;
267    *
268    * @return date and time of Message CreationDate as an integer
269    */

270   public final String JavaDoc getCreationDate()
271   {
272     return "" + cm.getCreationDate().getTime();
273   }
274
275   /**
276    * Date and time of Message ModifiedDate (integer) property which can be
277    * obtained by the JSP page using
278    * &lt;jsp:getProperty name=<i>"id"</i> property="modifiedDate"/&gt;
279    *
280    * @return date and time of Message ModifiedDate as an integer
281    */

282   public final String JavaDoc getModifiedDate()
283   {
284     return "" + cm.getModifiedDate().getTime();
285   }
286
287   /**
288    * Get the total number of replies to this message
289    *
290    * @return number of replies to this message
291    */

292   public final String JavaDoc getMessagecount()
293   {
294     if( gnm != null ) {
295       return "" + gnm.getTotal();
296     }
297     return "";
298   }
299
300   /**
301    * Get the ID of the thread this message is in
302    *
303    * @return threadID
304    */

305   public final String JavaDoc getThreadid()
306   {
307     return "" + cm.getForumThread().getID();
308   }
309
310   /**
311    * Method used by the getYazdProperty tag to get an extended Message
312    * property from the message tag script variable.
313    *
314    * @return String - value of the property
315    */

316   public final String JavaDoc getProperty(String JavaDoc name)
317   {
318     String JavaDoc tmp = cm.getProperty(name);
319     if( tmp != null )return tmp;
320     return "";
321   }
322
323   /**
324    * Method used by the setYazdProperty tag to set an extended Message
325    * property from the message tag script variable.
326    */

327   public final void setProperty(String JavaDoc name, String JavaDoc value)
328   {
329     cm.setProperty(name,value);
330   }
331
332   /**
333    * Determine if message was modified since users last visit.
334    *
335    * @return boolean - true or false
336    */

337   public final boolean newMessages()
338   {
339     if( js.getLastForumVisitDate(cm.getForumThread().getForum(),jr).getTime() <
340         cm.getModifiedDate().getTime() )
341                 return true;
342     return false;
343   }
344
345   /**
346    * Determine if message was posted by an anonymous user.
347    *
348    * @return boolean - true or false
349    */

350   public final boolean isAnonymous()
351   {
352     return cm.isAnonymous();
353   }
354
355   /**
356    * Remove the script variable after message tag closed out
357    */

358   public final void release()
359   {
360     if( id != null && id.length() > 0 )
361       pageContext.removeAttribute(id,PageContext.PAGE_SCOPE);
362   }
363
364   public final int getReplyPrivateUserId()
365   {
366     return cm.getReplyPrivateUserId();
367   }
368
369   public boolean isPrivate() {
370       return cm.isPrivate();
371   }
372
373   public final boolean isApproved()
374   {
375     return cm.isApproved();
376   }
377
378 }
379
Popular Tags