0001: package salomeTMF_plug.mantis.sqlWrapper;
0002:
0003: import java.sql.Date;
0004: import java.sql.PreparedStatement;
0005: import java.sql.ResultSet;
0006: import java.text.DateFormat;
0007: import java.text.SimpleDateFormat;
0008: import java.util.Hashtable;
0009: import java.util.Properties;
0010: import java.util.Vector;
0011:
0012: import org.objectweb.salome_tmf.api.Api;
0013: import org.objectweb.salome_tmf.api.Util;
0014: import org.objectweb.salome_tmf.api.sql.IDataBase;
0015: import org.objectweb.salome_tmf.api.sql.ISQLObjectFactory;
0016: import org.objectweb.salome_tmf.data.Environment;
0017: import org.objectweb.salome_tmf.data.Project;
0018: import org.objectweb.salome_tmf.data.User;
0019:
0020: public class SQLMantis implements MantisConnector {
0021:
0022: IDataBase iDB = null;
0023: Properties sql_prop = null;
0024: final String MANTIS_STMT_FILE = "/salomeTMF_plug/mantis/resources/sql/Mantis_Stmts.properties";
0025:
0026: boolean connected = false;
0027:
0028: static int RELATION_RELATED_TO = 1;
0029: static int RELATION_PARENT_OFF = 2;
0030: static int RELATION_CHILD_OF = 3;
0031: static int RELATION_DUPLICATE_OF = 4;
0032:
0033: public void initConnector(String driver, String url_db,
0034: String user, String pwd) throws Exception {
0035: ISQLObjectFactory iSQL_OF = Api.getISQLObjectFactory();
0036: if (iDB == null) {
0037: iDB = iSQL_OF.getInstanceOfDataBase(driver);
0038: iDB.open(url_db, user, pwd);
0039: SQLUtils.initSQLUtils(iDB);
0040: }
0041: // Mantis properties file for SQL statements
0042: sql_prop = Util.getPropertiesFile(getClass().getResource(
0043: MANTIS_STMT_FILE));
0044: connected = true;
0045: }
0046:
0047: public int getUserID(String user_login) throws Exception {
0048: int bugUsrID = -1;
0049: if (!connected) {
0050: throw new Exception(
0051: "Connector JDBC is not connected to the mantis database");
0052: }
0053: ResultSet stmtRes = null;
0054: PreparedStatement prep = iDB.prepareStatement(sql_prop
0055: .getProperty("SelectUsr"));
0056: prep.setString(1, user_login);
0057: stmtRes = prep.executeQuery();
0058: if (stmtRes.next()) {
0059: Util.log("[MantisPlugin] Mantis connexion with user : "
0060: + user_login);
0061: bugUsrID = stmtRes.getInt("id");
0062: } else {
0063: Util
0064: .log("[MantisPlugin] Current user in Salomé doesn't exist in mantis DB : "
0065: + user_login);
0066: }
0067: return bugUsrID;
0068: }
0069:
0070: public int getUserIDInProject(String user_login, int bugProjectID)
0071: throws Exception {
0072: int bugUsrID = -1;
0073: if (!connected) {
0074: throw new Exception(
0075: "Connector JDBC is not connected to the mantis database");
0076: }
0077: ResultSet stmtRes = null;
0078: PreparedStatement prep = iDB.prepareStatement(sql_prop
0079: .getProperty("SelectUsrProject"));
0080: prep.setString(1, user_login);
0081: prep.setInt(2, bugProjectID);
0082: stmtRes = prep.executeQuery();
0083: if (stmtRes.next()) {
0084: Util.log("[MantisPlugin] Mantis connexion with user : "
0085: + user_login);
0086: bugUsrID = stmtRes.getInt("id");
0087: } else {
0088: Util
0089: .log("[MantisPlugin] Current user in Salomé doesn't exist in mantis DB : "
0090: + user_login);
0091: }
0092: return bugUsrID;
0093: }
0094:
0095: public int getProjectID(String project_name) throws Exception {
0096: int bugProjectID = -1;
0097: if (!connected) {
0098: throw new Exception(
0099: "Connector JDBC is not connected to the mantis database");
0100: }
0101: PreparedStatement prep = iDB.prepareStatement(sql_prop
0102: .getProperty("SelectProject"));
0103: prep.setString(1, project_name);
0104: ResultSet stmtRes2 = prep.executeQuery();
0105: if (stmtRes2.next()) {
0106: bugProjectID = stmtRes2.getInt("id");
0107: }
0108: return bugProjectID;
0109: }
0110:
0111: public int getUserAccesLevel(int bugUsrID, int bugProjectID)
0112: throws Exception {
0113: if (!connected) {
0114: throw new Exception(
0115: "Connector JDBC is not connected to the mantis database");
0116: }
0117: int access_level = -1;
0118: if ((bugUsrID != -1) && (bugProjectID != -1)) {
0119: PreparedStatement prep = iDB.prepareStatement(sql_prop
0120: .getProperty("SelectProjectUser"));
0121: prep.setInt(1, bugProjectID);
0122: prep.setInt(2, bugUsrID);
0123: ResultSet stmtRes3 = prep.executeQuery();
0124: if (stmtRes3.next()) {
0125: access_level = stmtRes3.getInt("access_level");
0126: //isCurrentUserExistsInProject = true;
0127: }
0128: }
0129: return access_level;
0130: }
0131:
0132: public int addUser(User pUser) throws Exception {
0133: if (!connected) {
0134: throw new Exception(
0135: "Connector JDBC is not connected to the mantis database");
0136: }
0137: //Adding the user to Mantis
0138: int nbTrans = -1;
0139: PreparedStatement prep = null;
0140: DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
0141: String date = sdf.format(new java.util.Date()); // gives the current date in MySQL-acceptable format.
0142: String cookieName = pUser.getLoginFromModel() + "_"
0143: + new java.util.Date().getTime();
0144:
0145: int bugUsrID = -1;
0146: // Add user to mantis DB with access_level = viewer
0147: try {
0148: nbTrans = SQLUtils.beginTrans();
0149: prep = iDB.prepareStatement(sql_prop
0150: .getProperty("AddNewBugUser"));
0151: prep.setString(1, pUser.getLoginFromModel());
0152: prep.setString(2, pUser.getEmailFromModel());
0153: prep.setString(3, pUser.getPasswordFromDB());
0154: prep.setString(4, date);
0155: prep.setString(5, date);
0156: prep.setInt(6, 1);
0157: prep.setInt(7, 10); //0 : NONE 10 ://viewer
0158: prep.setInt(8, 0);
0159: prep.setString(9, pUser.getFirstNameFromModel() + " "
0160: + pUser.getLastNameFromModel());
0161: prep.setString(10, cookieName);
0162: prep.executeUpdate();
0163:
0164: ResultSet stmtRes = null;
0165: prep = iDB.prepareStatement(sql_prop
0166: .getProperty("SelectUsr"));
0167: prep.setString(1, pUser.getLoginFromModel());
0168: stmtRes = prep.executeQuery();
0169: if (stmtRes.next()) {
0170: bugUsrID = stmtRes.getInt("id");
0171: }
0172: if (bugUsrID == -1) {
0173: throw new Exception("User no found");
0174: }
0175: SQLUtils.commitTrans(nbTrans);
0176: } catch (Exception e) {
0177: e.printStackTrace();
0178: SQLUtils.rollBackTrans(nbTrans);
0179: throw e;
0180: }
0181:
0182: /* User ID
0183: try {
0184: ResultSet stmtRes = null;
0185: prep = iDB.prepareStatement(sql_prop.getProperty("SelectUsr"));
0186: prep.setString(1, pUser.getLoginFromModel());
0187: stmtRes = prep.executeQuery();
0188: if (stmtRes.next()) {
0189: bugUsrID = stmtRes.getInt("id");
0190: }
0191: } catch (Exception e) {
0192: e.printStackTrace();
0193: throw e;
0194: }*/
0195:
0196: // Default user preferences
0197: try {
0198: nbTrans = SQLUtils.beginTrans();
0199: prep = iDB.prepareStatement(sql_prop
0200: .getProperty("AddDefaultUserPref"));
0201: prep.setInt(1, bugUsrID);
0202: prep.executeUpdate();
0203: SQLUtils.commitTrans(nbTrans);
0204: } catch (Exception e) {
0205: e.printStackTrace();
0206: SQLUtils.rollBackTrans(nbTrans);
0207: throw e;
0208: }
0209: return bugUsrID;
0210: }
0211:
0212: public int addProject(Project project) throws Exception {
0213: if (!connected) {
0214: throw new Exception(
0215: "Connector JDBC is not connected to the mantis database");
0216: }
0217: int bugProjectID = -1;
0218: //Adding Salomé TMF project to Mantis DB
0219: int nbTrans = -1;
0220: PreparedStatement prep;
0221: try {
0222: nbTrans = SQLUtils.beginTrans();
0223: prep = iDB.prepareStatement(sql_prop
0224: .getProperty("AddNewProject"));
0225: prep.setString(1, project.getNameFromModel());
0226: prep.setString(2, "");
0227: prep.setString(3, project.getDescriptionFromModel());
0228: prep.executeUpdate();
0229:
0230: //Get project ID
0231: prep = iDB.prepareStatement(sql_prop
0232: .getProperty("SelectProject"));
0233: prep.setString(1, project.getNameFromModel());
0234: ResultSet stmtRes = prep.executeQuery();
0235: if (stmtRes.next()) {
0236: bugProjectID = stmtRes.getInt("id");
0237: }
0238:
0239: if (bugProjectID == -1) {
0240: throw new Exception("Project no found");
0241: }
0242:
0243: SQLUtils.commitTrans(nbTrans);
0244: } catch (Exception e) {
0245: e.printStackTrace();
0246: SQLUtils.rollBackTrans(nbTrans);
0247: throw e;
0248: }
0249:
0250: return bugProjectID;
0251:
0252: }
0253:
0254: public void addUserInProject(int bugUsrID, int bugProjectID,
0255: int access_level) throws Exception {
0256: if (!connected) {
0257: throw new Exception(
0258: "Connector JDBC is not connected to the mantis database");
0259: }
0260: if (access_level <= -1) {
0261: access_level = 70;// manager
0262: }
0263: /*if (currentProject.getAdministratorWrapperFromDB().getLogin().equals(currentUser.getLoginFromModel())) {
0264: access_level = 90; // admin
0265: }*/
0266: int nbTrans = -1;
0267: try {
0268: nbTrans = SQLUtils.beginTrans();
0269: PreparedStatement prep = iDB.prepareStatement(sql_prop
0270: .getProperty("AddUserToProject"));
0271: prep.setInt(1, bugProjectID);
0272: prep.setInt(2, bugUsrID);
0273: prep.setInt(3, access_level);
0274: prep.executeUpdate();
0275: SQLUtils.commitTrans(nbTrans);
0276: Util.log("[MantisPlugin] Add User id=" + bugUsrID
0277: + " to project id=" + bugProjectID
0278: + " with access level=" + access_level);
0279: } catch (Exception e) {
0280: e.printStackTrace();
0281: SQLUtils.rollBackTrans(nbTrans);
0282: throw e;
0283: }
0284: }
0285:
0286: public void addDefaultEnvToProject(int bugProjectID)
0287: throws Exception {
0288: if (!connected) {
0289: throw new Exception(
0290: "Connector JDBC is not connected to the mantis database");
0291: }
0292: //Adding default environment
0293: int nbTrans = -1;
0294: DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
0295: String date = sdf.format(new java.util.Date()); // gives the current date in MySQL-acceptable format.
0296: try {
0297: nbTrans = SQLUtils.beginTrans();
0298: PreparedStatement prep = iDB.prepareStatement(sql_prop
0299: .getProperty("AddNewEnvironment"));
0300: prep.setInt(1, bugProjectID);
0301: prep.setString(2, "___NO_ENV___");
0302: prep.setString(3, date);
0303: prep.setString(4, "[SALOME_DEFAULT_ENVIRONMENT]");
0304: prep.executeUpdate();
0305: SQLUtils.commitTrans(nbTrans);
0306: } catch (Exception e) {
0307: e.printStackTrace();
0308: SQLUtils.rollBackTrans(nbTrans);
0309: throw e;
0310: }
0311: }
0312:
0313: public void addEnvironment(String name, String description,
0314: int bugProjectID) throws Exception {
0315: if (!connected) {
0316: throw new Exception(
0317: "Connector JDBC is not connected to the mantis database");
0318: }
0319: if (bugProjectID <= 0) {
0320: throw new Exception("Project Id is not valid");
0321: }
0322:
0323: int nbTrans = -1;
0324: DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
0325: String date = sdf.format(new java.util.Date()); // gives you the current date in MySQL-acceptable format.
0326: try {
0327: nbTrans = SQLUtils.beginTrans();
0328: PreparedStatement prep = iDB.prepareStatement(sql_prop
0329: .getProperty("AddNewEnvironment"));
0330: prep.setInt(1, bugProjectID);
0331: prep.setString(2, name.trim());
0332: prep.setString(3, date);
0333: prep.setString(4, description);
0334: prep.executeUpdate();
0335: SQLUtils.commitTrans(nbTrans);
0336: } catch (Exception e) {
0337: e.printStackTrace();
0338: SQLUtils.rollBackTrans(nbTrans);
0339: throw e;
0340: }
0341: }
0342:
0343: public void addEnvironment(Environment environment, int bugProjectID)
0344: throws Exception {
0345: addEnvironment(environment.getNameFromModel(), environment
0346: .getDescriptionFromModel(), bugProjectID);
0347: }
0348:
0349: public void addDefectLink(int userID, int bugSource, int bugDest)
0350: throws Exception {
0351:
0352: if (!connected) {
0353: throw new Exception(
0354: "Connector JDBC is not connected to the mantis database");
0355: }
0356: if (userID <= 0) {
0357: throw new Exception("User Id is not valid");
0358: }
0359:
0360: if (bugSource <= 0) {
0361: throw new Exception("Source Id is not valid");
0362: }
0363:
0364: if (bugDest <= 0) {
0365: throw new Exception("Destination Id is not valid");
0366: }
0367: int nbTrans = -1;
0368: try {
0369: nbTrans = SQLUtils.beginTrans();
0370: PreparedStatement prep = iDB.prepareStatement(sql_prop
0371: .getProperty("AddLinkedDefect"));
0372: prep.setInt(1, bugSource);
0373: prep.setInt(2, bugDest);
0374: prep.setInt(3, RELATION_RELATED_TO);
0375: prep.executeUpdate();
0376:
0377: addBugHistory(userID, bugSource, 18, "", "1", "" + bugDest);
0378: addBugHistory(userID, bugDest, 18, "", "1", "" + bugSource);
0379:
0380: SQLUtils.commitTrans(nbTrans);
0381: } catch (Exception e) {
0382: e.printStackTrace();
0383: SQLUtils.rollBackTrans(nbTrans);
0384: throw e;
0385: }
0386: }
0387:
0388: public void updateEnvironment(String old_component,
0389: String new_component, String description, int bugProjectID)
0390: throws Exception {
0391: if (!connected) {
0392: throw new Exception(
0393: "Connector JDBC is not connected to the mantis database");
0394: }
0395: if (bugProjectID <= 0) {
0396: throw new Exception("Project Id is not valid");
0397: }
0398: int nbTrans = -1;
0399: PreparedStatement prep;
0400: try {
0401: nbTrans = SQLUtils.beginTrans();
0402:
0403: prep = iDB.prepareStatement(sql_prop
0404: .getProperty("UpdateEnvironment"));
0405: prep.setString(1, new_component);
0406: prep.setString(2, description);
0407: prep.setInt(3, bugProjectID);
0408: prep.setString(4, old_component);
0409: prep.executeUpdate();
0410:
0411: prep = iDB.prepareStatement(sql_prop
0412: .getProperty("UpdateEnvironmentForBugs"));
0413: prep.setString(1, new_component.trim());
0414: prep.setInt(2, bugProjectID);
0415: prep.setString(3, old_component.trim());
0416: prep.executeUpdate();
0417:
0418: SQLUtils.commitTrans(nbTrans);
0419: } catch (Exception e) {
0420: e.printStackTrace();
0421: SQLUtils.rollBackTrans(nbTrans);
0422: throw e;
0423: }
0424: }
0425:
0426: public void deleteEnvironment(String environment, int bugProjectID)
0427: throws Exception {
0428: if (!connected) {
0429: throw new Exception(
0430: "Connector JDBC is not connected to the mantis database");
0431: }
0432: if (bugProjectID <= 0) {
0433: throw new Exception("Project Id is not valid");
0434: }
0435: int nbTrans = -1;
0436: PreparedStatement prep;
0437:
0438: try {
0439: nbTrans = SQLUtils.beginTrans();
0440: prep = iDB.prepareStatement(sql_prop
0441: .getProperty("UpdateEnvironmentForBugs"));
0442: prep.setString(1, "___NO_ENV___");
0443: prep.setInt(2, bugProjectID);
0444: prep.setString(3, environment);
0445: prep.executeUpdate();
0446:
0447: prep = iDB.prepareStatement(sql_prop
0448: .getProperty("DeleteEnvironment"));
0449: prep.setInt(1, bugProjectID);
0450: prep.setString(2, environment);
0451: prep.executeUpdate();
0452:
0453: SQLUtils.commitTrans(nbTrans);
0454: } catch (Exception e) {
0455: e.printStackTrace();
0456: SQLUtils.rollBackTrans(nbTrans);
0457: throw e;
0458: }
0459: }
0460:
0461: public void deleteDefectLink(int userID, int bugSource, int bugDest)
0462: throws Exception {
0463:
0464: if (!connected) {
0465: throw new Exception(
0466: "Connector JDBC is not connected to the mantis database");
0467: }
0468:
0469: if (userID <= 0) {
0470: throw new Exception("User Id is not valid");
0471: }
0472: if (bugSource <= 0) {
0473: throw new Exception("Source Id is not valid");
0474: }
0475:
0476: if (bugDest <= 0) {
0477: throw new Exception("Destination Id is not valid");
0478: }
0479: int nbTrans = -1;
0480: try {
0481: nbTrans = SQLUtils.beginTrans();
0482: PreparedStatement prep = iDB.prepareStatement(sql_prop
0483: .getProperty("DeleteLinkedDefect"));
0484: prep.setInt(1, bugSource);
0485: prep.setInt(2, bugDest);
0486: prep.setInt(3, RELATION_RELATED_TO);
0487: int val = prep.executeUpdate();
0488: if (val > 0) {
0489: addBugHistory(userID, bugSource, 19, "", "1", ""
0490: + bugDest);
0491: addBugHistory(userID, bugDest, 19, "", "1", ""
0492: + bugSource);
0493: }
0494:
0495: prep = iDB.prepareStatement(sql_prop
0496: .getProperty("DeleteLinkedDefect"));
0497: prep.setInt(2, bugSource);
0498: prep.setInt(1, bugDest);
0499: prep.setInt(3, RELATION_RELATED_TO);
0500: val = prep.executeUpdate();
0501: if (val > 0) {
0502: addBugHistory(userID, bugSource, 19, "", "1", ""
0503: + bugDest);
0504: addBugHistory(userID, bugDest, 19, "", "1", ""
0505: + bugSource);
0506: }
0507:
0508: SQLUtils.commitTrans(nbTrans);
0509: } catch (Exception e) {
0510: e.printStackTrace();
0511: SQLUtils.rollBackTrans(nbTrans);
0512: throw e;
0513: }
0514: }
0515:
0516: public boolean isExistEnv(int bugProjectID, String envName)
0517: throws Exception {
0518: if (!(bugProjectID > 0)) {
0519: return false;
0520: }
0521: boolean ret = true;
0522: try {
0523: PreparedStatement prep = iDB.prepareStatement(sql_prop
0524: .getProperty("SelectEnvironment"));
0525: prep.setInt(1, bugProjectID);
0526: prep.setString(2, envName.trim());
0527: ResultSet stmtRes = prep.executeQuery();
0528: if (!stmtRes.next()) {
0529: ret = false;
0530: }
0531: } catch (Exception e) {
0532: e.printStackTrace();
0533: throw e;
0534: }
0535: return ret;
0536: }
0537:
0538: public int addDefect(int bugUsrID, int bugProjectID,
0539: int assigned_to_ID, String long_desc, String url_attach,
0540: String short_desc, String bug_OS, int bug_priority,
0541: String bug_platform, int bug_reproductibility,
0542: int bug_severity, String component) throws Exception {
0543:
0544: if (!connected) {
0545: throw new Exception(
0546: "Connector JDBC is not connected to the mantis database");
0547: }
0548: if (bugProjectID <= 0) {
0549: throw new Exception("Project Id is not valid");
0550: }
0551: if (bugUsrID <= 0) {
0552: throw new Exception("User Id is not valid");
0553: }
0554: if (assigned_to_ID <= 0) {
0555: throw new Exception("assigned user Id is not valid");
0556: }
0557: int bug_id = -1;
0558: PreparedStatement prep;
0559: int nbTrans = -1;
0560: DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
0561: String date = sdf.format(new java.util.Date()); // gives you the current date in MySQL-acceptable format.
0562:
0563: try {
0564: nbTrans = SQLUtils.beginTrans();
0565:
0566: /*Adding bug description*/
0567: prep = iDB.prepareStatement(sql_prop
0568: .getProperty("AddBugDesc"));
0569: prep.setString(1, long_desc);
0570: prep.setString(2, bugUsrID + "_" + date);
0571: prep.executeUpdate();
0572:
0573: /* Selection of bug decription ID */
0574: int bug_desc_id = -1;
0575: prep = iDB.prepareStatement(sql_prop
0576: .getProperty("SelectBugDesc"));
0577: prep.setString(1, bugUsrID + "_" + date);
0578: ResultSet stmtRes = prep.executeQuery();
0579: if (stmtRes.next()) {
0580: bug_desc_id = stmtRes.getInt("id");
0581: }
0582:
0583: /* Adding the bug */
0584: if ((url_attach != null) && (!url_attach.equals(""))) {
0585: short_desc += " (URL = " + url_attach + ")";
0586: }
0587: if ((bug_OS == null) || (bug_OS.equals("")))
0588: bug_OS = " ";
0589:
0590: if ((short_desc == null) || (short_desc.equals("")))
0591: short_desc = " ";
0592:
0593: prep = iDB.prepareStatement(sql_prop.getProperty("AddBug"));
0594: prep.setInt(1, bugProjectID);
0595: prep.setInt(2, bugUsrID);
0596: prep.setInt(3, assigned_to_ID);
0597: prep.setInt(4, bug_priority);
0598: prep.setInt(5, bug_severity);
0599: prep.setInt(6, bug_reproductibility);
0600: prep.setString(7, date);
0601: prep.setString(8, date);
0602: prep.setInt(9, bug_desc_id);
0603: prep.setString(10, bug_OS);
0604: prep.setString(11, bug_platform);
0605: prep.setString(12, short_desc);
0606: prep.setString(13, component);
0607: prep.executeUpdate();
0608:
0609: /* Get BugID */
0610: prep = iDB.prepareStatement(sql_prop
0611: .getProperty("SelectBugID"));
0612: prep.setString(1, bugUsrID + "_" + date);
0613:
0614: stmtRes = prep.executeQuery();
0615: if (stmtRes.next()) {
0616: bug_id = stmtRes.getInt("id");
0617: addBugHistory(bugUsrID, bug_id, 1, "", "", "");
0618: Util.log("[MantisPlugin]Add bug id = " + bug_id);
0619: }
0620:
0621: SQLUtils.commitTrans(nbTrans);
0622: } catch (Exception e) {
0623: e.printStackTrace();
0624: SQLUtils.rollBackTrans(nbTrans);
0625: throw e;
0626: }
0627:
0628: return bug_id;
0629: }
0630:
0631: public void updateDefect(int bugUsrID, int bugProjectID, int bugID,
0632: int id_assigned_to, String long_desc, int id_bug_severity,
0633: int id_bug_satus, String short_desc, String bug_OS,
0634: int id_bug_priority, String bug_platform, String bug_env,
0635: int id_bug_reproducibility, int id_bug_resolution)
0636: throws Exception {
0637: if (!connected) {
0638: throw new Exception(
0639: "Connector JDBC is not connected to the mantis database");
0640: }
0641: if (bugProjectID <= 0) {
0642: throw new Exception("Project Id is not valid");
0643: }
0644: if (bugUsrID <= 0) {
0645: throw new Exception("User Id is not valid");
0646: }
0647: if (id_assigned_to <= 0) {
0648: throw new Exception("assigned user Id is not valid");
0649: }
0650:
0651: int nbTrans = -1;
0652: int bug_desc_id = -1;
0653: try {
0654: nbTrans = SQLUtils.beginTrans();
0655: /* Selection of bug decription ID */
0656: PreparedStatement prep = iDB.prepareStatement(sql_prop
0657: .getProperty("SelectBugDesc2"));
0658: prep.setInt(1, bugID);
0659: ResultSet stmtRes = prep.executeQuery();
0660: if (stmtRes.next()) {
0661: bug_desc_id = stmtRes.getInt("id");
0662: }
0663:
0664: /* Get Old Information */
0665: String oldplateforme = bug_platform; //OK
0666: String oldos = bug_OS; //OK
0667: int oldpriority = id_bug_priority; //OK
0668: int oldseverity = id_bug_severity; //OK
0669: int oldstatus = id_bug_satus; //OK
0670: int oldreproducibility = id_bug_reproducibility;
0671: int oldresolution = id_bug_resolution;
0672: int oldrecipient = id_assigned_to;
0673: String oldresume = short_desc; //OK
0674: String olddescription = long_desc; //OK
0675: prep = iDB.prepareStatement(sql_prop
0676: .getProperty("SelectBugInfo"));
0677: prep.setInt(1, bugID);
0678: stmtRes = prep.executeQuery();
0679: if (stmtRes.next()) {
0680: oldplateforme = stmtRes.getString("platform");
0681: oldos = stmtRes.getString("os");
0682: oldpriority = stmtRes.getInt("priority");
0683: oldseverity = stmtRes.getInt("severity");
0684: oldstatus = stmtRes.getInt("status");
0685: oldreproducibility = stmtRes.getInt("reproducibility");
0686: oldresolution = stmtRes.getInt("resolution");
0687: oldresume = stmtRes.getString("summary");
0688: olddescription = stmtRes.getString("description");
0689: }
0690: prep = iDB.prepareStatement(sql_prop
0691: .getProperty("SelectBugHandler"));
0692: prep.setInt(1, bugID);
0693: stmtRes = prep.executeQuery();
0694: if (stmtRes.next()) {
0695: oldrecipient = stmtRes.getInt("id");
0696: }
0697:
0698: /* Update bug description */
0699: if (bug_desc_id > 0) {
0700: prep = iDB.prepareStatement(sql_prop
0701: .getProperty("UpdateBugDesc"));
0702: prep.setString(1, long_desc);
0703: prep.setInt(2, bug_desc_id);
0704: prep.executeUpdate();
0705: }
0706:
0707: /* Update bug information */
0708: prep = iDB.prepareStatement(sql_prop
0709: .getProperty("UpdateBugInfo"));
0710: prep.setInt(1, id_assigned_to); //OK
0711: prep.setInt(2, id_bug_priority); //OK
0712: prep.setInt(3, id_bug_severity); //OK
0713: prep.setInt(4, id_bug_satus); //OK
0714: prep.setInt(5, id_bug_reproducibility);
0715: prep.setInt(6, id_bug_resolution);
0716: prep.setString(7, bug_OS); //OK
0717: prep.setString(8, bug_platform); //OK
0718: prep.setString(9, bug_env); //OK
0719: prep.setString(10, short_desc); //OK
0720: prep.setInt(11, bugID); //OK
0721: prep.executeUpdate();
0722:
0723: /* Add bug history */
0724: if (oldrecipient != id_assigned_to) {
0725: try {
0726: addBugHistory(bugUsrID, bugID, 0, "handler_id", ""
0727: + oldrecipient, "" + id_assigned_to);
0728: } catch (Exception e) {
0729: }
0730: }
0731: if (oldpriority != id_bug_priority) {
0732: try {
0733: addBugHistory(bugUsrID, bugID, 0, "priority", ""
0734: + oldpriority, "" + id_bug_priority);
0735: } catch (Exception e) {
0736: }
0737: }
0738: if (oldseverity != id_bug_severity) {
0739: try {
0740: addBugHistory(bugUsrID, bugID, 0, "severity", ""
0741: + oldseverity, "" + id_bug_severity);
0742: } catch (Exception e) {
0743: }
0744: }
0745: if (oldstatus != id_bug_satus) {
0746: try {
0747: addBugHistory(bugUsrID, bugID, 0, "status", ""
0748: + oldstatus, "" + id_bug_satus);
0749: } catch (Exception e) {
0750: }
0751: }
0752: if (oldreproducibility != id_bug_reproducibility) {
0753: try {
0754: addBugHistory(bugUsrID, bugID, 0,
0755: "reproducibility", "" + oldreproducibility,
0756: "" + id_bug_reproducibility);
0757: } catch (Exception e) {
0758: }
0759: }
0760: if (oldresolution != id_bug_resolution) {
0761: try {
0762: addBugHistory(bugUsrID, bugID, 0, "resolution", ""
0763: + oldresolution, "" + id_bug_resolution);
0764: } catch (Exception e) {
0765: }
0766: }
0767: if (!olddescription.equals(long_desc)) {
0768: try {
0769: addBugHistory(bugUsrID, bugID, 6, "", "", "");
0770: } catch (Exception e) {
0771: }
0772: }
0773: if (!oldresume.equals(short_desc)) {
0774: try {
0775: addBugHistory(bugUsrID, bugID, 0, "summary",
0776: oldresume, short_desc);
0777: } catch (Exception e) {
0778: }
0779: }
0780:
0781: SQLUtils.commitTrans(nbTrans);
0782: } catch (Exception E) {
0783: E.printStackTrace();
0784: SQLUtils.rollBackTrans(nbTrans);
0785: throw E;
0786: }
0787:
0788: }
0789:
0790: public DefectWrapper getDefectInfo(int bugID) throws Exception {
0791: if (!connected) {
0792: throw new Exception(
0793: "Connector JDBC is not connected to the mantis database");
0794: }
0795: if (bugID <= 0) {
0796: throw new Exception("Bug Id is not valid");
0797: }
0798: DefectWrapper pDefectWrapper = new DefectWrapper();
0799:
0800: PreparedStatement prep = iDB.prepareStatement(sql_prop
0801: .getProperty("SelectBugInfo"));
0802: prep.setInt(1, bugID);
0803: ResultSet stmtRes = prep.executeQuery();
0804: if (stmtRes.next()) {
0805: pDefectWrapper.setId(stmtRes.getInt("mantis_bug_table.id"));
0806: pDefectWrapper
0807: .setEnvironement(stmtRes.getString("version"));
0808: pDefectWrapper.setUser(stmtRes.getString("username"));
0809: pDefectWrapper.setPlateforme(stmtRes.getString("platform"));
0810: pDefectWrapper.setOs(stmtRes.getString("os"));
0811: //priority = getBugPriority(new Integer(stmtRes.getInt("priority")));
0812: pDefectWrapper.setPriority(stmtRes.getInt("priority"));
0813: //severity = getBugSeverity(new Integer(stmtRes.getInt("severity")));
0814: pDefectWrapper.setSeverity(stmtRes.getInt("severity"));
0815: //status = getBugStatus(new Integer(stmtRes.getInt("status")));
0816: pDefectWrapper.setStatus(stmtRes.getInt("status"));
0817: //reproducibility = getBugReproducibility(new Integer(stmtRes.getInt("reproducibility")));
0818: pDefectWrapper.setReproducibility(stmtRes
0819: .getInt("reproducibility"));
0820: //resolution = getBugResolution(new Integer(stmtRes.getInt("resolution")));
0821: pDefectWrapper.setResolution(stmtRes.getInt("resolution"));
0822: pDefectWrapper.setResume(stmtRes.getString("summary"));
0823: pDefectWrapper.setDescription(stmtRes
0824: .getString("description"));
0825: } else {
0826: throw new Exception("No bug");
0827: }
0828: prep = iDB.prepareStatement(sql_prop
0829: .getProperty("SelectBugHandler"));
0830: prep.setInt(1, bugID);
0831: stmtRes = prep.executeQuery();
0832: if (stmtRes.next()) {
0833: pDefectWrapper.setRecipient(stmtRes.getString("username"));
0834: } else {
0835: throw new Exception("No bug");
0836: }
0837: return pDefectWrapper;
0838: }
0839:
0840: public Hashtable getProjectDefects(int bugProjectID)
0841: throws Exception {
0842: if (!connected) {
0843: throw new Exception(
0844: "Connector JDBC is not connected to the mantis database");
0845: }
0846: if (bugProjectID <= 0) {
0847: throw new Exception("Project Id is not valid");
0848: }
0849: Hashtable listDefectWrapper = new Hashtable();
0850: Hashtable menbers = getBugTrackerUsers(bugProjectID);
0851:
0852: PreparedStatement prep = iDB.prepareStatement(sql_prop
0853: .getProperty("SelectBugOfProject"));
0854: prep.setInt(1, bugProjectID);
0855: ResultSet stmtRes = prep.executeQuery();
0856: while (stmtRes.next()) {
0857: DefectWrapper pDefectWrapper = new DefectWrapper();
0858: pDefectWrapper
0859: .setEnvironement(stmtRes.getString("version")); //OK
0860: pDefectWrapper.setId(stmtRes.getInt("id"));
0861: int userID = stmtRes.getInt("reporter_id"); //OK
0862: pDefectWrapper.setUser((String) menbers.get(new Integer(
0863: userID)));
0864: pDefectWrapper.setPlateforme(stmtRes.getString("platform")); //OK
0865: pDefectWrapper.setOs(stmtRes.getString("os")); //OK
0866: pDefectWrapper.setPriority(stmtRes.getInt("priority")); //OK
0867: pDefectWrapper.setSeverity(stmtRes.getInt("severity"));//OK
0868: pDefectWrapper.setStatus(stmtRes.getInt("status")); //OK
0869: pDefectWrapper.setReproducibility(stmtRes
0870: .getInt("reproducibility")); //OK
0871: pDefectWrapper.setResolution(stmtRes.getInt("resolution")); //ok
0872: pDefectWrapper.setResume(stmtRes.getString("summary")); //OK
0873: pDefectWrapper.setDescription(stmtRes
0874: .getString("description")); //OK
0875: int handlerID = stmtRes.getInt("handler_id"); //OK
0876: pDefectWrapper.setRecipient((String) menbers
0877: .get(new Integer(handlerID)));
0878: listDefectWrapper.put(new Integer(pDefectWrapper.getId()),
0879: pDefectWrapper);
0880: }
0881: return listDefectWrapper;
0882: }
0883:
0884: public Hashtable getDefectLink(int bugProjectID, int bugID)
0885: throws Exception {
0886: if (!connected) {
0887: throw new Exception(
0888: "Connector JDBC is not connected to the mantis database");
0889: }
0890: if (bugProjectID <= 0) {
0891: throw new Exception("Project Id is not valid");
0892: }
0893: if (bugID <= 0) {
0894: throw new Exception("defect Id is not valid");
0895: }
0896: Hashtable listDefectWrapper = new Hashtable();
0897: Hashtable menbers = getBugTrackerUsers(bugProjectID);
0898:
0899: PreparedStatement prep = iDB.prepareStatement(sql_prop
0900: .getProperty("SelectLinkedDefect"));
0901: prep.setInt(1, bugID);
0902: ResultSet stmtRes = prep.executeQuery();
0903: while (stmtRes.next()) {
0904: DefectWrapper pDefectWrapper = new DefectWrapper();
0905: pDefectWrapper
0906: .setEnvironement(stmtRes.getString("version")); //OK
0907: pDefectWrapper.setId(stmtRes.getInt("mantis_bug_table.id"));
0908: int userID = stmtRes.getInt("reporter_id"); //OK
0909: pDefectWrapper.setUser((String) menbers.get(new Integer(
0910: userID)));
0911: pDefectWrapper.setPlateforme(stmtRes.getString("platform")); //OK
0912: pDefectWrapper.setOs(stmtRes.getString("os")); //OK
0913: pDefectWrapper.setPriority(stmtRes.getInt("priority")); //OK
0914: pDefectWrapper.setSeverity(stmtRes.getInt("severity"));//OK
0915: pDefectWrapper.setStatus(stmtRes.getInt("status")); //OK
0916: pDefectWrapper.setReproducibility(stmtRes
0917: .getInt("reproducibility")); //OK
0918: pDefectWrapper.setResolution(stmtRes.getInt("resolution")); //ok
0919: pDefectWrapper.setResume(stmtRes.getString("summary")); //OK
0920: pDefectWrapper.setDescription(stmtRes
0921: .getString("description")); //OK
0922: int handlerID = stmtRes.getInt("handler_id"); //OK
0923: pDefectWrapper.setRecipient((String) menbers
0924: .get(new Integer(handlerID)));
0925: listDefectWrapper.put(new Integer(pDefectWrapper.getId()),
0926: pDefectWrapper);
0927: }
0928:
0929: prep = iDB.prepareStatement(sql_prop
0930: .getProperty("SelectLinkedDefect2"));
0931: prep.setInt(1, bugID);
0932: stmtRes = prep.executeQuery();
0933: while (stmtRes.next()) {
0934: DefectWrapper pDefectWrapper = new DefectWrapper();
0935: pDefectWrapper
0936: .setEnvironement(stmtRes.getString("version")); //OK
0937: pDefectWrapper.setId(stmtRes.getInt("mantis_bug_table.id"));
0938: int userID = stmtRes.getInt("reporter_id"); //OK
0939: pDefectWrapper.setUser((String) menbers.get(new Integer(
0940: userID)));
0941: pDefectWrapper.setPlateforme(stmtRes.getString("platform")); //OK
0942: pDefectWrapper.setOs(stmtRes.getString("os")); //OK
0943: pDefectWrapper.setPriority(stmtRes.getInt("priority")); //OK
0944: pDefectWrapper.setSeverity(stmtRes.getInt("severity"));//OK
0945: pDefectWrapper.setStatus(stmtRes.getInt("status")); //OK
0946: pDefectWrapper.setReproducibility(stmtRes
0947: .getInt("reproducibility")); //OK
0948: pDefectWrapper.setResolution(stmtRes.getInt("resolution")); //ok
0949: pDefectWrapper.setResume(stmtRes.getString("summary")); //OK
0950: pDefectWrapper.setDescription(stmtRes
0951: .getString("description")); //OK
0952: int handlerID = stmtRes.getInt("handler_id"); //OK
0953: pDefectWrapper.setRecipient((String) menbers
0954: .get(new Integer(handlerID)));
0955: listDefectWrapper.put(new Integer(pDefectWrapper.getId()),
0956: pDefectWrapper);
0957: }
0958:
0959: return listDefectWrapper;
0960: }
0961:
0962: public Vector getBugTrackerAllUsers(int bugProjectID) {
0963: if (!(bugProjectID > 0)) {
0964: return new Vector();
0965: }
0966: Vector res = null;
0967: try {
0968: ResultSet stmtRes;
0969: PreparedStatement prep = iDB.prepareStatement(sql_prop
0970: .getProperty("SelectAllLoginFromProject"));
0971: prep.setInt(1, bugProjectID);
0972: stmtRes = prep.executeQuery();
0973: res = new Vector();
0974: while (stmtRes.next()) {
0975: res.add(stmtRes.getString("username"));
0976: }
0977: } catch (Exception E) {
0978: E.printStackTrace();
0979: }
0980: return res;
0981: }
0982:
0983: public Hashtable getBugTrackerUsers(int bugProjectID) {
0984: if (!(bugProjectID > 0)) {
0985: return new Hashtable();
0986: }
0987: Hashtable res = null;
0988: try {
0989: ResultSet stmtRes;
0990: PreparedStatement prep = iDB.prepareStatement(sql_prop
0991: .getProperty("SelectAllLoginFromProject"));
0992: prep.setInt(1, bugProjectID);
0993: stmtRes = prep.executeQuery();
0994: res = new Hashtable();
0995: while (stmtRes.next()) {
0996: res.put(new Integer(stmtRes.getInt("id")), stmtRes
0997: .getString("username"));
0998: }
0999: } catch (Exception E) {
1000: res = null;
1001: E.printStackTrace();
1002: }
1003: return res;
1004: }
1005:
1006: public void suspend() throws Exception {
1007: iDB.close();
1008: iDB = null;
1009: connected = false;
1010: }
1011:
1012: /*************************************************************************************************************/
1013:
1014: void addBugHistory(int bugUsrID, int bug_id, int type,
1015: String field, String oldVal, String newVal)
1016: throws Exception {
1017: PreparedStatement prep;
1018: int nbTrans = -1;
1019: try {
1020: DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
1021: String date = sdf.format(new java.util.Date());
1022: nbTrans = SQLUtils.beginTrans();
1023: prep = iDB.prepareStatement(sql_prop
1024: .getProperty("AddBugHistory"));
1025: prep.setInt(1, bugUsrID);
1026: prep.setInt(2, bug_id);
1027: prep.setString(3, date);
1028: prep.setString(4, field);
1029: prep.setString(5, oldVal);
1030: prep.setString(6, newVal);
1031: prep.setInt(7, type);
1032: prep.executeUpdate();
1033: SQLUtils.commitTrans(nbTrans);
1034: } catch (Exception e) {
1035: e.printStackTrace();
1036: SQLUtils.rollBackTrans(nbTrans);
1037: throw e;
1038: }
1039: }
1040:
1041: public void deleteDefect(int bugProjectID, int bugID)
1042: throws Exception {
1043: if (!connected) {
1044: throw new Exception(
1045: "Connector JDBC is not connected to the mantis database");
1046: }
1047: if (bugProjectID <= 0) {
1048: throw new Exception("Project Id is not valid");
1049: }
1050: if (bugID <= 0) {
1051: throw new Exception("defect Id is not valid");
1052: }
1053: int nbTrans = -1;
1054: try {
1055: nbTrans = SQLUtils.beginTrans();
1056:
1057: PreparedStatement prep = iDB.prepareStatement(sql_prop
1058: .getProperty("DeleteLinkedDefectSource"));
1059: prep.setInt(1, bugID);
1060: prep.executeUpdate();
1061:
1062: prep = iDB.prepareStatement(sql_prop
1063: .getProperty("DeleteLinkedDefectDest"));
1064: prep.setInt(1, bugID);
1065: prep.executeUpdate();
1066:
1067: prep = iDB.prepareStatement(sql_prop
1068: .getProperty("DeleteBugNote"));
1069: prep.setInt(1, bugID);
1070: prep.executeUpdate();
1071:
1072: prep = iDB.prepareStatement(sql_prop
1073: .getProperty("DeleteMonitor"));
1074: prep.setInt(1, bugID);
1075: prep.executeUpdate();
1076:
1077: prep = iDB.prepareStatement(sql_prop
1078: .getProperty("DeleteBugFile"));
1079: prep.setInt(1, bugID);
1080: prep.executeUpdate();
1081:
1082: prep = iDB.prepareStatement(sql_prop
1083: .getProperty("DeleteHistory"));
1084: prep.setInt(1, bugID);
1085: prep.executeUpdate();
1086:
1087: prep = iDB.prepareStatement(sql_prop
1088: .getProperty("DeleteBugText"));
1089: prep.setInt(1, bugID);
1090: prep.executeUpdate();
1091:
1092: prep = iDB.prepareStatement(sql_prop
1093: .getProperty("DeleteDefect"));
1094: prep.setInt(1, bugID);
1095: prep.executeUpdate();
1096:
1097: SQLUtils.commitTrans(nbTrans);
1098: } catch (Exception e) {
1099: e.printStackTrace();
1100: SQLUtils.rollBackTrans(nbTrans);
1101: throw e;
1102: }
1103: }
1104:
1105: public Vector getBugHistory(int bugProjectID, int bugID)
1106: throws Exception {
1107: if (!connected) {
1108: throw new Exception(
1109: "Connector JDBC is not connected to the mantis database");
1110: }
1111: if (bugProjectID <= 0) {
1112: throw new Exception("Project Id is not valid");
1113: }
1114: if (bugID <= 0) {
1115: throw new Exception("defect Id is not valid");
1116: }
1117: Vector listOfHistory = new Vector();
1118: Hashtable menbers = getBugTrackerUsers(bugProjectID);
1119:
1120: PreparedStatement prep = iDB.prepareStatement(sql_prop
1121: .getProperty("SelectDefectHistory"));
1122: prep.setInt(1, bugID);
1123: ResultSet stmtRes = prep.executeQuery();
1124: while (stmtRes.next()) {
1125: int userID = stmtRes.getInt("user_id");
1126: String user = (String) menbers.get(new Integer(userID));
1127: int code = stmtRes.getInt("type");
1128: Date pDate = stmtRes.getDate("date_modified");
1129: String field_name = stmtRes.getString("field_name");
1130: String old_value = stmtRes.getString("old_value");
1131: String new_value = stmtRes.getString("new_value");
1132: HistoryWrapper pHistoryWrapper = new HistoryWrapper();
1133:
1134: pHistoryWrapper.setId(stmtRes.getInt("id"));
1135: pHistoryWrapper.setCode(code);
1136: pHistoryWrapper.setUsername(user);
1137: pHistoryWrapper.setField_name(field_name.trim());
1138: pHistoryWrapper.setOld_value(old_value.trim());
1139: pHistoryWrapper.setNew_value(new_value.trim());
1140: pHistoryWrapper.setPDate(pDate);
1141: listOfHistory.add(pHistoryWrapper);
1142: }
1143:
1144: return listOfHistory;
1145: }
1146: }
|