| 1 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 ; 45 import javax.servlet.http.HttpServletResponse ; 46 import java.util.Map ; 47 48 52 public class EditBlogPreferencesPlugin extends BaseAdminPlugin { 53 54 private static Log _logger = LogFactory.getLog(EditBlogPropertiesPlugin.class); 55 56 private static final String EDIT_BLOG_PREFERENCES_PAGE = "/org/blojsom/plugin/admin/templates/admin-edit-blog-preferences"; 57 58 private static final String FAILED_EDIT_PREFERENCES_PERMISSION_KEY = "failed.edit.preferences.permission.text"; 60 61 private static final String EDIT_BLOG_PREFERENCES_PERMISSION = "edit_blog_preferences_permission"; 63 64 private static final String EDIT_BLOG_PREFERENCES_ACTION = "edit-blog-preferences"; 66 67 private static final String BLOJSOM_USER_OBJECT = "BLOJSOM_USER_OBJECT"; 68 69 private Fetcher _fetcher; 70 71 74 public EditBlogPreferencesPlugin() { 75 } 76 77 82 public void setFetcher(Fetcher fetcher) { 83 _fetcher = fetcher; 84 } 85 86 97 public Entry[] process(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Blog blog, Map 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 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 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 try { 124 User user = _fetcher.loadUser(blog, username); 125 126 String 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 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 |