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.Enumeration;
028: import java.util.Hashtable;
029: import java.util.Iterator;
030: import java.util.Set;
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.EnvironmentWrapper;
036: import org.objectweb.salome_tmf.api.data.FileAttachementWrapper;
037: import org.objectweb.salome_tmf.api.data.SalomeFileWrapper;
038: import org.objectweb.salome_tmf.api.data.ScriptWrapper;
039: import org.objectweb.salome_tmf.api.data.UrlAttachementWrapper;
040: import org.objectweb.salome_tmf.api.data.ValuedParameterWrapper;
041: import org.objectweb.salome_tmf.api.sql.ISQLConfig;
042: import org.objectweb.salome_tmf.api.sql.ISQLEnvironment;
043: import org.objectweb.salome_tmf.ihm.languages.Language;
044: import org.objectweb.salome_tmf.ihm.main.datawrapper.DataModel;
045:
046: public class Environment extends WithAttachment {
047: static ISQLEnvironment pISQLEnvironment = null;
048: static ISQLConfig pISQLConfig = null;
049:
050: protected Script initScript;
051: protected Hashtable parametersHashTable;
052: protected Project pProject;
053:
054: public Environment(String name, String description) {
055: super (name, description);
056: initScript = null;
057: parametersHashTable = new Hashtable();
058: pProject = null;
059: if (pISQLEnvironment == null) {
060: pISQLEnvironment = Api.getISQLObjectFactory()
061: .getISQLEnvironment();
062: }
063: if (pISQLConfig == null) {
064: pISQLConfig = Api.getISQLObjectFactory().getISQLConfig();
065: }
066: }
067:
068: public Environment(EnvironmentWrapper pEnv) {
069: super (pEnv.getName(), pEnv.getDescription());
070: idBdd = pEnv.getIdBDD();
071: initScript = null;
072: parametersHashTable = new Hashtable();
073: pProject = null;
074: if (pISQLEnvironment == null) {
075: pISQLEnvironment = Api.getISQLObjectFactory()
076: .getISQLEnvironment();
077: }
078: if (pISQLConfig == null) {
079: pISQLConfig = Api.getISQLObjectFactory().getISQLConfig();
080: }
081: }
082:
083: public void reloadFromDB(boolean base, Hashtable paramInModel)
084: throws Exception {
085: if (base) {
086: reloadBaseFromDB();
087: }
088: reloadScriptFromDB();
089: reloadDefParameterFromDB(paramInModel);
090: reloadAttachmentDataFromDB(false);
091: }
092:
093: public void reloadBaseFromDB() throws Exception {
094: if (!isInBase()) {
095: throw new Exception("Environment " + name
096: + " is not in BDD");
097: }
098: EnvironmentWrapper pEnv = pISQLEnvironment.getWrapper(idBdd);
099: name = pEnv.getName();
100: description = pEnv.getDescription();
101: }
102:
103: /***************************** Basic Operation *********************************/
104:
105: void addInDB(int idProject) throws Exception {
106: if (isInBase()) {
107: throw new Exception("Environment " + name
108: + " is already in BDD");
109: }
110: idBdd = pISQLEnvironment.insert(idProject, name, description);
111: Project.pCurrentProject.notifyChanged(
112: ApiConstants.INSERT_ENVIRONMENT, this );
113: }
114:
115: void addInModel(Project pProject) {
116: this .pProject = pProject;
117: //pProject.addEnvironmentInModel(this); //NON
118: }
119:
120: void addInBddAndModel(Project pProject) throws Exception {
121: addInDB(pProject.getIdBdd());
122: addInModel(pProject);
123: }
124:
125: public void updateInDB(String newName, String newDesc)
126: throws Exception {
127: if (!isInBase()) {
128: throw new Exception("Environment " + name
129: + " is not in BDD");
130: }
131: pISQLEnvironment.update(idBdd, newName, newDesc);
132: //System.out.println("[ENVIRONMENT-DB] old Name = " + name);
133: //System.out.println("[ENVIRONMENT-DB] new Name = " + newName);
134: Project.pCurrentProject.notifyChanged(
135: ApiConstants.UPDATE_ENVIRONMENT, this ,
136: new String(name), newName);
137: }
138:
139: public void updateInModel(String newName, String newDesc) {
140: name = newName;
141: description = newDesc;
142: }
143:
144: public void updateInDBAndModel(String newName, String newDesc)
145: throws Exception {
146: updateInDB(newName, newDesc);
147: updateInModel(newName, newDesc);
148: }
149:
150: public void deleteInDB() throws Exception {
151: if (!isInBase()) {
152: throw new Exception("Environment " + name
153: + " is not in BDD");
154: }
155: pISQLEnvironment.delete(idBdd);
156: Project.pCurrentProject.notifyChanged(
157: ApiConstants.DELETE_ENVIRONMENT, this );
158:
159: // Corresponding ICAL values
160: if (pISQLConfig != null) {
161: String key = "Mantis_"
162: + Language.getInstance().getText("ICAL") + "_"
163: + idBdd;
164: pISQLConfig.deleteProjectConf(key, DataModel
165: .getCurrentProject().getIdBdd());
166:
167: // Corresponding QS Scoring values
168: key = "Mantis_QsScore_" + idBdd;
169: pISQLConfig.deleteProjectConf(key, DataModel
170: .getCurrentProject().getIdBdd());
171: }
172:
173: }
174:
175: public void deleteInModel() {
176: //pProject.removeEnvironmentInModel(name);
177: pProject = null;
178: parametersHashTable.clear();
179: clearAttachInModel();
180: }
181:
182: public void deleteInDBAndModel() throws Exception {
183: deleteInDB();
184: deleteInModel();
185: }
186:
187: /**************************** Script *************************************/
188:
189: public void reloadScriptFromDB() throws Exception {
190: if (!isInBase()) {
191: throw new Exception("Environment " + name
192: + " is not in BDD");
193: }
194: ScriptWrapper script = getInitScriptFromDB();
195: if (script != null) {
196: addScriptInModel(new Script(script));
197: }
198: }
199:
200: void addScriptInDB(Script script, File file) throws Exception {
201: if (!isInBase()) {
202: throw new Exception("Environment " + name
203: + " is not in BDD");
204: }
205:
206: int id = pISQLEnvironment.addScript(idBdd,
207: new SalomeFileWrapper(file), script
208: .getDescriptionFromModel(), script
209: .getNameFromModel(), script
210: .getScriptExtensionFromModel(), script
211: .getPlugArgFromModel());
212: script.setIdBdd(id);
213: }
214:
215: public void addScriptInModel(Script script) {
216: initScript = script;
217: }
218:
219: public void addScriptInDBAndModel(Script script, File file)
220: throws Exception {
221: addScriptInDB(script, file);
222: addScriptInModel(script);
223: }
224:
225: public void deleteScriptInDB() throws Exception {
226: if (!isInBase()) {
227: throw new Exception("Environment " + name
228: + " is not in BDD");
229: }
230: pISQLEnvironment.deleteScript(idBdd);
231: }
232:
233: public void deleteScriptInModel() {
234: initScript = null;
235: }
236:
237: public void deleteScriptInDBAndModel() throws Exception {
238: deleteScriptInDB();
239: deleteScriptInModel();
240: }
241:
242: public File getEnvScriptFromDB() throws Exception {
243: if (!isInBase()) {
244: throw new Exception("Environment " + name
245: + " is not in BDD");
246: }
247: return pISQLEnvironment.getScript(idBdd).toFile();
248: }
249:
250: public Script getInitScriptFromModel() {
251: return initScript;
252: }
253:
254: public File getInitScriptFileFromDB() throws Exception {
255: if (!isInBase()) {
256: throw new Exception("Environment " + name
257: + " is not in BDD");
258: }
259: return pISQLEnvironment.getScript(idBdd).toFile();
260: }
261:
262: public ScriptWrapper getInitScriptFromDB() throws Exception {
263: if (!isInBase()) {
264: throw new Exception("Environment " + name
265: + " is not in BDD");
266: }
267: return pISQLEnvironment.getScriptWrapper(idBdd);
268: }
269:
270: /************************** Parameters ***************************/
271: /**
272: * paramInModel can be null if no object mapping with project params is need
273: */
274: public void reloadDefParameterFromDB(Hashtable paramInModel)
275: throws Exception {
276: if (!isInBase()) {
277: throw new Exception("Environment " + name
278: + " is not in BDD");
279: }
280: parametersHashTable.clear();
281: ValuedParameterWrapper[] definedParams = pISQLEnvironment
282: .getDefinedParameters(idBdd);
283: for (int i = 0; i < definedParams.length; i++) {
284: Parameter param = null;
285: ValuedParameterWrapper pValuedParameterWrapper = definedParams[i];
286: if (paramInModel == null) {
287: param = new Parameter(pValuedParameterWrapper
288: .getParameterWrapper());
289: } else {
290: param = (Parameter) paramInModel
291: .get(pValuedParameterWrapper
292: .getParameterWrapper().getName());
293: if (param == null) {
294: param = new Parameter(pValuedParameterWrapper
295: .getParameterWrapper());
296: paramInModel.put(pValuedParameterWrapper
297: .getParameterWrapper().getName(), param);
298: }
299:
300: }
301: addParameterValueInModel(param, pValuedParameterWrapper
302: .getValue());
303: }
304: }
305:
306: public void setParametersHashTableInModel(Hashtable hashtable) {
307: parametersHashTable = hashtable;
308: }
309:
310: public void addParameterValueInModel(Parameter param, String value) {
311: parametersHashTable.put(param, value);
312: }
313:
314: public void addParameterValueInDBModel(Parameter param, String value)
315: throws Exception {
316: addParameterValueInDB(param, value);
317: addParameterValueInModel(param, value);
318: }
319:
320: public void addParameterValueInDB(Parameter param, String value)
321: throws Exception {
322: if (!isInBase()) {
323: throw new Exception("Environment " + name
324: + " is not in BDD");
325: }
326: pISQLEnvironment.addParamValue(idBdd, param.getIdBdd(), value,
327: param.getDescriptionFromModel());
328: }
329:
330: public void addParamValueInDBAndModel(Parameter param, String value)
331: throws Exception {
332: addParameterValueInDB(param, value);
333: addParameterValueInModel(param, value);
334: }
335:
336: public void updateParamValueInModel(Parameter param, String value) {
337: parametersHashTable.remove(param);
338: addParameterValueInModel(param, value);
339: }
340:
341: public void updateParamValueInDB(Parameter param, String value)
342: throws Exception {
343: if (!isInBase()) {
344: throw new Exception("Environment " + name
345: + " is not in BDD");
346: }
347: pISQLEnvironment.updateParamValue(idBdd, param.getIdBdd(),
348: value, param.getDescriptionFromModel());
349: }
350:
351: public void updateParamValueInDBAndModel(Parameter param,
352: String value) throws Exception {
353: updateParamValueInDB(param, value);
354:
355: updateParamValueInModel(param, value);
356: }
357:
358: public void deleteDefParameterInModel(Parameter param) {
359: parametersHashTable.remove(param);
360: }
361:
362: public void deleteDefParameterInModel(String paramName) {
363: Set keysSet = parametersHashTable.keySet();
364: for (Iterator iter = keysSet.iterator(); iter.hasNext();) {
365: Parameter element = (Parameter) iter.next();
366: if (element.getNameFromModel().equals(paramName)) {
367: parametersHashTable.remove(element);
368: return;
369: }
370: }
371: }
372:
373: public void deleteDefParamInDB(int paramId) throws Exception {
374: if (!isInBase()) {
375: throw new Exception("Environment " + name
376: + " is not in BDD");
377: }
378: pISQLEnvironment.deleteDefParam(idBdd, paramId);
379: }
380:
381: public void deleteDefParamInDBAndModel(Parameter param)
382: throws Exception {
383: deleteDefParamInDB(param.getIdBdd());
384: deleteDefParameterInModel(param);
385: }
386:
387: public Parameter getParameterFromModel(String paramName) {
388: Set keysSet = parametersHashTable.keySet();
389: for (Iterator iter = keysSet.iterator(); iter.hasNext();) {
390: Parameter element = (Parameter) iter.next();
391: if (element.getNameFromModel().equals(paramName)) {
392: return element;
393: }
394: }
395: return null;
396: }
397:
398: public String getParameterValue(String paramName) {
399: Set keysSet = parametersHashTable.keySet();
400: for (Iterator iter = keysSet.iterator(); iter.hasNext();) {
401: Parameter element = (Parameter) iter.next();
402: if (element.getNameFromModel().equals(paramName)) {
403: return (String) parametersHashTable.get(element);
404: }
405: }
406: return null;
407: }
408:
409: public String getParameterValueFromModel(Parameter param) {
410: return (String) parametersHashTable.get(param);
411: }
412:
413: public Hashtable getCopyOfParameterHashTableFromModel() {
414: Hashtable copyParameter = new Hashtable();
415: Enumeration enumKey = parametersHashTable.keys();
416: while (enumKey.hasMoreElements()) {
417: Object key = enumKey.nextElement();
418: copyParameter.put(key, parametersHashTable.get(key));
419: }
420: return copyParameter;
421: }
422:
423: public Hashtable getParametersHashTableFromModel() {
424: return parametersHashTable;
425: }
426:
427: public boolean containsParameterInModel(Parameter param) {
428: return parametersHashTable.containsKey(param);
429: }
430:
431: public boolean containsParameterInModel(String paramName) {
432: Set keysSet = parametersHashTable.keySet();
433: for (Iterator iter = keysSet.iterator(); iter.hasNext();) {
434: Parameter element = (Parameter) iter.next();
435: if (element.getNameFromModel().equals(paramName)) {
436: return true;
437: }
438: }
439: return false;
440: }
441:
442: public Vector getValuedParametersWrapperFromDB() throws Exception {
443: if (!isInBase()) {
444: throw new Exception("Environment " + name
445: + " is not in BDD");
446: }
447: ValuedParameterWrapper[] tmpArray = pISQLEnvironment
448: .getDefinedParameters(idBdd);
449: Vector tmpVector = new Vector();
450: for (int tmpI = 0; tmpI < tmpArray.length; tmpI++) {
451: tmpVector.add(tmpArray[tmpI]);
452: }
453: return tmpVector;
454: }
455:
456: /************************** Attachements ***************************/
457: public void addAttachementInDB(Attachment attach) throws Exception {
458: if (attach instanceof FileAttachment) {
459: addAttachFileInDB((FileAttachment) attach);
460: } else {
461: addAttachUrlInDB((UrlAttachment) attach);
462: }
463: }
464:
465: void addAttachFileInDB(FileAttachment file) throws Exception {
466: if (!isInBase()) {
467: throw new Exception("Environment " + name
468: + " is not in BDD");
469: }
470: File f = file.getLocalFile();
471: int id = pISQLEnvironment.addAttachFile(idBdd,
472: new SalomeFileWrapper(f), file
473: .getDescriptionFromModel());
474: file.setIdBdd(id);
475: }
476:
477: void addAttachUrlInDB(UrlAttachment url) throws Exception {
478: if (!isInBase()) {
479: throw new Exception("Environment " + name
480: + " is not in BDD");
481: }
482: int id = pISQLEnvironment.addAttachUrl(idBdd, url
483: .getNameFromModel(), url.getDescriptionFromModel());
484: url.setIdBdd(id);
485: }
486:
487: public void addAttachInDBAndModel(Attachment attach)
488: throws Exception {
489: addAttachementInDB(attach);
490: addAttachementInModel(attach);
491: }
492:
493: public void deleteAttachementInDB(int attachId) throws Exception {
494: if (!isInBase()) {
495: throw new Exception("Environment " + name
496: + " is not in BDD");
497: }
498: pISQLEnvironment.deleteAttach(idBdd, attachId);
499: }
500:
501: public void deleteAttachementInDBAndModel(Attachment attach)
502: throws Exception {
503: deleteAttachementInDB(attach.getIdBdd());
504: deleteAttachmentInModel(attach);
505: }
506:
507: /**
508: * A vector of FileAttachementWrapper
509: */
510: public Vector getAttachFilesFromDB() throws Exception {
511: if (!isInBase()) {
512: throw new Exception("Environment " + name
513: + " is not in BDD");
514: }
515: FileAttachementWrapper[] tmpArray = pISQLEnvironment
516: .getAttachFiles(idBdd);
517: Vector tmpVector = new Vector();
518: for (int tmpI = 0; tmpI < tmpArray.length; tmpI++) {
519: tmpVector.add(tmpArray[tmpI]);
520: }
521: return tmpVector;
522: }
523:
524: public Vector getAttachFilesNameFromDB() throws Exception {
525: if (!isInBase()) {
526: throw new Exception("Environment " + name
527: + " is not in BDD");
528: }
529: Vector res = new Vector();
530: FileAttachementWrapper[] listfilesWrapper = pISQLEnvironment
531: .getAttachFiles(idBdd);
532: for (int i = 0; i < listfilesWrapper.length; i++) {
533: res.add(listfilesWrapper[i].getName());
534: }
535: return res;
536: }
537:
538: /**
539: * A vector of UrlAttachementWrapper
540: */
541: public Vector getAttachUrlsFromDB() throws Exception {
542: if (!isInBase()) {
543: throw new Exception("Environment " + name
544: + " is not in BDD");
545: }
546: UrlAttachementWrapper[] tmpArray = pISQLEnvironment
547: .getAttachUrls(idBdd);
548: Vector tmpVector = new Vector();
549: for (int tmpI = 0; tmpI < tmpArray.length; tmpI++) {
550: tmpVector.add(tmpArray[tmpI]);
551: }
552: return tmpVector;
553: }
554:
555: public Vector getAttachUrlsNameFromDB() throws Exception {
556: if (!isInBase()) {
557: throw new Exception("Environment " + name
558: + " is not in BDD");
559: }
560: Vector res = new Vector();
561: UrlAttachementWrapper[] listUrlsWrapper = pISQLEnvironment
562: .getAttachUrls(idBdd);
563: for (int i = 0; i < listUrlsWrapper.length; i++) {
564: res.add(listUrlsWrapper[i].getName());
565: }
566: return res;
567: }
568:
569: public static boolean isInBase(Project pProject, String envName) {
570: try {
571: int id = pISQLEnvironment.getID(pProject.getIdBdd(),
572: envName);
573: if (id > 0) {
574: return true;
575: }
576: return false;
577: } catch (Exception e) {
578:
579: }
580: return false;
581: } // Fin de la méthode isInBase/1
582:
583: public boolean existeInBase() throws Exception {
584: if (!isInBase()) {
585: return false;
586: }
587: return pISQLEnvironment.getID(pProject.getIdBdd(), name) == idBdd;
588: }
589: }
|