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.Map;
26  
27  import javax.servlet.ServletException;
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  import javax.servlet.http.HttpSession;
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.CustomField;
40  import org.itracker.model.CustomFieldValue;
41  import org.itracker.model.Language;
42  import org.itracker.model.NameValuePair;
43  import org.itracker.services.ConfigurationService;
44  import org.itracker.services.exceptions.SystemConfigurationException;
45  import org.itracker.services.util.CustomFieldUtilities;
46  import org.itracker.services.util.UserUtilities;
47  import org.itracker.web.actions.base.ItrackerBaseAction;
48  import org.itracker.web.forms.CustomFieldValueForm;
49  import org.itracker.web.util.Constants;
50  
51  public class EditCustomFieldValueFormAction extends ItrackerBaseAction {
52  	private static final Logger log = Logger
53  			.getLogger(EditCustomFieldValueFormAction.class);
54  
55  	public ActionForward execute(ActionMapping mapping, ActionForm form,
56  			HttpServletRequest request, HttpServletResponse response)
57  			throws ServletException, IOException {
58  		ActionMessages errors = new ActionMessages();
59  		//  TODO: Action Cleanup
60  
61  		if (!hasPermission(UserUtilities.PERMISSION_USER_ADMIN, request,
62  				response)) {
63  			return mapping.findForward("unauthorized");
64  		}
65  		ConfigurationService configurationService = getITrackerServices()
66  		.getConfigurationService();
67  
68  		try {
69  			
70  			// TODO: it looks like to following 3 lines can be removed, we
71  			// comment them and add a task.
72  			HttpSession session = request.getSession(true);
73  			Map<String, List<String>> languages = configurationService
74  					.getAvailableLanguages();
75  
76  			CustomFieldValueForm customFieldValueForm = (CustomFieldValueForm) form;
77  
78  			if (customFieldValueForm == null) {
79  				customFieldValueForm = new CustomFieldValueForm();
80  			}
81  
82  			CustomFieldValue customFieldValue = new CustomFieldValue();
83  
84  			String action = customFieldValueForm.getAction();
85  
86  			if ("update".equals(action)) {
87  				Integer id = customFieldValueForm.getId();
88  				customFieldValue = configurationService.getCustomFieldValue(id);
89  				if (customFieldValue == null) {
90  					throw new SystemConfigurationException(
91  							"Invalid custom field value id " + id);
92  				}
93  //				String name = CustomFieldUtilities.getCustomFieldOptionName(
94  //						customFieldValue.getCustomField().getId(), id);
95  //				customFieldValue.setName(name);
96  
97  				customFieldValueForm.setId(id);
98  				customFieldValueForm.setValue(customFieldValue.getValue());
99  
100 				customFieldValueForm.setSortOrder(customFieldValue.getSortOrder());
101 
102 				HashMap<String, String> translations = new HashMap<String, String>();
103 				List<Language> languageItems = configurationService
104 						.getLanguageItemsByKey(CustomFieldUtilities
105 								.getCustomFieldOptionLabelKey(customFieldValue
106 										.getCustomField().getId(),
107 										customFieldValue.getId()));
108 
109 				for (int i = 0; i < languageItems.size(); i++) {
110 					translations.put(languageItems.get(i).getLocale(),
111 							languageItems.get(i).getResourceValue());
112 				}
113 				customFieldValueForm.setTranslations(translations);
114 			}
115 			CustomField field = (CustomField) session
116 					.getAttribute(Constants.CUSTOMFIELD_KEY);
117 			if (field == null) {
118 				return mapping.findForward("unauthorized");
119 
120 			} else {
121 	            String pageTitleKey = "";
122 	            String pageTitleArg = "";
123 	            pageTitleKey = "itracker.web.admin.editcustomfield.title.create";
124 	            if (action == "update") {
125 	            	 pageTitleKey = "itracker.web.admin.editcustomfield.title.update";
126 	            }
127 	            
128 	            request.setAttribute("languages", configurationService.getAvailableLanguages());
129 	            request.setAttribute("pageTitleKey", pageTitleKey); 
130 	            request.setAttribute("pageTitleArg", pageTitleArg);    
131 				
132 				request.setAttribute("languages", languages);
133 				request.setAttribute("customFieldValueForm", customFieldValueForm);
134 				request.setAttribute("action", action);
135 				session.setAttribute(Constants.CUSTOMFIELDVALUE_KEY,
136 						customFieldValue);
137 				session.setAttribute("field", field);
138 				saveToken(request);
139 				setRequestEnvironment(request,configurationService);
140 				return mapping.getInputForward();
141 			}
142 
143 			
144 		} catch (SystemConfigurationException sce) {
145 			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
146 					"itracker.web.error.invalidcustomfieldvalue"));
147 		} catch (Exception e) {
148 			log.error("Exception while creating edit custom field value form.",
149 					e);
150 			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
151 					"itracker.web.error.system"));
152 		}
153 		if (!errors.isEmpty()) {
154 			saveErrors(request, errors);
155 			setRequestEnvironment(request,configurationService);
156 			return mapping.getInputForward();
157 		}
158 
159 		return mapping.findForward("error");
160 	}
161 	public static void setRequestEnvironment(HttpServletRequest request,ConfigurationService configurationService){
162 		Map<String, List<String>> languages = configurationService.getAvailableLanguages();
163 		Map<NameValuePair,List<NameValuePair>> languagesNameValuePair = new HashMap<NameValuePair, List<NameValuePair>>();
164 		for (Map.Entry<String, List<String>> entry : languages.entrySet()) {
165 			String language = entry.getKey();
166 			List<String> locales = entry.getValue();
167 //			System.out.println(language);
168 //			System.out.println(locales);
169 			List<NameValuePair> localesNameValuePair = new ArrayList<NameValuePair>();
170 			for (String locale : locales) {
171 				NameValuePair localeNameValuePair = new NameValuePair(locale,ITrackerResources.getString("itracker.locale.name", locale));
172 				localesNameValuePair.add(localeNameValuePair);
173 			}
174 			NameValuePair languageNameValuePair = new NameValuePair(language,ITrackerResources.getString("itracker.locale.name", language));
175 			languagesNameValuePair.put(languageNameValuePair, localesNameValuePair);
176 		}
177 		request.setAttribute("languagesNameValuePair", languagesNameValuePair);
178 		request.setAttribute("baseLocale",ITrackerResources.BASE_LOCALE);
179 	}
180 
181 }