1 17 18 package org.pentaho.repository.content; 19 20 import java.io.BufferedReader ; 21 import java.io.File ; 22 import java.io.FileInputStream ; 23 import java.io.FileOutputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.InputStreamReader ; 27 import java.io.OutputStream ; 28 import java.io.Reader ; 29 import java.util.Date ; 30 import java.util.List ; 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 osFileName; 48 49 private String osPath; 50 51 private String actionName; 52 53 private Date storedFileDate; 54 55 private String id; 56 57 private int revision = -1; 59 private int versionNum; 60 61 private ContentItem parent; 62 63 private int initialized = -1; 64 65 private File itemFile; 66 67 private static final String PATH_BUILDER = "{0}/{1}"; 69 protected ContentItemFile() { 70 } 71 72 public boolean equals(Object 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 getMessages() { 88 return null; 89 } 90 91 94 public int getRevision() { 95 return revision; 96 } 97 98 102 public void setRevision(int revision) { 103 this.revision = revision; 104 } 105 106 protected ContentItemFile(ContentItem parent, String guid, String osPath, String osFileName, int versionNum, String 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 (getCompleteFileName()); 114 this.setFileDateTime(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(Messages.getErrorString("CONTFILE.ERROR_0001_FILE_CANNOT_BE_READ", fName)); } 126 throw new ContentException(Messages.getErrorString("CONTFILE.ERROR_0002_FILE_DOES_NOT_EXIST", fName)); } catch (IOException e) { 128 throw new ContentException(e.getMessage(), e); 129 } 130 } 131 132 public Reader getReader() throws ContentException { 133 InputStream is = getInputStream(); 134 return new BufferedReader (new InputStreamReader (is)); 135 } 136 137 public OutputStream getOutputStream(boolean overWriteOk) throws ContentException { 138 return getOutputStream(overWriteOk, false); 139 } 140 141 public OutputStream getOutputStream(boolean overWriteOk, boolean append) throws ContentException { 142 String fName = getCompleteFileName(); 143 if (itemFile.exists() && (!overWriteOk)) { 144 throw new ContentException(Messages.getErrorString("CONTFILE.ERROR_0003_OVERWRITE_DISALLOWED", fName)); } 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)); } 153 } 154 if (itemFile.createNewFile()) { 155 return new FileOutputStream (itemFile); 156 } 157 throw new ContentException(Messages.getErrorString("CONTFILE.ERROR_0005_CANNOT_CREATE", fName)); } 159 return new FileOutputStream (itemFile, append); 160 } catch (IOException ex) { 161 throw new ContentException(ex); 162 } 163 } 164 165 public long copyToFile(String newFileName) throws ContentException { 166 try { 167 InputStream is = getInputStream(); 168 try { 169 OutputStream os = new FileOutputStream (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 ex) { 187 throw new ContentException(Messages.getErrorString("CONTFILE.ERROR_0006_DURING_COPY", this.getCompleteFileName(), newFileName), ex); } 189 } 190 191 public boolean deleteOsFile() { 192 String fName = getCompleteFileName(); 193 File f = new File (fName); 194 return f.delete(); 195 } 196 197 protected String getCompleteFileName() { 198 return MessageUtil.formatMessage(PATH_BUILDER, PentahoSystem.getApplicationContext().getFileOutputPath("system/content") + "/" + getOsPath(), getOsFileName()); } 200 201 204 public ContentItem getParent() { 205 return parent; 206 } 207 208 212 public void setParent(ContentItem parent) { 213 this.parent = parent; 214 } 215 216 219 public String getActionName() { 220 return actionName; 221 } 222 223 227 public void setActionName(String actionName) { 228 this.actionName = actionName; 229 } 230 231 234 public Date getFileDateTime() { 235 if (itemFile.exists()) { 237 if (storedFileDate.getTime() != itemFile.lastModified()) { 238 setFileDateTime(new Date (itemFile.lastModified())); 242 } 243 } 244 return storedFileDate; 245 } 246 247 251 protected void setFileDateTime(Date fileDateTime) { 252 storedFileDate = fileDateTime; 255 } 256 257 260 public long getFileSize() { 261 return itemFile.length(); 263 } 264 265 269 public void setFileSize(long fileSize) { 270 fileSize++; 273 } 274 275 278 public String getId() { 279 return id; 280 } 281 282 286 public void setId(String id) { 287 this.id = id; 288 } 289 290 293 public String getOsFileName() { 294 return osFileName; 295 } 296 297 301 public void setOsFileName(String osFileName) { 302 this.osFileName = osFileName; 303 } 304 305 308 public String getOsPath() { 309 return osPath; 310 } 311 312 316 public void setOsPath(String osPath) { 317 this.osPath = osPath; 318 } 319 320 323 public int getVersionNum() { 324 return versionNum; 325 } 326 327 331 public void setVersionNum(int versionNum) { 332 this.versionNum = versionNum; 333 } 334 335 338 public int getInitialized() { 339 return initialized; 340 } 341 342 346 public void setInitialized(int initialized) { 347 itemFile = new File (getCompleteFileName()); 354 this.initialized = initialized; 355 } 356 357 360 public File getItemFile() { 361 return itemFile; 362 } 363 364 368 public void setItemFile(File itemFile) { 369 this.itemFile = itemFile; 370 } 371 372 377 public Log getLogger() { 378 return logger; 379 } 380 381 } 382 | Popular Tags |