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.BufferedOutputStream;
027: import java.io.File;
028: import java.io.FileOutputStream;
029: import java.sql.Date;
030:
031: import org.objectweb.salome_tmf.api.Api;
032: import org.objectweb.salome_tmf.api.data.FileAttachementWrapper;
033: import org.objectweb.salome_tmf.api.data.SalomeFileWrapper;
034: import org.objectweb.salome_tmf.api.sql.ISQLFileAttachment;
035:
036: public class FileAttachment extends Attachment {
037:
038: transient static ISQLFileAttachment pISQLFileAttachment = null;
039:
040: private Long size;
041: private Date date;
042:
043: /////////////////////// Selecteur //////////////////////////
044: public Date getDate() {
045: return date;
046: }
047:
048: public Long getSize() {
049: return size;
050: }
051:
052: ///////////////////// Constructeur ///////////////////////////
053:
054: public FileAttachment(String name, String description) {
055: super (name, description);
056: if (pISQLFileAttachment == null) {
057: pISQLFileAttachment = Api.getISQLObjectFactory()
058: .getISQLFileAttachment();
059: }
060: }
061:
062: public FileAttachment(File f, String description) {
063: super (f.getName(), description);
064: size = new Long(f.length());
065: date = new Date(f.lastModified());
066: localisation = f.getAbsolutePath();
067: if (pISQLFileAttachment == null) {
068: pISQLFileAttachment = Api.getISQLObjectFactory()
069: .getISQLFileAttachment();
070: }
071: }
072:
073: public void clearCache() {
074: /* NOTHING */
075: }
076:
077: // public FileAttachment(SalomeFileWrapper f, String description) {
078: // super(f.getName(), description);
079: // size = new Long(f.getLength());
080: // date = new Date(f.getLastModified());
081: // localisation = f.getAbsolutePath();
082: // if (pISQLFileAttachment == null){
083: // pISQLFileAttachment = Api.getISQLObjectFactory().getISQLFileAttachment();
084: // }
085: // }
086:
087: public FileAttachment(FileAttachementWrapper pFileAttachementWrapper) {
088: super (pFileAttachementWrapper.getName(),
089: pFileAttachementWrapper.getDescription());
090: idBdd = pFileAttachementWrapper.getIdBDD();
091: date = pFileAttachementWrapper.getDate();
092: size = pFileAttachementWrapper.getSize();
093: localisation = pFileAttachementWrapper.getLocalisation();
094: if (pISQLFileAttachment == null) {
095: pISQLFileAttachment = Api.getISQLObjectFactory()
096: .getISQLFileAttachment();
097: }
098: }
099:
100: ///////////////////// Methode //////////////////////////
101:
102: public void updateInDB(File f) throws Exception {
103: if (!f.exists()) {
104: throw new Exception("[FileAttachment] File no exits");
105: }
106: pISQLFileAttachment.updateFile(idBdd, new SalomeFileWrapper(f));
107: }
108:
109: public void updateInModel(File f) {
110: name = f.getName();
111: size = new Long(f.length());
112: date = new Date(f.lastModified());
113: localisation = f.getAbsolutePath();
114: }
115:
116: public void updateInDBAndModel(File f) throws Exception {
117: updateInDB(f);
118: updateInModel(f);
119: }
120:
121: public File getFileFromDB() throws Exception {
122: File f = getFileFromDB(null);
123: localisation = f.getAbsolutePath();
124: return f;
125:
126: // SalomeFileWrapper f = pISQLFileAttachment.getFile(idBdd);
127: //
128: // String path = null;
129: // File file = null;
130: // InputStream is = null;
131: // String tmpDir;
132: // Properties sys = System.getProperties();
133: // String fs = sys.getProperty("file.separator");
134: // if (path == null || path.equals("")){
135: // // Repertoire temporaire système
136: // tmpDir = sys.getProperty("java.io.tmpdir");
137: // tmpDir = tmpDir + fs + ApiConstants.PATH_TO_ADD_TO_TEMP + fs;
138: // } else {
139: // tmpDir = path;
140: // if (!path.endsWith(fs)){
141: // tmpDir = tmpDir + fs;
142: // }
143: //
144: // }
145: // Util.log("[SQLFileAttachment->getFile] at path : " + tmpDir);
146: // file = new File(tmpDir + f.getName());
147: // File fPath = new File(file.getCanonicalPath().substring(0,file.getCanonicalPath().lastIndexOf(fs)+1));
148: // if (!fPath.exists()){
149: // fPath.mkdirs();
150: // }
151: // file.createNewFile();
152: //
153: // // Flux d'écriture sur le fichier
154: // FileOutputStream fos = new FileOutputStream(file);
155: // BufferedOutputStream bos = new BufferedOutputStream(fos);
156: //
157: // // Flux du fichier stocké dans la BdD
158: // bos.write(f.getContent());
159: //
160: // // Fermeture des flux de données
161: // bos.flush();
162: // bos.close();
163: //
164: // localisation = file.getAbsolutePath();
165: // return new SalomeFileWrapper(file);
166: }
167:
168: public File getFileFromDB(String filePath) throws Exception {
169:
170: //long debut = Calendar.getInstance().getTimeInMillis();
171: return pISQLFileAttachment.getFile(idBdd).toFile(filePath);
172:
173: /*
174: SalomeFileWrapper f = pISQLFileAttachment.getFile(idBdd);
175:
176: //String path = null;
177: String path = filePath;
178: File file = null;
179: String tmpDir;
180: Properties sys = System.getProperties();
181: String fs = sys.getProperty("file.separator");
182: if (path == null || path.equals("")){
183: // Repertoire temporaire système
184: tmpDir = sys.getProperty("java.io.tmpdir");
185: tmpDir = tmpDir + fs + ApiConstants.PATH_TO_ADD_TO_TEMP + fs;
186: } else {
187: tmpDir = path;
188: if (!path.endsWith(fs)){
189: tmpDir = tmpDir + fs;
190: }
191:
192: }
193: Util.log("[SQLFileAttachment->getFile] at path : " + tmpDir);
194: file = new File(tmpDir + f.getName());
195: File fPath = new File(file.getCanonicalPath().substring(0,file.getCanonicalPath().lastIndexOf(fs)+1));
196: if (!fPath.exists()){
197: fPath.mkdirs();
198: }
199: file.createNewFile();
200:
201: // Flux d'écriture sur le fichier
202: FileOutputStream fos = new FileOutputStream(file);
203: BufferedOutputStream bos = new BufferedOutputStream(fos);
204:
205: // Flux du fichier stocké dans la BdD
206: bos.write(f.getContent());
207:
208: // Fermeture des flux de données
209: bos.flush();
210: bos.close();
211:
212: return file;*/
213: }
214:
215: public File getFileFromDBIn(File f) throws Exception {
216: SalomeFileWrapper file = pISQLFileAttachment.getFileIn(idBdd,
217: new SalomeFileWrapper(f));
218: //localisation = f.getAbsolutePath();
219: f.createNewFile();
220:
221: // Flux d'écriture sur le fichier
222: FileOutputStream fos = new FileOutputStream(f);
223: BufferedOutputStream bos = new BufferedOutputStream(fos);
224:
225: // Flux du fichier stocké dans la BdD
226: bos.write(file.getContent());
227:
228: // Fermeture des flux de données
229: bos.flush();
230:
231: return file.toFile();
232: }
233:
234: public void deleteFromDisk() throws Exception {
235: if (localisation != null && !localisation.equals("")) {
236: File file = new File(localisation);
237: if (file.exists()) {
238: file.delete();
239: }
240: }
241: localisation = null;
242: }
243:
244: public File getLocalFile() throws Exception {
245: File file = null;
246: if (localisation != null && !localisation.equals("")) {
247: file = new File(localisation);
248: }
249: return file;
250: }
251:
252: public boolean equals(Object o) {
253: if (o instanceof FileAttachment) {
254: FileAttachment toTest = (FileAttachment) o;
255: if (isInBase() && toTest.isInBase()) {
256: if (getIdBdd() == toTest.getIdBdd()) {
257: return true;
258: }
259: } else {
260: String loc2 = toTest.getLocalisation();
261: if (localisation != null && !localisation.equals("")
262: && loc2 != null && !loc2.equals("")) {
263: return localisation.equals(loc2);
264: }
265: return super .equals(o);
266: }
267: }
268: return false;
269: }
270: }
|