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.BufferedOutputStream;
028: import java.io.File;
029: import java.io.FileInputStream;
030: import java.io.FileOutputStream;
031: import java.io.InputStream;
032: import java.sql.Date;
033: import java.sql.PreparedStatement;
034: import java.sql.ResultSet;
035: import java.util.Properties;
036:
037: import org.objectweb.salome_tmf.api.Api;
038: import org.objectweb.salome_tmf.api.ApiConstants;
039: import org.objectweb.salome_tmf.api.Util;
040: import org.objectweb.salome_tmf.api.data.SalomeFileWrapper;
041: import org.objectweb.salome_tmf.api.sql.ISQLFileAttachment;
042:
043: public class SQLFileAttachment extends SQLAttachment implements
044: ISQLFileAttachment {
045:
046: /**
047: * Insert an attachment as file in the table ATTACHEMENT
048: * @param f
049: * @param description
050: * @throws Exception
051: * @return the id of the new attached file
052: * no permission needed
053: */
054: public int insert(SalomeFileWrapper f, String description)
055: throws Exception {
056: int transNumber = -1;
057: int idAttach = -1;
058: try {
059: transNumber = SQLEngine.beginTransaction(0,
060: ApiConstants.INSERT_ATTACHMENT);
061: // FileInputStream fis = new FileInputStream(f);
062: // BufferedInputStream bis = new BufferedInputStream(fis);
063:
064: PreparedStatement prep = SQLEngine
065: .getSQLAddQuery("insertFileAttachIntoDB"); //ok
066: prep.setString(1, f.getName());
067: prep.setBinaryStream(2, f.openInputStream(), f.getLength());
068: prep.setString(3, description);
069: prep.setLong(4, f.getLength());
070: prep.setDate(5, new Date(f.getLastModified()));
071: SQLEngine.runAddQuery(prep);
072: // bis.close();
073: idAttach = getLastIdAttach();
074: if (idAttach < 1) {
075: throw new Exception(
076: "[SQLFileAttachment : insert -> campaign have no id]");
077: }
078:
079: SQLEngine.commitTrans(transNumber);
080: } catch (Exception e) {
081: Util.log("[SQLFileAttachment->insert]" + e);
082: if (Api.isDEBUG()) {
083: e.printStackTrace();
084: }
085: SQLEngine.rollBackTrans(transNumber);
086: throw e;
087: }
088: return idAttach;
089: }
090:
091: /**
092: * Update the file in database table ATTACHEMENT, this include the update of content, date and length
093: * @param idBdd : id of the file in the table ATTACHEMENT
094: * @param file : the java file representing the new content
095: * @throws Exception
096: * no permission needed
097: */
098: public void updateFile(int idBdd, SalomeFileWrapper file)
099: throws Exception {
100: if (idBdd < 1) {
101: throw new Exception(
102: "[SQLFileAttachment->updateFile] entry data are not valid");
103: }
104: if (file == null || file.exists() == false) {
105: throw new Exception(
106: "[SQLFileAttachment->updateFile] entry data are not valid");
107: }
108: int transNumber = -1;
109: try {
110: transNumber = SQLEngine.beginTransaction(0,
111: ApiConstants.UPDATE_ATTACHMENT);
112:
113: InputStream fis = file.openInputStream();
114: BufferedInputStream bis = new BufferedInputStream(fis);
115:
116: PreparedStatement prep = SQLEngine
117: .getSQLUpdateQuery("updateFileAttach2"); //ok
118: prep.setBinaryStream(1, bis, bis.available());
119: prep.setLong(2, file.getLength());
120: prep.setDate(3, new Date(file.getLastModified()));
121: prep.setString(4, file.getName());
122: prep.setInt(5, idBdd);
123: SQLEngine.runUpdateQuery(prep);
124:
125: bis.close();
126:
127: SQLEngine.commitTrans(transNumber);
128: } catch (Exception e) {
129: Util.log("[SQLFileAttachment->updateFileName]" + e);
130: if (Api.isDEBUG()) {
131: e.printStackTrace();
132: }
133: SQLEngine.rollBackTrans(transNumber);
134: throw e;
135: }
136: /*
137: FileInputStream fis = new FileInputStream(file);
138: BufferedInputStream bis = new BufferedInputStream(fis);
139: updateFileContent(idBdd, bis);
140: updateFileDate(idBdd, new Date(file.lastModified()));
141: updateFileLength(idBdd, file.length());
142: bis.close();
143: */
144: }
145:
146: /**
147: * Update the file name of the attachements identified by idBdd
148: * @param idBdd
149: * @param name : the new name
150: * @throws Exception
151: * no permission needed
152: */
153: public void updateFileName(int idBdd, String name) throws Exception {
154: if (idBdd < 1) {
155: throw new Exception(
156: "[SQLFileAttachment->updateFileName] entry data are not valid");
157: }
158: int transNumber = -1;
159: try {
160: transNumber = SQLEngine.beginTransaction(0,
161: ApiConstants.UPDATE_ATTACHMENT);
162:
163: PreparedStatement prep = SQLEngine
164: .getSQLUpdateQuery("updateFileAttachName"); //ok
165: prep.setString(1, name);
166: prep.setInt(2, idBdd);
167: SQLEngine.runUpdateQuery(prep);
168:
169: SQLEngine.commitTrans(transNumber);
170: } catch (Exception e) {
171: Util.log("[SQLFileAttachment->updateFileName]" + e);
172: if (Api.isDEBUG()) {
173: e.printStackTrace();
174: }
175: SQLEngine.rollBackTrans(transNumber);
176: throw e;
177: }
178: }
179:
180: /**
181: * Change the content of an existing file in the database
182: * @param idBdd : id of the file in the table ATTACHMENT
183: * @param fileContent : the new content
184: * @throws Exception
185: * no permission needed
186: */
187: public void updateFileContent(int idBdd, byte[] fileContent)
188: throws Exception {
189: if (idBdd < 1) {
190: throw new Exception(
191: "[SQLFileAttachment->updateFileContent] entry data are not valid");
192: }
193: int transNumber = -1;
194: try {
195: transNumber = SQLEngine.beginTransaction(0,
196: ApiConstants.UPDATE_ATTACHMENT);
197:
198: PreparedStatement prep = SQLEngine
199: .getSQLUpdateQuery("updateFileAttach"); //ok
200: prep.setBytes(1, fileContent);
201: //prep.setBinaryStream(1,fileContent,fileContent.available());
202: prep.setInt(2, idBdd);
203: SQLEngine.runUpdateQuery(prep);
204:
205: SQLEngine.commitTrans(transNumber);
206: } catch (Exception e) {
207: Util.log("[SQLFileAttachment->updateFileContent]" + e);
208: if (Api.isDEBUG()) {
209: e.printStackTrace();
210: }
211: SQLEngine.rollBackTrans(transNumber);
212: throw e;
213: }
214: }
215:
216: /**
217: * Change the date of the file in the database
218: * @param idBdd : id of the file in the table ATTACHMENT
219: * @param date : the new date
220: * @throws Exception
221: * no permission needed
222: */
223: public void updateFileDate(int idBdd, Date date) throws Exception {
224: if (idBdd < 1) {
225: throw new Exception(
226: "[SQLFileAttachment->updateFileDate] entry data are not valid");
227: }
228: int transNumber = -1;
229: try {
230: transNumber = SQLEngine.beginTransaction(0,
231: ApiConstants.UPDATE_ATTACHMENT);
232:
233: PreparedStatement prep = SQLEngine
234: .getSQLUpdateQuery("updateFileAttachDate"); //ok
235: prep.setDate(1, date);
236: prep.setInt(2, idBdd);
237: SQLEngine.runUpdateQuery(prep);
238:
239: SQLEngine.commitTrans(transNumber);
240: } catch (Exception e) {
241: Util.log("[SQLFileAttachment->updateFileDate]" + e);
242: if (Api.isDEBUG()) {
243: e.printStackTrace();
244: }
245: SQLEngine.rollBackTrans(transNumber);
246: throw e;
247: }
248: }
249:
250: /**
251: * Change the length of the file in the database
252: * @param idBdd : id of the file in the table ATTACHMENT
253: * @param length : the new length
254: * no permission needed
255: */
256: public void updateFileLength(int idBdd, long length)
257: throws Exception {
258: if (idBdd < 1) {
259: throw new Exception(
260: "[SQLFileAttachment->updateFileLength] entry data are not valid");
261: }
262: int transNumber = -1;
263: try {
264: transNumber = SQLEngine.beginTransaction(0,
265: ApiConstants.UPDATE_ATTACHMENT);
266:
267: PreparedStatement prep = SQLEngine
268: .getSQLUpdateQuery("updateFileAttachLength"); //ok
269: prep.setLong(1, length);
270: prep.setInt(2, idBdd);
271: SQLEngine.runUpdateQuery(prep);
272:
273: SQLEngine.commitTrans(transNumber);
274: } catch (Exception e) {
275: Util.log("[SQLFileAttachment->updateFileLength]" + e);
276: if (Api.isDEBUG()) {
277: e.printStackTrace();
278: }
279: SQLEngine.rollBackTrans(transNumber);
280: throw e;
281: }
282: }
283:
284: /**
285: * Get the file on local disk (at path specified) for the attachment identified by idBdd
286: * @param idBdd : id of the file in the table ATTACHMENT
287: * @return the java file object representing localy the file
288: * @throws Exception
289: */
290: public SalomeFileWrapper getFile(int idBdd) throws Exception {
291: if (idBdd < 1) {
292: throw new Exception(
293: "[SQLFileAttachment->getFile] entry data are not valid");
294: }
295: File file = null;
296: InputStream is = null;
297: String tmpDir;
298: Properties sys = System.getProperties();
299: String fs = sys.getProperty("file.separator");
300:
301: // Repertoire temporaire système
302: tmpDir = sys.getProperty("java.io.tmpdir");
303: //tmpDir = tmpDir + fs + ApiConstants.PATH_TO_ADD_TO_TEMP+"_soap" + fs;
304: tmpDir = tmpDir + fs + ApiConstants.PATH_TO_ADD_TO_TEMP + fs;
305: PreparedStatement prep = SQLEngine
306: .getSQLSelectQuery("selectFileAttachFromDB"); //ok
307: prep.setInt(1, idBdd);
308: ResultSet res = SQLEngine.runSelectQuery(prep);
309: if (res.next()) {
310: //file = new File(res.getString("nom_attach"));
311: file = new File(tmpDir + res.getString("nom_attach"));
312: File fPath = new File(file.getCanonicalPath().substring(0,
313: file.getCanonicalPath().lastIndexOf(fs) + 1));
314: if (!fPath.exists()) {
315: fPath.mkdirs();
316: }
317: file.createNewFile();
318: // Flux d'écriture sur le fichier
319: FileOutputStream fos = new FileOutputStream(file);
320: BufferedOutputStream bos = new BufferedOutputStream(fos);
321:
322: // Flux du fichier stocké dans la BdD
323: is = res.getBinaryStream("contenu_attach");
324: BufferedInputStream bis = new BufferedInputStream(is);
325:
326: // Copie du flux dans le fichier
327: int car = bis.read();
328: while (car > -1) {
329: bos.write(car);
330: car = bis.read();
331: }
332: // Fermeture des flux de données
333: bos.flush();
334: bos.close();
335: bis.close();
336: }
337:
338: return new SalomeFileWrapper(file);
339: }
340:
341: /**
342: * Get the file on local disk (at path specified) for the attachment identified by idBdd
343: * @param idBdd : id of the file in the table ATTACHMENT
344: * @param file;
345: * @return the java file object representing localy the file
346: * @throws Exception
347: */
348: public SalomeFileWrapper getFileIn(int idBdd, SalomeFileWrapper file)
349: throws Exception {
350: if (idBdd < 1) {
351: throw new Exception(
352: "[SQLFileAttachment->getFile] entry data are not valid");
353: }
354: InputStream is = null;
355: String tmpDir;
356: Properties sys = System.getProperties();
357: String fs = sys.getProperty("file.separator");
358: // Repertoire temporaire système
359: tmpDir = sys.getProperty("java.io.tmpdir");
360: //tmpDir = tmpDir + fs + ApiConstants.PATH_TO_ADD_TO_TEMP+"_soap" + fs;
361: tmpDir = tmpDir + fs + ApiConstants.PATH_TO_ADD_TO_TEMP + fs;
362: PreparedStatement prep = SQLEngine
363: .getSQLSelectQuery("selectFileAttachFromDB"); //ok
364: prep.setInt(1, idBdd);
365: ResultSet res = SQLEngine.runSelectQuery(prep);
366: if (res.next()) {
367: //File f = new File(file.getFileName());
368: File f = new File(tmpDir + res.getString("nom_attach"));
369: File fPath = new File(f.getCanonicalPath().substring(0,
370: f.getCanonicalPath().lastIndexOf(fs) + 1));
371: if (!fPath.exists()) {
372: fPath.mkdirs();
373: }
374: f.createNewFile();
375:
376: // Flux d'écriture sur le fichier
377: FileOutputStream fos = new FileOutputStream(f);
378: BufferedOutputStream bos = new BufferedOutputStream(fos);
379:
380: // Flux du fichier stocké dans la BdD
381: is = res.getBinaryStream("contenu_attach");
382: BufferedInputStream bis = new BufferedInputStream(is);
383:
384: // Copie du flux dans le fichier
385: int car = bis.read();
386: while (car > -1) {
387: bos.write(car);
388: car = bis.read();
389: }
390: // Fermeture des flux de données
391: bos.flush();
392: bos.close();
393: bis.close();
394: return new SalomeFileWrapper(f);
395: }
396:
397: return null;
398: }
399:
400: }
|