KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > blojsom > plugin > macro > admin > MacroExpansionAdminPlugin


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.macro.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.plugin.PluginException;
38 import org.blojsom.plugin.admin.WebAdminPlugin;
39 import org.blojsom.plugin.macro.MacroExpansionUtilities;
40 import org.blojsom.util.BlojsomConstants;
41 import org.blojsom.util.BlojsomUtils;
42 import org.blojsom.fetcher.Fetcher;
43 import org.blojsom.fetcher.FetcherException;
44
45 import javax.servlet.http.HttpServletRequest JavaDoc;
46 import javax.servlet.http.HttpServletResponse JavaDoc;
47 import java.util.Map JavaDoc;
48 import java.util.HashMap JavaDoc;
49
50 /**
51  * Macro Expansion Admin Plugin
52  *
53  * @author David Czarnecki
54  * @version $Id: MacroExpansionAdminPlugin.java,v 1.2 2006/03/20 22:50:44 czarneckid Exp $
55  * @since blojsom 3.0
56  */

57 public class MacroExpansionAdminPlugin extends WebAdminPlugin {
58
59     private Log _logger = LogFactory.getLog(MacroExpansionAdminPlugin.class);
60
61     private static final String JavaDoc EDIT_MACRO_EXPANSION_SETTINGS_PAGE = "/org/blojsom/plugin/macro/admin/templates/admin-edit-macro-expansion-settings";
62
63     private static final String JavaDoc BLOJSOM_PLUGIN_EDIT_MACRO_EXPANSION_MACROS = "BLOJSOM_PLUGIN_EDIT_MACRO_EXPANSION_MACROS";
64
65     // Localization constants
66
private static final String JavaDoc FAILED_MACRO_ADMIN_PERMISSION_KEY = "failed.macro.admin.permission.text";
67     private static final String JavaDoc DELETED_MACROS_KEY = "deleted.macros.text";
68     private static final String JavaDoc NO_MACROS_SELECTED_TO_DELETE_KEY = "no.macros.selected.to.delete.text";
69     private static final String JavaDoc MISSING_MACRO_PARAMETERS_KEY = "missing.macro.parameters.text";
70     private static final String JavaDoc ADDED_MACRO_KEY = "added.macro.text";
71     private static final String JavaDoc MACRO_EXISTS_KEY = "macro.exists.text";
72
73     // Form items
74
private static final String JavaDoc MACRO_SHORT_NAME = "macro-short-name";
75     private static final String JavaDoc MACRO_EXPANSION = "macro-expansion";
76     private static final String JavaDoc MACROS = "macros";
77
78     // Actions
79
private static final String JavaDoc ADD_MACRO_ACTION = "add-macro";
80     private static final String JavaDoc DELETE_SELECTED_MACROS_ACTION = "delete-selected-macros";
81
82     // Permissions
83
private static final String JavaDoc MACRO_EXPANSION_ADMIN_PERMISSION = "macro_expansion_admin_permission";
84
85     private Fetcher _fetcher;
86
87     /**
88      * Default constructor
89      */

90     public MacroExpansionAdminPlugin() {
91     }
92
93     /**
94      * Return the display name for the plugin
95      *
96      * @return Display name for the plugin
97      */

98     public String JavaDoc getDisplayName() {
99         return "Macro Expansion plugin";
100     }
101
102     /**
103      * Return the name of the initial editing page for the plugin
104      *
105      * @return Name of the initial editing page for the plugin
106      */

107     public String JavaDoc getInitialPage() {
108         return EDIT_MACRO_EXPANSION_SETTINGS_PAGE;
109     }
110
111     /**
112      * Set the {@link Fetcher}
113      *
114      * @param fetcher {@link Fetcher}
115      */

116     public void setFetcher(Fetcher fetcher) {
117         _fetcher = fetcher;
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         entries = super.process(httpServletRequest, httpServletResponse, blog, context, entries);
133         String JavaDoc page = BlojsomUtils.getRequestValue(BlojsomConstants.PAGE_PARAM, httpServletRequest);
134
135         String JavaDoc username = getUsernameFromSession(httpServletRequest, blog);
136         if (!checkPermission(blog, null, username, MACRO_EXPANSION_ADMIN_PERMISSION)) {
137             httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, ADMIN_ADMINISTRATION_PAGE);
138             addOperationResultMessage(context, getAdminResource(FAILED_MACRO_ADMIN_PERMISSION_KEY, FAILED_MACRO_ADMIN_PERMISSION_KEY, blog.getBlogAdministrationLocale()));
139
140             return entries;
141         }
142
143         if (ADMIN_LOGIN_PAGE.equals(page)) {
144             return entries;
145         } else {
146             String JavaDoc action = BlojsomUtils.getRequestValue(ACTION_PARAM, httpServletRequest);
147             Map JavaDoc macros = MacroExpansionUtilities.readMacros(blog);
148             Map JavaDoc updatedBlogProperties = new HashMap JavaDoc(blog.getProperties());
149
150             if (DELETE_SELECTED_MACROS_ACTION.equals(action)) {
151                 String JavaDoc[] macrosToDelete = httpServletRequest.getParameterValues(MACROS);
152                 if (macrosToDelete != null && macrosToDelete.length > 0) {
153                     for (int i = 0; i < macrosToDelete.length; i++) {
154                         String JavaDoc macro = macrosToDelete[i];
155                         macros.remove(macro);
156                         updatedBlogProperties.remove(MacroExpansionUtilities.PLUGIN_MACRO_PREFIX + macro);
157                     }
158
159                     blog.setProperties(updatedBlogProperties);
160
161                     try {
162                         _fetcher.saveBlog(blog);
163                     } catch (FetcherException e) {
164                         if (_logger.isErrorEnabled()) {
165                             if (_logger.isErrorEnabled()) {
166                                 _logger.error(e);
167                             }
168                         }
169                     }
170
171                     addOperationResultMessage(context, formatAdminResource(DELETED_MACROS_KEY, DELETED_MACROS_KEY, blog.getBlogAdministrationLocale(), new Object JavaDoc[]{new Integer JavaDoc(macrosToDelete.length)}));
172                 } else {
173                     addOperationResultMessage(context, getAdminResource(NO_MACROS_SELECTED_TO_DELETE_KEY, NO_MACROS_SELECTED_TO_DELETE_KEY, blog.getBlogAdministrationLocale()));
174                 }
175             } else if (ADD_MACRO_ACTION.equals(action)) {
176                 String JavaDoc macroShortName = BlojsomUtils.getRequestValue(MACRO_SHORT_NAME, httpServletRequest);
177                 String JavaDoc macroExpansion = BlojsomUtils.getRequestValue(MACRO_EXPANSION, httpServletRequest);
178
179                 if (BlojsomUtils.checkNullOrBlank(macroShortName) || BlojsomUtils.checkNullOrBlank(macroExpansion)) {
180                     addOperationResultMessage(context, getAdminResource(MISSING_MACRO_PARAMETERS_KEY, MISSING_MACRO_PARAMETERS_KEY, blog.getBlogAdministrationLocale()));
181                 } else {
182                     if (!macros.containsKey(macroShortName)) {
183                         macros.put(macroShortName, macroExpansion);
184                         updatedBlogProperties.put(MacroExpansionUtilities.PLUGIN_MACRO_PREFIX + macroShortName, macroExpansion);
185
186                         blog.setProperties(updatedBlogProperties);
187
188                         try {
189                             _fetcher.saveBlog(blog);
190                         } catch (FetcherException e) {
191                             if (_logger.isErrorEnabled()) {
192                                 _logger.error(e);
193                             }
194                         }
195
196                         addOperationResultMessage(context, formatAdminResource(ADDED_MACRO_KEY, ADDED_MACRO_KEY, blog.getBlogAdministrationLocale(), new Object JavaDoc[]{macroShortName}));
197                     } else {
198                         addOperationResultMessage(context, formatAdminResource(MACRO_EXISTS_KEY, MACRO_EXISTS_KEY, blog.getBlogAdministrationLocale(), new Object JavaDoc[]{macroShortName}));
199                     }
200                 }
201             }
202
203             context.put(BLOJSOM_PLUGIN_EDIT_MACRO_EXPANSION_MACROS, macros);
204         }
205
206         return entries;
207     }
208 }
Popular Tags