package net.sf.jasperreports.engine.fill;

import java.sql.Connection;
import java.util.Map;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;

public abstract class JRFiller {
  public static JasperPrint fillReport(JasperReport jasperReport, Map parameters, Connection conn) throws JRException {
    JRBaseFiller filler = createFiller(jasperReport);
    JasperPrint jasperPrint = null;
    try {
      jasperPrint = filler.fill(parameters, conn);
    } catch (JRFillInterruptedException e) {
      throw new JRException("The report filling thread was interrupted.");
    } 
    return jasperPrint;
  }
  
  public static JasperPrint fillReport(JasperReport jasperReport, Map parameters, JRDataSource dataSource) throws JRException {
    JRBaseFiller filler = createFiller(jasperReport);
    JasperPrint jasperPrint = null;
    try {
      jasperPrint = filler.fill(parameters, dataSource);
    } catch (JRFillInterruptedException e) {
      throw new JRException("The report filling thread was interrupted.");
    } 
    return jasperPrint;
  }
  
  public static JasperPrint fillReport(JasperReport jasperReport, Map parameters) throws JRException {
    JRBaseFiller filler = createFiller(jasperReport);
    try {
      JasperPrint jasperPrint = filler.fill(parameters);
      return jasperPrint;
    } catch (JRFillInterruptedException e) {
      throw new JRException("The report filling thread was interrupted.");
    } 
  }
  
  public static JRBaseFiller createFiller(JasperReport jasperReport) throws JRException {
    JRBaseFiller filler = null;
    switch (jasperReport.getPrintOrder()) {
      case 2:
        filler = new JRHorizontalFiller(jasperReport);
        break;
      case 1:
        filler = new JRVerticalFiller(jasperReport);
        break;
    } 
    return filler;
  }
}
