001/* 002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 * $Id$ 020 * 021 */ 022package org.nuxeo.ecm.platform.pictures.tiles.api.imageresource; 023 024import java.util.Calendar; 025 026import org.nuxeo.ecm.core.api.Blob; 027import org.nuxeo.ecm.core.api.DocumentModel; 028import org.nuxeo.ecm.core.api.PropertyException; 029 030/** 031 * DocumentModel-based implementation of ImageResource. Supports clean digest and modification date to have a clean 032 * invalidation system. 033 * 034 * @author tiry 035 */ 036public class DocumentImageResource implements ImageResource { 037 038 private static final long serialVersionUID = 1L; 039 040 protected Blob blob; 041 042 protected String hash; 043 044 protected Calendar modified; 045 046 protected DocumentModel doc; 047 048 protected String xPath; 049 050 protected String fileName; 051 052 public DocumentImageResource(DocumentModel doc, String xPath) { 053 this.doc = doc; 054 this.xPath = xPath; 055 } 056 057 protected String getEscapedxPath(String xPath) { 058 String clean = xPath.replace(":", "_"); 059 clean = clean.replace("/", "_"); 060 clean = clean.replace("[", ""); 061 clean = clean.replace("]", ""); 062 return clean; 063 } 064 065 protected void compute() throws PropertyException { 066 067 blob = (Blob) doc.getProperty(xPath).getValue(); 068 modified = (Calendar) doc.getProperty("dublincore", "modified"); 069 070 hash = blob.getDigest(); 071 if (hash == null) { 072 hash = doc.getRepositoryName() + "_" + doc.getId() + "_" + getEscapedxPath(xPath); 073 if (modified != null) { 074 hash = hash + "_" + modified.getTimeInMillis(); 075 } 076 } 077 } 078 079 public Blob getBlob() { 080 if (blob == null) { 081 compute(); 082 } 083 if (fileName != null) { 084 blob.setFilename(fileName); 085 } 086 return blob; 087 } 088 089 public String getHash() { 090 if (hash == null) { 091 compute(); 092 } 093 return hash; 094 } 095 096 public Calendar getModificationDate() { 097 if (modified == null) { 098 compute(); 099 } 100 return modified; 101 } 102 103 public void setFileName(String name) { 104 this.fileName = name; 105 } 106 107}