001: package com.espada.bugtracker.app;
002:
003: // Java core API
004: import java.io.*;
005: import java.sql.*;
006: import java.util.*;
007:
008: // Espada core API
009: import com.espada.*;
010:
011: // Espada DB Pool API
012: import com.sr.espada.se.util.db.pool.*;
013: import com.sr.espada.se.util.logwriter.*;
014:
015: /**
016: * This is the Project class.
017: *
018: * When an instance of Project is created, the class searches the database for an<BR>
019: * existing project with the corresponding project id, and fills up the object properties.<BR>
020: *
021: * To create a new project in the database, the static method Project.createProject() must be used.<BR>
022: *
023: *
024: * @version: $Id: Project.java,v 1.13 2001/04/16 09:26:58 manik Exp $<BR>
025: * @author: Manik Surtani<BR>
026: **/
027:
028: public class Project {
029:
030: private int pid;
031:
032: private String pname;
033:
034: public Project(int prid) {
035: try {
036:
037: Connection d = DatabaseConnectionPool.getConnection();
038: Statement st = d.createStatement();
039: ResultSet rs = st
040: .executeQuery("select * from projects where pid="
041: + prid);
042:
043: while (rs.next()) {
044:
045: pid = rs.getInt(1);
046: pname = rs.getString(2);
047:
048: }
049:
050: st.close();
051: DatabaseConnectionPool.freeConnection(d);
052: }
053:
054: catch (Exception E) {
055:
056: //Exception haddling
057:
058: }
059:
060: } //end of method Project
061:
062: public Vector getProjectManagers() {
063:
064: Vector v = new Vector();
065: try {
066: Connection d = DatabaseConnectionPool.getConnection();
067: Statement st = d.createStatement();
068: ResultSet rs = st
069: .executeQuery("select uid from projectuser where pid="
070: + pid + " and rid=2");
071: while (rs.next()) {
072:
073: v.add(new User(rs.getInt(1)));
074: }
075:
076: st.close();
077: DatabaseConnectionPool.freeConnection(d);
078: }
079:
080: catch (Exception E) {
081: LogWriter.write(E.getMessage());
082:
083: //Exception haddling
084:
085: }
086:
087: return v;
088:
089: }
090:
091: public String getRoleID(int uid) {
092:
093: return getRole("select rid from projectuser where uid = " + uid
094: + " and pid=" + pid);
095:
096: } //end of method getRoleID
097:
098: public String getRoleName(String uid) {
099:
100: String s = null;
101: try {
102: s = getRole("select roleDesc from roles, projectuser where projectuser.rid = roles.rid and projectuser.uid = "
103: + uid + " and pid=" + pid);
104: } catch (NullPointerException npex) {
105:
106: } finally {
107: if (s == null)
108: s = "-- Not Assigned --";
109: }
110: return s;
111:
112: } //end of method Project
113:
114: private String getRole(String SQL) {
115:
116: String v = null;
117:
118: try {
119:
120: Connection d = DatabaseConnectionPool.getConnection();
121: Statement st = d.createStatement();
122: ResultSet rs = st.executeQuery(SQL);
123:
124: while (rs.next()) {
125:
126: v = rs.getString(1);
127:
128: }
129:
130: st.close();
131: DatabaseConnectionPool.freeConnection(d);
132:
133: }
134:
135: catch (Exception E) {
136:
137: }
138: return v;
139:
140: } //end of method getRole
141:
142: public boolean delete() {
143:
144: try {
145:
146: Connection d = DatabaseConnectionPool.getConnection();
147: Statement st = d.createStatement();
148: int rs = st.executeUpdate("delete from projects where pid="
149: + pid);
150: st.close();
151: DatabaseConnectionPool.freeConnection(d);
152: return (rs != 0);
153:
154: }
155:
156: catch (Exception E) {
157:
158: return false;
159:
160: }
161:
162: } //end of method delete
163:
164: public static Vector getProjects() {
165:
166: return getProjectsDef("select * from projects order by pid");
167:
168: } //end of method getProjects
169:
170: public static Vector getProjectsByUser(int uid) {
171:
172: return getProjectsDef("select projects.* from projects, projectuser where projectuser.uid="
173: + uid
174: + " and projectuser.pid=projects.pid order by projects.pid");
175:
176: } //end of method getProjectsByUser
177:
178: public static Vector getProjectsByUserRole(int uid, int rid) {
179:
180: return getProjectsDef("select projects.pid from projects,projectuser where projectuser.uid="
181: + uid
182: + " and projectuser.rid="
183: + rid
184: + " and projectuser.pid=projects.pid");
185:
186: } //end of method getProjectsByUserRole
187:
188: private static Vector getProjectsDef(String SQL) {
189:
190: Vector v = new Vector();
191:
192: try {
193:
194: Connection d = DatabaseConnectionPool.getConnection();
195: Statement st = d.createStatement();
196: ResultSet rs = st.executeQuery(SQL);
197:
198: while (rs.next()) {
199: v.add(new Project(rs.getInt(1)));
200: }
201:
202: st.close();
203: DatabaseConnectionPool.freeConnection(d);
204:
205: return v;
206: }
207:
208: catch (Exception E) {
209:
210: E.printStackTrace();
211: return v;
212:
213: }
214:
215: } //end of method getProjectsDef
216:
217: public int getPID() {
218:
219: return pid;
220:
221: } //end of method getPID
222:
223: public String getName() {
224:
225: return pname;
226:
227: } //end of method getName
228:
229: public static Project createProject(String pn, int uid)
230: throws CannotCreateProjectException {
231:
232: Project p = null;
233:
234: try {
235:
236: Connection d = DatabaseConnectionPool.getConnection();
237:
238: try {
239:
240: Statement st = d.createStatement();
241: st.executeQuery("insert into projects values(0, '" + pn
242: + "')");
243: ResultSet rs = st
244: .executeQuery("select pid from projects where pname='"
245: + pn + "'");
246:
247: int pd = 0;
248:
249: while (rs.next()) {
250:
251: pd = rs.getInt(1);
252: p = new Project(pd);
253:
254: }
255:
256: /************ now lets update users and roles. Make everyone a User/Bug Tester
257: String SQL = "insert into projectuser select " + pd + ", uid, 4 from user";***/
258:
259: /****** now lets make the user/UID as the site admin *******/
260:
261: String SQL = "insert into projectuser select " + pd
262: + " , " + uid + " , 1 from user";
263:
264: st.executeQuery(SQL);
265: st.close();
266:
267: }
268:
269: finally {
270:
271: if (d != null) {
272:
273: DatabaseConnectionPool.freeConnection(d);
274: }
275:
276: }
277: }
278:
279: catch (Exception e) {
280:
281: throw new CannotCreateProjectException(pn, e);
282:
283: }
284:
285: return p;
286:
287: } //end of method createProject
288:
289: } //end of class
|