001: /*
002: * Copyright (c) 2000, Manik Surtani <manik@post1.com> All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions
006: * are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: *
011: * 2. Redistributions in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in
013: * the documentation and/or other materials provided with the
014: * distribution.
015: *
016: * 3. All advertising materials mentioning features or use of this
017: * software must display the following acknowledgment:
018: * "This product includes software developed by Manik Surtani
019: * for use in the "BugTracker" JSP/servlet application
020: * <http://loonix.espadanet.com/>."
021: *
022: * 4. The name "BugTracker" must not be used to endorse or promote products
023: * derived from this software without prior written permission.
024: *
025: * 5. Products derived from this software may not be called "BugTracker"
026: * nor may "BugTracker" nor "Loonix" appear in their names without
027: * prior written permission of Manik Surtani.
028: *
029: * 6. Redistributions of any form whatsoever must retain the following
030: * acknowledgment:
031: * "This product includes software developed by Manik Surtani
032: * for use in the "BugTracker" JSP/servlet application
033: * <http://loonix.espadanet.com/>."
034: *
035: * THIS SOFTWARE IS PROVIDED BY MANIK SURTANI PROJECT "AS IS" AND ANY
036: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
037: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
038: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JAVA APACHE PROJECT OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
041: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
042: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
043: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
044: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
045: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
046: * OF THE POSSIBILITY OF SUCH DAMAGE.
047: *
048: *
049: */
050:
051: package com.espada.bugtracker.app;
052:
053: // Java core packages:
054:
055: import java.io.*;
056: import java.sql.*;
057: import java.util.*;
058:
059: // Espada Logwriter API
060: import com.sr.espada.se.util.logwriter.LogWriter;
061:
062: // Espada DB Pool API
063: import com.sr.espada.se.util.db.pool.*;
064:
065: // Bugtracker API
066: import com.espada.bugtracker.app.*;
067:
068: /**
069: * This is the Bug class, which is the core of the entire application.
070: *
071: * When an instance of Bug is created, the class searches the database for an<BR>
072: * existing bug with the corresponding bug id, and fills up the object properties.<BR>
073: *
074: * To create a new bug in the database, the static method Bug.createBug() must be used.<BR>
075: *
076: *
077: * @version: $Id: Bug.java,v 1.5 2001/03/13 07:43:44 manik Exp $<BR>
078: * @author: Manik Surtani<BR>
079: **/
080:
081: public class Bug {
082:
083: /**
084: * The bug ID of this bug
085: **/
086: public int bid;
087:
088: /**
089: * The title of this bug
090: **/
091: public String title;
092:
093: /**
094: * The bug description of this bug
095: **/
096: public String detail;
097:
098: /**
099: * Has this bug been reproduced?
100: **/
101: public boolean reproduced;
102:
103: /**
104: * The current status of this bug
105: **/
106: public Status status;
107:
108: /**
109: * The severity of this bug
110: **/
111: public Severity severity;
112:
113: /**
114: * The User who reported this bug
115: **/
116: public User user;
117:
118: /**
119: * The Project to which this bug belongs
120: **/
121: public Project project;
122:
123: /**
124: * The URL of this bug
125: **/
126: public String bugURL;
127:
128: /**
129: * The browser type/version used when this bug was found
130: **/
131: public String browserVersion;
132:
133: /**
134: * The date and time of reporting.
135: **/
136: public String dateString;
137:
138: public Bug() {
139:
140: //Defult values
141:
142: }
143:
144: public Bug(int bidn) {
145:
146: try {
147:
148: Connection d = DatabaseConnectionPool.getConnection();
149: Statement st = d.createStatement();
150: ResultSet rs = st
151: .executeQuery("select * from bugs where bid="
152: + bidn);
153:
154: while (rs.next()) {
155:
156: bid = rs.getInt(1);
157:
158: title = unesc(rs.getString(2));
159:
160: detail = unesc(rs.getString(3));
161:
162: reproduced = (rs.getInt(4) == 1);
163:
164: status = new Status(rs.getInt(5));
165:
166: severity = new Severity(rs.getInt(6));
167:
168: user = new User(rs.getInt(7));
169:
170: project = new Project(rs.getInt(8));
171:
172: bugURL = rs.getString(9);
173:
174: browserVersion = unesc(rs.getString(10));
175:
176: dateString = rs.getString(11);
177:
178: }
179:
180: st.close();
181: DatabaseConnectionPool.freeConnection(d);
182:
183: }
184:
185: catch (Exception E) {
186:
187: //Exception handlling
188:
189: }
190:
191: } //end of method Bug
192:
193: public boolean delete() {
194:
195: try {
196:
197: Connection d = DatabaseConnectionPool.getConnection();
198: Statement st = d.createStatement();
199: int rs = st.executeUpdate("delete from bugs where bid="
200: + bid);
201: st.close();
202: DatabaseConnectionPool.freeConnection(d);
203: return (rs != 0);
204:
205: }
206:
207: catch (Exception E) {
208:
209: return false;
210:
211: }
212:
213: } //end of method delete
214:
215: /**
216: * Returns a vector of Bug objects related to project p.
217: **/
218: public static Vector getAllBugs(Project p, String sortBy,
219: Status st, int pageNumber, int trancheSize) {
220:
221: String limitClause = " limit " + (pageNumber * trancheSize)
222: + "," + trancheSize;
223:
224: String SQL = "select * from bugs where status=" + st.getID();
225: SQL += " and pid=" + p.getPID() + " order by " + sortBy
226: + limitClause;
227:
228: if (sortBy.compareTo("userid") == 0) {
229:
230: SQL = "select bugs.* from bugs, user where user.uid=bugs.userid and status="
231: + st.getID();
232: SQL += " and bugs.pid=" + p.getPID()
233: + " order by user.username" + limitClause;
234:
235: }
236:
237: if (sortBy.compareTo("severity") == 0) {
238:
239: SQL = "select bugs.* from bugs, severity where severity.severity_id=bugs.severity";
240: SQL += " and status=" + st.getID() + " and bugs.pid="
241: + p.getPID() + " order by severity.weight desc"
242: + limitClause;
243: }
244:
245: return getBugs(SQL);
246:
247: } //end of method getAllBugs
248:
249: /**
250: * Returns a vector of Bug objects related to project p.
251: **/
252: public static Vector getAllBugs(Project p, String sortBy,
253: Severity sv, int pageNumber, int trancheSize) {
254:
255: String limitClause = " limit " + (pageNumber * trancheSize)
256: + "," + trancheSize;
257:
258: String SQL = "select * from bugs where severity=" + sv.getID();
259: SQL += " and pid=" + p.getPID() + " order by " + sortBy
260: + limitClause;
261:
262: if (sortBy.compareTo("userid") == 0) {
263: SQL = "select bugs.* from bugs, user where user.uid=bugs.userid and severity="
264: + sv.getID();
265: SQL += " and bugs.pid=" + p.getPID()
266: + " order by user.username" + limitClause;
267: }
268:
269: if (sortBy.compareTo("severity") == 0) {
270: SQL = "select bugs.* from bugs, severity where severity.severity_id=bugs.severity";
271: SQL += " and bugs.severity=" + sv.getID()
272: + " and bugs.pid=" + p.getPID()
273: + " order by severity.weight desc" + limitClause;
274: }
275:
276: return getBugs(SQL);
277:
278: } //end of method getAllBugs
279:
280: /**
281: * Returns a vector of Bug objects related to project p.
282: **/
283: public static Vector getAllBugs(Project p, String sortBy,
284: Status st, Severity sv, int pageNumber, int trancheSize) {
285:
286: String limitClause = " limit " + (pageNumber * trancheSize)
287: + "," + trancheSize;
288: String SQL = "select * from bugs where severity=" + sv.getID()
289: + " and status=" + st.getID();
290: SQL += " and pid=" + p.getPID() + " order by " + sortBy
291: + limitClause;
292:
293: if (sortBy.compareTo("userid") == 0) {
294:
295: SQL = "select bugs.* from bugs, user where user.uid=bugs.userid and severity="
296: + sv.getID();
297: SQL += " and status=" + st.getID() + " and bugs.pid="
298: + p.getPID() + " order by user.username"
299: + limitClause;
300:
301: }
302:
303: if (sortBy.compareTo("severity") == 0) {
304:
305: SQL = "select bugs.* from bugs, severity where severity.severity_id=bugs.severity";
306: SQL += " and status=" + st.getID() + " and bugs.severity="
307: + sv.getID() + " and bugs.pid=" + p.getPID()
308: + " order by severity.weight desc" + limitClause;
309:
310: }
311:
312: return getBugs(SQL);
313:
314: } //end of method getAllBugs
315:
316: /**
317: * Returns a vector of Bug objects related to project p.
318: **/
319: public static Vector getAllBugs(Project p, String sortBy) {
320:
321: String SQL = "select * from bugs where pid=" + p.getPID()
322: + " order by " + sortBy;
323:
324: if (sortBy.compareTo("userid") == 0) {
325:
326: SQL = "select bugs.* from bugs, user where user.uid=bugs.userid and bugs.pid="
327: + p.getPID() + " order by user.username";
328: }
329:
330: if (sortBy.compareTo("severity") == 0) {
331:
332: SQL = "select bugs.* from bugs, severity where severity.severity_id=bugs.severity and bugs.pid="
333: + p.getPID() + " order by severity.weight desc";
334:
335: }
336:
337: return getBugs(SQL);
338:
339: } //end of method getAllBugs
340:
341: public static Vector getAllBugs(Project p, String sortBy,
342: int pageNumber, int trancheSize) {
343:
344: String limitClause = " limit " + (pageNumber * trancheSize)
345: + "," + trancheSize;
346: String SQL = "select * from bugs where pid=" + p.getPID()
347: + " order by " + sortBy + limitClause;
348:
349: if (sortBy.compareTo("userid") == 0) {
350:
351: SQL = "select bugs.* from bugs, user where user.uid=bugs.userid and bugs.pid="
352: + p.getPID()
353: + " order by user.username"
354: + limitClause;
355:
356: }
357:
358: if (sortBy.compareTo("severity") == 0) {
359:
360: SQL = "select bugs.* from bugs, severity where severity.severity_id=bugs.severity and bugs.pid="
361: + p.getPID()
362: + " order by severity.weight desc"
363: + limitClause;
364:
365: }
366:
367: return getBugs(SQL);
368:
369: } //end of method getAllBugs
370:
371: private static Vector getBugs(String sql) {
372:
373: LogWriter.write("******\n");
374: LogWriter.write("Exec: " + sql + "\n");
375: LogWriter.write("******\n");
376: Vector v = new Vector();
377:
378: try {
379: Connection d = DatabaseConnectionPool.getConnection();
380: Statement st = d.createStatement();
381: Bug _b = null;
382: ResultSet rs = st.executeQuery(sql);
383: while (rs.next()) {
384: _b = new Bug();
385: _b.bid = rs.getInt(1);
386: _b.title = unesc(rs.getString(2));
387: _b.detail = unesc(rs.getString(3));
388: _b.reproduced = (rs.getInt(4) == 1);
389: _b.status = new Status(rs.getInt(5));
390: _b.severity = new Severity(rs.getInt(6));
391: _b.user = new User(rs.getInt(7));
392: _b.project = new Project(rs.getInt(8));
393: _b.bugURL = rs.getString(9);
394: _b.browserVersion = unesc(rs.getString(10));
395: _b.dateString = rs.getString(11);
396: v.add(_b);
397: }
398: st.close();
399: DatabaseConnectionPool.freeConnection(d);
400: return v;
401: }
402:
403: catch (Exception E) {
404: LogWriter.write("Bug.getAllBugs Error: \n");
405: LogWriter.write(E.getMessage());
406: LogWriter.write("Params: SQL was \n");
407: LogWriter.write(sql + "\n\n");
408: v.add(new Bug());
409: return v;
410: }
411:
412: } //end of method getBugs
413:
414: private static String esc(String s) {
415:
416: return s.replace('\'', '`');
417:
418: } //end of method esc
419:
420: private static String unesc(String s) {
421:
422: return s.replace('`', '\'');
423:
424: } //end of method unesc
425:
426: /**
427: * Updates the values of this bug in the database
428: **/
429: public String update() {
430:
431: int rep = (reproduced) ? 1 : 0;
432: String SQL = "update bugs set detail='" + esc(detail) + "',";
433: SQL += "reproduced=" + rep + ", status='" + status.getID()
434: + "', severity='" + severity.getID();
435: SQL += "', bugURL='" + bugURL + "', browserVersion='"
436: + esc(browserVersion) + "' where bid=" + bid;
437: try {
438:
439: Bug b = null;
440: Connection d = DatabaseConnectionPool.getConnection();
441: Statement st = d.createStatement();
442: st.executeQuery(SQL);
443: st.close();
444: DatabaseConnectionPool.freeConnection(d);
445: return SQL;
446:
447: }
448:
449: catch (Exception E) {
450:
451: return "Err " + E.getMessage() + SQL;
452:
453: }
454:
455: } //end of method update
456:
457: public static String createBug(String title, String detail,
458: int reproduced, int status, int severity, int uid, int pid,
459: String bugURL, String browserVersion) {
460:
461: String SQL = "insert into bugs values(0, '" + esc(title)
462: + "', '" + esc(detail) + "',";
463: SQL += reproduced + ", '" + status + "', '" + severity + "', "
464: + uid + "," + pid + ", '";
465: SQL += bugURL + "', '" + esc(browserVersion) + "', '"
466: + new java.util.Date().toString() + "')";
467: try {
468:
469: Bug b = null;
470: Connection d = DatabaseConnectionPool.getConnection();
471: Statement st = d.createStatement();
472: st.executeQuery(SQL);
473: st.close();
474: DatabaseConnectionPool.freeConnection(d);
475: return SQL;
476:
477: }
478:
479: catch (Exception E) {
480:
481: return "Err " + E.getMessage() + SQL;
482:
483: }
484:
485: } //end of method createBug
486:
487: } //end of class
|