KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > blojsom > plugin > admin > EditBlogPreferencesPlugin


1 /**
2  * Copyright (c) 2003-2006, David A. Czarnecki
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and the
9  * following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
11  * following disclaimer in the documentation and/or other materials provided with the distribution.
12  * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
13  * endorse or promote products derived from this software without specific prior written permission.
14  * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
15  * without prior written permission of David A. Czarnecki.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
18  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
21  * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */

31 package org.blojsom.plugin.admin;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.blojsom.fetcher.Fetcher;
36 import org.blojsom.fetcher.FetcherException;
37 import org.blojsom.blog.Blog;
38 import org.blojsom.blog.Entry;
39 import org.blojsom.blog.User;
40 import org.blojsom.plugin.PluginException;
41 import org.blojsom.util.BlojsomConstants;
42 import org.blojsom.util.BlojsomUtils;
43
44 import javax.servlet.http.HttpServletRequest JavaDoc;
45 import javax.servlet.http.HttpServletResponse JavaDoc;
46 import java.util.Map JavaDoc;
47
48 /**
49  * @author David Czarnecki
50  * @version $Id: EditBlogPreferencesPlugin.java,v 1.2 2006/09/18 18:30:53 czarneckid Exp $
51  */

52 public class EditBlogPreferencesPlugin extends BaseAdminPlugin {
53
54     private static Log _logger = LogFactory.getLog(EditBlogPropertiesPlugin.class);
55
56     private static final String JavaDoc EDIT_BLOG_PREFERENCES_PAGE = "/org/blojsom/plugin/admin/templates/admin-edit-blog-preferences";
57
58     // Localization constants
59
private static final String JavaDoc FAILED_EDIT_PREFERENCES_PERMISSION_KEY = "failed.edit.preferences.permission.text";
60
61     // Permissions
62
private static final String JavaDoc EDIT_BLOG_PREFERENCES_PERMISSION = "edit_blog_preferences_permission";
63
64     // Actions
65
private static final String JavaDoc EDIT_BLOG_PREFERENCES_ACTION = "edit-blog-preferences";
66
67     private static final String JavaDoc BLOJSOM_USER_OBJECT = "BLOJSOM_USER_OBJECT";
68     
69     private Fetcher _fetcher;
70
71     /**
72      * Default constructor.
73      */

74     public EditBlogPreferencesPlugin() {
75     }
76
77     /**
78      * Set the {@link Fetcher}
79      *
80      * @param fetcher {@link Fetcher}
81      */

82     public void setFetcher(Fetcher fetcher) {
83         _fetcher = fetcher;
84     }
85
86     /**
87      * Process the blog entries
88      *
89      * @param httpServletRequest Request
90      * @param httpServletResponse Response
91      * @param blog {@link Blog} instance
92      * @param context Context
93      * @param entries Blog entries retrieved for the particular request
94      * @return Modified set of blog entries
95      * @throws PluginException If there is an error processing the blog entries
96      */

97     public Entry[] process(HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc httpServletResponse, Blog blog, Map JavaDoc context, Entry[] entries) throws PluginException {
98         if (!authenticateUser(httpServletRequest, httpServletResponse, context, blog)) {
99             httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, ADMIN_LOGIN_PAGE);
100
101             return entries;
102         }
103
104         String JavaDoc username = getUsernameFromSession(httpServletRequest, blog);
105         if (!checkPermission(blog, null, username, EDIT_BLOG_PREFERENCES_PERMISSION)) {
106             httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, ADMIN_ADMINISTRATION_PAGE);
107             addOperationResultMessage(context, getAdminResource(FAILED_EDIT_PREFERENCES_PERMISSION_KEY, FAILED_EDIT_PREFERENCES_PERMISSION_KEY, blog.getBlogAdministrationLocale()));
108
109             return entries;
110         }
111
112         String JavaDoc action = BlojsomUtils.getRequestValue(ACTION_PARAM, httpServletRequest);
113         if (BlojsomUtils.checkNullOrBlank(action)) {
114             _logger.debug("User did not request edit action");
115             httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, ADMIN_ADMINISTRATION_PAGE);
116         } else if (PAGE_ACTION.equals(action)) {
117             _logger.debug("User requested edit page");
118             httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_PREFERENCES_PAGE);
119         } else if (EDIT_BLOG_PREFERENCES_ACTION.equals(action)) {
120             _logger.debug("User requested edit action");
121
122             // User-specific settings
123
try {
124                 User user = _fetcher.loadUser(blog, username);
125
126                 String JavaDoc blogPropertyValue = BlojsomUtils.getRequestValue(BlojsomConstants.USE_RICHTEXT_EDITOR_PREFERENCE, httpServletRequest);
127                 user.getMetaData().put(BlojsomConstants.USE_RICHTEXT_EDITOR_PREFERENCE, blogPropertyValue);
128
129                 blogPropertyValue = BlojsomUtils.getRequestValue(BlojsomConstants.DISPLAY_RESPONSE_TEXT_PREFERENCE, httpServletRequest);
130                 user.getMetaData().put(BlojsomConstants.DISPLAY_RESPONSE_TEXT_PREFERENCE, blogPropertyValue);
131
132                 _fetcher.saveUser(blog, user);
133             } catch (FetcherException e) {
134                 if (_logger.isErrorEnabled()) {
135                     _logger.error(e);
136                 }
137             }
138
139             // Request that we go back to the edit blog properties page
140
httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_PREFERENCES_PAGE);
141         }
142
143         try {
144             context.put(BLOJSOM_USER_OBJECT, _fetcher.loadUser(blog, username));
145         } catch (FetcherException e) {
146             if (_logger.isErrorEnabled()) {
147                 _logger.error(e);
148             }
149         }
150
151         return entries;
152     }
153 }
154
Popular Tags