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.CustomField;
24  import org.itracker.model.PermissionType;
25  import org.itracker.model.Project;
26  import org.itracker.model.User;
27  import org.itracker.model.util.ProjectUtilities;
28  import org.itracker.model.util.UserUtilities;
29  import org.itracker.services.ProjectService;
30  import org.itracker.web.actions.base.ItrackerBaseAction;
31  import org.itracker.web.forms.ProjectForm;
32  import org.itracker.web.util.Constants;
33  import org.itracker.web.util.RequestHelper;
34  import org.itracker.web.util.ServletContextUtils;
35  
36  import javax.servlet.ServletException;
37  import javax.servlet.http.HttpServletRequest;
38  import javax.servlet.http.HttpServletResponse;
39  import javax.servlet.http.HttpSession;
40  import java.io.IOException;
41  import java.util.List;
42  import java.util.Map;
43  import java.util.Set;
44  
45  
46  public class EditProjectFormAction extends ItrackerBaseAction {
47  
48      private static final Logger log = Logger.getLogger(EditProjectFormAction.class);
49  
50  
51      public ActionForward execute(ActionMapping mapping,
52                                   ActionForm form,
53                                   HttpServletRequest request,
54                                   HttpServletResponse response)
55              throws ServletException, IOException {
56  
57          ActionMessages errors = new ActionMessages();
58  
59          try {
60              ProjectService projectService = ServletContextUtils.getItrackerServices().getProjectService();
61  
62              HttpSession session = request.getSession(true);
63  
64  
65              Map<Integer, Set<PermissionType>> userPermissions = RequestHelper.getUserPermissions(session);
66              Useref="../../../../../../org/itracker/model/User.html#User">User user = (User) session.getAttribute(Constants.USER_KEY);
67  
68              ProjectForm./../../org/itracker/web/forms/ProjectForm.html#ProjectForm">ProjectForm projectForm = (ProjectForm) form;
69  
70              if (projectForm == null) {
71                  return mapping.getInputForward();
72              }
73  
74              Project project;
75              if (projectForm.getAction() != null && projectForm.getAction().equals("update")) {
76                  project = null;
77                  if (null == projectForm.getId()) {
78                      errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidproject"));
79                  } else {
80                      project = projectService.getProject(projectForm.getId());
81                      if (null == project) {
82                          errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidproject"));
83                      }
84                  }
85  
86              } else {
87                  project = new Project();
88              }
89  
90  
91              if ("create".equals(projectForm.getAction())) {
92                  if (!user.isSuperUser()) {
93                      return mapping.findForward("unauthorized");
94                  }
95                  projectForm.setId(project.getId());
96              } else if ("update".equals(projectForm.getAction())) {
97  
98  
99                  if (errors.isEmpty()) {
100 
101                     if (!UserUtilities.hasPermission(userPermissions, project.getId(), UserUtilities.PERMISSION_PRODUCT_ADMIN)) {
102                         return mapping.findForward("unauthorized");
103                     } else if (errors.isEmpty()) {
104                         projectForm.setId(project.getId());
105                         projectForm.setName(project.getName());
106                         projectForm.setDescription(project.getDescription());
107                         projectForm.setStatus(project.getStatus().getCode());
108                         int currentOptions = project.getOptions();
109                         projectForm.setOptions(ProjectUtilities.getOptions(currentOptions));
110 
111                         List<CustomField> fields = project.getCustomFields();
112                         Integer[] fieldIds = new Integer[fields.size()];
113                         for (int i = 0; i < fields.size(); i++) {
114                             fieldIds[i] = fields.get(i).getId();
115                         }
116                         projectForm.setFields(fieldIds);
117 
118                         List<User> owners = project.getOwners();
119                         Integer[] ownerIds = new Integer[owners.size()];
120 
121                         for (int i = 0; i < owners.size(); i++) {
122                             ownerIds[i] = owners.get(i).getId();
123                         }
124 
125                         projectForm.setOwners(ownerIds);
126                     }
127                 }
128 
129             } else {
130                 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidaction"));
131             }
132 
133             if (errors.isEmpty()) {
134                 request.setAttribute("projectForm", projectForm);
135                 session.setAttribute(Constants.PROJECT_KEY, project);
136                 saveToken(request);
137                 ActionForward af = projectForm.init(mapping, request);
138                 if (af != null) {
139                     return af;
140                 }
141                 return mapping.getInputForward();
142             }
143 
144         } catch (Exception e) {
145             log.error("Exception while creating edit project form.", e);
146             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
147         }
148 
149         if (!errors.isEmpty()) {
150             saveErrors(request, errors);
151         }
152 
153         return mapping.findForward("error");
154 
155     }
156 
157 }