KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > web > actions > EditIssueFormAction


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 cowsultants.itracker.web.actions;
20
21 import java.io.*;
22 import java.rmi.*;
23 import java.util.*;
24 import javax.ejb.*;
25 import javax.rmi.*;
26 import javax.naming.*;
27 import javax.servlet.*;
28 import javax.servlet.http.*;
29
30 import org.apache.commons.beanutils.*;
31 import org.apache.struts.action.*;
32 import org.apache.struts.upload.*;
33 import org.apache.struts.util.*;
34 import org.apache.struts.validator.*;
35
36 import cowsultants.itracker.ejb.client.exceptions.*;
37 import cowsultants.itracker.ejb.client.interfaces.*;
38 import cowsultants.itracker.ejb.client.models.*;
39 import cowsultants.itracker.ejb.client.util.*;
40 import cowsultants.itracker.web.forms.*;
41 import cowsultants.itracker.web.util.*;
42
43 /**
44   * This class populates an IssueForm object for display by the edit issue page.
45   */

46 public class EditIssueFormAction extends ITrackerAction {
47
48     public EditIssueFormAction() {
49     }
50
51     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
52         ActionErrors errors = new ActionErrors();
53
54         if(! isLoggedIn(request, response)) {
55             return mapping.findForward("login");
56         }
57
58         try {
59             InitialContext ic = new InitialContext();
60
61             Object JavaDoc ihRef = ic.lookup("java:comp/env/" + IssueHandler.JNDI_NAME);
62             IssueHandlerHome ihHome = (IssueHandlerHome) PortableRemoteObject.narrow(ihRef, IssueHandlerHome.class);
63             IssueHandler ih = ihHome.create();
64
65             Integer JavaDoc issueId = new Integer JavaDoc((request.getParameter("id") == null ? "-1" : (request.getParameter("id"))));
66
67             IssueModel issue = ih.getIssue(issueId);
68             ProjectModel project = ih.getIssueProject(issueId);
69
70             if(issue == null) {
71                 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.invalidissue"));
72             } else if(project == null) {
73                 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.invalidproject"));
74             } else if(project.getStatus() != ProjectUtilities.STATUS_ACTIVE) {
75                 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.projectlocked"));
76             } else {
77                 HttpSession session = request.getSession(true);
78                 UserModel currUser = (UserModel) session.getAttribute(Constants.USER_KEY);
79                 HashMap currPermissions = (HashMap) session.getAttribute(Constants.PERMISSIONS_KEY);
80                 Locale currLocale = (Locale) session.getAttribute(Constants.LOCALE_KEY);
81
82                 if(! IssueUtilities.canEditIssue(issue, currUser.getId(), currPermissions)) {
83                         Logger.logDebug("Unauthorized user requested access to edit issue for project " + project.getId());
84                         return mapping.findForward("unauthorized");
85                 }
86
87                 if(errors.isEmpty()) {
88                     Logger.logDebug("Forwarding to edit issue form for issue " + issue.getId());
89                     DynaValidatorForm issueForm = (DynaValidatorForm) form;
90                     if(issueForm == null) {
91                         issueForm = new IssueForm();
92                     }
93                     issueForm.set("id", issue.getId());
94                     issueForm.set("projectId", issue.getProjectId());
95                     issueForm.set("prevStatus", new Integer JavaDoc(issue.getStatus()));
96                     issueForm.set("caller", request.getParameter("caller"));
97
98                     issueForm.set("description", HTMLUtilities.handleQuotes(issue.getDescription()));
99                     issueForm.set("status", new Integer JavaDoc(issue.getStatus()));
100
101                     if(! ProjectUtilities.hasOption(ProjectUtilities.OPTION_PREDEFINED_RESOLUTIONS, project.getOptions())) {
102                         try {
103                             // Test to see if the resolution is only a number. If it is, assume that
104
// it is actually a key from a fixed resolution and try that
105
int resolutionNumber = Integer.parseInt(issue.getResolution());
106                             issue.setResolution(IssueUtilities.checkResolutionName(issue.getResolution(), currLocale));
107                         } catch(MissingResourceException mre) {
108                         } catch(NumberFormatException JavaDoc nfe) {
109                         }
110                     }
111                     issueForm.set("resolution", HTMLUtilities.handleQuotes(issue.getResolution()));
112                     issueForm.set("severity", new Integer JavaDoc(issue.getSeverity()));
113                     issueForm.set("targetVersion", (issue.getTargetVersionId() == null ? new Integer JavaDoc(-1) : issue.getTargetVersionId()));
114                     issueForm.set("ownerId", (issue.getOwnerId() == null ? new Integer JavaDoc(-1) : issue.getOwnerId()));
115
116                     IssueFieldModel[] fields = issue.getFields();
117                     HashMap customFields = new HashMap();
118                     for(int i = 0; i < fields.length; i++) {
119                         customFields.put(fields[i].getCustomFieldId(), fields[i].getValue(currLocale));
120                     }
121                     issueForm.set("customFields", customFields);
122
123                     HashSet selectedComponents = ih.getIssueComponentIds(issueId);
124                     if(selectedComponents != null) {
125                         Integer JavaDoc[] components = new Integer JavaDoc[selectedComponents.size()];
126                         int i = 0;
127                         for(Iterator iter = selectedComponents.iterator(); iter.hasNext(); i++) {
128                             components[i] = (Integer JavaDoc) iter.next();
129                         }
130                         issueForm.set("components", components);
131                     }
132
133                     HashSet selectedVersions = ih.getIssueVersionIds(issueId);
134                     if(selectedVersions != null) {
135                         Integer JavaDoc[] versions = new Integer JavaDoc[selectedVersions.size()];
136                         int i = 0;
137                         for(Iterator iter = selectedVersions.iterator(); iter.hasNext(); i++) {
138                             versions[i] = (Integer JavaDoc) iter.next();
139                         }
140                         issueForm.set("versions", versions);
141                     }
142
143                     request.setAttribute("issueForm", issueForm);
144                     session.setAttribute(Constants.ISSUE_KEY, issue);
145                     saveToken(request);
146                     return mapping.getInputForward();
147                 }
148             }
149         } catch(Exception JavaDoc e) {
150             Logger.logError("Exception while creating edit issue form.", e);
151             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.system"));
152         }
153
154         if(! errors.isEmpty()) {
155             saveErrors(request, errors);
156         }
157
158         return mapping.findForward("error");
159     }
160
161 }
162   
Popular Tags