KickJava   Java API By Example, From Geeks To Geeks.

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


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</b>, used to get information about current thread.
121  * <p>
122  * Requires that attribute <b>id</b> be set to the name of a
123  * script variable for later use in JSP to retrieve Thread data
124  * using &lt;jsp:getProperty/&gt;.
125  * <p>
126  * Gets the current thread from within a <b>thread_loop</b> tag or
127  * the current thread from the user state information.
128  * <p>
129  * The <b>move_thread</b> or <b>move_message</b> tag can be used
130  * nested within the <b>thread</b> tag to move a thread to a
131  * different forum or move the thread message as a reply to another
132  * forum thread message.
133  * <p>
134  * JSP Tag Lib Descriptor
135  * <p><pre>
136  * &lt;name&gt;thread&lt;/name&gt;
137  * &lt;tagclass&gt;com.Yasna.forum.tags.ThreadTag&lt;/tagclass&gt;
138  * &lt;teiclass&gt;com.Yasna.forum.tags.ThreadTEI&lt;/teiclass&gt;
139  * &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
140  * &lt;info&gt;Get the current thread data&lt;/info&gt;
141  * &lt;attribute&gt;
142  * &lt;name&gt;id&lt;/name&gt;
143  * &lt;required&gt;true&lt;/required&gt;
144  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
145  * &lt;/attribute&gt;
146  * </pre>
147  *
148  * @see ThreadLoopTag
149  * @see MoveThreadTag
150  * @see MoveMessageTag
151  * @see YazdRequest
152  * @see YazdState
153  * @see NewMessages
154  * @see ChangeForum
155  * @see GetNestedMessage
156  *
157  * @author Glenn Nielsen
158  */

159 public class ThreadTag extends TagSupport implements GetNestedMessage,
160     ChangeForum, NewMessages
161 {
162   private ForumThread ct = null;
163   private YazdRequest jr = null;
164   private YazdState js = null;
165   // ID's of previous and next thread for use as a navigation aide
166
private int prevThreadID = -1;
167   private int nextThreadID = -1;
168   // Move thread or thread message to another forum variables
169
private Forum mf = null;
170   private int mtid = 0;
171   private int mmid = 0;
172
173   /**
174    * Method called at start of thread Tag to get Thread
175    *
176    * @return EVAL_BODY_INCLUDE if there is a thread or SKIP_BODY if there is not a thread to view
177    */

178   public final int doStartTag() throws JspException
179   {
180     // Get the user state information
181
js = (YazdState)pageContext.getAttribute("yazdUserState",
182                 PageContext.SESSION_SCOPE);
183     if( js == null ) {
184       throw new JspException("Yazd thread tag could not get yazd state.");
185     }
186
187     // Get the user request information
188
jr = (YazdRequest)pageContext.getAttribute("yazdUserRequest",
189                 PageContext.REQUEST_SCOPE);
190     if( jr == null )
191       throw new JspException("Yazd thread tag, could not find request");
192
193     // See if we are nested inside a thread_loop tag
194
ThreadLoopTag tl = null;
195     try {
196       tl = (ThreadLoopTag)this.findAncestorWithClass(this,
197         Class.forName("com.Yasna.forum.tags.ThreadLoopTag"));
198     } catch(Exception JavaDoc e) {
199     }
200
201     if( tl != null ) {
202       ct = tl.getThread();
203     } else {
204       // Get the thread from user state information
205
ct = jr.getThread();
206     }
207
208     if( ct == null )
209       return SKIP_BODY;
210
211     // Get the next and previous thread ID's just in case they are needed
212
Iterator thrit = jr.getForum().threads();
213     int nextid;
214     int previd = -1;
215     while( thrit.hasNext() ) {
216       nextid = ((ForumThread)thrit.next()).getID();
217       if( nextid == ct.getID() ) {
218         prevThreadID = previd;
219         if( thrit.hasNext() )
220           nextThreadID = ((ForumThread)thrit.next()).getID();
221         break;
222       }
223       previd = nextid;
224     }
225
226     // Save the script variable so JSP author can access thread data
227
pageContext.setAttribute(id,this,PageContext.PAGE_SCOPE);
228     return EVAL_BODY_INCLUDE;
229   }
230
231   /**
232    * Used by MessageTag, WalkTag, and MoveThreadTag to get current ForumThread
233    *
234    * @return ForumThread
235    */

236   public final ForumThread getThread()
237   {
238     return ct;
239   }
240
241   /**
242    * Thread ID property which can be obtained by the JSP page
243    * using &lt;jsp:getProperty name=<i>"id"</i> property="threadid"/&gt;
244    *
245    * @return String - thread ID
246    */

247   public final String JavaDoc getThreadid()
248   {
249     return "" + ct.getID();
250   }
251
252   /**
253    * Thread MessageCount property which can be obtained by the JSP page
254    * using &lt;jsp:getProperty name=<i>"id"</i> property="messagecount"/&gt;
255    *
256    * @return String - thread MessageCount
257    */

258   public final String JavaDoc getMessagecount()
259   {
260     return "" + ct.getMessageCount();
261   }
262
263   /**
264    * Thread Message Replies property which can be obtained by the JSP page
265    * using &lt;jsp:getProperty name=<i>"id"</i> property="messagereplies"/&gt;
266    *
267    * @return String - thread Message Replies
268    */

269   public final String JavaDoc getMessagereplies()
270   {
271     return "" + (ct.getMessageCount() - 1);
272   }
273
274   /**
275    * Return total number of messages in thread, required by
276    * GetNestedMessage.
277    *
278    * @return String - thread MessageCount
279    */

280   public final int getTotal()
281   {
282     return ct.getMessageCount();
283   }
284
285   /**
286    * Thread Name property which can be obtained by the JSP page
287    * using &lt;jsp:getProperty name=<i>"id"</i> property="name"/&gt;
288    *
289    * @return String - thread Name
290    */

291   public final String JavaDoc getName()
292   {
293     return ct.getName();
294   }
295
296   /**
297    * Forum Name property which can be obtained by the JSP page
298    * using &lt;jsp:getProperty name=<i>"id"</i> property="forumName"/&gt;
299    *
300    * @return String - name of the forum this thread is in
301    */

302   public final String JavaDoc getForumName()
303   {
304     return ct.getForum().getName();
305   }
306
307   /**
308    * Forum ID property which can be obtained by the JSP page
309    * using &lt;jsp:getProperty name=<i>"id"</i> property="forumid"/&gt;
310    *
311    * @return String - id of the forum this thread is in
312    */

313   public final String JavaDoc getForumid()
314   {
315     return "" + ct.getForum().getID();
316   }
317
318   /**
319    * Date and time of Thread CreationDate (integer) property which can be
320    * obtained by the JSP page using
321    * &lt;jsp:getProperty name=<i>"id"</i> property="creationDate"/&gt;
322    *
323    * @return date and time of Thread CreationDate as an integer
324    */

325   public final String JavaDoc getCreationDate()
326   {
327     return "" + ct.getCreationDate().getTime();
328   }
329
330   /**
331    * Date and time of Thread ModifiedDate (integer) property which can be
332    * obtained by the JSP page using
333    * &lt;jsp:getProperty name=<i>"id"</i> property="modifiedDate"/&gt;
334    *
335    * @return date and time of Thread ModifiedDate as an integer
336    */

337   public final String JavaDoc getModifiedDate()
338   {
339     return "" + ct.getModifiedDate().getTime();
340   }
341
342   /**
343    * Next Thread ID property which can be obtained by the JSP page
344    * using &lt;jsp:getProperty name=<i>"id"</i> property="nextThreadid"/&gt;
345    *
346    * @return String - next thread ID
347    */

348   public final String JavaDoc getNextThreadid()
349   {
350     return "" + nextThreadID;
351   }
352
353   /**
354    * Previous Thread ID property which can be obtained by the JSP page
355    * using &lt;jsp:getProperty name=<i>"id"</i> property="prevThreadid"/&gt;
356    *
357    * @return String - previous thread ID
358    */

359   public final String JavaDoc getPrevThreadid()
360   {
361     return "" + prevThreadID;
362   }
363
364   /**
365    * Get the root message for this thread
366    *
367    * @return ForumMessage
368    */

369   public final ForumMessage getMessage() {
370     return ct.getRootMessage();
371   }
372
373   /**
374    * Move the thread to a different forum, used by move_thread tag.
375    *
376    * @return true if thread moved, false if thread move failed
377    */

378   public final boolean moveThread() throws JspException
379   {
380     Forum cf = ct.getForum();
381     int threadid = ct.getID();
382     if( mf == null )
383       throw new JspException(
384     "Yazd thread tag, you must set a forum using change_forum tag before using move_thread tag");
385     try {
386       cf.moveThread(ct,mf);
387       return true;
388
389       /* This should no longer be needed, it should be
390          encapsulated in moveThread now
391       try {
392         ForumThread nt = mf.getThread(threadid);
393     ct = nt;
394     SearchIndexer si = jr.getForumFactory().getSearchIndexer();
395         si.updateThreadForum(ct);
396     return true;
397       } catch(ForumThreadNotFoundException e) {}
398       */

399
400     } catch(UnauthorizedException ue) {
401       jr.addError(TagPropertyManager.getTagProperty("yazd.tag.post.authorize.failed"));
402     }
403     return false;
404   }
405
406   /**
407    * Remove the thread and post its message as a reply to a parent message
408    * for a different forum thread, used by move_message tag.
409    *
410    * @return true if message moved, false if message move failed
411    */

412   public final boolean moveMessage() throws JspException
413   {
414     Forum cf = ct.getForum();
415     int threadid = ct.getID();
416     ForumMessage cm = null;
417     ForumThread mt = null;
418
419     cm = ct.getRootMessage();
420     if( cm == null )
421       throw new JspException(
422         "Yazd thread tag, could not find message to move using move_message tag");
423
424     if( ct.getMessageCount() != 1 )
425       throw new JspException(
426         "Yazd thread tag, you can not move a message thread that has replies using move_message tag");
427
428     if( mf == null )
429       throw new JspException(
430         "Yazd thread tag, you must set a forum using change_forum tag before using move_message tag");
431
432     if( mtid == 0 )
433       throw new JspException(
434         "Yazd thread tag, you must set a thread using set_thread tag before using move_message tag");
435
436     if( mmid == 0 )
437       throw new JspException(
438         "Yazd thread tag, you must set a parent message using set_parent_message tag before using move_message tag");
439
440     cm = ct.getRootMessage();
441     if( cm == null ) {
442       throw new JspException(
443         "Yazd thread tag, could not find thread root message for move_message tag");
444     }
445
446     try {
447       mt = mf.getThread(mtid);
448     } catch(ForumThreadNotFoundException e) {
449       throw new JspException(
450         "Yazd thread tag, could not find thread to move message to");
451     }
452     try {
453       ForumMessage parentMessage = mt.getMessage( mmid );
454       ct.moveMessage(cm,mt,parentMessage);
455       ct = mt;
456       return true;
457
458       /* old move message code
459       cf.removeThread(ct);
460       try {
461     ForumMessage parentMessage = mt.getMessage( mmid );
462     mt.addMessage( parentMessage, cm );
463         ct = mt;
464         SearchIndexer si = jr.getForumFactory().getSearchIndexer();
465         si.updateMessageForum(ct.getMessage(cm.getID()));
466         return true;
467       } catch(ForumMessageNotFoundException e) {
468         throw new JspException(
469           "Yazd thread tag, could not find parent message for move_message tag");
470       }
471       */

472
473     } catch(UnauthorizedException ue) {
474       jr.addError(TagPropertyManager.getTagProperty("yazd.tag.post.authorize.failed"));
475     } catch(ForumMessageNotFoundException e) {
476       throw new JspException(
477         "Yazd thread tag, could not find parent message for move_message tag");
478     }
479     return false;
480   }
481
482   /**
483    * Set an alternate forum where message is to be moved
484    */

485   public final void changeForum(Forum apf)
486   {
487     mf = apf;
488   }
489
490   /**
491    * Determine if thread has new messages since users last visit.
492    *
493    * @return boolean - true or false
494    */

495   public final boolean newMessages()
496   {
497     if( js.getLastForumVisitDate(ct.getForum(),jr).getTime() <
498         ct.getModifiedDate().getTime() )
499                 return true;
500     return false;
501   }
502
503   /**
504    * Determine if there is a thread preceding this one
505    *
506    * @return boolean - true or false
507    */

508   public final boolean prevThread()
509   {
510     if( prevThreadID > 0 )
511       return true;
512     return false;
513   }
514
515   /**
516    * Determine if there is a thread following this one
517    *
518    * @return boolean - true or false
519    */

520   public final boolean nextThread()
521   {
522     if( nextThreadID > 0 )
523       return true;
524     return false;
525   }
526
527   /**
528    * Set the threadId for thread message is being moved to
529    */

530   public final void setThread(int tid)
531   {
532     mtid = tid;
533   }
534
535   /**
536    * Change to a different thread
537    */

538   public final void changeThread(int at) throws JspException
539   {
540     // See if we are nested inside a forum tag
541
ForumTag ft = null;
542     try {
543       ft = (ForumTag)this.findAncestorWithClass(this,
544         Class.forName("com.Yasna.forum.tags.ForumTag"));
545     } catch(Exception JavaDoc e) {
546     }
547     if( ft == null ) {
548       throw new JspException("Yazd thread tag must be nested inside a forum tag");
549     }
550
551     try {
552       ForumThread nt = ft.getForum().getThread(at);
553       ct = nt;
554     } catch(ForumThreadNotFoundException e) {
555       throw new JspException(
556         "Yazd thread tag, could not find thread to change_thread to");
557     }
558   }
559
560   /**
561    * Set the parent message id for message being moved
562    */

563   public final void setParentMessage(int mid)
564   {
565     mmid = mid;
566   }
567
568
569   /**
570    * Remove the script variable after forum tag closed out
571    */

572   public final void release()
573   {
574     if( id != null && id.length() > 0 )
575       pageContext.removeAttribute(id,PageContext.PAGE_SCOPE);
576   }
577
578 }
579
Popular Tags