EditLanguageFormAction.java

  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. package org.itracker.web.actions.admin.language;

  19. import org.apache.commons.beanutils.PropertyUtils;
  20. import org.apache.log4j.Logger;
  21. import org.apache.struts.action.*;
  22. import org.itracker.core.resources.ITrackerResources;
  23. import org.itracker.model.Language;
  24. import org.itracker.model.util.PropertiesFileHandler;
  25. import org.itracker.model.util.SystemConfigurationUtilities;
  26. import org.itracker.services.ConfigurationService;
  27. import org.itracker.web.actions.base.ItrackerBaseAction;
  28. import org.itracker.web.forms.LanguageForm;
  29. import org.itracker.web.util.Constants;
  30. import org.itracker.web.util.ServletContextUtils;

  31. import javax.servlet.ServletException;
  32. import javax.servlet.http.HttpServletRequest;
  33. import javax.servlet.http.HttpServletResponse;
  34. import javax.servlet.http.HttpSession;
  35. import java.io.File;
  36. import java.io.IOException;
  37. import java.lang.reflect.InvocationTargetException;
  38. import java.util.*;


  39. public class EditLanguageFormAction extends ItrackerBaseAction {
  40.     private static final Logger log = Logger.getLogger(EditLanguageFormAction.class);

  41.     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  42.         ActionMessages errors = new ActionMessages();


  43.         try {
  44.             ConfigurationService configurationService = ServletContextUtils.getItrackerServices().getConfigurationService();

  45.             HttpSession session = request.getSession(true);

  46.             LanguageForm languageForm = (LanguageForm) form;
  47.             if (languageForm == null) {
  48.                 languageForm = new LanguageForm();
  49.             }

  50.             String locale = (String) PropertyUtils.getSimpleProperty(form, "locale");
  51.             int localeType = SystemConfigurationUtilities.getLocaleType(locale);
  52.             if (localeType == SystemConfigurationUtilities.LOCALE_TYPE_INVALID) {
  53.                 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidlocale"));
  54.             } else {
  55.                 if ("create".equals(PropertyUtils.getSimpleProperty(form, "action"))) {
  56.                     // The locale passed in on a create action is actually the parent locale.  Reset the parent
  57.                     // locale, increment the type (since we are creating the next type, and clear the locale
  58.                     localeType++;
  59.                     languageForm.setParentLocale(locale);
  60.                     if (localeType == SystemConfigurationUtilities.LOCALE_TYPE_LOCALE) {
  61.                         languageForm.setLocale(locale + "_");
  62.                     } else {
  63.                         languageForm.setLocale("");
  64.                     }
  65.                 }

  66.                 String[] sortedKeys = configurationService.getSortedKeys();

  67.                 Map<String, String> baseItems = new HashMap<>();
  68.                 Map<String, String> langItems = new HashMap<>();
  69.                 Map<String, String> locItems = new HashMap<>();
  70.                 Map<String, String> items = new HashMap<>();

  71.                 log.debug("Loading language elements for edit.  Edit type is " + localeType);

  72.                 if (localeType >= SystemConfigurationUtilities.LOCALE_TYPE_BASE) {
  73.                     baseItems = configurationService.getDefinedKeys(null);
  74. //                    putPropertiesKeys(baseItems, ITrackerResources.BASE_LOCALE);
  75.                     items = new HashMap<>();
  76.                     log.debug("Base Locale has " + baseItems.size() + " keys defined.");
  77.                 }

  78.                 if (localeType >= SystemConfigurationUtilities.LOCALE_TYPE_LANGUAGE) {
  79.                     if (!locale.equalsIgnoreCase(ITrackerResources.BASE_LOCALE)) {
  80.                         String parentLocale = SystemConfigurationUtilities.getLocalePart(locale, SystemConfigurationUtilities.LOCALE_TYPE_LANGUAGE);
  81.                         languageForm.setParentLocale(parentLocale);
  82.                         langItems = configurationService.getDefinedKeys(parentLocale);
  83. //                        putPropertiesKeys(langItems, parentLocale);

  84.                         items = new HashMap<>();
  85.                         log.debug("Language " + parentLocale + " has " + langItems.size() + " keys defined.");
  86.                     }
  87.                 }

  88.                 if (localeType == SystemConfigurationUtilities.LOCALE_TYPE_LOCALE) {
  89.                     locItems = configurationService.getDefinedKeys(locale);
  90. //                    putPropertiesKeys(locItems, locale);

  91.                     items = locItems;
  92.                     log.debug("Locale " + locale + " has " + locItems.size() + " keys defined.");
  93.                 }

  94.                 if (!"create".equals(PropertyUtils.getSimpleProperty(form, "action"))) {
  95.                     Map<String, String> formItems = new HashMap<>();
  96.                     for (Enumeration<String> en = ITrackerResources.getBundle(locale).getKeys(); en.hasMoreElements(); ) {
  97.                         String key = en.nextElement();
  98.                         formItems.put(key, "");
  99.                     }
  100.                     formItems.putAll(items);

  101.                     languageForm.setItems(new TreeMap<>(formItems));
  102.                 } else {
  103.                     String parentLocale = null;

  104.                     if (!locale.equalsIgnoreCase(ITrackerResources.BASE_LOCALE)) {
  105.                         parentLocale = SystemConfigurationUtilities.getLocalePart(locale, SystemConfigurationUtilities.LOCALE_TYPE_LANGUAGE);
  106.                     }
  107.                     langItems = configurationService.getDefinedKeys(parentLocale);

  108.                     Map<String, String> formItems = new HashMap<>();
  109.                     if (log.isDebugEnabled()) {
  110.                         log.debug("putPropertiesKeys: items: " + items);
  111.                     }
  112.                     for (Enumeration<String> en = ITrackerResources.getBundle(locale).getKeys(); en.hasMoreElements(); ) {
  113.                         String key = en.nextElement();
  114.                         formItems.put(key, "");
  115.                     }

  116.                     formItems.putAll(items);

  117.                     languageForm.setItems(new TreeMap<>(formItems));

  118.                 }
  119.                 Locale curLocale = ITrackerResources.getLocale(locale);

  120.                 Language languageItem = new Language(locale, "itracker.locale.name",
  121.                         ITrackerResources.getString("itracker.locale.name", curLocale));// configurationService.getLanguageItemByKey("itracker.locale.name", curLocale);

  122.                 languageForm.setLocaleTitle(languageItem.getResourceValue());
  123.                 languageItem = new Language(locale, "itracker.locale.name",
  124.                         ITrackerResources.getString("itracker.locale.name." + locale, ITrackerResources.BASE_LOCALE));// configurationService.getLanguageItemByKey("itracker.locale.name", curLocale);

  125.                 languageForm.setLocaleBaseTitle(languageItem.getResourceValue());
  126.                 session.setAttribute(Constants.EDIT_LANGUAGE_KEYS_KEY, sortedKeys);
  127.                 session.setAttribute(Constants.EDIT_LANGUAGE_BASE_KEY, baseItems);
  128.                 session.setAttribute(Constants.EDIT_LANGUAGE_LANG_KEY, langItems);
  129.                 session.setAttribute(Constants.EDIT_LANGUAGE_LOC_KEY, locItems);
  130.                 session.setAttribute(Constants.EDIT_LANGUAGE_TYPE_KEY, localeType);
  131.                 request.setAttribute("languageForm", languageForm);
  132.                 if (log.isDebugEnabled()) {
  133.                     log.debug("Locale = " + languageForm.getLocale());
  134.                 }
  135.                 saveToken(request);
  136.                 return mapping.getInputForward();
  137.             }
  138.         } catch (RuntimeException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
  139.             log.error("Exception while creating edit language form.", e);
  140.             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
  141.         }

  142.         if (!errors.isEmpty()) {
  143.             saveErrors(request, errors);
  144.         }

  145.         return mapping.findForward("error");
  146.     }

  147.     @SuppressWarnings("unchecked")
  148.     void putPropertiesKeys(Map<String, String> locItems, String locale) {
  149.         try {
  150.             Hashtable<Object, Object> p;
  151.             try {
  152.                 String path = File.separatorChar + ITrackerResources.RESOURCE_BUNDLE_NAME.replace('.', File.separatorChar) + (null != locale && !(ITrackerResources.BASE_LOCALE.equals(locale)) ? "_" + locale : "") + ".properties";
  153.                 if (log.isDebugEnabled()) {
  154.                     log.debug("putPropertiesKeys: loading: " + path);
  155.                 }
  156.                 p = new PropertiesFileHandler(path).getProperties();
  157.                 p = new Hashtable<>(p);

  158.                 if (log.isDebugEnabled()) {
  159.                     log.debug("putPropertiesKeys: loaded properties: " + p);
  160.                 }
  161.             } catch (Exception e) {
  162.                 if (log.isDebugEnabled()) {
  163.                     log.debug("putPropertiesKeys", e);
  164.                 }
  165.                 p = new Properties();
  166.             }
  167.             // overload properties by loc items from db
  168.             if (log.isDebugEnabled()) {
  169.                 log.debug("putPropertiesKeys: overloading locItems: " + locItems);
  170.             }
  171.             p.putAll(locItems);
  172.             locItems.putAll(Collections.checkedMap((Map) p, String.class, String.class));

  173.         } catch (RuntimeException e) {
  174.             log.error("addPropertiesKeys: caught ", e);
  175.         }
  176.     }
  177. }
  178.