package com.ffii.core.utils;

import org.springframework.util.ResourceUtils;
import java.io.File;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import java.util.Map;
import java.util.List;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

public class PdfUtils {

    public static JasperReport loadJasperReport(String path) throws Exception {
        File file = ResourceUtils.getFile(path);
        JasperReport reportTemplate = JasperCompileManager.compileReport(file.getAbsolutePath());
        return reportTemplate;
    }

    public static <T> JasperPrint fillReport(JasperReport report, List<T> loopList, Map<String, Object> params)
            throws Exception {
        JRDataSource dataSources = loopList.size() > 0 ? new JRBeanCollectionDataSource(loopList)
                : new JREmptyDataSource();
        JasperPrint jasperPrint = JasperFillManager.fillReport(report, params, dataSources);
        return jasperPrint;
    }

}