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