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

tools.dynamia.zk.reports.ReportDownload Maven / Gradle / Ivy

There is a newer version: 4.2.0
Show newest version
/*
 * Copyright (C) 2009 - 2019 Dynamia Soluciones IT S.A.S - NIT 900302344-1
 * Colombia - South America
 * All Rights Reserved.
 *
 * DynamiaTools is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License (LGPL v3) as
 * published by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * DynamiaTools is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with DynamiaTools.  If not, see .
 */
package tools.dynamia.zk.reports;

import org.zkoss.zul.Filedownload;
import tools.dynamia.reports.*;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class ReportDownload {

    public static void save(ReportDescriptor reportDescriptor) {
        Report report = ReportFiller.fill(reportDescriptor);
        save(report, reportDescriptor.getName(), reportDescriptor.getDefaultOutputType(), reportDescriptor.getExporterParameters());
    }

    public static void save(Report report) {
        save(report, report.getDefaultOutputType());
    }

    public static void save(Report report, ReportOutputType ouputType) {
        save(report, "export", ouputType);
    }

    public static void save(Report report, String fileName, ReportOutputType outputType) {
        save(report, fileName, outputType, null);
    }

    public static void save(Report report, String fileName, ReportOutputType outputType, Map exporterParams) {
        List reports = Arrays.asList(report);
        save(reports, fileName, outputType, exporterParams);
    }

    public static void save(List reports, String fileName, ReportOutputType outputType) {
        save(reports, fileName, outputType, null);
    }

    public static void save(List reports, String fileName, ReportOutputType outputType, Map exporterParams) {
        try {
            File reportFile = createFile(fileName, outputType);
            ReportExporter.export(reports, reportFile, outputType, exporterParams);
            Filedownload.save(reportFile, outputType.getContentType());
        } catch (Exception e) {
            throw new ReportExporterException(e);
        }
    }

    private static File createFile(String fileName, ReportOutputType ouputType) throws IOException {

        String prefix = "export" + System.currentTimeMillis();
        if (fileName != null) {
            prefix = fileName + "_" + System.currentTimeMillis();
        }
        String suffix = "." + ouputType.getExtension();

        return File.createTempFile(prefix, suffix);
    }

    private ReportDownload() {
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy