Question

I have this problem. I have to generate a report using Jasper reports, then I have to update some records according to a some conditions. Here some code:

def crearReporte = {
    //FIRST: generate the report
    chain(controller:'jasper',action:'index',params:params)
    //SECOND: update the reported information
    def recepciones = RecepcionDeEstano.findAllByTransportePagadoAndFechaDeRecepcionBetween("NO",fechaInicial1,fechaFinal1)
    pagarTransporte recepciones
}

def pagarTransporte = { lista ->
    lista.each {
        it.transportePagado="SI"
        it.save()
    }
}

My report needs the transporte_pagado record's field having the value of 'NO', but the updating operation is executed so immediately that the records and the transporte_pagado field involved are updated to 'SI' before the report is generated giving as result and empty report.

How can I delay the updating operation? Or, how can I perform a task strictly after another task is completed?

Was it helpful?

Solution

I solved my problem (Well, Sergio Michels helped me). This is the code I used (I changed some domain class and variables names to offer a general solution):

def createReport = {
    Map reportParams = [:]
    byte[] bytes
    //report parameters
    reportParams.put("PARAM_1",params.param1)
    reportParams.put("PARAM_2",params.param2)
    reportParams.put("PARAM_N",params.paramn)

    //here I had to write some IF's to know what report to send
    if(aCondition1)
        def reportDef = new JasperReportDef(name:'my_report1.jasper',fileFormat:JasperExportFormat.PDF_FORMAT,parameters: reportParams)
        bytes = jasperService.generateReport(reportDef).toByteArray()        
        //update records after they are reported
        doSomeUpdate param1
    }   
    if(aCondition2)
        def reportDef = new JasperReportDef(name:'my_report2.jasper',fileFormat:JasperExportFormat.PDF_FORMAT,parameters: reportParams)
        bytes = jasperService.generateReport(reportDef).toByteArray()        
        //update records after they are reported
        doSomeUpdate param2
    }
    //send report for download
    response.addHeader("Content-Disposition", 'attachment; filename="report.pdf"')
    response.contentType = 'application/pdf'
    response.outputStream << bytes
    response.outputStream.flush()
}

I found that for this case is better to use the server instead of the chain. This works as a charm!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top