View Javadoc
1   /*
2    * This software was designed and created by Jason Carroll.
3    * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4    * The author can be reached at jcarroll@cowsultants.com
5    * ITracker website: http://www.cowsultants.com
6    * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7    *
8    * This program is free software; you can redistribute it and/or modify
9    * it only under the terms of the GNU General Public License as published by
10   * the Free Software Foundation; either version 2 of the License, or
11   * (at your option) any later version.
12   *
13   * This program is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   * GNU General Public License for more details.
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.model.util.PropertiesFileHandler;
27  import org.itracker.model.util.SystemConfigurationUtilities;
28  import org.itracker.services.ConfigurationService;
29  import org.itracker.web.actions.base.ItrackerBaseAction;
30  import org.itracker.web.forms.LanguageForm;
31  import org.itracker.web.util.Constants;
32  import org.itracker.web.util.ServletContextUtils;
33  
34  import javax.servlet.ServletException;
35  import javax.servlet.http.HttpServletRequest;
36  import javax.servlet.http.HttpServletResponse;
37  import javax.servlet.http.HttpSession;
38  import java.io.File;
39  import java.io.IOException;
40  import java.lang.reflect.InvocationTargetException;
41  import java.util.*;
42  
43  
44  public class EditLanguageFormAction extends ItrackerBaseAction {
45      private static final Logger log = Logger.getLogger(EditLanguageFormAction.class);
46  
47      public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
48          ActionMessages errors = new ActionMessages();
49  
50  
51          try {
52              ConfigurationService configurationService = ServletContextUtils.getItrackerServices().getConfigurationService();
53  
54              HttpSession session = request.getSession(true);
55  
56              LanguageForm../../org/itracker/web/forms/LanguageForm.html#LanguageForm">LanguageForm languageForm = (LanguageForm) form;
57              if (languageForm == null) {
58                  languageForm = new LanguageForm();
59              }
60  
61              String locale = (String) PropertyUtils.getSimpleProperty(form, "locale");
62              int localeType = SystemConfigurationUtilities.getLocaleType(locale);
63              if (localeType == SystemConfigurationUtilities.LOCALE_TYPE_INVALID) {
64                  errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidlocale"));
65              } else {
66                  if ("create".equals(PropertyUtils.getSimpleProperty(form, "action"))) {
67                      // The locale passed in on a create action is actually the parent locale.  Reset the parent
68                      // locale, increment the type (since we are creating the next type, and clear the locale
69                      localeType++;
70                      languageForm.setParentLocale(locale);
71                      if (localeType == SystemConfigurationUtilities.LOCALE_TYPE_LOCALE) {
72                          languageForm.setLocale(locale + "_");
73                      } else {
74                          languageForm.setLocale("");
75                      }
76                  }
77  
78                  String[] sortedKeys = configurationService.getSortedKeys();
79  
80                  Map<String, String> baseItems = new HashMap<>();
81                  Map<String, String> langItems = new HashMap<>();
82                  Map<String, String> locItems = new HashMap<>();
83                  Map<String, String> items = new HashMap<>();
84  
85                  log.debug("Loading language elements for edit.  Edit type is " + localeType);
86  
87                  if (localeType >= SystemConfigurationUtilities.LOCALE_TYPE_BASE) {
88                      baseItems = configurationService.getDefinedKeys(null);
89  //                    putPropertiesKeys(baseItems, ITrackerResources.BASE_LOCALE);
90                      items = new HashMap<>();
91                      log.debug("Base Locale has " + baseItems.size() + " keys defined.");
92                  }
93  
94                  if (localeType >= SystemConfigurationUtilities.LOCALE_TYPE_LANGUAGE) {
95                      if (!locale.equalsIgnoreCase(ITrackerResources.BASE_LOCALE)) {
96                          String parentLocale = SystemConfigurationUtilities.getLocalePart(locale, SystemConfigurationUtilities.LOCALE_TYPE_LANGUAGE);
97                          languageForm.setParentLocale(parentLocale);
98                          langItems = configurationService.getDefinedKeys(parentLocale);
99  //                        putPropertiesKeys(langItems, parentLocale);
100 
101                         items = new HashMap<>();
102                         log.debug("Language " + parentLocale + " has " + langItems.size() + " keys defined.");
103                     }
104                 }
105 
106                 if (localeType == SystemConfigurationUtilities.LOCALE_TYPE_LOCALE) {
107                     locItems = configurationService.getDefinedKeys(locale);
108 //                    putPropertiesKeys(locItems, locale);
109 
110                     items = locItems;
111                     log.debug("Locale " + locale + " has " + locItems.size() + " keys defined.");
112                 }
113 
114                 if (!"create".equals(PropertyUtils.getSimpleProperty(form, "action"))) {
115                     Map<String, String> formItems = new HashMap<>();
116                     for (Enumeration<String> en = ITrackerResources.getBundle(locale).getKeys(); en.hasMoreElements(); ) {
117                         String key = en.nextElement();
118                         formItems.put(key, "");
119                     }
120                     formItems.putAll(items);
121 
122                     languageForm.setItems(new TreeMap<>(formItems));
123                 } else {
124                     String parentLocale = null;
125 
126                     if (!locale.equalsIgnoreCase(ITrackerResources.BASE_LOCALE)) {
127                         parentLocale = SystemConfigurationUtilities.getLocalePart(locale, SystemConfigurationUtilities.LOCALE_TYPE_LANGUAGE);
128                     }
129                     langItems = configurationService.getDefinedKeys(parentLocale);
130 
131                     Map<String, String> formItems = new HashMap<>();
132                     if (log.isDebugEnabled()) {
133                         log.debug("putPropertiesKeys: items: " + items);
134                     }
135                     for (Enumeration<String> en = ITrackerResources.getBundle(locale).getKeys(); en.hasMoreElements(); ) {
136                         String key = en.nextElement();
137                         formItems.put(key, "");
138                     }
139 
140                     formItems.putAll(items);
141 
142                     languageForm.setItems(new TreeMap<>(formItems));
143 
144                 }
145                 Locale curLocale = ITrackerResources.getLocale(locale);
146 
147                 Language languageItem = new Language(locale, "itracker.locale.name",
148                         ITrackerResources.getString("itracker.locale.name", curLocale));// configurationService.getLanguageItemByKey("itracker.locale.name", curLocale);
149 
150                 languageForm.setLocaleTitle(languageItem.getResourceValue());
151                 languageItem = new Language(locale, "itracker.locale.name",
152                         ITrackerResources.getString("itracker.locale.name." + locale, ITrackerResources.BASE_LOCALE));// configurationService.getLanguageItemByKey("itracker.locale.name", curLocale);
153 
154                 languageForm.setLocaleBaseTitle(languageItem.getResourceValue());
155                 session.setAttribute(Constants.EDIT_LANGUAGE_KEYS_KEY, sortedKeys);
156                 session.setAttribute(Constants.EDIT_LANGUAGE_BASE_KEY, baseItems);
157                 session.setAttribute(Constants.EDIT_LANGUAGE_LANG_KEY, langItems);
158                 session.setAttribute(Constants.EDIT_LANGUAGE_LOC_KEY, locItems);
159                 session.setAttribute(Constants.EDIT_LANGUAGE_TYPE_KEY, localeType);
160                 request.setAttribute("languageForm", languageForm);
161                 if (log.isDebugEnabled()) {
162                     log.debug("Locale = " + languageForm.getLocale());
163                 }
164                 saveToken(request);
165                 return mapping.getInputForward();
166             }
167         } catch (RuntimeException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
168             log.error("Exception while creating edit language form.", e);
169             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
170         }
171 
172         if (!errors.isEmpty()) {
173             saveErrors(request, errors);
174         }
175 
176         return mapping.findForward("error");
177     }
178 
179     @SuppressWarnings("unchecked")
180     void putPropertiesKeys(Map<String, String> locItems, String locale) {
181         try {
182             Hashtable<Object, Object> p;
183             try {
184                 String path = File.separatorChar + ITrackerResources.RESOURCE_BUNDLE_NAME.replace('.', File.separatorChar) + (null != locale && !(ITrackerResources.BASE_LOCALE.equals(locale)) ? "_" + locale : "") + ".properties";
185                 if (log.isDebugEnabled()) {
186                     log.debug("putPropertiesKeys: loading: " + path);
187                 }
188                 p = new PropertiesFileHandler(path).getProperties();
189                 p = new Hashtable<>(p);
190 
191                 if (log.isDebugEnabled()) {
192                     log.debug("putPropertiesKeys: loaded properties: " + p);
193                 }
194             } catch (Exception e) {
195                 if (log.isDebugEnabled()) {
196                     log.debug("putPropertiesKeys", e);
197                 }
198                 p = new Properties();
199             }
200             // overload properties by loc items from db
201             if (log.isDebugEnabled()) {
202                 log.debug("putPropertiesKeys: overloading locItems: " + locItems);
203             }
204             p.putAll(locItems);
205             locItems.putAll(Collections.checkedMap((Map) p, String.class, String.class));
206 
207         } catch (RuntimeException e) {
208             log.error("addPropertiesKeys: caught ", e);
209         }
210     }
211 }
212