KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.*;
41
42
43 public class SearchIssuesAction extends ITrackerAction {
44
45     public SearchIssuesAction() {
46     }
47
48     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
49         ActionErrors errors = new ActionErrors();
50
51         if(! isLoggedIn(request, response)) {
52             return mapping.findForward("login");
53         }
54
55         HttpSession session = request.getSession();
56         UserModel user = (UserModel) session.getAttribute(Constants.USER_KEY);
57         HashMap permissions = (HashMap) session.getAttribute(Constants.PERMISSIONS_KEY);
58         if(user == null || permissions == null) {
59             return mapping.findForward("login");
60         }
61
62         try {
63             InitialContext ic = new InitialContext();
64
65             Object JavaDoc isRef = ic.lookup("java:comp/env/" + IssueSearch.JNDI_NAME);
66             IssueSearchHome isHome = (IssueSearchHome) PortableRemoteObject.narrow(isRef, IssueSearchHome.class);
67             IssueSearch is = isHome.create();
68
69             IssueSearchQueryModel isqm = (IssueSearchQueryModel) session.getAttribute(Constants.SEARCH_QUERY_KEY);
70             if(isqm == null) {
71                 return mapping.findForward("searchissues");
72             }
73             processQueryParameters(isqm, (DynaValidatorForm) form, errors);
74
75             if(errors.isEmpty()) {
76                 IssueModel[] results = is.searchIssues(isqm, user, permissions);
77                 if(Logger.isLoggingDebug()) {
78                     Logger.logDebug("SearchIssuesAction received " + results.length + " results to query.");
79                 }
80
81                 String JavaDoc order = isqm.getOrderBy();
82                 if("id".equals(order)) {
83                     Arrays.sort(results, new IssueModel().new CompareById());
84                 } else if("sev".equals(order)) {
85                     Arrays.sort(results, new IssueModel().new CompareBySeverity());
86                 } else if("proj".equals(order)) {
87                     Arrays.sort(results, new IssueModel().new CompareByProjectAndStatus());
88                 } else if("owner".equals(order)) {
89                     Arrays.sort(results, new IssueModel().new CompareByOwnerAndStatus());
90                 } else if("lm".equals(order)) {
91                     Arrays.sort(results, new IssueModel().new CompareByDate(false));
92                 } else {
93                     Arrays.sort(results, new IssueModel());
94                 }
95
96                 isqm.setResults(results);
97                 Logger.logDebug("Setting search results with " + isqm.getResults().length + " results");
98                 session.setAttribute(Constants.SEARCH_QUERY_KEY, isqm);
99             }
100         } catch(IssueSearchException ise) {
101             if(ise.getType() == IssueSearchException.ERROR_NULL_QUERY) {
102                 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.nullsearch"));
103             } else {
104                 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.system"));
105             }
106         } catch(Exception JavaDoc e) {
107             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.system"));
108         }
109
110         if(! errors.isEmpty()) {
111             saveErrors(request, errors);
112         }
113
114         return mapping.getInputForward();
115     }
116
117     private IssueSearchQueryModel processQueryParameters(IssueSearchQueryModel isqm, DynaValidatorForm form, ActionErrors errors) {
118         if(isqm == null) {
119             isqm = new IssueSearchQueryModel();
120         }
121
122         try {
123             String JavaDoc creatorValue = (String JavaDoc) PropertyUtils.getSimpleProperty(form, "creator");
124             if(creatorValue != null && ! creatorValue.equals("") && ! creatorValue.equals("-1")) {
125                 isqm.setCreator(creatorValue);
126             } else {
127                 isqm.setCreator(null);
128             }
129
130             String JavaDoc contributorValue = (String JavaDoc) PropertyUtils.getSimpleProperty(form, "contributor");
131             if(contributorValue != null && ! contributorValue.equals("") && ! contributorValue.equals("-1")) {
132                 isqm.setContributor(contributorValue);
133             } else {
134                 isqm.setContributor(null);
135             }
136
137             String JavaDoc ownerValue = (String JavaDoc) PropertyUtils.getSimpleProperty(form, "owner");
138             if(ownerValue != null && ! ownerValue.equals("") && ! ownerValue.equals("-1")) {
139                 isqm.setOwner(ownerValue);
140             } else {
141                 isqm.setOwner(null);
142             }
143
144             String JavaDoc textValue = (String JavaDoc) PropertyUtils.getSimpleProperty(form, "textphrase");
145             if(textValue != null && ! textValue.equals("")) {
146                 isqm.setText(textValue);
147             } else {
148                 isqm.setText(null);
149             }
150
151             String JavaDoc resolutionValue = (String JavaDoc) PropertyUtils.getSimpleProperty(form, "resolution");
152             if(resolutionValue != null && ! resolutionValue.equals("")) {
153                 isqm.setResolution(resolutionValue);
154             } else {
155                 isqm.setResolution(null);
156             }
157
158             Integer JavaDoc[] projects = (Integer JavaDoc[]) PropertyUtils.getSimpleProperty(form, "projects");
159             if(projects == null || projects.length == 0) {
160                 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.projectrequired"));
161             } else {
162                 isqm.setProjects(projects);
163             }
164
165             Integer JavaDoc[] severities = (Integer JavaDoc[]) PropertyUtils.getSimpleProperty(form, "severities");
166             if(severities != null) {
167                 isqm.setSeverities(severities);
168             }
169
170             Integer JavaDoc[] statuses = (Integer JavaDoc[]) PropertyUtils.getSimpleProperty(form, "statuses");
171             if(statuses != null) {
172                 isqm.setStatuses(statuses);
173             }
174
175             Integer JavaDoc[] components = (Integer JavaDoc[]) PropertyUtils.getSimpleProperty(form, "components");
176             if(components != null) {
177                 isqm.setComponents(components);
178             }
179
180             Integer JavaDoc[] versions = (Integer JavaDoc[]) PropertyUtils.getSimpleProperty(form, "versions");
181             if(versions != null) {
182                 isqm.setVersions(versions);
183             }
184
185             Integer JavaDoc targetVersion = (Integer JavaDoc) PropertyUtils.getSimpleProperty(form, "targetVersion");
186             if(targetVersion != null && ! targetVersion.equals("") && targetVersion.intValue() != -1) {
187                 isqm.setTargetVersion(targetVersion);
188             } else {
189                 isqm.setTargetVersion(null);
190             }
191
192             String JavaDoc orderBy = (String JavaDoc) PropertyUtils.getSimpleProperty(form, "orderBy");
193             if(orderBy != null && ! orderBy.equals("")) {
194                 isqm.setOrderBy(orderBy);
195             }
196
197             Integer JavaDoc type = (Integer JavaDoc) PropertyUtils.getSimpleProperty(form, "type");
198             if(type != null) {
199                 isqm.setType(type);
200             }
201         } catch(Exception JavaDoc e) {
202             Logger.logDebug("Unable to parse search query parameters: " + e.getMessage(), e);
203             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.invalidsearchquery"));
204         }
205
206         return isqm;
207     }
208 }
209   
Popular Tags