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.macro.admin;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.blojsom.blog.Blog;
035: import org.blojsom.blog.Entry;
036: import org.blojsom.plugin.PluginException;
037: import org.blojsom.plugin.admin.WebAdminPlugin;
038: import org.blojsom.plugin.macro.MacroExpansionUtilities;
039: import org.blojsom.util.BlojsomConstants;
040: import org.blojsom.util.BlojsomUtils;
041: import org.blojsom.fetcher.Fetcher;
042: import org.blojsom.fetcher.FetcherException;
043:
044: import javax.servlet.http.HttpServletRequest;
045: import javax.servlet.http.HttpServletResponse;
046: import java.util.Map;
047: import java.util.HashMap;
048:
049: /**
050: * Macro Expansion Admin Plugin
051: *
052: * @author David Czarnecki
053: * @version $Id: MacroExpansionAdminPlugin.java,v 1.3 2007/01/17 02:35:11 czarneckid Exp $
054: * @since blojsom 3.0
055: */
056: public class MacroExpansionAdminPlugin extends WebAdminPlugin {
057:
058: private Log _logger = LogFactory
059: .getLog(MacroExpansionAdminPlugin.class);
060:
061: private static final String EDIT_MACRO_EXPANSION_SETTINGS_PAGE = "/org/blojsom/plugin/macro/admin/templates/admin-edit-macro-expansion-settings";
062:
063: private static final String BLOJSOM_PLUGIN_EDIT_MACRO_EXPANSION_MACROS = "BLOJSOM_PLUGIN_EDIT_MACRO_EXPANSION_MACROS";
064:
065: // Localization constants
066: private static final String FAILED_MACRO_ADMIN_PERMISSION_KEY = "failed.macro.admin.permission.text";
067: private static final String DELETED_MACROS_KEY = "deleted.macros.text";
068: private static final String NO_MACROS_SELECTED_TO_DELETE_KEY = "no.macros.selected.to.delete.text";
069: private static final String MISSING_MACRO_PARAMETERS_KEY = "missing.macro.parameters.text";
070: private static final String ADDED_MACRO_KEY = "added.macro.text";
071: private static final String MACRO_EXISTS_KEY = "macro.exists.text";
072:
073: // Form items
074: private static final String MACRO_SHORT_NAME = "macro-short-name";
075: private static final String MACRO_EXPANSION = "macro-expansion";
076: private static final String MACROS = "macros";
077:
078: // Actions
079: private static final String ADD_MACRO_ACTION = "add-macro";
080: private static final String DELETE_SELECTED_MACROS_ACTION = "delete-selected-macros";
081:
082: // Permissions
083: private static final String MACRO_EXPANSION_ADMIN_PERMISSION = "macro_expansion_admin_permission";
084:
085: private Fetcher _fetcher;
086:
087: /**
088: * Default constructor
089: */
090: public MacroExpansionAdminPlugin() {
091: }
092:
093: /**
094: * Return the display name for the plugin
095: *
096: * @return Display name for the plugin
097: */
098: public String getDisplayName() {
099: 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 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 httpServletRequest,
132: HttpServletResponse httpServletResponse, Blog blog,
133: Map context, Entry[] entries) throws PluginException {
134: entries = super .process(httpServletRequest,
135: httpServletResponse, blog, context, entries);
136: String page = BlojsomUtils.getRequestValue(
137: BlojsomConstants.PAGE_PARAM, httpServletRequest);
138:
139: String username = getUsernameFromSession(httpServletRequest,
140: blog);
141: if (!checkPermission(blog, null, username,
142: MACRO_EXPANSION_ADMIN_PERMISSION)) {
143: httpServletRequest.setAttribute(
144: BlojsomConstants.PAGE_PARAM,
145: ADMIN_ADMINISTRATION_PAGE);
146: addOperationResultMessage(context, getAdminResource(
147: FAILED_MACRO_ADMIN_PERMISSION_KEY,
148: FAILED_MACRO_ADMIN_PERMISSION_KEY, blog
149: .getBlogAdministrationLocale()));
150:
151: return entries;
152: }
153:
154: if (ADMIN_LOGIN_PAGE.equals(page)) {
155: return entries;
156: } else {
157: String action = BlojsomUtils.getRequestValue(ACTION_PARAM,
158: httpServletRequest);
159: Map macros = MacroExpansionUtilities.readMacros(blog);
160: Map updatedBlogProperties = new HashMap(blog
161: .getProperties());
162:
163: if (DELETE_SELECTED_MACROS_ACTION.equals(action)) {
164: String[] macrosToDelete = httpServletRequest
165: .getParameterValues(MACROS);
166: if (macrosToDelete != null && macrosToDelete.length > 0) {
167: for (int i = 0; i < macrosToDelete.length; i++) {
168: String macro = macrosToDelete[i];
169: macros.remove(macro);
170: updatedBlogProperties
171: .remove(MacroExpansionUtilities.PLUGIN_MACRO_PREFIX
172: + macro);
173: }
174:
175: blog.setProperties(updatedBlogProperties);
176:
177: try {
178: _fetcher.saveBlog(blog);
179: } catch (FetcherException e) {
180: if (_logger.isErrorEnabled()) {
181: if (_logger.isErrorEnabled()) {
182: _logger.error(e);
183: }
184: }
185: }
186:
187: addOperationResultMessage(context,
188: formatAdminResource(DELETED_MACROS_KEY,
189: DELETED_MACROS_KEY,
190: blog.getBlogAdministrationLocale(),
191: new Object[] { new Integer(
192: macrosToDelete.length) }));
193: } else {
194: addOperationResultMessage(context,
195: getAdminResource(
196: NO_MACROS_SELECTED_TO_DELETE_KEY,
197: NO_MACROS_SELECTED_TO_DELETE_KEY,
198: blog.getBlogAdministrationLocale()));
199: }
200: } else if (ADD_MACRO_ACTION.equals(action)) {
201: String macroShortName = BlojsomUtils.getRequestValue(
202: MACRO_SHORT_NAME, httpServletRequest);
203: String macroExpansion = BlojsomUtils.getRequestValue(
204: MACRO_EXPANSION, httpServletRequest);
205:
206: if (BlojsomUtils.checkNullOrBlank(macroShortName)
207: || BlojsomUtils
208: .checkNullOrBlank(macroExpansion)) {
209: addOperationResultMessage(context,
210: getAdminResource(
211: MISSING_MACRO_PARAMETERS_KEY,
212: MISSING_MACRO_PARAMETERS_KEY,
213: blog.getBlogAdministrationLocale()));
214: } else {
215: if (!macros.containsKey(macroShortName)) {
216: macros.put(macroShortName, macroExpansion);
217: updatedBlogProperties
218: .put(
219: MacroExpansionUtilities.PLUGIN_MACRO_PREFIX
220: + macroShortName,
221: macroExpansion);
222:
223: blog.setProperties(updatedBlogProperties);
224:
225: try {
226: _fetcher.saveBlog(blog);
227: } catch (FetcherException e) {
228: if (_logger.isErrorEnabled()) {
229: _logger.error(e);
230: }
231: }
232:
233: addOperationResultMessage(
234: context,
235: formatAdminResource(
236: ADDED_MACRO_KEY,
237: ADDED_MACRO_KEY,
238: blog
239: .getBlogAdministrationLocale(),
240: new Object[] { macroShortName }));
241: } else {
242: addOperationResultMessage(
243: context,
244: formatAdminResource(
245: MACRO_EXISTS_KEY,
246: MACRO_EXISTS_KEY,
247: blog
248: .getBlogAdministrationLocale(),
249: new Object[] { macroShortName }));
250: }
251: }
252: }
253:
254: context.put(BLOJSOM_PLUGIN_EDIT_MACRO_EXPANSION_MACROS,
255: macros);
256: }
257:
258: return entries;
259: }
260: }
|