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.util.ArrayList;
028: import java.util.Enumeration;
029: import java.util.HashMap;
030: import java.util.Hashtable;
031:
032: import org.objectweb.salome_tmf.api.Api;
033: import org.objectweb.salome_tmf.api.ApiConstants;
034: import org.objectweb.salome_tmf.api.Util;
035: import org.objectweb.salome_tmf.api.data.ActionWrapper;
036: import org.objectweb.salome_tmf.api.data.ManualTestWrapper;
037: import org.objectweb.salome_tmf.api.sql.ISQLManualTest;
038:
039: public class ManualTest extends Test {
040:
041: static ISQLManualTest pISQLManualTest = null;
042:
043: private ArrayList<Action> actionList = new ArrayList<Action>();
044:
045: private boolean actionLoaded = false;
046:
047: public ManualTest(String name, String description) {
048: super (name, description);
049: //actionList = new ArrayList();
050: if (pISQLManualTest == null) {
051: pISQLManualTest = Api.getISQLObjectFactory()
052: .getISQLManualTest();
053: }
054: //actionLoaded = false;
055: }
056:
057: public ManualTest(ManualTestWrapper pTest) {
058: super (pTest);
059: //actionList = new ArrayList();
060: if (pISQLManualTest == null) {
061: pISQLManualTest = Api.getISQLObjectFactory()
062: .getISQLManualTest();
063: }
064: }
065:
066: public ManualTest(TestList pList, ManualTestWrapper pTest) {
067: super (pList, pTest);
068: //actionList = new ArrayList();
069: if (pISQLManualTest == null) {
070: pISQLManualTest = Api.getISQLObjectFactory()
071: .getISQLManualTest();
072: }
073: }
074:
075: public void reloadFromDB(boolean base,
076: Hashtable<String, Parameter> paramInModel, boolean attach)
077: throws Exception {
078: int transNuber = -1;
079: try {
080: transNuber = Api
081: .beginTransaction(101, ApiConstants.LOADING);
082: commonReloadFromDB(base, paramInModel, attach);
083: if (!DataLoader.isDynamic) {
084: reloadActionFromDB(paramInModel, attach); //if dynamique
085: }
086: Api.commitTrans(transNuber);
087: } catch (Exception e) {
088: Api.forceRollBackTrans(transNuber);
089: throw e;
090: }
091: }
092:
093: public void clearCache() {
094: if (doClearCache) {
095: actionLoaded = false;
096: if (actionList != null) {
097: actionList.clear();
098: actionList = null;
099: }
100: clearAttachCache();
101: }
102: }
103:
104: /**************************************************************************/
105: /** ACCESSEURS ET MUTATEURS ***/
106: /**************************************************************************/
107:
108: void loadActionFromDB(boolean withAttach) {
109: try {
110: reloadActionFromDB(Project.pCurrentProject
111: .getParameterSetFromModel(), withAttach);
112: } catch (Exception e) {
113:
114: }
115: }
116:
117: public ArrayList<Action> getActionListFromModel(boolean withAttach) { //TO BE Dynamic
118: if (!actionLoaded) {
119: loadActionFromDB(withAttach);
120: }
121: return actionList;
122: }
123:
124: /*public void setActionListInModel(ArrayList list) { //TO BE Dynamic
125: actionList = list;
126: } */
127:
128: public String getExtensionFromModel() {
129: return "";
130: }
131:
132: /********************************* Basic Operation ****************************/
133: /* used by TestList */
134: void addInDB(int idTestList) throws Exception {
135: if (isInBase()) {
136: throw new Exception("Test " + name + " already in BDD");
137: }
138: if (idTestList == -1) {
139: idTestList = getTestListFromModel().getIdBdd();
140: if (idTestList == -1) {
141: throw new Exception("TestList not defined");
142: }
143: }
144: idBdd = pISQLManualTest.insert(idTestList, name, description,
145: conceptorLogin, "");
146: order = getOrderFromDB();
147: Project.pCurrentProject.notifyChanged(ApiConstants.INSERT_TEST,
148: this );
149: }
150:
151: /* used by TestList */
152: /*
153: * WARNING This methode don't delete the test in campaign model
154: * Use for that TestPlanData.deleteTestFromListFromModel()
155: */
156: void deleteInModel() {
157: clearAttachInModel();
158: parameterList.clear();
159: parameterList = null;
160: if (actionList != null) {
161: for (int i = 0; i < actionList.size(); i++) {
162: Action pAction = (Action) actionList.get(i);
163: pAction.deleteInModel();
164: Project.pCurrentProject.notifyChanged(
165: ApiConstants.DELETE_ACTION, pAction);
166: }
167: actionList.clear();
168: actionList = null;
169: }
170: actionLoaded = false;
171: }
172:
173: /* used by TestList */
174: /*
175: * WARNING This methode don't delete the test in campaign model
176: * Use for that TestPlanData.deleteTestFromListFromModel()
177: */
178: void deleteInDBAndModel() throws Exception {
179: deleteInDB();
180: deleteInModel();
181: }
182:
183: /******************** ACTIONS *************************/
184:
185: public void reloadActionFromDB(
186: Hashtable<String, Parameter> paramInModel, boolean attach)
187: throws Exception {
188: if (!isInBase()) {
189: throw new Exception("Test " + name + " is not in BDD");
190: }
191: if (actionList != null) {
192: for (int i = 0; i < actionList.size(); i++) {
193: deleteActionInModel((Action) actionList.get(i));
194: }
195: actionList.clear();
196: } else {
197: actionList = new ArrayList<Action>();
198: }
199: actionLoaded = true;
200: ActionWrapper[] actionVector = pISQLManualTest
201: .getTestActions(idBdd);
202: for (int i = 0; i < actionVector.length; i++) {
203: Action action = new Action(actionVector[i], this );
204: action.reloadFromDB(false, paramInModel, attach);
205: addActionInModel(action);
206: }
207: }
208:
209: public void addActionInModel(Action action) { //TO BE Dynamic
210: if (!actionLoaded) {
211: loadActionFromDB(false);
212: }
213: actionList.add(action);
214: Enumeration<Parameter> iterator = action
215: .getParameterHashSetFromModel().elements();
216: while (iterator.hasMoreElements()) {
217: Parameter param = iterator.nextElement();
218: if (getUsedParameterFromModel(param.getNameFromModel()) == null) {
219: setUseParamInModel(param);
220: }
221: }
222: action.addInModel(this );
223: }
224:
225: public void addActionInDB(Action action) throws Exception {
226: action.addInDB(this );
227: }
228:
229: public void addActionInDBAndModel(Action action) throws Exception {
230: addActionInDB(action);
231: addActionInModel(action);
232: }
233:
234: public void deleteActionInDBModel(Action pAction) throws Exception {
235: pAction.deleteInDB();
236: deleteActionInModel(pAction);
237: }
238:
239: public void deleteActionInModel(Action pAction) { //TO BE Dynamic
240: if (actionLoaded) {
241: actionList.remove(pAction);
242: pAction.deleteInModel();
243: pAction.clearCache();
244: }
245: /*
246: actionList.remove(pAction);
247: pAction.deleteInModel();
248: */
249: }
250:
251: public void deleteActionInDB(Action pAction) throws Exception {
252: pAction.deleteInDB();
253: }
254:
255: public Action getActionFromModel(String name) { //TO BE Dynamic
256: if (!actionLoaded) {
257: loadActionFromDB(false);
258: }
259: for (int i = 0; i < actionList.size(); i++) {
260: if (((Action) actionList.get(i)).getNameFromModel().equals(
261: name)) {
262: return (Action) actionList.get(i);
263: }
264: }
265: return null;
266: }
267:
268: public boolean hasActionNameInModel(String name) { //TO BE Dynamic
269: if (!actionLoaded) {
270: loadActionFromDB(false);
271: }
272: for (int i = 0; i < actionList.size(); i++) {
273: if (((Action) actionList.get(i)).getNameFromModel().equals(
274: name)) {
275: return true;
276: }
277: }
278: return false;
279: }
280:
281: /******************** PARAMETERS **********************/
282:
283: public void deleteUseParameterInModel(Parameter pParam) {
284: Util
285: .log("[ManualTest->deleteUseParameterInModel] Delete Use Parameter "
286: + pParam + ", in test " + name);
287: if (actionLoaded) {
288: for (int i = 0; i < getActionListFromModel(false).size(); i++) {
289: Action action = (Action) (getActionListFromModel(false)
290: .get(i));
291: action.deleteUseParamInModel(pParam);
292: }
293: }
294: deleteUseParameterInModel(pParam.getNameFromModel());
295: }
296:
297: /* + Heritage de Test */
298:
299: /**** COPIER/COLER ************/
300:
301: public static ManualTest getCopie(ManualTest toCopie)
302: throws Exception {
303: ManualTest pCopie = new ManualTest(toCopie.getNameFromModel(),
304: toCopie.getDescriptionFromModel());
305: toCopie.reloadUSeParameterFromDB(DataLoader.getCurrentProject()
306: .getParameterSetFromModel());
307: ArrayList<Parameter> paramList = toCopie
308: .getParameterListFromModel();
309: for (Parameter param : paramList) {
310: pCopie.setUseParamInModel(param);
311: }
312: //pCopie.reloadUSeParameterFromDB(DataLoader.getCurrentProject().getParameterSetFromModel());
313:
314: ActionWrapper[] actionVector = pISQLManualTest
315: .getTestActions(toCopie.getIdBdd());
316: for (int i = 0; i < actionVector.length; i++) {
317: ActionWrapper pActionWrapper = actionVector[i];
318:
319: //pActionWrapper.setIdBDD(-1);
320: Action action = new Action(pActionWrapper, pCopie);
321: action.reloadFromDB(false, DataLoader.getCurrentProject()
322: .getParameterSetFromModel(), false);
323: action.setIdBdd(-1);
324: pCopie.addActionInModel(action);
325: }
326: return pCopie;
327: }
328:
329: public static ManualTest copieIn(ManualTest toCopie,
330: TestList pTestList) throws Exception {
331: ManualTest pTest = ManualTest.getCopie((ManualTest) toCopie);
332: String testName = toCopie.getNameFromModel();
333: int i = 0;
334: while (Test.isInBase(pTestList, testName)) {
335: testName = toCopie.getNameFromModel() + "_" + i;
336: i++;
337: }
338: pTest.updateInModel(testName, pTest.getDescriptionFromModel());
339: pTest.setConceptorLoginInModel(DataLoader.getCurrentUser()
340: .getLoginFromModel());
341: /* 1 Ajout du test */
342: pTestList.addTestInDBAndModel(pTest);
343:
344: /* 2 Ajout des paramtres */
345: ArrayList<Parameter> paramList = pTest
346: .getParameterListFromModel();
347: for (Parameter param : paramList) {
348: pTest.setUseParamInDB(param.getIdBdd());
349: }
350:
351: /* 3 Ajout des Actions */
352: //pTest.loadActionFromDB(false);
353: for (Action pAction : pTest.actionList) {
354: pTest.addActionInDB(pAction);
355: Hashtable<String, Parameter> parameterHashTable = pAction
356: .getParameterHashSetFromModel();
357: Enumeration<Parameter> enumElement = parameterHashTable
358: .elements();
359: while (enumElement.hasMoreElements()) {
360: Parameter param = enumElement.nextElement();
361: pAction.setUseParamInDB(param.getIdBdd());
362: }
363: }
364: return pTest;
365: }
366:
367: /**
368: * Creates a copy of a manual test in the same suite, the imported test takes
369: * the parameter "oldName" as name
370: * @param toCopy
371: * @param newNameOfOldTest
372: * @return
373: */
374: public static ManualTest createCopyInDBAndModel(ManualTest toCopy,
375: String oldName) throws Exception {
376: ManualTest pCopy = new ManualTest(oldName, toCopy
377: .getDescriptionFromModel());
378: pCopy.setConceptorLoginInModel(DataLoader.getCurrentUser()
379: .getLoginFromModel());
380:
381: //add to test list
382: toCopy.getTestListFromModel().addTestInDBAndModel(pCopy);
383:
384: //add attachments
385: HashMap<String, Attachment> attachs = toCopy
386: .getAttachmentMapFromModel();
387: for (Attachment attach : attachs.values()) {
388: if (attach instanceof UrlAttachment) {
389: pCopy.addAttachementInDBAndModel(attach);
390: } else {
391: FileAttachment fileAttach = (FileAttachment) attach;
392: File attachFile = fileAttach.getFileFromDB(null);
393: FileAttachment toAdd = new FileAttachment(attachFile,
394: fileAttach.getDescriptionFromModel());
395: pCopy.addAttachementInDBAndModel(toAdd);
396: attachFile.delete();
397: }
398: }
399:
400: //used parameter
401: toCopy.reloadUSeParameterFromDB(DataLoader.getCurrentProject()
402: .getParameterSetFromModel());
403: ArrayList<Parameter> paramList = toCopy
404: .getParameterListFromModel();
405: for (Parameter param : paramList) {
406: pCopy.setUseParamInDBAndModel(param);
407: }
408:
409: //actions
410: ActionWrapper[] actionVector = pISQLManualTest
411: .getTestActions(toCopy.getIdBdd());
412: for (int i = 0; i < actionVector.length; i++) {
413: ActionWrapper pActionWrapper = actionVector[i];
414:
415: Action pAction = new Action(pActionWrapper, pCopy);
416: pAction.reloadFromDB(false, DataLoader.getCurrentProject()
417: .getParameterSetFromModel(), false);
418: pAction.setIdBdd(-1);
419: pCopy.addActionInDBAndModel(pAction);
420: Hashtable<String, Parameter> parameterHashTable = pAction
421: .getParameterHashSetFromModel();
422: for (Parameter param : parameterHashTable.values()) {
423: pAction.setUseParamInDB(param.getIdBdd());
424: }
425: }
426:
427: ArrayList<Action> actionList = pCopy
428: .getActionListFromModel(true);
429:
430: for (Action action : actionList) {
431: HashMap<String, Attachment> actionAttachs = action
432: .getAttachmentMapFromModel();
433: for (Attachment attach : actionAttachs.values()) {
434: if (attach instanceof UrlAttachment) {
435: pCopy.addAttachementInDBAndModel(attach);
436: } else {
437: FileAttachment fileAttach = (FileAttachment) attach;
438: File attachFile = fileAttach.getFileFromDB(null);
439: FileAttachment toAdd = new FileAttachment(
440: attachFile, fileAttach
441: .getDescriptionFromModel());
442: pCopy.addAttachementInDBAndModel(toAdd);
443: attachFile.delete();
444: }
445: }
446: }
447:
448: return pCopy;
449: }
450: }
|