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 java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Locale;
26  import java.util.Map;
27  
28  import javax.servlet.ServletException;
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  
32  import org.apache.log4j.Logger;
33  import org.apache.struts.action.ActionForm;
34  import org.apache.struts.action.ActionForward;
35  import org.apache.struts.action.ActionMapping;
36  import org.apache.struts.action.ActionMessage;
37  import org.apache.struts.action.ActionMessages;
38  import org.itracker.core.resources.ITrackerResources;
39  import org.itracker.model.Configuration;
40  import org.itracker.model.Language;
41  import org.itracker.model.NameValuePair;
42  import org.itracker.services.ConfigurationService;
43  import org.itracker.services.exceptions.SystemConfigurationException;
44  import org.itracker.services.util.SystemConfigurationUtilities;
45  import org.itracker.services.util.UserUtilities;
46  import org.itracker.web.actions.base.ItrackerBaseAction;
47  import org.itracker.web.forms.ConfigurationForm;
48  import org.itracker.web.util.LoginUtilities;
49  
50  public class EditConfigurationFormAction extends ItrackerBaseAction {
51  	private static final Logger log = Logger
52  			.getLogger(EditConfigurationFormAction.class);
53  
54  	public ActionForward execute(ActionMapping mapping, ActionForm form,
55  			HttpServletRequest request, HttpServletResponse response)
56  			throws ServletException, IOException {
57  		ActionMessages errors = new ActionMessages();
58  
59  		if (log.isDebugEnabled()) {
60  			log.debug("execute: called");
61  		}
62  		if (!hasPermission(UserUtilities.PERMISSION_USER_ADMIN, request,
63  				response)) {
64  			log.warn("execute: user has not permissue user-admin: " + LoginUtilities.getCurrentUser(request) + ", forwarding to unauthorized!");
65  			
66  			return mapping.findForward("unauthorized");
67  		}
68  
69  		try {
70  			ConfigurationService configurationService = getITrackerServices()
71  					.getConfigurationService();
72  
73  			ConfigurationForm configurationForm = (ConfigurationForm) form;
74  			if (configurationForm == null) {
75  				configurationForm = new ConfigurationForm();
76  			}
77  
78  			String action = configurationForm.getAction();
79  
80  			String formValue = configurationForm.getValue();
81  
82  			if ("update".equals(action)) {
83  				Integer id = configurationForm.getId();
84  				formValue = String.valueOf(id);
85  				Configuration configItem = configurationService
86  						.getConfigurationItem(id);
87  
88  				if (configItem == null) {
89  					throw new SystemConfigurationException(
90  							"Invalid configuration item id " + id);
91  				}
92  				configurationForm.setId(id);
93  
94  				if (configItem.getType() == SystemConfigurationUtilities.TYPE_STATUS) {
95  					configurationForm.setValue(configItem.getValue());
96  				}
97  
98  				HashMap<String, String> translations = new HashMap<String, String>();
99  				List<Language> languageItems = configurationService
100 						.getLanguageItemsByKey(SystemConfigurationUtilities
101 								.getLanguageKey(configItem));
102 
103 				for (int i = 0; i < languageItems.size(); i++) {
104 					translations.put(languageItems.get(i).getLocale(),
105 							languageItems.get(i).getResourceValue());
106 				}
107 				configurationForm.setTranslations(translations);
108 			}
109 			Map<String, List<String>> languages = configurationService
110 					.getAvailableLanguages();
111 			Map<NameValuePair, List<NameValuePair>> languagesNameValuePair = new HashMap<NameValuePair, List<NameValuePair>>();
112 			for (Map.Entry<String, List<String>> entry : languages.entrySet()) {
113 				String language = entry.getKey();
114 				List<String> locales = entry.getValue();
115 				List<NameValuePair> localesNameValuePair = new ArrayList<NameValuePair>();
116 				for (String locale : locales) {
117 					NameValuePair localeNameValuePair = new NameValuePair(
118 							locale, ITrackerResources.getString(
119 									"itracker.locale.name", locale));
120 					localesNameValuePair.add(localeNameValuePair);
121 				}
122 				NameValuePair languageNameValuePair = new NameValuePair(
123 						language, ITrackerResources.getString(
124 								"itracker.locale.name", language));
125 				languagesNameValuePair.put(languageNameValuePair,
126 						localesNameValuePair);
127 			}
128 			String baseLocaleKey = "translations("
129 					+ ITrackerResources.BASE_LOCALE + ")";
130 
131 			String pageTitleKey = "";
132 			String pageTitleArg = "";
133 			boolean isUpdate = false;
134 
135 			if (log.isDebugEnabled()) {
136 				log.debug("execute: action was "
137 						+ configurationForm.getAction());
138 			}
139 			if ("update".equals(configurationForm.getAction())) {
140 				isUpdate = true;
141 				pageTitleKey = "itracker.web.admin.editconfiguration.title.update";
142 			} else {
143 				Locale locale = getLocale(request);
144 				pageTitleKey = "itracker.web.admin.editconfiguration.title.create";
145 				if ("createseverity".equals(configurationForm.getAction())) {
146 					pageTitleArg = ITrackerResources.getString(
147 							"itracker.web.attr.severity", locale);
148 				} else if ("createstatus".equals(configurationForm.getAction())) {
149 					pageTitleArg = ITrackerResources.getString(
150 							"itracker.web.attr.status", locale);
151 				} else if ("createresolution".equals(configurationForm
152 						.getAction())) {
153 					pageTitleArg = ITrackerResources.getString(
154 							"itracker.web.attr.resolution", locale);
155 				} else {
156 					log.warn("execute: unrecognized action in form: "
157 							+ configurationForm.getAction());
158 					return mapping.findForward("unauthorized");
159 				}
160 			}
161 			request.setAttribute("isUpdate", Boolean.valueOf(isUpdate));
162 			request.setAttribute("pageTitleKey", pageTitleKey);
163 			request.setAttribute("pageTitleArg", pageTitleArg);
164 
165 			request.setAttribute("languagesNameValuePair",
166 					languagesNameValuePair);
167 			request.setAttribute("sc", configurationService);
168 			request.setAttribute("configurationForm", configurationForm);
169 			request.setAttribute("action", action);
170 			request.setAttribute("value", formValue);
171 			request.setAttribute("baseLocaleKey", baseLocaleKey);
172 			saveToken(request);
173 
174 			return mapping.getInputForward();
175 		} catch (SystemConfigurationException sce) {
176 			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
177 					"itracker.web.error.invalidconfiguration"));
178 		} catch (Exception e) {
179 			log.error("Exception while creating edit configuration form.", e);
180 			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
181 					"itracker.web.error.system"));
182 		}
183 
184 		if (!errors.isEmpty()) {
185 			saveErrors(request, errors);
186 			return mapping.getInputForward();
187 		}
188 
189 		return mapping.findForward("error");
190 	}
191 
192 }