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.Permission;
032: import org.objectweb.salome_tmf.api.Util;
033: import org.objectweb.salome_tmf.api.data.DataUpToDateException;
034: import org.objectweb.salome_tmf.api.data.SalomeFileWrapper;
035: import org.objectweb.salome_tmf.api.data.ScriptWrapper;
036: import org.objectweb.salome_tmf.api.sql.ISQLAutomaticTest;
037:
038: public class SQLAutomaticTest extends SQLTest implements
039: ISQLAutomaticTest {
040:
041: /**
042: * Insert an Automatic test in table CAS_TEST
043: * @param idTestList : the id of the testlist which contain the inserted test
044: * @param name : the name of the test
045: * @param description : the description of the tests
046: * @param conceptor : the conceptor of the test
047: * @param extension : the plug-in extension of the test
048: * @return the id of the test in table CAS_TEST
049: * @throws Exception
050: * need permission canCreateTest
051: */
052: public int insert(int idTestList, String name, String description,
053: String conceptor, String extension) throws Exception {
054: int testId = -1;
055: int transNumber = -1;
056: if (!SQLEngine.specialAllow) {
057: if (!Permission.canCreateTest()) {
058: throw new SecurityException(
059: "[SQLTest : insert -> canCreateTest]");
060: }
061: }
062: if (idTestList < 1) {
063: throw new Exception(
064: "[SQLAutomaticTest->insert] suite have no id");
065: }
066: if (SQLObjectFactory.getInstanceOfISQLTestList().getTestList(
067: idTestList) == null) {
068: throw new DataUpToDateException();
069: }
070: try {
071: transNumber = SQLEngine.beginTransaction(100,
072: ApiConstants.INSERT_TEST);
073:
074: int order = SQLObjectFactory.getInstanceOfISQLTestList()
075: .getNumberOfTest(idTestList);
076: order--; //Because index begin at 0
077: PreparedStatement prep = SQLEngine
078: .getSQLAddQuery("addTest"); //ok
079: int idPers = SQLObjectFactory.getInstanceOfISQLPersonne()
080: .getID(conceptor);
081: if (idPers < 1) {
082: throw new Exception(
083: "[SQLAutomaticTest->insert] user have no id");
084: }
085: prep.setInt(1, idPers);
086: prep.setInt(2, idTestList);
087: prep.setString(3, name);
088: prep.setDate(4, Util.getCurrentDate());
089: prep.setTime(5, Util.getCurrentTime());
090: prep.setString(6, ApiConstants.AUTOMATIC);
091: prep.setString(7, description);
092: prep.setInt(8, order + 1);
093: prep.setString(9, extension);
094: SQLEngine.runAddQuery(prep);
095:
096: testId = getID(idTestList, name);
097: if (testId < 1) {
098: throw new Exception(
099: "[SQLAutomaticTest->insert] test have no id");
100: }
101: SQLEngine.commitTrans(transNumber);
102: } catch (Exception e) {
103: Util.log("[SQLAutomaticTest->insert]" + e);
104: if (Api.isDEBUG()) {
105: e.printStackTrace();
106: }
107: SQLEngine.rollBackTrans(transNumber);
108: throw e;
109: }
110: return testId;
111: }
112:
113: /**
114: * Insert a Script to the test
115: * @param idTest : id of the test to insert the script
116: * @param file : the file of the script
117: * @param description : the description of the file
118: * @param name : the name of the script (localisation/name) (url_script in BDD)
119: * @param arg1 : argument 1 of the script (free use for plug-in) (classe_autom_script in BDD)
120: * @param extension : argument 1 of the script (plug-in extenstion) (classpath_script in BDD)
121: * @return the id of script inserted in the table SCRIPT_ATTACHEMENT
122: * @throws Exception
123: * @see ISQLFileAttachment.insert(File, String)
124: * @see ISQLScript.addAttach(int, int);
125: * need permission canUpdateTest
126: */
127: public int addScript(int idTest, SalomeFileWrapper file,
128: String description, String name, String arg1,
129: String extension) throws Exception {
130: int idScript = -1;
131: int idAttach = -1;
132: int transNumber = -1;
133: if (idTest < 1) {
134: throw new Exception(
135: "[SQLAutomaticTest->addScript] test have no id");
136: }
137: if (file == null) {
138: throw new Exception(
139: "[SQLAutomaticTest->addScript] file is null");
140: }
141: if (!SQLEngine.specialAllow) {
142: if (!Permission.canUpdateTest()) {
143: throw new SecurityException(
144: "[SQLTest : deleteScript -> canUpdateTest]");
145: }
146: }
147: if (getTest(idTest) == null) {
148: throw new DataUpToDateException();
149: }
150: try {
151: transNumber = SQLEngine.beginTransaction(100,
152: ApiConstants.INSERT_SCRIPT);
153: idScript = SQLObjectFactory.getInstanceOfISQLScript()
154: .insert(name, arg1, extension,
155: ApiConstants.TEST_SCRIPT);
156:
157: idAttach = SQLObjectFactory
158: .getInstanceOfISQLFileAttachment().insert(file,
159: description);
160:
161: SQLObjectFactory.getInstanceOfISQLScript().addAttach(
162: idScript, idAttach);
163:
164: PreparedStatement prep = SQLEngine
165: .getSQLAddQuery("attachScriptToTest"); //ok
166: prep.setInt(1, idScript);
167: prep.setInt(2, idTest);
168: SQLEngine.runAddQuery(prep);
169:
170: SQLEngine.commitTrans(transNumber);
171: } catch (Exception e) {
172: Util.log("[SQLAutomaticTest->addScript]" + e);
173: if (Api.isDEBUG()) {
174: e.printStackTrace();
175: }
176: SQLEngine.rollBackTrans(transNumber);
177: throw e;
178: }
179: return idScript;
180: }
181:
182: /**
183: * Delete the test in the database, this include :
184: * delete test script, and all reference (Parameter, Campaign) and the test attachments
185: * @param idTest
186: * @throws Exception
187: * need permission canDeleteTest (do a special allow)
188: */
189: public void delete(int idTest) throws Exception {
190: int transNumber = -1;
191: boolean dospecialAllow = false;
192: if (idTest < 1) {
193: throw new Exception(
194: "[SQLAutomaticTest->delete] test have no id");
195: }
196: if (!SQLEngine.specialAllow) {
197: if (!Permission.canDeleteTest()) {
198: throw new SecurityException(
199: "[SQLTest : updateOrder -> canDeleteTest]");
200: }
201: //Require specialAllow
202: dospecialAllow = true;
203: SQLEngine.setSpecialAllow(true);
204: }
205:
206: try {
207:
208: transNumber = SQLEngine.beginTransaction(110,
209: ApiConstants.DELETE_TEST);
210:
211: // Begin Delete
212:
213: //Delete Script Test if test is Automatic
214: deleteScript(idTest);
215:
216: // Suppression du test
217: deleteRef(idTest, true); //reorder
218:
219: SQLEngine.commitTrans(transNumber);
220: } catch (Exception e) {
221: if (dospecialAllow) {
222: SQLEngine.setSpecialAllow(false);
223: }
224: Util.log("[SQLAutomaticTest->delete]" + e);
225: if (Api.isDEBUG()) {
226: e.printStackTrace();
227: }
228: SQLEngine.rollBackTrans(transNumber);
229: throw e;
230: }
231: if (dospecialAllow) {
232: SQLEngine.setSpecialAllow(false);
233: }
234: }
235:
236: /**
237: * Delete reference about using parameter paramId (table CAS_PARAM_TEST) for the test
238: * @param idTest
239: * @param paramId
240: * @throws Exception
241: * need permission canUpdateTest
242: * @see deleteUseParamRef
243: */
244: public void deleteUseParam(int idTest, int paramId)
245: throws Exception {
246: int transNumber = -1;
247: if (idTest < 1) {
248: throw new Exception(
249: "[SQLAutomaticTest->deleteUseParam] test have no id");
250: }
251: if (paramId < 1) {
252: throw new Exception(
253: "[SQLAutomaticTest->deleteUseParam] param have no id");
254: }
255: if (!SQLEngine.specialAllow) {
256: if (!Permission.canUpdateTest()) {
257: throw new SecurityException(
258: "[SQLAutomaticTest : deleteUseParam -> canUpdateTest]");
259: }
260: }
261: try {
262: transNumber = SQLEngine.beginTransaction(110,
263: ApiConstants.DELETE_PARAMETER_FROM_TEST);
264:
265: deleteUseParamRef(idTest, paramId);
266:
267: SQLEngine.commitTrans(transNumber);
268: } catch (Exception e) {
269: Util.log("[SQLAutomaticTest->deleteUseParam]" + e);
270: if (Api.isDEBUG()) {
271: e.printStackTrace();
272: }
273: SQLEngine.rollBackTrans(transNumber);
274: throw e;
275: }
276: }
277:
278: /**
279: * Delete the script used by the tests, this include the removing of Attachement and all reference
280: * @param testId
281: * @throws Exception
282: * need permission canUpdateTest
283: * @see ISQLScript().delete(int, int)
284: */
285: public void deleteScript(int testId) throws Exception {
286: if (testId < 1) {
287: throw new Exception(
288: "[SQLAutomaticTest->deleteScript] test have no id");
289: }
290: int transNumber = -1;
291: if (!SQLEngine.specialAllow) {
292: if (!Permission.canUpdateTest()) {
293: throw new SecurityException(
294: "[SQLTest : deleteScript -> canUpdateTest]");
295: }
296: }
297: try {
298: transNumber = SQLEngine.beginTransaction(100,
299: ApiConstants.UPDATE_TEST);
300:
301: PreparedStatement prep = SQLEngine
302: .getSQLSelectQuery("selectScriptAttchOfTest");
303: prep.setInt(1, testId);
304: ResultSet stmtRes = SQLEngine.runSelectQuery(prep);
305: if (stmtRes.next()) {
306: int scriptId = stmtRes.getInt("SCRIPT_id_script");
307: //int attachId = stmtRes.getInt("ATTACHEMENT_id_attach");
308:
309: SQLObjectFactory.getInstanceOfISQLScript().delete(
310: scriptId);
311: //SQLObjectFactory.getInstanceOfISQLAttachment().delete(attachId);
312: prep = SQLEngine
313: .getSQLUpdateQuery("updateScriptFromTest"); //ok
314: prep.setInt(1, testId);
315: SQLEngine.runUpdateQuery(prep);
316: }
317:
318: SQLEngine.commitTrans(transNumber);
319: } catch (Exception e) {
320: Util.log("[SQLAutomaticTest->deleteScript]" + e);
321: if (Api.isDEBUG()) {
322: e.printStackTrace();
323: }
324: SQLEngine.rollBackTrans(transNumber);
325: throw e;
326: }
327: }
328:
329: /**
330: * Get a ScriptWrapper representing the script for the tests testId
331: * @param testId : id of the test
332: * @return
333: * @throws Exception
334: * @see ScriptWrapper
335: */
336: public ScriptWrapper getTestScript(int testId) throws Exception {
337: ScriptWrapper pScript = null;
338: PreparedStatement prep = SQLEngine
339: .getSQLSelectQuery("selectTestScript"); //ok
340: prep.setInt(1, testId);
341: ResultSet stmtRes = SQLEngine.runSelectQuery(prep);
342: if (stmtRes.next()) {
343: pScript = new ScriptWrapper();
344: pScript.setName(stmtRes.getString("url_script"));
345: pScript.setScriptExtension(stmtRes
346: .getString("classpath_script"));
347: pScript
348: .setPlugArg(stmtRes
349: .getString("classe_autom_script"));
350: pScript.setType(stmtRes.getString("type_script"));
351: pScript.setIdBDD(stmtRes.getInt("id_script"));
352: }
353: return pScript;
354: }
355:
356: /**
357: * Get the Script File atached to the script for the tests testId
358: * @param testId
359: * @return
360: * @throws Exception
361: * @see ISQLScript.getFile(int)
362: * no permission needed
363: */
364: public SalomeFileWrapper getTestScriptFile(int testId)
365: throws Exception {
366: ScriptWrapper pScript = getTestScript(testId);
367: return SQLObjectFactory.getInstanceOfISQLScript().getFile(
368: pScript.getIdBDD());
369: }
370: }
|