KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.text.*;
112 import javax.servlet.*;
113 import javax.servlet.jsp.*;
114 import javax.servlet.jsp.tagext.*;
115 import javax.servlet.http.*;
116 import com.Yasna.forum.*;
117 import com.Yasna.forum.tags.*;
118
119 /**
120  * JSP Tag <b>message_loop</b>, used to loop through messages
121  * returned by a query in the current forum.
122  * <p>
123  * Required attribute <b>query</b> is set to the script
124  * variable id of a <b>query</b> tag to search for messages
125  * based on a query.
126  * <p>
127  * Must be nested inside a <b>forum</b> tag.
128  * <p>
129  * During each loop the body of the message_loop tag is processed.
130  * <p>
131  * The <b>on_entry</b> tag can be used to include content if this is
132  * the first time through the loop.
133  * <p>
134  * The <b>on_exit</b> tag can be used to include content if this is
135  * the last time through the loop.
136  * <p>
137  * The message_loop continues looping until there are no more messages
138  * or the users items_per_page limit has been reached. The <b>next_page</b>
139  * tag can be used to determine if the message loop ended due to the
140  * users items_per_page limit. Then the <b>next_item</b> tag can be
141  * used to get the query portion of an HTML href so that you can setup
142  * an href to page to the next list of messages.
143  * <p>
144  * The message_loop can loop back to the previous list of messages
145  * after the message_loop has been paged. The <b>prev_page</b>
146  * tag can be used to determine if the message loop has a previous page.
147  * Then the <b>prev_item</b> tag can be used to get the query portion
148  * of an HTML href so that you can setup an href to page to the previous
149  * list of messages.
150  * <p>
151  * Uses the the following HTTP input parameter
152  * <p><ul>
153  * <li><b>begin</b> - index of message in current forum query results to start this message_loop at
154  * </ul>
155  * <p>
156  * JSP Tag Lib Descriptor
157  * <p><pre>
158  * &lt;name&gt;message_loop&lt;/name&gt;
159  * &lt;tagclass&gt;com.Yasna.forum.tags.MessageLoopTag&lt;/tagclass&gt;
160  * &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
161  * &lt;info&gt;Implement a loop for a list of messages in a forum&lt;/info&gt;
162  * &lt;attribute&gt;
163  * &lt;name&gt;query&lt;/name&gt;
164  * &lt;required&gt;true&lt;/required&gt;
165  * &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
166  * &lt;/attribute&gt;
167  * </pre>
168  *
169  * @see ForumTag
170  * @see QueryTag
171  * @see OnEntryTag
172  * @see NextPageTag
173  * @see NextItemTag
174  * @see PrevPageTag
175  * @see PrevItemTag
176  * @see OnExitTag
177  * @see YazdRequest
178  *
179  * @author Glenn Nielsen
180  */

181 public class MessageLoopTag extends BodyTagSupport
182   implements Paging, GetNestedMessage
183 {
184   private YazdState js = null;
185   private YazdRequest jr = null;
186   // int's to keep track of our looping
187
private int begin = 0;
188   private String JavaDoc query = null;
189
190   private int size = 0;
191   // Number of messages being displayed on this page
192
private int message_num = 0;
193   // Do we have more messages to display after current page?
194
private boolean next_page = false;
195   // Do we have a previous list of messages to display before current page?
196
private boolean prev_page = false;
197   // Flag whether this is the first iteration of loop
198
private boolean is_entry = true;
199   // Flag whether this is the last iteration of loop
200
private boolean is_exit = false;
201   // Our list of messages to iterate through
202
private Iterator mit = null;
203   // Current message to display inside message_loop
204
private ForumMessage cm = null;
205
206   /**
207    * Method called at start of message_loop Tag
208    *
209    * @return an EVAL_BODY_TAG if message_loop should continue, or a SKIP_BODY if message_loop is completed.
210    */

211   public final int doStartTag() throws JspException
212   {
213     // Get the user state information
214
js = (YazdState)pageContext.getAttribute("yazdUserState",
215                 PageContext.SESSION_SCOPE);
216     if( js == null )
217       throw new JspException("Yazd message_loop tag, could not find yazdUserState");
218
219     // Get the user request information
220
jr = (YazdRequest)pageContext.getAttribute("yazdUserRequest",
221                 PageContext.REQUEST_SCOPE);
222     if( jr == null )
223       throw new JspException("Could not find request");
224
225     // Get the Forum this message_loop tag is nested within
226
ForumTag ft = null;
227     try {
228       ft = (ForumTag)this.findAncestorWithClass(this,
229         Class.forName("com.Yasna.forum.tags.ForumTag"));
230     } catch(Exception JavaDoc e) {
231     }
232
233     if( ft == null ) {
234       throw new JspException("Yazd message_loop tag could not find forum.");
235     }
236
237     // Get the HTML input parameter begin to setup where message_loop
238
// should start
239
ServletRequest req = pageContext.getRequest();
240     String JavaDoc tmp = req.getParameter("begin");
241     if( tmp != null && tmp.length() > 0 ) {
242       try {
243         begin = Integer.valueOf(tmp).intValue();
244       } catch(NumberFormatException JavaDoc e) {
245       }
246     }
247
248     QueryTag qt = null;
249     try {
250       qt = (QueryTag)pageContext.getAttribute(query,PageContext.PAGE_SCOPE);
251     } catch(Exception JavaDoc e) {
252       throw new JspException(
253     "Yazd message tag, could not find query tag with id: " + query);
254     }
255     Map properties = qt.getQuery();
256     Query q = ft.getForum().createQuery();
257     if( properties.get("beforeDate") != null ) {
258       tmp = (String JavaDoc)properties.get("beforeDate");
259       try {
260         if( tmp != null && tmp.length() > 0 ) {
261           Date date = new Date(Long.valueOf(tmp).longValue());
262           q.setBeforeDate(date);
263         }
264       } catch(Exception JavaDoc e) {
265     throw new JspException("Yazd message tag, bad query tag beforeDate");
266       }
267     }
268     if( properties.get("afterDate") != null ) {
269       tmp = (String JavaDoc)properties.get("afterDate");
270       try {
271     if( tmp != null && tmp.length() > 0 ) {
272           Date date = new Date(Long.valueOf(tmp).longValue());
273           q.setAfterDate(date);
274         }
275       } catch(Exception JavaDoc e) {
276         throw new JspException("Yazd message tag, bad query tag afterDate");
277       }
278     }
279     if( properties.get("queryString") != null ) {
280       tmp = (String JavaDoc)properties.get("queryString");
281       if( tmp == null ) {
282     throw new JspException("Yazd message tag, bad query tag queryString");
283       }
284       if( tmp.length() > 0 )
285         q.setQueryString(tmp);
286     }
287     if( properties.get("userID") != null ) {
288       tmp = (String JavaDoc)properties.get("userID");
289       try {
290         ProfileManager pm = jr.getProfileManager();
291         User user = pm.getUser(tmp);
292         q.filterOnUser(user);
293       } catch(UserNotFoundException ue) {
294 // System.out.println("Search Query User: " + tmp + " not found.");
295
} catch(Exception JavaDoc e) {
296         throw new JspException("Yazd message tag, bad query tag userID:" +
297       e.getMessage());
298       }
299     }
300     mit = q.results(begin,js.getItemsPerPage());
301
302     if( mit == null ) {
303       throw new JspException("Yazd message_loop tag could not get Iterator for forum messages.");
304     }
305
306     if( !mit.hasNext() )
307       return SKIP_BODY;
308     cm = (ForumMessage)mit.next();
309     message_num++;
310     if( !mit.hasNext() ) {
311       is_exit = true;
312       if( message_num >= js.getItemsPerPage() )
313         next_page = true;
314     }
315     if( begin > 0 )
316       prev_page = true;
317
318     return EVAL_BODY_TAG;
319   }
320
321   /**
322    * Method called at end of each message_loop Tag Body
323    *
324    * @return an EVAL_BODY_TAG if message_loop should continue, or a SKIP_BODY if message_loop is completed.
325    */

326   public final int doAfterBody() throws JspException
327   {
328     is_entry = prev_page = false;
329     message_num++;
330     if( !mit.hasNext() ) {
331       return SKIP_BODY;
332     }
333     cm = (ForumMessage)mit.next();
334     if( !mit.hasNext() ) {
335       is_exit = true;
336       if( message_num >= js.getItemsPerPage() )
337         next_page = true;
338     }
339     return EVAL_BODY_TAG;
340   }
341
342   /**
343    * Method called at end of message_loop Tag
344    * @return EVAL_PAGE
345    */

346   public final int doEndTag() throws JspException
347   {
348     try
349     {
350       if(bodyContent != null)
351         bodyContent.writeOut(bodyContent.getEnclosingWriter());
352     } catch(java.io.IOException JavaDoc e)
353     {
354       throw new JspException("IO Error: " + e.getMessage());
355     }
356     return EVAL_PAGE;
357   }
358
359   /**
360    * Set the forum message query to use
361    */

362   public final void setQuery(String JavaDoc qry)
363   {
364     query = qry;
365   }
366
367   /**
368    * Used by <b>message</b> tag to get the current ForumMessage
369    *
370    * @return ForumMessage
371    */

372   public final ForumMessage getMessage()
373   {
374     return cm;
375   }
376
377   /**
378    * Return total number of messages in thread, required by
379    * GetNestedMessage.
380    *
381    * @return String - thread MessageCount
382    */

383   public final int getTotal()
384   {
385     return 0;
386   }
387
388   /**
389    * Used by <b>on_entry</b> tag to detect if this is the first iteration
390    * of the loop.
391    *
392    * @return true or false
393    */

394   public final boolean isEntry()
395   {
396     return is_entry;
397   }
398
399   /**
400    * Used by <b>on_exit</b> tag to detect if this is the last iteration
401    * of the loop.
402    *
403    * @return true or false
404    */

405   public final boolean isExit()
406   {
407     return is_exit;
408   }
409
410   /**
411    * Used by <b>next_page</b> tag to detect if message listing could continue
412    * on another page.
413    *
414    * @return true or false
415    */

416   public final boolean isNextPage()
417   {
418     return next_page;
419   }
420
421   /**
422    * Used by <b>prev_page</b> tag to detect if message listing
423    * has a previous page.
424    *
425    * @return true or false
426    */

427   public final boolean isPrevPage()
428   {
429     return prev_page;
430   }
431
432   /**
433    * Used by <b>next_item</b> tag to return query portion of an HTML GET href
434    * for paging to next list of messages.
435    *
436    * @return String - query portion of an HTML GET href
437    */

438   public final String JavaDoc nextItem()
439   {
440     return "begin=" + (begin+message_num);
441   }
442
443   /**
444    * Used by <b>prev_item</b> tag to return query portion of an HTML GET href
445    * for paging to previous list of messages.
446    *
447    * @return String - query portion of an HTML GET href
448    */

449   public final String JavaDoc prevItem()
450   {
451     int prev = begin - js.getItemsPerPage();
452     if( prev < 0 )
453       prev = 0;
454     return "begin=" + prev;
455   }
456
457 }
458
Popular Tags