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.dom4j.DocumentException;
022import org.nuxeo.ecm.automation.OperationContext;
023import org.nuxeo.ecm.automation.core.Constants;
024import org.nuxeo.ecm.automation.core.annotations.Context;
025import org.nuxeo.ecm.automation.core.annotations.Operation;
026import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
027import org.nuxeo.ecm.automation.core.annotations.Param;
028
029import org.nuxeo.ecm.core.api.Blob;
030import org.nuxeo.ecm.core.api.DocumentModel;
031import org.nuxeo.ecm.core.api.NuxeoException;
032import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
033import org.nuxeo.runtime.api.Framework;
034import org.nuxeo.template.XMLSerializer;
035import org.nuxeo.template.adapters.doc.TemplateBindings;
036import org.nuxeo.template.api.TemplateInput;
037import org.nuxeo.template.api.TemplateProcessorService;
038import org.nuxeo.template.api.adapters.TemplateBasedDocument;
039
040import java.util.List;
041
042/**
043 * Operation to wrapp the rendition process
044 *
045 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
046 */
047@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.")
048public class RenderWithTemplateOperation {
049
050    public static final String ID = "TemplateProcessor.Render";
051
052    @Context
053    protected OperationContext ctx;
054
055    @Param(name = "templateName", required = false)
056    protected String templateName = TemplateBindings.DEFAULT_BINDING;
057
058    @Param(name = "store", required = false, values = "false")
059    protected Boolean store = false;
060
061    @Param(name = "save", required = false, values = "true")
062    protected Boolean save = true;
063
064    @Param(name = "attach", required = false)
065    protected Boolean attach = false;
066
067    @Param(name = "templateData", required = false)
068    protected String templateData = null;
069
070    @OperationMethod
071    public Blob run(DocumentModel targetDocument) {
072        TemplateBasedDocument renderable = targetDocument.getAdapter(TemplateBasedDocument.class);
073        if (attach && (renderable == null || !renderable.getTemplateNames().contains(templateName))) {
074            TemplateProcessorService tps = Framework.getService(TemplateProcessorService.class);
075            DocumentModel template = tps.getTemplateDoc(ctx.getCoreSession(), templateName);
076            renderable = (template == null) ? null :
077                tps.makeTemplateBasedDocument(targetDocument, template, true)
078                    .getAdapter(TemplateBasedDocument.class);
079        }
080        if (renderable != null) {
081            if (templateData != null) {
082                List<TemplateInput> params;
083                try {
084                    params = XMLSerializer.readFromXml(templateData);
085                } catch (DocumentException e) {
086                    throw new NuxeoException(e.getMessage(), e);
087                }
088                if (params != null) {
089                    renderable.saveParams(templateName, params, true);
090                }
091            }
092            if (store) {
093                return renderable.renderAndStoreAsAttachment(templateName, save);
094            } else {
095                return renderable.renderWithTemplate(templateName);
096            }
097        } else {
098            BlobHolder bh = targetDocument.getAdapter(BlobHolder.class);
099            if (bh != null) {
100                return bh.getBlob();
101            } else {
102                return null;
103            }
104        }
105    }
106}