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.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              // Move to validation code
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