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: import java.util.Vector;
029:
030: import org.objectweb.salome_tmf.api.Api;
031: import org.objectweb.salome_tmf.api.ApiConstants;
032: import org.objectweb.salome_tmf.api.Permission;
033: import org.objectweb.salome_tmf.api.Util;
034: import org.objectweb.salome_tmf.api.data.ActionWrapper;
035: import org.objectweb.salome_tmf.api.data.DataUpToDateException;
036: import org.objectweb.salome_tmf.api.sql.ISQLManualTest;
037:
038: public class SQLManualTest extends SQLTest implements ISQLManualTest {
039:
040: /**
041: * Insert a Manual test in table CAS_TEST
042: * @param idTestList : the id of the testlist which contain the inserted test
043: * @param name : the name of the test
044: * @param description : the description of the tests
045: * @param conceptor : the conceptor of the test
046: * @param extension : the plug-in extension of the test
047: * @return the id of the test in table CAS_TEST
048: * @throws Exception
049: * need permission canCreateTest
050: */
051: public int insert(int idTestList, String name, String description,
052: String conceptor, String extension) throws Exception {
053: int testId = -1;
054: int transNumber = -1;
055: if (idTestList < 1) {
056: throw new Exception(
057: "[SQLManualTest->insert] entry data are not valid");
058: }
059: if (!SQLEngine.specialAllow) {
060: if (!Permission.canCreateTest()) {
061: throw new SecurityException(
062: "[SQLTest : insert -> canCreateTest]");
063: }
064: }
065: if (SQLObjectFactory.getInstanceOfISQLTestList().getTestList(
066: idTestList) == null) {
067: throw new DataUpToDateException();
068: }
069: try {
070: transNumber = SQLEngine.beginTransaction(100,
071: ApiConstants.INSERT_TEST);
072:
073: int order = SQLObjectFactory.getInstanceOfISQLTestList()
074: .getNumberOfTest(idTestList);
075: order--; //Because index begin at 0
076: PreparedStatement prep = SQLEngine
077: .getSQLAddQuery("addTest"); //ok
078: int idPers = SQLObjectFactory.getInstanceOfISQLPersonne()
079: .getID(conceptor);
080: if (idPers < 1) {
081: throw new Exception(
082: "[SQLManualTest->insert] id is not valid : "
083: + idPers + ", conceptor is :"
084: + conceptor);
085: }
086: prep.setInt(1, idPers);
087: prep.setInt(2, idTestList);
088: prep.setString(3, name);
089: prep.setDate(4, Util.getCurrentDate());
090: prep.setTime(5, Util.getCurrentTime());
091: prep.setString(6, ApiConstants.MANUAL);
092: prep.setString(7, description);
093: prep.setInt(8, order + 1);
094: prep.setString(9, extension);
095: SQLEngine.runAddQuery(prep);
096:
097: testId = getID(idTestList, name);
098: if (testId < 1) {
099: throw new Exception(
100: "[SQLManualTest->insert] id is not valid : "
101: + testId + ", TestList is :"
102: + idTestList + ", test : " + name);
103: }
104:
105: SQLEngine.commitTrans(transNumber);
106: } catch (Exception e) {
107: Util.log("[SQLManualTest->insert]" + e);
108: if (Api.isDEBUG()) {
109: e.printStackTrace();
110: }
111: SQLEngine.rollBackTrans(transNumber);
112: throw e;
113: }
114: return testId;
115: }
116:
117: /**
118: * Delete the test in the database, this include :
119: * delete test actions, and all reference (Parameter, Campaign) and the test attachments
120: * @param idTest : id of the test
121: * @throws Exception
122: * @see ISQLAction.delete(int)
123: * need permission canDeleteTest (do a special allow)
124: */
125: public void delete(int idTest) throws Exception {
126: if (idTest < 1) {
127: throw new Exception(
128: "[SQLManualTest->delete] entry data are not valid");
129: }
130: int transNumber = -1;
131: boolean dospecialAllow = false;
132: if (!SQLEngine.specialAllow) {
133: if (!Permission.canDeleteTest()) {
134: throw new SecurityException(
135: "[SQLTest : delete -> canDeleteTest]");
136: }
137: //Require specialAllow
138: dospecialAllow = true;
139: SQLEngine.setSpecialAllow(true);
140: }
141: try {
142:
143: transNumber = SQLEngine.beginTransaction(110,
144: ApiConstants.DELETE_TEST);
145:
146: // Begin Delete
147: //Delete All test actions if test is manual
148: ActionWrapper[] actionList = getTestActions(idTest);
149: for (int i = 0; i < actionList.length; i++) {
150: int actionId = (actionList[i]).getIdBDD();
151: SQLObjectFactory.getInstanceOfISQLAction().delete(
152: actionId, false);
153: }
154:
155: deleteRef(idTest, true);
156:
157: SQLEngine.commitTrans(transNumber);
158: } catch (Exception e) {
159: if (dospecialAllow) {
160: SQLEngine.setSpecialAllow(false);
161: }
162: Util.log("[SQLManualTest->delete]" + e);
163: if (Api.isDEBUG()) {
164: e.printStackTrace();
165: }
166: SQLEngine.rollBackTrans(transNumber);
167: throw e;
168: }
169: if (dospecialAllow) {
170: SQLEngine.setSpecialAllow(false);
171: }
172: }
173:
174: /**
175: * Delete reference about using parameter paramId (table CAS_PARAM_TEST) for the test and his actions
176: * @param idTest : id of the test
177: * @param paramId : id of the parameter
178: * @throws Exception
179: * need permission canUpdateTest
180: * @see deleteUseParamRef
181: * @see ISQLAction.deleteParamUse(int, int)
182: */
183: public void deleteUseParam(int idTest, int paramId)
184: throws Exception {
185: if (idTest < 1 || paramId < 1) {
186: throw new Exception(
187: "[SQLManualTest->deleteUseParam] entry data are not valid");
188: }
189: int transNumber = -1;
190: if (!SQLEngine.specialAllow) {
191: if (!Permission.canUpdateTest()) {
192: throw new SecurityException(
193: "[SQLTest : deleteUseParam -> canUpdateTest]");
194: }
195: }
196: try {
197: transNumber = SQLEngine.beginTransaction(100,
198: ApiConstants.DELETE_PARAMETER_FROM_TEST);
199:
200: ActionWrapper[] actionList = getTestActions(idTest);
201: for (int i = 0; i < actionList.length; i++) {
202: ActionWrapper pActionWrapper = actionList[i];
203: SQLObjectFactory.getInstanceOfISQLAction()
204: .deleteParamUse(pActionWrapper.getIdBDD(),
205: paramId);
206: }
207: deleteUseParamRef(idTest, paramId);
208:
209: SQLEngine.commitTrans(transNumber);
210: } catch (Exception e) {
211: Util.log("[SQLManualTest->deleteUseParam]" + e);
212: if (Api.isDEBUG()) {
213: e.printStackTrace();
214: }
215: SQLEngine.rollBackTrans(transNumber);
216: throw e;
217: }
218: }
219:
220: /**
221: * Get a Vector of ActionWrapper representing all Action of the test
222: * @param idTest : id of the test
223: * @return
224: * @throws Exception
225: */
226: public ActionWrapper[] getTestActions(int idTest) throws Exception {
227: if (idTest < 1) {
228: throw new Exception(
229: "[SQLManualTest->getTestActions] entry data are not valid");
230: }
231: Vector result = new Vector();
232: PreparedStatement prep = SQLEngine
233: .getSQLSelectQuery("selectTestActions"); //ok
234: prep.setInt(1, idTest);
235: ResultSet stmtRes = SQLEngine.runSelectQuery(prep);
236:
237: while (stmtRes.next()) {
238: ActionWrapper pAction = new ActionWrapper();
239: pAction.setName(stmtRes.getString("nom_action"));
240: pAction.setDescription(stmtRes
241: .getString("description_action"));
242: pAction.setAwaitedResult(stmtRes
243: .getString("res_attendu_action"));
244: pAction.setOrderIndex(stmtRes.getInt("num_step_action"));
245: pAction.setIdBDD(stmtRes.getInt("id_action"));
246: pAction.setOrder(stmtRes.getInt("num_step_action"));
247: pAction.setIdTest(stmtRes.getInt("CAS_TEST_id_cas"));
248: result.addElement(pAction);
249: }
250: ActionWrapper[] awArray = new ActionWrapper[result.size()];
251: for (int i = 0; i < result.size(); i++) {
252: awArray[i] = (ActionWrapper) result.get(i);
253: }
254: return awArray;
255: }
256:
257: /**
258: * Get the number of action contening in the test identified by idTest
259: * @param idTest
260: * @return
261: * @throws Exception
262: */
263: public int getNumberOfAction(int idTest) throws Exception {
264: return getTestActions(idTest).length;
265: }
266:
267: /**
268: * Get a ActionWrapper representing an action in position order for the test idTest
269: * @param idTest : id of the test
270: * @param order : order of the action
271: * @return a ActionWrapper
272: * @throws Exception
273: */
274: public ActionWrapper getActionByOrder(int idTest, int order)
275: throws Exception {
276: if (idTest < 1 || order < 0) {
277: throw new Exception(
278: "[SQLManualTest->getActionByOrder] entry data are not valid");
279: }
280: ActionWrapper pAction = null;
281: PreparedStatement prep = SQLEngine
282: .getSQLSelectQuery("selectActionByOrder"); //ok
283: prep.setInt(1, idTest);
284: prep.setInt(2, order);
285: ResultSet stmtRes = SQLEngine.runSelectQuery(prep);
286:
287: while (stmtRes.next()) {
288: pAction = new ActionWrapper();
289: pAction.setName(stmtRes.getString("nom_action"));
290: pAction.setDescription(stmtRes
291: .getString("description_action"));
292: pAction.setIdBDD(stmtRes.getInt("id_action"));
293: pAction.setOrder(stmtRes.getInt("num_step_action"));
294: }
295: return pAction;
296: }
297: }
|