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.model.CustomField;
26 import org.itracker.model.Language;
27 import org.itracker.model.util.CustomFieldUtilities;
28 import org.itracker.model.util.UserUtilities;
29 import org.itracker.services.ConfigurationService;
30 import org.itracker.web.actions.base.ItrackerBaseAction;
31 import org.itracker.web.forms.CustomFieldForm;
32 import org.itracker.web.util.Constants;
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 javax.servlet.http.HttpSession;
40 import java.io.IOException;
41 import java.util.List;
42 import java.util.Map;
43 import java.util.TreeMap;
44
45
46
47 public class EditCustomFieldFormAction extends ItrackerBaseAction {
48 private static final Logger log = Logger.getLogger(EditCustomFieldFormAction.class);
49
50
51
52
53 @Override
54 public ActionForward execute(ActionMapping mapping, ActionForm form,
55 HttpServletRequest request, HttpServletResponse response)
56 throws ServletException, IOException {
57 ActionMessages errors = new ActionMessages();
58
59 if (!LoginUtilities.hasPermission(UserUtilities.PERMISSION_USER_ADMIN, request, response)) {
60 return mapping.findForward("unauthorized");
61 }
62
63 try {
64
65 HttpSession session = request.getSession(true);
66
67 CustomFieldFormorg/itracker/web/forms/CustomFieldForm.html#CustomFieldForm">CustomFieldForm customFieldForm = (CustomFieldForm) form;
68 if (customFieldForm == null)
69 customFieldForm = new CustomFieldForm();
70
71 String action = (String) PropertyUtils.getSimpleProperty(customFieldForm, "action");
72 CustomField customField = null;
73 ConfigurationService configurationService = ServletContextUtils.getItrackerServices().getConfigurationService();
74 if ("create".equals(action)) {
75 customField = new CustomField();
76 } else if ("update".equals(action)) {
77 Integer id = (Integer) PropertyUtils.getSimpleProperty(customFieldForm, "id");
78 customField = configurationService.getCustomField(id);
79 if (customField == null)
80 throw new SystemConfigurationException("Invalid custom field id " + id);
81
82 customFieldForm.setId(id);
83 customFieldForm.setFieldType(customField.getFieldType().getCode());
84 customFieldForm.setRequired(Boolean.toString(customField.isRequired()));
85 customFieldForm.setDateFormat(customField.getDateFormat());
86 customFieldForm.setSortOptionsByName(Boolean.toString(customField.isSortOptionsByName()));
87
88 Map<String, String> translations = new TreeMap<>();
89 List<Language> languageItems = configurationService.getLanguageItemsByKey(CustomFieldUtilities.getCustomFieldLabelKey(customField.getId()));
90 for (Language languageItem : languageItems) {
91 translations.put(languageItem.getLocale(), languageItem.getResourceValue());
92 }
93 customFieldForm.setTranslations(translations);
94 }
95
96
97
98
99
100
101 if (customField == null) {
102 log.error("EditCustomFieldFormAction#execute: customField was null!");
103 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidproject"));
104 return mapping.findForward("error");
105 }
106 session.setAttribute(Constants.CUSTOMFIELD_KEY, customField);
107 customFieldForm.setRequestEnv(request);
108 saveToken(request);
109
110 } catch (SystemConfigurationException sce) {
111 log.error("execute: Exception system configuration exception caught.", sce);
112 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
113 "itracker.web.error.invalidcustomfield"));
114 } catch (Exception e) {
115 log.error("execute: Exception while creating edit custom field form.", e);
116 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
117 "itracker.web.error.system"));
118 }
119
120 if (!errors.isEmpty()) {
121 saveErrors(request, errors);
122 return mapping.findForward("error");
123 }
124
125 return mapping.getInputForward();
126 }
127
128
129 }