Generate Ireport in a documentViewer Primefaces

1

I need to know how I can generate an ireport report in a variable StreamedContent . The report is in a package in the project. The difficulty I have is that I can not get the route of the project. At the moment I have this portion of code:

 public void generarReporte() {
    try{
        JRBeanCollectionDataSource solicitudLista = new JRBeanCollectionDataSource(super.getSolicitudes());
        byte[] pdf;            
        String report = FacesContext.getCurrentInstance().getExternalContext()
                        .getRealPath("/com/../reportes/solicitudAspirante.jasper");
        //InputStream report =   this.getClass().getClassLoader().getResourceAsStream("");
        //JasperReport report = JasperCompileManager.compileReport(reporte);
        JasperPrint print = JasperFillManager.fillReport(report, new HashMap(),solicitudLista);
        pdf = JasperExportManager.exportReportToPdf(print);
        this.archivo.setFile(new DefaultStreamedContent(new ByteArrayInputStream(pdf), "application/pdf"));  
    }catch(Exception e){
        System.out.println(e.getMessage());
    }

It is assumed that in the variable report should load the compiled report but it is not in the specified route. How can I do to upload the report?

    
asked by Ernesto Alberto Martinez Marin 30.11.2016 в 15:16
source

2 answers

0

I use this code to generate the report in a pe: documentViewer:

Generate a document:

public DefaultStreamedContent getImprimeOrdenInicioStream() {
    if (ordenInicioSelected != null) {
        String reportePath = "/archivos/ordenInicio/rptOrdenInicio.jasper";
        Map<String, Object> parametros = new HashMap<>();
        parametros.put("idOrdenInicio", ordenInicioSelected.getIdOrdenInicio());
        parametros.put("idPersona", usuario.getIdPersona().getIdPersona());

        return imprimePDFEnDocumentViewer(reportePath, parametros, "rptOrdenInicio");
    }
    return new DefaultStreamedContent();
}

In this method I define the parameters of the JasperReports.

And then I call the other method called printDPFEnDocumentViewer:

public DefaultStreamedContent imprimePDFEnDocumentViewer(String rutaReporte, Map parametros, String nombreReporte) {
    JasperPrint jasperPrint;

    try {
        // Obteniendo las rutas relativas de los archivos necesarios
        String reportePath = sc.getRealPath(rutaReporte);
        String logoPath = sc.getRealPath("/resources/images/logo.png");
        String escudoPath = sc.getRealPath("/resources/images/escudo.png");
        parametros.put("logo_isbm", logoPath);
        parametros.put("escudo_logo", escudoPath);
        parametros.put(JRParameter.REPORT_LOCALE, locale);

        // Obteniendo la conexion del JDNI
        Context initialContext = new InitialContext();
        if (initialContext == null) {
            System.out.println("Problema con el JNDI. No se puede obtener el InitialContext.");
        }
        DataSource datasource = (DataSource) initialContext.lookup(DATASOURCE_CONTEXT);
        if (datasource != null) {
            conn = datasource.getConnection();
        } else {
            System.out.println("Error al buscar el datasource.");
        }

        jasperPrint = JasperFillManager.fillReport(reportePath, parametros, conn);

        // Mostrando el documento
        byte[] docPdf = JasperExportManager.exportReportToPdf(jasperPrint);
        return new DefaultStreamedContent(new ByteArrayInputStream(docPdf), "application/pdf", nombreReporte);

    } catch (JRException | NamingException ex) {
        Logger.getLogger(GeneraReportes.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    } catch (SQLException ex) {
        System.out.println("No se puede obtener la conexion: " + ex);
        return null;
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(GeneraReportes.class.getName()).log(Level.SEVERE, null, ex);
                return null;
            }
        }
    }
}

All routes are relative.

Then in the xhtml I call it in the following way:

On a button of a

<p:commandButton icon="ui-icon-print" title="#{msg.imprimir}" process="@this" oncomplete="PF('wDia').show()" update=":dia" rendered="true">
        <f:setPropertyActionListener value="#{inicio}" target="#{generaReportes.ordenInicioSelected}" />
</p:commandButton>

The variable #{inicio} is the attribute var of the dataTable and #{generaReportes.ordenInicioSelected} is a variable in bean that generates it, which is used in the code that I put above.

    
answered by 30.11.2016 / 15:41
source
0

I write the created class, so that other people can use it.

public class GenerarReporte {

private Map<String, Object> parameters;
private String reportPath;

public GenerarReporte(Map<String, Object> parameters, String reportPath) {
    this.reportPath = reportPath;
    this.parameters = parameters;
}

public DefaultStreamedContent generarReporte() {
    DefaultStreamedContent file = new DefaultStreamedContent();
    if (this.parameters != null && this.reportPath != null) {
        file = imprimePDFEnDocumentViewer(this.reportPath, this.parameters, "reporte");
    }
    return file;
}

private DefaultStreamedContent imprimePDFEnDocumentViewer(String rutaReporte, Map parameters, String nombreReporte) {
    JasperPrint jasperPrint;
    DefaultStreamedContent content = new DefaultStreamedContent();
    byte[] docPdf;
    try {
        // Obteniendo las rutas relativas de los archivos necesarios
        ExternalContext sc = FacesContext.getCurrentInstance().getExternalContext();
        String reportePath = sc.getRealPath(rutaReporte);
        //JRBeanCollectionDataSource datasourcesFields = new JRBeanCollectionDataSource(fields);           
        jasperPrint = JasperFillManager.fillReport(reportePath, parameters, new JREmptyDataSource());
        // Mostrando el documento
        docPdf = JasperExportManager.exportReportToPdf(jasperPrint);
        content = new DefaultStreamedContent(new ByteArrayInputStream(docPdf), "application/pdf", nombreReporte);

    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
    return content;
}

}

    
answered by 01.12.2016 в 01:24