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.forms;
20  
21  import org.apache.log4j.Logger;
22  import org.apache.struts.action.*;
23  import org.apache.struts.validator.ValidatorForm;
24  import org.itracker.model.*;
25  import org.itracker.model.util.CustomFieldUtilities;
26  import org.itracker.model.util.IssueUtilities;
27  import org.itracker.model.util.ProjectUtilities;
28  import org.itracker.model.util.UserUtilities;
29  import org.itracker.services.ITrackerServices;
30  import org.itracker.services.ProjectService;
31  import org.itracker.services.UserService;
32  import org.itracker.web.ptos.ProjectScriptPTO;
33  import org.itracker.web.util.*;
34  
35  import javax.servlet.http.HttpServletRequest;
36  import javax.servlet.http.HttpSession;
37  import java.util.*;
38  
39  /**
40   * This is the LoginForm Struts Form. It is used by Login form.
41   *
42   * @author ready
43   */
44  public class ProjectForm extends ValidatorForm {
45      /**
46       *
47       */
48      private static final long serialVersionUID = 1L;
49      public static final String TITLE_UPDATE = "itracker.web.admin.editproject.title.update";
50      public static final String TITLE_CREATE = "itracker.web.admin.editproject.title.create";
51      private String action;
52      private Integer id;
53      private String name;
54      private Integer status;
55      private String description;
56      private Integer[] owners;
57      private Integer[] users;
58      private Integer[] permissions;
59      private Integer[] options;
60      private Integer[] fields;
61  
62      private static final Logger log = Logger.getLogger(ProjectForm.class);
63  
64      public ActionForward init(ActionMapping mapping, HttpServletRequest request) {
65          ITrackerServices itrackerServices = ServletContextUtils.getItrackerServices();
66          ProjectService projectService = itrackerServices.getProjectService();
67          UserService userService = itrackerServices.getUserService();
68  
69          HttpSession session = request.getSession(true);
70          User user = (User) session.getAttribute(Constants.USER_KEY);
71          Boolean allowPermissionUpdate = userService.allowPermissionUpdates(user, null, UserUtilities.AUTH_TYPE_UNKNOWN, UserUtilities.REQ_SOURCE_WEB);
72  
73  
74          final Map<Integer, Set<PermissionType>> permissions = RequestHelper
75                  .getUserPermissions(session);
76          Project project = (Project) session.getAttribute(Constants.PROJECT_KEY);
77          boolean isUpdate;
78  
79          if (project == null) {
80              log.info("EditProjectAction: Forward: unauthorized");
81              return mapping.findForward("unauthorized");
82          } else {
83              isUpdate = false;
84              if (!project.isNew()) {
85                  isUpdate = true;
86              }
87          }
88          request.setAttribute("isUpdate", isUpdate);
89  
90          setupTitle(request, projectService);
91  
92  
93          List<NameValuePair> statuses = new ArrayList<NameValuePair>();
94          statuses.add(new NameValuePair(ProjectUtilities.getStatusName(Status.ACTIVE, LoginUtilities.getCurrentLocale(request)), Integer.toString(Status.ACTIVE.getCode())));
95          statuses.add(new NameValuePair(ProjectUtilities.getStatusName(Status.VIEWABLE, LoginUtilities.getCurrentLocale(request)), Integer.toString(Status.VIEWABLE.getCode())));
96          statuses.add(new NameValuePair(ProjectUtilities.getStatusName(Status.LOCKED, LoginUtilities.getCurrentLocale(request)), Integer.toString(Status.LOCKED.getCode())));
97          request.setAttribute("statuses", statuses);
98  
99          Set<User> owners = new TreeSet<User>(User.NAME_COMPARATOR);
100         if (!project.isNew()) {
101             owners.addAll(userService.getUsersWithProjectPermission(project.getId(), PermissionType.ISSUE_VIEW_ALL));
102         } else {
103             owners.addAll(userService.getSuperUsers());
104         }
105         owners.addAll(project.getOwners());
106         request.setAttribute("owners", owners);
107 
108         boolean allowPermissionUpdateOption = allowPermissionUpdate == null ? false
109                 : allowPermissionUpdate && UserUtilities.hasPermission(permissions, new Integer(-1), PermissionType.USER_ADMIN);
110         request.setAttribute("allowPermissionUpdateOption", allowPermissionUpdateOption);
111 
112         if (project.isNew()) {
113             List<User> users = new ArrayList<User>();
114             List<User> activeUsers = userService.getActiveUsers();
115             Collections.sort(activeUsers, User.NAME_COMPARATOR);
116             for (int i = 0; i < activeUsers.size(); i++) {
117                 if (owners.contains(activeUsers.get(i))) {
118                     continue;
119                 }
120                 users.add(activeUsers.get(i));
121             }
122             request.setAttribute("users", users);
123         }
124 
125 
126         List<NameValuePair> permissionNames = UserUtilities.getPermissionNames(LoginUtilities.getCurrentLocale(request));
127         request.setAttribute("permissions", permissionNames);
128 
129         request.setAttribute("optionSupressHistoryHtml", Integer.toString(ProjectUtilities.OPTION_SURPRESS_HISTORY_HTML));
130         request.setAttribute("optionPredefinedResolutions", Integer.toString(ProjectUtilities.OPTION_PREDEFINED_RESOLUTIONS));
131         request.setAttribute("optionAllowAssignToClose", Integer.toString(ProjectUtilities.OPTION_ALLOW_ASSIGN_TO_CLOSE));
132         request.setAttribute("optionAllowSefRegisteredCreate", Integer.toString(ProjectUtilities.OPTION_ALLOW_SELF_REGISTERED_CREATE));
133         request.setAttribute("optionLiteralHistoryHtml", Integer.toString(ProjectUtilities.OPTION_LITERAL_HISTORY_HTML));
134         request.setAttribute("optionNoAttachments", Integer.toString(ProjectUtilities.OPTION_NO_ATTACHMENTS));
135         request.setAttribute("optionAllowSelfRegisteredViewAll", Integer.toString(ProjectUtilities.OPTION_ALLOW_SELF_REGISTERED_VIEW_ALL));
136 
137         List<CustomField> customFields = IssueUtilities.getCustomFields();
138 
139 
140         List<EditProjectFormActionUtil.CustomFieldInfo> fieldInfos = new ArrayList<EditProjectFormActionUtil.CustomFieldInfo>(customFields.size());
141         Iterator<CustomField> fieldsIt = customFields.iterator();
142         CustomField ci;
143         while (fieldsIt.hasNext()) {
144             ci = fieldsIt.next();
145             fieldInfos.add(new EditProjectFormActionUtil.CustomFieldInfo(ci.getId(),
146                     CustomFieldUtilities.getCustomFieldName(ci.getId(), LoginUtilities.getCurrentLocale(request)),
147                     CustomFieldUtilities.getTypeString(ci.getFieldType(), LoginUtilities.getCurrentLocale(request))));
148         }
149 
150         request.setAttribute("customFields", fieldInfos);
151 
152 
153         // setup project-scripts
154 
155         List<ProjectScript> scripts = project.getScripts();
156         Collections.sort(scripts, ProjectScript.FIELD_PRIORITY_COMPARATOR);
157 
158         EditProjectFormActionUtil.setUpPrioritiesInEnv(request);
159 
160         Locale locale = LoginUtilities.getCurrentLocale(request);
161         Iterator<ProjectScript> it = scripts.iterator();
162 
163         List<ProjectScriptPTO> scriptPTOs = new ArrayList<ProjectScriptPTO>(scripts.size());
164         while (it.hasNext()) {
165             ProjectScriptPTOrojectScriptPTO">ProjectScriptPTO projectScript = new ProjectScriptPTO(it.next(), locale);
166             scriptPTOs.add(projectScript);
167         }
168         request.setAttribute("projectScripts", scriptPTOs);
169 
170         List<Version> versions = project.getVersions();
171         Collections.sort(versions, new Version.VersionComparator());
172         List<EditProjectFormActionUtil.VersionInfo> vis = new ArrayList<EditProjectFormActionUtil.VersionInfo>();
173 
174         for (Version v : versions)
175             vis.add(new EditProjectFormActionUtil.VersionInfo(v.getId(), v.getNumber(), v.getDescription(), v.getLastModifiedDate(), projectService.countIssuesByVersion(v.getId())));
176         request.setAttribute("versions", vis);
177 
178         List<Component> components = project.getComponents();
179         Collections.sort(components);
180         List<EditProjectFormActionUtil.ComponentInfo> cis = new ArrayList<EditProjectFormActionUtil.ComponentInfo>();
181 
182         for (Component c : components)
183             cis.add(new EditProjectFormActionUtil.ComponentInfo(c.getId(), c.getName(), c.getDescription(), c.getLastModifiedDate(), projectService.countIssuesByComponent(c.getId())));
184         request.setAttribute("components", cis);
185         return null;
186     }
187 
188     /**
189      * Setup the title for the Project-Form Action
190      *
191      * @param request        -  the servlet request
192      * @param projectService - project-service
193      */
194     public void setupTitle(HttpServletRequest request, ProjectService projectService) {
195         String pageTitleKey;
196         String pageTitleArg = "";
197 
198         if ("update".equals(getAction())) {
199             pageTitleKey = TITLE_UPDATE;
200                 Project project = projectService.getProject(getId());
201                 if (null != project) {
202                     pageTitleArg = project.getName();
203 
204             }
205         } else {
206             setAction("create");
207             pageTitleKey = TITLE_CREATE;
208         }
209         request.setAttribute(Constants.PAGE_TITLE_KEY, pageTitleKey);
210         request.setAttribute(Constants.PAGE_TITLE_ARG, pageTitleArg);
211     }
212 
213     public void reset(ActionMapping mapping, HttpServletRequest request) {
214         action = null;
215         id = null;
216         name = null;
217         status = null;
218         description = null;
219         owners = null;
220         users = null;
221         permissions = null;
222         options = null;
223         fields = null;
224 
225     }
226 
227     public ActionErrors validate(ActionMapping mapping,
228                                  HttpServletRequest request) {
229         ActionErrors errors = super.validate(mapping, request);
230         if (log.isDebugEnabled()) {
231             log.debug("ProjectForm validate called: mapping: " + mapping
232                     + ", request: " + request + ", errors: " + errors);
233         }
234         if (ServletContextUtils.getItrackerServices().getProjectService()
235                 .isUniqueProjectName(getName(), getId())) {
236         } else {
237             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
238                     "itracker.web.error.project.duplicate.name"));
239         }
240 
241         init(mapping, request);
242         return errors;
243     }
244 
245     public String getAction() {
246         return action;
247     }
248 
249     public void setAction(String action) {
250         this.action = action;
251     }
252 
253     public String getDescription() {
254         return description;
255     }
256 
257     public void setDescription(String description) {
258         this.description = description;
259     }
260 
261     public Integer[] getFields() {
262         if (null == fields)
263             return null;
264         return fields.clone();
265     }
266 
267     public void setFields(Integer[] fields) {
268         if (null == fields)
269             this.fields = null;
270         else
271             this.fields = fields.clone();
272     }
273 
274     public Integer getId() {
275         return id;
276     }
277 
278     public void setId(Integer id) {
279         this.id = id;
280     }
281 
282     public String getName() {
283         return name;
284     }
285 
286     public void setName(String name) {
287         this.name = name;
288     }
289 
290     public Integer[] getOptions() {
291         if (null == options)
292             return null;
293         return options.clone();
294     }
295 
296     public void setOptions(Integer[] options) {
297         if (null == options)
298             this.options = null;
299         else
300             this.options = options.clone();
301     }
302 
303     public Integer[] getOwners() {
304         if (null == owners)
305             return null;
306         return owners.clone();
307     }
308 
309     public void setOwners(Integer[] owners) {
310         if (null == owners)
311             this.owners = null;
312         else
313             this.owners = owners.clone();
314     }
315 
316     public Integer[] getPermissions() {
317         if (null == permissions)
318             return null;
319 
320         return permissions.clone();
321 
322     }
323 
324     public void setPermissions(Integer[] permissions) {
325         if (null == permissions)
326             this.permissions = null;
327         else
328             this.permissions = permissions.clone();
329     }
330 
331     public Integer getStatus() {
332         return status;
333     }
334 
335     public void setStatus(Integer status) {
336         this.status = status;
337     }
338 
339     public Integer[] getUsers() {
340         if (null == users)
341             return null;
342         return users.clone();
343     }
344 
345     public void setUsers(Integer[] users) {
346         if (null == users)
347             this.users = null;
348         else
349             this.users = users.clone();
350     }
351 
352 }