Report one object at a time with Jaspersoft Studio

Asked

Viewed 193 times

0

I have a system of loose notes in jsf and primefaces. I was able to generate a set report of several notes at the same time, searching through two input parameters, initial date and final 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 select with the mouse to which it generate 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;
}

1 answer

0

You can use Jrbeancollectiondatasource by passing the list as parameter.

It would look something like this:

InputStream relatorioStream = this.getClass().getResourceAsStream(this.caminhoRelatorio);
JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(suaLista);
JasperPrint print = JasperFillManager.fillReport(relatorioStream, this.parametros, connection, ds);
this.relatorioGerado = print.getPages().size() > 0;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.