1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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.util.CustomFieldUtilities;
29 import org.itracker.model.util.SystemConfigurationUtilities;
30 import org.itracker.model.util.UserUtilities;
31 import org.itracker.services.ConfigurationService;
32 import org.itracker.web.actions.base.ItrackerBaseAction;
33 import org.itracker.web.util.LoginUtilities;
34 import org.itracker.web.util.ServletContextUtils;
35
36 import javax.servlet.ServletException;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.servlet.http.HttpServletResponse;
39 import java.io.IOException;
40
41
42 public class RemoveCustomFieldAction extends ItrackerBaseAction {
43 private static final Logger log = Logger.getLogger(RemoveCustomFieldAction.class);
44
45
46 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
47 ActionMessages errors = new ActionMessages();
48
49
50 if (!LoginUtilities.hasPermission(UserUtilities.PERMISSION_USER_ADMIN, request, response)) {
51 return mapping.findForward("unauthorized");
52 }
53
54 try {
55 ConfigurationService configurationService = ServletContextUtils.getItrackerServices().getConfigurationService();
56
57 Integer valueId = (Integer) PropertyUtils.getSimpleProperty(form, "id");
58 if (valueId == null || valueId.intValue() <= 0) {
59 throw new SystemConfigurationException("Invalid custom field id.");
60 }
61
62 CustomField customField = configurationService.getCustomField(valueId);
63 if (customField == null) {
64 throw new SystemConfigurationException("Invalid custom field id.");
65 }
66 String key = CustomFieldUtilities.getCustomFieldLabelKey(customField.getId());
67 boolean status = configurationService.removeCustomField(customField.getId());
68 if (status) {
69 if (key != null)
70 ITrackerResources.clearKeyFromBundles(key, false);
71 } else {
72 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
73 }
74
75 configurationService.resetConfigurationCache(Configuration.Type.customfield);
76 if (!errors.isEmpty()) {
77 saveErrors(request, errors);
78 return mapping.getInputForward();
79 }
80 return mapping.findForward("listconfiguration");
81 } catch (SystemConfigurationException sce) {
82 log.debug(sce.getMessage(), sce);
83 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidcustomfield"));
84 } catch (NumberFormatException nfe) {
85 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidcustomfield"));
86 log.debug("Invalid custom field id " + request.getParameter("id") + " specified.");
87 } catch (Exception e) {
88 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
89 log.error("System Error.", e);
90 }
91 if (!errors.isEmpty()) {
92 saveMessages(request, errors);
93 }
94 return mapping.findForward("error");
95 }
96
97 }