001/*
002 * (C) Copyright 2010 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 * Contributors:
014 * Nuxeo - initial API and implementation
015 */
016package org.nuxeo.ecm.platform.rendition.adapter;
017
018import java.util.List;
019
020import org.nuxeo.ecm.core.api.DocumentModel;
021import org.nuxeo.ecm.platform.rendition.Renderable;
022import org.nuxeo.ecm.platform.rendition.Rendition;
023import org.nuxeo.ecm.platform.rendition.service.RenditionDefinition;
024import org.nuxeo.ecm.platform.rendition.service.RenditionService;
025import org.nuxeo.runtime.api.Framework;
026
027/**
028 * Default implementation for {@link Renderable} interface.
029 * <p>
030 * This is a simple wrapper around the {@link RenditionService} in the context of a given {@link DocumentModel}
031 *
032 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
033 */
034public class RenderableDocument implements Renderable {
035
036    protected final DocumentModel doc;
037
038    protected List<RenditionDefinition> defs = null;
039
040    public RenderableDocument(DocumentModel doc) {
041        this.doc = doc;
042    }
043
044    @Override
045    public List<RenditionDefinition> getAvailableRenditionDefinitions() {
046        if (defs == null) {
047            defs = Framework.getLocalService(RenditionService.class).getAvailableRenditionDefinitions(doc);
048        }
049        return defs;
050    }
051
052    @Override
053    public Rendition getRenditionByName(String name) {
054        for (RenditionDefinition def : getAvailableRenditionDefinitions()) {
055            if (def.getName().equals(name)) {
056                return getRendition(def);
057            }
058        }
059        return null;
060    }
061
062    @Override
063    public Rendition getRenditionByKind(String kind) {
064        for (RenditionDefinition def : getAvailableRenditionDefinitions()) {
065            if (def.getKind().equals(kind)) {
066                return getRendition(def);
067            }
068        }
069        return null;
070    }
071
072    protected Rendition getRendition(RenditionDefinition def) {
073        return Framework.getLocalService(RenditionService.class).getRendition(doc, def.getName());
074
075    }
076
077}