KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.text.*;
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.util.*;
117
118  /**
119  * Maintains Yazd state information for the current request.
120  * <p>
121  * Created by the <b>authorize</b> tag and is the class that is tied
122  * to the <b>authorize</b> script variable to provide access by JSP
123  * page authors to request data using &lt;jsp:getProperty/&gt;.
124  * <p>
125  * Provides methods required for maintaining the request state information
126  * for use by other tags.
127
128  * @see AuthorizeTag
129  * @see YazdState
130  *
131  * @author Glenn Nielsen
132  */

133
134 public class YazdRequest
135 {
136   private YazdState js = null;
137   private ForumFactory ff = null;
138   private PageContext pageContext = null;
139   private ProfileManager pm = null;
140   private List forums = null;
141   private List error = new java.util.LinkedList JavaDoc();
142
143   /**
144    * YazdRequest frequently needs to use the YazdState,
145    * so just set it once.
146    *
147    * @param YazdState
148    */

149   public final void setYazdState(YazdState j)
150   {
151     js = j;
152   }
153
154   /**
155    * Get the list of Forums user is authorized to view.
156    * Used by the <b>forum</b> and <b>forum_loop</b> tags.
157    *
158    * @return a List of Forums
159    */

160
161   public final List getForums(Map match) throws JspException
162   {
163     checkYazdState();
164     if( forums == null ) {
165       forums = new java.util.LinkedList JavaDoc();
166       Iterator vf = ff.forums();
167       while( vf.hasNext() ) {
168         Forum forum = (Forum)vf.next();
169         boolean match_made = true;
170         String JavaDoc prop;
171         String JavaDoc value;
172         for(Iterator it=match.keySet().iterator();it.hasNext();) {
173           prop = (String JavaDoc)it.next();
174           if( (value = forum.getProperty(prop)) == null ) {
175             match_made = false;
176             break;
177           }
178           if( !value.equals((String JavaDoc)match.get(prop)) ) {
179             match_made = false;
180             break;
181           }
182         }
183         if( match_made ) {
184       forums.add( forum );
185         }
186       }
187     }
188     return forums;
189   }
190 /*
191   public final List getForums(Map match) throws JspException
192   {
193     checkYazdState();
194     if( forums == null ) {
195       forums = new java.util.LinkedList();
196       Iterator vf = ff.forums(match); Fix Me
197       while( vf.hasNext() ) {
198            forums.add( vf.next() );
199       }
200     }
201     return forums;
202   }
203 */

204
205
206   /**
207    * Gets the current forum user is viewing by current thread or
208    * defaults to first forum.
209    *
210    * Used by the <b>forum</b> and <b>thread</b> tags.
211    *
212    * @return currently selected Forum
213    */

214   public final Forum getForum() throws JspException
215   {
216     Forum cf = null;
217     checkYazdState();
218
219     int threadid = js.getForumID();
220     if( threadid < 0 ) {
221       java.util.LinkedList JavaDoc forums = (java.util.LinkedList JavaDoc)getForums(new HashMap());
222       if( forums.size() > 0 ) {
223         cf = (Forum)forums.get(0);
224         js.setForumID(cf.getID());
225       }
226     } else {
227       try {
228         cf = ff.getForum(threadid);
229       } catch(ForumNotFoundException e) {
230         throw new JspException("Yazd forum tag, could not find forum with ID:" +
231           threadid);
232       } catch(UnauthorizedException e) {
233         throw new JspException("Yazd forum tag, user not authorized for forum with ID:" +
234           threadid);
235       }
236     }
237     if( cf == null ) {
238       throw new JspException("Yazd forum tag, no forums available for user.");
239     }
240
241     return cf;
242   }
243     public final ThreadType getThreadType(int id) throws JspException{
244         checkYazdState();
245         return ff.getThreadType(id);
246     }
247
248   /**
249    * Gets the current forum user is viewing by current thread or
250    * defaults to first thread in current forum.
251    *
252    * Used by the <b>thread</b> tag.
253    *
254    * @return currently selected Thread
255    */

256   public final ForumThread getThread() throws JspException
257   {
258     Forum cf = getForum();
259     ForumThread ct = null;
260
261     int threadid = js.getThreadID();
262     if( threadid < 0 ) {
263       Iterator threads = cf.threads(0,1,Forum.SORT_BY_CREATE_DATE);
264       if( threads.hasNext() ) {
265         ct = (ForumThread)threads.next();
266       }
267     } else {
268       try {
269         ct = cf.getThread(threadid);
270       } catch(ForumThreadNotFoundException e) {
271         throw new JspException("Could not find thread with ID:" + threadid);
272       }
273     }
274     if( ct == null ) {
275       throw new JspException("Yazd thread tag, no threads available for user.");
276     }
277
278     return ct;
279   }
280
281   /**
282    * Gets the current message user is viewing by current message or
283    * defaults to first message in current thread.
284    *
285    * Used by the <b>message</b> tag.
286    *
287    * @return currently selected Message
288    */

289   public final ForumMessage getMessage() throws JspException
290   {
291     ForumThread ct = getThread();
292     ForumMessage cm = null;
293
294     int messageid = js.getMessageID();
295     try {
296       if( messageid < 0 ) {
297         cm = ct.getRootMessage();
298       } else {
299         cm = ct.getMessage(messageid);
300       }
301     } catch(ForumMessageNotFoundException e) {
302         throw new JspException("Could not find message with messageID: " +
303            messageid);
304     }
305     if( cm == null ) {
306       throw new JspException("Yazd message tag, no messages available for user.");
307     }
308
309     return cm;
310   }
311
312   /**
313    * NumberOfForums property which can be obtained by the JSP page
314    * using the <b>authorize</b> script variable with
315    * &lt;jsp:getProperty name=<i>"id"</i> property="numberOfForums"/&gt;
316    *
317    * @return number of Forums user is authorized to view
318    */

319   public final String JavaDoc getNumberOfForums() throws JspException
320   {
321     return "" + getForumFactory().getForumCount();
322   }
323
324   /**
325    * Date and time of users last visit (integer) property which can be
326    * obtained by the JSP page using the <b>authorize</b> script variable with
327    * &lt;jsp:getProperty name=<i>"id"</i> property="lastVisit"/&gt;
328    *
329    * @return date and time of users last visit
330    */

331   public final String JavaDoc getLastVisit()
332   {
333     return "" + js.getLastVisit().getTime();
334   }
335
336   /**
337    * Method used by many tags to add an error to the list of errors
338    * which can be returned using the <b>error_loop</b> tag.
339    */

340   public final void addError(String JavaDoc err)
341   {
342     error.add((Object JavaDoc)new StringBuffer JavaDoc(err));
343   }
344
345   /**
346    * Method used by <b>if_error</b> and <b>error_loop</b> tags to
347    * get the list of user errors.
348    *
349    * @return a List of user errors
350    */

351   public final List getErrorList()
352   {
353     return error;
354   }
355
356   /**
357    * Used by the JSP page to set the error for the current request
358    * using the <b>authorize</b> script variable with
359    * &lt;jsp:setProperty name=<i>"id"</i> property="error" value="<i>string</i>"/&gt;
360    */

361   public final void setError(String JavaDoc err)
362   {
363     error.clear();
364     error.add((Object JavaDoc)new StringBuffer JavaDoc(err));
365   }
366
367   /**
368    * Test whether current user is anonymous.
369    *
370    * @return boolean - true or false
371    */

372   public final boolean isAnonymous()
373   {
374     if( js.getLoggedIn() )
375       return false;
376     return true;
377   }
378
379   /**
380    * MessageDepth property which can be obtained by the JSP page
381    * using the <b>authorize</b> script variable with
382    * &lt;jsp:getProperty name=<i>"id"</i> property="messageDepth"/&gt;
383    *
384    * @return user message_depth display preference
385    */

386   public final String JavaDoc getMessageDepth()
387   {
388     return "" + js.getMessageDepth();
389   }
390
391   /**
392    * Used by the JSP page to set the message_depth display preferences of
393    * user for current session using the <b>authorize</b> script variable with
394    * &lt;jsp:setProperty name=<i>"id"</i> property="messageDepth" value="<i>integer</i>"/&gt;
395    */

396   public final void setMessageDepth(String JavaDoc s)
397   {
398     try {
399       js.setMessageDepth(Integer.valueOf(s).intValue());
400     } catch(NumberFormatException JavaDoc e) {
401     }
402   }
403
404   /**
405    * ThreadDepth property which can be obtained by the JSP page
406    * using the <b>authorize</b> script variable with
407    * &lt;jsp:getProperty name=<i>"id"</i> property="threadDepth"/&gt;
408    *
409    * @return user thread_depth display preference
410    */

411   public final String JavaDoc getThreadDepth()
412   {
413     return "" + js.getThreadDepth();
414   }
415
416   /**
417    * Used by the JSP page to set the thread_depth display preferences of
418    * user for current session using the <b>authorize</b> script variable with
419    * &lt;jsp:setProperty name=<i>"id"</i> property="theadDepth" value="<i>integer</i>"/&gt;
420    */

421   public final void setThreadDepth(String JavaDoc s)
422   {
423     try {
424       js.setThreadDepth(Integer.valueOf(s).intValue());
425     } catch(NumberFormatException JavaDoc e) {
426     }
427   }
428
429   /**
430    * ItemsPerPage property which can be obtained by the JSP page
431    * using the <b>authorize</b> script variable with
432    * &lt;jsp:getProperty name=<i>"id"</i> property="itemsPerPage"/&gt;
433    *
434    * @return user items_per_page display preference
435    */

436   public final String JavaDoc getItemsPerPage()
437   {
438     return "" + js.getItemsPerPage();
439   }
440
441   /**
442    * Used by the JSP page to set the items_per_page display preferences of
443    * user for current session using the <b>authorize</b> script variable with
444    * &lt;jsp:setProperty name=<i>"id"</i> property="itemsPerPage" value="<i>integer</i>"/&gt;
445    */

446   public final void setItemsPerPage(String JavaDoc s)
447   {
448     try {
449       js.setItemsPerPage(Integer.valueOf(s).intValue());
450     } catch(NumberFormatException JavaDoc e) {
451     }
452   }
453
454   /**
455    * Generate a random password.
456    *
457    * @return an 8 character password
458    */

459   public final String JavaDoc GeneratePassword()
460   {
461     return StringUtils.randomString(8);
462   }
463
464   /**
465    * Method used by many tags to get the ForumFactory.
466    *
467    * @return ForumFactory
468    */

469   public final ForumFactory getForumFactory() throws JspException
470   {
471     checkYazdState();
472     return ff;
473   }
474
475   /**
476    * Method used by many tags to get the User ProfileManager.
477    *
478    * @return User ProfileManager
479    */

480   public final ProfileManager getProfileManager() throws JspException
481   {
482     checkYazdState();
483     if( pm == null )
484       pm = ff.getProfileManager();
485     if( pm == null )
486       throw new JspException("Could not get ProfileManager.");
487     return pm;
488   }
489
490   /**
491    * Method used by many YazdRequest methods to make sure
492    * the necessary resources are available.
493    *
494    * @throws JspException if a resource can not be found
495    */

496   private void checkYazdState() throws JspException
497   {
498     if( js == null || js.getAuthorization() == null )
499       throw new JspException("YazdState not set for YazdRequest");
500     if( ff == null )
501       ff = ForumFactory.getInstance(js.getAuthorization());
502     if( ff == null )
503       throw new JspException("Could not get ForumFactory.");
504   }
505 }
506
Popular Tags