package net.sf.jasperreports.engine;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.util.Map;
import net.sf.jasperreports.engine.fill.JRFiller;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.engine.util.JRSaver;

public class JasperFillManager {
  public static String fillReportToFile(String sourceFileName, Map parameters, Connection connection) throws JRException {
    File sourceFile = new File(sourceFileName);
    JasperReport jasperReport = (JasperReport)JRLoader.loadObject(sourceFile);
    File destFile = new File(sourceFile.getParent(), jasperReport.getName() + ".jrprint");
    String destFileName = destFile.toString();
    fillReportToFile(jasperReport, destFileName, parameters, connection);
    return destFileName;
  }
  
  public static String fillReportToFile(String sourceFileName, Map parameters) throws JRException {
    File sourceFile = new File(sourceFileName);
    JasperReport jasperReport = (JasperReport)JRLoader.loadObject(sourceFile);
    File destFile = new File(sourceFile.getParent(), jasperReport.getName() + ".jrprint");
    String destFileName = destFile.toString();
    fillReportToFile(jasperReport, destFileName, parameters);
    return destFileName;
  }
  
  public static void fillReportToFile(String sourceFileName, String destFileName, Map parameters, Connection connection) throws JRException {
    JasperReport jasperReport = (JasperReport)JRLoader.loadObject(sourceFileName);
    fillReportToFile(jasperReport, destFileName, parameters, connection);
  }
  
  public static void fillReportToFile(String sourceFileName, String destFileName, Map parameters) throws JRException {
    JasperReport jasperReport = (JasperReport)JRLoader.loadObject(sourceFileName);
    fillReportToFile(jasperReport, destFileName, parameters);
  }
  
  public static void fillReportToFile(JasperReport jasperReport, String destFileName, Map parameters, Connection connection) throws JRException {
    JasperPrint jasperPrint = fillReport(jasperReport, parameters, connection);
    JRSaver.saveObject(jasperPrint, destFileName);
  }
  
  public static void fillReportToFile(JasperReport jasperReport, String destFileName, Map parameters) throws JRException {
    JasperPrint jasperPrint = fillReport(jasperReport, parameters);
    JRSaver.saveObject(jasperPrint, destFileName);
  }
  
  public static JasperPrint fillReport(String sourceFileName, Map parameters, Connection connection) throws JRException {
    File sourceFile = new File(sourceFileName);
    JasperReport jasperReport = (JasperReport)JRLoader.loadObject(sourceFile);
    return fillReport(jasperReport, parameters, connection);
  }
  
  public static JasperPrint fillReport(String sourceFileName, Map parameters) throws JRException {
    File sourceFile = new File(sourceFileName);
    JasperReport jasperReport = (JasperReport)JRLoader.loadObject(sourceFile);
    return fillReport(jasperReport, parameters);
  }
  
  public static void fillReportToStream(InputStream inputStream, OutputStream outputStream, Map parameters, Connection connection) throws JRException {
    JasperReport jasperReport = (JasperReport)JRLoader.loadObject(inputStream);
    fillReportToStream(jasperReport, outputStream, parameters, connection);
  }
  
  public static void fillReportToStream(InputStream inputStream, OutputStream outputStream, Map parameters) throws JRException {
    JasperReport jasperReport = (JasperReport)JRLoader.loadObject(inputStream);
    fillReportToStream(jasperReport, outputStream, parameters);
  }
  
  public static void fillReportToStream(JasperReport jasperReport, OutputStream outputStream, Map parameters, Connection connection) throws JRException {
    JasperPrint jasperPrint = fillReport(jasperReport, parameters, connection);
    JRSaver.saveObject(jasperPrint, outputStream);
  }
  
  public static void fillReportToStream(JasperReport jasperReport, OutputStream outputStream, Map parameters) throws JRException {
    JasperPrint jasperPrint = fillReport(jasperReport, parameters);
    JRSaver.saveObject(jasperPrint, outputStream);
  }
  
  public static JasperPrint fillReport(InputStream inputStream, Map parameters, Connection connection) throws JRException {
    JasperReport jasperReport = (JasperReport)JRLoader.loadObject(inputStream);
    return fillReport(jasperReport, parameters, connection);
  }
  
  public static JasperPrint fillReport(InputStream inputStream, Map parameters) throws JRException {
    JasperReport jasperReport = (JasperReport)JRLoader.loadObject(inputStream);
    return fillReport(jasperReport, parameters);
  }
  
  public static JasperPrint fillReport(JasperReport jasperReport, Map parameters, Connection connection) throws JRException {
    return JRFiller.fillReport(jasperReport, parameters, connection);
  }
  
  public static JasperPrint fillReport(JasperReport jasperReport, Map parameters) throws JRException {
    return JRFiller.fillReport(jasperReport, parameters);
  }
  
  public static String fillReportToFile(String sourceFileName, Map parameters, JRDataSource dataSource) throws JRException {
    File sourceFile = new File(sourceFileName);
    JasperReport jasperReport = (JasperReport)JRLoader.loadObject(sourceFile);
    File destFile = new File(sourceFile.getParent(), jasperReport.getName() + ".jrprint");
    String destFileName = destFile.toString();
    fillReportToFile(jasperReport, destFileName, parameters, dataSource);
    return destFileName;
  }
  
  public static void fillReportToFile(String sourceFileName, String destFileName, Map parameters, JRDataSource dataSource) throws JRException {
    JasperReport jasperReport = (JasperReport)JRLoader.loadObject(sourceFileName);
    fillReportToFile(jasperReport, destFileName, parameters, dataSource);
  }
  
  public static void fillReportToFile(JasperReport jasperReport, String destFileName, Map parameters, JRDataSource dataSource) throws JRException {
    JasperPrint jasperPrint = fillReport(jasperReport, parameters, dataSource);
    JRSaver.saveObject(jasperPrint, destFileName);
  }
  
  public static JasperPrint fillReport(String sourceFileName, Map parameters, JRDataSource dataSource) throws JRException {
    File sourceFile = new File(sourceFileName);
    JasperReport jasperReport = (JasperReport)JRLoader.loadObject(sourceFile);
    return fillReport(jasperReport, parameters, dataSource);
  }
  
  public static void fillReportToStream(InputStream inputStream, OutputStream outputStream, Map parameters, JRDataSource dataSource) throws JRException {
    JasperReport jasperReport = (JasperReport)JRLoader.loadObject(inputStream);
    fillReportToStream(jasperReport, outputStream, parameters, dataSource);
  }
  
  public static void fillReportToStream(JasperReport jasperReport, OutputStream outputStream, Map parameters, JRDataSource dataSource) throws JRException {
    JasperPrint jasperPrint = fillReport(jasperReport, parameters, dataSource);
    JRSaver.saveObject(jasperPrint, outputStream);
  }
  
  public static JasperPrint fillReport(InputStream inputStream, Map parameters, JRDataSource dataSource) throws JRException {
    JasperReport jasperReport = (JasperReport)JRLoader.loadObject(inputStream);
    return fillReport(jasperReport, parameters, dataSource);
  }
  
  public static JasperPrint fillReport(JasperReport jasperReport, Map parameters, JRDataSource dataSource) throws JRException {
    return JRFiller.fillReport(jasperReport, parameters, dataSource);
  }
}
