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.CustomFieldValue;
29 import org.itracker.model.Language;
30 import org.itracker.model.util.CustomFieldUtilities;
31 import org.itracker.model.util.SystemConfigurationUtilities;
32 import org.itracker.services.ConfigurationService;
33 import org.itracker.web.actions.base.ItrackerBaseAction;
34 import org.itracker.web.forms.CustomFieldForm;
35 import org.itracker.web.util.Constants;
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 import java.util.HashMap;
44 import java.util.Iterator;
45 import java.util.List;
46
47 public class EditCustomFieldAction extends ItrackerBaseAction {
48 private static final Logger log = Logger.getLogger(EditCustomFieldAction.class);
49
50
51
52
53
54 @SuppressWarnings("unchecked")
55 @Override
56 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
57 ActionMessages errors = new ActionMessages();
58
59 if (!isTokenValid(request)) {
60 log.debug("Invalid request token while editing configuration.");
61 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
62 "itracker.web.error.transaction"));
63 saveErrors(request, errors);
64 saveToken(request);
65 return mapping.getInputForward();
66 }
67 resetToken(request);
68 try {
69 ConfigurationService configurationService = ServletContextUtils.getItrackerServices().getConfigurationService();
70 String action = (String) PropertyUtils.getSimpleProperty(form, "action");
71 if (action == null) {
72 return mapping.findForward("listconfiguration");
73 }
74 CustomFieldFormorg/itracker/web/forms/CustomFieldForm.html#CustomFieldForm">CustomFieldForm customFieldForm = (CustomFieldForm) form;
75
76 CustomField customField;
77 if ("create".equals(action)) {
78 customField = new CustomField();
79 customField.setFieldType(CustomField.Type.valueOf(customFieldForm.getFieldType()));
80 customField.setRequired(("true".equals(PropertyUtils.getSimpleProperty(form, "required")) ? true : false));
81 customField.setSortOptionsByName(("true".equals((String) PropertyUtils.getSimpleProperty(form, "sortOptionsByName")) ? true : false));
82 customField.setDateFormat((String) PropertyUtils.getSimpleProperty(form, "dateFormat"));
83 customField = configurationService.createCustomField(customField);
84 } else if ("update".equals(action)) {
85 Integer id = (Integer) PropertyUtils.getSimpleProperty(form, "id");
86 customField = configurationService.getCustomField(id);
87 if (customField == null) {
88 throw new SystemConfigurationException("Invalid custom field id " + id);
89 }
90 List<CustomFieldValue> customFieldValues = customField.getOptions();
91 customField.setFieldType(CustomField.Type.valueOf(customFieldForm.getFieldType()));
92 customField.setRequired(("true".equals((String) PropertyUtils.getSimpleProperty(form, "required")) ? true : false));
93 customField.setSortOptionsByName(("true".equals((String) PropertyUtils.getSimpleProperty(form, "sortOptionsByName")) ? true : false));
94 customField.setDateFormat((String) PropertyUtils.getSimpleProperty(form, "dateFormat"));
95 customField.setOptions(customFieldValues);
96 customField = configurationService.updateCustomField(customField);
97 } else {
98 throw new SystemConfigurationException("Invalid action " + action + " while editing custom field.");
99 }
100
101 if (customField == null) {
102 throw new SystemConfigurationException(
103 "Unable to create new custom field model.");
104 }
105
106 HashMap<String, String> translations = (HashMap<String, String>) PropertyUtils
107 .getSimpleProperty(form, "translations");
108 String key = CustomFieldUtilities
109 .getCustomFieldLabelKey(customField.getId());
110 log.debug("Processing label translations for custom field "
111 + customField.getId() + " with key " + key);
112 if (translations != null && key != null && !key.equals("")) {
113 configurationService.removeLanguageKey(key);
114 Iterator<String> iter = translations.keySet().iterator();
115 while (iter.hasNext()) {
116 String locale = iter.next();
117 if (locale != null) {
118 String translation = translations.get(locale);
119 if (translation != null && !translation.trim().equals("")) {
120 log.debug("Adding new translation for locale "
121 + locale + " for "
122 + customField.getId());
123 configurationService
124 .updateLanguageItem(new Language(locale,
125 key, translation));
126 }
127 }
128 }
129 }
130 if (key != null) {
131 ITrackerResources.clearKeyFromBundles(key, true);
132 }
133 try {
134
135 configurationService.resetConfigurationCache(Configuration.Type.customfield);
136 } catch (Exception e) {
137 log.info("execute: resetConfigurationCache trowed exception, caught", e);
138 }
139
140 HttpSession session = request.getSession();
141 if (customField.getFieldType() == CustomField.Type.LIST && "create".equals(action)) {
142 session.setAttribute(Constants.CUSTOMFIELD_KEY, customField);
143 customFieldForm.setRequestEnv(request);
144 saveToken(request);
145 return new ActionForward(mapping.findForward("editcustomfield").getPath() + "?id=" + customField.getId() + "&action=update");
146 }
147
148 session.removeAttribute(Constants.CUSTOMFIELD_KEY);
149 } catch (SystemConfigurationException sce) {
150 log.error("Exception processing form data: " + sce.getMessage(), sce);
151 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(sce.getKey()));
152 } catch (Exception e) {
153 log.error("Exception processing form data", e);
154 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
155 }
156
157 if (!errors.isEmpty()) {
158 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
159 "itracker.web.error.transaction"));
160 saveErrors(request, errors);
161 return mapping.findForward("error");
162 }
163
164 return mapping.findForward("listconfiguration");
165 }
166 }
167