001/*
002 * (C) Copyright 2012 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 *     Thierry Delprat
018 */
019package org.nuxeo.template.automation;
020
021import org.nuxeo.ecm.automation.OperationContext;
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;
027
028import org.nuxeo.ecm.core.api.Blob;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
031import org.nuxeo.template.adapters.doc.TemplateBindings;
032import org.nuxeo.template.api.adapters.TemplateBasedDocument;
033
034/**
035 * Operation to wrapp the rendition process
036 *
037 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
038 */
039@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.")
040public class RenderWithTemplateOperation {
041
042    public static final String ID = "TemplateProcessor.Render";
043
044    @Context
045    protected OperationContext ctx;
046
047    @Param(name = "templateName", required = false)
048    protected String templateName = TemplateBindings.DEFAULT_BINDING;
049
050    @Param(name = "store", required = false, values = "false")
051    protected Boolean store = false;
052
053    @Param(name = "save", required = false, values = "true")
054    protected Boolean save = true;
055
056    @OperationMethod
057    public Blob run(DocumentModel targetDocument) {
058        TemplateBasedDocument renderable = targetDocument.getAdapter(TemplateBasedDocument.class);
059        if (renderable != null) {
060            if (store) {
061                return renderable.renderAndStoreAsAttachment(templateName, save);
062            } else {
063                return renderable.renderWithTemplate(templateName);
064            }
065        } else {
066            BlobHolder bh = targetDocument.getAdapter(BlobHolder.class);
067            if (bh != null) {
068                return bh.getBlob();
069            } else {
070                return null;
071            }
072        }
073    }
074}