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