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.io.BufferedInputStream;
027: import java.io.File;
028: import java.io.FileInputStream;
029: import java.sql.Date;
030: import java.sql.PreparedStatement;
031: import java.sql.ResultSet;
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.SalomeFileWrapper;
037: import org.objectweb.salome_tmf.api.sql.ISQLScript;
038:
039: public class SQLScript implements ISQLScript {
040:
041: /**
042: * Insert a new script in the table SCRIPT (No attachment are mapped)
043: * @param name : the name of the script
044: * @param arg1 : argument 1 of the script (free use for plug-in)
045: * @param extension : argument 2 of the script (plugin extension)
046: * @param type : type of the script
047: * (ApiConstants.TEST_SCRIPT, ApiConstants.INIT_SCRIPT, ApiConstants.PRE_SCRIPT, ApiConstants.POST_SCRIPT)
048: * @return the id of the new Script
049: * @throws Exception
050: */
051: public int insert(String name, String arg1, String extension,
052: String type) throws Exception {
053: int idScript = -1;
054: int transNumber = -1;
055: try {
056: transNumber = SQLEngine.beginTransaction(0,
057: ApiConstants.INSERT_SCRIPT);
058:
059: PreparedStatement prep = SQLEngine
060: .getSQLAddQuery("addScriptToTest");
061: prep.setString(1, name);
062: prep.setString(2, arg1);
063: prep.setString(3, extension);
064: prep.setString(4, type);
065: SQLEngine.runAddQuery(prep);
066: idScript = getLastIdAttach();
067:
068: SQLEngine.commitTrans(transNumber);
069: } catch (Exception e) {
070: Util.log("[SQLScript->insert]" + e);
071: if (Api.isDEBUG()) {
072: e.printStackTrace();
073: }
074: SQLEngine.rollBackTrans(transNumber);
075: throw e;
076: }
077:
078: return idScript;
079: }
080:
081: /**
082: * Insert a new script in the table SCRIPT (No attachment are mapped) for an Environnement
083: * @param name : the name of the script
084: * @param arg1 : argument 1 of the script (free use for plug-in)
085: * @param extension : argument 2 of the script (plugin extension)
086: * @param type : type of the script
087: * @param idEnv : id of the Environnement mapped with the script
088: * (ApiConstants.TEST_SCRIPT, ApiConstants.INIT_SCRIPT, ApiConstants.PRE_SCRIPT, ApiConstants.POST_SCRIPT)
089: * @return the id of the new Script
090: * @throws Exception
091: */
092: public int insert(String name, String arg1, String extension,
093: String type, int idEnv) throws Exception {
094: int idScript = -1;
095: int transNumber = -1;
096: try {
097: transNumber = SQLEngine.beginTransaction(0,
098: ApiConstants.INSERT_SCRIPT);
099:
100: PreparedStatement prep = SQLEngine
101: .getSQLAddQuery("attachScriptToEnvironment"); //ok
102: prep.setString(1, name);
103: prep.setString(2, arg1);
104: prep.setString(3, extension);
105: prep.setString(4, type);
106: prep.setInt(5, idEnv);
107: SQLEngine.runAddQuery(prep);
108: idScript = getLastIdAttach();
109:
110: SQLEngine.commitTrans(transNumber);
111: } catch (Exception e) {
112: Util.log("[SQLScript->insert]" + e);
113: if (Api.isDEBUG()) {
114: e.printStackTrace();
115: }
116: SQLEngine.rollBackTrans(transNumber);
117: throw e;
118: }
119:
120: return idScript;
121: }
122:
123: /**
124: * Insert a new script in the table SCRIPT (No attachment are mapped) for an EXECUTION
125: * @param name : the name of the script
126: * @param arg1 : argument 1 of the script (free use for plug-in)
127: * @param extension : argument 2 of the script (plugin extension)
128: * @param type : type of the script
129: * @param idEnv : id of the Environnement mapped with the script
130: * (ApiConstants.TEST_SCRIPT, ApiConstants.INIT_SCRIPT, ApiConstants.PRE_SCRIPT, ApiConstants.POST_SCRIPT)
131: * @return the id of the new Script
132: * @throws Exception
133: */
134: public int insert(int idExec, String name, String arg1,
135: String extension, String type) throws Exception {
136: if (idExec < 1) {
137: throw new Exception(
138: "[SQLScript->insert] entry data are not valid");
139: }
140: int idScript = -1;
141: int transNumber = -1;
142: try {
143: transNumber = SQLEngine.beginTransaction(0,
144: ApiConstants.INSERT_SCRIPT);
145:
146: PreparedStatement prep = SQLEngine
147: .getSQLAddQuery("attachScriptToExecution"); //ok
148: prep.setString(1, name);
149: prep.setString(2, arg1);
150: prep.setString(3, extension);
151: prep.setString(4, type);
152: prep.setInt(5, idExec);
153:
154: SQLEngine.runAddQuery(prep);
155: idScript = getLastIdAttach();
156: if (idScript < 1) {
157: throw new Exception(
158: "[SQLScript->insert] id is not valid");
159: }
160:
161: SQLEngine.commitTrans(transNumber);
162: } catch (Exception e) {
163: Util.log("[SQLScript->insert]" + e);
164: if (Api.isDEBUG()) {
165: e.printStackTrace();
166: }
167: SQLEngine.rollBackTrans(transNumber);
168: throw e;
169: }
170:
171: return idScript;
172: }
173:
174: /**
175: * Map the attachment attachId to the script scriptId in the table SCRIPT_ATTACHEMENT
176: * @param scriptId
177: * @param attachId
178: * @throws Exception
179: */
180: public void addAttach(int scriptId, int attachId) throws Exception {
181: if (scriptId < 1 || attachId < 1) {
182: throw new Exception(
183: "[SQLScript->addAttach] entry data are not valid");
184: }
185: int transNumber = -1;
186: try {
187: transNumber = SQLEngine.beginTransaction(0,
188: ApiConstants.INSERT_ATTACHMENT);
189:
190: PreparedStatement prep = SQLEngine
191: .getSQLAddQuery("addFileAttachToScript"); //ok
192: prep.setInt(1, scriptId);
193: prep.setInt(2, attachId);
194: SQLEngine.runAddQuery(prep);
195:
196: SQLEngine.commitTrans(transNumber);
197: } catch (Exception e) {
198: Util.log("[SQLScript->addAttach]" + e);
199: if (Api.isDEBUG()) {
200: e.printStackTrace();
201: }
202: SQLEngine.rollBackTrans(transNumber);
203: throw e;
204: }
205: }
206:
207: /**
208: * Update the length of the script idScript in the database
209: * @param idScript
210: * @param length
211: * @throws Exception
212: * no permission needed
213: */
214: public void updateLength(int idScript, long length)
215: throws Exception {
216: if (idScript < 1) {
217: throw new Exception(
218: "[SQLScript->updateLength] entry data are not valid");
219: }
220: int transNumber = -1;
221: try {
222: transNumber = SQLEngine.beginTransaction(0,
223: ApiConstants.UPDATE_ATTACHMENT);
224: int idAttach = getScriptIdAttach(idScript);
225: if (idAttach != -1) {
226: SQLObjectFactory.getInstanceOfISQLFileAttachment()
227: .updateFileLength(idAttach, length);
228: }
229:
230: SQLEngine.commitTrans(transNumber);
231: } catch (Exception e) {
232: Util.log("[SQLScript->updateLength]" + e);
233: if (Api.isDEBUG()) {
234: e.printStackTrace();
235: }
236: SQLEngine.rollBackTrans(transNumber);
237: throw e;
238: }
239: }
240:
241: /**
242: * Update the date of the script idScript in the database
243: * @param idScript
244: * @param date
245: * @throws Exception
246: * no permission needed
247: */
248: public void updateDate(int idScript, Date date) throws Exception {
249: if (idScript < 1) {
250: throw new Exception(
251: "[SQLScript->updateDate] entry data are not valid");
252: }
253: int transNumber = -1;
254: try {
255: transNumber = SQLEngine.beginTransaction(0,
256: ApiConstants.UPDATE_ATTACHMENT);
257:
258: int idAttach = getScriptIdAttach(idScript);
259: if (idAttach != -1) {
260: SQLObjectFactory.getInstanceOfISQLFileAttachment()
261: .updateFileDate(idAttach, date);
262: }
263:
264: SQLEngine.commitTrans(transNumber);
265: } catch (Exception e) {
266: Util.log("[SQLScript->updateDate]" + e);
267: if (Api.isDEBUG()) {
268: e.printStackTrace();
269: }
270: SQLEngine.rollBackTrans(transNumber);
271: throw e;
272: }
273: }
274:
275: /**
276: * Update the argument reserved for plugin in the script idScript in the database (classe_autom_script)
277: * @param idScript
278: * @param arg
279: * @throws Exception
280: * no permission needed
281: */
282: public void updatePlugArg(int idScript, String arg)
283: throws Exception {
284: if (idScript < 1) {
285: throw new Exception(
286: "[SQLScript->updatePlugArg] entry data are not valid");
287: }
288: int transNumber = -1;
289: try {
290: transNumber = SQLEngine.beginTransaction(0,
291: ApiConstants.UPDATE_ATTACHMENT);
292:
293: PreparedStatement prep = SQLEngine
294: .getSQLUpdateQuery("updateScriptClassToBeExecuted");
295: prep.setString(1, arg);
296: prep.setInt(2, idScript);
297: SQLEngine.runUpdateQuery(prep);
298:
299: SQLEngine.commitTrans(transNumber);
300: } catch (Exception e) {
301: Util.log("[SQLScript->updatePlugArg]" + e);
302: if (Api.isDEBUG()) {
303: e.printStackTrace();
304: }
305: SQLEngine.rollBackTrans(transNumber);
306: throw e;
307: }
308: }
309:
310: /**
311: * Update the conetent of the script id with the content of the file with path filePath
312: * @param idScript
313: * @param filePath
314: * @throws Exception
315: * @see ISQLFileAttachment.updateFileContent(int,BufferedInputStream);
316: * no permission needed
317: */
318: public void updateContent(int idScript, String filePath)
319: throws Exception {
320: if (idScript < 1) {
321: throw new Exception(
322: "[SQLScript->updateContent] entry data are not valid");
323: }
324: int transNumber = -1;
325: BufferedInputStream bis = null;
326: try {
327: transNumber = SQLEngine.beginTransaction(0,
328: ApiConstants.UPDATE_ATTACHMENT);
329:
330: File file = new File(filePath);
331: FileInputStream fis = new FileInputStream(file);
332: bis = new BufferedInputStream(fis);
333: byte[] fileContent = null;
334: bis.read(fileContent);
335:
336: int scriptAttachId = getScriptIdAttach(idScript);
337: SQLObjectFactory.getInstanceOfISQLFileAttachment()
338: .updateFileContent(scriptAttachId, fileContent);
339: bis.close();
340:
341: SQLEngine.commitTrans(transNumber);
342: } catch (Exception e) {
343: Util.log("[SQLScript->updateContent]" + e);
344: if (Api.isDEBUG()) {
345: e.printStackTrace();
346: }
347: SQLEngine.rollBackTrans(transNumber);
348: if (bis != null) {
349: bis.close();
350: }
351: throw e;
352: }
353: }
354:
355: /**
356: * Update the script attachement (content, date size, and name) In Database
357: * @param idScript
358: * @param filePath
359: * @throws Exception
360: * @see ISQLFileAttachment.updateFile(int, File);
361: * no permission needed
362: */
363: public void update(int idScript, String filePath) throws Exception {
364: update(idScript, new SalomeFileWrapper(new File(filePath)));
365: }
366:
367: /**
368: * Update the script attachement (content, date size, and name) In Database
369: * @param idScript
370: * @param File
371: * @throws Exception
372: * @see ISQLFileAttachment.updateFile(int, File);
373: * no permission needed
374: */
375: public void update(int idScript, SalomeFileWrapper file)
376: throws Exception {
377: if (idScript < 1) {
378: throw new Exception(
379: "[SQLScript->update] entry data are not valid");
380: }
381: if (file == null || file.exists() == false) {
382: throw new Exception(
383: "[SQLScript->update] entry data are not valid");
384: }
385: int transNumber = -1;
386: try {
387: transNumber = SQLEngine.beginTransaction(0,
388: ApiConstants.UPDATE_ATTACHMENT);
389: int scriptAttachId = getScriptIdAttach(idScript);
390: SQLObjectFactory.getInstanceOfISQLFileAttachment()
391: .updateFile(scriptAttachId, file);
392:
393: PreparedStatement prep = SQLEngine
394: .getSQLUpdateQuery("updateScriptName"); //ok
395: prep.setString(1, file.getName());
396: prep.setInt(2, idScript);
397: SQLEngine.runUpdateQuery(prep);
398:
399: SQLEngine.commitTrans(transNumber);
400:
401: } catch (Exception e) {
402: Util.log("[SQLScript->update]" + e);
403: if (Api.isDEBUG()) {
404: e.printStackTrace();
405: }
406: SQLEngine.rollBackTrans(transNumber);
407: throw e;
408: }
409: }
410:
411: /**
412: * Update the Script name the table SCRIPT and ATTACHEMENT
413: * @param idScript
414: * @param name
415: * @throws Exception
416: * @see ISQLFileAttachment.updateFileName(int, String);
417: * no permission needed
418: */
419: public void updateName(int idScript, String name) throws Exception {
420: if (idScript < 1) {
421: throw new Exception(
422: "[SQLScript->updateName] entry data are not valid");
423: }
424: int transNumber = -1;
425: try {
426: transNumber = SQLEngine.beginTransaction(0,
427: ApiConstants.UPDATE_ATTACHMENT);
428:
429: int scriptAttachId = getScriptIdAttach(idScript);
430: SQLObjectFactory.getInstanceOfISQLFileAttachment()
431: .updateFileName(scriptAttachId, name);
432:
433: PreparedStatement prep = SQLEngine
434: .getSQLUpdateQuery("updateScriptName"); //ok
435: prep.setString(1, name);
436: prep.setInt(2, idScript);
437: SQLEngine.runUpdateQuery(prep);
438:
439: SQLEngine.commitTrans(transNumber);
440: } catch (Exception e) {
441: Util.log("[SQLScript->updateName]" + e);
442: if (Api.isDEBUG()) {
443: e.printStackTrace();
444: }
445: SQLEngine.rollBackTrans(transNumber);
446: throw e;
447: }
448: }
449:
450: /**
451: * Delete the script and mapped file in the tables ( SCRIPT, SCRIPT_ATTACHEMENT, ATTACHEMENT)
452: * @param idScript
453: * @param idAttach
454: * @throws Exception
455: * @see ISQLAttachment().delete(int)
456: */
457: public void delete(int idScript) throws Exception {
458: int transNumber = -1;
459: if (idScript < 1) {
460: throw new Exception(
461: "[SQLScript->delete] entry data are not valid");
462: }
463: try {
464: transNumber = SQLEngine.beginTransaction(0,
465: ApiConstants.DELETE_SCRIPT);
466:
467: int idAttach = getScriptIdAttach(idScript);
468:
469: PreparedStatement prep = SQLEngine
470: .getSQLDeleteQuery("deleteAttachFromScript"); //ok
471: prep.setInt(1, idScript);
472: prep.setInt(2, idAttach);
473: SQLEngine.runDeleteQuery(prep);
474:
475: prep = SQLEngine.getSQLDeleteQuery("deleteScriptFromDB");
476: prep.setInt(1, idScript);
477: SQLEngine.runDeleteQuery(prep);
478:
479: SQLObjectFactory.getInstanceOfISQLAttachment().delete(
480: idAttach);
481:
482: SQLEngine.commitTrans(transNumber);
483: } catch (Exception e) {
484: Util.log("[SQLScript->delete]" + e);
485: if (Api.isDEBUG()) {
486: e.printStackTrace();
487: }
488: SQLEngine.rollBackTrans(transNumber);
489: throw e;
490: }
491: }
492:
493: /**
494: * @return the last id of an attachment in the table SCRIPT_ATTACHEMENT
495: * @throws Exception
496: */
497: public int getLastIdAttach() throws Exception {
498: int maxIdAttach = -1;
499: PreparedStatement prep = SQLEngine
500: .getSQLSelectQuery("selectMaxIdScript"); //ok
501: ResultSet stmtRes = SQLEngine.runSelectQuery(prep);
502: if (stmtRes.next()) {
503: maxIdAttach = stmtRes.getInt("max_id_script");
504: } else {
505: throw new Exception(
506: "[SQLScript->getLastIdAttach] id not exist");
507: }
508: return maxIdAttach;
509: }
510:
511: /**
512: * @param idScript : id of the script in the database
513: * @return the id of the attachment mapped to the script in the database
514: * @throws Exception
515: */
516: public int getScriptIdAttach(int idScript) throws Exception {
517: if (idScript < 1) {
518: throw new Exception(
519: "[SQLScript->getScriptIdAttach] entry data are not valid");
520: }
521: int fileAttachId = -1;
522: PreparedStatement prep = SQLEngine
523: .getSQLSelectQuery("selectScriptAttachId"); //ok
524: prep.setInt(1, idScript);
525: ResultSet stmtRes = SQLEngine.runSelectQuery(prep);
526: if (stmtRes.next()) {
527: fileAttachId = stmtRes.getInt("ATTACHEMENT_id_attach");
528: }
529: return fileAttachId;
530: }
531:
532: /**
533: * @param idScript : id of the script in the database
534: * @return a File attached to the script
535: * @throws Exception
536: * @see ISQLFileAttachment.getFile(int)
537: */
538: public SalomeFileWrapper getFile(int idScript) throws Exception {
539: if (idScript < 1) {
540: throw new Exception(
541: "[SQLScript->getFile] entry data are not valid");
542: }
543: SalomeFileWrapper f = null;
544: int fileAttachId = getScriptIdAttach(idScript);
545: f = SQLObjectFactory.getInstanceOfISQLFileAttachment().getFile(
546: fileAttachId);
547: return f;
548: }
549:
550: /**
551: * @param idScript : id of the script in the database
552: * @return a File attached to the script
553: * @throws Exception
554: * @see ISQLFileAttachment.getFile(int)
555: */
556: /*public SalomeFileWrapper getFile(int idScript) throws Exception {
557: if (idScript <1) {
558: throw new Exception("[SQLScript->getFile] entry data are not valid");
559: }
560: SalomeFileWrapper f = null;
561: int fileAttachId = getScriptIdAttach(idScript);
562: f = SQLObjectFactory.getInstanceOfISQLFileAttachment().getFile(fileAttachId);
563: return f;
564: }*/
565: }
|