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 *     Estelle Giuly <egiuly@nuxeo.com>
019 */
020package org.nuxeo.template.rendition;
021
022import java.util.ArrayList;
023import java.util.List;
024
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027import org.nuxeo.ecm.core.api.Blob;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.platform.rendition.extension.RenditionProvider;
030import org.nuxeo.ecm.platform.rendition.service.RenditionDefinition;
031import org.nuxeo.template.api.adapters.TemplateBasedDocument;
032
033public class TemplateBasedRenditionProvider implements RenditionProvider {
034
035    protected static Log log = LogFactory.getLog(TemplateBasedRenditionProvider.class);
036
037    @Override
038    public boolean isAvailable(DocumentModel doc, RenditionDefinition def) {
039        TemplateBasedDocument tbd = doc.getAdapter(TemplateBasedDocument.class);
040        if (tbd != null) {
041            // check if some template has been bound to a rendition
042            String template = tbd.getTemplateNameForRendition(def.getName());
043            return template == null ? false : true;
044        }
045        return false;
046    }
047
048    @Override
049    public List<Blob> render(DocumentModel doc, RenditionDefinition definition) {
050        TemplateBasedDocument tbd = doc.getAdapter(TemplateBasedDocument.class);
051        String template = tbd.getTemplateNameForRendition(definition.getName());
052        List<Blob> blobs = new ArrayList<>();
053        if (template != null) {
054            Blob rendered = tbd.renderWithTemplate(template);
055            blobs.add(rendered);
056        }
057        return blobs;
058    }
059
060}