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: package org.objectweb.salome_tmf.data;
024:
025: import java.io.File;
026: import java.io.Serializable;
027: import java.util.Enumeration;
028: import java.util.HashSet;
029: import java.util.Hashtable;
030: import java.util.Iterator;
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.Util;
036: import org.objectweb.salome_tmf.api.data.ActionWrapper;
037: import org.objectweb.salome_tmf.api.data.FileAttachementWrapper;
038: import org.objectweb.salome_tmf.api.data.ParameterWrapper;
039: import org.objectweb.salome_tmf.api.data.SalomeFileWrapper;
040: import org.objectweb.salome_tmf.api.data.UrlAttachementWrapper;
041: import org.objectweb.salome_tmf.api.sql.ISQLAction;
042:
043: public class Action extends WithAttachment {
044: static ISQLAction pISQLAction = null;
045:
046: private String awaitedResult;
047: private int orderIndex;
048: private Hashtable parameterHashTable;
049: private Test pTest = null;
050:
051: public Action(Test pTest, String name, String description) {
052: super (name, description);
053: awaitedResult = "";
054: orderIndex = 0;
055: parameterHashTable = new Hashtable();
056: this .pTest = pTest;
057: if (pISQLAction == null) {
058: pISQLAction = Api.getISQLObjectFactory().getISQLAction();
059: }
060: }
061:
062: public Action(ActionWrapper pAction, Test pTest) {
063: this (pTest, pAction.getName(), pAction.getDescription());
064: awaitedResult = pAction.getAwaitedResult();
065: orderIndex = pAction.getOrderIndex();
066: idBdd = pAction.getIdBDD();
067: }
068:
069: public Action(Action pAction, Test pTest) {
070: this (pTest, pAction.getNameFromModel(), pAction
071: .getDescriptionFromModel());
072: awaitedResult = pAction.getAwaitedResultFromModel();
073: } // Fin du constructeur Action/1
074:
075: protected void reloadBaseFromDB() throws Exception {
076: if (isInBase()) {
077: throw new Exception("Action " + name + " is already in BDD");
078: }
079: ActionWrapper pActionWrapper = pISQLAction
080: .getActionWrapper(idBdd);
081: awaitedResult = pActionWrapper.getAwaitedResult();
082: orderIndex = pActionWrapper.getOrderIndex();
083: name = pActionWrapper.getName();
084: description = pActionWrapper.getDescription();
085: }
086:
087: public void reloadFromDB(boolean base, Hashtable paramsInModel,
088: boolean attach) throws Exception {
089: int transNuber = -1;
090: try {
091: transNuber = Api
092: .beginTransaction(101, ApiConstants.LOADING);
093: if (base) {
094: reloadBaseFromDB();
095: }
096: reloadUsedParameterFromDB(paramsInModel);
097: if (attach) {
098: reloadAttachmentDataFromDB(false);
099: }
100: Api.commitTrans(transNuber);
101: } catch (Exception e) {
102: Api.forceRollBackTrans(transNuber);
103: throw e;
104: }
105: }
106:
107: public void clearCache() {
108: /* TODO ClearAttachement */
109:
110: }
111:
112: /******************************************************************************/
113: /** ACCESSEURS ET MUTATEURS ***/
114: /******************************************************************************/
115:
116: public String getAwaitedResultFromModel() {
117: return awaitedResult;
118: }
119:
120: public void setAwaitedResultInModel(String string) {
121: awaitedResult = string;
122: }
123:
124: public int getOrderIndexFromModel() {
125: return orderIndex;
126: }
127:
128: public void setOrderIndex(int i) {
129: orderIndex = i;
130: }
131:
132: public Test getTest() {
133: return pTest;
134: }
135:
136: /////////////////////////////// Basic Operation /////////////////////////
137:
138: /* Used by Manuel Test */
139: void addInDB(Test pTest) throws Exception {
140: //boolean needUpdate = false;
141: if (isInBase()) {
142: throw new Exception("Action " + name + " is already in BDD");
143: }
144: if (!pTest.isInBase()) {
145: throw new Exception("Test " + pTest.getNameFromModel()
146: + " is not in BDD");
147: }
148: int id = pISQLAction.insert(pTest.getIdBdd(), name,
149: description, awaitedResult);
150: setIdBdd(id);
151: orderIndex = pISQLAction.getActionWrapper(id).getOrder();
152: /*if (orderIndex != rowCount){
153: needUpdate = true;
154: }
155: return needUpdate;*/
156: Project.pCurrentProject.notifyChanged(
157: ApiConstants.INSERT_ACTION, this );
158:
159: }
160:
161: /* Used by Manuel Test */
162: void addInModel(Test pTest) {
163: this .pTest = pTest;
164: }
165:
166: /* Used by Manuel Test */
167: void addInDBAndModel(Test pTest) throws Exception {
168: //boolean res;
169: addInDB(pTest);
170: addInModel(pTest);
171: //return res;
172: }
173:
174: public void updateInDB(String newActionName, String newActionDesc,
175: String newActionResAttendu) throws Exception {
176: if (!isInBase()) {
177: throw new Exception("Action " + name + " is not in BDD");
178: }
179: pISQLAction.update(idBdd, newActionName, newActionDesc,
180: newActionResAttendu);
181: Project.pCurrentProject.notifyChanged(
182: ApiConstants.UPDATE_ACTION, this , new String(name),
183: newActionName);
184:
185: }
186:
187: public void updateInModel(String newActionName,
188: String newActionDesc, String newActionResAttendu) {
189: setNameInModel(newActionName);
190: updateDescriptionInModel(newActionDesc);
191: setAwaitedResultInModel(newActionResAttendu);
192: }
193:
194: public void updateInDBAndModel(String newActionName,
195: String newActionDesc, String newActionResAttendu)
196: throws Exception {
197: updateInDB(newActionName, newActionDesc, newActionResAttendu);
198: updateInModel(newActionName, newActionDesc, newActionResAttendu);
199: }
200:
201: public void updateInDBAndModel(String newName, String newDesc)
202: throws Exception {
203: updateInDB(newName, newDesc, awaitedResult);
204: updateInModel(newName, newDesc, awaitedResult);
205: }
206:
207: public void updateOrderInDBAndModel(boolean inc) throws Exception {
208: if (!isInBase()) {
209: throw new Exception("Action " + name + " is not in BDD");
210: }
211: orderIndex = pISQLAction.updateOrder(idBdd, inc);
212: }
213:
214: /* Used by Manuel Test */
215: void deleteInDB() throws Exception {
216: if (!isInBase()) {
217: throw new Exception("Action " + name + " is not in BDD");
218: }
219: pISQLAction.delete(idBdd);
220: Project.pCurrentProject.notifyChanged(
221: ApiConstants.DELETE_ACTION, this );
222: }
223:
224: /* Used by Manuel Test */
225: void deleteInModel() {
226: pTest = null;
227: parameterHashTable.clear();
228: clearAttachInModel();
229: }
230:
231: /* Used by Manuel Test */
232: void deleteInDBAndModel() throws Exception {
233: deleteInDB();
234: deleteInModel();
235: }
236:
237: //////////////// PARAMETERS ////////////////////////
238:
239: public void setParameterHashSetInModel(HashSet set) {
240: parameterHashTable.clear();
241: for (Iterator iter = set.iterator(); iter.hasNext();) {
242: Parameter param = (Parameter) iter.next();
243: parameterHashTable.put(param.getNameFromModel(), param);
244: }
245:
246: }
247:
248: public void setParameterHashtableInModel(Hashtable table) {
249: parameterHashTable.clear();
250: parameterHashTable = table;
251: }
252:
253: public Hashtable getCopyOfParameterHashTableFromModel() {
254: Hashtable copyParameter = new Hashtable();
255: Enumeration enumKey = parameterHashTable.keys();
256: while (enumKey.hasMoreElements()) {
257: Object key = enumKey.nextElement();
258: copyParameter.put(key, parameterHashTable.get(key));
259: }
260: return copyParameter;
261: }
262:
263: public void reloadUsedParameterFromDB(Hashtable parametersInModel)
264: throws Exception {
265: if (!isInBase()) {
266: throw new Exception("Action " + name + " is not in BDD");
267: }
268: ParameterWrapper[] paramActionArray = pISQLAction
269: .getParamsUses(idBdd);
270: for (int i = 0; i < paramActionArray.length; i++) {
271: Parameter param = null;
272: ParameterWrapper pParameterWrapper = paramActionArray[i];
273: if (parametersInModel != null) {
274: param = (Parameter) parametersInModel
275: .get(pParameterWrapper.getName());
276: if (param == null) {
277: param = new Parameter(pParameterWrapper);
278: }
279: }
280: setUseParamInModel(param);
281: }
282: }
283:
284: public void setUseParamInModel(Parameter pParam) {
285: parameterHashTable.put(pParam.getNameFromModel(), pParam);
286: if (pTest != null) {
287: if (pTest.getUsedParameterFromModel(pParam
288: .getNameFromModel()) == null) {
289: pTest.setUseParamInModel(pParam);
290: }
291: }
292: }
293:
294: public void setUseParamInDB(int paramId) throws Exception {
295: if (!isInBase()) {
296: throw new Exception("Action " + name + " is not in BDD");
297: }
298: pISQLAction.addUseParam(idBdd, paramId);
299: }
300:
301: public void setUseParamInDBAndModel(Parameter pParam)
302: throws Exception {
303: //DB
304: setUseParamInDB(pParam.getIdBdd());
305: //model
306: setUseParamInModel(pParam);
307: }
308:
309: public void deleteUseParamInDB(int paramId) throws Exception {
310: if (!isInBase()) {
311: throw new Exception("Action " + name + " is not in BDD");
312: }
313: pISQLAction.deleteParamUse(idBdd, paramId);
314: }
315:
316: public void deleteUseParamInDBAndModel(Parameter pParam)
317: throws Exception {
318: deleteUseParamInDB(pParam.getIdBdd());
319: deleteUseParamInModel(pParam);
320: }
321:
322: public void deleteUseParamInModel(Parameter pParam) {
323: // Clean Action
324: String newDesc = clearStringOfParameter(
325: getDescriptionFromModel(), pParam);
326: String newResult = clearStringOfParameter(
327: getAwaitedResultFromModel(), pParam);
328: description = newDesc;
329: awaitedResult = newResult;
330: Object o = parameterHashTable.remove(pParam.getNameFromModel());
331: Util
332: .log("[Action->deleteUseParamInModel] Delete Use Parameter "
333: + pParam
334: + ", in Action "
335: + name
336: + " res is "
337: + o);
338:
339: }
340:
341: public Parameter getParameterFromModel(String name) {
342: Enumeration paramList = parameterHashTable.elements();
343: while (paramList.hasMoreElements()) {
344: Parameter param = (Parameter) paramList.nextElement();
345: if (param.getNameFromModel().equals(name)) {
346: return param;
347: }
348: }
349: return null;
350: }
351:
352: public Parameter getParameterFromDB(String name) throws Exception {
353: if (!isInBase()) {
354: throw new Exception("Action " + name + " is not in BDD");
355: }
356: HashSet result = getParameterHashSetFromDB();
357: for (Iterator iter = result.iterator(); iter.hasNext();) {
358: Parameter param = (Parameter) iter.next();
359: if (param.getNameFromModel().equals(name)) {
360: return param;
361: }
362: }
363: return null;
364: }
365:
366: public HashSet getParameterHashSetFromDB() throws Exception {
367: if (!isInBase()) {
368: throw new Exception("Action " + name + " is not in BDD");
369: }
370: HashSet result = new HashSet();
371: if (!isInBase()) {
372: throw new Exception("Action " + name + " is not in BDD");
373: }
374: ParameterWrapper[] listParamWrapper = pISQLAction
375: .getParamsUses(idBdd);
376: for (int i = 0; i < listParamWrapper.length; i++) {
377: result.add(new Parameter(listParamWrapper[i]));
378: }
379: return result;
380: }
381:
382: public Vector getParameterVectorFromDB() throws Exception {
383: if (!isInBase()) {
384: throw new Exception("Action " + name + " is not in BDD");
385: }
386: Vector result = new Vector();
387: if (!isInBase()) {
388: throw new Exception("Action " + name + " is not in BDD");
389: }
390: ParameterWrapper[] listParamWrapper = pISQLAction
391: .getParamsUses(idBdd);
392: for (int i = 0; i < listParamWrapper.length; i++) {
393: result.add(new Parameter(listParamWrapper[i]));
394: }
395: return result;
396: }
397:
398: public Hashtable getParameterHashSetFromModel() {
399: return parameterHashTable;
400: }
401:
402: ////////////// ATTACHEMENT ///////////////////////
403:
404: public void addAttachementInDB(Attachment attach) throws Exception {
405: if (attach instanceof FileAttachment) {
406: addAttachFileInDB((FileAttachment) attach);
407: } else {
408: addAttachUrlInDB((UrlAttachment) attach);
409: }
410: }
411:
412: void addAttachFileInDB(FileAttachment file) throws Exception {
413: if (!isInBase()) {
414: throw new Exception("Action " + name + " is not in BDD");
415: }
416: File f = file.getLocalFile();
417: int id = pISQLAction.addFileAttach(idBdd,
418: new SalomeFileWrapper(f), file
419: .getDescriptionFromModel());
420: file.setIdBdd(id);
421: }
422:
423: void addAttachUrlInDB(UrlAttachment url) throws Exception {
424: if (!isInBase()) {
425: throw new Exception("Action " + name + " is not in BDD");
426: }
427: int id = pISQLAction.addUrlAttach(idBdd,
428: url.getNameFromModel(), url.getDescriptionFromModel());
429: url.setIdBdd(id);
430: }
431:
432: public void deleteAttachementInDB(int attachId) throws Exception {
433: if (!isInBase()) {
434: throw new Exception("Action " + name + " is not in BDD");
435: }
436: pISQLAction.deleteAttachment(idBdd, attachId);
437: }
438:
439: public void deleteAttachementInDBAndModel(Attachment attach)
440: throws Exception {
441: deleteAttachementInDB(attach.getIdBdd());
442: deleteAttachmentInModel(attach);
443:
444: }
445:
446: public Vector getAttachFilesFromDB() throws Exception {
447: if (!isInBase()) {
448: throw new Exception("Action " + name + " is not in BDD");
449: }
450: FileAttachementWrapper[] fawArray = pISQLAction
451: .getAllAttachFile(idBdd);
452: Vector result = new Vector();
453: for (int i = 0; i < fawArray.length; i++) {
454: result.add(fawArray[i]);
455: }
456: return result;
457: }
458:
459: public Vector getAttachUrlsFromDB() throws Exception {
460: if (!isInBase()) {
461: throw new Exception("Action " + name + " is not in BDD");
462: }
463: UrlAttachementWrapper[] uawArray = pISQLAction
464: .getAllAttachUrl(idBdd);
465: Vector result = new Vector();
466: for (int i = 0; i < uawArray.length; i++) {
467: result.add(uawArray[i]);
468: }
469: return result;
470: }
471:
472: //////// PROTECTED //////////////
473: protected String clearStringOfParameter(String prtString,
474: Parameter pParam) {
475: String result = prtString;
476: result = result.replaceAll("[$]" + pParam.getNameFromModel()
477: + "[$]", "");
478: return result;
479: }
480:
481: public static boolean isInBase(Test pTest, String actionName) {
482: try {
483: int id = pISQLAction.getID(pTest.getIdBdd(), actionName);
484: if (id > 0) {
485: return true;
486: }
487: return false;
488: } catch (Exception e) {
489:
490: }
491: return false;
492: } // Fin de la méthode isInBase/1
493:
494: public boolean existeInBase() throws Exception {
495: if (!isInBase()) {
496: return false;
497: }
498: return pISQLAction.getID(pTest.getIdBdd(), name) == idBdd;
499: }
500: }
|