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.project;
20  
21  import org.apache.log4j.Logger;
22  import org.apache.struts.action.*;
23  import org.itracker.model.*;
24  import org.itracker.model.util.IssueUtilities;
25  import org.itracker.services.IssueService;
26  import org.itracker.services.UserService;
27  import org.itracker.web.actions.base.ItrackerBaseAction;
28  import org.itracker.web.forms.IssueForm;
29  import org.itracker.web.util.*;
30  
31  import javax.servlet.ServletException;
32  import javax.servlet.http.HttpServletRequest;
33  import javax.servlet.http.HttpServletResponse;
34  import javax.servlet.http.HttpSession;
35  import java.io.IOException;
36  import java.util.List;
37  import java.util.Locale;
38  import java.util.Map;
39  import java.util.Set;
40  
41  /**
42   * This class populates an IssueForm object for display by the edit issue page.
43   */
44  public class EditIssueFormAction extends ItrackerBaseAction {
45      private static final Logger log = Logger.getLogger(EditIssueFormAction.class);
46  
47      /* (non-Javadoc)
48        * @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)
49        */
50      @Override
51      public ActionForward execute(ActionMapping mapping, ActionForm form,
52                                   HttpServletRequest request, HttpServletResponse response)
53              throws ServletException, IOException {
54          if (log.isDebugEnabled()) {
55              log.debug("execute: called with mapping: " + mapping + ", form: "
56                      + form + ", request: " + request + ", response: "
57                      + response);
58          }
59          ActionMessages errors = new ActionMessages();
60  
61          try {
62              IssueService issueService = ServletContextUtils.getItrackerServices().getIssueService();
63              UserService userService = ServletContextUtils.getItrackerServices().getUserService();
64              Integer issueId = new Integer(
65                      (request.getParameter("id") == null ? "-1" : (request
66                              .getParameter("id"))));
67  
68              Issue issue;
69              try {
70                  issue = issueService.getIssue(issueId);
71              } catch (Exception ex) {
72                  issue = null;
73              }
74              if (issue == null) {
75                  errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
76                          "itracker.web.error.invalidissue"));
77                  saveErrors(request, errors);
78                  return mapping.findForward("error");
79              }
80              Project project = issueService.getIssueProject(issueId);
81  
82              if (project == null) {
83                  errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
84                          "itracker.web.error.invalidproject"));
85              } else if (project.getStatus() != Status.ACTIVE) {
86                  errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
87                          "itracker.web.error.projectlocked"));
88              } else {
89                  HttpSession session = request.getSession(true);
90                  User currUser = (User) session.getAttribute(Constants.USER_KEY);
91                  Map<Integer, Set<PermissionType>> userPermissions = RequestHelper.getUserPermissions(session);
92  
93                  Locale locale = getLocale(request);
94  
95                  List<NameValuePair> ownersList = EditIssueActionUtil
96                          .getAssignableIssueOwnersList(issue, project, currUser,
97                                  locale, userService, userPermissions);
98  
99                  if (!IssueUtilities.canEditIssue(issue, currUser.getId(),
100                         userPermissions)) {
101                     log
102                             .debug("Unauthorized user requested access to edit issue for project "
103                                     + project.getId());
104                     return mapping.findForward("unauthorized");
105                 }
106 
107                 IssueForm../../../org/itracker/web/forms/IssueForm.html#IssueForm">IssueForm issueForm = (IssueForm) form;
108                 if (issueForm == null) {
109                     issueForm = new IssueForm();
110                 }
111                 Map<Integer, List<NameValuePair>> listOptions = EditIssueActionUtil.getListOptions(
112                         request, issue, ownersList, userPermissions, issue
113                         .getProject(), currUser);
114 
115                 issueForm.setupIssueForm(issue, listOptions, request, errors);
116                 IssueNavigationUtil.setupNextPreviousIssueInRequest(request, issue, issueService);
117 
118                 IssueForm.setupJspEnv(mapping, issueForm, request,
119                         issue, issueService, userService, userPermissions,
120                         listOptions, errors);
121 
122                 log.debug("Forwarding to edit issue form for issue "
123                         + issue.getId());
124 
125                 // TODO: Sort attachments
126                 // Collections.sort(attachments,
127                 // IssueAttachment.CREATE_DATE_COMPARATOR);
128 
129                 saveToken(request);
130                 if (issue == null) {
131                     return mapping.findForward("error");
132                 }
133                 if (errors.isEmpty()) {
134                     log.info("EditIssueFormAction: Forward: InputForward");
135                     saveErrors(request, errors);
136                     return mapping.getInputForward();
137                 }
138             }
139         } catch (Exception e) {
140             log.error("Exception while creating edit issue form.", e);
141             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
142                     "itracker.web.error.system"));
143         }
144 
145         if (!errors.isEmpty()) {
146             saveErrors(request, errors);
147         }
148 
149         log.info("EditIssueFormAction: Forward: Error");
150         return mapping.findForward("error");
151     }
152 
153 }