ProjectForm.java

  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. package org.itracker.web.forms;

  19. import org.apache.log4j.Logger;
  20. import org.apache.struts.action.*;
  21. import org.apache.struts.validator.ValidatorForm;
  22. import org.itracker.model.*;
  23. import org.itracker.model.util.CustomFieldUtilities;
  24. import org.itracker.model.util.IssueUtilities;
  25. import org.itracker.model.util.ProjectUtilities;
  26. import org.itracker.model.util.UserUtilities;
  27. import org.itracker.services.ITrackerServices;
  28. import org.itracker.services.ProjectService;
  29. import org.itracker.services.UserService;
  30. import org.itracker.web.ptos.ProjectScriptPTO;
  31. import org.itracker.web.util.*;

  32. import javax.servlet.http.HttpServletRequest;
  33. import javax.servlet.http.HttpSession;
  34. import java.util.*;

  35. /**
  36.  * This is the LoginForm Struts Form. It is used by Login form.
  37.  *
  38.  * @author ready
  39.  */
  40. public class ProjectForm extends ValidatorForm {
  41.     /**
  42.      *
  43.      */
  44.     private static final long serialVersionUID = 1L;
  45.     public static final String TITLE_UPDATE = "itracker.web.admin.editproject.title.update";
  46.     public static final String TITLE_CREATE = "itracker.web.admin.editproject.title.create";
  47.     private String action;
  48.     private Integer id;
  49.     private String name;
  50.     private Integer status;
  51.     private String description;
  52.     private Integer[] owners;
  53.     private Integer[] users;
  54.     private Integer[] permissions;
  55.     private Integer[] options;
  56.     private Integer[] fields;

  57.     private static final Logger log = Logger.getLogger(ProjectForm.class);

  58.     public ActionForward init(ActionMapping mapping, HttpServletRequest request) {
  59.         ITrackerServices itrackerServices = ServletContextUtils.getItrackerServices();
  60.         ProjectService projectService = itrackerServices.getProjectService();
  61.         UserService userService = itrackerServices.getUserService();

  62.         HttpSession session = request.getSession(true);
  63.         User user = (User) session.getAttribute(Constants.USER_KEY);
  64.         Boolean allowPermissionUpdate = userService.allowPermissionUpdates(user, null, UserUtilities.AUTH_TYPE_UNKNOWN, UserUtilities.REQ_SOURCE_WEB);


  65.         final Map<Integer, Set<PermissionType>> permissions = RequestHelper
  66.                 .getUserPermissions(session);
  67.         Project project = (Project) session.getAttribute(Constants.PROJECT_KEY);
  68.         boolean isUpdate;

  69.         if (project == null) {
  70.             log.info("EditProjectAction: Forward: unauthorized");
  71.             return mapping.findForward("unauthorized");
  72.         } else {
  73.             isUpdate = false;
  74.             if (!project.isNew()) {
  75.                 isUpdate = true;
  76.             }
  77.         }
  78.         request.setAttribute("isUpdate", isUpdate);

  79.         setupTitle(request, projectService);


  80.         List<NameValuePair> statuses = new ArrayList<NameValuePair>();
  81.         statuses.add(new NameValuePair(ProjectUtilities.getStatusName(Status.ACTIVE, LoginUtilities.getCurrentLocale(request)), Integer.toString(Status.ACTIVE.getCode())));
  82.         statuses.add(new NameValuePair(ProjectUtilities.getStatusName(Status.VIEWABLE, LoginUtilities.getCurrentLocale(request)), Integer.toString(Status.VIEWABLE.getCode())));
  83.         statuses.add(new NameValuePair(ProjectUtilities.getStatusName(Status.LOCKED, LoginUtilities.getCurrentLocale(request)), Integer.toString(Status.LOCKED.getCode())));
  84.         request.setAttribute("statuses", statuses);

  85.         Set<User> owners = new TreeSet<User>(User.NAME_COMPARATOR);
  86.         if (!project.isNew()) {
  87.             owners.addAll(userService.getUsersWithProjectPermission(project.getId(), PermissionType.ISSUE_VIEW_ALL));
  88.         } else {
  89.             owners.addAll(userService.getSuperUsers());
  90.         }
  91.         owners.addAll(project.getOwners());
  92.         request.setAttribute("owners", owners);

  93.         boolean allowPermissionUpdateOption = allowPermissionUpdate == null ? false
  94.                 : allowPermissionUpdate && UserUtilities.hasPermission(permissions, new Integer(-1), PermissionType.USER_ADMIN);
  95.         request.setAttribute("allowPermissionUpdateOption", allowPermissionUpdateOption);

  96.         if (project.isNew()) {
  97.             List<User> users = new ArrayList<User>();
  98.             List<User> activeUsers = userService.getActiveUsers();
  99.             Collections.sort(activeUsers, User.NAME_COMPARATOR);
  100.             for (int i = 0; i < activeUsers.size(); i++) {
  101.                 if (owners.contains(activeUsers.get(i))) {
  102.                     continue;
  103.                 }
  104.                 users.add(activeUsers.get(i));
  105.             }
  106.             request.setAttribute("users", users);
  107.         }


  108.         List<NameValuePair> permissionNames = UserUtilities.getPermissionNames(LoginUtilities.getCurrentLocale(request));
  109.         request.setAttribute("permissions", permissionNames);

  110.         request.setAttribute("optionSupressHistoryHtml", Integer.toString(ProjectUtilities.OPTION_SURPRESS_HISTORY_HTML));
  111.         request.setAttribute("optionPredefinedResolutions", Integer.toString(ProjectUtilities.OPTION_PREDEFINED_RESOLUTIONS));
  112.         request.setAttribute("optionAllowAssignToClose", Integer.toString(ProjectUtilities.OPTION_ALLOW_ASSIGN_TO_CLOSE));
  113.         request.setAttribute("optionAllowSefRegisteredCreate", Integer.toString(ProjectUtilities.OPTION_ALLOW_SELF_REGISTERED_CREATE));
  114.         request.setAttribute("optionLiteralHistoryHtml", Integer.toString(ProjectUtilities.OPTION_LITERAL_HISTORY_HTML));
  115.         request.setAttribute("optionNoAttachments", Integer.toString(ProjectUtilities.OPTION_NO_ATTACHMENTS));
  116.         request.setAttribute("optionAllowSelfRegisteredViewAll", Integer.toString(ProjectUtilities.OPTION_ALLOW_SELF_REGISTERED_VIEW_ALL));

  117.         List<CustomField> customFields = IssueUtilities.getCustomFields();


  118.         List<EditProjectFormActionUtil.CustomFieldInfo> fieldInfos = new ArrayList<EditProjectFormActionUtil.CustomFieldInfo>(customFields.size());
  119.         Iterator<CustomField> fieldsIt = customFields.iterator();
  120.         CustomField ci;
  121.         while (fieldsIt.hasNext()) {
  122.             ci = fieldsIt.next();
  123.             fieldInfos.add(new EditProjectFormActionUtil.CustomFieldInfo(ci.getId(),
  124.                     CustomFieldUtilities.getCustomFieldName(ci.getId(), LoginUtilities.getCurrentLocale(request)),
  125.                     CustomFieldUtilities.getTypeString(ci.getFieldType(), LoginUtilities.getCurrentLocale(request))));
  126.         }

  127.         request.setAttribute("customFields", fieldInfos);


  128.         // setup project-scripts

  129.         List<ProjectScript> scripts = project.getScripts();
  130.         Collections.sort(scripts, ProjectScript.FIELD_PRIORITY_COMPARATOR);

  131.         EditProjectFormActionUtil.setUpPrioritiesInEnv(request);

  132.         Locale locale = LoginUtilities.getCurrentLocale(request);
  133.         Iterator<ProjectScript> it = scripts.iterator();

  134.         List<ProjectScriptPTO> scriptPTOs = new ArrayList<ProjectScriptPTO>(scripts.size());
  135.         while (it.hasNext()) {
  136.             ProjectScriptPTO projectScript = new ProjectScriptPTO(it.next(), locale);
  137.             scriptPTOs.add(projectScript);
  138.         }
  139.         request.setAttribute("projectScripts", scriptPTOs);

  140.         List<Version> versions = project.getVersions();
  141.         Collections.sort(versions, new Version.VersionComparator());
  142.         List<EditProjectFormActionUtil.VersionInfo> vis = new ArrayList<EditProjectFormActionUtil.VersionInfo>();

  143.         for (Version v : versions)
  144.             vis.add(new EditProjectFormActionUtil.VersionInfo(v.getId(), v.getNumber(), v.getDescription(), v.getLastModifiedDate(), projectService.countIssuesByVersion(v.getId())));
  145.         request.setAttribute("versions", vis);

  146.         List<Component> components = project.getComponents();
  147.         Collections.sort(components);
  148.         List<EditProjectFormActionUtil.ComponentInfo> cis = new ArrayList<EditProjectFormActionUtil.ComponentInfo>();

  149.         for (Component c : components)
  150.             cis.add(new EditProjectFormActionUtil.ComponentInfo(c.getId(), c.getName(), c.getDescription(), c.getLastModifiedDate(), projectService.countIssuesByComponent(c.getId())));
  151.         request.setAttribute("components", cis);
  152.         return null;
  153.     }

  154.     /**
  155.      * Setup the title for the Project-Form Action
  156.      *
  157.      * @param request        -  the servlet request
  158.      * @param projectService - project-service
  159.      */
  160.     public void setupTitle(HttpServletRequest request, ProjectService projectService) {
  161.         String pageTitleKey;
  162.         String pageTitleArg = "";

  163.         if ("update".equals(getAction())) {
  164.             pageTitleKey = TITLE_UPDATE;
  165.                 Project project = projectService.getProject(getId());
  166.                 if (null != project) {
  167.                     pageTitleArg = project.getName();

  168.             }
  169.         } else {
  170.             setAction("create");
  171.             pageTitleKey = TITLE_CREATE;
  172.         }
  173.         request.setAttribute(Constants.PAGE_TITLE_KEY, pageTitleKey);
  174.         request.setAttribute(Constants.PAGE_TITLE_ARG, pageTitleArg);
  175.     }

  176.     public void reset(ActionMapping mapping, HttpServletRequest request) {
  177.         action = null;
  178.         id = null;
  179.         name = null;
  180.         status = null;
  181.         description = null;
  182.         owners = null;
  183.         users = null;
  184.         permissions = null;
  185.         options = null;
  186.         fields = null;

  187.     }

  188.     public ActionErrors validate(ActionMapping mapping,
  189.                                  HttpServletRequest request) {
  190.         ActionErrors errors = super.validate(mapping, request);
  191.         if (log.isDebugEnabled()) {
  192.             log.debug("ProjectForm validate called: mapping: " + mapping
  193.                     + ", request: " + request + ", errors: " + errors);
  194.         }
  195.         if (ServletContextUtils.getItrackerServices().getProjectService()
  196.                 .isUniqueProjectName(getName(), getId())) {
  197.         } else {
  198.             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
  199.                     "itracker.web.error.project.duplicate.name"));
  200.         }

  201.         init(mapping, request);
  202.         return errors;
  203.     }

  204.     public String getAction() {
  205.         return action;
  206.     }

  207.     public void setAction(String action) {
  208.         this.action = action;
  209.     }

  210.     public String getDescription() {
  211.         return description;
  212.     }

  213.     public void setDescription(String description) {
  214.         this.description = description;
  215.     }

  216.     public Integer[] getFields() {
  217.         if (null == fields)
  218.             return null;
  219.         return fields.clone();
  220.     }

  221.     public void setFields(Integer[] fields) {
  222.         if (null == fields)
  223.             this.fields = null;
  224.         else
  225.             this.fields = fields.clone();
  226.     }

  227.     public Integer getId() {
  228.         return id;
  229.     }

  230.     public void setId(Integer id) {
  231.         this.id = id;
  232.     }

  233.     public String getName() {
  234.         return name;
  235.     }

  236.     public void setName(String name) {
  237.         this.name = name;
  238.     }

  239.     public Integer[] getOptions() {
  240.         if (null == options)
  241.             return null;
  242.         return options.clone();
  243.     }

  244.     public void setOptions(Integer[] options) {
  245.         if (null == options)
  246.             this.options = null;
  247.         else
  248.             this.options = options.clone();
  249.     }

  250.     public Integer[] getOwners() {
  251.         if (null == owners)
  252.             return null;
  253.         return owners.clone();
  254.     }

  255.     public void setOwners(Integer[] owners) {
  256.         if (null == owners)
  257.             this.owners = null;
  258.         else
  259.             this.owners = owners.clone();
  260.     }

  261.     public Integer[] getPermissions() {
  262.         if (null == permissions)
  263.             return null;

  264.         return permissions.clone();

  265.     }

  266.     public void setPermissions(Integer[] permissions) {
  267.         if (null == permissions)
  268.             this.permissions = null;
  269.         else
  270.             this.permissions = permissions.clone();
  271.     }

  272.     public Integer getStatus() {
  273.         return status;
  274.     }

  275.     public void setStatus(Integer status) {
  276.         this.status = status;
  277.     }

  278.     public Integer[] getUsers() {
  279.         if (null == users)
  280.             return null;
  281.         return users.clone();
  282.     }

  283.     public void setUsers(Integer[] users) {
  284.         if (null == users)
  285.             this.users = null;
  286.         else
  287.             this.users = users.clone();
  288.     }

  289. }