1 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 ; 46 import javax.servlet.http.HttpServletResponse ; 47 import java.util.Map ; 48 import java.util.HashMap ; 49 50 57 public class MacroExpansionAdminPlugin extends WebAdminPlugin { 58 59 private Log _logger = LogFactory.getLog(MacroExpansionAdminPlugin.class); 60 61 private static final String EDIT_MACRO_EXPANSION_SETTINGS_PAGE = "/org/blojsom/plugin/macro/admin/templates/admin-edit-macro-expansion-settings"; 62 63 private static final String BLOJSOM_PLUGIN_EDIT_MACRO_EXPANSION_MACROS = "BLOJSOM_PLUGIN_EDIT_MACRO_EXPANSION_MACROS"; 64 65 private static final String FAILED_MACRO_ADMIN_PERMISSION_KEY = "failed.macro.admin.permission.text"; 67 private static final String DELETED_MACROS_KEY = "deleted.macros.text"; 68 private static final String NO_MACROS_SELECTED_TO_DELETE_KEY = "no.macros.selected.to.delete.text"; 69 private static final String MISSING_MACRO_PARAMETERS_KEY = "missing.macro.parameters.text"; 70 private static final String ADDED_MACRO_KEY = "added.macro.text"; 71 private static final String MACRO_EXISTS_KEY = "macro.exists.text"; 72 73 private static final String MACRO_SHORT_NAME = "macro-short-name"; 75 private static final String MACRO_EXPANSION = "macro-expansion"; 76 private static final String MACROS = "macros"; 77 78 private static final String ADD_MACRO_ACTION = "add-macro"; 80 private static final String DELETE_SELECTED_MACROS_ACTION = "delete-selected-macros"; 81 82 private static final String MACRO_EXPANSION_ADMIN_PERMISSION = "macro_expansion_admin_permission"; 84 85 private Fetcher _fetcher; 86 87 90 public MacroExpansionAdminPlugin() { 91 } 92 93 98 public String getDisplayName() { 99 return "Macro Expansion plugin"; 100 } 101 102 107 public String getInitialPage() { 108 return EDIT_MACRO_EXPANSION_SETTINGS_PAGE; 109 } 110 111 116 public void setFetcher(Fetcher fetcher) { 117 _fetcher = fetcher; 118 } 119 120 131 public Entry[] process(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Blog blog, Map context, Entry[] entries) throws PluginException { 132 entries = super.process(httpServletRequest, httpServletResponse, blog, context, entries); 133 String page = BlojsomUtils.getRequestValue(BlojsomConstants.PAGE_PARAM, httpServletRequest); 134 135 String 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 action = BlojsomUtils.getRequestValue(ACTION_PARAM, httpServletRequest); 147 Map macros = MacroExpansionUtilities.readMacros(blog); 148 Map updatedBlogProperties = new HashMap (blog.getProperties()); 149 150 if (DELETE_SELECTED_MACROS_ACTION.equals(action)) { 151 String [] macrosToDelete = httpServletRequest.getParameterValues(MACROS); 152 if (macrosToDelete != null && macrosToDelete.length > 0) { 153 for (int i = 0; i < macrosToDelete.length; i++) { 154 String 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 []{new Integer (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 macroShortName = BlojsomUtils.getRequestValue(MACRO_SHORT_NAME, httpServletRequest); 177 String 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 []{macroShortName})); 197 } else { 198 addOperationResultMessage(context, formatAdminResource(MACRO_EXISTS_KEY, MACRO_EXISTS_KEY, blog.getBlogAdministrationLocale(), new Object []{macroShortName})); 199 } 200 } 201 } 202 203 context.put(BLOJSOM_PLUGIN_EDIT_MACRO_EXPANSION_MACROS, macros); 204 } 205 206 return entries; 207 } 208 } | Popular Tags |