EditIssueFormAction.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.actions.project;

  19. import org.apache.log4j.Logger;
  20. import org.apache.struts.action.*;
  21. import org.itracker.model.*;
  22. import org.itracker.model.util.IssueUtilities;
  23. import org.itracker.services.IssueService;
  24. import org.itracker.services.UserService;
  25. import org.itracker.web.actions.base.ItrackerBaseAction;
  26. import org.itracker.web.forms.IssueForm;
  27. import org.itracker.web.util.*;

  28. import javax.servlet.ServletException;
  29. import javax.servlet.http.HttpServletRequest;
  30. import javax.servlet.http.HttpServletResponse;
  31. import javax.servlet.http.HttpSession;
  32. import java.io.IOException;
  33. import java.util.List;
  34. import java.util.Locale;
  35. import java.util.Map;
  36. import java.util.Set;

  37. /**
  38.  * This class populates an IssueForm object for display by the edit issue page.
  39.  */
  40. public class EditIssueFormAction extends ItrackerBaseAction {
  41.     private static final Logger log = Logger.getLogger(EditIssueFormAction.class);

  42.     /* (non-Javadoc)
  43.       * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
  44.       */
  45.     @Override
  46.     public ActionForward execute(ActionMapping mapping, ActionForm form,
  47.                                  HttpServletRequest request, HttpServletResponse response)
  48.             throws ServletException, IOException {
  49.         if (log.isDebugEnabled()) {
  50.             log.debug("execute: called with mapping: " + mapping + ", form: "
  51.                     + form + ", request: " + request + ", response: "
  52.                     + response);
  53.         }
  54.         ActionMessages errors = new ActionMessages();

  55.         try {
  56.             IssueService issueService = ServletContextUtils.getItrackerServices().getIssueService();
  57.             UserService userService = ServletContextUtils.getItrackerServices().getUserService();
  58.             Integer issueId = new Integer(
  59.                     (request.getParameter("id") == null ? "-1" : (request
  60.                             .getParameter("id"))));

  61.             Issue issue;
  62.             try {
  63.                 issue = issueService.getIssue(issueId);
  64.             } catch (Exception ex) {
  65.                 issue = null;
  66.             }
  67.             if (issue == null) {
  68.                 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
  69.                         "itracker.web.error.invalidissue"));
  70.                 saveErrors(request, errors);
  71.                 return mapping.findForward("error");
  72.             }
  73.             Project project = issueService.getIssueProject(issueId);

  74.             if (project == null) {
  75.                 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
  76.                         "itracker.web.error.invalidproject"));
  77.             } else if (project.getStatus() != Status.ACTIVE) {
  78.                 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
  79.                         "itracker.web.error.projectlocked"));
  80.             } else {
  81.                 HttpSession session = request.getSession(true);
  82.                 User currUser = (User) session.getAttribute(Constants.USER_KEY);
  83.                 Map<Integer, Set<PermissionType>> userPermissions = RequestHelper.getUserPermissions(session);

  84.                 Locale locale = getLocale(request);

  85.                 List<NameValuePair> ownersList = EditIssueActionUtil
  86.                         .getAssignableIssueOwnersList(issue, project, currUser,
  87.                                 locale, userService, userPermissions);

  88.                 if (!IssueUtilities.canEditIssue(issue, currUser.getId(),
  89.                         userPermissions)) {
  90.                     log
  91.                             .debug("Unauthorized user requested access to edit issue for project "
  92.                                     + project.getId());
  93.                     return mapping.findForward("unauthorized");
  94.                 }

  95.                 IssueForm issueForm = (IssueForm) form;
  96.                 if (issueForm == null) {
  97.                     issueForm = new IssueForm();
  98.                 }
  99.                 Map<Integer, List<NameValuePair>> listOptions = EditIssueActionUtil.getListOptions(
  100.                         request, issue, ownersList, userPermissions, issue
  101.                         .getProject(), currUser);

  102.                 issueForm.setupIssueForm(issue, listOptions, request, errors);
  103.                 IssueNavigationUtil.setupNextPreviousIssueInRequest(request, issue, issueService);

  104.                 IssueForm.setupJspEnv(mapping, issueForm, request,
  105.                         issue, issueService, userService, userPermissions,
  106.                         listOptions, errors);

  107.                 log.debug("Forwarding to edit issue form for issue "
  108.                         + issue.getId());

  109.                 // TODO: Sort attachments
  110.                 // Collections.sort(attachments,
  111.                 // IssueAttachment.CREATE_DATE_COMPARATOR);

  112.                 saveToken(request);
  113.                 if (issue == null) {
  114.                     return mapping.findForward("error");
  115.                 }
  116.                 if (errors.isEmpty()) {
  117.                     log.info("EditIssueFormAction: Forward: InputForward");
  118.                     saveErrors(request, errors);
  119.                     return mapping.getInputForward();
  120.                 }
  121.             }
  122.         } catch (Exception e) {
  123.             log.error("Exception while creating edit issue form.", e);
  124.             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
  125.                     "itracker.web.error.system"));
  126.         }

  127.         if (!errors.isEmpty()) {
  128.             saveErrors(request, errors);
  129.         }

  130.         log.info("EditIssueFormAction: Forward: Error");
  131.         return mapping.findForward("error");
  132.     }

  133. }