001/* 002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo - initial API and implementation 016 * 017 * $Id$ 018 * 019 */ 020package org.nuxeo.ecm.platform.pictures.tiles.api.imageresource; 021 022import java.util.Calendar; 023 024import org.nuxeo.ecm.core.api.Blob; 025import org.nuxeo.ecm.core.api.DocumentModel; 026import org.nuxeo.ecm.core.api.PropertyException; 027 028/** 029 * DocumentModel-based implementation of ImageResource. Supports clean digest and modification date to have a clean 030 * invalidation system. 031 * 032 * @author tiry 033 */ 034public class DocumentImageResource implements ImageResource { 035 036 private static final long serialVersionUID = 1L; 037 038 protected Blob blob; 039 040 protected String hash; 041 042 protected Calendar modified; 043 044 protected DocumentModel doc; 045 046 protected String xPath; 047 048 protected String fileName; 049 050 public DocumentImageResource(DocumentModel doc, String xPath) { 051 this.doc = doc; 052 this.xPath = xPath; 053 } 054 055 protected String getEscapedxPath(String xPath) { 056 String clean = xPath.replace(":", "_"); 057 clean = clean.replace("/", "_"); 058 clean = clean.replace("[", ""); 059 clean = clean.replace("]", ""); 060 return clean; 061 } 062 063 protected void compute() throws PropertyException { 064 065 blob = (Blob) doc.getProperty(xPath).getValue(); 066 modified = (Calendar) doc.getProperty("dublincore", "modified"); 067 068 hash = blob.getDigest(); 069 if (hash == null) { 070 hash = doc.getRepositoryName() + "_" + doc.getId() + "_" + getEscapedxPath(xPath); 071 if (modified != null) { 072 hash = hash + "_" + modified.getTimeInMillis(); 073 } 074 } 075 } 076 077 public Blob getBlob() { 078 if (blob == null) { 079 compute(); 080 } 081 if (fileName != null) { 082 blob.setFilename(fileName); 083 } 084 return blob; 085 } 086 087 public String getHash() { 088 if (hash == null) { 089 compute(); 090 } 091 return hash; 092 } 093 094 public Calendar getModificationDate() { 095 if (modified == null) { 096 compute(); 097 } 098 return modified; 099 } 100 101 public void setFileName(String name) { 102 this.fileName = name; 103 } 104 105}