我有这个问题。我必须使用Jasper报告生成报告,然后我必须根据某些条件更新一些记录。这里有一些代码:
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()
}
}
我的报告需要transporte_pagado
记录的字段,其值为'NO'
,但更新操作会立即执行,以便所涉及的记录和transporte_pagado
字段更新为{{1生成报告之前,结果为空,报告为空。
如何延迟更新操作?或者,如何在完成另一项任务后严格执行任务?
答案 0 :(得分:0)
我解决了我的问题(嗯,Sergio Michels帮助了我)。这是我使用的代码(我更改了一些域类和变量名称以提供一般解决方案):
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()
}
我发现对于这种情况最好使用服务器而不是链。这是一种魅力!