View Javadoc
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  
19  package org.itracker.web.actions.admin.configuration;
20  
21  import org.apache.commons.beanutils.PropertyUtils;
22  import org.apache.log4j.Logger;
23  import org.apache.struts.action.*;
24  import org.itracker.SystemConfigurationException;
25  import org.itracker.core.resources.ITrackerResources;
26  import org.itracker.model.Configuration;
27  import org.itracker.model.CustomField;
28  import org.itracker.model.CustomFieldValue;
29  import org.itracker.model.util.CustomFieldUtilities;
30  import org.itracker.model.util.SystemConfigurationUtilities;
31  import org.itracker.model.util.UserUtilities;
32  import org.itracker.services.ConfigurationService;
33  import org.itracker.web.actions.base.ItrackerBaseAction;
34  import org.itracker.web.util.Constants;
35  import org.itracker.web.util.LoginUtilities;
36  import org.itracker.web.util.ServletContextUtils;
37  
38  import javax.servlet.ServletException;
39  import javax.servlet.http.HttpServletRequest;
40  import javax.servlet.http.HttpServletResponse;
41  import javax.servlet.http.HttpSession;
42  import java.io.IOException;
43  
44  
45  public class RemoveCustomFieldValueAction extends ItrackerBaseAction {
46      private static final Logger log = Logger.getLogger(RemoveCustomFieldValueAction.class);
47  
48      public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
49          ActionMessages errors = new ActionMessages();
50  
51  
52          if (!LoginUtilities.hasPermission(UserUtilities.PERMISSION_USER_ADMIN, request, response)) {
53              return mapping.findForward("unauthorized");
54          }
55  
56          try {
57              ConfigurationService configurationService = ServletContextUtils.getItrackerServices().getConfigurationService();
58  
59              Integer valueId = (Integer) PropertyUtils.getSimpleProperty(form, "id");
60              if (valueId == null || valueId.intValue() <= 0) {
61                  throw new SystemConfigurationException("Invalid custom field value id.");
62              }
63  
64              CustomFieldValue customFieldValue = configurationService.getCustomFieldValue(valueId);
65              if (customFieldValue == null) {
66                  throw new SystemConfigurationException("Invalid custom field value id.");
67              }
68  
69              String key = CustomFieldUtilities.getCustomFieldOptionLabelKey(customFieldValue.getCustomField().getId(), customFieldValue.getId());
70              boolean status = configurationService.removeCustomFieldValue(customFieldValue.getId());
71  
72              if (status) {
73                  if (key != null) {
74                      ITrackerResources.clearKeyFromBundles(key, false);
75                  }
76                  configurationService.resetConfigurationCache(Configuration.Type.customfield);
77  
78                  HttpSession session = request.getSession(true);
79                  CustomField customField = (CustomField) session.getAttribute(Constants.CUSTOMFIELD_KEY);
80                  if (customField == null) {
81                      return mapping.findForward("listconfiguration");
82                  }
83                  return new ActionForward(mapping.findForward("editcustomfield").getPath() + "?id=" + customField.getId() + "&action=update");
84              } else {
85                  errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
86              }
87  
88          } catch (SystemConfigurationException sce) {
89              log.debug(sce.getMessage(), sce);
90              errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidcustomfieldvalue"));
91          } catch (NumberFormatException nfe) {
92              errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidcustomfieldvalue"));
93              log.debug("Invalid custom field value id " + request.getParameter("id") + " specified.");
94          } catch (Exception e) {
95              errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
96              log.error("System Error.", e);
97          }
98          if (!errors.isEmpty()) {
99              saveErrors(request, errors);
100         }
101         return mapping.findForward("error");
102     }
103 
104 }