KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > ejb > beans > session > IssueSearchBean


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.ejb.beans.session;
20
21 import java.sql.*;
22 import java.rmi.*;
23 import java.util.*;
24 import javax.ejb.*;
25 import javax.naming.*;
26 import javax.rmi.*;
27 import javax.sql.*;
28
29 import cowsultants.itracker.ejb.client.exceptions.*;
30 import cowsultants.itracker.ejb.client.interfaces.*;
31 import cowsultants.itracker.ejb.client.models.*;
32 import cowsultants.itracker.ejb.client.util.*;
33
34
35 public class IssueSearchBean implements SessionBean {
36     private static String JavaDoc componentbeanTableName;
37     private static String JavaDoc componentbeanRelTableName;
38     private static String JavaDoc issuebeanTableName;
39     private static String JavaDoc issuehistorybeanTableName;
40     private static String JavaDoc projectbeanTableName;
41     private static String JavaDoc versionbeanTableName;
42     private static String JavaDoc versionbeanRelTableName;
43
44     private static DataSource ds;
45
46     private InitialContext ic = null;
47     private IssueHandlerHome ihHome = null;
48
49     public IssueModel[] searchIssues(IssueSearchQueryModel queryModel, UserModel user, HashMap permissions) throws IssueSearchException {
50         Connection conn = null;
51
52         if(queryModel == null) {
53             throw new IssueSearchException("Null search query received.", IssueSearchException.ERROR_NULL_QUERY);
54         }
55
56         try {
57             conn = ds.getConnection();
58
59             String JavaDoc queryString = "";
60             if(queryModel.getProjects() != null && queryModel.getProjects().length > 0) {
61                 Integer JavaDoc[] values = queryModel.getProjects();
62                 queryString += " AND project_id IN ( ";
63                 for(int i = 0; i < values.length; i++) {
64                     queryString += (i == 0 ? "": ", ") + values[i].toString();
65                 }
66                 queryString += " )";
67             }
68
69             if(queryModel.getSeverities() != null && queryModel.getSeverities().length > 0) {
70                 Integer JavaDoc[] values = queryModel.getSeverities();
71                 queryString += " AND severity IN ( ";
72                 for(int i = 0; i < values.length; i++) {
73                     queryString += (i == 0 ? "": ", ") + values[i].toString();
74                 }
75                 queryString += " )";
76             }
77
78             if(queryModel.getStatuses() != null && queryModel.getStatuses().length > 0) {
79                 Integer JavaDoc[] values = queryModel.getStatuses();
80                 queryString += " AND i.status IN ( ";
81                 for(int i = 0; i < values.length; i++) {
82                     queryString += (i == 0 ? "": ", ") + values[i].toString();
83                 }
84                 queryString += " )";
85             }
86
87             boolean hasComponents = false;
88             if(queryModel.getComponents() != null && queryModel.getComponents().length > 0) {
89                 hasComponents = true;
90                 Integer JavaDoc[] values = queryModel.getComponents();
91                 queryString += " AND c.component_id IN ( ";
92                 for(int i = 0; i < values.length; i++) {
93                     queryString += (i == 0 ? "": ", ") + values[i].toString();
94                 }
95                 queryString += " )";
96             }
97
98             boolean hasVersions = false;
99             if(queryModel.getVersions() != null && queryModel.getVersions().length > 0) {
100                 hasVersions = true;
101                 Integer JavaDoc[] values = queryModel.getVersions();
102                 queryString += " AND v.version_id IN ( ";
103                 for(int i = 0; i < values.length; i++) {
104                     queryString += (i == 0 ? "": ", ") + values[i].toString();
105                 }
106                 queryString += " )";
107             }
108
109             if(queryModel.getTargetVersion() != null) {
110                 queryString += " AND i.target_version_id = " + queryModel.getTargetVersion();
111             }
112
113             if(queryModel.getOwner() != null) {
114                 queryString += " AND i.owner_id = " + queryModel.getOwner();
115             }
116
117             if(queryModel.getCreator() != null) {
118                 queryString += " AND i.creator_id = " + queryModel.getCreator();
119             }
120
121             if(queryModel.getContributor() != null) {
122                 queryString += " AND (ih.user_id = " + queryModel.getContributor() + " OR i.creator_id = " + queryModel.getCreator() + " OR i.owner_id = " + queryModel.getOwner() + ")";
123             }
124
125             if(queryModel.getText() != null && ! queryModel.getText().equals("")) {
126                 queryString += " AND (i.description like '%" + HTMLUtilities.removeQuotes(queryModel.getText()) + "%' OR " +
127                                "ih.description like '%" + HTMLUtilities.removeQuotes(queryModel.getText()) + "%')";
128             }
129
130             if(queryModel.getResolution() != null && ! queryModel.getResolution().equals("")) {
131                 queryString += " AND i.resolution like '%" + HTMLUtilities.removeQuotes(queryModel.getResolution()) + "%'";
132             }
133
134             queryString = "select distinct i.id as id from " +
135                             issuebeanTableName + " i, " +
136                             (hasComponents ? componentbeanRelTableName + " c, " : "") +
137                             (hasVersions ? versionbeanRelTableName + " v, " : "") +
138                             issuehistorybeanTableName + " ih " +
139                           "where ih.issue_id = i.id" +
140                             (hasComponents ? " AND i.id = c.issue_id" : "") +
141                             (hasVersions ? " AND i.id = v.issue_id" : "") + queryString;
142             if(Logger.isLoggingDebug()) {
143                 Logger.logDebug("Searching for issues using the following query: " + queryString);
144             }
145
146             PreparedStatement pstmt = conn.prepareStatement(queryString);
147             ResultSet rs = pstmt.executeQuery();
148
149             Vector issuesFound = new Vector();
150             while(rs.next()) {
151                 issuesFound.add(new Integer JavaDoc(rs.getInt("id")));
152             }
153             rs.close();
154             pstmt.close();
155             if(conn != null) {
156                 conn.close();
157             }
158
159             IssueHandler ih = ihHome.create();
160             Vector issuesAvailable = new Vector();
161             for(int i = 0; i < issuesFound.size(); i++) {
162                 IssueModel issue = ih.getIssue((Integer JavaDoc) issuesFound.elementAt(i));
163
164                 if(IssueUtilities.canViewIssue(issue, user, permissions)) {
165                    issuesAvailable.add(issue);
166                     if(Logger.isLoggingDebug()) {
167                         Logger.logDebug("Adding viewable issue " + issue.getId() + " to user results.");
168                     }
169                 } else if(Logger.isLoggingDebug()) {
170                     Logger.logDebug("Skipping nonviewable issue " + issue.getId());
171                 }
172             }
173
174             IssueModel[] issues = new IssueModel[issuesAvailable.size()];
175             issuesAvailable.copyInto(issues);
176
177             return issues;
178         } catch(CreateException ce) {
179             throw new IssueSearchException("CreateException caught while performing search. ", IssueSearchException.ERROR_EJB_EXCEPTION);
180         } catch(SQLException sqle) {
181             if(Logger.isLoggingDebug()) {
182                 Logger.logDebug("SQLException while searching for issue ids. " + sqle.getMessage());
183             }
184             throw new IssueSearchException("SQL Exception caught while performing search.", IssueSearchException.ERROR_SQL_EXCEPTION);
185         } finally {
186             try {
187                 if(conn != null) {
188                     conn.close();
189                 }
190             } catch(SQLException sqle) {
191             }
192         }
193     }
194
195     public void ejbCreate() {
196         try {
197             ic = new InitialContext();
198             Object JavaDoc ihRef = ic.lookup("java:comp/env/" + IssueHandler.JNDI_NAME);
199             ihHome = (IssueHandlerHome) PortableRemoteObject.narrow(ihRef, IssueHandlerHome.class);
200
201             Object JavaDoc scRef = ic.lookup("java:comp/env/" + SystemConfiguration.JNDI_NAME);
202             SystemConfigurationHome scHome = (SystemConfigurationHome) PortableRemoteObject.narrow(scRef, SystemConfigurationHome.class);
203             SystemConfiguration sc = scHome.create();
204
205             componentbeanTableName = sc.getProperty("componentbean_table_name", SystemConfigurationUtilities.DEFAULT_COMPONENTBEAN_TABLE_NAME);
206             componentbeanRelTableName = sc.getProperty("componentbean_rel_table_name", SystemConfigurationUtilities.DEFAULT_COMPONENTBEAN_REL_TABLE_NAME);
207             issuebeanTableName = sc.getProperty("issuebean_table_name", SystemConfigurationUtilities.DEFAULT_ISSUEBEAN_TABLE_NAME);
208             issuehistorybeanTableName = sc.getProperty("issuehistorybean_table_name", SystemConfigurationUtilities.DEFAULT_ISSUEHISTORYBEAN_TABLE_NAME);
209             projectbeanTableName = sc.getProperty("projectbean_table_name", SystemConfigurationUtilities.DEFAULT_PROJECTBEAN_TABLE_NAME);
210             versionbeanTableName = sc.getProperty("versionbean_table_name", SystemConfigurationUtilities.DEFAULT_VERSIONBEAN_TABLE_NAME);
211             versionbeanRelTableName = sc.getProperty("versionbean_rel_table_name", SystemConfigurationUtilities.DEFAULT_VERSIONBEAN_REL_TABLE_NAME);
212
213             ds = (DataSource) ic.lookup(sc.getProperty("default_ds", SystemConfigurationUtilities.DEFAULT_DATASOURCE));
214         } catch(CreateException ce) {
215         } catch(NamingException ne) {
216             Logger.logError("Exception while looking up home interfaces.", ne);
217         }
218     }
219
220     public void setSessionContext(SessionContext value) {}
221     public void ejbActivate() {}
222     public void ejbPassivate() {}
223     public void ejbRemove() {}
224 }
225   
Popular Tags