KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > espada > bugtracker > app > Project


1
2 package com.espada.bugtracker.app;
3
4 // Java core API
5
import java.io.*;
6 import java.sql.*;
7 import java.util.*;
8
9 // Espada core API
10
import com.espada.*;
11
12 // Espada DB Pool API
13
import com.espada.bugtracker.persistence.*;
14
15 /**
16 * This is the Project class.
17 *
18 * When an instance of Project is created, the class searches the database for an<BR>
19 * existing project with the corresponding project id, and fills up the object properties.<BR>
20 *
21 * To create a new project in the database, the static method Project.createProject() must be used.<BR>
22 *
23 *
24 * @version: $Id: Project.java,v 1.15 2001/04/17 12:43:02 manik Exp $<BR>
25 * @author: Manik Surtani<BR>
26 **/

27
28 public class Project
29 {
30
31   private int pid;
32
33   private String JavaDoc pname;
34
35
36   public Project(int prid)
37   {
38     try
39     {
40
41       Connection d = DatabaseConnectionPool.getConnection();
42       Statement st = d.createStatement();
43       ResultSet rs = st.executeQuery("select * from projects where pid=" + prid);
44
45       while (rs.next())
46       {
47
48         pid=rs.getInt(1);
49         pname=rs.getString(2);
50
51       }
52
53       st.close();
54       DatabaseConnectionPool.freeConnection(d);
55     }
56
57     catch (Exception JavaDoc E)
58     {
59
60       //Exception haddling
61

62     }
63
64   } //end of method Project
65

66   public Vector getProjectManagers()
67   {
68
69       Vector v = new Vector();
70       try {
71       Connection d = DatabaseConnectionPool.getConnection();
72       Statement st = d.createStatement();
73       ResultSet rs = st.executeQuery("select uid from projectuser where pid=" + pid + " and rid=2");
74       while (rs.next())
75       {
76
77       v.add( new User(rs.getInt(1)) );
78       }
79
80       st.close();
81       DatabaseConnectionPool.freeConnection(d);
82         }
83
84     catch (Exception JavaDoc E)
85     {
86
87       //Exception haddling
88

89     }
90         
91         return v;
92
93   }
94
95   public String JavaDoc getRoleID(int uid)
96   {
97
98         return getRole("select rid from projectuser where uid = " + uid + " and pid=" + pid);
99
100   } //end of method getRoleID
101

102   public String JavaDoc getRoleName(String JavaDoc uid)
103   {
104
105         String JavaDoc s=null;
106         try
107         {
108             s = getRole("select roleDesc from roles, projectuser where projectuser.rid = roles.rid and projectuser.uid = " + uid + " and pid=" + pid);
109         } catch (NullPointerException JavaDoc npex)
110         {
111             
112         }
113         finally
114         {
115             if (s==null)
116                 s="-- Not Assigned --";
117         }
118         return s;
119         
120
121   } //end of method Project
122

123   private String JavaDoc getRole(String JavaDoc SQL)
124   {
125
126     String JavaDoc v = null;
127
128     try
129     {
130
131       Connection d = DatabaseConnectionPool.getConnection();
132       Statement st = d.createStatement();
133       ResultSet rs = st.executeQuery(SQL);
134
135       while (rs.next())
136       {
137
138             v = rs.getString( 1 );
139
140       }
141
142       st.close();
143       DatabaseConnectionPool.freeConnection(d);
144
145     }
146
147     catch (Exception JavaDoc E)
148     {
149
150
151     }
152         return v;
153
154   } //end of method getRole
155

156
157   public boolean delete()
158   {
159
160     try
161     {
162
163       Connection d = DatabaseConnectionPool.getConnection();
164       Statement st = d.createStatement();
165       int rs = st.executeUpdate("delete from projects where pid=" + pid);
166       st.close();
167       DatabaseConnectionPool.freeConnection(d);
168       return (rs != 0);
169
170     }
171
172     catch (Exception JavaDoc E)
173     {
174
175         return false;
176
177     }
178   
179   } //end of method delete
180

181
182   public static Vector getProjects()
183   {
184
185       return getProjectsDef("select * from projects order by pid");
186
187   } //end of method getProjects
188

189
190   public static Vector getProjectsByUser(int uid)
191   {
192
193         return getProjectsDef("select projects.* from projects, projectuser where projectuser.uid=" + uid + " and projectuser.pid=projects.pid order by projects.pid");
194
195   } //end of method getProjectsByUser
196

197
198   public static Vector getProjectsByUserRole(int uid,int rid)
199   {
200
201         return getProjectsDef("select projects.pid from projects,projectuser where projectuser.uid=" + uid + " and projectuser.rid=" + rid + " and projectuser.pid=projects.pid");
202
203   } //end of method getProjectsByUserRole
204

205
206   private static Vector getProjectsDef(String JavaDoc SQL)
207   {
208
209     Vector v = new Vector();
210
211     try
212     {
213
214       Connection d = DatabaseConnectionPool.getConnection();
215       Statement st = d.createStatement();
216       ResultSet rs = st.executeQuery(SQL);
217
218       while (rs.next())
219       {
220             v.add( new Project( rs.getInt( 1 ) ) );
221       }
222
223       st.close();
224       DatabaseConnectionPool.freeConnection(d);
225
226       return v;
227     }
228
229     catch (Exception JavaDoc E)
230     {
231
232           E.printStackTrace();
233           return v;
234
235     }
236
237   } //end of method getProjectsDef
238

239
240   public int getPID()
241   {
242
243         return pid;
244
245   } //end of method getPID
246

247
248   public String JavaDoc getName()
249   {
250
251         return pname;
252
253   } //end of method getName
254

255
256   public static Project createProject(String JavaDoc pn,int uid)
257     throws CannotCreateProjectException
258   {
259
260     Project p = null;
261
262     try
263     {
264
265       Connection d = DatabaseConnectionPool.getConnection();
266
267       try
268       {
269
270         Statement st = d.createStatement();
271         st.executeQuery("insert into projects values(0, '" + pn + "')");
272         ResultSet rs = st.executeQuery("select pid from projects where pname='" + pn + "'");
273
274         int pd = 0;
275
276         while(rs.next())
277         {
278
279               pd = rs.getInt(1);
280               p = new Project(pd);
281
282         }
283
284         /************ now lets update users and roles. Make everyone a User/Bug Tester
285         String SQL = "insert into projectuser select " + pd + ", uid, 4 from user";***/

286
287         /****** now lets make the user/UID as the site admin *******/
288
289         String JavaDoc SQL = "insert into projectuser select " + pd + " , " + uid + " , 1 from user";
290
291         st.executeQuery(SQL);
292         st.close();
293
294       }
295
296       finally
297       {
298
299           if(d!=null)
300           {
301
302                 DatabaseConnectionPool.freeConnection(d);
303           }
304
305       }
306     }
307
308     catch(Exception JavaDoc e)
309     {
310
311           throw new CannotCreateProjectException(pn,e);
312
313     }
314
315     return p;
316
317   } //end of method createProject
318

319
320 } //end of class
321
Popular Tags