001/*
002 * (C) Copyright 2006-2016 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 */
019package org.nuxeo.ecm.platform.pictures.tiles.api.imageresource;
020
021import java.util.Calendar;
022
023import org.nuxeo.ecm.core.api.Blob;
024import org.nuxeo.ecm.core.api.DocumentModel;
025import org.nuxeo.ecm.core.api.PropertyException;
026
027/**
028 * DocumentModel-based implementation of ImageResource. Supports clean digest and modification date to have a clean
029 * invalidation system.
030 *
031 * @author tiry
032 */
033public class DocumentImageResource implements ImageResource {
034
035    private static final long serialVersionUID = 1L;
036
037    protected Blob blob;
038
039    protected String hash;
040
041    protected Calendar modified;
042
043    protected DocumentModel doc;
044
045    protected String xPath;
046
047    /**
048     * @deprecated since 9.1 as filename is now hold by the blob and no longer exist beside it
049     */
050    @Deprecated
051    protected String fileName;
052
053    public DocumentImageResource(DocumentModel doc, String xPath) {
054        this.doc = doc;
055        this.xPath = xPath;
056    }
057
058    protected String getEscapedxPath(String xPath) {
059        String clean = xPath.replace(":", "_");
060        clean = clean.replace("/", "_");
061        clean = clean.replace("[", "");
062        clean = clean.replace("]", "");
063        return clean;
064    }
065
066    protected void compute() throws PropertyException {
067
068        blob = (Blob) doc.getProperty(xPath).getValue();
069        modified = (Calendar) doc.getProperty("dublincore", "modified");
070
071        hash = blob.getDigest();
072        if (hash == null) {
073            hash = doc.getRepositoryName() + "_" + doc.getId() + "_" + getEscapedxPath(xPath);
074            if (modified != null) {
075                hash = hash + "_" + modified.getTimeInMillis();
076            }
077        }
078    }
079
080    public Blob getBlob() {
081        if (blob == null) {
082            compute();
083        }
084        if (fileName != null) {
085            blob.setFilename(fileName);
086        }
087        return blob;
088    }
089
090    public String getHash() {
091        if (hash == null) {
092            compute();
093        }
094        return hash;
095    }
096
097    public Calendar getModificationDate() {
098        if (modified == null) {
099            compute();
100        }
101        return modified;
102    }
103
104    /**
105     * @deprecated since 9.1 as filename is now hold by the blob and no longer exist beside it
106     */
107    @Deprecated
108    public void setFileName(String name) {
109        this.fileName = name;
110    }
111
112}