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.PermissionType;
24  import org.itracker.model.Project;
25  import org.itracker.model.Version;
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.VersionForm;
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   * Action for edit a version.
44   *
45   * @author ranks
46   */
47  public class EditVersionAction extends ItrackerBaseAction {
48      private static final Logger log = Logger.getLogger(EditVersionAction.class);
49  
50      public EditVersionAction() {
51      }
52  
53      public ActionForward execute(ActionMapping mapping, ActionForm form,
54                                   HttpServletRequest request, HttpServletResponse response)
55              throws ServletException, IOException {
56          ActionMessages errors = new ActionMessages();
57  
58  
59          if (!isTokenValid(request)) {
60              log.debug("Invalid request token while editing version.");
61              errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
62                      "itracker.web.error.transaction"));
63              saveErrors(request, errors);
64              return mapping.findForward("listprojectsadmin");
65          }
66          resetToken(request);
67  
68          Version version;
69          Project project;
70  
71          try {
72              VersionForm versionForm = (VersionForm) form;
73              ProjectService projectService = ServletContextUtils.getItrackerServices()
74                      .getProjectService();
75  
76              HttpSession session = request.getSession(true);
77              Map<Integer, Set<PermissionType>> userPermissions = RequestHelper.getUserPermissions(session);
78  
79              Integer projectId = versionForm.getProjectId();
80  
81              if (projectId == null) {
82                  errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
83                          "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(
90                                      "itracker.web.error.invalidproject"));
91                  } else {
92                      boolean authorised = UserUtilities.hasPermission(
93                              userPermissions, project.getId(),
94                              UserUtilities.PERMISSION_PRODUCT_ADMIN);
95  
96                      if (!authorised) {
97                          return mapping.findForward("unauthorised");
98                      } else {
99  
100                         String action = (String) request.getParameter("action");
101 
102                         if ("create".equals(action)) {
103                             version = new Version(project, versionForm
104                                     .getNumber());
105                             version
106                                     .setDescription(versionForm
107                                             .getDescription());
108                             version = projectService.addProjectVersion(project
109                                     .getId(), version);
110                         } else if ("update".equals(action)) {
111                             version = projectService
112                                     .getProjectVersion(versionForm.getId());
113                             version.setNumber(versionForm.getNumber());
114                             version.setProject(project);
115                             version
116                                     .setDescription(versionForm
117                                             .getDescription());
118                             version = projectService
119                                     .updateProjectVersion(version);
120                         }
121                         session.removeAttribute(Constants.VERSION_KEY);
122 
123                         return new ActionForward(mapping.findForward(
124                                 "editproject").getPath()
125                                 + "?id=" + project.getId() + "&action=update");
126                     }
127                 }
128             }
129         } catch (RuntimeException ex) {
130             log.error("Exception processing form data", ex);
131             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
132                     "itracker.web.error.system"));
133         }
134 
135         if (!errors.isEmpty()) {
136             saveErrors(request, errors);
137         }
138         return mapping.findForward("error");
139     }
140 
141 }