001: /*
002: * SalomeTMF is a Test Management Framework
003: * Copyright (C) 2005 France Telecom R&D
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * @author Marche Mikael
020: *
021: * Contact: mikael.marche@rd.francetelecom.com
022: */
023:
024: package org.objectweb.salome_tmf.databaseSQL;
025:
026: import java.sql.PreparedStatement;
027: import java.sql.ResultSet;
028:
029: import org.objectweb.salome_tmf.api.Api;
030: import org.objectweb.salome_tmf.api.ApiConstants;
031: import org.objectweb.salome_tmf.api.Util;
032: import org.objectweb.salome_tmf.api.sql.ISQLAttachment;
033:
034: public class SQLAttachment implements ISQLAttachment {
035:
036: /**
037: * Delete attchment idetified by idAttach in database from table ATTACHEMENT
038: * @param idAttach
039: * @throws Exception
040: * no permission needed
041: */
042: public void delete(int idAttach) throws Exception {
043: int transNumber = -1;
044: if (idAttach < 1) {
045: throw new Exception(
046: "[SQLAttachment->delete] attach have no id");
047: }
048: try {
049: transNumber = SQLEngine.beginTransaction(0,
050: ApiConstants.DELETE_ATTACHMENT);
051:
052: PreparedStatement prep = SQLEngine
053: .getSQLDeleteQuery("deleteAttachFromDB"); //ok
054: prep.setInt(1, idAttach);
055: SQLEngine.runDeleteQuery(prep);
056:
057: SQLEngine.commitTrans(transNumber);
058: } catch (Exception e) {
059: Util.log("[SQLAttachment->delete]" + e);
060: if (Api.isDEBUG()) {
061: e.printStackTrace();
062: }
063: SQLEngine.rollBackTrans(transNumber);
064: throw e;
065: }
066: }
067:
068: /**
069: * Update the description of an attachment (identified by idAttach) in the database
070: * @param idAttach
071: * @param description : the new description
072: * @throws Exception
073: * no permission needed
074: */
075: public void updateDescription(int idAttach, String description)
076: throws Exception {
077: int transNumber = -1;
078: if (idAttach < 1) {
079: throw new Exception(
080: "[SQLAttachment->delete] attach have no id");
081: }
082: try {
083: transNumber = SQLEngine.beginTransaction(0,
084: ApiConstants.UPDATE_ATTACHMENT);
085:
086: PreparedStatement prep = SQLEngine
087: .getSQLUpdateQuery("updateAttachDescription"); //ok
088: prep.setString(1, description);
089: prep.setInt(2, idAttach);
090: SQLEngine.runUpdateQuery(prep);
091:
092: SQLEngine.commitTrans(transNumber);
093: } catch (Exception e) {
094: Util.log("[SQLAttachment->updateDescription]" + e);
095: if (Api.isDEBUG()) {
096: e.printStackTrace();
097: }
098: SQLEngine.rollBackTrans(transNumber);
099: throw e;
100: }
101: }
102:
103: /**
104: * Return the last id of an attachment in the table ATTACHEMENT
105: * no permission needed
106: */
107: public int getLastIdAttach() throws Exception {
108: int maxIdAttach = -1;
109: PreparedStatement prep = SQLEngine
110: .getSQLSelectQuery("selectMaxIdAttach"); //ok
111: ResultSet stmtRes = SQLEngine.runSelectQuery(prep);
112: if (stmtRes.next()) {
113: maxIdAttach = stmtRes.getInt("max_id_attach");
114: } else {
115: throw new Exception(
116: "[SQLAttachment->getLastIdAttach] id not exist");
117: }
118: return maxIdAttach;
119: }
120: }
|