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