KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > jsp > CmsJspTagUser


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/jsp/CmsJspTagUser.java,v $
3  * Date : $Date: 2006/03/27 14:52:19 $
4  * Version: $Revision: 1.22 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.jsp;
33
34 import org.opencms.file.CmsObject;
35 import org.opencms.file.CmsUser;
36 import org.opencms.flex.CmsFlexController;
37 import org.opencms.i18n.CmsMessageContainer;
38 import org.opencms.main.CmsLog;
39
40 import java.util.Arrays JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.List JavaDoc;
43
44 import javax.servlet.ServletRequest JavaDoc;
45 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
46
47 import org.apache.commons.logging.Log;
48
49 /**
50  * Provides access to the data of the currently logged in user.<p>
51  *
52  * @author Alexander Kandzior
53  *
54  * @version $Revision: 1.22 $
55  *
56  * @since 6.0.0
57  */

58 public class CmsJspTagUser extends TagSupport JavaDoc {
59
60     /** Serial version UID required for safe serialization. */
61     private static final long serialVersionUID = 4520173761363738542L;
62
63     /** The log object for this class. */
64     private static final Log LOG = CmsLog.getLog(CmsJspTagUser.class);
65
66     /** Static array of the possible user properties. */
67     private static final String JavaDoc[] USER_PROPERTIES = {
68         "name",
69         "firstname",
70         "lastname",
71         "email",
72         "street",
73         "zip",
74         "city",
75         "description",
76         "group",
77         "currentgroup",
78         "defaultgroup",
79         "otherstuff"};
80
81     /** Array list for fast lookup. */
82     private static final List JavaDoc USER_PROPERTIES_LIST = Arrays.asList(USER_PROPERTIES);
83
84     // internal member variables
85
private String JavaDoc m_property;
86
87     /**
88      * Internal action method.<p>
89      *
90      * @param property the selected user property
91      * @param req the current request
92      * @return String the value of the selected user property
93      */

94     public static String JavaDoc userTagAction(String JavaDoc property, ServletRequest JavaDoc req) {
95
96         CmsFlexController controller = CmsFlexController.getController(req);
97
98         CmsObject cms = controller.getCmsObject();
99         CmsUser user = cms.getRequestContext().currentUser();
100
101         if (property == null) {
102             property = USER_PROPERTIES[0];
103         }
104
105         String JavaDoc result = null;
106         switch (USER_PROPERTIES_LIST.indexOf(property)) {
107             case 0: // name
108
result = user.getName();
109                 break;
110             case 1: // firstname
111
result = user.getFirstname();
112                 break;
113             case 2: // lastname
114
result = user.getLastname();
115                 break;
116             case 3: // email
117
result = user.getEmail();
118                 break;
119             case 4: // street
120
result = user.getAddress();
121                 break;
122             case 5: // zip
123
result = user.getZipcode();
124                 break;
125             case 6: // city
126
result = user.getCity();
127                 break;
128             case 7: // description
129
result = user.getDescription();
130                 break;
131             case 8: // group
132
case 9: // currentgroup
133
case 10: // defaultgroup
134
result = "";
135                 break;
136             case 11: // otherstuff
137
Iterator JavaDoc it = user.getAdditionalInfo().keySet().iterator();
138                 CmsMessageContainer msgContainer = Messages.get().container(Messages.GUI_TAG_USER_ADDITIONALINFO_0);
139                 result = Messages.getLocalizedMessage(msgContainer, req);
140                 while (it.hasNext()) {
141                     Object JavaDoc o = it.next();
142                     result += " " + o + "=" + user.getAdditionalInfo((String JavaDoc)o);
143                 }
144                 break;
145             default:
146                 msgContainer = Messages.get().container(Messages.GUI_ERR_INVALID_USER_PROP_1, property);
147                 result = Messages.getLocalizedMessage(msgContainer, req);
148
149         }
150
151         return result;
152     }
153
154     /**
155      * @see javax.servlet.jsp.tagext.Tag#doStartTag()
156      */

157     public int doStartTag() throws javax.servlet.jsp.JspException JavaDoc {
158
159         javax.servlet.ServletRequest JavaDoc req = pageContext.getRequest();
160
161         // This will always be true if the page is called through OpenCms
162
if (CmsFlexController.isCmsRequest(req)) {
163
164             try {
165                 String JavaDoc result = userTagAction(m_property, req);
166                 // Return value of selected property
167
pageContext.getOut().print(result);
168             } catch (Exception JavaDoc ex) {
169                 if (LOG.isErrorEnabled()) {
170                     LOG.error(Messages.get().getBundle().key(Messages.ERR_PROCESS_TAG_1, "user"), ex);
171                 }
172                 throw new javax.servlet.jsp.JspException JavaDoc(ex);
173             }
174         }
175         return SKIP_BODY;
176     }
177
178     /**
179      * Returns the property name.<p>
180      *
181      * @return String the property name
182      */

183     public String JavaDoc getProperty() {
184
185         return m_property != null ? m_property : "";
186     }
187
188     /**
189      * @see javax.servlet.jsp.tagext.Tag#release()
190      */

191     public void release() {
192
193         super.release();
194         m_property = null;
195     }
196
197     /**
198      * Sets the property name.<p>
199      *
200      * @param name the property name
201      */

202     public void setProperty(String JavaDoc name) {
203
204         if (name != null) {
205             m_property = name.toLowerCase();
206         }
207     }
208
209 }
210
Popular Tags