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