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.project;
20  
21  import java.io.IOException;
22  import java.util.Map;
23  import java.util.Set;
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.log4j.Logger;
31  import org.apache.struts.action.ActionForm;
32  import org.apache.struts.action.ActionForward;
33  import org.apache.struts.action.ActionMapping;
34  import org.apache.struts.action.ActionMessage;
35  import org.apache.struts.action.ActionMessages;
36  import org.itracker.model.Component;
37  import org.itracker.model.PermissionType;
38  import org.itracker.model.Project;
39  import org.itracker.services.ProjectService;
40  import org.itracker.services.util.UserUtilities;
41  import org.itracker.web.actions.base.ItrackerBaseAction;
42  import org.itracker.web.forms.ComponentForm;
43  import org.itracker.web.util.Constants;
44  
45  
46  /**
47   * Action for edit a component entity
48   * 
49   * @author ranks
50   *
51   */
52  public class EditComponentAction extends ItrackerBaseAction {
53  	private static final Logger log = Logger
54  			.getLogger(EditComponentAction.class);
55  
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 (!isTokenValid(request)) {
62  			log.debug("Invalid request token while editing component.");
63  			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
64  			"itracker.web.error.transaction"));
65  			saveErrors(request, errors);
66  			return mapping.findForward("listprojectsadmin");
67  		}
68  		resetToken(request);
69  
70  		Component component = null;
71  		Project project = null;
72  
73  		try {
74  			ComponentForm componentForm = (ComponentForm) form;
75  			ProjectService projectService = getITrackerServices()
76  					.getProjectService();
77  
78  			HttpSession session = request.getSession(true);
79  			Map<Integer, Set<PermissionType>> userPermissionsMap = getUserPermissions(session);
80  
81  			Integer projectId = componentForm.getProjectId();
82  
83  			if (projectId == null) {
84  				errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
85  						"itracker.web.error.invalidproject"));
86  			} else {
87  				project = projectService.getProject(projectId);
88  				if (project == null) {
89  					errors.add(ActionMessages.GLOBAL_MESSAGE,
90  							new ActionMessage(
91  									"itracker.web.error.invalidproject"));
92  				} else {
93  					boolean authorised = UserUtilities.hasPermission(
94  							userPermissionsMap, project.getId(),
95  							UserUtilities.PERMISSION_PRODUCT_ADMIN);
96  
97  					if (!authorised) {
98  						return mapping.findForward("unauthorized");
99  					} else {
100 						String action = (String) request.getParameter("action");
101 						if (log.isDebugEnabled()) {
102 							log.debug("execute: action was " + action);
103 						}
104 
105 						if ("create".equals(action)) {
106 							if (log.isDebugEnabled()) {
107 								log.debug("execute: create new component for " + project);
108 							}
109 							component = new Component(project, componentForm
110 									.getName());
111 							component.setDescription(componentForm
112 									.getDescription());
113 							if (log.isDebugEnabled()) {
114 								log.debug("execute: adding " + component);
115 							}
116 							component = projectService.addProjectComponent(
117 									project.getId(), component);
118 
119 							if (log.isDebugEnabled()) {
120 								log.debug("execute: added new component " + component);
121 							}
122 						} else if ("update".equals(action)) {
123 							
124 							component = projectService
125 									.getProjectComponent(componentForm.getId());
126 							if (log.isDebugEnabled()) {
127 								log.debug("execute: update component " + component);
128 							}
129 							
130 							component.setName(componentForm.getName());
131 							component.setDescription(componentForm
132 									.getDescription());
133 							component.setProject(project);
134 							
135 							if (log.isDebugEnabled()) {
136 								log.debug("execute: updating to " + component);
137 							}
138 							component = projectService
139 									.updateProjectComponent(component);
140 							
141 							
142 						}
143 						session.removeAttribute(Constants.COMPONENT_KEY);
144 						
145 						return new ActionForward(mapping.findForward(
146 								"editproject").getPath()
147 								+ "?id=" + project.getId() + "&action=update");
148 					}
149 				}
150 			}
151 		} catch (RuntimeException ex) {
152 			log.error("Exception processing form data", ex);
153 			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
154 					"itracker.web.error.system"));
155 		}
156 
157 		if (!errors.isEmpty()) {
158 			saveErrors(request, errors);
159 		}
160 		return mapping.findForward("error");
161 	}
162 
163 }