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