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.language;
20
21 import org.apache.commons.beanutils.PropertyUtils;
22 import org.apache.log4j.Logger;
23 import org.apache.struts.action.*;
24 import org.itracker.core.resources.ITrackerResources;
25 import org.itracker.model.Configuration;
26 import org.itracker.model.Language;
27 import org.itracker.persistence.dao.NoSuchEntityException;
28 import org.itracker.services.ConfigurationService;
29 import org.itracker.web.actions.base.ItrackerBaseAction;
30 import org.itracker.web.util.Constants;
31 import org.itracker.web.util.ServletContextUtils;
32
33 import javax.servlet.ServletException;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.servlet.http.HttpServletResponse;
36 import javax.servlet.http.HttpSession;
37 import java.io.IOException;
38 import java.util.HashMap;
39 import java.util.List;
40 import java.util.Locale;
41 import java.util.MissingResourceException;
42
43
44 public class EditLanguageAction extends ItrackerBaseAction {
45 private static final Logger log = Logger.getLogger(EditLanguageAction.class);
46
47
48 @SuppressWarnings("unchecked")
49 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
50 ActionMessages errors = new ActionMessages();
51 String action;
52 try {
53 action = (String) PropertyUtils.getSimpleProperty(form, "action");
54 } catch (Exception e) {
55 log.error("Exception processing form data", e);
56 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
57 return mapping.findForward("error");
58 }
59 if (!isTokenValid(request) && !"disable".equals(action)) {
60 log.info("Invalid request token while editing language.");
61 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
62 "itracker.web.error.transaction"));
63 saveErrors(request, errors);
64 return mapping.findForward("listlanguages");
65 }
66 resetToken(request);
67 HttpSession session = request.getSession(true);
68
69 try {
70 ConfigurationService configurationService = ServletContextUtils.getItrackerServices().getConfigurationService();
71
72 String locale = (String) PropertyUtils.getSimpleProperty(form, "locale");
73 String localeTitle = (String) PropertyUtils.getSimpleProperty(form, "localeTitle");
74 String localeBaseTitle = (String) PropertyUtils.getSimpleProperty(form, "localeBaseTitle");
75 HashMap<String, String> items = (HashMap<String, String>) PropertyUtils.getSimpleProperty(form, "items");
76
77 if (items == null) {
78 return mapping.findForward("listlanguages");
79 }
80
81 if (locale == null || "".equals(locale.trim())) {
82 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidlocale"));
83 } else if ("disable".equals(action)) {
84
85 configurationService.getAvailableLanguages();
86
87 List<Configuration> localeConfigs = configurationService.getConfigurationItemsByType(Configuration.Type.locale);
88
89 for (Configuration configuration: localeConfigs) {
90 if (configuration.getValue().equals(locale) ||
91 configuration.getValue().startsWith(locale + "_")) {
92 configurationService.removeConfigurationItem(configuration.getId());
93 ITrackerResources.clearBundles();
94 return mapping.findForward("listlanguages");
95 }
96 }
97
98
99 } else if ("create".equals(action)) {
100
101 if (locale.length() != 2 && (locale.length() != 5 || locale.indexOf('_') != 2)) {
102 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidlocale"));
103 } else {
104
105 Configuration localeConfig = new Configuration(Configuration.Type.locale, locale);
106 if (configurationService.configurationItemExists(localeConfig)) {
107 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidlocale"));
108 } else {
109
110 configurationService.updateLanguageItem(new Language(locale, "itracker.locale.name", localeTitle));
111 configurationService.updateLanguageItem(new Language(locale, "itracker.locale.name." + locale, localeTitle));
112 configurationService.updateLanguageItem(new Language(ITrackerResources.BASE_LOCALE, "itracker.locale.name." + locale, localeBaseTitle));
113 for (String key : items.keySet()) {
114 if (key != null) {
115 String value = items.get(key);
116 if (value != null && value.length() != 0) {
117 configurationService.updateLanguageItem(new Language(locale, key.replace('/', '.'), value));
118 }
119 }
120 }
121 configurationService.createConfigurationItem(localeConfig);
122 ITrackerResources.clearBundles();
123 clearSessionObjects(session);
124 return mapping.findForward("listlanguages");
125 }
126 }
127 } else if ("update".equals(action)) {
128
129 Locale updateLocale = ITrackerResources.getLocale(locale);
130 for (String key : items.keySet()) {
131 if (key != null) {
132 String value = items.get(key);
133 try {
134 String currValue = ITrackerResources.getCheckForKey(key.replace('/', '.'), updateLocale);
135 if (value == null || value.length() == 0) {
136 try {
137 configurationService.removeLanguageItem(new Language(locale, key.replace('/', '.')));
138 } catch (NoSuchEntityException e) {
139
140 }
141
142 } else if (!value.equals(currValue)) {
143 configurationService.updateLanguageItem(new Language(locale, key.replace('/', '.'), value));
144 }
145 } catch (MissingResourceException mre) {
146 if (value != null && !value.trim().equals("")) {
147 configurationService.updateLanguageItem(new Language(locale, key.replace('/', '.'), value));
148 }
149 }
150 }
151 }
152
153 configurationService.updateLanguageItem(new Language(locale, "itracker.locale.name", localeTitle));
154 configurationService.updateLanguageItem(new Language(locale, "itracker.locale.name." + locale, localeTitle));
155 configurationService.updateLanguageItem(new Language(ITrackerResources.BASE_LOCALE, "itracker.locale.name." + locale, localeBaseTitle));
156
157 ITrackerResources.clearBundles();
158 clearSessionObjects(session);
159 return mapping.findForward("listlanguages");
160 } else {
161 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidaction"));
162 }
163 } catch (Exception e) {
164 log.error("Exception processing form data", e);
165 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
166 }
167
168 if (!errors.isEmpty()) {
169 saveErrors(request, errors);
170 saveToken(request);
171 return mapping.getInputForward();
172 }
173
174 clearSessionObjects(session);
175 return mapping.findForward("error");
176 }
177
178
179 private void clearSessionObjects(HttpSession session) {
180 session.removeAttribute(Constants.EDIT_LANGUAGE_KEYS_KEY);
181 session.removeAttribute(Constants.EDIT_LANGUAGE_BASE_KEY);
182 session.removeAttribute(Constants.EDIT_LANGUAGE_LANG_KEY);
183 session.removeAttribute(Constants.EDIT_LANGUAGE_LOC_KEY);
184 session.removeAttribute(Constants.EDIT_LANGUAGE_TYPE_KEY);
185 }
186 }