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.rendering.RenderingService;
031import org.nuxeo.ecm.core.api.Blob;
032import org.nuxeo.ecm.core.api.Blobs;
033import org.nuxeo.ecm.core.api.DocumentModelList;
034import org.nuxeo.ecm.platform.rendering.api.RenderingException;
035import org.nuxeo.runtime.services.resource.ResourceService;
036
037import freemarker.template.TemplateException;
038
039/**
040 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
041 */
042@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")
043public class RenderDocumentFeed {
044
045    public static final String ID = "Render.DocumentFeed";
046
047    @Context
048    protected ResourceService rs;
049
050    @Context
051    protected OperationContext ctx;
052
053    @Param(name = "template", widget = Constants.W_TEMPLATE_RESOURCE)
054    protected String template;
055
056    @Param(name = "type", widget = Constants.W_OPTION, required = false, values = { "ftl", "mvel" })
057    protected String type = "ftl";
058
059    @Param(name = "filename", required = false, values = "output.ftl")
060    protected String name = "output.ftl";
061
062    @Param(name = "mimetype", required = false, values = "text/xml")
063    protected String mimeType = "text/xml";
064
065    @Param(name = "charset", required = false)
066    protected String charset = "UTF-8";
067
068    @OperationMethod
069    public Blob run(DocumentModelList docs) throws OperationException, RenderingException, TemplateException,
070            IOException {
071        String content = RenderingService.getInstance().render(type, template, ctx);
072        return Blobs.createBlob(content, mimeType, charset, name);
073    }
074
075}