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.Language;
26 import org.itracker.services.ConfigurationService;
27 import org.itracker.web.actions.base.ItrackerBaseAction;
28 import org.itracker.web.util.ServletContextUtils;
29
30 import javax.servlet.ServletException;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33 import java.io.IOException;
34 import java.util.HashMap;
35 import java.util.Iterator;
36
37
38 public class CreateLanguageKeyAction extends ItrackerBaseAction {
39 private static final Logger log = Logger.getLogger(CreateLanguageKeyAction.class);
40
41
42 @SuppressWarnings("unchecked")
43 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
44 ActionMessages errors = new ActionMessages();
45
46
47 if (!isTokenValid(request)) {
48 log.debug("Invalid request token while creating language key.");
49 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
50 "itracker.web.error.transaction"));
51 saveErrors(request, errors);
52 return mapping.findForward("listlanguages");
53 }
54 resetToken(request);
55
56 try {
57 ConfigurationService configurationService = ServletContextUtils.getItrackerServices().getConfigurationService();
58
59 String key = (String) PropertyUtils.getSimpleProperty(form, "key");
60 HashMap<String, String> items = (HashMap<String, String>) PropertyUtils.getSimpleProperty(form, "items");
61
62
63 if (items != null) {
64 log.debug("Adding new language key: " + key);
65 for (Iterator<String> iter = items.keySet().iterator(); iter.hasNext(); ) {
66 String locale = iter.next();
67 log.debug("Checking translation for locale " + locale);
68 if (locale != null) {
69 String value = items.get(locale);
70 log.debug("Locale value: " + value);
71 if (value != null && !value.equals("")) {
72 log.debug("Adding new translation for locale " + locale + " for key " + key);
73 configurationService.updateLanguageItem(new Language(locale, key, value));
74 }
75 }
76 }
77 String baseValue = (String) items.get(ITrackerResources.BASE_LOCALE);
78
79 configurationService.updateLanguageItem(new Language(ITrackerResources.BASE_LOCALE, key, baseValue));
80 ITrackerResources.clearKeyFromBundles(key, true);
81 }
82
83 return mapping.findForward("listlanguages");
84 } catch (Exception e) {
85 log.error("Exception processing form data", e);
86 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
87 }
88
89 if (!errors.isEmpty()) {
90 saveErrors(request, errors);
91 saveToken(request);
92 return mapping.getInputForward();
93 }
94 return mapping.findForward("error");
95 }
96 }
97