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.HashMap;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import javax.servlet.ServletException;
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  import javax.servlet.http.HttpSession;
30  
31  import org.apache.commons.beanutils.PropertyUtils;
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.services.ConfigurationService;
43  import org.itracker.services.exceptions.SystemConfigurationException;
44  import org.itracker.services.util.CustomFieldUtilities;
45  import org.itracker.services.util.SystemConfigurationUtilities;
46  import org.itracker.web.actions.base.ItrackerBaseAction;
47  import org.itracker.web.forms.CustomFieldForm;
48  import org.itracker.web.util.Constants;
49  
50  public class EditCustomFieldAction extends ItrackerBaseAction {
51  	private static final Logger log = Logger.getLogger(EditCustomFieldAction.class);
52  
53  
54  	/* (non-Javadoc)
55  	 * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
56  	 */
57  	@SuppressWarnings("unchecked")
58  	@Override
59  	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
60  		ActionMessages errors = new ActionMessages();
61  
62  		if(! isTokenValid(request)) {
63  			log.debug("Invalid request token while editing configuration.");
64  			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
65  			"itracker.web.error.transaction"));
66  			saveErrors(request, errors);
67  			saveToken(request);
68  			return mapping.getInputForward();
69  		}
70  		resetToken(request);
71  		try {
72  			ConfigurationService configurationService = getITrackerServices().getConfigurationService();
73  			String action = (String) PropertyUtils.getSimpleProperty(form, "action");
74  			if(action == null) {
75  				return mapping.findForward("listconfiguration");
76  			}
77  			CustomFieldForm customFieldForm = (CustomFieldForm) form;
78  
79  			CustomField customField = null;
80  			if("create".equals(action)) {
81  				customField = new CustomField();
82  //				customField.setName(" ");
83  				customField.setFieldType(CustomField.Type.valueOf(customFieldForm.getFieldType()));
84  				customField.setRequired(("true".equals((String) PropertyUtils.getSimpleProperty(form, "required")) ? true : false));
85  				customField.setSortOptionsByName(("true".equals((String) PropertyUtils.getSimpleProperty(form, "sortOptionsByName")) ? true : false));
86  				customField.setDateFormat((String) PropertyUtils.getSimpleProperty(form, "dateFormat"));
87  				customField = configurationService.createCustomField(customField);
88  			} else if("update".equals(action)) {
89  				Integer id = (Integer) PropertyUtils.getSimpleProperty(form, "id");
90  				customField = configurationService.getCustomField(id);
91  				if(customField == null) {
92  					throw new SystemConfigurationException("Invalid custom field id " + id);
93  				}
94  				List<CustomFieldValue> customFieldValues = customField.getOptions();
95  //				customField.setName(CustomFieldUtilities.getCustomFieldName(id));
96  				customField.setFieldType(CustomField.Type.valueOf(customFieldForm.getFieldType()));
97  				customField.setRequired(("true".equals((String) PropertyUtils.getSimpleProperty(form, "required")) ? true : false));
98  				customField.setSortOptionsByName(("true".equals((String) PropertyUtils.getSimpleProperty(form, "sortOptionsByName")) ? true : false));
99  				customField.setDateFormat((String) PropertyUtils.getSimpleProperty(form, "dateFormat"));
100 				customField.setOptions(customFieldValues);
101 				customField = configurationService.updateCustomField(customField);
102 			} else {
103 				throw new SystemConfigurationException("Invalid action " + action + " while editing custom field.");
104 			}
105 
106 			if (customField == null) {
107 				throw new SystemConfigurationException(
108 						"Unable to create new custom field model.");
109 			}
110 
111 			HashMap<String, String> translations = (HashMap<String, String>) PropertyUtils
112 					.getSimpleProperty(form, "translations");
113 			String key = CustomFieldUtilities
114 					.getCustomFieldLabelKey(customField.getId());
115 			log.debug("Processing label translations for custom field "
116 					+ customField.getId() + " with key " + key);
117 			if (translations != null && key != null && !key.equals("")) {
118 				configurationService.removeLanguageKey(key);
119 				Iterator<String> iter = translations.keySet().iterator();
120 				while (iter.hasNext()) {
121 					String locale = (String) iter.next();
122 					if (locale != null) {
123 						String translation = (String) translations.get(locale);
124 						if (translation != null && !translation.trim().equals("")) {
125 							log.debug("Adding new translation for locale "
126 									+ locale + " for "
127 									+ customField.getId());
128 							configurationService
129 									.updateLanguageItem(new Language(locale,
130 											key, translation));
131 						}
132 					}
133 				}
134 			}
135 			if ( key != null ) {
136 				ITrackerResources.clearKeyFromBundles(key, true);
137 			}
138 			try {
139 			// Now reset the cached versions in IssueUtilities
140 				configurationService.resetConfigurationCache(SystemConfigurationUtilities.TYPE_CUSTOMFIELD);
141 			} catch (Exception e) {
142 				log.info("execute: resetConfigurationCache trowed exception, caught", e);
143 			}
144 
145 			HttpSession session = request.getSession();
146 			if(customField.getFieldType() == CustomField.Type.LIST && "create".equals(action) ) { 
147 				session.setAttribute(Constants.CUSTOMFIELD_KEY, customField);
148 				EditCustomFieldActionUtil.setRequestEnv(request, customFieldForm);	
149 				saveToken(request);
150 				return new ActionForward(mapping.findForward("editcustomfield").getPath() + "?id=" + customField.getId() + "&action=update");
151 			}
152 			
153 			session.removeAttribute(Constants.CUSTOMFIELD_KEY);
154 		} catch(SystemConfigurationException sce) {
155 			log.error("Exception processing form data: " + sce.getMessage(), sce);
156 			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(sce.getKey()));
157 		} catch(Exception e) {
158 			log.error("Exception processing form data", e);
159 			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
160 		}
161 
162 		if(! errors.isEmpty()) {			
163 			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
164 					"itracker.web.error.transaction"));
165 			saveErrors(request, errors);
166 			return mapping.findForward("error");
167 		}
168 		
169 		return mapping.findForward("listconfiguration");		
170 	}
171 }
172