001/*
002 * (C) Copyright 2006-2011 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 *     bstefanescu
018 */
019package org.nuxeo.ecm.automation.core.rendering.operations;
020
021import java.io.IOException;
022
023import org.nuxeo.ecm.automation.OperationContext;
024import org.nuxeo.ecm.automation.OperationException;
025import org.nuxeo.ecm.automation.core.Constants;
026import org.nuxeo.ecm.automation.core.annotations.Context;
027import org.nuxeo.ecm.automation.core.annotations.Operation;
028import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
029import org.nuxeo.ecm.automation.core.annotations.Param;
030import org.nuxeo.ecm.automation.core.collectors.BlobCollector;
031import org.nuxeo.ecm.automation.core.rendering.RenderingService;
032import org.nuxeo.ecm.core.api.Blob;
033import org.nuxeo.ecm.core.api.Blobs;
034import org.nuxeo.ecm.core.api.DocumentModel;
035import org.nuxeo.ecm.platform.rendering.api.RenderingException;
036import org.nuxeo.runtime.services.resource.ResourceService;
037
038import freemarker.template.TemplateException;
039
040/**
041 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
042 */
043@Operation(id = RenderDocument.ID, category = Constants.CAT_CONVERSION, label = "Render Document", description = "Get a document or a list of document in input and outputs one or more blobs that contain a rendered view for each input document given a rendering template. The template attribute may contain either the template content either a template URI. Template URis are strings in the form 'template:template_name' and will be located using the runtime resource service. Return the rendered file(s) as blob(s)")
044public class RenderDocument {
045
046    public static final String ID = "Render.Document";
047
048    @Context
049    protected ResourceService rs;
050
051    @Context
052    protected OperationContext ctx;
053
054    @Param(name = "template", widget = Constants.W_TEMPLATE_RESOURCE)
055    protected String template;
056
057    @Param(name = "type", widget = Constants.W_OPTION, required = false, values = { "ftl", "mvel" })
058    protected String type = "ftl";
059
060    @Param(name = "filename", required = false, values = "output.ftl")
061    protected String name = "output.ftl";
062
063    @Param(name = "mimetype", required = false, values = "text/xml")
064    protected String mimeType = "text/xml";
065
066    @OperationMethod(collector = BlobCollector.class)
067    public Blob run(DocumentModel doc) throws OperationException, RenderingException, TemplateException, IOException {
068        String content = RenderingService.getInstance().render(type, template, ctx);
069        return Blobs.createBlob(content, mimeType, null, name);
070    }
071
072}