browserwindowopener FileDownloader jasper

Hi all,
I’m having a issue BrowserWindowOpener/FileDownloader(same behavior). I have a table where a choose a patient and create a pdf. The first time a do this no problem. Everything is ok. The pdf is created ok.
The second time i want to generate a pdf (same o other patient) i’ve got my pdf but also got the firt pdf and so on.
I don’t know what can be happening but the it’s like the previus pdf still some where.
This is some snippet code:

if(event.getSource() == this.getBtnGenerarReporte()){
               
                // Resource resource, bwo BrowserWindowOpener
                //paciente  object get from valueChange in the table
                resource = generarSource(paciente);    <-generarSoruce declare bottom
                bwo = new BrowserWindowOpener(resource);           
                bwo .extend(btnVerReporte); <-btn shows pdf
     }

[code]
//generarSource method
private StreamResource generarSource(final VistaAdmEstudiosPorRealizar paciente) {

    return new StreamResource(new StreamResource.StreamSource() {
        
        /**
         *
         */
        private static final long serialVersionUID = 1L;
        @Override
        public InputStream getStream() {
            
             Conexion conexion = null;
             String reportFile = "";
             byte b = null;
             log.trace("IdRutina del paciente seleccionado: " + paciente.getIdRutina());
             Map<String, Object> parameters = new HashMap<String, Object>();
             parameters.put("filter",paciente.getIdRutina());
             parameters.put("parametro1", paciente.getTipoExamen());
             parameters.put("parametro2", paciente.getRazonSocial());
             parameters.put("parametro3", paciente.getApellidoPaciente()+", "+paciente.getNombrePaciente());
             parameters.put("parametro4", paciente.getDireccion());
             parameters.put("parametro5", paciente.getNumeroDeDocumento());
             parameters.put("parametro6", paciente.getFechaNacimiento());
             parameters.put("parametro7", paciente.getNombreArt());
             parameters.put("parametro8", paciente.getEstadoCivil());
             parameters.put("parametro9", paciente.getTelefono());
             parameters.put("parametro10",paciente.getTipoDeDocumento());
             parameters.put("parametro11",paciente.getNacionalidad());
             parameters.put("parametro12",paciente.getFechaIngreso());
             parameters.put("parametro13",paciente.getOrientacionRutina());
              try {
                     if(paciente.getTipoExamen().equalsIgnoreCase("periodico")){
                            reportFile = "com/report/rutinasAVisarPeriodico.jasper";
                            }else{
                            reportFile = "com/report/rutinasAVisarIEO.jasper";    
                            }                
                     InputStream report = getClass().getClassLoader().getResourceAsStream(reportFile);
                     
                        if (report == null) {
                            Notification.show("No report!", Type.ERROR_MESSAGE);
                            return null;
                        }
                       
                     conexion = new Conexion();
                     b = JasperRunManager.runReportToPdf(report, parameters, conexion.getConexion());
                    //  conexion.getConexion().commit();
                  
                     } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (JRException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
            
                    
                    }
              return new ByteArrayInputStream(b);
              }
    }, "reporte"+paciente.getTipoExamen()+".pdf");
}

[/code]try with setTimeCache and all kind of ways but no luck.
Also attach some view of the behaviors.

any help appreciated!
Regards

:

26043.png
26044.png

I didn’t try it, but I think you should create BrowserWindowOpener and extend button only once; next time event is fired you should invoke setResource on existing BrowserWindowOpener instance

Hi Marco, i did this and is working (following your recomendation)
Can you tell me if this is a correct procedure?
I post what i did maybe can help someone else and no waste 2 weeks :slight_smile:
Regards!!

//class var
StreamResource resource = null;
BrowserWindowOpener fd = new BrowserWindowOpener(""); //<-only once. Set constructor
                                                      // like String first time

//Constructor
public LayAprobacionesEnEspera() {
   //layout setting
    fd.extend(btnVerReporte); //<-extend button only once
}

//listener over btn pdf generetor

if(event.getSource() == this.getBtnGenerarReporte()){
            
                resource = generarSource(); //<-declare above
                fd.setResource(resource);                   
                this.getBtnVerReporte().setEnabled(true);               
            }

//generarSource() remove parameter that a used to use
private StreamResource generarSource() {
            
        return new StreamResource(new StreamResource.StreamSource() {
      
            private static final long serialVersionUID = 1L;
           
            @Override
            public InputStream getStream() {
           
                Conexion conexion = null;
                 String reportFile = "";
                 byte[] b = null;
                 Paciente pacientito = (Paciente ) getTablaAprobaciones().getValue(); //<--recover my patien here!!!
                 log.trace("soy paciente!: "+pacientito.getIdRutina());
                 Map<String, Object> parameters = new HashMap<String, Object>();
                 parameters.put("filter",pacientito.getIdRutina());
                 parameters.put("parametro1", pacientito.getTipoExamen());
                 //few more parameters..                
                  try {
                         if(pacientito.getTipoExamen().equalsIgnoreCase("periodico") || pacientito.equals(null)){
                                reportFile = "com/report/rutinasAVisarPeriodico.jasper";
                                }else{
                                reportFile = "com/report/rutinasAVisarIEO.jasper";    
                                }                
                         InputStream report = getClass().getClassLoader().getResourceAsStream(reportFile);
                         
                            if (report == null) {
                                Notification.show("No report!", Type.ERROR_MESSAGE);
                                return null;
                            }
                           
                         conexion = new Conexion();
                         b = JasperRunManager.runReportToPdf(report, parameters, conexion.getConexion());
                        //  conexion.getConexion().commit();
                      
                         } catch (SQLException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (JRException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                
                        
                        }
                  
                  return new ByteArrayInputStream(b);
               }
        }, "report.pdf");
    }

I think this can work; maybe you can instantiiate BrowserWindowOpener with a null resource instead of an empty url (again, I did not try it) and you can remove the StreamResource member instance if you use it only in yuor istener.

Regards