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.blog.Blog; 36 import org.blojsom.blog.Category; 37 import org.blojsom.blog.Entry; 38 import org.blojsom.event.EventBroadcaster; 39 import org.blojsom.fetcher.Fetcher; 40 import org.blojsom.fetcher.FetcherException; 41 import org.blojsom.plugin.PluginException; 42 import org.blojsom.plugin.admin.event.CategoryAddedEvent; 43 import org.blojsom.plugin.admin.event.CategoryDeletedEvent; 44 import org.blojsom.plugin.admin.event.CategoryUpdatedEvent; 45 import org.blojsom.util.BlojsomUtils; 46 import org.blojsom.util.BlojsomConstants; 47 48 import javax.servlet.http.HttpServletRequest ; 49 import javax.servlet.http.HttpServletResponse ; 50 import java.io.BufferedReader ; 51 import java.io.IOException ; 52 import java.io.StringReader ; 53 import java.util.Date ; 54 import java.util.HashMap ; 55 import java.util.Iterator ; 56 import java.util.Map ; 57 58 65 public class EditBlogCategoriesPlugin extends BaseAdminPlugin { 66 67 private static Log _logger = LogFactory.getLog(EditBlogCategoriesPlugin.class); 68 69 private static final String FAILED_PERMISSION_KEY = "failed.permission.text"; 71 private static final String DELETED_CATEGORY_KEY = "deleted.category.text"; 72 private static final String FAILED_DELETED_CATEGORY_KEY = "failed.deleted.category.text"; 73 private static final String FAILED_LOAD_CATEGORY_KEY = "failed.load.category.text"; 74 private static final String NO_CATEGORY_SPECIFIED_KEY = "no.category.specified.text"; 75 private static final String FAILED_CATEGORY_METADATA_READ_KEY = "failed.category.metadata.read.text"; 76 private static final String CATEGORY_ADD_SUCCESS_KEY = "category.add.success.text"; 77 private static final String CATEGORY_UPDATE_SUCCESS_KEY = "category.update.success.text"; 78 private static final String CATEGORY_CHANGE_FAILED_KEY = "category.change.failed.text"; 79 80 private static final String EDIT_BLOG_CATEGORIES_PAGE = "/org/blojsom/plugin/admin/templates/admin-edit-blog-categories"; 82 private static final String EDIT_BLOG_CATEGORY_PAGE = "/org/blojsom/plugin/admin/templates/admin-edit-blog-category"; 83 84 private static final String BLOJSOM_PLUGIN_EDIT_BLOG_CATEGORIES_CATEGORY = "BLOJSOM_PLUGIN_EDIT_BLOG_CATEGORIES_CATEGORY"; 86 private static final String BLOJSOM_PLUGIN_EDIT_BLOG_CATEGORIES_CATEGORY_METADATA = "BLOJSOM_PLUGIN_EDIT_BLOG_CATEGORIES_CATEGORY_METADATA"; 87 private static final String BLOJSOM_PLUGIN_EDIT_BLOG_CATEGORIES_ALL_CATEGORIES = "BLOJSOM_PLUGIN_EDIT_BLOG_CATEGORIES_ALL_CATEGORIES"; 88 89 private static final String ADD_BLOG_CATEGORY_ACTION = "add-blog-category"; 91 private static final String DELETE_BLOG_CATEGORY_ACTION = "delete-blog-category"; 92 private static final String EDIT_BLOG_CATEGORY_ACTION = "edit-blog-category"; 93 private static final String UPDATE_BLOG_CATEGORY_ACTION = "update-blog-category"; 94 95 private static final String BLOG_CATEGORY_ID = "blog-category-id"; 97 private static final String BLOG_CATEGORY_PARENT_ID = "blog-category-parent-id"; 98 private static final String BLOG_CATEGORY_NAME = "blog-category-name"; 99 private static final String BLOG_CATEGORY_DESCRIPTION = "blog-category-description"; 100 private static final String BLOG_CATEGORY_META_DATA = "blog-category-meta-data"; 101 102 private static final String EDIT_BLOG_CATEGORIES_PERMISSION = "edit_blog_categories_permission"; 103 104 private Fetcher _fetcher; 105 private EventBroadcaster _eventBroadcaster; 106 107 110 public EditBlogCategoriesPlugin() { 111 } 112 113 118 public void setFetcher(Fetcher fetcher) { 119 _fetcher = fetcher; 120 } 121 122 127 public void setEventBroadcaster(EventBroadcaster eventBroadcaster) { 128 _eventBroadcaster = eventBroadcaster; 129 } 130 131 137 protected String getDisplayName(Category category) { 138 if (!BlojsomUtils.checkNullOrBlank(category.getDescription())) { 139 return category.getDescription(); 140 } 141 142 return category.getName(); 143 } 144 145 156 public Entry[] process(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Blog blog, Map context, Entry[] entries) throws PluginException { 157 if (!authenticateUser(httpServletRequest, httpServletResponse, context, blog)) { 158 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, ADMIN_LOGIN_PAGE); 159 160 return entries; 161 } 162 163 String action = BlojsomUtils.getRequestValue(ACTION_PARAM, httpServletRequest); 164 try { 165 Category[] allCategories = _fetcher.loadAllCategories(blog); 166 context.put(BLOJSOM_PLUGIN_EDIT_BLOG_CATEGORIES_ALL_CATEGORIES, allCategories); 167 } catch (FetcherException e) { 168 if (_logger.isErrorEnabled()) { 169 _logger.error(e); 170 } 171 } 172 173 String username = getUsernameFromSession(httpServletRequest, blog); 174 if (!checkPermission(blog, null, username, EDIT_BLOG_CATEGORIES_PERMISSION)) { 175 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, ADMIN_ADMINISTRATION_PAGE); 176 addOperationResultMessage(context, getAdminResource(FAILED_PERMISSION_KEY, FAILED_PERMISSION_KEY, blog.getBlogAdministrationLocale())); 177 178 return entries; 179 } 180 181 if (BlojsomUtils.checkNullOrBlank(action)) { 182 if (_logger.isDebugEnabled()) { 183 _logger.debug("User did not request edit action"); 184 } 185 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, ADMIN_ADMINISTRATION_PAGE); 186 } else if (PAGE_ACTION.equals(action)) { 187 if (_logger.isDebugEnabled()) { 188 _logger.debug("User requested edit categories page"); 189 } 190 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_CATEGORIES_PAGE); 191 } else if (DELETE_BLOG_CATEGORY_ACTION.equals(action)) { 192 if (_logger.isDebugEnabled()) { 193 _logger.debug("User request blog category delete action"); 194 } 195 String blogCategoryId = BlojsomUtils.getRequestValue(BLOG_CATEGORY_ID, httpServletRequest); 196 if (_logger.isDebugEnabled()) { 197 _logger.debug("Delting blog category: " + blogCategoryId); 198 } 199 200 try { 201 Integer categoryID = Integer.valueOf(blogCategoryId); 202 Category blogCategoryToDelete = _fetcher.newCategory(); 203 blogCategoryToDelete.setId(categoryID); 204 _fetcher.loadCategory(blog, blogCategoryToDelete); 205 _fetcher.deleteCategory(blog, blogCategoryToDelete); 206 207 if (_logger.isDebugEnabled()) { 208 _logger.debug("Deleted blog category: " + blogCategoryId); 209 } 210 addOperationResultMessage(context, formatAdminResource(DELETED_CATEGORY_KEY, DELETED_CATEGORY_KEY, blog.getBlogAdministrationLocale(), new Object []{getDisplayName(blogCategoryToDelete)})); 211 212 _eventBroadcaster.broadcastEvent(new CategoryDeletedEvent(this, new Date (), blogCategoryToDelete, blog)); 213 } catch (NumberFormatException e) { 214 addOperationResultMessage(context, formatAdminResource(FAILED_DELETED_CATEGORY_KEY, FAILED_DELETED_CATEGORY_KEY, blog.getBlogAdministrationLocale(), new Object []{blogCategoryId})); 215 } catch (FetcherException e) { 216 if (_logger.isErrorEnabled()) { 217 _logger.error(e); 218 } 219 220 addOperationResultMessage(context, formatAdminResource(FAILED_DELETED_CATEGORY_KEY, FAILED_DELETED_CATEGORY_KEY, blog.getBlogAdministrationLocale(), new Object []{blogCategoryId})); 221 } 222 223 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_CATEGORIES_PAGE); 224 } else if (EDIT_BLOG_CATEGORY_ACTION.equals(action)) { 225 String blogCategoryId = BlojsomUtils.getRequestValue(BLOG_CATEGORY_ID, httpServletRequest); 226 if (_logger.isDebugEnabled()) { 227 _logger.debug("Editing blog category: " + blogCategoryId); 228 } 229 Category blogCategoryToEdit; 230 231 try { 232 Integer categoryID = Integer.valueOf(blogCategoryId); 233 blogCategoryToEdit = _fetcher.newCategory(); 234 blogCategoryToEdit.setId(categoryID); 235 _fetcher.loadCategory(blog, blogCategoryToEdit); 236 237 context.put(BLOJSOM_PLUGIN_EDIT_BLOG_CATEGORIES_CATEGORY, blogCategoryToEdit); 238 239 StringBuffer categoryPropertiesString = new StringBuffer (); 240 Iterator keyIterator = blogCategoryToEdit.getMetaData().keySet().iterator(); 241 Object key; 242 while (keyIterator.hasNext()) { 243 key = keyIterator.next(); 244 categoryPropertiesString.append(key.toString()).append("=").append(blogCategoryToEdit.getMetaData().get(key)).append("\r\n"); 245 } 246 247 context.put(BLOJSOM_PLUGIN_EDIT_BLOG_CATEGORIES_CATEGORY_METADATA, categoryPropertiesString.toString()); 248 } catch (NumberFormatException e) { 249 addOperationResultMessage(context, formatAdminResource(FAILED_LOAD_CATEGORY_KEY, FAILED_LOAD_CATEGORY_KEY, blog.getBlogAdministrationLocale(), new Object []{blogCategoryId})); 250 } catch (FetcherException e) { 251 if (_logger.isErrorEnabled()) { 252 _logger.error(e); 253 } 254 255 addOperationResultMessage(context, formatAdminResource(FAILED_LOAD_CATEGORY_KEY, FAILED_LOAD_CATEGORY_KEY, blog.getBlogAdministrationLocale(), new Object []{blogCategoryId})); 256 } 257 258 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_CATEGORY_PAGE); 259 } else if (ADD_BLOG_CATEGORY_ACTION.equals(action) || UPDATE_BLOG_CATEGORY_ACTION.equals(action)) { 260 boolean isUpdatingCategory = UPDATE_BLOG_CATEGORY_ACTION.equals(action); 261 262 String blogCategoryName = BlojsomUtils.getRequestValue(BLOG_CATEGORY_NAME, httpServletRequest); 263 if (BlojsomUtils.checkNullOrBlank(blogCategoryName)) { 265 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_CATEGORIES_PAGE); 266 addOperationResultMessage(context, getAdminResource(NO_CATEGORY_SPECIFIED_KEY, NO_CATEGORY_SPECIFIED_KEY, blog.getBlogAdministrationLocale())); 267 268 return entries; 269 } 270 271 blogCategoryName = BlojsomUtils.normalize(blogCategoryName); 272 blogCategoryName = BlojsomUtils.addSlashes(blogCategoryName); 273 274 String blogCategoryId = BlojsomUtils.getRequestValue(BLOG_CATEGORY_ID, httpServletRequest); 275 String blogCategoryParentId = BlojsomUtils.getRequestValue(BLOG_CATEGORY_PARENT_ID, httpServletRequest); 276 String blogCategoryDescription = BlojsomUtils.getRequestValue(BLOG_CATEGORY_DESCRIPTION, httpServletRequest); 277 278 if (!isUpdatingCategory) { 279 if (_logger.isDebugEnabled()) { 280 _logger.debug("Adding blog category: " + blogCategoryName); 281 } 282 } else { 283 if (_logger.isDebugEnabled()) { 284 _logger.debug("Updating blog category: " + blogCategoryName); 285 } 286 } 287 288 String blogCategoryMetaData = BlojsomUtils.getRequestValue(BLOG_CATEGORY_META_DATA, httpServletRequest); 289 if (blogCategoryMetaData == null) { 290 blogCategoryMetaData = ""; 291 } 292 293 if (!isUpdatingCategory) { 294 if (_logger.isDebugEnabled()) { 295 _logger.debug("Adding blog category meta-data: " + blogCategoryMetaData); 296 } 297 } 298 299 BufferedReader br = new BufferedReader (new StringReader (blogCategoryMetaData)); 301 String input; 302 String [] splitInput; 303 Map categoryMetaData = new HashMap (); 304 try { 305 while ((input = br.readLine()) != null) { 306 splitInput = input.split("="); 307 if (splitInput.length == 2) { 308 categoryMetaData.put(splitInput[0], splitInput[1]); 309 } 310 } 311 } catch (IOException e) { 312 if (_logger.isErrorEnabled()) { 313 _logger.error(e); 314 } 315 316 addOperationResultMessage(context, getAdminResource(FAILED_CATEGORY_METADATA_READ_KEY, FAILED_CATEGORY_METADATA_READ_KEY, blog.getBlogAdministrationLocale())); 317 } 318 319 Integer parentCategoryID = new Integer (0); 320 try { 321 parentCategoryID = Integer.valueOf(blogCategoryParentId); 322 } catch (NumberFormatException e) { 323 } 324 325 Category blogCategory; 326 if (!isUpdatingCategory) { 327 blogCategory = _fetcher.newCategory(); 328 } else { 329 try { 330 blogCategory = _fetcher.newCategory(); 331 Integer categoryID = Integer.valueOf(blogCategoryId); 332 blogCategory.setId(categoryID); 333 334 _fetcher.loadCategory(blog, blogCategory); 335 } catch (NumberFormatException e) { 336 addOperationResultMessage(context, formatAdminResource(CATEGORY_CHANGE_FAILED_KEY, CATEGORY_CHANGE_FAILED_KEY, blog.getBlogAdministrationLocale(), new Object []{blogCategoryId})); 337 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_CATEGORIES_PAGE); 338 339 return entries; 340 } catch (FetcherException e) { 341 if (_logger.isErrorEnabled()) { 342 _logger.error(e); 343 } 344 345 addOperationResultMessage(context, formatAdminResource(CATEGORY_CHANGE_FAILED_KEY, CATEGORY_CHANGE_FAILED_KEY, blog.getBlogAdministrationLocale(), new Object []{blogCategoryId})); 346 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_CATEGORIES_PAGE); 347 348 return entries; 349 } 350 } 351 352 if (!BlojsomUtils.checkNullOrBlank(blogCategoryParentId)) { 353 blogCategory.setParentCategoryId(parentCategoryID); 354 } else { 355 blogCategory.setParentCategoryId(null); 356 } 357 358 blogCategory.setName(blogCategoryName); 359 blogCategory.setDescription(blogCategoryDescription); 360 blogCategory.setBlogId(blog.getId()); 361 blogCategory.setMetaData(categoryMetaData); 362 363 try { 364 _fetcher.saveCategory(blog, blogCategory); 365 366 if (!isUpdatingCategory) { 367 if (_logger.isDebugEnabled()) { 368 _logger.debug("Successfully added new blog category: " + blogCategoryName); 369 } 370 371 addOperationResultMessage(context, formatAdminResource(CATEGORY_ADD_SUCCESS_KEY, CATEGORY_ADD_SUCCESS_KEY, blog.getBlogAdministrationLocale(), new Object []{getDisplayName(blogCategory)})); 372 373 _eventBroadcaster.broadcastEvent(new CategoryAddedEvent(this, new Date (), blogCategory, blog)); 374 } else { 375 if (_logger.isDebugEnabled()) { 376 _logger.debug("Successfully updated blog category: " + blogCategoryName); 377 } 378 379 addOperationResultMessage(context, formatAdminResource(CATEGORY_UPDATE_SUCCESS_KEY, CATEGORY_UPDATE_SUCCESS_KEY, blog.getBlogAdministrationLocale(), new Object []{getDisplayName(blogCategory)})); 380 381 _eventBroadcaster.broadcastEvent(new CategoryUpdatedEvent(this, new Date (), blogCategory, blog)); 382 } 383 } catch (FetcherException e) { 384 if (_logger.isErrorEnabled()) { 385 _logger.error(e); 386 } 387 388 addOperationResultMessage(context, formatAdminResource(CATEGORY_CHANGE_FAILED_KEY, CATEGORY_CHANGE_FAILED_KEY, blog.getBlogAdministrationLocale(), new Object []{getDisplayName(blogCategory)})); 389 } 390 391 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, EDIT_BLOG_CATEGORIES_PAGE); 392 } 393 394 try { 395 Category[] allCategories = _fetcher.loadAllCategories(blog); 396 context.put(BLOJSOM_PLUGIN_EDIT_BLOG_CATEGORIES_ALL_CATEGORIES, allCategories); 397 } catch (FetcherException e) { 398 if (_logger.isErrorEnabled()) { 399 _logger.error(e); 400 } 401 } 402 403 return entries; 404 } 405 } 406 | Popular Tags |