KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > repository > content > ContentItemFile


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * @created Jul 1, 2005
14  * @author Marc Batchelor
15  *
16  */

17
18 package org.pentaho.repository.content;
19
20 import java.io.BufferedReader JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.FileInputStream JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.InputStreamReader JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.io.Reader JavaDoc;
29 import java.util.Date JavaDoc;
30 import java.util.List JavaDoc;
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.pentaho.core.repository.content.ContentException;
34 import org.pentaho.core.repository.IContentItemFile;
35 import org.pentaho.core.system.PentahoBase;
36 import org.pentaho.core.system.PentahoSystem;
37 import org.pentaho.messages.MessageUtil;
38 import org.pentaho.messages.Messages;
39
40 public class ContentItemFile extends PentahoBase implements IContentItemFile {
41     public static final int ClassVersionNumber = 1;
42
43     private static final long serialVersionUID = 946969559555268447L;
44
45     private static final Log logger = LogFactory.getLog(ContentItemFile.class);
46
47     private String JavaDoc osFileName;
48
49     private String JavaDoc osPath;
50
51     private String JavaDoc actionName;
52
53     private Date JavaDoc storedFileDate;
54
55     private String JavaDoc id;
56
57     private int revision = -1; // Hibernate Version #
58

59     private int versionNum;
60
61     private ContentItem parent;
62
63     private int initialized = -1;
64
65     private File JavaDoc itemFile;
66
67     private static final String JavaDoc PATH_BUILDER = "{0}/{1}"; //$NON-NLS-1$
68

69     protected ContentItemFile() {
70     }
71
72     public boolean equals(Object JavaDoc other) {
73         if (this == other) {
74             return true;
75         }
76         if (!(other instanceof ContentItemFile)) {
77             return false;
78         }
79         final ContentItemFile that = (ContentItemFile) other;
80         return this.getId().equals(that.getId());
81     }
82
83     public int hashCode() {
84         return getId().hashCode();
85     }
86
87     public List JavaDoc getMessages() {
88         return null;
89     }
90
91     /**
92      * @return Returns the revision.
93      */

94     public int getRevision() {
95         return revision;
96     }
97
98     /**
99      * @param revision
100      * The revision to set.
101      */

102     public void setRevision(int revision) {
103         this.revision = revision;
104     }
105
106     protected ContentItemFile(ContentItem parent, String JavaDoc guid, String JavaDoc osPath, String JavaDoc osFileName, int versionNum, String JavaDoc actionName) {
107         this.parent = parent;
108         this.id = guid;
109         this.osPath = osPath;
110         this.osFileName = osFileName;
111         this.versionNum = versionNum;
112         this.actionName = actionName;
113         this.itemFile = new File JavaDoc(getCompleteFileName());
114         this.setFileDateTime(new Date JavaDoc(System.currentTimeMillis()));
115     }
116
117     public InputStream JavaDoc getInputStream() throws ContentException {
118         String JavaDoc fName = getCompleteFileName();
119         try {
120             if (itemFile.exists()) {
121                 if (itemFile.canRead()) {
122                     return new FileInputStream JavaDoc(itemFile);
123                 }
124                 throw new ContentException(Messages.getErrorString("CONTFILE.ERROR_0001_FILE_CANNOT_BE_READ", fName)); //$NON-NLS-1$
125
}
126             throw new ContentException(Messages.getErrorString("CONTFILE.ERROR_0002_FILE_DOES_NOT_EXIST", fName)); //$NON-NLS-1$
127
} catch (IOException JavaDoc e) {
128             throw new ContentException(e.getMessage(), e);
129         }
130     }
131
132     public Reader JavaDoc getReader() throws ContentException {
133         InputStream JavaDoc is = getInputStream();
134         return new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is));
135     }
136
137     public OutputStream JavaDoc getOutputStream(boolean overWriteOk) throws ContentException {
138         return getOutputStream(overWriteOk, false);
139     }
140
141     public OutputStream JavaDoc getOutputStream(boolean overWriteOk, boolean append) throws ContentException {
142         String JavaDoc fName = getCompleteFileName();
143         if (itemFile.exists() && (!overWriteOk)) {
144             // Not allowed to overwrite a file that is versioned.
145
throw new ContentException(Messages.getErrorString("CONTFILE.ERROR_0003_OVERWRITE_DISALLOWED", fName)); //$NON-NLS-1$
146
}
147         try {
148             if (!append) {
149                 if (itemFile.exists()) {
150                     if (!itemFile.delete()) {
151                         throw new ContentException(Messages.getErrorString("CONTFILE.ERROR_0004_CANNOT_DELETE_FOR_CREATE", fName)); //$NON-NLS-1$
152
}
153                 }
154                 if (itemFile.createNewFile()) {
155                     return new FileOutputStream JavaDoc(itemFile);
156                 }
157                 throw new ContentException(Messages.getErrorString("CONTFILE.ERROR_0005_CANNOT_CREATE", fName)); //$NON-NLS-1$
158
}
159             return new FileOutputStream JavaDoc(itemFile, append);
160         } catch (IOException JavaDoc ex) {
161             throw new ContentException(ex);
162         }
163     }
164
165     public long copyToFile(String JavaDoc newFileName) throws ContentException {
166         try {
167             InputStream JavaDoc is = getInputStream();
168             try {
169                 OutputStream JavaDoc os = new FileOutputStream JavaDoc(newFileName);
170                 try {
171                     long bytesCopied = 0;
172                     int size;
173                     byte[] copyBuffer = new byte[4096];
174                     while ((size = is.read(copyBuffer)) != -1) {
175                         os.write(copyBuffer, 0, size);
176                         bytesCopied += size;
177                     }
178                     return bytesCopied;
179                 } finally {
180                     os.flush();
181                     os.close();
182                 }
183             } finally {
184                 is.close();
185             }
186         } catch (IOException JavaDoc ex) {
187             throw new ContentException(Messages.getErrorString("CONTFILE.ERROR_0006_DURING_COPY", this.getCompleteFileName(), newFileName), ex); //$NON-NLS-1$
188
}
189     }
190
191     public boolean deleteOsFile() {
192         String JavaDoc fName = getCompleteFileName();
193         File JavaDoc f = new File JavaDoc(fName);
194         return f.delete();
195     }
196
197     protected String JavaDoc getCompleteFileName() {
198         return MessageUtil.formatMessage(PATH_BUILDER, PentahoSystem.getApplicationContext().getFileOutputPath("system/content") + "/" + getOsPath(), getOsFileName()); //$NON-NLS-1$ //$NON-NLS-2$
199
}
200
201     /**
202      * @return Returns the parent.
203      */

204     public ContentItem getParent() {
205         return parent;
206     }
207
208     /**
209      * @param parent
210      * The parent to set.
211      */

212     public void setParent(ContentItem parent) {
213         this.parent = parent;
214     }
215
216     /**
217      * @return Returns the actionName.
218      */

219     public String JavaDoc getActionName() {
220         return actionName;
221     }
222
223     /**
224      * @param actionName
225      * The actionName to set.
226      */

227     public void setActionName(String JavaDoc actionName) {
228         this.actionName = actionName;
229     }
230
231     /**
232      * @return Returns the fileDateTime.
233      */

234     public Date JavaDoc getFileDateTime() {
235         // Return the OS Last Modified Date, if possible.
236
if (itemFile.exists()) {
237             if (storedFileDate.getTime() != itemFile.lastModified()) {
238                 // Kinda creepy here - but, the getter may indeed call the
239
// setter if the values aren't the same. This should end up
240
// marking the class dirty for hibernate to save to the RDB.
241
setFileDateTime(new Date JavaDoc(itemFile.lastModified()));
242             }
243         }
244         return storedFileDate;
245     }
246
247     /**
248      * @param fileDateTime
249      * The fileDateTime to set.
250      */

251     protected void setFileDateTime(Date JavaDoc fileDateTime) {
252         // Now, handle the case where the file is on another machine from this
253
// one.
254
storedFileDate = fileDateTime;
255     }
256
257     /**
258      * @return Returns the fileSize.
259      */

260     public long getFileSize() {
261         // Return the OS File Size
262
return itemFile.length();
263     }
264
265     /**
266      * @param fileSize
267      * The fileSize to set.
268      */

269     public void setFileSize(long fileSize) {
270         // Do nothing because it'll be obtained by the OS
271
// this line is here to prevent compiler warnings only
272
fileSize++;
273     }
274
275     /**
276      * @return Returns the id.
277      */

278     public String JavaDoc getId() {
279         return id;
280     }
281
282     /**
283      * @param id
284      * The id to set.
285      */

286     public void setId(String JavaDoc id) {
287         this.id = id;
288     }
289
290     /**
291      * @return Returns the osFileName.
292      */

293     public String JavaDoc getOsFileName() {
294         return osFileName;
295     }
296
297     /**
298      * @param osFileName
299      * The osFileName to set.
300      */

301     public void setOsFileName(String JavaDoc osFileName) {
302         this.osFileName = osFileName;
303     }
304
305     /**
306      * @return Returns the osPath.
307      */

308     public String JavaDoc getOsPath() {
309         return osPath;
310     }
311
312     /**
313      * @param osPath
314      * The osPath to set.
315      */

316     public void setOsPath(String JavaDoc osPath) {
317         this.osPath = osPath;
318     }
319
320     /**
321      * @return Returns the versionNum.
322      */

323     public int getVersionNum() {
324         return versionNum;
325     }
326
327     /**
328      * @param versionNum
329      * The versionNum to set.
330      */

331     public void setVersionNum(int versionNum) {
332         this.versionNum = versionNum;
333     }
334
335     /**
336      * @return Returns the initialized.
337      */

338     public int getInitialized() {
339         return initialized;
340     }
341
342     /**
343      * @param initialized
344      * The initialized to set.
345      */

346     public void setInitialized(int initialized) {
347         // This is a dummy property that gets filled after construction of the
348
// object from
349
// hibernate. There may be a better way of being called after all of an
350
// objects'
351
// properties get initialized, but I didn't see one (short of creating a
352
// new user type).
353
itemFile = new File JavaDoc(getCompleteFileName());
354         this.initialized = initialized;
355     }
356
357     /**
358      * @return Returns the itemFile.
359      */

360     public File JavaDoc getItemFile() {
361         return itemFile;
362     }
363
364     /**
365      * @param itemFile
366      * The itemFile to set.
367      */

368     public void setItemFile(File JavaDoc itemFile) {
369         this.itemFile = itemFile;
370     }
371
372     /*
373      * (non-Javadoc)
374      *
375      * @see org.pentaho.core.system.PentahoBase#getLogger()
376      */

377     public Log getLogger() {
378         return logger;
379     }
380
381 }
382
Popular Tags