001package org.nuxeo.template.automation;
002
003import org.nuxeo.ecm.automation.OperationContext;
004import org.nuxeo.ecm.automation.core.Constants;
005import org.nuxeo.ecm.automation.core.annotations.Context;
006import org.nuxeo.ecm.automation.core.annotations.Operation;
007import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
008import org.nuxeo.ecm.automation.core.annotations.Param;
009
010import org.nuxeo.ecm.core.api.Blob;
011import org.nuxeo.ecm.core.api.DocumentModel;
012import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
013import org.nuxeo.template.adapters.doc.TemplateBindings;
014import org.nuxeo.template.api.adapters.TemplateBasedDocument;
015
016/**
017 * Operation to wrapp the rendition process
018 *
019 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
020 */
021@Operation(id = RenderWithTemplateOperation.ID, category = Constants.CAT_CONVERSION, label = "Render with template", description = "Render the target document with the associated template if any. Returns the rendered Blob or the main Blob if no template is associated to the document.")
022public class RenderWithTemplateOperation {
023
024    public static final String ID = "TemplateProcessor.Render";
025
026    @Context
027    protected OperationContext ctx;
028
029    @Param(name = "templateName", required = false)
030    protected String templateName = TemplateBindings.DEFAULT_BINDING;
031
032    @Param(name = "store", required = false, values = "false")
033    protected Boolean store = false;
034
035    @Param(name = "save", required = false, values = "true")
036    protected Boolean save = true;
037
038    @OperationMethod
039    public Blob run(DocumentModel targetDocument) {
040        TemplateBasedDocument renderable = targetDocument.getAdapter(TemplateBasedDocument.class);
041        if (renderable != null) {
042            if (store) {
043                return renderable.renderAndStoreAsAttachment(templateName, save);
044            } else {
045                return renderable.renderWithTemplate(templateName);
046            }
047        } else {
048            BlobHolder bh = targetDocument.getAdapter(BlobHolder.class);
049            if (bh != null) {
050                return bh.getBlob();
051            } else {
052                return null;
053            }
054        }
055    }
056}