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