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.rendering.RenderingService;
024import org.nuxeo.ecm.core.api.Blob;
025import org.nuxeo.ecm.core.api.Blobs;
026import org.nuxeo.ecm.core.api.DocumentModelList;
027import org.nuxeo.ecm.platform.rendering.api.RenderingException;
028import org.nuxeo.runtime.services.resource.ResourceService;
029
030import freemarker.template.TemplateException;
031
032/**
033 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
034 */
035@Operation(id = RenderDocumentFeed.ID, category = Constants.CAT_CONVERSION, label = "Render Document Feed", description = "Get a list of documents as input and outputs a single blob containing the rendering of the document list. 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 blob")
036public class RenderDocumentFeed {
037
038    public static final String ID = "Render.DocumentFeed";
039
040    @Context
041    protected ResourceService rs;
042
043    @Context
044    protected OperationContext ctx;
045
046    @Param(name = "template", widget = Constants.W_TEMPLATE_RESOURCE)
047    protected String template;
048
049    @Param(name = "type", widget = Constants.W_OPTION, required = false, values = { "ftl", "mvel" })
050    protected String type = "ftl";
051
052    @Param(name = "filename", required = false, values = "output.ftl")
053    protected String name = "output.ftl";
054
055    @Param(name = "mimetype", required = false, values = "text/xml")
056    protected String mimeType = "text/xml";
057
058    @Param(name = "charset", required = false)
059    protected String charset = "UTF-8";
060
061    @OperationMethod
062    public Blob run(DocumentModelList docs) throws OperationException, RenderingException, TemplateException,
063            IOException {
064        String content = RenderingService.getInstance().render(type, template, ctx);
065        return Blobs.createBlob(content, mimeType, charset, name);
066    }
067
068}