1 17 18 package org.pentaho.repository.content; 19 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.io.OutputStream ; 23 import java.io.Reader ; 24 import java.text.MessageFormat ; 25 import java.util.ArrayList ; 26 import java.util.Date ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import javax.activation.FileDataSource ; 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 import org.pentaho.core.repository.content.ContentException; 33 import org.pentaho.core.repository.IContentItem; 34 import org.pentaho.core.system.PentahoBase; 35 import org.pentaho.core.system.PentahoSystem; 36 import org.pentaho.messages.MessageUtil; 37 import org.pentaho.messages.Messages; 38 import org.pentaho.repository.HibernateUtil; 39 import org.pentaho.repository.ISearchable; 40 import org.pentaho.util.UUIDUtil; 41 42 public class ContentItem extends PentahoBase implements IContentItem, ISearchable { 43 public static final int ClassVersionNumber = 1; 44 45 private static final long serialVersionUID = 823604019645900631L; 46 47 private static final Log logger = LogFactory.getLog(ContentItem.class); 48 49 private String id; 51 private ContentLocation parent; 53 private String path; 55 private String name; 57 private String title; 59 private String mimeType; 61 private String url; 63 private String extension; 65 private List fileVersions = new ArrayList (); 66 67 private ContentItemFile latestFile; 68 69 private int latestVersionNum; 71 private int revision = -1; 73 private int writeMode; 75 77 private static final String PATH_BUILDER = "{0}/{1}"; 79 private static final String [] SearchableColumns = { "name", "title", "path" }; 83 private static final String SearchableTable = "org.pentaho.repository.content.ContentItem"; 85 private static final String SearchablePhraseNamedQuery = "org.pentaho.repository.content.ContentItem.itemSearcher"; 87 88 89 protected ContentItem() { 90 } 91 92 102 protected ContentItem(String cntId, ContentLocation theParent, String theName, String title, String mType, String extension, String url, int writeMode) { 103 name = theName; 104 mimeType = mType; 105 parent = theParent; 106 id = cntId; 107 String extnSep = "."; if (!extension.startsWith(extnSep)) { 109 this.extension = extnSep + extension; 110 } else { 111 this.extension = extension; 112 } 113 this.title = title; 114 this.url = url; 115 this.path = MessageUtil.formatMessage(PATH_BUILDER, parent.getDirPath(), this.getName()); 116 this.writeMode = writeMode; 117 } 118 119 public List getMessages() { 120 return null; 121 } 122 123 protected ContentItemFile newContentFile(String actionName) { 124 String fileGuid = UUIDUtil.getUUIDAsString(); 125 String fileName = fileGuid + this.getExtension(); 126 latestVersionNum++; 127 ContentItemFile theFile = new ContentItemFile(this, fileGuid, parent.getDirPath(), fileName, latestVersionNum, actionName); 128 this.latestFile = theFile; 129 fileVersions.add(theFile); 130 return theFile; 131 } 132 133 public InputStream getInputStream() throws ContentException { 134 ContentItemFile cif = getLatestFile(); 135 if (this.latestFile == null) { 136 throw new ContentException(Messages.getErrorString("CONTITEM.ERROR_0001_NO_EXISTING_FILES", this.getName())); } 138 return cif.getInputStream(); 139 } 140 141 public FileDataSource getDataSource() { 142 ContentItemFile cif = getLatestFile(); 143 if (this.latestFile == null) { 144 throw new ContentException(Messages.getErrorString("CONTITEM.ERROR_0001_NO_EXISTING_FILES", this.getName())); } 146 147 String fullPath = PentahoSystem.getApplicationContext().getFileOutputPath("system/content/" + cif.getOsPath() + "/" + cif.getId() + extension); FileDataSource dataSource = new FileDataSource (fullPath); 149 return dataSource; 150 } 151 152 public Reader getReader() throws ContentException { 153 ContentItemFile cif = getLatestFile(); 154 if (this.latestFile == null) { 155 throw new ContentException(Messages.getErrorString("CONTITEM.ERROR_0002_NO_EXISTING_FILES", this.getName())); } 157 return cif.getReader(); 158 } 159 160 public OutputStream getOutputStream(String actionName) throws IOException { 161 if (actionName == null) { 162 throw new IllegalArgumentException (Messages.getErrorString("CONTITEM.ERROR_0006_ACTION_NAME_CANNOT_BE_NULL")); } 164 switch (getWriteMode()) { 165 case WRITEMODE_KEEPVERSIONS: { 166 ContentItemFile cif = newContentFile(actionName); 167 return cif.getOutputStream(false); 168 } 169 case WRITEMODE_OVERWRITE: { 170 ContentItemFile cif = getLatestFile(); 171 if (cif == null) { 172 cif = newContentFile(actionName); 173 } 174 if (cif != null) { 175 return cif.getOutputStream(true); 176 } 177 throw new IOException (Messages.getErrorString("CONTITEM.ERROR_0004_OUTPUT_STREAM_NOT_AVAILABLE")); } 179 case WRITEMODE_APPEND: { 180 ContentItemFile cif = getLatestFile(); 181 if (cif == null) { 182 cif = newContentFile(actionName); 183 } 184 if (cif != null) { 185 return cif.getOutputStream(true, true); 186 } 187 throw new IOException (Messages.getErrorString("CONTITEM.ERROR_0004_OUTPUT_STREAM_NOT_AVAILABLE")); } 189 default: { 190 throw new ContentException(Messages.getErrorString("CONTITEM.ERROR_0003_BAD_WRITE_MODE", Integer.toString(getWriteMode()))); } 192 } 193 } 194 195 public String getActionName() { 196 return this.getLatestFile() != null ? this.getLatestFile().getActionName() : null; 197 } 198 199 public String getFileId() { 200 return this.getLatestFile() != null ? this.getLatestFile().getId() : null; 201 } 202 203 public long getFileSize() { 204 return this.getLatestFile() != null ? this.getLatestFile().getFileSize() : -1; 205 } 206 207 public Date getFileDateTime() { 208 return this.getLatestFile() != null ? this.getLatestFile().getFileDateTime() : null; 209 } 210 211 214 public int getWriteMode() { 215 return writeMode; 216 } 217 218 222 public void setWriteMode(int writeMode) { 223 this.writeMode = writeMode; 224 } 225 226 231 public boolean equals(Object other) { 232 if (this == other) { 233 return true; 234 } 235 if (!(other instanceof ContentItem)) { 236 return false; 237 } 238 final ContentItem that = (ContentItem) other; 239 return this.getId().equals(that.getId()); 240 } 241 242 247 public int hashCode() { 248 return getId().hashCode(); 249 } 250 251 254 public String getExtension() { 255 return extension; 256 } 257 258 262 public void setExtension(String extension) { 263 this.extension = extension; 264 } 265 266 269 protected ContentItemFile getLatestFile() { 270 if (latestFile == null) { 271 List revs = this.getFileVersions(); 272 if ((revs != null) && (revs.size() > 0)) { 273 this.latestFile = (ContentItemFile) revs.get(revs.size() - 1); 274 } 275 } 276 return latestFile; 277 } 278 279 283 public void setLatestFile(ContentItemFile latestFile) { 284 this.latestFile = latestFile; 285 } 286 287 290 public int getLatestVersionNum() { 291 return latestVersionNum; 292 } 293 294 298 public void setLatestVersionNum(int latestVersionNum) { 299 this.latestVersionNum = latestVersionNum; 300 } 301 302 305 public int getRevision() { 306 return revision; 307 } 308 309 313 public void setRevision(int revision) { 314 this.revision = revision; 315 } 316 317 public void removeVersion(ContentItemFile cif) { 318 try { 319 cif.deleteOsFile(); 320 } catch (Exception ex) { 321 logger.error(Messages.getErrorString("CONTITEM.ERROR_0005_COULD_NOT_DELETE_OS_FILE", cif.getCompleteFileName()), ex); } 323 HibernateUtil.makeTransient(cif); 324 getFileVersions().remove(cif); 325 if (latestFile == null || latestFile.getId().equals(cif.getId())) { 326 if (getFileVersions().size() == 0) { 328 this.latestFile = null; 330 } else { 331 this.latestFile = (ContentItemFile) getFileVersions().get(getFileVersions().size() - 1); 333 } 334 } 335 } 336 337 public void removeVersion(String fileId) { 338 Iterator it = getFileVersions().iterator(); 339 ContentItemFile cif; 340 while (it.hasNext()) { 341 cif = (ContentItemFile) it.next(); 342 if (fileId.equalsIgnoreCase(cif.getId())) { 343 removeVersion(cif); 344 break; 345 } 346 } 347 } 348 349 352 public void removeAllVersions() { 353 Iterator it = getFileVersions().iterator(); 354 ContentItemFile cif; 355 while (it.hasNext()) { 356 cif = (ContentItemFile) it.next(); 357 cif.deleteOsFile(); 358 HibernateUtil.makeTransient(cif); 359 } 360 getFileVersions().clear(); 361 this.latestFile = null; 362 } 363 364 367 368 371 public ContentLocation getParent() { 372 return parent; 373 } 374 375 379 public void setParent(ContentLocation theParent) { 380 this.parent = theParent; 381 } 382 383 386 public String getName() { 387 return name; 388 } 389 390 394 public void setName(String fName) { 395 this.name = fName; 396 } 397 398 401 public String getId() { 402 return id; 403 } 404 405 409 public void setId(String id) { 410 this.id = id; 411 } 412 413 416 public String getMimeType() { 417 return mimeType; 418 } 419 420 424 public void setMimeType(String mimeType) { 425 this.mimeType = mimeType; 426 } 427 428 431 public String getUrl() { 432 return url; 433 } 434 435 439 public void setUrl(String url) { 440 this.url = url; 441 } 442 443 446 public String getPath() { 447 return path; 448 } 449 450 454 public void setPath(String path) { 455 this.path = path; 456 } 457 458 461 public List getFileVersions() { 462 return fileVersions; 463 } 464 465 469 public void setFileVersions(List fileVersions) { 470 this.fileVersions = fileVersions; 471 } 472 473 476 public String getTitle() { 477 return title; 478 } 479 480 484 public void setTitle(String title) { 485 this.title = title; 486 } 487 488 491 public Log getLogger() { 492 return logger; 493 } 494 495 496 public String [] getSearchableColumns() { 497 return SearchableColumns; 498 } 499 500 public String getSearchableTable() { 501 return SearchableTable; 502 } 503 504 public String getPhraseSearchQueryName() { 505 return SearchablePhraseNamedQuery; 506 } 507 508 public void makeTransient() { 509 this.removeAllVersions(); 510 HibernateUtil.makeTransient(this); 511 } 512 513 public String toString() { 514 return MessageFormat.format("{0}, {1}, {2}", new String [] {this.getTitle(), this.getPath(), this.getMimeType()}); } 516 517 } 518 | Popular Tags |