KickJava   Java API By Example, From Geeks To Geeks.

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


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>thread_loop</b>, used to loop through available threads
121  * in the current forum.
122  * <p>
123  * Optional attribute <b>begin</b> can be set to the number at
124  * which the thread_loop should start.
125  * <p>
126  * Optional attribute <b>end</b> can be set to the number at
127  * which the thread_loop should end.
128  * <p>
129  * Must be nested inside a <b>forum</b> tag.
130  * <p>
131  * During each loop the body of the thread_loop tag is processed.
132  * <p>
133  * The <b>on_entry</b> tag can be used to include content if this is
134  * the first time through the loop.
135  * <p>
136  * The <b>on_exit</b> tag can be used to include content if this is
137  * the last time through the loop.
138  * <p>
139  * The thread_loop continues looping until there are no more threads
140  * or the users items_per_page limit has been reached. The <b>next_page</b>
141  * tag can be used to determine if the thread loop ended due to the
142  * users items_per_page limit. Then the <b>next_item</b> tag can be
143  * used to get the query portion of an HTML href so that you can setup
144  * an href to page to the next list of threads.
145  * <p>
146  * The thread_loop can loop back to the previous list of threads
147  * after the thread_loop has been paged. The <b>prev_page</b>
148  * tag can be used to determine if the thread loop has a previous page.
149  * Then the <b>prev_item</b> tag can be used to get the query portion
150  * of an HTML href so that you can setup an href to page to the previous
151  * list of threads.
152  * <p>
153  * Uses the the following HTTP input parameter
154  * <p><ul>
155  * <li><b>begin</b> - index of thread in current forum to start this thread_loop at
156  * </ul>
157  * <p>
158  * JSP Tag Lib Descriptor
159  * <p><pre>
160  * &lt;name&gt;thread_loop&lt;/name&gt;
161  * &lt;tagclass&gt;com.Yasna.forum.tags.ThreadLoopTag&lt;/tagclass&gt;
162  * &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
163  * &lt;info&gt;Implement a loop for a list of threads in a forum&lt;/info&gt;
164  * &lt;attribute&gt;
165  * &lt;name&gt;begin&lt;/name&gt;
166  * &lt;required&gt;false&lt;/required&gt;
167  * &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
168  * &lt;/attribute&gt;
169  * &lt;attribute&gt;
170  * &lt;name&gt;end&lt;/name&gt;
171  * &lt;required&gt;false&lt;/required&gt;
172  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
173  * &lt;/attribute&gt;
174  * </pre>
175  *
176  * @see ForumTag
177  * @see ThreadTag
178  * @see OnEntryTag
179  * @see NextPageTag
180  * @see NextItemTag
181  * @see PrevPageTag
182  * @see PrevItemTag
183  * @see OnExitTag
184  * @see YazdRequest
185  *
186  * @author Glenn Nielsen
187  */

188 public class ThreadLoopTag extends BodyTagSupport implements Paging
189 {
190   private YazdState js = null;
191   private YazdRequest jr = null;
192   // int's to keep track of our looping
193
private int begin = 0;
194   private int end = -1;
195
196   private int size = 0;
197   // Number of threads being displayed on this page
198
private int thread_num = 0;
199   // Do we have more threads to display after current page?
200
private boolean next_page = false;
201   // Do we have a previous list of threads to display before current page?
202
private boolean prev_page = false;
203   // Flag whether this is the first iteration of loop
204
private boolean is_entry = true;
205   // Flag whether this is the last iteration of loop
206
private boolean is_exit = false;
207   // Our list of threads to iterate through
208
private Iterator thrit = null;
209   // Current thread to display inside thread_loop
210
private ForumThread thr = null;
211
212   /**
213    * Method called at start of thread_loop Tag
214    *
215    * @return an EVAL_BODY_TAG if thread_loop should continue, or a SKIP_BODY if thread_loop is completed.
216    */

217   public final int doStartTag() throws JspException
218   {
219     // Get the user state information
220
js = (YazdState)pageContext.getAttribute("yazdUserState",
221                 PageContext.SESSION_SCOPE);
222     if( js == null )
223       throw new JspException("Yazd thread_loop tag, could not find yazdUserState");
224
225     // Get the user request information
226
jr = (YazdRequest)pageContext.getAttribute("yazdUserRequest",
227                 PageContext.REQUEST_SCOPE);
228     if( jr == null )
229       throw new JspException("Could not find request");
230
231     // Get the Forum this thread_loop tag is nested within
232
ForumTag ft = null;
233     try {
234       ft = (ForumTag)this.findAncestorWithClass(this,
235         Class.forName("com.Yasna.forum.tags.ForumTag"));
236     } catch(Exception JavaDoc e) {
237     }
238
239     if( ft == null ) {
240       throw new JspException("Yazd thread_loop tag could not find forum.");
241     }
242
243     // Get the HTML input parameter begin to setup where thread_loop
244
// should start
245
ServletRequest req = pageContext.getRequest();
246     String JavaDoc tmp = req.getParameter("begin");
247     if( tmp != null && tmp.length() > 0 ) {
248       try {
249         begin = Integer.valueOf(tmp).intValue();
250       } catch(NumberFormatException JavaDoc e) {
251       }
252     }
253
254     // Get the list of threads and setup counters for the thread_loop
255
size = ft.getForum().getThreadCount();
256
257     if( end < 0 || end > size )
258       end = size;
259     if( (begin >= end) )
260       return SKIP_BODY;
261     if( (end - begin) > js.getItemsPerPage() )
262       end = begin + js.getItemsPerPage();
263
264     String JavaDoc sortByStr = (String JavaDoc)req.getAttribute("sortBy");
265     int sortBy = Forum.SORT_BY_CREATE_DATE;
266     if ("modifiedDate".equals(sortByStr)) {
267         sortBy = Forum.SORT_BY_MODIFIED_DATE;
268     }
269     thrit = ft.getForum().threads(begin,end-begin, sortBy);
270     if( thrit == null ) {
271       throw new JspException("Yazd thread_loop tag could not get Iterator for forum.");
272     }
273
274     if( !thrit.hasNext() )
275       return SKIP_BODY;
276     thr = (ForumThread)thrit.next();
277     thread_num++;
278     if( !thrit.hasNext() ) {
279       is_exit = true;
280       if( end < size )
281         next_page = true;
282     }
283     if( begin > 0 )
284       prev_page = true;
285
286     return EVAL_BODY_TAG;
287   }
288
289   /**
290    * Method called at end of each thread_loop Tag Body
291    *
292    * @return an EVAL_BODY_TAG if thread_loop should continue, or a SKIP_BODY if thread_loop is completed.
293    */

294   public final int doAfterBody() throws JspException
295   {
296     is_entry = prev_page = false;
297     thread_num++;
298     if( !thrit.hasNext() ) {
299       return SKIP_BODY;
300     }
301     thr = (ForumThread)thrit.next();
302     if( !thrit.hasNext() ) {
303       is_exit = true;
304       if( end < size )
305         next_page = true;
306     }
307     return EVAL_BODY_TAG;
308   }
309
310   /**
311    * Method called at end of thread_loop Tag
312    * @return EVAL_PAGE
313    */

314   public final int doEndTag() throws JspException
315   {
316     try
317     {
318       if(bodyContent != null)
319         bodyContent.writeOut(bodyContent.getEnclosingWriter());
320     } catch(java.io.IOException JavaDoc e)
321     {
322       throw new JspException("IO Error: " + e.getMessage());
323     }
324     return EVAL_PAGE;
325   }
326
327   /**
328    * Set the beginning index into an enumerated list
329    */

330   public final void setBegin(String JavaDoc beg)
331   {
332     begin = Integer.valueOf(beg).intValue();
333   }
334
335   /**
336    * Set the ending index into an enumerated list
337    */

338   public final void setEnd(String JavaDoc last)
339   {
340     end = Integer.valueOf(last).intValue();
341   }
342
343   /**
344    * Used by <b>thread</b> tag to get the current ForumThread
345    *
346    * @return Current ForumThread
347    */

348   public final ForumThread getThread()
349   {
350     return thr;
351   }
352
353   /**
354    * Used by <b>on_entry</b> tag to detect if this is the first iteration
355    * of the loop.
356    *
357    * @return true or false
358    */

359   public final boolean isEntry()
360   {
361     return is_entry;
362   }
363
364   /**
365    * Used by <b>on_exit</b> tag to detect if this is the last iteration
366    * of the loop.
367    *
368    * @return true or false
369    */

370   public final boolean isExit()
371   {
372     return is_exit;
373   }
374
375   /**
376    * Used by <b>next_page</b> tag to detect if thread listing could continue
377    * on another page.
378    *
379    * @return true or false
380    */

381   public final boolean isNextPage()
382   {
383     return next_page;
384   }
385
386   /**
387    * Used by <b>prev_page</b> tag to detect if thread listing
388    * has a previous page.
389    *
390    * @return true or false
391    */

392   public final boolean isPrevPage()
393   {
394     return prev_page;
395   }
396
397   /**
398    * Used by <b>next_item</b> tag to return query portion of an HTML GET href
399    * for paging to next list of threads.
400    *
401    * @return String - query portion of an HTML GET href
402    */

403   public final String JavaDoc nextItem()
404   {
405     return "begin=" + (begin+thread_num);
406   }
407
408   /**
409    * Used by <b>prev_item</b> tag to return query portion of an HTML GET href
410    * for paging to previous list of threads.
411    *
412    * @return String - query portion of an HTML GET href
413    */

414   public final String JavaDoc prevItem()
415   {
416     int prev = begin - js.getItemsPerPage();
417     if( prev < 0 )
418       prev = 0;
419     return "begin=" + prev;
420   }
421
422 }
423
Popular Tags