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.List;
24  
25  import javax.servlet.ServletException;
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  import javax.servlet.http.HttpSession;
29  
30  import org.apache.commons.beanutils.PropertyUtils;
31  import org.apache.log4j.Logger;
32  import org.apache.struts.action.ActionForm;
33  import org.apache.struts.action.ActionForward;
34  import org.apache.struts.action.ActionMapping;
35  import org.apache.struts.action.ActionMessage;
36  import org.apache.struts.action.ActionMessages;
37  import org.itracker.model.CustomField;
38  import org.itracker.model.Language;
39  import org.itracker.services.ConfigurationService;
40  import org.itracker.services.exceptions.SystemConfigurationException;
41  import org.itracker.services.util.CustomFieldUtilities;
42  import org.itracker.services.util.UserUtilities;
43  import org.itracker.web.actions.base.ItrackerBaseAction;
44  import org.itracker.web.forms.CustomFieldForm;
45  import org.itracker.web.util.Constants;
46  
47  //  TODO: Action Cleanup
48  
49  public class EditCustomFieldFormAction extends ItrackerBaseAction {
50  	private static final Logger log = Logger.getLogger(EditCustomFieldFormAction.class);
51  
52  	/* (non-Javadoc)
53  	 * @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)
54  	 */
55  	@Override
56  	public ActionForward execute(ActionMapping mapping, ActionForm form,
57  			HttpServletRequest request, HttpServletResponse response)
58  	throws ServletException, IOException {
59  		ActionMessages errors = new ActionMessages();
60  
61  		if (! hasPermission(UserUtilities.PERMISSION_USER_ADMIN, request, response)) {
62  			return mapping.findForward("unauthorized");
63  		}
64  
65  		try {
66  
67  			HttpSession session = request.getSession(true);
68  
69  			CustomFieldForm customFieldForm = (CustomFieldForm) form;			
70  			if (customFieldForm == null) 
71  				customFieldForm = new CustomFieldForm();
72  
73  			String action = (String) PropertyUtils.getSimpleProperty(customFieldForm, "action");
74  			CustomField customField = null;
75  			ConfigurationService configurationService = getITrackerServices().getConfigurationService();
76  			if ("create".equals(action)) {
77  				customField = new CustomField();
78  			} else if ("update".equals(action)) {
79  				Integer id = (Integer) PropertyUtils.getSimpleProperty(customFieldForm, "id");
80  				customField = configurationService.getCustomField(id);
81  				if (customField == null) 
82  					throw new SystemConfigurationException("Invalid custom field id " + id);
83  				
84  				customFieldForm.setId(id);
85  				customFieldForm.setFieldType(customField.getFieldType().getCode());
86  				customFieldForm.setRequired(Boolean.toString(customField.isRequired()));
87  				customFieldForm.setDateFormat(customField.getDateFormat());
88  				customFieldForm.setSortOptionsByName(Boolean.toString(customField.isSortOptionsByName()));
89  
90  				HashMap<String, String> translations = new HashMap<String, String>();
91  				List<Language> languageItems = configurationService.getLanguageItemsByKey(CustomFieldUtilities.getCustomFieldLabelKey(customField.getId()));
92  				for (int i = 0; i < languageItems.size(); i++) {
93  					translations.put(languageItems.get(i).getLocale(), languageItems.get(i).getResourceValue());
94  				}
95  				customFieldForm.setTranslations(translations);
96  //				customField.setLabels(locale);
97  			}
98  			
99  			/*
100 			 * Check whether customField is null or not, if null redirect the
101 			 * page to error, otherwise put required objects in request
102 			 * object to render output
103 			 */
104 			if (customField == null) {			
105 				log.error("EditCustomFieldFormAction#execute: customField was null!");
106 				errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidproject"));
107 				return mapping.findForward("error");
108 			} 
109 			session.setAttribute(Constants.CUSTOMFIELD_KEY, customField);
110 			EditCustomFieldActionUtil.setRequestEnv(request, customFieldForm);	
111 			saveToken(request);
112 
113 		} catch (SystemConfigurationException sce) {
114 			log.error("execute: Exception system configuration exception caught.", sce);
115 			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
116 			"itracker.web.error.invalidcustomfield"));
117 		} catch (Exception e) {
118 			log.error("execute: Exception while creating edit custom field form.", e);
119 			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
120 			"itracker.web.error.system"));
121 		}
122 		
123 		if (! errors.isEmpty()) {
124 			saveErrors(request, errors);
125 			return mapping.findForward("error");
126 		}
127 
128 		return mapping.getInputForward();			
129 	}
130 
131 
132 }