KickJava   Java API By Example, From Geeks To Geeks.

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


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.blog.Blog;
36 import org.blojsom.blog.Entry;
37 import org.blojsom.fetcher.Fetcher;
38 import org.blojsom.fetcher.FetcherException;
39 import org.blojsom.plugin.PluginException;
40 import org.blojsom.plugin.comment.CommentModerationPlugin;
41 import org.blojsom.plugin.comment.CommentPlugin;
42 import org.blojsom.plugin.pingback.PingbackPlugin;
43 import org.blojsom.plugin.trackback.TrackbackModerationPlugin;
44 import org.blojsom.plugin.trackback.TrackbackPlugin;
45 import org.blojsom.plugin.weblogsping.WeblogsPingPlugin;
46 import org.blojsom.util.BlojsomConstants;
47 import org.blojsom.util.BlojsomUtils;
48
49 import javax.servlet.http.HttpServletRequest JavaDoc;
50 import javax.servlet.http.HttpServletResponse JavaDoc;
51 import java.util.Map JavaDoc;
52 import java.util.Properties JavaDoc;
53
54 /**
55  * EditBlogPropertiesPlugin
56  *
57  * @author David Czarnecki
58  * @version $Id: EditBlogPropertiesPlugin.java,v 1.9 2006/09/12 14:52:41 czarneckid Exp $
59  * @since blojsom 3.0
60  */

61 public class EditBlogPropertiesPlugin extends BaseAdminPlugin {
62
63     private static Log _logger = LogFactory.getLog(EditBlogPropertiesPlugin.class);
64
65     private static final String JavaDoc EDIT_BLOG_PROPERTIES_PAGE = "/org/blojsom/plugin/admin/templates/admin-edit-blog-properties";
66
67     // Localization constants
68
private static final String JavaDoc FAILED_EDIT_PROPERTIES_PERMISSION_KEY = "failed.edit.properties.permission.text";
69     private static final String JavaDoc UPDATED_BLOG_PROPERTIES_KEY = "updated.blog.properties.text";
70     private static final String JavaDoc FAILED_SAVE_BLOG_PROPERTIES_KEY = "failed.save.blog.properties.text";
71     private static final String JavaDoc BLOG_PROPERTY_HAS_VALUE_KEY = "blog.property.has.value.text";
72     private static final String JavaDoc BLOG_PROPERTY_NOT_FOUND_KEY = "blog.property.not.found.text";
73
74     // Actions
75
private static final String JavaDoc EDIT_BLOG_PROPERTIES_ACTION = "edit-blog-properties";
76     private static final String JavaDoc CHECK_BLOG_PROPERTY_ACTION = "check-blog-property";
77     private static final String JavaDoc SET_BLOG_PROPERTY_ACTION = "set-blog-property";
78
79     private static final String JavaDoc BLOJSOM_INSTALLED_LOCALES = "BLOJSOM_INSTALLED_LOCALES";
80     private static final String JavaDoc BLOJSOM_JVM_LANGUAGES = "BLOJSOM_JVM_LANGUAGES";
81     private static final String JavaDoc BLOJSOM_JVM_COUNTRIES = "BLOJSOM_JVM_COUNTRIES";
82     private static final String JavaDoc BLOJSOM_JVM_TIMEZONES = "BLOJSOM_JVM_TIMEZONES";
83
84     // Permissions
85
private static final String JavaDoc EDIT_BLOG_PROPERTIES_PERMISSION = "edit_blog_properties_permission";
86     private static final String JavaDoc SET_ARBITRARY_PROPERTIES_PERMISSION = "set_arbitrary_properties_permission";
87     private static final String JavaDoc CHECK_ARBITRARY_PROPERTIES_PERMISSION = "check_arbitrary_properties_permission";
88
89     // Form items
90
private static final String JavaDoc INDIVIDUAL_BLOG_PROPERTY = "individual-blog-property";
91     private static final String JavaDoc INDIVIDUAL_BLOG_PROPERTY_VALUE = "individual-blog-property-value";
92
93     private Properties JavaDoc _blojsomProperties;
94     private Fetcher _fetcher;
95
96     /**
97      * Default constructor.
98      */

99     public EditBlogPropertiesPlugin() {
100     }
101
102     /**
103      * Set the {@link Fetcher}
104      *
105      * @param fetcher {@link Fetcher}
106      */

107     public void setFetcher(Fetcher fetcher) {
108         _fetcher = fetcher;
109     }
110
111     /**
112      * Set the default properties
113      *
114      * @param properties Default properties
115      */

116     public void setBlojsomProperties(Properties JavaDoc blojsomProperties) {
117         _blojsomProperties = blojsomProperties;
118     }
119
120     /**
121      * Process the blog entries
122      *
123      * @param httpServletRequest Request
124      * @param httpServletResponse Response
125      * @param blog {@link Blog} instance
126      * @param context Context
127      * @param entries Blog entries retrieved for the particular request
128      * @return Modified set of blog entries
129      * @throws PluginException If there is an error processing the blog entries
130      */

131     public Entry[] process(HttpServletRequest JavaDoc httpServletRequest, HttpServletResponse JavaDoc httpServletResponse, Blog blog, Map JavaDoc context, Entry[] entries) throws PluginException {
132         if (!authenticateUser(httpServletRequest, httpServletResponse, context, blog)) {
133             httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, ADMIN_LOGIN_PAGE);
134
135             return entries;
136         }
137
138         String JavaDoc username = getUsernameFromSession(httpServletRequest, blog);
139         if (!checkPermission(blog, null, username, EDIT_BLOG_PROPERTIES_PERMISSION)) {
140             httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, ADMIN_ADMINISTRATION_PAGE);
141             addOperationResultMessage(context, getAdminResource(FAILED_EDIT_PROPERTIES_PERMISSION_KEY, FAILED_EDIT_PROPERTIES_PERMISSION_KEY, blog.getBlogAdministrationLocale()));
142
143             return entries;
144         }
145
146         String JavaDoc action = BlojsomUtils.getRequestValue(ACTION_PARAM, httpServletRequest);
147         if (BlojsomUtils.checkNullOrBlank(action)) {
148             _logger.debug("User did not request edit action");
149             httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, ADMIN_ADMINISTRATION_PAGE);
150         } else if (PAGE_ACTION.equals(action)) {
151             _logger.debug("User requested edit page");
152             httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_PROPERTIES_PAGE);
153         } else if (EDIT_BLOG_PROPERTIES_ACTION.equals(action)) {
154             _logger.debug("User requested edit action");
155
156             String JavaDoc blogPropertyValue = BlojsomUtils.getRequestValue(BlojsomConstants.BLOG_NAME_IP, httpServletRequest);
157             blog.setBlogName(blogPropertyValue);
158             blogPropertyValue = BlojsomUtils.getRequestValue(BlojsomConstants.BLOG_DESCRIPTION_IP, httpServletRequest);
159             blog.setBlogDescription(blogPropertyValue);
160             blogPropertyValue = BlojsomUtils.getRequestValue(BlojsomConstants.BLOG_COUNTRY_IP, httpServletRequest);
161             blog.setBlogCountry(blogPropertyValue);
162             blogPropertyValue = BlojsomUtils.getRequestValue(BlojsomConstants.BLOG_LANGUAGE_IP, httpServletRequest);
163             blog.setBlogLanguage(blogPropertyValue);
164             blogPropertyValue = BlojsomUtils.getRequestValue(BlojsomConstants.BLOG_ADMINISTRATION_LOCALE_IP, httpServletRequest);
165             blog.setBlogAdministrationLocale(blogPropertyValue);
166             blogPropertyValue = BlojsomUtils.getRequestValue("blog-timezone-id", httpServletRequest);
167             if (BlojsomUtils.checkNullOrBlank(blogPropertyValue)) {
168                 blogPropertyValue = BlojsomConstants.BLOG_DEFAULT_TIMEZONE;
169             }
170             blog.setProperty("blog-timezone-id", blogPropertyValue);
171             blogPropertyValue = BlojsomUtils.getRequestValue("blog-display-entries", httpServletRequest);
172             try {
173                 int blogDisplayEntries = Integer.parseInt(blogPropertyValue);
174                 blog.setBlogDisplayEntries(blogDisplayEntries);
175             } catch (NumberFormatException JavaDoc e) {
176                 _logger.error("Blog display entries parameter invalid.", e);
177             }
178             blogPropertyValue = BlojsomUtils.getRequestValue(BlojsomConstants.BLOG_OWNER, httpServletRequest);
179             blog.setBlogOwner(blogPropertyValue);
180             blogPropertyValue = BlojsomUtils.getRequestValue(BlojsomConstants.BLOG_OWNER_EMAIL, httpServletRequest);
181             blog.setBlogOwnerEmail(blogPropertyValue);
182             blogPropertyValue = BlojsomUtils.getRequestValue(BlojsomConstants.BLOG_COMMENTS_ENABLED_IP, httpServletRequest);
183             blog.setBlogCommentsEnabled(Boolean.valueOf(blogPropertyValue));
184             blogPropertyValue = BlojsomUtils.getRequestValue(BlojsomConstants.BLOG_TRACKBACKS_ENABLED_IP, httpServletRequest);
185             blog.setBlogTrackbacksEnabled(Boolean.valueOf(blogPropertyValue));
186             blogPropertyValue = BlojsomUtils.getRequestValue(BlojsomConstants.BLOG_EMAIL_ENABLED_IP, httpServletRequest);
187             blog.setBlogEmailEnabled(Boolean.valueOf(blogPropertyValue));
188             blogPropertyValue = BlojsomUtils.getRequestValue(BlojsomConstants.BLOG_DEFAULT_FLAVOR_IP, httpServletRequest);
189             blog.setBlogDefaultFlavor(blogPropertyValue);
190             blogPropertyValue = BlojsomUtils.getRequestValue(BlojsomConstants.LINEAR_NAVIGATION_ENABLED_IP, httpServletRequest);
191             blog.setLinearNavigationEnabled(Boolean.valueOf(blogPropertyValue));
192             blogPropertyValue = BlojsomUtils.getRequestValue(BlojsomConstants.BLOG_URL_IP, httpServletRequest);
193             blog.setBlogURL(blogPropertyValue);
194             blogPropertyValue = BlojsomUtils.getRequestValue(BlojsomConstants.BLOG_BASE_URL_IP, httpServletRequest);
195             blog.setBlogBaseURL(blogPropertyValue);
196             blogPropertyValue = BlojsomUtils.getRequestValue(BlojsomConstants.DEFAULT_POST_CATEGORY, httpServletRequest);
197             blog.setProperty(BlojsomConstants.DEFAULT_POST_CATEGORY, blogPropertyValue);
198
199             // Comment plugin properties
200
blogPropertyValue = BlojsomUtils.getRequestValue(CommentPlugin.COMMENT_AUTOFORMAT_IP, httpServletRequest);
201             blog.setProperty(CommentPlugin.COMMENT_AUTOFORMAT_IP, blogPropertyValue);
202             blogPropertyValue = BlojsomUtils.getRequestValue(CommentPlugin.COMMENT_PREFIX_IP, httpServletRequest);
203             blog.setProperty(CommentPlugin.COMMENT_PREFIX_IP, blogPropertyValue);
204             blogPropertyValue = BlojsomUtils.getRequestValue(CommentPlugin.COMMENT_COOKIE_EXPIRATION_DURATION_IP, httpServletRequest);
205             blog.setProperty(CommentPlugin.COMMENT_COOKIE_EXPIRATION_DURATION_IP, blogPropertyValue);
206             blogPropertyValue = BlojsomUtils.getRequestValue(CommentPlugin.COMMENT_THROTTLE_MINUTES_IP, httpServletRequest);
207             blog.setProperty(CommentPlugin.COMMENT_THROTTLE_MINUTES_IP, blogPropertyValue);
208             blogPropertyValue = BlojsomUtils.getRequestValue(CommentPlugin.COMMENT_DAYS_EXPIRATION_IP, httpServletRequest);
209             blog.setProperty(CommentPlugin.COMMENT_DAYS_EXPIRATION_IP, blogPropertyValue);
210             blogPropertyValue = BlojsomUtils.getRequestValue(CommentModerationPlugin.COMMENT_MODERATION_ENABLED, httpServletRequest);
211             blog.setProperty(CommentModerationPlugin.COMMENT_MODERATION_ENABLED, blogPropertyValue);
212
213             // Trackback plugin properties
214
blogPropertyValue = BlojsomUtils.getRequestValue(TrackbackPlugin.TRACKBACK_THROTTLE_MINUTES_IP, httpServletRequest);
215             blog.setProperty(TrackbackPlugin.TRACKBACK_THROTTLE_MINUTES_IP, blogPropertyValue);
216             blogPropertyValue = BlojsomUtils.getRequestValue(TrackbackPlugin.TRACKBACK_PREFIX_IP, httpServletRequest);
217             blog.setProperty(TrackbackPlugin.TRACKBACK_PREFIX_IP, blogPropertyValue);
218             blogPropertyValue = BlojsomUtils.getRequestValue(TrackbackPlugin.TRACKBACK_DAYS_EXPIRATION_IP, httpServletRequest);
219             blog.setProperty(TrackbackPlugin.TRACKBACK_DAYS_EXPIRATION_IP, blogPropertyValue);
220             blogPropertyValue = BlojsomUtils.getRequestValue(TrackbackModerationPlugin.TRACKBACK_MODERATION_ENABLED, httpServletRequest);
221             blog.setProperty(TrackbackModerationPlugin.TRACKBACK_MODERATION_ENABLED, blogPropertyValue);
222
223             // Pingback properties
224
blogPropertyValue = BlojsomUtils.getRequestValue(BlojsomConstants.BLOG_PINGBACKS_ENABLED_IP, httpServletRequest);
225             blog.setBlogPingbacksEnabled(Boolean.valueOf(blogPropertyValue));
226             blogPropertyValue = BlojsomUtils.getRequestValue(PingbackPlugin.PINGBACK_PREFIX_IP, httpServletRequest);
227             blog.setProperty(PingbackPlugin.PINGBACK_PREFIX_IP, blogPropertyValue);
228             blogPropertyValue = BlojsomUtils.getRequestValue(PingbackPlugin.PINGBACK_MODERATION_ENABLED, httpServletRequest);
229             blog.setProperty(PingbackPlugin.PINGBACK_MODERATION_ENABLED, blogPropertyValue);
230
231             // Weblogs Ping plugin properties
232
blogPropertyValue = BlojsomUtils.getRequestValue(WeblogsPingPlugin.BLOG_PING_URLS_IP, httpServletRequest);
233             String JavaDoc[] pingURLs = BlojsomUtils.parseDelimitedList(blogPropertyValue, BlojsomUtils.WHITESPACE);
234             if (pingURLs != null && pingURLs.length > 0) {
235                 blog.setProperty(WeblogsPingPlugin.BLOG_PING_URLS_IP, BlojsomUtils.arrayOfStringsToString(pingURLs, " "));
236             } else {
237                 blog.setProperty(WeblogsPingPlugin.BLOG_PING_URLS_IP, "");
238             }
239
240             // XML-RPC settings
241
blogPropertyValue = BlojsomUtils.getRequestValue(BlojsomConstants.XMLRPC_ENABLED_IP, httpServletRequest);
242             blog.setXmlrpcEnabled(Boolean.valueOf(blogPropertyValue));
243
244             try {
245                 _fetcher.saveBlog(blog);
246
247                 addOperationResultMessage(context, getAdminResource(UPDATED_BLOG_PROPERTIES_KEY, UPDATED_BLOG_PROPERTIES_KEY, blog.getBlogAdministrationLocale()));
248             } catch (FetcherException e) {
249                 if (_logger.isErrorEnabled()) {
250                     _logger.error(e);
251                 }
252
253                 addOperationResultMessage(context, getAdminResource(FAILED_SAVE_BLOG_PROPERTIES_KEY, FAILED_SAVE_BLOG_PROPERTIES_KEY, blog.getBlogAdministrationLocale()));
254             }
255
256             // Request that we go back to the edit blog properties page
257
httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_PROPERTIES_PAGE);
258         } else if (SET_BLOG_PROPERTY_ACTION.equals(action)) {
259             _logger.debug("User requested set blog property action");
260
261             if (!checkPermission(blog, null, username, SET_ARBITRARY_PROPERTIES_PERMISSION)) {
262                 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, ADMIN_ADMINISTRATION_PAGE);
263                 addOperationResultMessage(context, getAdminResource(FAILED_EDIT_PROPERTIES_PERMISSION_KEY, FAILED_EDIT_PROPERTIES_PERMISSION_KEY, blog.getBlogAdministrationLocale()));
264
265                 return entries;
266             }
267
268             String JavaDoc blogProperty = BlojsomUtils.getRequestValue(INDIVIDUAL_BLOG_PROPERTY, httpServletRequest);
269             if (!BlojsomUtils.checkNullOrBlank(blogProperty)) {
270                 String JavaDoc blogPropertyValue = BlojsomUtils.getRequestValue(INDIVIDUAL_BLOG_PROPERTY_VALUE, httpServletRequest);
271                 if (blogPropertyValue == null) {
272                     blogPropertyValue = "";
273                 }
274
275                 blog.setProperty(blogProperty, blogPropertyValue);
276
277                 try {
278                     _fetcher.saveBlog(blog);
279
280                     addOperationResultMessage(context, getAdminResource(UPDATED_BLOG_PROPERTIES_KEY, UPDATED_BLOG_PROPERTIES_KEY, blog.getBlogAdministrationLocale()));
281                 } catch (FetcherException e) {
282                     if (_logger.isErrorEnabled()) {
283                         _logger.error(e);
284                     }
285
286                     addOperationResultMessage(context, getAdminResource(FAILED_SAVE_BLOG_PROPERTIES_KEY, FAILED_SAVE_BLOG_PROPERTIES_KEY, blog.getBlogAdministrationLocale()));
287                 }
288             }
289
290             // Request that we go back to the edit blog properties page
291
httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_PROPERTIES_PAGE);
292         } else if (CHECK_BLOG_PROPERTY_ACTION.equals(action)) {
293             _logger.debug("User requested check blog property action");
294
295             if (!checkPermission(blog, null, username, CHECK_ARBITRARY_PROPERTIES_PERMISSION)) {
296                 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, ADMIN_ADMINISTRATION_PAGE);
297                 addOperationResultMessage(context, getAdminResource(FAILED_EDIT_PROPERTIES_PERMISSION_KEY, FAILED_EDIT_PROPERTIES_PERMISSION_KEY, blog.getBlogAdministrationLocale()));
298
299                 return entries;
300             }
301
302             String JavaDoc blogProperty = BlojsomUtils.getRequestValue(INDIVIDUAL_BLOG_PROPERTY, httpServletRequest);
303
304             if (!BlojsomUtils.checkNullOrBlank(blogProperty)) {
305                 if (blog.getProperty(blogProperty) != null) {
306                     addOperationResultMessage(context, formatAdminResource(BLOG_PROPERTY_HAS_VALUE_KEY, BLOG_PROPERTY_HAS_VALUE_KEY, blog.getBlogAdministrationLocale(), new Object JavaDoc[] {blogProperty, blog.getProperty(blogProperty)}));
307                 } else {
308                     addOperationResultMessage(context, formatAdminResource(BLOG_PROPERTY_NOT_FOUND_KEY, BLOG_PROPERTY_NOT_FOUND_KEY, blog.getBlogAdministrationLocale(), new Object JavaDoc[] {blogProperty}));
309                 }
310             }
311
312             // Request that we go back to the edit blog properties page
313
httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_PROPERTIES_PAGE);
314         }
315
316         String JavaDoc installedLocales = _blojsomProperties.getProperty(BlojsomConstants.INSTALLED_LOCALES_IP);
317         if (installedLocales != null) {
318             context.put(BLOJSOM_INSTALLED_LOCALES, BlojsomUtils.parseCommaList(installedLocales));
319         }
320         context.put(BLOJSOM_JVM_LANGUAGES, BlojsomUtils.getLanguagesForSystem(blog.getBlogAdministrationLocale()));
321         context.put(BLOJSOM_JVM_COUNTRIES, BlojsomUtils.getCountriesForSystem(blog.getBlogAdministrationLocale()));
322         context.put(BLOJSOM_JVM_TIMEZONES, BlojsomUtils.getTimeZonesForSystem(blog.getBlogAdministrationLocale()));
323
324         return entries;
325     }
326 }
327
Popular Tags