KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jresearch > gossip > tags > PageRefTag


1 /*
2  * $$Id$$
3  *
4  * ***** BEGIN LICENSE BLOCK *****
5  * The contents of this file are subject to the Mozilla Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License
8  * at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific language governing rights and
13  * limitations under the License.
14  *
15  * The Original Code is JGossip forum code.
16  *
17  * The Initial Developer of the Original Code is the JResearch, Org.
18  * Portions created by the Initial Developer are Copyright (C) 2004
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  * Dmitry Belov <bel@jresearch.org>
23  *
24  * ***** END LICENSE BLOCK ***** */

25 /*
26  * Created on Nov 9, 2003
27  *
28  */

29 package org.jresearch.gossip.tags;
30
31 import java.io.IOException JavaDoc;
32
33 import javax.servlet.http.HttpServletResponse JavaDoc;
34 import javax.servlet.http.HttpSession JavaDoc;
35 import javax.servlet.jsp.JspException JavaDoc;
36 import javax.servlet.jsp.JspWriter JavaDoc;
37 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
38
39 import org.apache.log.Logger;
40 import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
41 import org.jresearch.gossip.IConst;
42 import org.jresearch.gossip.beans.forum.Topic;
43 import org.jresearch.gossip.beans.user.User;
44 import org.jresearch.gossip.exception.SystemException;
45 import org.jresearch.gossip.log.avalon.JGossipLog;
46
47 /**
48  * DOCUMENT ME!
49  *
50  * @author Bel
51  */

52 public class PageRefTag extends TagSupport JavaDoc {
53
54     /**
55      * Logger instance.
56      */

57     private Logger log;
58
59     // name of topic bean from page scope
60
private String JavaDoc threadBean;
61
62     // name of forumid bean from page scope
63
private String JavaDoc forumId;
64
65     /**
66      * Default c'tor.
67      */

68     public PageRefTag() {
69         super();
70         try {
71             log = JGossipLog.getInstance().getAppLogger();
72         } catch (SystemException e) { /* Ignore Exception! */
73         }
74     }
75
76     private void eval() throws JspException JavaDoc {
77         forumId = ExpressionEvaluatorManager.evaluate("forumId", forumId,
78                 Integer JavaDoc.class, this, pageContext).toString();
79     }
80
81     /**
82      * DOCUMENT ME!
83      *
84      * @return DOCUMENT ME!
85      *
86      * @throws JspException
87      * DOCUMENT ME!
88      */

89     public int doStartTag() throws JspException JavaDoc {
90         eval();
91
92         try {
93             HttpSession JavaDoc session = pageContext.getSession();
94             HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) pageContext
95                     .getResponse();
96             User user = (User) session.getAttribute(IConst.SESSION.USER_KEY);
97             Topic t = (Topic) pageContext.getAttribute(threadBean);
98             int total = (int) t.getMessagesCount();
99
100             if (total > user.getSettings().getMes_per_page()) {
101                 StringBuffer JavaDoc links = new StringBuffer JavaDoc("(");
102
103                 int rep = (int) Math.floor(total
104                         / user.getSettings().getMes_per_page()) + 1;
105                 int i = 1;
106
107                 while ((i <= rep)
108                         && (((i - 1) * user.getSettings().getMes_per_page()) < total)) {
109                     StringBuffer JavaDoc href = new StringBuffer JavaDoc();
110                     links.append("<a HREF=\"");
111                     href.append("ShowThread.do?fid=");
112                     href.append(forumId);
113                     href.append("&tid=");
114                     href.append(t.getThreadid());
115                     href.append("&block=");
116                     href.append((i - 1) * user.getSettings().getMes_per_page());
117                     links.append(response.encodeURL(href.toString()));
118                     links.append("\" class=\"thread_name\" >");
119                     links.append(i);
120                     links.append("</a>");
121
122                     if ((++i) <= rep) {
123                         links.append(", ");
124                     }
125                 }
126
127                 links.append(")");
128
129                 JspWriter JavaDoc out = pageContext.getOut();
130                 out.print(links.toString());
131             }
132         } catch (IOException JavaDoc e) {
133             if (log.isErrorEnabled()) {
134                 log.error("PageRefTag::", e);
135             }
136             throw new JspException JavaDoc("error in PageRefTag tag:", e);
137         }
138
139         return (SKIP_BODY);
140     }
141
142     /**
143      * DOCUMENT ME!
144      *
145      * @return
146      */

147     public String JavaDoc getForumId() {
148         return forumId;
149     }
150
151     /**
152      * DOCUMENT ME!
153      *
154      * @return
155      */

156     public String JavaDoc getThreadBean() {
157         return threadBean;
158     }
159
160     /**
161      * DOCUMENT ME!
162      *
163      * @param string
164      */

165     public void setForumId(String JavaDoc string) {
166         forumId = string;
167     }
168
169     /**
170      * DOCUMENT ME!
171      *
172      * @param string
173      */

174     public void setThreadBean(String JavaDoc string) {
175         threadBean = string;
176     }
177 }
178
Popular Tags