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.lang.reflect.InvocationTargetException;
23  import java.util.Arrays;
24  import java.util.HashSet;
25  
26  import java.util.Map;
27  import java.util.Set;
28  
29  import javax.servlet.ServletException;
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.http.HttpServletResponse;
32  import javax.servlet.http.HttpSession;
33  
34  import org.apache.commons.beanutils.PropertyUtils;
35  import org.apache.log4j.Logger;
36  import org.apache.struts.action.ActionForm;
37  import org.apache.struts.action.ActionForward;
38  import org.apache.struts.action.ActionMapping;
39  import org.apache.struts.action.ActionMessage;
40  import org.apache.struts.action.ActionMessages;
41  import org.itracker.model.PermissionType;
42  import org.itracker.model.Project;
43  import org.itracker.model.User;
44  import org.itracker.services.ProjectService;
45  import org.itracker.services.UserService;
46  import org.itracker.services.util.UserUtilities;
47  import org.itracker.web.actions.base.ItrackerBaseAction;
48  import org.itracker.web.util.Constants;
49  import org.itracker.web.util.LoginUtilities;
50  
51  public class EditProjectAction extends ItrackerBaseAction {
52  	private static final Logger log = Logger.getLogger(EditProjectAction.class);
53  
54  	public ActionForward execute(ActionMapping mapping, ActionForm form,
55  			HttpServletRequest request, HttpServletResponse response)
56  			throws ServletException, IOException {
57  
58  		ActionMessages errors = new ActionMessages();
59  
60  		if (!isTokenValid(request)) {
61  			log.debug("Invalid request token while editing project.");
62  			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
63  					"itracker.web.error.transaction"));
64  			saveErrors(request, errors);
65  			saveToken(request);
66  			return mapping.getInputForward();
67  			// return mapping.findForward("listprojectsadmin");
68  
69  		}
70  		resetToken(request);
71  
72  		Project project = null;
73  		try {
74  			ProjectService projectService = getITrackerServices()
75  					.getProjectService();
76  			UserService userService = getITrackerServices().getUserService();
77  
78  			HttpSession session = request.getSession(true);
79  			User user = LoginUtilities.getCurrentUser(request);
80  
81  			String action = (String) request.getParameter("action");
82  
83  			if ("update".equals(action)) {
84  
85  				Map<Integer, Set<PermissionType>> userPermissions = getUserPermissions(session);
86  
87  				project = projectService.getProject((Integer) PropertyUtils
88  						.getSimpleProperty(form, "id"));
89  				if (!UserUtilities.hasPermission(userPermissions, project
90  						.getId(), UserUtilities.PERMISSION_PRODUCT_ADMIN)) {
91  					return mapping.findForward("unauthorized");
92  				}
93  				AdminProjectUtilities.setFormProperties(project,
94  						projectService, form, errors);
95  				if (!errors.isEmpty()) {
96  					saveErrors(request, errors);
97  					return mapping.getInputForward();
98  				} else {
99  					Integer[] ownersArray = (Integer[]) PropertyUtils
100 							.getSimpleProperty(form, "owners");
101 					Set<Integer> ownerIds = null == ownersArray ? new HashSet<Integer>()
102 							: new HashSet<Integer>(Arrays.asList(ownersArray));
103 					AdminProjectUtilities.updateProjectOwners(project,
104 							ownerIds, projectService, userService);
105 
106 					if (log.isDebugEnabled()) {
107 						log.debug("execute: updating existing project: "
108 								+ project);
109 					}
110 					project = projectService.updateProject(project, user
111 							.getId());
112 				}
113 			} else if ("create".equals(action)) {
114 				if (!user.isSuperUser()) {
115 					return mapping.findForward("unauthorized");
116 				}
117 
118 				project = new Project();
119 				AdminProjectUtilities.setFormProperties(project,
120 						projectService, form, errors);
121 				if (!errors.isEmpty()) {
122 					saveErrors(request, errors);
123 					return mapping.getInputForward();
124 				}
125 				project = projectService.createProject(project, user.getId());
126 
127 				if (log.isDebugEnabled()) {
128 					log.debug("execute: created new project: " + project);
129 				}
130 
131 				Integer[] users = (Integer[]) PropertyUtils.getSimpleProperty(
132 						form, "users");
133 				if (users != null) {
134 					// get the initial project members from create-form
135 					Set<Integer> userIds = new HashSet<Integer>(Arrays
136 							.asList(users));
137 					// get the permissions-set for initial project members
138 					Integer[] permissionArray = (Integer[]) PropertyUtils
139 							.getSimpleProperty(form, "permissions");
140 					Set<Integer> permissions = null == permissionArray ? new HashSet<Integer>(
141 							0)
142 							: new HashSet<Integer>(Arrays
143 									.asList(permissionArray));
144 
145 					Integer[] ownersArray = (Integer[]) PropertyUtils
146 							.getSimpleProperty(form, "owners");
147 					Set<Integer> ownerIds = null == ownersArray ? new HashSet<Integer>()
148 							: new HashSet<Integer>(Arrays.asList(ownersArray));
149 
150 					// if admin-permission is selected, all permissions will be
151 					// granted and users added as project owners
152 					if (permissions
153 							.contains(UserUtilities.PERMISSION_PRODUCT_ADMIN)) {
154 						ownerIds.addAll(userIds);
155 					} else {
156 						// handle special initial user-/permissions-set
157 						AdminProjectUtilities.handleInitialProjectMembers(
158 								project, userIds, permissions, projectService,
159 								userService);
160 					}
161 
162 					// set project owners with all permissions
163 					AdminProjectUtilities.updateProjectOwners(project,
164 							ownerIds, projectService, userService);
165 				}
166 
167 				if (log.isDebugEnabled()) {
168 					log.debug("execute: updating new project: " + project);
169 				}
170 				session.removeAttribute(Constants.PROJECT_KEY);
171 			}
172 		} catch (RuntimeException e) {
173 			log.error("execute: Exception processing form data", e);
174 			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
175 					"itracker.web.error.system"));
176 		} catch (IllegalAccessException e) {
177 			log.error("execute: Exception processing form data", e);
178 			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
179 					"itracker.web.error.system"));
180 		} catch (InvocationTargetException e) {
181 			log.error("execute: Exception processing form data", e);
182 			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
183 					"itracker.web.error.system"));
184 		} catch (NoSuchMethodException e) {
185 			log.error("execute: Exception processing form data", e);
186 			errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
187 					"itracker.web.error.system"));
188 		}
189 
190 		if (!errors.isEmpty()) {
191 			saveErrors(request, errors);
192 			if (log.isDebugEnabled()) {
193 				log.debug("execute: got errors in action-messages: " + errors);
194 			}
195 			return mapping.findForward("error");
196 		}
197 
198 		return mapping.findForward("listprojectsadmin");
199 	}
200 
201 }