001/*
002 * (C) Copyright 2012 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 *     Thierry Delprat
018 */
019package org.nuxeo.template.context;
020
021import java.util.List;
022
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025import org.nuxeo.ecm.core.api.Blob;
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
028import org.nuxeo.ecm.core.io.download.DownloadService;
029import org.nuxeo.runtime.api.Framework;
030
031/**
032 * Class helper used to expose Document as a {@link BlobHolder} in FreeMarker context
033 *
034 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
035 */
036public class BlobHolderWrapper {
037
038    protected final BlobHolder bh;
039
040    protected final DocumentModel doc;
041
042    protected static final Log log = LogFactory.getLog(BlobHolderWrapper.class);
043
044    public BlobHolderWrapper(DocumentModel doc) {
045        bh = doc.getAdapter(BlobHolder.class);
046        this.doc = doc;
047    }
048
049    protected static String getContextPathProperty() {
050        return Framework.getProperty("org.nuxeo.ecm.contextPath", "/nuxeo");
051    }
052
053    public Blob getBlob() {
054        if (bh == null) {
055            return null;
056        }
057        return bh.getBlob();
058    }
059
060    public List<Blob> getBlobs() {
061        if (bh == null) {
062            return null;
063        }
064        return bh.getBlobs();
065    }
066
067    public Blob getBlob(String name) {
068
069        List<Blob> blobs = getBlobs();
070        if (blobs == null) {
071            return null;
072        }
073        for (Blob blob : blobs) {
074            if (name.equals(blob.getFilename())) {
075                return blob;
076            }
077        }
078        return null;
079    }
080
081    public Blob getBlob(int index) {
082
083        List<Blob> blobs = getBlobs();
084        if (blobs == null) {
085            return null;
086        }
087        if (index >= blobs.size()) {
088            return null;
089        }
090        return blobs.get(index);
091    }
092
093    public String getBlobUrl(int index) {
094
095        List<Blob> blobs = getBlobs();
096        if (blobs == null) {
097            return null;
098        }
099        if (index >= blobs.size()) {
100            return null;
101        }
102
103        DownloadService downloadService = Framework.getService(DownloadService.class);
104        String xpath = DownloadService.BLOBHOLDER_PREFIX + index;
105        String filename = blobs.get(index).getFilename();
106        return getContextPathProperty() + "/" + downloadService.getDownloadUrl(doc, xpath, filename) + "?inline=true";
107    }
108
109    public String getBlobUrl(String name) {
110
111        List<Blob> blobs = getBlobs();
112        if (blobs == null) {
113            return null;
114        }
115        for (int index = 0; index < blobs.size(); index++) {
116            if (name.equals(blobs.get(index).getFilename())) {
117                return getBlobUrl(index);
118            }
119        }
120        return null;
121    }
122
123}