001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * @created Jul 1, 2005
014: * @author Marc Batchelor
015: *
016: */
017:
018: package org.pentaho.repository.content;
019:
020: import java.io.BufferedReader;
021: import java.io.File;
022: import java.io.FileInputStream;
023: import java.io.FileOutputStream;
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.io.InputStreamReader;
027: import java.io.OutputStream;
028: import java.io.Reader;
029: import java.util.Date;
030: import java.util.List;
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033: import org.pentaho.core.repository.content.ContentException;
034: import org.pentaho.core.repository.IContentItemFile;
035: import org.pentaho.core.system.PentahoBase;
036: import org.pentaho.core.system.PentahoSystem;
037: import org.pentaho.messages.MessageUtil;
038: import org.pentaho.messages.Messages;
039:
040: public class ContentItemFile extends PentahoBase implements
041: IContentItemFile {
042: public static final int ClassVersionNumber = 2;
043:
044: private static final long serialVersionUID = 946969559555268447L;
045:
046: private static final Log logger = LogFactory
047: .getLog(ContentItemFile.class);
048:
049: private String osFileName;
050:
051: private String osPath;
052:
053: private String actionName;
054:
055: private Date storedFileDate;
056:
057: private String id;
058:
059: private int revision = -1; // Hibernate Version #
060:
061: private ContentItem parent;
062:
063: private int initialized = -1;
064:
065: private File itemFile;
066:
067: private static final String PATH_BUILDER = "{0}/{1}"; //$NON-NLS-1$
068:
069: protected ContentItemFile() {
070: }
071:
072: public boolean equals(Object other) {
073: if (this == other) {
074: return true;
075: }
076: if (!(other instanceof ContentItemFile)) {
077: return false;
078: }
079: final ContentItemFile that = (ContentItemFile) other;
080: return this .getId().equals(that.getId());
081: }
082:
083: public int hashCode() {
084: return getId().hashCode();
085: }
086:
087: public List getMessages() {
088: return null;
089: }
090:
091: /**
092: * @return Returns the revision.
093: */
094: public int getRevision() {
095: return revision;
096: }
097:
098: /**
099: * @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 guid,
107: String osPath, String osFileName, String actionName) {
108: this .parent = parent;
109: this .id = guid;
110: this .osPath = osPath;
111: this .osFileName = osFileName;
112: this .actionName = actionName;
113: this .itemFile = new File(getCompleteFileName());
114: this .storedFileDate = new Date(System.currentTimeMillis());
115: }
116:
117: public InputStream getInputStream() throws ContentException {
118: String fName = getCompleteFileName();
119: try {
120: if (itemFile.exists()) {
121: if (itemFile.canRead()) {
122: return new FileInputStream(itemFile);
123: }
124: throw new ContentException(
125: Messages
126: .getErrorString(
127: "CONTFILE.ERROR_0001_FILE_CANNOT_BE_READ", fName)); //$NON-NLS-1$
128: }
129: throw new ContentException(Messages.getErrorString(
130: "CONTFILE.ERROR_0002_FILE_DOES_NOT_EXIST", fName)); //$NON-NLS-1$
131: } catch (IOException e) {
132: throw new ContentException(e.getMessage(), e);
133: }
134: }
135:
136: public Reader getReader() throws ContentException {
137: InputStream is = getInputStream();
138: return new BufferedReader(new InputStreamReader(is));
139: }
140:
141: public OutputStream getOutputStream(boolean overWriteOk)
142: throws ContentException {
143: return getOutputStream(overWriteOk, false);
144: }
145:
146: public OutputStream getOutputStream(boolean overWriteOk,
147: boolean append) throws ContentException {
148: String fName = getCompleteFileName();
149: if (itemFile.exists() && (!overWriteOk)) {
150: // Not allowed to overwrite a file that is versioned.
151: throw new ContentException(Messages.getErrorString(
152: "CONTFILE.ERROR_0003_OVERWRITE_DISALLOWED", fName)); //$NON-NLS-1$
153: }
154: try {
155: if (!append) {
156: if (itemFile.exists()) {
157: if (!itemFile.delete()) {
158: throw new ContentException(
159: Messages
160: .getErrorString(
161: "CONTFILE.ERROR_0004_CANNOT_DELETE_FOR_CREATE", fName)); //$NON-NLS-1$
162: }
163: }
164: if (itemFile.createNewFile()) {
165: return new FileOutputStream(itemFile);
166: }
167: throw new ContentException(Messages.getErrorString(
168: "CONTFILE.ERROR_0005_CANNOT_CREATE", fName)); //$NON-NLS-1$
169: }
170: return new FileOutputStream(itemFile, append);
171: } catch (IOException ex) {
172: throw new ContentException(ex);
173: }
174: }
175:
176: public long copyToFile(String newFileName) throws ContentException {
177: try {
178: InputStream is = getInputStream();
179: try {
180: OutputStream os = new FileOutputStream(newFileName);
181: try {
182: long bytesCopied = 0;
183: int size;
184: byte[] copyBuffer = new byte[4096];
185: while ((size = is.read(copyBuffer)) != -1) {
186: os.write(copyBuffer, 0, size);
187: bytesCopied += size;
188: }
189: return bytesCopied;
190: } finally {
191: os.flush();
192: os.close();
193: }
194: } finally {
195: is.close();
196: }
197: } catch (IOException ex) {
198: throw new ContentException(
199: Messages
200: .getErrorString(
201: "CONTFILE.ERROR_0006_DURING_COPY", this .getCompleteFileName(), newFileName), ex); //$NON-NLS-1$
202: }
203: }
204:
205: public boolean deleteOsFile() {
206: String fName = getCompleteFileName();
207: File f = new File(fName);
208: return f.delete();
209: }
210:
211: protected String getCompleteFileName() {
212: return MessageUtil
213: .formatMessage(
214: PATH_BUILDER,
215: PentahoSystem.getApplicationContext()
216: .getFileOutputPath("system/content") + "/" + getOsPath(), getOsFileName()); //$NON-NLS-1$ //$NON-NLS-2$
217: }
218:
219: /**
220: * @return Returns the parent.
221: */
222: public ContentItem getParent() {
223: return parent;
224: }
225:
226: /**
227: * @param parent
228: * The parent to set.
229: */
230: public void setParent(ContentItem parent) {
231: this .parent = parent;
232: }
233:
234: /**
235: * @return Returns the actionName.
236: */
237: public String getActionName() {
238: return actionName;
239: }
240:
241: /**
242: * @param actionName
243: * The actionName to set.
244: */
245: public void setActionName(String actionName) {
246: this .actionName = actionName;
247: }
248:
249: /**
250: * @return Returns the fileDateTime.
251: */
252: public Date getFileDateTime() {
253: return storedFileDate;
254: }
255:
256: /**
257: * @param fileDateTime
258: * The fileDateTime to set.
259: */
260: protected void setFileDateTime(Date fileDateTime) {
261: // Now, handle the case where the file is on another machine from this
262: // one.
263: storedFileDate = fileDateTime;
264: }
265:
266: /**
267: * @return Returns the fileSize.
268: */
269: public long getFileSize() {
270: // Return the OS File Size
271: return itemFile.length();
272: }
273:
274: /**
275: * @param fileSize
276: * The fileSize to set.
277: */
278: public void setFileSize(long fileSize) {
279: // Do nothing because it'll be obtained by the OS
280: // this line is here to prevent compiler warnings only
281: fileSize++;
282: }
283:
284: /**
285: * @return Returns the id.
286: */
287: public String getId() {
288: return id;
289: }
290:
291: /**
292: * @param id
293: * The id to set.
294: */
295: public void setId(String id) {
296: this .id = id;
297: }
298:
299: /**
300: * @return Returns the osFileName.
301: */
302: public String getOsFileName() {
303: return osFileName;
304: }
305:
306: /**
307: * @param osFileName
308: * The osFileName to set.
309: */
310: public void setOsFileName(String osFileName) {
311: this .osFileName = osFileName;
312: }
313:
314: /**
315: * @return Returns the osPath.
316: */
317: public String getOsPath() {
318: return osPath;
319: }
320:
321: /**
322: * @param osPath
323: * The osPath to set.
324: */
325: public void setOsPath(String osPath) {
326: this .osPath = osPath;
327: }
328:
329: /**
330: * @return Returns the initialized.
331: */
332: public int getInitialized() {
333: return initialized;
334: }
335:
336: /**
337: * @param initialized
338: * The initialized to set.
339: */
340: public void setInitialized(int initialized) {
341: // This is a dummy property that gets filled after construction of the
342: // object from
343: // hibernate. There may be a better way of being called after all of an
344: // objects'
345: // properties get initialized, but I didn't see one (short of creating a
346: // new user type).
347: itemFile = new File(getCompleteFileName());
348: this .initialized = initialized;
349: }
350:
351: /**
352: * @return Returns the itemFile.
353: */
354: public File getItemFile() {
355: return itemFile;
356: }
357:
358: /**
359: * @param itemFile
360: * The itemFile to set.
361: */
362: public void setItemFile(File itemFile) {
363: this .itemFile = itemFile;
364: }
365:
366: /*
367: * (non-Javadoc)
368: *
369: * @see org.pentaho.core.system.PentahoBase#getLogger()
370: */
371: public Log getLogger() {
372: return logger;
373: }
374:
375: public String toString() {
376: StringBuffer buf = new StringBuffer()
377: .append("[").append(this .getId()) //$NON-NLS-1$
378: .append(",").append(this .getOsPath()) //$NON-NLS-1$
379: .append(",").append(this .getOsFileName()) //$NON-NLS-1$
380: .append(",").append(this .getActionName()) //$NON-NLS-1$
381: .append("]"); //$NON-NLS-1$
382: return buf.toString();
383: }
384:
385: }
|