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.Component;
25  import org.itracker.model.PermissionType;
26  import org.itracker.model.Project;
27  import org.itracker.model.util.UserUtilities;
28  import org.itracker.services.ProjectService;
29  import org.itracker.web.actions.base.ItrackerBaseAction;
30  import org.itracker.web.forms.ComponentForm;
31  import org.itracker.web.util.Constants;
32  import org.itracker.web.util.EditComponentFormActionUtil;
33  import org.itracker.web.util.ServletContextUtils;
34  
35  import javax.servlet.ServletException;
36  import javax.servlet.http.HttpServletRequest;
37  import javax.servlet.http.HttpServletResponse;
38  import javax.servlet.http.HttpSession;
39  import java.io.IOException;
40  import java.util.Map;
41  import java.util.Set;
42  
43  
44  public class EditComponentFormAction extends ItrackerBaseAction {
45      private static final Logger log = Logger.getLogger(EditComponentFormAction.class);
46  
47  
48      @SuppressWarnings("unchecked")
49      public ActionForward execute(ActionMapping mapping,
50                                   ActionForm form,
51                                   HttpServletRequest request,
52                                   HttpServletResponse response)
53              throws ServletException, IOException {
54          ActionMessages errors = new ActionMessages();
55  
56          String pageTitleKey = "";
57          String pageTitleArg = "";
58  
59  
60          try {
61              ProjectService projectService = ServletContextUtils.getItrackerServices().getProjectService();
62  
63              HttpSession session = request.getSession(true);
64              String action = request.getParameter("action");
65              Map<Integer, Set<PermissionType>> userPermissions = (Map<Integer, Set<PermissionType>>) session.getAttribute(Constants.PERMISSIONS_KEY);
66              Component component = (Component) session.getAttribute(Constants.COMPONENT_KEY);
67              Project project;
68              ComponentForm/../org/itracker/web/forms/ComponentForm.html#ComponentForm">ComponentForm componentForm = (ComponentForm) form;
69  
70              if (componentForm == null) {
71                  componentForm = new ComponentForm();
72              }
73  
74              if ("create".equals(action)) {
75                  Integer projectId = (Integer) PropertyUtils.getSimpleProperty(form, "projectId");
76  
77                  if (action != null && action.equals("create")) {
78                      pageTitleKey = "itracker.web.admin.editcomponent.title.create";
79                  }
80  
81                  if (projectId == null) {
82                      errors.add(ActionMessages.GLOBAL_MESSAGE,
83                              new ActionMessage("itracker.web.error.invalidproject"));
84                  } else {
85                      project = projectService.getProject(projectId);
86  
87                      if (project == null) {
88                          errors.add(ActionMessages.GLOBAL_MESSAGE,
89                                  new ActionMessage("itracker.web.error.invalidproject"));
90                      } else if (!UserUtilities.hasPermission(userPermissions,
91                              project.getId(), UserUtilities.PERMISSION_PRODUCT_ADMIN)) {
92                          return mapping.findForward("unauthorized");
93                      } else {
94                          component = new Component();
95                          component.setProject(project);
96                          componentForm.setAction("create");
97                          componentForm.setId(component.getId());
98                          componentForm.setProjectId(component.getProject().getId());
99                      }
100                 }
101             } else if ("update".equals(action)) {
102                 Integer componentId = (Integer) PropertyUtils.getSimpleProperty(form, "id");
103                 component = projectService.getProjectComponent(componentId);
104                 if (action != null && action.equals("update")) {
105                     pageTitleKey = "itracker.web.admin.editcomponent.title.update";
106                     pageTitleArg = component.getName();
107                 }
108                 if (component == null) {
109                     errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidcomponent"));
110                 } else {
111                     project = component.getProject();
112                     if (component == null) {
113                         errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidproject"));
114                     } else if (!UserUtilities.hasPermission(userPermissions, component.getProject().getId(), UserUtilities.PERMISSION_PRODUCT_ADMIN)) {
115                         return mapping.findForward("unauthorized");
116                     } else {
117                         componentForm.setAction("update");
118                         componentForm.setId(component.getId());
119 
120                         componentForm.setProjectId(project.getId());
121                         componentForm.setName(component.getName());
122                         componentForm.setDescription(component.getDescription());
123                     }
124                 }
125             } else {
126                 errors.add(ActionMessages.GLOBAL_MESSAGE,
127                         new ActionMessage("itracker.web.error.invalidaction"));
128             }
129 
130             if (errors.isEmpty()) {
131                 request.setAttribute("componentForm", componentForm);
132                 session.setAttribute(Constants.COMPONENT_KEY, component);
133                 saveToken(request);
134                 request.setAttribute("pageTitleKey", pageTitleKey);
135                 request.setAttribute("pageTitleArg", pageTitleArg);
136                 ActionForward af = EditComponentFormActionUtil.init(mapping, request);
137                 if (af != null) return af;
138                 return mapping.getInputForward();
139             }
140         } catch (Exception e) {
141             pageTitleKey = "itracker.web.error.title";
142 
143             request.setAttribute("pageTitleKey", pageTitleKey);
144             request.setAttribute("pageTitleArg", pageTitleArg);
145 
146             log.error("Exception while creating edit component form.", e);
147             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
148         }
149 
150         if (!errors.isEmpty()) {
151             saveErrors(request, errors);
152 
153             return mapping.findForward("error");
154         }
155         return mapping.getInputForward();
156     }
157 
158 }
159