-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBirtReportService.groovy
689 lines (650 loc) · 29 KB
/
BirtReportService.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
package com.itjw.grails.birt
import grails.config.Config
import grails.core.GrailsApplication
import grails.util.Environment
import org.eclipse.birt.core.data.DataTypeUtil
import org.eclipse.birt.core.exception.BirtException
import org.eclipse.birt.core.framework.PlatformFileContext
import org.eclipse.birt.data.engine.api.DataEngine
import org.eclipse.birt.report.engine.api.*
import org.eclipse.birt.report.model.api.elements.DesignChoiceConstants
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.InitializingBean
import org.springframework.context.ApplicationContext
import org.springframework.context.ApplicationContextAware
import org.springframework.web.context.request.RequestContextHolder
import org.springframework.web.servlet.support.RequestContextUtils as RCU
import javax.servlet.ServletContext
import javax.servlet.ServletException
import java.util.logging.Level
class BirtReportService implements InitializingBean, ApplicationContextAware {
private static Logger LOG = LoggerFactory.getLogger(BirtReportService.class);
private static final String REPORT_EXT = ".rptdesign"
private static final String SUPPORTED_IMAGE_FORMATS = "PNG;GIF;JPG;BMP"
public static final String DEFAULT_REPORT_HOME = "classpath:Reports"
public static final int DEFAULT_CACHE_SIZE = 100
private boolean svgEnabled = false
static transactional = false
ApplicationContext applicationContext
GrailsApplication grailsApplication
def dataSource
boolean useGrailsDatasource
// where are the reports located
String reportHome = ""
// at which URL are the reports generated
String baseURL
// if the generated base url should be absolute
boolean generateAbsoluteBaseURL = false
// at which URL are the images accessible
String baseImageURL
// what is the image dir on disc
String imageDir
def defaultFormat = "inline"
BirtReportService(GrailsApplication grailsApplication) {
this.grailsApplication = grailsApplication
}
void afterPropertiesSet() {
ServletContext sc = applicationContext?.servletContext
if (!sc) {
LOG.error "Could not derive servlet context, report generation disabled"
return
}
Config config = grailsApplication.config
reportHome = detectReportHome(config)
LOG.info "reportHome is ${reportHome}"
if(config.birt?.useGrailsDatasource) {
useGrailsDatasource = true
}
LOG.info "${useGrailsDatasource?'':'not '}using grails data source"
if (config.birt?.generateAbsoluteBaseURL) {
generateAbsoluteBaseURL = true
}
if (config.birt?.baseUrl) {
baseURL = config.birt.baseUrl
LOG.info "baseURL is ${baseURL}"
} else {
LOG.info "generated baseURL will ${generateAbsoluteBaseURL?'':'not '}be absolute"
}
detectImageUrls(sc, config)
def imgDir = new File(imageDir)
LOG.info "baseImageUrl is ${baseImageURL} and points to ${imageDir}"
if (!imgDir.exists() && !imgDir.mkdirs()) {
LOG.error "Could not create report image directory ${imgDir.absolutePath}, report generation disabled."
return
}
initEngine(baseURL, config)
}
protected void detectImageUrls(ServletContext sc, Config config) {
baseImageURL = sc.contextPath + "/images/" + "rpt-img"
imageDir = sc.getRealPath("/images/" + "rpt")
if (config.birt?.imageUrl) {
if (config.birt.imageUrl[0] == '/' || config.birt.imageUrl[1] == ':') {
baseImageURL = sc.contextPath + config.birt.imageUrl
imageDir = sc.getRealPath(config..birtimageUrl as String)
} else {
baseImageURL = sc.contextPath + "/" + config.birt.imageUrl
imageDir = sc.getRealPath("/" + config.birt.imageUrl)
}
}
}
protected void initEngine(String baseURL, Config config) {
System.setProperty("RUN_UNDER_ECLIPSE", "false")
HTMLServerImageHandler imageHandler = new HTMLServerImageHandler()
// for file based output
// HTMLCompleteImageHandler imageHandler = new HTMLCompleteImageHandler()
HTMLActionHandler actionHandler = new GrailsHTMLActionHandler(baseURL, defaultFormat)
HTMLRenderOption renderOption = new HTMLRenderOption()
renderOption.imageHandler = imageHandler
renderOption.actionHandler = actionHandler
// appContext[EngineContants.APPCONTEXT_CHART_RESOLUTION] = myvalue
// Create the engineConfig for the report generator
def engineConfig = new EngineConfig()
Integer cacheSize = DEFAULT_CACHE_SIZE
if(config != null && config.birt?.cacheSize) {
cacheSize = config.cacheSize
}
engineConfig.appContext = [(DataEngine.MEMORY_BUFFER_SIZE): cacheSize]
engineConfig.engineHome = ""
engineConfig.platformContext = new PlatformFileContext(engineConfig)
engineConfig.setLogConfig(null, Environment.isDevelopmentMode() ? Level.ALL : Level.SEVERE)
engineConfig.setEmitterConfiguration(RenderOption.OUTPUT_FORMAT_HTML, renderOption)
BirtEngineFactory.init(engineConfig)
}
public InputStream getInputStreamForResource(String reportName) {
String fn = createCompleteReportFilename(reportName)
return applicationContext.getResource(fn).inputStream
}
protected String detectReportHome(Config config) {
if(config != null && config.birt?.reportHome) {
return config.birt.reportHome
} else {
return DEFAULT_REPORT_HOME
}
}
/**
* Returns a list of available reports in the reportHome directory. The list contains Maps of property
* name/value pairs. The properties contain the BIRT standart properties:<ul>
* <li>IReportRunnable.AUTHOR</li>
* <li>IReportRunnable.BASE_PROP</li>
* <li>IReportRunnable.COMMENTS</li>
* <li>IReportRunnable.CREATEDBY</li>
* <li>IReportRunnable.DESCRIPTION</li>
* <li>IReportRunnable.HELP_GUIDE</li>
* <li>IReportRunnable.REFRESH_RATE</li>
* <li>IReportRunnable.TITLE</li>
* <li>IReportRunnable.UNITS</li></ul>
* and 3 custom ones:<ul>
* <li>report name</li>
* <li>report design name</li>
* <li>absolute file name (including full path)</li></ul>
*
* @return List<Map>
*/
def listReports() {
return listReports(null)
}
/**
* Returns a list of available reports in the reportHome directory. The list contains Maps of property
* name/value pairs. The properties contain the BIRT standart properties:<ul>
* <li>IReportRunnable.AUTHOR</li>
* <li>IReportRunnable.BASE_PROP</li>
* <li>IReportRunnable.COMMENTS</li>
* <li>IReportRunnable.CREATEDBY</li>
* <li>IReportRunnable.DESCRIPTION</li>
* <li>IReportRunnable.HELP_GUIDE</li>
* <li>IReportRunnable.REFRESH_RATE</li>
* <li>IReportRunnable.TITLE</li>
* <li>IReportRunnable.UNITS</li></ul>
* as well as 3 custom ones:<ul>
* <li>report name</li>
* <li>report design name</li>
* <li>absolute file name (including full path)</li></ul>
* and user properties which are specified by the userProps
*
* @param userProps
* @return List<Map>
*/
def listReports(userProps) {
LOG.trace "Function: listReports()"
def reports = []
if (reportHome) {
File reportDir = new File(reportHome,)
def files = reportDir?.list().grep { it ==~ /.*\.rptdesign/ }
files.each {
def name = it.replace(REPORT_EXT, '')
def prop = getReportProperties(name, userProps)
prop["name"] = name
prop["file"] = it
prop["fullfile"] = reportDir.absolutePath + reportDir.separator + it
reports << prop
}
}
return reports
}
/**
* Returns a map containing the properties of a report design as
* name/value pairs. The properties contain the BIRT standart properties:<ul>
* <li>IReportRunnable.AUTHOR</li>
* <li>IReportRunnable.BASE_PROP</li>
* <li>IReportRunnable.COMMENTS</li>
* <li>IReportRunnable.CREATEDBY</li>
* <li>IReportRunnable.DESCRIPTION</li>
* <li>IReportRunnable.HELP_GUIDE</li>
* <li>IReportRunnable.REFRESH_RATE</li>
* <li>IReportRunnable.TITLE</li>
* <li>IReportRunnable.UNITS</li></ul>
* and 3 custom ones:<ul>
* <li>report name</li>
* <li>report design name</li>
* <li>absolute file name (including full path)</li></ul>
*
* @param reportName
* @return List<Map>
*/
def getReportProperties(reportName) {
return getReportProperties(reportName, null)
}
/**
* Returns a map containing the properties of a report design as
* name/value pairs. The properties contain the BIRT standart properties:<ul>
* <li>IReportRunnable.AUTHOR</li>
* <li>IReportRunnable.BASE_PROP</li>
* <li>IReportRunnable.COMMENTS</li>
* <li>IReportRunnable.CREATEDBY</li>
* <li>IReportRunnable.DESCRIPTION</li>
* <li>IReportRunnable.HELP_GUIDE</li>
* <li>IReportRunnable.REFRESH_RATE</li>
* <li>IReportRunnable.TITLE</li>
* <li>IReportRunnable.UNITS</li></ul>
* and 3 custom ones:<ul>
* <li>report name</li>
* <li>report design name</li>
* <li>absolute file name (including full path)</li></ul>
* and user properties which are specified by the userProps
*
* @param reportName
* @param userProps
* @return List<Map>
*/
def getReportProperties(reportName, userProps) {
getReportProperties(reportName, null, userProps)
}
/**
* Returns a map containing the properties of a report design as
* name/value pairs. The properties contain the BIRT standart properties:<ul>
* <li>IReportRunnable.AUTHOR</li>
* <li>IReportRunnable.BASE_PROP</li>
* <li>IReportRunnable.COMMENTS</li>
* <li>IReportRunnable.CREATEDBY</li>
* <li>IReportRunnable.DESCRIPTION</li>
* <li>IReportRunnable.HELP_GUIDE</li>
* <li>IReportRunnable.REFRESH_RATE</li>
* <li>IReportRunnable.TITLE</li>
* <li>IReportRunnable.UNITS</li></ul>
* and 3 custom ones:<ul>
* <li>report name</li>
* <li>report design name</li>
* <li>absolute file name (including full path)</li></ul>
* and user properties which are specified by the userProps
*
* @param reportName
* @param inputStream the input stream containing the report, optionally null
* @param userProps
* @return List<Map>
*/
def getReportProperties(reportName, inputStream, userProps) {
LOG.trace "Function: getReportProperties(${reportName}, ${inputStream}, ${userProps})"
def props = [:]
def reportFileName = createCompleteReportFilename(reportName)
def propnames = userProps ? userProps.keySet() : []
propnames += [IReportRunnable.AUTHOR, IReportRunnable.BASE_PROP, IReportRunnable.COMMENTS, IReportRunnable.CREATEDBY, IReportRunnable.DESCRIPTION, IReportRunnable.HELP_GUIDE, IReportRunnable.REFRESH_RATE, IReportRunnable.TITLE, IReportRunnable.UNITS]
try {
//Open report design
IReportRunnable design = inputStream?
BirtEngineFactory.engine?.openReportDesign(reportName, inputStream):
BirtEngineFactory.engine?.openReportDesign(reportFileName)
if (!design) return props
propnames.each {
def prop = design.getProperty(it)
if (prop) // property is defined by report
props[it] = prop
else if (userProps && userProps.containsKey(it)) { // property value is default
props[it] = userProps[it]
}
}
LOG.debug "Reportparams:${props}"
return props
} catch (Exception e) {
LOG.error("Exception occured while getReportProperties: ${e.message}", e)
throw new ServletException(e)
}
}
/**
* Extracts the report parameters of a report design. The returned list contains a map for each parameter
* containing:<ul>
* <li>name</li>
* <li>type</li>
* <li>controlType</li>
* <li>defaultVal</li>
* <li>helpText</li>
* <li>promptText</li>
* <li>allowBlank</li>
* <li>listEntries (a list conaining the possibe values for restricted types)</li></ul>
*
* @param reportName
* @return List
*/
def getReportParams(reportName) {
getReportParams(reportName, null)
}
/**
* Extracts the report parameters of a report design. The returned list contains a map for each parameter
* containing:<ul>
* <li>name</li>
* <li>type</li>
* <li>controlType</li>
* <li>defaultVal</li>
* <li>helpText</li>
* <li>promptText</li>
* <li>allowBlank</li>
* <li>listEntries (a list conaining the possibe values for restricted types)</li></ul>
*
* @param reportName
* @param inputStream the input stream containing the report, optionally null
* @return List
*/
def getReportParams(reportName, inputStream) {
LOG.trace "Function: getReportParams(${reportName})"
def reportParams = []
def reportFileName = createCompleteReportFilename(reportName)
if (!new File(reportFileName).exists()) return reportParams
try {
//Open report design
// def engine = BirtEngineFactory.engine
if (!BirtEngineFactory.engine) return reportParams
IReportRunnable design = inputStream?
BirtEngineFactory.engine?.openReportDesign(reportName, inputStream):
BirtEngineFactory.engine?.openReportDesign(reportFileName)
IGetParameterDefinitionTask task = BirtEngineFactory.engine.createGetParameterDefinitionTask(design)
task.locale=getLocale()
if(useGrailsDatasource) task.getAppContext().put("OdaJDBCDriverPassInConnection", dataSource.getConnection())
// Iterate over all parameters, Don't report about groups
task.getParameterDefns(false).each {param ->
//Group section found
if (!(param instanceof IParameterGroupDefn)) { //Groups are not supported
//Parameters are not in a group
def listentries = []
if (param.controlType == IScalarParameterDefn.LIST_BOX || param.controlType == IScalarParameterDefn.RADIO_BUTTON) {
//Parameter is a List Box
task.getSelectionList(param.name)?.each {
//Print out the selection choices
def selectionItem = (IParameterSelectionChoice) it
def value = selectionItem.value
def label = selectionItem.label
// log.debug label + "--" + value
listentries << ['label': label ?: value, 'value': value]
}
}
reportParams << ['name': param.name,
'type': param.dataType,
'paramType': param.scalarParameterType,
'controlType': param.controlType,
'defaultVal': task.getDefaultValue(param),
'helpText': param.helpText,
'promptText': param.promptText,
'allowBlank': param.allowBlank(),
'listEntries': listentries
]
}
}
task.close()
} catch (Exception e) {
LOG.error("Exception occured while getReportParams: ${e.message}", e)
throw new ServletException(e)
}
return reportParams
}
/**
* Extracts the parameter names of a report design as a list of Strings
*
* @param reportName
* @return List
*/
def getReportParamNames(reportName) {
LOG.trace "Function: getReportParamNames(${reportName})"
def params = getReportParams(reportName)
return params.name
}
/**
* Extracts the parameter names of a report design as a list of Strings
*
* @param reportName
* @param inputStream the input stream containing the report, optionally null
* @return List
*/
def getReportParamNames(reportName, inputStream) {
LOG.trace "Function: getReportParamNames(${reportName})"
def params = getReportParams(reportName, inputStream)
return params.name
}
/**
* Creates a renderOption for the given format. Supported values are 'inline' (html fragment),
* 'html', 'pdf', 'xls', 'doc', 'ppt', 'odt', 'ods', 'odp'
*
* @param format
* @return IRenderOption
*/
def getRenderOption(format) {
return getRenderOption(null, format)
}
/**
* Creates a renderOption for the given HTTPServletRequest and format. Supported values for the format are
* 'inline' (html fragment), 'html', 'pdf', 'xls', 'doc', 'ppt', 'odt', 'ods', 'odp'
* The request is used to derive the appropriate url used in hyperlinks within reports (e.g. drill-downs).
* This method allows to enable/disable the use of SVG on a per-request basis (the setting will persist until
* it is changed)
*
* @param request
* @param format
* @param genSVG
* @return IRenderOption
*/
def getRenderOption(request, format, genSVG) {
svgEnabled = genSVG
return getRenderOption(request, format)
}
/**
* Creates a renderOption for the given HTTPServletRequest and format. Supported values for the format are
* 'inline' (html fragment), 'html', 'pdf', 'xls', 'doc', 'ppt', 'odt', 'ods', 'odp'
* The request is used to derive the appropriate url used in hyperlinks within reports (e.g. drill-downs).
*
* @param request
* @param format
* @return IRenderOption
*/
def getRenderOption(request, format) {
LOG.trace "Function: getRenderOption(${request}, ${format})"
IRenderOption options = new RenderOption()
// set an absolute url to be used in output
if (baseURL =~ /^\w+:\/\//) { // we have a complete URL
options.baseURL = baseURL
} else if(request != null) {
if(generateAbsoluteBaseURL) {
// add the protocol/host/port part of the URL
options.baseURL = "${request.getScheme()}://${request.getServerName()}:${request.getServerPort()}"
// append application context path either as absolute path or as relative
options.baseURL += baseURL[0] == "/" ? baseURL : request.getContextPath() + "/" + baseURL
} else {
options.baseURL = request.contextPath
}
} else {
options.baseURL = "/"
}
options.actionHandler = new GrailsHTMLActionHandler(options.baseURL, format?:defaultFormat)
options.outputFormat = format?:"html"
switch (options.outputFormat.toLowerCase()) {
case "html":
HTMLRenderOption htmlOptions = new HTMLRenderOption(options)
htmlOptions.htmlPagination = false
htmlOptions.embeddable = false
htmlOptions.baseImageURL = baseImageURL
htmlOptions.imageDirectory = imageDir
htmlOptions.supportedImageFormats = SUPPORTED_IMAGE_FORMATS + (svgEnabled ? ";SVG" : "")
return htmlOptions
case "pdf":
PDFRenderOption pdfOptions = new PDFRenderOption(options)
// pdfOptions.setOption(IPDFRenderOption.PAGE_OVERFLOW, IPDFRenderOption.FIT_TO_PAGE_SIZE)
pdfOptions.setOption(IPDFRenderOption.PAGE_OVERFLOW, IPDFRenderOption.OUTPUT_TO_MULTIPLE_PAGES)
pdfOptions.supportedImageFormats = SUPPORTED_IMAGE_FORMATS + (svgEnabled ? ";SVG" : "")
return pdfOptions
default:
return options
}
}
private Object getParamValue(def dataType, def param) {
switch (dataType) {
case IScalarParameterDefn.TYPE_BOOLEAN:
return DataTypeUtil.toBoolean(param); break
case IScalarParameterDefn.TYPE_DATE:
return DataTypeUtil.toSqlDate(param); break
case IScalarParameterDefn.TYPE_TIME:
return DataTypeUtil.toSqlTime(param); break
case IScalarParameterDefn.TYPE_DATE_TIME:
return DataTypeUtil.toDate(param); break
case IScalarParameterDefn.TYPE_DECIMAL:
return DataTypeUtil.toBigDecimal(param); break
case IScalarParameterDefn.TYPE_FLOAT:
return DataTypeUtil.toDouble(param); break
case IScalarParameterDefn.TYPE_STRING:
return DataTypeUtil.toString(param); break
case IScalarParameterDefn.TYPE_INTEGER:
return DataTypeUtil.toInteger(param); break
default: throw new RuntimeException("Unsupported dataType = " + dataType + ", param " + param)
}
}
private getReportBirtParams(Map params, IReportRunnable runnable) {
LOG.trace "Function: getReportBirtParams(${params}, ${runnable})"
try {
//get parameter definitions
// def engine = BirtEngineFactory.engine
if (!BirtEngineFactory.engine) return null
def task = BirtEngineFactory.engine.createGetParameterDefinitionTask(runnable)
task.locale=getLocale()
def paramDefs = task.getParameterDefns(false)
//iterate over each parameter definition, updating as appropriate
//from the supplied ReportAttributes object
def paramMap = new HashMap()
paramDefs.each {
def paramName = it.name
def paramVal
if (params.containsKey(paramName)) {
def value = params[paramName];
if(value != null) {
if(it.scalarParameterType.equals(DesignChoiceConstants.SCALAR_PARAM_TYPE_MULTI_VALUE)) {
for(int i = 0; i < value.size(); i++) {
value[i] = getParamValue(it.dataType, value[i])
}
paramVal = value
} else {
paramVal = getParamValue(it.dataType, value)
}
}
}
if (paramVal != null) paramMap[paramName] = paramVal
}
return paramMap
} catch (BirtException e) {
// log.error("BIRT Exception occured while getReportBirtParams: ${e.message}", e)
throw new Exception(e.message)
}
}
/**
* Get the locale of the request or as fallback of the host system
* @return locale
* */
def getLocale() {
Locale locale = null
try {
locale = RCU.getLocale(RequestContextHolder.currentRequestAttributes().getSession().request)
} catch(Exception ignored){
locale = Locale.getDefault()
}
LOG.debug "locale: ${locale}"
return locale
}
/**
* Runs and renders a report design into the format specified by renderOptions. Parameters are specified as
* name/value pairs and will be parsed (by BIRT) into the appropriate format.
*
* @param reportName
* @param parameters
* @param renderOptions
* @return ByteArrayOutputStream
*/
def runAndRender(String reportName, parameters, renderOptions, Locale locale = null) {
runAndRender(reportName, null, parameters, renderOptions, locale)
}
/**
* Runs and renders a report design into the format specified by renderOptions. Parameters are specified as
* name/value pairs and will be parsed (by BIRT) into the appropriate format.
*
* @param reportName
* @param inputStream the input stream containing the report, optionally null
* @param parameters
* @param renderOptions
* @return ByteArrayOutputStream
*/
def runAndRender(String reportName, InputStream inputStream, parameters, renderOptions, Locale locale = null) {
LOG.trace "Function: runAndRender(${reportName}, ${parameters}, ${renderOptions})"
def reportFileName = createCompleteReportFilename(reportName)
LOG.debug "Parameters are ${parameters}"
// def engine = BirtEngineFactory.engine
if (!BirtEngineFactory.engine) return null
//Open report design
IReportRunnable design = inputStream?
BirtEngineFactory.engine?.openReportDesign(reportName, inputStream):
BirtEngineFactory.engine?.openReportDesign(reportFileName)
//create task to run and render report
IRunAndRenderTask task = BirtEngineFactory.engine.createRunAndRenderTask(design)
task.locale=locale?:getLocale()
// other options IN_MEMORY_CUBE_SIZE
def taskParams = getReportBirtParams(parameters, design)
LOG.debug "taskParams: ${taskParams}"
task.setParameterValues(taskParams)
task.validateParameters()
ByteArrayOutputStream buf = new ByteArrayOutputStream()
renderOptions.outputStream = buf
task.renderOption = renderOptions
if(useGrailsDatasource) task.getAppContext().put("OdaJDBCDriverPassInConnection", dataSource.getConnection())
task.run()
task.close()
return buf
}
protected String createCompleteReportFilename(String reportName) {
return reportHome + File.separator + reportName + REPORT_EXT
}
/**
* Runs a report design from classpath resource and generates a reportDocument with the given name. Parameters are specified as
* name/value pairs and will be parsed (by BIRT) into the appropriate format.
*
* @param reportName
* @param parameters
* @param reportDocumentName
*/
def run(String reportName, parameters, String reportDocumentName, Locale locale = null) {
run(reportName, getInputStreamForResource(reportName), parameters, reportDocumentName, locale)
}
/**
* Runs a report design and generates a reportDocument with the given name. Parameters are specified as
* name/value pairs and will be parsed (by BIRT) into the appropriate format.
*
* @param reportName
* @param inputStream the input stream containing the report, optionally null
* @param parameters
* @param reportDocumentName
*/
def void run(String reportName, InputStream inputStream, parameters, String reportDocumentName, Locale locale = null) {
LOG.trace "Function: run(${reportName}, ${parameters}, ${reportDocumentName})"
String reportFileName = createCompleteReportFilename(reportName)
LOG.debug "Parameters are ${parameters}"
// def engine = BirtEngineFactory.engine
if (!BirtEngineFactory.engine) return
//Open report design
IReportRunnable design = (inputStream != null)?
BirtEngineFactory.engine?.openReportDesign(reportName, inputStream):
BirtEngineFactory.engine?.openReportDesign(reportFileName as String)
//create task to run and render report
IRunTask task = BirtEngineFactory.engine.createRunTask(design)
task.locale=locale?:getLocale()
// other options IN_MEMORY_CUBE_SIZE
def taskParams = getReportBirtParams(parameters, design)
LOG.debug "taskParams: ${taskParams}"
task.parameterValues = taskParams
if(useGrailsDatasource) task.getAppContext().put("OdaJDBCDriverPassInConnection", dataSource.getConnection())
task.validateParameters()
task.run(reportDocumentName)
task.close()
}
/**
* Renders a report document into the format specified by renderOptions. Parameters are specified as
* name/value pairs and will be parsed (by BIRT) into the appropriate format.
*/
def render(reportDocumentName, parameters, renderOptions, Locale locale = null) {
LOG.trace "Function: render(${reportDocumentName}, ${renderOptions})"
if (!BirtEngineFactory.engine) return null
// Open report design
IReportDocument design = BirtEngineFactory.engine.openReportDocument(reportDocumentName)
// create task to run and render report
IRenderTask task = BirtEngineFactory.engine.createRenderTask(design)
task.locale=locale?:getLocale()
if (parameters) task.parameterValues = parameters
ByteArrayOutputStream buf = new ByteArrayOutputStream()
renderOptions.outputStream = buf
task.renderOption = renderOptions
if(useGrailsDatasource) task.getAppContext().put("OdaJDBCDriverPassInConnection", dataSource.getConnection())
task.render()
task.close()
return buf
}
}