KickJava   Java API By Example, From Geeks To Geeks.

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


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 SearchIssuesFormAction extends ITrackerAction {
44
45     public SearchIssuesFormAction() {
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
57         try {
58             InitialContext ic = new InitialContext();
59
60             Object JavaDoc phRef = ic.lookup("java:comp/env/" + ProjectHandler.JNDI_NAME);
61             ProjectHandlerHome phHome = (ProjectHandlerHome) PortableRemoteObject.narrow(phRef, ProjectHandlerHome.class);
62             ProjectHandler ph = phHome.create();
63
64             UserPreferencesModel userPrefs = (UserPreferencesModel) session.getAttribute(Constants.PREFERENCES_KEY);
65             HashMap userPermissions = (HashMap) session.getAttribute(Constants.PERMISSIONS_KEY);
66
67             String JavaDoc projectId = request.getParameter("pid");
68             String JavaDoc action = (String JavaDoc) PropertyUtils.getSimpleProperty((DynaValidatorForm) form, "action");
69
70             DynaValidatorForm searchForm = (DynaValidatorForm) form;
71             if(searchForm == null) {
72                 searchForm = new DynaValidatorForm();
73             }
74
75             boolean newQuery = false;
76             IssueSearchQueryModel query = (IssueSearchQueryModel) session.getAttribute(Constants.SEARCH_QUERY_KEY);
77
78             Logger.logDebug("projectid = " + projectId);
79             Logger.logDebug("query type = " + (query == null ? "NULL" : query.getType().toString()));
80             Logger.logDebug("query projectid = " + (query == null ? "NULL" : query.getProjectId().toString()));
81
82             if(query == null || query.getType() == null || "reset".equalsIgnoreCase(action) || (userPrefs != null && ! userPrefs.getRememberLastSearch())) {
83                 Logger.logDebug("New search query. No existing query, reset forced, or saved querys not allowed.");
84                 query = new IssueSearchQueryModel();
85                 query.setType(IssueSearchQueryModel.TYPE_FULL);
86                 newQuery = true;
87             } else if(query.getType().intValue() == IssueSearchQueryModel.TYPE_FULL.intValue() && projectId != null) {
88                 Logger.logDebug("New search query. Previous query FULL, new query PROJECT.");
89                 query = new IssueSearchQueryModel();
90                 query.setType(IssueSearchQueryModel.TYPE_PROJECT);
91                 newQuery = true;
92             } else if(query.getType().intValue() == IssueSearchQueryModel.TYPE_PROJECT.intValue()) {
93                 if(projectId == null || projectId.equals("")) {
94                     Logger.logDebug("New search query. Previous query PROJECT, new query FULL.");
95                     query = new IssueSearchQueryModel();
96                     query.setType(IssueSearchQueryModel.TYPE_FULL);
97                     newQuery = true;
98                 } else if(! projectId.equals(query.getProjectId().toString())) {
99                     Logger.logDebug("New search query. Requested project (" + projectId + ") different from previous query (" + query.getProjectId().toString() + ")");
100                     query = new IssueSearchQueryModel();
101                     query.setType(IssueSearchQueryModel.TYPE_PROJECT);
102                     newQuery = true;
103                 }
104             }
105
106             query.setAvailableProjects(null);
107
108             ProjectModel[] projects = ph.getAllAvailableProjects();
109             Arrays.sort(projects, new ProjectModel());
110
111             Vector availableProjectsVector = new Vector();
112             Vector selectedProjectsVector = new Vector();
113             for(int i = 0; i < projects.length; i++) {
114                 if(! UserUtilities.hasPermission(userPermissions, projects[i].getId(), UserUtilities.PERMISSION_VIEW_ALL) &&
115                    ! UserUtilities.hasPermission(userPermissions, projects[i].getId(), UserUtilities.PERMISSION_VIEW_USERS)) {
116                        continue;
117                 }
118                 Logger.logDebug("Adding project " + projects[i].getId() + " to list of available projects.");
119                 availableProjectsVector.add(projects[i]);
120
121                 if(projectId != null && projects[i].getId().toString().equals(projectId)) {
122                     query.setType(IssueSearchQueryModel.TYPE_PROJECT);
123                     query.setProject(projects[i]);
124                     break;
125                 } else {
126                     for(int j = 0; j < query.getProjects().length; j++) {
127                         if(query.getProjects()[j].equals(projects[i].getId())) {
128                             selectedProjectsVector.add(projects[i].getId());
129                             break;
130                         }
131                     }
132                 }
133             }
134             ProjectModel[] availableProjects = new ProjectModel[availableProjectsVector.size()];
135             availableProjectsVector.copyInto(availableProjects);
136             Logger.logDebug("Issue Search has " + availableProjects.length + " available projects.");
137
138             if(availableProjects.length != 0) {
139                 query.setAvailableProjects(availableProjects);
140                 if(query.getType().equals(IssueSearchQueryModel.TYPE_PROJECT)) {
141                     searchForm.set("project", query.getProjectId());
142                 }
143
144                 if(newQuery) {
145                     Logger.logDebug("New search query. Clearing results and setting defaults.");
146                     query.setResults(null);
147                     Vector selectedStatusesVector = new Vector();
148                     for(int i = 0; i < IssueUtilities.getStatuses().length; i++) {
149                         try {
150                             int statusNumber = Integer.parseInt(IssueUtilities.getStatuses()[i].getValue());
151                             if(statusNumber < IssueUtilities.STATUS_CLOSED) {
152                                 selectedStatusesVector.add(new Integer JavaDoc(statusNumber));
153                             }
154                         } catch(Exception JavaDoc e) {
155                             Logger.logDebug("Invalid status entry: " + IssueUtilities.getStatuses()[i]);
156                         }
157                     }
158                     Integer JavaDoc[] selectedStatuses = new Integer JavaDoc[selectedStatusesVector.size()];
159                     selectedStatusesVector.copyInto(selectedStatuses);
160                     searchForm.set("statuses", selectedStatuses);
161
162                     Integer JavaDoc[] selectedSeverities = new Integer JavaDoc[IssueUtilities.getNumberSeverities()];
163                     for(int i = 1; i <= IssueUtilities.getNumberSeverities(); i++) {
164                         selectedSeverities[i - 1] = new Integer JavaDoc(i);
165                     }
166                     searchForm.set("severities", selectedSeverities);
167                 } else {
168                     Integer JavaDoc[] selectedProjects = new Integer JavaDoc[selectedProjectsVector.size()];
169                     selectedProjectsVector.copyInto(selectedProjects);
170                     query.setProjects(selectedProjects);
171
172                     searchForm.set("components", query.getComponents());
173                     searchForm.set("contributor", query.getContributor());
174                     searchForm.set("creator", query.getCreator());
175                     searchForm.set("orderBy", query.getOrderBy());
176                     searchForm.set("project", query.getProjectId());
177                     searchForm.set("projects", query.getProjects());
178                     searchForm.set("resolution", query.getResolution());
179                     searchForm.set("severities", query.getSeverities());
180                     searchForm.set("statuses", query.getStatuses());
181                     searchForm.set("targetVersion", query.getTargetVersion());
182                     searchForm.set("textphrase", query.getText());
183                     searchForm.set("versions", query.getVersions());
184                 }
185
186                 request.setAttribute("searchForm", searchForm);
187                 session.setAttribute(Constants.SEARCH_QUERY_KEY, query);
188             } else {
189                 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.noprojects"));
190             }
191
192             if(errors.isEmpty()) {
193                 return mapping.getInputForward();
194             }
195         } catch(Exception JavaDoc e) {
196             Logger.logError("Exception while creating search issues form.", e);
197             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.system"));
198         }
199
200         if(! errors.isEmpty()) {
201             saveErrors(request, errors);
202         }
203
204         return mapping.findForward("error");
205     }
206
207 }
208   
Popular Tags