001: /**
002: * Copyright (c) 2003-2007, David A. Czarnecki
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * Redistributions of source code must retain the above copyright notice, this list of conditions and the
009: * following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
011: * following disclaimer in the documentation and/or other materials provided with the distribution.
012: * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
013: * endorse or promote products derived from this software without specific prior written permission.
014: * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
015: * without prior written permission of David A. Czarnecki.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
018: * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
019: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
021: * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
022: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
025: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026: * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
027: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
028: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
029: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030: */package org.blojsom.plugin.admin;
031:
032: import org.apache.velocity.util.StringUtils;
033: import org.blojsom.blog.Blog;
034: import org.blojsom.blog.Entry;
035: import org.blojsom.blog.User;
036: import org.blojsom.fetcher.Fetcher;
037: import org.blojsom.plugin.PluginException;
038: import org.blojsom.util.BlojsomConstants;
039:
040: import javax.servlet.http.HttpServletRequest;
041: import javax.servlet.http.HttpServletResponse;
042: import java.util.Collections;
043: import java.util.Iterator;
044: import java.util.Map;
045: import java.util.TreeMap;
046:
047: /**
048: * * View custom properties for a Blog user. Useful for collecting various
049: * information from a user upon registration.
050: * </p>
051: *
052: * @author Eric Broyles
053: * @version $Id: ViewBlogUserPropertiesPlugin.java,v 1.2 2007/01/17 02:35:04 czarneckid Exp $
054: */
055: public class ViewBlogUserPropertiesPlugin extends BaseAdminPlugin {
056:
057: /**
058: * The suffix used to identify custom user properties.
059: */
060: protected static final String USER_PROPERTIES_SUFFIX = "_property";
061:
062: protected static final String BLOJSOM_PLUGIN_VIEW_USER_PROPERTIES_USER_MAP = "BLOJSOM_PLUGIN_VIEW_USER_PROPERTIES_USER_MAP";
063:
064: protected static final String VIEW_USER_PROPERTIES_PAGE = "/org/blojsom/plugin/admin/templates/admin-view-user-properties";
065:
066: protected static final String VIEW_USER_PROPERTIES_PERMISSION = "view_user_properties_permission";
067:
068: protected static final String FAILED_VIEW_PROPERTIES_KEY = "failed.view.user.properties.text";
069:
070: protected Fetcher _fetcher;
071:
072: /**
073: * Construct a new instance of the View blog user properties plugin
074: */
075: public ViewBlogUserPropertiesPlugin() {
076: }
077:
078: /**
079: * Set the {@link Fetcher}
080: *
081: * @param fetcher {@link Fetcher}
082: */
083: public void setFetcher(Fetcher fetcher) {
084: _fetcher = fetcher;
085: }
086:
087: /**
088: * Read the properties for the specified user
089: *
090: * @param user User
091: * @return Properties for the given user
092: */
093: protected Map readPropertiesForUser(User user) {
094: Map properties = new TreeMap();
095: Iterator keyIterator = user.getMetaData().keySet().iterator();
096:
097: while (keyIterator.hasNext()) {
098: String propertyKey = (String) keyIterator.next();
099: if (propertyKey.endsWith(USER_PROPERTIES_SUFFIX)) {
100: // Camel case the property key and remove underscores for readability in the display
101: properties.put(StringUtils.removeAndHump(propertyKey
102: .replaceAll("_", " _"), "_"), user
103: .getMetaData().get(propertyKey));
104: }
105: }
106: properties.put("Name", user.getUserName());
107: properties.put("Email", user.getUserEmail());
108: properties.put("Status", user.getUserStatus());
109: properties.put("Registered", user.getUserRegistered());
110:
111: return properties;
112: }
113:
114: /**
115: * Add the properties for the users in a blog to the context
116: *
117: * @param context Context
118: * @param blog {@link Blog}
119: */
120: protected void setupPropertiesInContext(Map context, Blog blog) {
121: User[] users = _fetcher.getUsers(blog);
122: TreeMap userIDs = new TreeMap();
123: for (int i = 0; i < users.length; i++) {
124: User userFromBlog = users[i];
125: Map propertiesForUser = readPropertiesForUser(userFromBlog);
126:
127: userIDs.put(userFromBlog.getUserLogin(), propertiesForUser);
128: }
129:
130: context.put(BLOJSOM_PLUGIN_VIEW_USER_PROPERTIES_USER_MAP,
131: Collections.unmodifiableMap(userIDs));
132: }
133:
134: /**
135: * Process the blog entries
136: *
137: * @param httpServletRequest Request
138: * @param httpServletResponse Response
139: * @param blog {@link Blog} instance
140: * @param context Context
141: * @param entries Blog entries retrieved for the particular request
142: * @return Modified set of blog entries
143: * @throws PluginException If there is an error processing the blog entries
144: */
145: public Entry[] process(HttpServletRequest httpServletRequest,
146: HttpServletResponse httpServletResponse, Blog blog,
147: Map context, Entry[] entries) throws PluginException {
148: if (!authenticateUser(httpServletRequest, httpServletResponse,
149: context, blog)) {
150: httpServletRequest.setAttribute(
151: BlojsomConstants.PAGE_PARAM, ADMIN_LOGIN_PAGE);
152:
153: return entries;
154: }
155:
156: String username = getUsernameFromSession(httpServletRequest,
157: blog);
158: if (!checkPermission(blog, null, username,
159: VIEW_USER_PROPERTIES_PERMISSION)) {
160: httpServletRequest.setAttribute(
161: BlojsomConstants.PAGE_PARAM,
162: ADMIN_ADMINISTRATION_PAGE);
163: addOperationResultMessage(context, getAdminResource(
164: FAILED_VIEW_PROPERTIES_KEY,
165: FAILED_VIEW_PROPERTIES_KEY, blog
166: .getBlogAdministrationLocale()));
167:
168: return entries;
169: }
170:
171: setupPropertiesInContext(context, blog);
172: httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM,
173: VIEW_USER_PROPERTIES_PAGE);
174:
175: return entries;
176: }
177: }
|