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.configuration;
20  
21  import org.apache.log4j.Logger;
22  import org.apache.struts.action.*;
23  import org.itracker.SystemConfigurationException;
24  import org.itracker.core.resources.ITrackerResources;
25  import org.itracker.model.Configuration;
26  import org.itracker.model.Language;
27  import org.itracker.model.NameValuePair;
28  import org.itracker.model.util.SystemConfigurationUtilities;
29  import org.itracker.services.ConfigurationService;
30  import org.itracker.web.actions.base.ItrackerBaseAction;
31  import org.itracker.web.forms.ConfigurationForm;
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 java.io.IOException;
38  import java.util.*;
39  
40  public class EditConfigurationFormAction extends ItrackerBaseAction {
41      private static final Logger log = Logger
42              .getLogger(EditConfigurationFormAction.class);
43  
44      public ActionForward execute(ActionMapping mapping, ActionForm form,
45                                   HttpServletRequest request, HttpServletResponse response)
46              throws ServletException, IOException {
47          ActionMessages errors = new ActionMessages();
48  
49          if (log.isDebugEnabled()) {
50              log.debug("execute: called");
51          }
52  
53          try {
54              ConfigurationService configurationService = ServletContextUtils.getItrackerServices()
55                      .getConfigurationService();
56  
57              ConfigurationFormitracker/web/forms/ConfigurationForm.html#ConfigurationForm">ConfigurationForm configurationForm = (ConfigurationForm) form;
58              if (configurationForm == null) {
59                  configurationForm = new ConfigurationForm();
60              }
61  
62              String action = configurationForm.getAction();
63  
64  
65              if ("update".equals(action)) {
66                  Integer id = configurationForm.getId();
67                  Configuration configItem = configurationService
68                          .getConfigurationItem(id);
69  
70                  if (configItem == null) {
71                      throw new SystemConfigurationException(
72                              "Invalid configuration item id " + id);
73                  }
74                  configurationForm.setId(id);
75                  configurationForm.setValue(configItem.getValue());
76                  configurationForm.setKey(SystemConfigurationUtilities
77                                                  .getLanguageKey(configItem));
78                  configurationForm.setTypeKey(SystemConfigurationUtilities.getTypeLanguageKey(configItem));
79                  configurationForm.setOrder(configItem.getOrder());
80  
81                  Map<String, String> translations = new TreeMap<>();
82                  List<Language> languageItems = configurationService
83                          .getLanguageItemsByKey(configurationForm.getKey());
84                  Collections.sort(languageItems, Language.KEY_COMPARATOR);
85                  for (Language languageItem : languageItems) {
86                      translations.put(languageItem.getLocale(),
87                              languageItem.getResourceValue());
88                  }
89                  configurationForm.setTranslations(translations);
90              }
91              Map<String, List<String>> languages = configurationService
92                      .getAvailableLanguages();
93              Map<NameValuePair, List<NameValuePair>> languagesNameValuePair = new TreeMap<>(NameValuePair.KEY_COMPARATOR);
94              for (Map.Entry<String, List<String>> entry : languages.entrySet()) {
95                  String language = entry.getKey();
96                  List<String> locales = entry.getValue();
97                  List<NameValuePair> localesNameValuePair = new ArrayList<>();
98                  for (String locale : locales) {
99                      NameValuePair localeNameValuePair = new NameValuePair(
100                             locale, ITrackerResources.getString(
101                             "itracker.locale.name", locale));
102                     localesNameValuePair.add(localeNameValuePair);
103                 }
104                 NameValuePair languageNameValuePair = new NameValuePair(
105                         language, ITrackerResources.getString(
106                         "itracker.locale.name", language));
107                 languagesNameValuePair.put(languageNameValuePair,
108                         localesNameValuePair);
109             }
110 
111             String pageTitleKey = "";
112             String pageTitleArg = "";
113 
114             if (log.isDebugEnabled()) {
115                 log.debug("execute: action was "
116                         + configurationForm.getAction());
117             }
118             if ("update".equals(configurationForm.getAction())) {
119                 pageTitleKey = "itracker.web.admin.editconfiguration.title.update";
120             } else {
121                 Locale locale = getLocale(request);
122                 pageTitleKey = "itracker.web.admin.editconfiguration.title.create";
123                 if ("createseverity".equals(configurationForm.getAction())) {
124                     pageTitleArg = ITrackerResources.getString(
125                             "itracker.web.attr.severity", locale);
126                 } else if ("createstatus".equals(configurationForm.getAction())) {
127                     pageTitleArg = ITrackerResources.getString(
128                             "itracker.web.attr.status", locale);
129                 } else if ("createresolution".equals(configurationForm
130                         .getAction())) {
131                     pageTitleArg = ITrackerResources.getString(
132                             "itracker.web.attr.resolution", locale);
133                 } else {
134                     log.warn("execute: unrecognized action in form: "
135                             + configurationForm.getAction());
136                     return mapping.findForward("unauthorized");
137                 }
138             }
139             request.setAttribute("pageTitleKey", pageTitleKey);
140             request.setAttribute("pageTitleArg", pageTitleArg);
141 
142             configurationForm.setLanguages(languagesNameValuePair);
143 
144             request.setAttribute("configurationForm", configurationForm);
145             saveToken(request);
146 
147             return mapping.getInputForward();
148         } catch (SystemConfigurationException sce) {
149             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
150                     "itracker.web.error.invalidconfiguration"));
151         } catch (Exception e) {
152             log.error("Exception while creating edit configuration form.", e);
153             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
154                     "itracker.web.error.system"));
155         }
156 
157         if (!errors.isEmpty()) {
158             saveErrors(request, errors);
159             return mapping.getInputForward();
160         }
161 
162         return mapping.findForward("error");
163     }
164 
165 }