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 *     Tiago Cardoso <tcardoso@nuxeo.com>
018 */
019package org.nuxeo.ecm.platform.threed.rendition;
020
021import org.nuxeo.ecm.core.api.Blob;
022import org.nuxeo.ecm.core.api.DocumentModel;
023import org.nuxeo.ecm.platform.rendition.extension.RenditionProvider;
024import org.nuxeo.ecm.platform.rendition.service.RenditionDefinition;
025import org.nuxeo.ecm.platform.threed.ThreeDDocument;
026import org.nuxeo.ecm.platform.threed.ThreeDRenderView;
027
028import java.util.Collections;
029import java.util.List;
030
031/**
032 * Provides a rendition based on a render view (referenced through the rendition definition name).
033 *
034 * @since 8.4
035 */
036public class RenderViewRenditionProvider implements RenditionProvider {
037    @Override
038    public boolean isAvailable(DocumentModel documentModel, RenditionDefinition renditionDefinition) {
039        ThreeDDocument threeDDocument = documentModel.getAdapter(ThreeDDocument.class);
040        return threeDDocument != null && threeDDocument.getRenderView(renditionDefinition.getName()) != null;
041    }
042
043    @Override
044    public List<Blob> render(DocumentModel documentModel, RenditionDefinition renditionDefinition) {
045        ThreeDDocument threeDDocument = documentModel.getAdapter(ThreeDDocument.class);
046        if (threeDDocument == null) {
047            return Collections.emptyList();
048        }
049        ThreeDRenderView renderView = threeDDocument.getRenderView(renditionDefinition.getName());
050        return (renderView.getContent() != null) ? Collections.singletonList(renderView.getContent())
051                : Collections.emptyList();
052    }
053}