001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 */
012package org.nuxeo.ecm.automation.core.rendering.operations;
013
014import java.io.IOException;
015
016import org.nuxeo.ecm.automation.OperationContext;
017import org.nuxeo.ecm.automation.OperationException;
018import org.nuxeo.ecm.automation.core.Constants;
019import org.nuxeo.ecm.automation.core.annotations.Context;
020import org.nuxeo.ecm.automation.core.annotations.Operation;
021import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
022import org.nuxeo.ecm.automation.core.annotations.Param;
023import org.nuxeo.ecm.automation.core.collectors.BlobCollector;
024import org.nuxeo.ecm.automation.core.rendering.RenderingService;
025import org.nuxeo.ecm.core.api.Blob;
026import org.nuxeo.ecm.core.api.Blobs;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.platform.rendering.api.RenderingException;
029import org.nuxeo.runtime.services.resource.ResourceService;
030
031import freemarker.template.TemplateException;
032
033/**
034 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
035 */
036@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)")
037public class RenderDocument {
038
039    public static final String ID = "Render.Document";
040
041    @Context
042    protected ResourceService rs;
043
044    @Context
045    protected OperationContext ctx;
046
047    @Param(name = "template", widget = Constants.W_TEMPLATE_RESOURCE)
048    protected String template;
049
050    @Param(name = "type", widget = Constants.W_OPTION, required = false, values = { "ftl", "mvel" })
051    protected String type = "ftl";
052
053    @Param(name = "filename", required = false, values = "output.ftl")
054    protected String name = "output.ftl";
055
056    @Param(name = "mimetype", required = false, values = "text/xml")
057    protected String mimeType = "text/xml";
058
059    @OperationMethod(collector = BlobCollector.class)
060    public Blob run(DocumentModel doc) throws OperationException, RenderingException, TemplateException, IOException {
061        String content = RenderingService.getInstance().render(type, template, ctx);
062        return Blobs.createBlob(content, mimeType, null, name);
063    }
064
065}