// Path: src/main/java/com/github/marcelothebuilder/webpedidos/util/report/EmissorRelatorio.java
// public interface EmissorRelatorio {
// 
// 	void setConnection(Connection connection);
// 
// 	void emitir() throws IOException, JRException;
// 
// 	boolean isRelatorioGerado();
// 
// }
// 
// Path: src/main/java/com/github/marcelothebuilder/webpedidos/util/report/Relatorio.java
// public interface Relatorio {
// 
// 	Map<String, Object> getParametros();
// 
// 	RelatorioFile getArquivo();
// }
// 
// Path: src/main/java/com/github/marcelothebuilder/webpedidos/util/report/RelatorioNaoExisteException.java
// public class RelatorioNaoExisteException extends RuntimeException {
// 	private static final long serialVersionUID = 1L;
// 
// 	public RelatorioNaoExisteException() {}
// 
// 	/**
// 	 * @param message
// 	 */
// 	public RelatorioNaoExisteException(String message) {
// 		super(message);
// 	}
// 
// 	/**
// 	 * @param cause
// 	 */
// 	public RelatorioNaoExisteException(Throwable cause) {
// 		super(cause);
// 	}
// 
// 	/**
// 	 * @param message
// 	 * @param cause
// 	 */
// 	public RelatorioNaoExisteException(String message, Throwable cause) {
// 		super(message, cause);
// 	}
// 
// 	/**
// 	 * @param message
// 	 * @param cause
// 	 * @param enableSuppression
// 	 * @param writableStackTrace
// 	 */
// 	public RelatorioNaoExisteException(String message, Throwable cause, boolean enableSuppression,
// 			boolean writableStackTrace) {
// 		super(message, cause, enableSuppression, writableStackTrace);
// 	}
// 
// 	public RelatorioNaoExisteException(Relatorio relatorio, Throwable cause) {
// 		this(String.format("Não foi possível encontrar o relatório %s", relatorio.getArquivo().getCaminho()), cause);
// 	}
// 
// }


import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import javax.servlet.http.HttpServletResponse;
import com.github.marcelothebuilder.webpedidos.util.report.EmissorRelatorio;
import com.github.marcelothebuilder.webpedidos.util.report.Relatorio;
import com.github.marcelothebuilder.webpedidos.util.report.RelatorioNaoExisteException;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
/**
 * 
 */
package com.github.marcelothebuilder.webpedidos.util.report.impl;





/**
 * @author Marcelo Paixao Resende
 *
 */
public final class EmissorRelatorioServlet implements EmissorRelatorio {
	private Relatorio relatorio;
	private Connection connection;
	private HttpServletResponse servletResponse;
	private boolean relatorioGerado = false;

	public EmissorRelatorioServlet(Relatorio relatorio, HttpServletResponse servletResponse) {
		this.relatorio = relatorio;
		this.servletResponse = servletResponse;
	}

	public EmissorRelatorioServlet(Relatorio relatorio, HttpServletResponse output, Connection connection) {
		this(relatorio, output);
		this.connection = connection;
	}

	/* (non-Javadoc)
	 * @see com.github.marcelothebuilder.webpedidos.util.report.impl.EmissorRelatorio#setConnection(java.sql.Connection)
	 */
	@Override
	public void setConnection(Connection connection) {
		this.connection = connection;
	}

	/* (non-Javadoc)
	 * @see com.github.marcelothebuilder.webpedidos.util.report.impl.EmissorRelatorio#emitir()
	 */
	@Override
	public void emitir() throws JRException {
		try (InputStream relatorioArquivoStream = this.getClass().getResourceAsStream(relatorio.getArquivo().getCaminho())) {
			JasperPrint print = JasperFillManager.fillReport(relatorioArquivoStream, relatorio.getParametros(), connection);
			this.relatorioGerado = print.getPages().size() > 0;
			
			if(this.isRelatorioGerado()) {
				String attachment = String.format("attachment; filename=\"%s.pdf\"", relatorio.getArquivo().getNome());
				servletResponse.setHeader("Content-Disposition", attachment);
				JasperExportManager.exportReportToPdfStream(print, servletResponse.getOutputStream());		
			}
		} catch (IOException e) {
// Path: src/main/java/com/github/marcelothebuilder/webpedidos/util/report/EmissorRelatorio.java
// public interface EmissorRelatorio {
// 
// 	void setConnection(Connection connection);
// 
// 	void emitir() throws IOException, JRException;
// 
// 	boolean isRelatorioGerado();
// 
// }
// 
// Path: src/main/java/com/github/marcelothebuilder/webpedidos/util/report/Relatorio.java
// public interface Relatorio {
// 
// 	Map<String, Object> getParametros();
// 
// 	RelatorioFile getArquivo();
// }
// 
// Path: src/main/java/com/github/marcelothebuilder/webpedidos/util/report/RelatorioNaoExisteException.java
// public class RelatorioNaoExisteException extends RuntimeException {
// 	private static final long serialVersionUID = 1L;
// 
// 	public RelatorioNaoExisteException() {}
// 
// 	/**
// 	 * @param message
// 	 */
// 	public RelatorioNaoExisteException(String message) {
// 		super(message);
// 	}
// 
// 	/**
// 	 * @param cause
// 	 */
// 	public RelatorioNaoExisteException(Throwable cause) {
// 		super(cause);
// 	}
// 
// 	/**
// 	 * @param message
// 	 * @param cause
// 	 */
// 	public RelatorioNaoExisteException(String message, Throwable cause) {
// 		super(message, cause);
// 	}
// 
// 	/**
// 	 * @param message
// 	 * @param cause
// 	 * @param enableSuppression
// 	 * @param writableStackTrace
// 	 */
// 	public RelatorioNaoExisteException(String message, Throwable cause, boolean enableSuppression,
// 			boolean writableStackTrace) {
// 		super(message, cause, enableSuppression, writableStackTrace);
// 	}
// 
// 	public RelatorioNaoExisteException(Relatorio relatorio, Throwable cause) {
// 		this(String.format("Não foi possível encontrar o relatório %s", relatorio.getArquivo().getCaminho()), cause);
// 	}
// 
// }


// Path: src/main/java/com/github/marcelothebuilder/webpedidos/util/report/impl/EmissorRelatorioServlet.java
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import javax.servlet.http.HttpServletResponse;
import com.github.marcelothebuilder.webpedidos.util.report.EmissorRelatorio;
import com.github.marcelothebuilder.webpedidos.util.report.Relatorio;
import com.github.marcelothebuilder.webpedidos.util.report.RelatorioNaoExisteException;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;

/**
 * 
 */
package com.github.marcelothebuilder.webpedidos.util.report.impl;





/**
 * @author Marcelo Paixao Resende
 *
 */
public final class EmissorRelatorioServlet implements EmissorRelatorio {
	private Relatorio relatorio;
	private Connection connection;
	private HttpServletResponse servletResponse;
	private boolean relatorioGerado = false;

	public EmissorRelatorioServlet(Relatorio relatorio, HttpServletResponse servletResponse) {
		this.relatorio = relatorio;
		this.servletResponse = servletResponse;
	}

	public EmissorRelatorioServlet(Relatorio relatorio, HttpServletResponse output, Connection connection) {
		this(relatorio, output);
		this.connection = connection;
	}

	/* (non-Javadoc)
	 * @see com.github.marcelothebuilder.webpedidos.util.report.impl.EmissorRelatorio#setConnection(java.sql.Connection)
	 */
	@Override
	public void setConnection(Connection connection) {
		this.connection = connection;
	}

	/* (non-Javadoc)
	 * @see com.github.marcelothebuilder.webpedidos.util.report.impl.EmissorRelatorio#emitir()
	 */
	@Override
	public void emitir() throws JRException {
		try (InputStream relatorioArquivoStream = this.getClass().getResourceAsStream(relatorio.getArquivo().getCaminho())) {
			JasperPrint print = JasperFillManager.fillReport(relatorioArquivoStream, relatorio.getParametros(), connection);
			this.relatorioGerado = print.getPages().size() > 0;
			
			if(this.isRelatorioGerado()) {
				String attachment = String.format("attachment; filename=\"%s.pdf\"", relatorio.getArquivo().getNome());
				servletResponse.setHeader("Content-Disposition", attachment);
				JasperExportManager.exportReportToPdfStream(print, servletResponse.getOutputStream());		
			}
		} catch (IOException e) {
			throw new RelatorioNaoExisteException(relatorio, e);