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