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.data;
025:
026: import java.io.File;
027: import java.sql.Date;
028: import java.util.ArrayList;
029: import java.util.GregorianCalendar;
030: import java.util.Hashtable;
031: import java.util.Vector;
032:
033: import org.objectweb.salome_tmf.api.Api;
034: import org.objectweb.salome_tmf.api.ApiConstants;
035: import org.objectweb.salome_tmf.api.data.FileAttachementWrapper;
036: import org.objectweb.salome_tmf.api.data.ParameterWrapper;
037: import org.objectweb.salome_tmf.api.data.SalomeFileWrapper;
038: import org.objectweb.salome_tmf.api.data.TestWrapper;
039: import org.objectweb.salome_tmf.api.data.UrlAttachementWrapper;
040: import org.objectweb.salome_tmf.api.sql.ISQLTest;
041:
042: abstract public class Test extends WithAttachment {
043: static ISQLTest pISQLTest = null;
044:
045: // Ajour une refence au campagne ??? pour resoudre le Warning
046:
047: /**
048: * Date de création du test
049: */
050: protected Date creationDate;
051:
052: /**
053: * Nom complet du concepteur du test
054: */
055: protected String conceptorName;
056:
057: /**
058: * Login du concepteur du test
059: */
060: protected String conceptorLogin;
061:
062: /**
063: * Nom de la suite de tests à laquelle appartient le test
064: */
065: protected TestList testList;
066:
067: protected int idSuite;
068: /**
069: * Position du test dans l'arborescence à partir de la racine
070: */
071: protected int order;
072:
073: /**
074: * Liste des paramètres du test
075: */
076: protected ArrayList parameterList;
077:
078: public Test(String name, String description) {
079: super (name, description);
080: creationDate = new Date(GregorianCalendar.getInstance()
081: .getTimeInMillis());
082: conceptorName = "";
083: conceptorLogin = "";
084: description = "";
085: testList = null;
086: order = 0;
087: parameterList = new ArrayList();
088: if (pISQLTest == null) {
089: pISQLTest = Api.getISQLObjectFactory().getISQLTest();
090: }
091: }
092:
093: /**
094: * Constructeur d'un test
095: */
096: public Test(TestWrapper pTest) {
097: super (pTest.getName(), pTest.getDescription());
098: name = pTest.getName();
099: creationDate = pTest.getCreationDate();
100: conceptorName = pTest.getConceptor(); // modofier pour avoir Nom, Prenom
101: idBdd = pTest.getIdBDD();
102: conceptorLogin = pTest.getConceptor();
103: testList = null;
104: parameterList = new ArrayList();
105: order = pTest.getOrder();
106: idSuite = pTest.getIdSuite();
107: if (pISQLTest == null) {
108: pISQLTest = Api.getISQLObjectFactory().getISQLTest();
109: }
110: } // Fin di constructeur Test/0
111:
112: public Test(TestList testList, TestWrapper pTest) {
113: this (pTest);
114: this .testList = testList;
115:
116: } // Fin di constructeur Test/0
117:
118: //public abstract void clearCache();
119:
120: public void realoadBaseFromDB() throws Exception {
121: if (!isInBase()) {
122: throw new Exception("Test " + name + " is not in BDD");
123: }
124: TestWrapper pTest = pISQLTest.getTest(idBdd);
125: name = pTest.getName();
126: description = pTest.getDescription();
127: //creationDate = pTest.getCreationDate();
128: //conceptorName = pTest.getConceptor(); // modofier pour avoir Nom, Prenom
129: //conceptorLogin = pTest.getConceptor();
130: order = pTest.getOrder();
131: //idSuite = pTest.getIdSuite();
132: }
133:
134: protected void commonReloadFromDB(boolean base,
135: Hashtable<String, Parameter> paramInModel, boolean attach)
136: throws Exception {
137: int transNuber = -1;
138: try {
139: transNuber = Api
140: .beginTransaction(101, ApiConstants.LOADING);
141:
142: if (base) {
143: realoadBaseFromDB();
144: }
145: reloadUSeParameterFromDB(paramInModel);
146: if (attach) {
147: reloadAttachmentDataFromDB(false);
148: }
149: Api.commitTrans(transNuber);
150: } catch (Exception e) {
151: Api.forceRollBackTrans(transNuber);
152: throw e;
153: }
154:
155: }
156:
157: public abstract void reloadFromDB(boolean base,
158: Hashtable<String, Parameter> paramInModel, boolean attach)
159: throws Exception;
160:
161: /**************************************************************************/
162: /** ACCESSEURS ET MUTATEURS ***/
163: /**************************************************************************/
164:
165: int getIdSuiteFromModel() {
166: return idSuite;
167: }
168:
169: public String getConceptorFromModel() {
170: return conceptorName;
171: }
172:
173: public Date getCreationDateFromModel() {
174: return creationDate;
175: }
176:
177: public TestList getTestListFromModel() {
178: return testList;
179: }
180:
181: public int getOrderFromModel() {
182: return order;
183: }
184:
185: public int getOrderFromDB() throws Exception {
186: if (!isInBase()) {
187: throw new Exception("Test " + name + " is not in BDD");
188: }
189:
190: return pISQLTest.getTest(idBdd).getOrder();
191: }
192:
193: public void setConceptorInModel(String name) {
194: conceptorName = name;
195: }
196:
197: public void setCreationDateInModel(Date date) {
198: creationDate = date;
199: }
200:
201: public void setTestListInModel(TestList list) {
202: testList = list;
203: idSuite = list.getIdBdd();
204: }
205:
206: public void setOrderInModel(int i) {
207: order = i;
208: }
209:
210: public String getConceptorLoginFromModel() {
211: return conceptorLogin;
212: }
213:
214: public void setConceptorLoginInModel(String string) {
215: conceptorLogin = string;
216: }
217:
218: abstract public String getExtensionFromModel();
219:
220: /***************************** Basic Operation *********************************/
221:
222: /* used by TestList */
223: abstract void addInDB(int idTestList) throws Exception;
224:
225: /* used by TestList */
226: void addInModel(TestList pList) {
227: testList = pList;
228: idSuite = pList.getIdBdd();
229: }
230:
231: /* used by TestList */
232: void addInDBAndModel(TestList pList) throws Exception {
233: addInModel(pList);
234: addInDB(pList.getIdBdd());
235: }
236:
237: public void updateInDB(String newName, String newDesc)
238: throws Exception {
239: if (!isInBase()) {
240: throw new Exception("Test " + name + " is not in BDD");
241: }
242: pISQLTest.update(idBdd, newName, newDesc);
243: Project.pCurrentProject.notifyChanged(ApiConstants.UPDATE_TEST,
244: this , new String(name), newName);
245: }
246:
247: public void updateInModel(String newName, String newDesc) {
248: name = newName;
249: description = newDesc;
250: }
251:
252: public void updateInDBAndModel(String newName, String newDesc)
253: throws Exception {
254: //DB
255: updateInDB(newName, newDesc);
256: //Model
257: updateInModel(newName, newDesc);
258:
259: }
260:
261: public void updateOrderInDBAndModel(boolean inc) throws Exception {
262: if (!isInBase()) {
263: throw new Exception("Test " + name + " is not in BDD");
264: }
265: order = pISQLTest.updateOrder(idBdd, inc);
266: }
267:
268: /* used by TestList */
269: void deleteInDB() throws Exception {
270: if (!isInBase()) {
271: throw new Exception("Test " + name + " is not in BDD");
272: }
273: pISQLTest.delete(idBdd);
274: Project.pCurrentProject.notifyChanged(ApiConstants.DELETE_TEST,
275: this );
276: }
277:
278: /*
279: * WARNING This methode don't delete the test in campaign model
280: * Use for that TestPlanData.deleteTestFromListFromModel()
281: */
282: abstract void deleteInDBAndModel() throws Exception;
283:
284: /*
285: * used by TestList
286: * WARNING This methode don't delete the test in campaign model
287: * Use for that TestPlanData.deleteTestFromListFromModel()
288: */
289: abstract void deleteInModel();
290:
291: /************************** ATTACHEMENTS ******************************/
292: public void addAttachementInDB(Attachment attach) throws Exception {
293: if (attach instanceof FileAttachment) {
294: addAttachFileInDB((FileAttachment) attach);
295: } else {
296: addAttachUrlInDB((UrlAttachment) attach);
297: }
298: }
299:
300: void addAttachFileInDB(FileAttachment file) throws Exception {
301: if (!isInBase()) {
302: throw new Exception("Test " + name + " is not in BDD");
303: }
304: File f = file.getLocalFile();
305: int id = pISQLTest.addAttachFile(idBdd,
306: new SalomeFileWrapper(f), file
307: .getDescriptionFromModel());
308: file.setIdBdd(id);
309:
310: }
311:
312: void addAttachUrlInDB(UrlAttachment url) throws Exception {
313: if (!isInBase()) {
314: throw new Exception("Test " + name + " is not in BDD");
315: }
316: int id = pISQLTest.addAttachUrl(idBdd, url.getNameFromModel(),
317: url.getDescriptionFromModel());
318: url.setIdBdd(id);
319: }
320:
321: public void addAttachInDBAndModel(Attachment attach)
322: throws Exception {
323: if (attach instanceof FileAttachment) {
324: addAttachFileInDB((FileAttachment) attach);
325: } else {
326: addAttachUrlInDB((UrlAttachment) attach);
327: }
328: addAttachementInModel(attach);
329: }
330:
331: public void deleteAttachementInDBAndModel(Attachment attach)
332: throws Exception {
333: deleteAttachementInDB(attach.getIdBdd());
334:
335: deleteAttachmentInModel(attach);
336: }
337:
338: protected void deleteAttachementInDB(int attachId) throws Exception {
339: if (!isInBase()) {
340: throw new Exception("Test " + name + " is not in BDD");
341: }
342: pISQLTest.deleteAttach(idBdd, attachId);
343: }
344:
345: public Vector getAttachFilesFromDB() throws Exception {
346: if (!isInBase()) {
347: throw new Exception("Test " + name + " is not in BDD");
348: }
349: FileAttachementWrapper[] tmpArray = pISQLTest
350: .getAllAttachFiles(idBdd);
351: Vector tmpVector = new Vector();
352: for (int tmpI = 0; tmpI < tmpArray.length; tmpI++) {
353: tmpVector.add(tmpArray[tmpI]);
354: }
355: return tmpVector;
356: }
357:
358: public Vector getAttachUrlsFromDB() throws Exception {
359: if (!isInBase()) {
360: throw new Exception("Test " + name + " is not in BDD");
361: }
362: UrlAttachementWrapper[] tmpArray = pISQLTest
363: .getAllAttachUrls(idBdd);
364: Vector tmpVector = new Vector();
365: for (int tmpI = 0; tmpI < tmpArray.length; tmpI++) {
366: tmpVector.add(tmpArray[tmpI]);
367: }
368: return tmpVector;
369: }
370:
371: /************************** PARAMETERS ********************************/
372:
373: public void reloadUSeParameterFromDB(Hashtable paramInModel)
374: throws Exception {
375: parameterList.clear();
376: Vector paramList = getUsedParametersFromDB();
377: for (int i = 0; i < paramList.size(); i++) {
378: ParameterWrapper pParameterWrapper = (ParameterWrapper) paramList
379: .elementAt(i);
380: Parameter param = null;
381: if (paramInModel != null) {
382: param = (Parameter) paramInModel.get(pParameterWrapper
383: .getName());
384: if (param == null) {
385: param = new Parameter(pParameterWrapper);
386: }
387: } else {
388: param = new Parameter(pParameterWrapper);
389: }
390: setUseParamInModel(param);
391: }
392: }
393:
394: public void setUseParamInDB(int paramId) throws Exception {
395: if (!isInBase()) {
396: throw new Exception("Test " + name + " is not in BDD");
397: }
398: if (paramId == -1) {
399: throw new Exception("Parameter is not in BDD");
400: }
401: pISQLTest.addUseParam(idBdd, paramId);
402: }
403:
404: public void setUseParamInModel(Parameter param) {
405: parameterList.add(param);
406: } // Fin de la méthode addParamter/1
407:
408: public void setUseParamInDBAndModel(Parameter pParam)
409: throws Exception {
410: //DB
411: setUseParamInDB(pParam.getIdBdd());
412: //Model
413: setUseParamInModel(pParam);
414: }
415:
416: public boolean hasUsedParameterNameFromModel(String name) {
417: for (int i = 0; i < parameterList.size(); i++) {
418: if (((Parameter) parameterList.get(i)).getNameFromModel()
419: .equals(name)) {
420: return true;
421: }
422: }
423: return false;
424: }
425:
426: abstract void deleteUseParameterInModel(Parameter pParm);
427:
428: protected void deleteUseParameterInModel(String name) {
429: for (int i = 0; i < parameterList.size(); i++) {
430: if (((Parameter) parameterList.get(i)).getNameFromModel()
431: .equals(name)) {
432: parameterList.remove(i);
433: return;
434: }
435: }
436: }
437:
438: void deleteUseParamInDB(Parameter pParam) throws Exception {
439: if (!isInBase()) {
440: throw new Exception("Test " + name + " is not in BDD");
441: }
442: if (!pParam.isInBase()) {
443: throw new Exception("Param " + pParam.getNameFromModel()
444: + " is not in BDD");
445: }
446: pISQLTest.deleteUseParam(idBdd, pParam.getIdBdd());
447: }
448:
449: public void deleteUseParamInDBAndModel(Parameter pParam)
450: throws Exception {
451: deleteUseParamInDB(pParam);
452: deleteUseParameterInModel(pParam);
453: }
454:
455: public Parameter getUsedParameterFromModel(String name) {
456: for (int i = 0; i < parameterList.size(); i++) {
457: if (((Parameter) parameterList.get(i)).getNameFromModel()
458: .equals(name)) {
459: return (Parameter) parameterList.get(i);
460: }
461: }
462: return null;
463: }
464:
465: public ArrayList getParameterListFromModel() {
466: return parameterList;
467: }
468:
469: public Vector getUsedParametersFromDB() throws Exception {
470: if (!isInBase()) {
471: throw new Exception("Test " + name + " is not in BDD");
472: }
473: ParameterWrapper[] tmpArray = pISQLTest.getAllUseParams(idBdd);
474: Vector tmpVector = new Vector();
475: for (int tmpI = 0; tmpI < tmpArray.length; tmpI++) {
476: tmpVector.add(tmpArray[tmpI]);
477: }
478: return tmpVector;
479: }
480:
481: ///////////////////////////////////////////////////////////////////////////////
482: public static boolean isInBase(TestList testList, Test test) {
483: return isInBase(testList, test.getNameFromModel());
484: } // Fin de la méthode isInBase/1
485:
486: public static boolean isInBase(TestList testList, String testName) {
487: try {
488: int id = pISQLTest.getID(testList.getIdBdd(), testName);
489: if (id > 0) {
490: return true;
491: }
492: return false;
493: } catch (Exception e) {
494:
495: }
496: return false;
497: } // Fin de la méthode isInBase/1
498:
499: public boolean existeInBase() throws Exception {
500: if (!isInBase()) {
501: return false;
502: }
503: return pISQLTest.getID(testList.getIdBdd(), name) == idBdd;
504: }
505: }
|