I have a system of single notes in jsf and primefaces. I was able to generate a set report of several notes at the same time by searching through two input parameters, start date and end date. But what I want is to generate a note without having to inform any input parameter, the systems generates a list from a search of the saved notes and the user to select with the mouse the one that he generates and then the pdf is generated.
This is the bean that generates the pdf
private Date dataInicial;
private Date dataFinal;'
@Inject
private LancamentoTributo lancamennto;
@Inject
private FacesContext facesContext;
@Inject
private HttpServletResponse response;
@Inject
private EntityManager manager;
public void emitir() {
Map<String, Object> parametros = new HashMap<>();
parametros.put("data_inicial", this.dataInicial );
parametros.put("data_final", this.dataFinal );
ExecutorRelatorio executor = new ExecutorRelatorio("/relatorios/nota_fiscal.jasper", response, parametros,
"NTFSavemitida.pdf");
Session session = manager.unwrap(Session.class);
session.doWork(executor);
if (executor.isRelatorioGerado()) {
facesContext.responseComplete();
} else {
FacesUtil.addErrorMessage("Nenhuma nota emitida");
}
}
@NotNull
public Date getDataInicial() {
return dataInicial;
}
public void setDataInicial(Date dataInicial) {
this.dataInicial = dataInicial;
}
@NotNull
public Date getDataFinal() {
return dataFinal;
}
public void setDataFinal(Date dataFinal) {
this.dataFinal = dataFinal;
}
public LancamentoTributo getLancamennto() {
return lancamennto;
}
}
Classe Executor
private String caminhoRelatorio;
private HttpServletResponse response;
private Map<String, Object>parametros;
private String nomeArquivoSaida;
private boolean relatorioGerado;
public ExecutorRelatorio(String caminhoRelatorio, HttpServletResponse
response,Map<String, Object>parametros,
String nomeArquivoSaida) {
this.caminhoRelatorio = caminhoRelatorio;
this.response = response;
this.parametros = parametros;
this.nomeArquivoSaida = nomeArquivoSaida;
this.parametros.put(JRParameter.REPORT_LOCALE, new Locale("pt","BR"));
}
@Override
public void execute(Connection connection) throws SQLException {
try {
InputStream relatorioStream = this.getClass().getResourceAsStream(this.caminhoRelatorio);
JasperPrint print = JasperFillManager.fillReport(relatorioStream, this.parametros, connection);
this.relatorioGerado = print.getPages().size() > 0;
if (this.relatorioGerado) {
Exporter<ExporterInput, PdfReportConfiguration, PdfExporterConfiguration,
OutputStreamExporterOutput> exportador = new JRPdfExporter();
exportador.setExporterInput(new SimpleExporterInput(print));
exportador.setExporterOutput(new SimpleOutputStreamExporterOutput(response.getOutputStream()));
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment; filename=\""
+ this.nomeArquivoSaida + "\""); //Download automatico do pdf
exportador.exportReport();
}
} catch (Exception e) {
throw new SQLException("Erro ao executar relatorio " + this.caminhoRelatorio, e);
}
}
public boolean isRelatorioGerado() {
return relatorioGerado;
}