RemoveCustomFieldValueAction.java

  1. /*
  2.  * This software was designed and created by Jason Carroll.
  3.  * Copyright (c) 2002, 2003, 2004 Jason Carroll.
  4.  * The author can be reached at jcarroll@cowsultants.com
  5.  * ITracker website: http://www.cowsultants.com
  6.  * ITracker forums: http://www.cowsultants.com/phpBB/index.php
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it only under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  */

  18. package org.itracker.web.actions.admin.configuration;

  19. import org.apache.commons.beanutils.PropertyUtils;
  20. import org.apache.log4j.Logger;
  21. import org.apache.struts.action.*;
  22. import org.itracker.SystemConfigurationException;
  23. import org.itracker.core.resources.ITrackerResources;
  24. import org.itracker.model.Configuration;
  25. import org.itracker.model.CustomField;
  26. import org.itracker.model.CustomFieldValue;
  27. import org.itracker.model.util.CustomFieldUtilities;
  28. import org.itracker.model.util.SystemConfigurationUtilities;
  29. import org.itracker.model.util.UserUtilities;
  30. import org.itracker.services.ConfigurationService;
  31. import org.itracker.web.actions.base.ItrackerBaseAction;
  32. import org.itracker.web.util.Constants;
  33. import org.itracker.web.util.LoginUtilities;
  34. import org.itracker.web.util.ServletContextUtils;

  35. import javax.servlet.ServletException;
  36. import javax.servlet.http.HttpServletRequest;
  37. import javax.servlet.http.HttpServletResponse;
  38. import javax.servlet.http.HttpSession;
  39. import java.io.IOException;


  40. public class RemoveCustomFieldValueAction extends ItrackerBaseAction {
  41.     private static final Logger log = Logger.getLogger(RemoveCustomFieldValueAction.class);

  42.     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  43.         ActionMessages errors = new ActionMessages();


  44.         if (!LoginUtilities.hasPermission(UserUtilities.PERMISSION_USER_ADMIN, request, response)) {
  45.             return mapping.findForward("unauthorized");
  46.         }

  47.         try {
  48.             ConfigurationService configurationService = ServletContextUtils.getItrackerServices().getConfigurationService();

  49.             Integer valueId = (Integer) PropertyUtils.getSimpleProperty(form, "id");
  50.             if (valueId == null || valueId.intValue() <= 0) {
  51.                 throw new SystemConfigurationException("Invalid custom field value id.");
  52.             }

  53.             CustomFieldValue customFieldValue = configurationService.getCustomFieldValue(valueId);
  54.             if (customFieldValue == null) {
  55.                 throw new SystemConfigurationException("Invalid custom field value id.");
  56.             }

  57.             String key = CustomFieldUtilities.getCustomFieldOptionLabelKey(customFieldValue.getCustomField().getId(), customFieldValue.getId());
  58.             boolean status = configurationService.removeCustomFieldValue(customFieldValue.getId());

  59.             if (status) {
  60.                 if (key != null) {
  61.                     ITrackerResources.clearKeyFromBundles(key, false);
  62.                 }
  63.                 configurationService.resetConfigurationCache(Configuration.Type.customfield);

  64.                 HttpSession session = request.getSession(true);
  65.                 CustomField customField = (CustomField) session.getAttribute(Constants.CUSTOMFIELD_KEY);
  66.                 if (customField == null) {
  67.                     return mapping.findForward("listconfiguration");
  68.                 }
  69.                 return new ActionForward(mapping.findForward("editcustomfield").getPath() + "?id=" + customField.getId() + "&action=update");
  70.             } else {
  71.                 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
  72.             }

  73.         } catch (SystemConfigurationException sce) {
  74.             log.debug(sce.getMessage(), sce);
  75.             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidcustomfieldvalue"));
  76.         } catch (NumberFormatException nfe) {
  77.             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidcustomfieldvalue"));
  78.             log.debug("Invalid custom field value id " + request.getParameter("id") + " specified.");
  79.         } catch (Exception e) {
  80.             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
  81.             log.error("System Error.", e);
  82.         }
  83.         if (!errors.isEmpty()) {
  84.             saveErrors(request, errors);
  85.         }
  86.         return mapping.findForward("error");
  87.     }

  88. }