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 salomeTMF_plug.requirements.sqlWrapper;
025:
026: import java.sql.PreparedStatement;
027: import java.sql.ResultSet;
028: import java.util.Properties;
029:
030: import org.objectweb.salome_tmf.api.Api;
031: import org.objectweb.salome_tmf.api.Util;
032: import org.objectweb.salome_tmf.api.sql.IDataBase;
033: import org.objectweb.salome_tmf.api.sql.ISQLEngine;
034: import org.objectweb.salome_tmf.api.sql.ISQLObjectFactory;
035:
036: public class SQLWrapper {
037: static Properties addSqlQuery;
038: static Properties deleteSqlQuery;
039: static Properties updateSqlQuery;
040: static Properties selectSqlQuery;
041: static Properties comonQuery;
042:
043: static IDataBase db;
044:
045: static SQLCommon pSQLCommon = new SQLCommon();
046: static ISQLRequirement pSQLRequirement = new SQLRequirement();
047:
048: static ISQLEngine pISQLEngine = null;
049:
050: public static void init(ISQLObjectFactory pISQLObjectFactory)
051: throws Exception {
052: addSqlQuery = Util
053: .getPropertiesFile(SQLWrapper.class
054: .getResource("/salomeTMF_plug/requirements/resources/sql/addSqlQuery.properties"));
055:
056: comonQuery = Util
057: .getPropertiesFile(SQLWrapper.class
058: .getResource("/salomeTMF_plug/requirements/resources/sql/commonSqlQuery.properties"));
059:
060: updateSqlQuery = Util
061: .getPropertiesFile(SQLWrapper.class
062: .getResource("/salomeTMF_plug/requirements/resources/sql/updateSqlQuery.properties"));
063:
064: selectSqlQuery = Util
065: .getPropertiesFile(SQLWrapper.class
066: .getResource("/salomeTMF_plug/requirements/resources/sql/selectSqlQuery.properties"));
067:
068: deleteSqlQuery = Util
069: .getPropertiesFile(SQLWrapper.class
070: .getResource("/salomeTMF_plug/requirements/resources/sql/delSqlQuery.properties"));
071:
072: db = pISQLObjectFactory.getInstanceOfSalomeDataBase();
073: Util.log("[SQLWrapper->init] database is " + db);
074: pISQLEngine = pISQLObjectFactory.getCurrentSQLEngine();
075:
076: if (pISQLEngine != null && !Api.isSOAP()) {
077: String SqlLockWrite = comonQuery.getProperty("lockWrite");
078: Util.log("[SQLWrapper->init] Add lock Write "
079: + SqlLockWrite + " to " + pISQLEngine);
080: String SqlLockRead = comonQuery.getProperty("lockWrite");
081: Util.log("[SQLWrapper->init] Add lock Read " + SqlLockRead
082: + " to " + pISQLEngine);
083: installPlugin();
084: pISQLEngine.addSQLLoackWrite(SqlLockWrite);
085: pISQLEngine.addSQLLoackRead(SqlLockRead);
086: }
087: }
088:
089: static void installPlugin() throws Exception {
090: pSQLCommon.installPlugin();
091: }
092:
093: public static ISQLRequirement getSQLRequirement() {
094: // Sélection de la factory en fonction de la version (SQL ou SOAP)
095:
096: // Version SQL
097: ISQLRequirement pSQLRequirement = SQLWrapper.pSQLRequirement;
098:
099: if (Api.isSOAP()) {
100: // Version SOAP
101: try {
102: ISQLSoapFactory pISQLSoapFactory = (ISQLSoapFactory) Class
103: .forName(
104: "salomeTMF_plug.requirements.soap.SQLSoapFactory")
105: .newInstance();
106: pSQLRequirement = pISQLSoapFactory.getISQLRequirement();
107: } catch (Exception e) {
108: e.printStackTrace();
109: }
110: }
111:
112: return pSQLRequirement;
113: }
114:
115: static PreparedStatement getSQLAddQuery(String queryName)
116: throws Exception {
117: Util.log("[SQLWrapper->getSQLAddQuery]" + queryName);
118: String sql = addSqlQuery.getProperty(queryName);
119: Util.log("[SQLWrapper->getSQLAddQuery] sql is " + sql);
120: PreparedStatement prep = db.prepareStatement(sql);
121: return prep;
122: }
123:
124: static PreparedStatement getSQLCommonQuery(String queryName)
125: throws Exception {
126: Util.log("[SQLWrapper->getSQLCommonQuery]" + queryName);
127: String sql = comonQuery.getProperty(queryName);
128: Util.log("[SQLWrapper->getSQLCommonQuery] sql is " + sql);
129: PreparedStatement prep = db.prepareStatement(sql);
130: return prep;
131: }
132:
133: static PreparedStatement getSQLSelectQuery(String queryName)
134: throws Exception {
135: Util.log("[SQLWrapper->getSQLSelectQuery]" + queryName + " : "
136: + selectSqlQuery);
137: String sql = selectSqlQuery.getProperty(queryName);
138: Util.log("[SQLWrapper->getSQLSelectQuery] sql is " + sql);
139: PreparedStatement prep = db.prepareStatement(sql);
140: return prep;
141: }
142:
143: static PreparedStatement getSQLUpdateQuery(String queryName)
144: throws Exception {
145: Util.log("[SQLWrapper->getSQLUpdateQuery]" + queryName);
146: String sql = updateSqlQuery.getProperty(queryName);
147: Util.log("[SQLWrapper->getSQLUpdateQuery] sql is " + sql);
148: PreparedStatement prep = db.prepareStatement(sql);
149: return prep;
150: }
151:
152: static PreparedStatement getSQLDeleteQuery(String queryName)
153: throws Exception {
154: Util.log("[SQLWrapper->getSQLDeleteQuery]" + queryName);
155: String sql = deleteSqlQuery.getProperty(queryName);
156: Util.log("[SQLWrapper->getSQLDeleteQuery] sql is " + sql);
157: PreparedStatement prep = db.prepareStatement(sql);
158: return prep;
159: }
160:
161: static synchronized int runAddQuery(PreparedStatement prep)
162: throws Exception {
163: return prep.executeUpdate();
164: }
165:
166: static synchronized int runDeleteQuery(PreparedStatement prep)
167: throws Exception {
168: return prep.executeUpdate();
169: }
170:
171: static synchronized int runUpdateQuery(PreparedStatement prep)
172: throws Exception {
173: return prep.executeUpdate();
174: }
175:
176: static synchronized ResultSet runSelectQuery(PreparedStatement prep)
177: throws Exception {
178: return prep.executeQuery();
179: }
180:
181: static public synchronized int beginTransaction() throws Exception {
182: if (pISQLEngine != null) {
183: return pISQLEngine.beginTransDB(0, 1000);
184: }
185: return -1;
186: }
187:
188: static synchronized public void commitTrans(int id)
189: throws Exception {
190: if (pISQLEngine != null) {
191: pISQLEngine.commitTransDB(id);
192: }
193: }
194:
195: static synchronized public void rollBackTrans(int id)
196: throws Exception {
197: if (pISQLEngine != null) {
198: pISQLEngine.rollForceBackTransDB(id);
199: }
200: }
201:
202: }
|