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.services.ConfigurationService;
32 import org.itracker.web.actions.base.ItrackerBaseAction;
33 import org.itracker.web.forms.CustomFieldValueForm;
34 import org.itracker.web.util.Constants;
35
36 import javax.servlet.ServletException;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.servlet.http.HttpServletResponse;
39 import javax.servlet.http.HttpSession;
40 import java.io.IOException;
41 import java.util.Map;
42
43 public class EditCustomFieldValueAction extends ItrackerBaseAction {
44 private static final Logger log = Logger.getLogger(EditCustomFieldValueAction.class);
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 (!isTokenValid(request)) {
51 log.debug("Invalid request token while editing configuration.");
52 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
53 "itracker.web.error.transaction"));
54 saveErrors(request, errors);
55 return mapping.findForward("listconfiguration");
56 }
57 resetToken(request);
58 HttpSession session = request.getSession(true);
59 CustomField customField = (CustomField) session.getAttribute(Constants.CUSTOMFIELD_KEY);
60 if (customField == null) {
61 return mapping.findForward("listconfiguration");
62 }
63
64 CustomFieldValueFormer/web/forms/CustomFieldValueForm.html#CustomFieldValueForm">CustomFieldValueForm customFieldValueForm = (CustomFieldValueForm) form;
65
66 try {
67
68 String action = customFieldValueForm.getAction();
69
70 if (action == null) {
71 return mapping.findForward("listconfiguration");
72 }
73
74 ConfigurationService configurationService = getITrackerServices().getConfigurationService();
75 CustomFieldValue customFieldValue;
76
77 if ("create".equals(action)) {
78 customFieldValue = new CustomFieldValue();
79 customFieldValue.setCustomField(customField);
80 customFieldValue.setValue(customFieldValueForm.getValue());
81 customFieldValue.setSortOrder(customFieldValueForm.getSortOrder());
82 customFieldValue = configurationService.createCustomFieldValue(customFieldValue);
83 } else if ("update".equals(action)) {
84 Integer id = (Integer) PropertyUtils.getSimpleProperty(form, "id");
85 customFieldValue = configurationService.getCustomFieldValue(id);
86 customFieldValue.setValue(customFieldValueForm.getValue());
87 customFieldValue.setSortOrder(customFieldValueForm.getSortOrder());
88 customFieldValue = configurationService.updateCustomFieldValue(customFieldValue);
89 } else {
90 throw new SystemConfigurationException("Invalid action " + action + " while editing custom field value.");
91 }
92
93 if (customFieldValue == null) {
94 throw new SystemConfigurationException("Unable to create new custom field value model.");
95 }
96
97 Map<String, String> translations = customFieldValueForm.getTranslations();
98 String key = CustomFieldUtilities.getCustomFieldOptionLabelKey(customField.getId(), customFieldValue.getId());
99 log.debug("Processing label translations for custom field value " + customFieldValue.getId() + " with key " + key);
100 if (translations != null && key != null && !key.equals("")) {
101 for (String locale : translations.keySet()) {
102 if (locale != null) {
103 String translation = translations.get(locale);
104 if (translation != null && !translation.equals("")) {
105 log.debug("Adding new translation for locale " + locale + " for " + String.valueOf(customFieldValue.getId()));
106 configurationService.updateLanguageItem(new Language(locale, key, translation));
107 }
108 }
109 }
110 String baseValue = translations.get(ITrackerResources.BASE_LOCALE);
111 configurationService.updateLanguageItem(new Language(ITrackerResources.BASE_LOCALE, key, baseValue));
112 }
113 if (key != null)
114 ITrackerResources.clearKeyFromBundles(key, true);
115
116 configurationService.resetConfigurationCache(Configuration.Type.customfield);
117 request.setAttribute("action", action);
118 String pageTitleKey = "";
119 String pageTitleArg = "";
120 pageTitleKey = "itracker.web.admin.editcustomfieldvalue.title.create";
121 if ("update".equals(action)) {
122 pageTitleKey = "itracker.web.admin.editcustomfieldvalue.title.update";
123 }
124
125 request.setAttribute("languages", configurationService.getAvailableLanguages());
126 request.setAttribute("pageTitleKey", pageTitleKey);
127 request.setAttribute("pageTitleArg", pageTitleArg);
128
129 saveToken(request);
130 return new ActionForward(mapping.findForward("editcustomfield").getPath() + "?id=" + customField.getId() + "&action=update");
131 } catch (SystemConfigurationException sce) {
132 log.error("Exception processing form data: " + sce.getMessage(), sce);
133 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(sce.getKey()));
134 } catch (Exception e) {
135 log.error("Exception processing form data", e);
136 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
137 }
138
139 if (!errors.isEmpty()) {
140 saveErrors(request, errors);
141 saveToken(request);
142 request.setAttribute("customFieldValueForm", form);
143 return mapping.getInputForward();
144 }
145
146 return mapping.findForward("error");
147 }
148 }
149