1 /**
2 * Copyright 2005-2006 the original author or authors.
3 *
4 * Licensed under the Gnu General Pubic License, Version 2.0 (the
5 * "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/gpl-license.php
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the Gnu General Public License for more details.
14 */
15 package org.figure8.join.control.taglib;
16
17 import org.figure8.join.core.ContainerContextHandler;
18 import org.figure8.join.services.cache.Cache;
19 import org.figure8.join.services.cache.CacheKeys;
20 import org.figure8.join.services.cache.EternalCacheAccessor;
21 import org.figure8.join.businessfacades.security.UserManager;
22 import org.figure8.join.businessobjects.security.User;
23
24 import org.apache.commons.beanutils.PropertyUtils;
25
26 import javax.servlet.jsp.JspWriter;
27 import javax.servlet.jsp.PageContext;
28 import javax.servlet.jsp.JspException;
29 import javax.servlet.jsp.JspTagException;
30 import javax.servlet.http.HttpServletRequest;
31 /**
32 * Retrieve a user's summary from the users cache using its identifier.<br/>
33 * This tag operates in one of two major modes, depending on whether or not the
34 * <code>userId</code> attribute is specified. If it is included, this attribute is
35 * used as the identifier for retrieving a user's summary. If it is not included,
36 * <code>name</code> and <code>property</code> attributes must be provided.<br/>
37 * <code>name</code> must be the name of the JSP bean (in page scope) containing
38 * the user's identifier. <code>property</code> must be the propery of this bean
39 * used to access identifier.
40 * <br/>
41 * The additional <code>link</code> boolean attribute can be set in order to display
42 * (or not) a link to the page displaying user's details. Default is true.
43 * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
44 * @version $Revision: 1.2 $
45 */
46 public class ResolveUserTag extends ContextTag{
47
48
49
50 /** User to resolve id */
51 private String userId;
52
53 /** Name of the bean to use (in some scope) */
54 private String name;
55 /** Property to call on bean in order to get user's id */
56 private String property;
57
58 /** Flag telling if user's details page should be linked. */
59 private boolean link = true;
60
61
62
63
64 /** @return The user to resolve identifier */
65 public String getUserId(){
66 return userId;
67 }
68 /** @param userId The user to resolve identifier */
69 public void setUserId(String userId){
70 this.userId = userId;
71 }
72
73 /** @return The name of the bean containing userId */
74 public String getName(){
75 return name;
76 }
77 /** @param name The name of the bean containing userId */
78 public void setName(String name){
79 this.name = name;
80 }
81 /** @return The property to apply on bean <b>name</b> */
82 public String getProperty(){
83 return property;
84 }
85 /** @param property The property to apply on bean <b>name</b> */
86 public void setProperty(String property){
87 this.property = property;
88 }
89
90 /** @return */
91 public boolean getLink(){
92 return link;
93 }
94 /** @param link */
95 public void setLink(boolean link){
96 this.link = link;
97 }
98
99
100
101
102 /**
103 * Get the user's summary from Users Cache and write it on output using
104 * the page JspWriter. Do not throw exception, if error occurs cause not
105 * having the user's summary is not lethal ...
106 */
107 public int doStartTag() throws JspException{
108 try{
109
110 if (userId == null){
111
112 Object bean = findBeanInScope(name, "page");
113 if (bean == null)
114 throw new JspTagException("Unable to find bean '" + name + "' into page scope !");
115
116 if (property == null)
117 userId = bean.toString();
118 else{
119 Object result = PropertyUtils.getProperty(bean, property);
120 if (result != null)
121 userId = result.toString();
122 }
123 }
124
125 User user = null;
126 Cache userCache = getUserIdToUserCache();
127 if (userCache != null)
128 user = (User)userCache.get(userId);
129
130 if (user == null){
131 ContainerContextHandler ctxHandler = ContainerContextHandler.getInstance();
132 UserManager manager = (UserManager)ctxHandler.getComponent("userManager");
133 user = manager.getUser(userId);
134 if (user != null && userCache != null)
135 userCache.put(user.getLogin(), user);
136 }
137
138
139 JspWriter writer = pageContext.getOut();
140 if (link){
141 HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
142 StringBuffer buffer = new StringBuffer("<a href='");
143 buffer.append(request.getContextPath());
144 buffer.append("/action/user?op=details&login=");
145 buffer.append(userId).append("'>");
146 writer.write(buffer.toString());
147 }
148
149 if (user == null) writer.write(userId);
150 else writer.write(user.getLastname() + " " + user.getFirstname());
151
152 if (link)
153 writer.write("</a>");
154 }
155 catch (Exception e){
156 System.err.println("Exception in doStartTag() handling ResolveUserTag: " + e);
157 }
158
159 return (SKIP_BODY);
160 }
161
162 /** Release any acquired resources. */
163 public int doEndTag() throws JspException{
164 release();
165 return (SKIP_BODY);
166 }
167
168 /** Release any acquired resource. */
169 public void release(){
170 super.release();
171 userId = null;
172 name = null;
173 property = null;
174 link = true;
175 }
176
177
178
179
180 /** Get the cache for users or null if it is not available */
181 private Cache getUserIdToUserCache(){
182
183 EternalCacheAccessor cacheAccessor = (EternalCacheAccessor)pageContext.getAttribute(
184 EternalCacheAccessor.CONTEXT_KEY, PageContext.APPLICATION_SCOPE);
185
186 if (cacheAccessor != null){
187 return cacheAccessor.getCache(CacheKeys.USER_ID_TO_USER_KEY);
188 }
189 return null;
190 }
191 }