1 2 package com.espada.bugtracker.app; 3 5 import java.io.*; 6 import java.sql.*; 7 import java.util.*; 8 9 10 import com.espada.bugtracker.persistence.*; 12 13 import com.espada.bugtracker.app.*; 15 16 28 29 public class Bug 30 { 31 32 35 public int bid; 36 37 40 public String title; 41 42 45 public String detail; 46 47 50 public boolean reproduced; 51 52 55 public Status status; 56 57 60 public Severity severity; 61 62 65 public User user; 66 67 70 public Project project; 71 72 75 public String bugURL; 76 77 80 public String browserVersion; 81 82 85 public String dateString; 86 87 public Bug() 88 { 89 90 92 } 93 94 public Bug(int bidn) 95 { 96 97 try 98 { 99 100 Connection d = DatabaseConnectionPool.getConnection(); 101 Statement st = d.createStatement(); 102 ResultSet rs = st.executeQuery("select * from bugs where bid=" + bidn); 103 104 while (rs.next()) 105 { 106 107 bid=rs.getInt(1); 108 109 title=unesc(rs.getString(2)); 110 111 detail=unesc(rs.getString(3)); 112 113 reproduced=(rs.getInt(4)==1); 114 115 status = new Status(rs.getInt(5)); 116 117 severity = new Severity(rs.getInt(6)); 118 119 user = new User(rs.getInt(7)); 120 121 project = new Project(rs.getInt(8)); 122 123 bugURL = rs.getString(9); 124 125 browserVersion = unesc(rs.getString(10)); 126 127 dateString = rs.getString(11); 128 129 } 130 131 st.close(); 132 DatabaseConnectionPool.freeConnection(d); 133 134 } 135 136 catch (Exception E) 137 { 138 139 141 } 142 143 144 } 146 147 public boolean delete() 148 { 149 150 try 151 { 152 153 Connection d = DatabaseConnectionPool.getConnection(); 154 Statement st = d.createStatement(); 155 int rs = st.executeUpdate("delete from bugs where bid=" + bid); 156 st.close(); 157 DatabaseConnectionPool.freeConnection(d); 158 return (rs != 0); 159 160 } 161 162 catch (Exception E) 163 { 164 165 return false; 166 167 } 168 169 } 171 172 175 public static Vector getAllBugs(Project p, String sortBy, Status st, int pageNumber, int trancheSize) 176 { 177 178 String limitClause = " limit " + (pageNumber * trancheSize) + "," + trancheSize; 179 180 String SQL = "select * from bugs where status=" + st.getID(); 181 SQL +=" and pid=" + p.getPID() + " order by " + sortBy + limitClause; 182 183 if( sortBy.compareTo("userid") == 0 ) 184 { 185 186 SQL = "select bugs.* from bugs, user where user.uid=bugs.userid and status=" + st.getID(); 187 SQL += " and bugs.pid=" + p.getPID() + " order by user.username" + limitClause; 188 189 } 190 191 if( sortBy.compareTo("severity") == 0 ) 192 { 193 194 SQL = "select bugs.* from bugs, severity where severity.severity_id=bugs.severity"; 195 SQL += " and status=" + st.getID() + " and bugs.pid=" + p.getPID() + " order by severity.weight desc" + limitClause; 196 } 197 198 return getBugs(SQL); 199 200 } 202 203 206 public static Vector getAllBugs(Project p, String sortBy, Severity sv, int pageNumber, int trancheSize) 207 { 208 209 String limitClause = " limit " + (pageNumber * trancheSize) + "," + trancheSize; 210 211 String SQL = "select * from bugs where severity=" + sv.getID(); 212 SQL +=" and pid=" + p.getPID() + " order by " + sortBy + limitClause; 213 214 if( sortBy.compareTo("userid") == 0 ) 215 { 216 SQL = "select bugs.* from bugs, user where user.uid=bugs.userid and severity=" + sv.getID(); 217 SQL += " and bugs.pid=" + p.getPID() + " order by user.username" + limitClause; 218 } 219 220 if( sortBy.compareTo("severity") == 0 ) 221 { 222 SQL = "select bugs.* from bugs, severity where severity.severity_id=bugs.severity"; 223 SQL += " and bugs.severity=" + sv.getID() + " and bugs.pid=" + p.getPID() + " order by severity.weight desc" + limitClause; 224 } 225 226 return getBugs(SQL); 227 228 } 230 231 234 public static Vector getAllBugs(Project p, String sortBy, Status st, Severity sv, int pageNumber, int trancheSize) 235 { 236 237 String limitClause =" limit " + (pageNumber * trancheSize) + "," + trancheSize; 238 String SQL = "select * from bugs where severity=" + sv.getID() + " and status=" + st.getID(); 239 SQL +=" and pid=" + p.getPID() + " order by " + sortBy + limitClause; 240 241 if( sortBy.compareTo("userid") == 0 ) 242 { 243 244 SQL = "select bugs.* from bugs, user where user.uid=bugs.userid and severity=" + sv.getID(); 245 SQL += " and status=" + st.getID() + " and bugs.pid=" + p.getPID() + " order by user.username" + limitClause; 246 247 } 248 249 if( sortBy.compareTo("severity") == 0 ) 250 { 251 252 SQL = "select bugs.* from bugs, severity where severity.severity_id=bugs.severity"; 253 SQL += " and status=" + st.getID() + " and bugs.severity=" + sv.getID() + " and bugs.pid=" + p.getPID() + " order by severity.weight desc" + limitClause; 254 255 } 256 257 return getBugs(SQL); 258 259 } 261 262 265 public static Vector getAllBugs(Project p, String sortBy) 266 { 267 268 String SQL = "select * from bugs where pid=" + p.getPID() + " order by " + sortBy; 269 270 if( sortBy.compareTo("userid") == 0 ) 271 { 272 273 SQL = "select bugs.* from bugs, user where user.uid=bugs.userid and bugs.pid=" + p.getPID() + " order by user.username"; 274 } 275 276 if( sortBy.compareTo("severity") == 0 ) 277 { 278 279 SQL = "select bugs.* from bugs, severity where severity.severity_id=bugs.severity and bugs.pid=" + p.getPID() + " order by severity.weight desc"; 280 281 } 282 283 return getBugs(SQL); 284 285 } 287 288 public static Vector getAllBugs(Project p, String sortBy, int pageNumber, int trancheSize ) 289 { 290 291 String limitClause = " limit " + (pageNumber * trancheSize) + "," + trancheSize; 292 String SQL = "select * from bugs where pid=" + p.getPID() + " order by " + sortBy + limitClause; 293 294 if( sortBy.compareTo("userid") == 0 ) 295 { 296 297 SQL = "select bugs.* from bugs, user where user.uid=bugs.userid and bugs.pid=" + p.getPID() + " order by user.username" + limitClause; 298 299 } 300 301 if( sortBy.compareTo("severity") == 0 ) 302 { 303 304 SQL = "select bugs.* from bugs, severity where severity.severity_id=bugs.severity and bugs.pid=" + p.getPID() + " order by severity.weight desc" + limitClause; 305 306 } 307 308 return getBugs(SQL); 309 310 } 312 313 private static Vector getBugs(String sql) 314 { 315 316 Vector v= new Vector(); 317 318 try 319 { 320 Connection d = DatabaseConnectionPool.getConnection(); 321 Statement st = d.createStatement(); 322 Bug _b = null; 323 ResultSet rs = st.executeQuery(sql); 324 while (rs.next()) 325 { 326 _b = new Bug(); 327 _b.bid=rs.getInt(1); 328 _b.title=unesc(rs.getString(2)); 329 _b.detail=unesc(rs.getString(3)); 330 _b.reproduced=(rs.getInt(4)==1); 331 _b.status = new Status(rs.getInt(5)); 332 _b.severity = new Severity(rs.getInt(6)); 333 _b.user = new User(rs.getInt(7)); 334 _b.project = new Project(rs.getInt(8)); 335 _b.bugURL = rs.getString(9); 336 _b.browserVersion = unesc(rs.getString(10)); 337 _b.dateString = rs.getString(11); 338 v.add( _b ); 339 } 340 st.close(); 341 DatabaseConnectionPool.freeConnection(d); 342 return v; 343 } 344 345 catch (Exception E) 346 { 347 v.add(new Bug()); 348 return v; 349 } 350 351 } 353 354 private static String esc(String s) 355 { 356 357 return s.replace('\'', '`'); 358 359 } 361 362 private static String unesc (String s) 363 { 364 365 return s.replace('`','\''); 366 367 } 369 370 373 public String update() 374 { 375 376 int rep = (reproduced)?1:0; 377 String SQL = "update bugs set detail='" + esc(detail) + "',"; 378 SQL += "reproduced=" + rep + ", status='" + status.getID() + "', severity='" + severity.getID(); 379 SQL += "', bugURL='" + bugURL + "', browserVersion='" + esc(browserVersion) + "' where bid=" + bid; 380 try 381 { 382 383 Bug b = null; 384 Connection d = DatabaseConnectionPool.getConnection(); 385 Statement st = d.createStatement(); 386 st.executeQuery(SQL); 387 st.close(); 388 DatabaseConnectionPool.freeConnection(d); 389 return SQL; 390 391 } 392 393 catch (Exception E) 394 { 395 396 return "Err " + E.getMessage() + SQL; 397 398 399 } 400 401 } 403 404 public static String createBug(String title, String detail, int reproduced, int status, int severity, int uid, int pid, String bugURL, String browserVersion) 405 { 406 407 String SQL = "insert into bugs values(0, '" + esc(title) + "', '" + esc(detail) + "',"; 408 SQL += reproduced + ", '" + status + "', '" + severity + "', " + uid + "," + pid + ", '"; 409 SQL += bugURL + "', '" + esc(browserVersion) + "', '" + new java.util.Date ().toString() + "')"; 410 try 411 { 412 413 Bug b = null; 414 Connection d = DatabaseConnectionPool.getConnection(); 415 Statement st = d.createStatement(); 416 st.executeQuery(SQL); 417 st.close(); 418 DatabaseConnectionPool.freeConnection(d); 419 return SQL; 420 421 } 422 423 catch (Exception E) 424 { 425 426 return "Err " + E.getMessage() + SQL; 427 428 } 429 430 } 432 433 } | Popular Tags |