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