1 32 33 package com.knowgate.projtrack; 34 35 import com.knowgate.debug.DebugFile; 36 37 import com.knowgate.dataobjs.DB; 38 import com.knowgate.jdc.JDCConnection; 39 import com.knowgate.dataobjs.DBBind; 40 import com.knowgate.dataobjs.DBPersist; 41 import com.knowgate.dataobjs.DBSubset; 42 import com.knowgate.lucene.Indexer; 43 import com.knowgate.lucene.BugIndexer; 44 45 import com.knowgate.misc.Gadgets; 46 47 import java.util.HashMap ; 48 import java.util.Iterator ; 49 import java.util.Properties ; 50 51 import java.io.FileNotFoundException ; 52 import java.io.IOException ; 53 54 import java.sql.Connection ; 55 import java.sql.SQLException ; 56 import java.sql.CallableStatement ; 57 import java.sql.PreparedStatement ; 58 import java.sql.Statement ; 59 import java.sql.ResultSet ; 60 import java.sql.Timestamp ; 61 import java.sql.Types ; 62 63 68 public class Bug extends DBPersist { 69 70 73 public Bug() { 74 super(DB.k_bugs, "Bug"); 75 } 76 77 79 85 public Bug(JDCConnection oConn, String sIdBug) throws SQLException { 86 super(DB.k_bugs,"Bug"); 87 88 Object aBug[] = { sIdBug }; 89 90 load (oConn,aBug); 91 } 92 93 95 103 public Bug(JDCConnection oConn, int iPgBug, String sWorkArea) throws SQLException { 104 super(DB.k_bugs,"Bug"); 105 Object aBug[] = { Bug.getIdFromPg(oConn, iPgBug, sWorkArea) }; 106 if (null!=aBug[0]) load (oConn,aBug); 107 } 108 109 111 118 public boolean delete(JDCConnection oConn) throws SQLException { 119 return Bug.delete(oConn, getString(DB.gu_bug)); 120 } 121 122 124 135 public boolean delete(JDCConnection oConn, Properties oCnf) 136 throws SQLException , IOException , NoSuchFieldException , IllegalAccessException { 137 return Bug.delete(oConn, getString(DB.gu_bug), oCnf); 138 } 139 140 142 152 public boolean store(JDCConnection oConn) throws SQLException { 153 int iPgBug; 154 Timestamp dtNow; 155 Object oOldValue; 156 String sSQL; 157 158 if (DebugFile.trace) { 159 DebugFile.writeln("Begin Bug.store()"); 160 DebugFile.incIdent(); 161 } 162 163 dtNow = new Timestamp (DBBind.getTime()); 164 165 if (!AllVals.containsKey(DB.gu_bug)) { 166 put(DB.gu_bug, Gadgets.generateUUID()); 167 if (!AllVals.containsKey(DB.pg_bug)) { 168 iPgBug = Bug.getPgFromId(oConn, getString(DB.gu_bug)); 169 if (-1==iPgBug) iPgBug = DBBind.nextVal(oConn, "seq_" + DB.k_bugs); 170 put(DB.pg_bug, iPgBug); 171 } 172 } else { 173 if (!AllVals.containsKey(DB.pg_bug)) { 174 iPgBug = Bug.getPgFromId(oConn, getString(DB.gu_bug)); 175 if (-1==iPgBug) iPgBug = DBBind.nextVal(oConn, "seq_" + DB.k_bugs); 176 put(DB.pg_bug, iPgBug); 177 } 178 replace(DB.dt_modified, dtNow); 179 Bug oOld = new Bug (); 180 if (oOld.load(oConn, new Object []{get(DB.gu_bug)})) { 181 if (DBBind.exists(oConn, DB.k_bugs_changelog,"U")) { 182 HashMap oLog = changelog(oOld); 183 sSQL = "INSERT INTO "+DB.k_bugs_changelog+" ("+DB.gu_bug+","+DB.pg_bug+","+DB.nm_column+","+DB.gu_writer+","+DB.tx_oldvalue+") VALUES (?,?,?,?,?)"; 184 if (DebugFile.trace) DebugFile.writeln("PreparedStatement.prepareStatement("+sSQL+")"); 185 PreparedStatement oWriteLog = oConn.prepareStatement(sSQL); 186 oWriteLog.setString(1, getString(DB.gu_bug)); 187 oWriteLog.setInt (2, getInt(DB.pg_bug)); 188 Iterator oIter = oLog.keySet().iterator(); 189 while (oIter.hasNext()) { 190 String sColumnName = (String ) oIter.next(); 191 if (!sColumnName.equalsIgnoreCase(DB.dt_modified)) { 192 oWriteLog.setString(3, sColumnName); 193 oWriteLog.setString(4, getStringNull(DB.gu_writer,null)); 194 oOldValue = oLog.get(sColumnName); 195 if (null==oOldValue) 196 oWriteLog.setNull(5, Types.VARCHAR); 197 else 198 oWriteLog.setString(5, Gadgets.left(oOldValue.toString(),255)); 199 if (DebugFile.trace) DebugFile.writeln("PreparedStatement.executeUpdate("+sColumnName+")"); 200 oWriteLog.executeUpdate(); 201 } 202 } if (DebugFile.trace) DebugFile.writeln("PreparedStatement.close()"); 204 oWriteLog.close(); 205 } } } 209 boolean bRetVal = super.store(oConn); 210 211 if (DebugFile.trace) { 212 DebugFile.decIdent(); 213 DebugFile.writeln("End Bug.store()"); 214 } 215 return bRetVal; 216 } 218 220 233 public boolean storeAndIndex(JDCConnection oConn, Properties oCnf) 234 throws SQLException , IOException ,ClassNotFoundException ,NoSuchFieldException , 235 IllegalAccessException ,InstantiationException { 236 if (DebugFile.trace) { 237 DebugFile.writeln("Begin Bug.storeAndIndex([JDCConnection], [Properties])"); 238 DebugFile.incIdent(); 239 } 240 boolean bStore = store(oConn); 241 if (bStore) { 242 String sLuceneIndex = oCnf.getProperty("luceneindex",""); 243 if (sLuceneIndex.length()>0) { 244 String sWrkA = getWorkArea(oConn, getString(DB.gu_bug)); 245 BugIndexer.addBug(oCnf, oConn, sWrkA, this); 246 } 247 } 248 if (DebugFile.trace) { 249 DebugFile.decIdent(); 250 DebugFile.writeln("End Bug.storeAndIndex()"); 251 } 252 return bStore; 253 } 255 267 public void reIndex(JDCConnection oConn, Properties oCnf) 268 throws SQLException , IOException ,ClassNotFoundException ,NoSuchFieldException , 269 IllegalAccessException ,InstantiationException { 270 if (DebugFile.trace) { 271 DebugFile.writeln("Begin Bug.reIndex([JDCConnection], [Properties])"); 272 DebugFile.incIdent(); 273 } 274 275 String sLuceneIndex = oCnf.getProperty("luceneindex",""); 276 if (sLuceneIndex.length()>0) { 277 String sWrkA = getWorkArea(oConn, getString(DB.gu_bug)); 278 if (null!=sWrkA) { 279 Indexer.delete("k_bugs", sWrkA, oCnf, getString(DB.gu_bug)); 280 BugIndexer.addBug(oCnf, oConn, sWrkA, this); 281 } } 284 if (DebugFile.trace) { 285 DebugFile.decIdent(); 286 DebugFile.writeln("End Bug.reIndex()"); 287 } 288 } 290 292 302 public void attachFile(JDCConnection oConn, String sFilePath) 303 throws SQLException , FileNotFoundException , IOException , NullPointerException { 304 305 if (DebugFile.trace) { 306 DebugFile.writeln("Begin Bug.attachFile([JDCConnection],"+sFilePath+")"); 307 DebugFile.incIdent(); 308 } 309 310 BugAttachment.createFromFile(oConn, getString(DB.gu_bug), sFilePath); 311 312 if (DebugFile.trace) { 313 DebugFile.decIdent(); 314 DebugFile.writeln("End Bug.attachFile()"); 315 } 316 } 318 320 326 public void removeAttachment(JDCConnection oConn, String sFileName) 327 throws SQLException { 328 BugAttachment.delete(oConn, getString(DB.gu_bug), sFileName); 329 } 331 333 340 public BugAttachment[] attachments(JDCConnection oConn) 341 throws SQLException { 342 DBSubset oAttachs = new DBSubset(DB.k_bugs_attach, DB.tx_file+","+DB.len_file, DB.gu_bug+"=?", 10); 343 int iAttachs = oAttachs.load(oConn, new Object []{getString(DB.gu_bug)}); 344 if (0==iAttachs) 345 return null; 346 else { 347 BugAttachment[] aAttachs = new BugAttachment[iAttachs]; 348 for (int a=0; a<iAttachs; a++) 349 aAttachs[a] = new BugAttachment(getString(DB.gu_bug),oAttachs.getString(1,a), oAttachs.getInt(2,a)); 350 return aAttachs; 351 } 352 } 354 356 363 public BugChangeLog[] changeLog(JDCConnection oConn) 364 throws SQLException { 365 BugChangeLog[] aBcl; 366 DBSubset oLog = new DBSubset(DB.k_bugs_changelog, 367 DB.gu_bug+","+DB.pg_bug+","+DB.nm_column+","+DB.dt_modified+","+DB.gu_writer+","+DB.tx_oldvalue, 368 DB.gu_bug+"=? ORDER BY 4", 10); 369 int iLog = oLog.load(oConn, new Object []{getString(DB.gu_bug)}); 370 if (0==iLog) { 371 aBcl = null; 372 } else { 373 aBcl = new BugChangeLog[iLog]; 374 for (int l=0; l<iLog; l++) { 375 aBcl[l] = new BugChangeLog(); 376 aBcl[l].putAll(oLog.getRowAsMap(l)); 377 aBcl[l].setWriter(oConn, oLog.getStringNull(4,l,null)); 378 } } return aBcl; 381 } 383 385 393 public BugChangeLog[] changeLog(JDCConnection oConn, String sColumnName) 394 throws SQLException { 395 BugChangeLog[] aBcl; 396 DBSubset oLog = new DBSubset(DB.k_bugs_changelog, 397 DB.gu_bug+","+DB.pg_bug+","+DB.nm_column+","+DB.dt_modified+","+DB.gu_writer+","+DB.tx_oldvalue, 398 DB.gu_bug+"=? AND "+DB.nm_column+"=? ORDER BY 4", 10); 399 int iLog = oLog.load(oConn, new Object []{getString(DB.gu_bug), sColumnName}); 400 if (0==iLog) { 401 aBcl = null; 402 } else { 403 aBcl = new BugChangeLog[iLog]; 404 for (int l=0; l<iLog; l++) { 405 aBcl[l] = new BugChangeLog(); 406 aBcl[l].putAll(oLog.getRowAsMap(l)); 407 aBcl[l].setWriter(oConn, oLog.getStringNull(4,l,null)); 408 } } return aBcl; 411 } 413 414 417 423 private static String getWorkArea(JDCConnection oConn, String sGuid) 424 throws SQLException { 425 String sWrkA; 426 if (DebugFile.trace) { 427 DebugFile.writeln("JDCConnection.prepareStatement(SELECT p."+DB.gu_owner+" FROM "+DB.k_projects+" p,"+DB.k_bugs+" b WHERE p."+DB.gu_project+"=b."+DB.gu_project+" AND b."+DB.gu_bug+"='"+sGuid+"'"); 428 } 429 PreparedStatement oStmt = oConn.prepareStatement("SELECT p."+DB.gu_owner+" FROM "+DB.k_projects+" p,"+DB.k_bugs+" b WHERE p."+DB.gu_project+"=b."+DB.gu_project+" AND b."+DB.gu_bug+"=?", 430 ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY); 431 oStmt.setString(1, sGuid); 432 ResultSet oRSet = oStmt.executeQuery(); 433 if (oRSet.next()) 434 sWrkA = oRSet.getString(1); 435 else 436 sWrkA = null; 437 oRSet.close(); 438 oStmt.close(); 439 return sWrkA; 440 } 442 451 public static boolean delete(JDCConnection oConn, String sBugGUID) throws SQLException { 452 boolean bRetVal; 453 454 if (oConn.getDataBaseProduct()==JDCConnection.DBMS_POSTGRESQL) { 455 if (DebugFile.trace) DebugFile.writeln("Connection.executeQuery(SELECT k_sp_del_bug ('" + sBugGUID + "'))"); 456 Statement oStmt = oConn.createStatement(); 457 ResultSet oRSet = oStmt.executeQuery("SELECT k_sp_del_bug ('" + sBugGUID + "')"); 458 oRSet.close(); 459 oStmt.close(); 460 bRetVal = true; 461 } 462 else { 463 if (DebugFile.trace) DebugFile.writeln("Connection.prepareCall({ call k_sp_del_bug ('" + sBugGUID + "')})"); 464 CallableStatement oCall = oConn.prepareCall("{call k_sp_del_bug ('" + sBugGUID + "')}"); 465 bRetVal = oCall.execute(); 466 oCall.close(); 467 } 468 469 return bRetVal; 470 } 472 484 public static boolean delete(JDCConnection oConn, String sBugGUID, Properties oCnf) 485 throws SQLException , IOException , NoSuchFieldException , IllegalAccessException { 486 String sLuceneIndex = oCnf.getProperty("luceneindex",""); 487 if (sLuceneIndex.length()>0) { 488 String sWrkA = getWorkArea(oConn, sBugGUID); 489 if (null!=sWrkA) Indexer.delete("k_bugs", sWrkA, oCnf, sBugGUID); 490 } 491 return delete(oConn, sBugGUID); 492 } 494 496 507 public static int getPgFromId(JDCConnection oConn, String sBugId) throws SQLException { 508 int iRetVal; 509 PreparedStatement oStmt; 510 ResultSet oRSet; 511 512 oStmt = oConn.prepareStatement("SELECT " + DB.pg_bug + " FROM " + DB.k_bugs + " WHERE " + DB.gu_bug + "=?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 513 oStmt.setString(1, sBugId); 514 oRSet = oStmt.executeQuery(); 515 if (oRSet.next()) 516 iRetVal = oRSet.getInt(1); 517 else 518 iRetVal = -1; 519 oRSet.close(); 520 oStmt.close(); 521 return iRetVal; 522 } 524 526 539 public static String getIdFromPg(JDCConnection oConn, int iBugPg, String sWorkArea) throws SQLException { 540 String sRetVal; 541 PreparedStatement oStmt; 542 ResultSet oRSet; 543 544 oStmt = oConn.prepareStatement("SELECT " + DB.gu_bug + " FROM " + DB.k_bugs + 545 " b WHERE b." + DB.pg_bug + "=? AND EXISTS " + 546 " (SELECT p." + DB.gu_project + " FROM " + DB.k_projects + " p WHERE b." + DB.gu_project + "=p." + DB.gu_project + " AND p." + DB.gu_owner + "=?)", 547 ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 548 oStmt.setInt(1, iBugPg); 549 oStmt.setString(2, sWorkArea); 550 oRSet = oStmt.executeQuery(); 551 if (oRSet.next()) 552 sRetVal = oRSet.getString(1); 553 else 554 sRetVal = null; 555 oRSet.close(); 556 oStmt.close(); 557 558 return sRetVal; 559 } 561 564 public static final short ClassId = 82; 565 } 566 | Popular Tags |