KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jresearch > gossip > tags > userstatus > UserStatusTag


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 Sep 19, 2003
27  *
28  */

29 package org.jresearch.gossip.tags.userstatus;
30
31 import java.io.IOException JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.Locale JavaDoc;
34
35 import javax.servlet.http.HttpServletRequest JavaDoc;
36 import javax.servlet.jsp.JspException JavaDoc;
37 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
38
39 import org.apache.struts.Globals;
40 import org.apache.struts.util.MessageResources;
41 import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
42 import org.jresearch.gossip.IConst;
43 import org.jresearch.gossip.beans.RankInfoDTO;
44 import org.jresearch.gossip.configuration.Configurator;
45 import org.jresearch.gossip.constants.UserStatus;
46 import org.jresearch.gossip.exception.ConfiguratorException;
47
48 /**
49  * DOCUMENT ME!
50  *
51  * @author Bel
52  */

53 public class UserStatusTag extends TagSupport JavaDoc {
54
55     private String JavaDoc status;
56
57     private String JavaDoc count = "-1";
58
59     private int countInt = -1;
60
61     private int sts;
62
63     /**
64      * @return Returns the status.
65      */

66     public String JavaDoc getStatus() {
67         return status;
68     }
69
70     /**
71      * @param status
72      * The status to set.
73      */

74     public void setStatus(String JavaDoc status) {
75         this.status = status;
76     }
77
78     /**
79      * @return Returns the count.
80      */

81     public String JavaDoc getCount() {
82         return count;
83     }
84
85     /**
86      * @param count
87      * The count to set.
88      */

89     public void setCount(String JavaDoc count) {
90         this.count = count;
91     }
92
93     private void eval() throws JspException JavaDoc {
94         status = ExpressionEvaluatorManager.evaluate("status", status,
95                 String JavaDoc.class, this, pageContext).toString();
96         if (count == null) {
97             count = "-1";
98         }
99         count = ExpressionEvaluatorManager.evaluate("count", count,
100                 String JavaDoc.class, this, pageContext).toString();
101         try {
102             this.countInt = Integer.parseInt(this.count);
103         } catch (NumberFormatException JavaDoc e) {
104             this.countInt = -1;
105         }
106         try {
107             sts = Integer.parseInt(this.status);
108         } catch (NumberFormatException JavaDoc e) {
109             this.countInt = UserStatus.GUEST;
110         }
111     }
112
113     /**
114      * DOCUMENT ME!
115      *
116      * @return DOCUMENT ME!
117      *
118      * @throws JspException
119      * DOCUMENT ME!
120      */

121     public int doStartTag() throws JspException JavaDoc {
122         try {
123             eval();
124             String JavaDoc statusString = new String JavaDoc();
125
126             if (sts == UserStatus.USER
127                     && !Ranks.getInstance().getRanks().isEmpty()
128                     && countInt >= 0) {
129
130                 int delta = Integer.MAX_VALUE;
131                 Iterator JavaDoc it = Ranks.getInstance().getRanks().iterator();
132                 while (it.hasNext()) {
133                     RankInfoDTO rank = (RankInfoDTO) it.next();
134                     int d = rank.getCount() - countInt;
135                     if (d >= 0 && d < delta) {
136                         statusString = rank.getName();
137                     }
138                     delta = d;
139                 }
140             } else {
141                 MessageResources messages = (MessageResources) pageContext
142                         .getServletContext().getAttribute(
143                                 Globals.MESSAGES_KEY
144                                         + Configurator.getInstance().get(
145                                                 IConst.CONFIG.MODULE_PREFIX));
146
147                 String JavaDoc key = IConst.CONTEXT.STATUS_KEY_PREFIX + this.status;
148                 messages.setReturnNull(true);
149
150                 statusString = messages
151                         .getMessage((Locale JavaDoc) ((HttpServletRequest JavaDoc) pageContext
152                                 .getRequest()).getSession().getAttribute(
153                                 org.apache.struts.Globals.LOCALE_KEY), key);
154
155                 if (statusString == null) {
156                     statusString = messages
157                             .getMessage(IConst.CONTEXT.STATUS_KEY_PREFIX + "X");
158                 }
159                 messages.setReturnNull(false);
160             }
161             pageContext.getOut().print(statusString);
162
163         } catch (IOException JavaDoc ex) {
164             throw new JspException JavaDoc("IOException in UserStatusTag tag:", ex);
165         } catch (ConfiguratorException ex) {
166             throw new JspException JavaDoc(
167                     "ConfiguratorException in UserStatusTag tag:", ex);
168         }
169
170         return (SKIP_BODY);
171     }
172 }
Popular Tags