001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others.
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-2.1.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 *
014 * Contributors:
015 *     Thomas Roger
016 */
017
018package org.nuxeo.ecm.platform.picture.rendition;
019
020import java.util.Collections;
021import java.util.List;
022
023import org.nuxeo.ecm.core.api.Blob;
024import org.nuxeo.ecm.core.api.DocumentModel;
025import org.nuxeo.ecm.platform.picture.api.adapters.PictureResourceAdapter;
026import org.nuxeo.ecm.platform.rendition.extension.RenditionProvider;
027import org.nuxeo.ecm.platform.rendition.service.RenditionDefinition;
028
029/**
030 * Provides a rendition based on a picture view (referenced through the rendition definition name).
031 *
032 * @since 7.2
033 */
034public class PictureRenditionProvider implements RenditionProvider {
035
036    @Override
037    public boolean isAvailable(DocumentModel doc, RenditionDefinition definition) {
038        PictureResourceAdapter picture = doc.getAdapter(PictureResourceAdapter.class);
039        return picture != null && picture.getPictureFromTitle(definition.getName()) != null;
040    }
041
042    @Override
043    public List<Blob> render(DocumentModel doc, RenditionDefinition definition) {
044        PictureResourceAdapter picture = doc.getAdapter(PictureResourceAdapter.class);
045        if (picture == null) {
046            return Collections.emptyList();
047        }
048
049        Blob blob = picture.getPictureFromTitle(definition.getName());
050        return blob != null ? Collections.singletonList(blob) : Collections.emptyList();
051    }
052}