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.rendition.operation;
019
020import org.nuxeo.ecm.automation.core.Constants;
021import org.nuxeo.ecm.automation.core.annotations.Context;
022import org.nuxeo.ecm.automation.core.annotations.Operation;
023import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
024import org.nuxeo.ecm.automation.core.annotations.Param;
025import org.nuxeo.ecm.automation.core.collectors.BlobCollector;
026import org.nuxeo.ecm.core.api.Blob;
027import org.nuxeo.ecm.core.api.Blobs;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.platform.rendition.Rendition;
030import org.nuxeo.ecm.platform.rendition.service.RenditionService;
031
032/**
033 * Returns a document rendition given its name.
034 *
035 * @since 7.3
036 */
037@Operation(id = GetRendition.ID, category = Constants.CAT_BLOB, label = "Gets a document rendition", description = "Gets a document rendition given its name. Returns the rendition blob.")
038public class GetRendition {
039
040    public static final String ID = "Document.GetRendition";
041
042    @Context
043    protected RenditionService renditionService;
044
045    @Param(name = "renditionName")
046    protected String renditionName;
047
048    @OperationMethod(collector = BlobCollector.class)
049    public Blob run(DocumentModel doc) {
050        Rendition rendition = renditionService.getRendition(doc, renditionName);
051        Blob blob = rendition.getBlob();
052
053        // cannot return null since it may break the next operation
054        if (blob == null) { // create an empty blob
055            blob = Blobs.createBlob("");
056            blob.setFilename(doc.getName() + ".null");
057        }
058        return blob;
059    }
060}