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.Version;
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.VersionForm;
31  import org.itracker.web.util.Constants;
32  import org.itracker.web.util.EditVersionFormActionUtil;
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  public class EditVersionFormAction extends ItrackerBaseAction {
44      private static final Logger log = Logger.getLogger(EditVersionFormAction.class);
45  
46      @SuppressWarnings("unchecked")
47      public ActionForward execute(ActionMapping mapping,
48                                   ActionForm form,
49                                   HttpServletRequest request,
50                                   HttpServletResponse response)
51              throws ServletException, IOException {
52  
53          ActionMessages errors = new ActionMessages();
54  
55          String pageTitleKey = "";
56          String pageTitleArg = "";
57  
58  
59          try {
60              ProjectService projectService = ServletContextUtils.getItrackerServices().getProjectService();
61  
62              HttpSession session = request.getSession(true);
63              String action = request.getParameter("action");
64              Map<Integer, Set<PermissionType>> userPermissions = (Map<Integer, Set<PermissionType>>) session.getAttribute(Constants.PERMISSIONS_KEY);
65  
66              Version version;
67              version = (Version) session.getAttribute(Constants.VERSION_KEY);
68  
69              Project project;
70  
71              VersionForm./../../org/itracker/web/forms/VersionForm.html#VersionForm">VersionForm versionForm = (VersionForm) form;
72              if (versionForm == null) {
73                  versionForm = new VersionForm();
74              }
75  
76              Integer projectId = (Integer) PropertyUtils.getSimpleProperty(form, "projectId");
77  
78              if ("create".equals(action)) {
79  
80                  if (action != null && action.equals("create")) {
81                      pageTitleKey = "itracker.web.admin.editversion.title.create";
82                  }
83  
84                  if (projectId == null) {
85                      errors.add(ActionMessages.GLOBAL_MESSAGE,
86                              new ActionMessage("itracker.web.error.invalidproject"));
87                  } else {
88                      project = projectService.getProject(projectId);
89  
90                      if (project == null) {
91                          errors.add(ActionMessages.GLOBAL_MESSAGE,
92                                  new ActionMessage("itracker.web.error.invalidproject"));
93                      } else if (!UserUtilities.hasPermission(userPermissions,
94                              project.getId(), PermissionType.PRODUCT_ADMIN)) {
95                          return mapping.findForward("unauthorized");
96                      } else {
97                          version = new Version();
98                          version.setProject(project);
99                          versionForm.setAction("create");
100                         versionForm.setDescription(versionForm.getDescription());
101                         versionForm.setId(-1);
102                         versionForm.setProjectId(version.getProject().getId());
103                     }
104                 }
105             } else if ("update".equals(action)) {
106                 Integer versionId = (Integer) PropertyUtils.getSimpleProperty(form, "id");
107                 version = projectService.getProjectVersion(versionId);
108                 if (action != null && action.equals("update")) {
109                     pageTitleKey = "itracker.web.admin.editversion.title.update";
110                     pageTitleArg = version.getNumber();
111                 }
112                 if (version == null) {
113                     errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidversion"));
114                 } else {
115                     if (!UserUtilities.hasPermission(userPermissions, version.getProject().getId(), PermissionType.PRODUCT_ADMIN)) {
116                         return mapping.findForward("unauthorized");
117                     } else {
118                         versionForm.setAction("update");
119                         versionForm.setId(version.getId());
120                         versionForm.setProjectId(version.getProject().getId());
121                         versionForm.setNumber(version.getNumber());
122                         versionForm.setDescription(version.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("versionForm", versionForm);
132                 session.setAttribute(Constants.VERSION_KEY, version);
133                 saveToken(request);
134                 request.setAttribute("pageTitleKey", pageTitleKey);
135                 request.setAttribute("pageTitleArg", pageTitleArg);
136                 ActionForward af = EditVersionFormActionUtil.init(mapping, request);
137                 if (af != null) return af;
138                 return mapping.getInputForward();
139             }
140         } catch (Exception ex) {
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 version form.", ex);
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 }