KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ivata > groupware > web > tag > GetSettingTag


1 /*
2  * Copyright (c) 2001 - 2005 ivata limited.
3  * All rights reserved.
4  * -----------------------------------------------------------------------------
5  * ivata groupware may be redistributed under the GNU General Public
6  * License as published by the Free Software Foundation;
7  * version 2 of the License.
8  *
9  * These programs are free software; you can redistribute them and/or
10  * modify them under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; version 2 of the License.
12  *
13  * These programs are distributed in the hope that they will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  * See the GNU General Public License in the file LICENSE.txt for more
18  * details.
19  *
20  * If you would like a copy of the GNU General Public License write to
21  *
22  * Free Software Foundation, Inc.
23  * 59 Temple Place - Suite 330
24  * Boston, MA 02111-1307, USA.
25  *
26  *
27  * To arrange commercial support and licensing, contact ivata at
28  * http://www.ivata.com/contact.jsp
29  * -----------------------------------------------------------------------------
30  * $Log: GetSettingTag.java,v $
31  * Revision 1.4 2005/04/27 20:19:17 colinmacleod
32  * Fixed handling when value is null.
33  *
34  * Revision 1.3 2005/04/10 20:10:06 colinmacleod
35  * Added new themes.
36  * Changed id type to String.
37  * Changed i tag to em and b tag to strong.
38  * Improved PicoContainerFactory with NanoContainer scripts.
39  *
40  * Revision 1.2 2005/04/09 17:19:59 colinmacleod
41  * Changed copyright text to GPL v2 explicitly.
42  *
43  * Revision 1.1.1.1 2005/03/10 17:51:35 colinmacleod
44  * Restructured ivata op around Hibernate/PicoContainer.
45  * Renamed ivata groupware.
46  *
47  * Revision 1.3 2004/12/23 21:01:30 colinmacleod
48  * Updated Struts to v1.2.4.
49  * Changed base classes to use ivata masks.
50  *
51  * Revision 1.2 2004/11/12 15:57:20 colinmacleod
52  * Removed dependencies on SSLEXT.
53  * Moved Persistence classes to ivata masks.
54  *
55  * Revision 1.1 2004/09/30 15:16:02 colinmacleod
56  * Split off addressbook elements into security subproject.
57  *
58  * Revision 1.4 2004/07/13 19:41:15 colinmacleod
59  * Moved project to POJOs from EJBs.
60  * Applied PicoContainer to services layer (replacing session EJBs).
61  * Applied Hibernate to persistence layer (replacing entity EJBs).
62  *
63  * Revision 1.3 2004/03/21 21:16:08 colinmacleod
64  * Shortened name to ivata op.
65  *
66  * Revision 1.2 2004/02/01 22:00:34 colinmacleod
67  * Added full names to author tags
68  *
69  * Revision 1.1.1.1 2004/01/27 20:57:56 colinmacleod
70  * Moved ivata openportal to SourceForge..
71  *
72  * Revision 1.2 2003/10/17 12:36:13 jano
73  * fixing problems with building
74  * converting intranet -> portal
75  * Eclipse building
76  *
77  * Revision 1.1.1.1 2003/10/13 20:50:10 colin
78  * Restructured portal into subprojects
79  *
80  * Revision 1.1 2003/02/24 19:33:33 colin
81  * moved to jsp
82  *
83  * Revision 1.8 2003/02/04 17:43:50 colin
84  * copyright notice
85  *
86  * Revision 1.7 2002/09/03 11:05:50 colin
87  * fixed bug in setSetting - variable still called sSetting in setter
88  *
89  * Revision 1.6 2002/08/19 12:24:38 colin
90  * tidied documentation; removed Hungarian notation
91  *
92  * Revision 1.5 2002/06/13 07:44:07 colin
93  * first version of rose model: code tidied up/added javadoc
94  *
95  * Revision 1.4 2002/05/11 18:32:41 colin
96  * changed the name of the remote settings instance in the
97  * session from "SettingsRemote" to "settings" for easier
98  * usage in JSP files
99  *
100  * Revision 1.3 2002/05/11 08:53:53 colin
101  * updated and extended javadoc
102  *
103  * Revision 1.1 2002/02/03 01:00:46 colin
104  * first version for shop
105  * -----------------------------------------------------------------------------
106  */

107 package com.ivata.groupware.web.tag;
108
109 import java.io.IOException JavaDoc;
110
111 import javax.servlet.http.HttpServletRequest JavaDoc;
112 import javax.servlet.http.HttpSession JavaDoc;
113 import javax.servlet.jsp.JspException JavaDoc;
114 import javax.servlet.jsp.JspWriter JavaDoc;
115 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
116
117 import org.apache.struts.taglib.TagUtils;
118 import org.picocontainer.PicoContainer;
119
120 import com.ivata.groupware.admin.security.server.SecuritySession;
121 import com.ivata.groupware.admin.security.user.UserDO;
122 import com.ivata.groupware.admin.setting.Settings;
123 import com.ivata.groupware.container.PicoContainerFactory;
124 import com.ivata.mask.util.SystemException;
125
126
127 /**
128  * <p>This tag tries first to get the setting for the user specified
129  * by the session attribute 'userName'. If that has not been set (a
130  * la
131  * login page), or there is no setting for this user, then the
132  * default
133  * setting is retrieved.</p>
134  *
135  * <p>Normally there is a settings object in the session called
136  * 'SettingsRemote' and this is used internally to access the
137  * ivata groupware settings system.</p>
138  *
139  * <p><strong>Tag attributes:</strong><br/>
140  * <table cellpadding='2' cellspacing='5' border='0' align='center'
141  * width='85%'>
142  * <tr class='TableHeadingColor'>
143  * <th>attribute</th>
144  * <th>reqd.</th>
145  * <th>param. class</th>
146  * <th width='100%'>description</th>
147  * </tr>
148  * <tr class='TableRowColor'>
149  * <td>setting</td>
150  * <td>true</td>
151  * <td><code>String</code></td>
152  * <td>The name of the setting to retrieve from the ivata groupware
153  * system.</td>
154  * </tr>
155  * <tr class='TableRowColor'>
156  * <td>userName</td>
157  * <td>false</td>
158  * <td><code>String</code></td>
159  * <td>The name of the user for whom to retrieve the
160  * ivata groupware
161  * system
162  * setting.</td>
163  * </tr>
164  * </table>
165  * </p>
166  *
167  *
168  * @since 2002-02-02
169  * @author Colin MacLeod
170  * <a HREF='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
171  * @version $Revision: 1.4 $
172  */

173 public class GetSettingTag extends TagSupport JavaDoc {
174     /**
175      * <p>
176      * Identifier for page attribute. If this attribute is not set, then
177      * the setting is output to the page.
178      * </p>
179      */

180     private String JavaDoc id = null;
181     /**
182      * <p>
183      * Scope to which the page attribute is set, if <code>id</code> is set.
184      * </p>
185      */

186     private String JavaDoc scope = "page";
187     /**
188      * <p>Property declaration for tag attribute: setting.</p>
189      */

190     private String JavaDoc setting = null;
191     /**
192      * <p>Class of the setting to be retrieved - this must match the setting
193      * type.</p>
194      */

195     private String JavaDoc type = null;
196
197     /**
198      * <p>This method is called when the JSP engine encounters the start
199      * tag, after the attributes are processed.<p>
200      *
201      * <p>Scripting variables (if any) have their values set here.</p>
202      *
203      * <p><strong>Technical implementation:</strong><br/> We locate a {@link
204      * com.ivata.groupware.admin.setting.SettingsRemote SettingsRemote} object
205      * in
206      * the session (attribute) 'SettingsRemote'. We then use this to get
207      * the setting specified. If it is not null, we send this to the
208      * <code>JspWriter</code> from the <code>pageContext</code>.</p>
209      *
210      * @return <code>SKIP_BODY</code> since this tag has no body
211      * @exception JspException if there is a <code>NamingExcpetion</code>
212      * getting the <code>InitialContext</code>
213      * @exception JspException if the session applicationServer is not
214      * set
215      * @throws JspException if there is a problem creating the
216      * SettingsRemote EJB
217      * @throws JspException if there is a
218      * <code>java.rmi.RemoteException</code retrieving the setting
219      * @throws JspException if there is an error wrting to
220      * <code>out.print()</code>
221      */

222     public int doStartTag() throws JspException JavaDoc {
223         // before we do anything else, get the session
224
HttpSession JavaDoc session = pageContext.getSession();
225         SecuritySession securitySession = (SecuritySession)
226             session.getAttribute("securitySession");
227         PicoContainer container;
228         UserDO user;
229         // if there is no security session (= timeout), then we'll default to
230
// the global system setting
231
if (securitySession == null) {
232             try {
233                 container = PicoContainerFactory.getInstance().getGlobalContainer();
234             } catch (SystemException e) {
235                 throw new JspException JavaDoc(e);
236             }
237             user = null;
238         } else {
239             container = securitySession.getContainer();
240             user = securitySession.getUser();
241         }
242         Settings settings = (Settings) container.getComponentInstance(
243                 Settings.class);
244
245         Object JavaDoc value;
246         try {
247             value = settings.getSetting(securitySession, setting,
248                     user);
249         } catch(SystemException eRemote) {
250             throw new JspException JavaDoc(eRemote);
251         }
252
253         // check the type matches, if specified
254
if (type != null) {
255             Class JavaDoc typeClass;
256             try {
257                 typeClass = Class.forName(type);
258             } catch (ClassNotFoundException JavaDoc e) {
259                 throw new JspException JavaDoc(e);
260             }
261             if ((value != null)
262                     && (!typeClass.isAssignableFrom(value.getClass()))) {
263                 throw new JspException JavaDoc("ERROR: setting '"
264                     + setting
265                     + "' is type '"
266                     + value.getClass().getName()
267                     + "', expected '"
268                     + type
269                     + "'");
270             }
271         }
272
273
274         // if we got a value, try sending this to the page
275
if(value != null) {
276             if (id == null) {
277                 try {
278                     JspWriter JavaDoc out = pageContext.getOut();
279                     out.print(value);
280                 } catch(IOException JavaDoc eIo) {
281                     throw new JspException JavaDoc(eIo);
282                 }
283             } else {
284                 // if there _is_ an id, set the value to the page
285
HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc)
286                     pageContext.getRequest();
287                 int pageScope = TagUtils.getInstance().getScope(scope);
288                 pageContext.setAttribute(id, value, pageScope);
289             }
290         }
291         // indicates that the body should not be evaluated - this tag has no body
292
return SKIP_BODY;
293     }
294     /**
295      * <p>
296      * Identifier for page attribute. If this attribute is not set, then
297      * the setting is output to the page.
298      * </p>
299      *
300      * @return current identifier for page attribute.
301      */

302     public final String JavaDoc getId() {
303         return id;
304     }
305
306     /**
307      * <p>
308      * Scope to which the page attribute is set, if <code>id</code> is set.
309      * </p>
310      *
311      * @return scope to which the page attribute is set.
312      */

313     public final String JavaDoc getScope() {
314         return scope;
315     }
316
317     /**
318      * <p>Get the value supplied to the attribute 'setting'.</p>
319      *
320      * <p>This attribute represents the name of the setting to retrieve
321      * from the ivata groupware system.</p>
322      *
323      * @return the value supplied to the attribute 'setting'
324      */

325     public final String JavaDoc getSetting() {
326         return setting;
327     }
328     /**
329      * <p>Class of the setting to be retrieved - this must match the setting
330      * type.</p>
331      *
332      * @return class of the setting to be retrieved.
333      */

334     public final String JavaDoc getType() {
335         return type;
336     }
337
338     /**
339      * <p>
340      * Identifier for page attribute. If this attribute is not set, then
341      * the setting is output to the page.
342      * </p>
343      *
344      * @param string new identifier for page attribute.
345      */

346     public final void setId(final String JavaDoc string) {
347         id = string;
348     }
349
350     /**
351      * <p>
352      * Scope to which the page attribute is set, if <code>id</code> is set.
353      * </p>
354      *
355      * @param string scope to which the page attribute is set.
356      */

357     public final void setScope(final String JavaDoc string) {
358         scope = string;
359     }
360
361     /**
362      * <p>Set the value supplied to the attribute 'setting'.</p>
363      *
364      * <p>This attribute represents the name of the setting to retrieve
365      * from the ivata groupware system.</p>
366      *
367      * @param sSetting the value supplied to the attribute 'setting'
368      */

369     public final void setSetting(final String JavaDoc setting) {
370         this.setting = setting;
371     }
372
373     /**
374      * <p>Class of the setting to be retrieved - this must match the setting
375      * type.</p>
376      *
377      * @param string class of the setting to be retrieved.
378      */

379     public final void setType(final String JavaDoc string) {
380         type = string;
381     }
382
383 }
384
Popular Tags