All Downloads are FREE. Search and download functionalities are using the official Maven repository.

act.view.excel.ExcelTemplate Maven / Gradle / Ivy

There is a newer version: 1.9.2
Show newest version
package act.view.excel;

/*-
 * #%L
 * ACT Excel
 * %%
 * Copyright (C) 2015 - 2017 ActFramework
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import act.Act;
import act.app.ActionContext;
import act.view.TemplateBase;
import org.jxls.common.Context;
import org.jxls.expression.JexlExpressionEvaluator;
import org.jxls.transform.Transformer;
import org.jxls.util.JxlsHelper;
import org.jxls.util.TransformerFactory;
import org.osgl.$;
import org.osgl.http.H;
import org.osgl.mvc.result.ServerError;
import org.osgl.util.E;
import org.osgl.util.IO;
import org.osgl.util.S;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Map;

class ExcelTemplate extends TemplateBase {

    private URL resource;

    ExcelTemplate(URL url) {
        this.resource = $.notNull(url);
    }

    /**
     * Setup the content-type and content-disposition
     */
    @Override
    protected void beforeRender(ActionContext context) {
        H.Format format = context.accept();
        String contentType = format.contentType();
        H.Response response = context.resp();
        response.contentType(contentType);

        if(!response.containsHeader("content-disposition")) {
            String name = context.paramVal("filename");
            if (S.blank(name)) {
                name = S.afterLast(context.actionPath(), ".");
                name = S.fmt("%s.%s", name, format.name());
            }
            response.contentDisposition(name, false);
        }
    }

    @Override
    protected void merge(Map renderArgs, H.Response response) {
        Context context = new Context(renderArgs);
        InputStream is = IO.is(resource);
        try {
            Transformer transformer = TransformerFactory.createTransformer(is, response.outputStream());
            JexlExpressionEvaluator evaluator = (JexlExpressionEvaluator)transformer.getTransformationConfig().getExpressionEvaluator();
            JexlFunctionManager funcMgr = Act.getInstance(JexlFunctionManager.class);
            evaluator.getJexlEngine().setFunctions(funcMgr.functions());
            JxlsHelper.getInstance().processTemplate(context, transformer);
        } catch (IOException e) {
            throw new ServerError(e, "Error processing excel template: %s", resource.getPath());
        }
    }

    @Override
    protected String render(Map map) {
        throw E.unsupport();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy