private void prepareCSVRender (Map reportParams, HttpServletRequest request, IReportRunnable design, String userId, String documentId, IEngUserProfile profile, String kpiUrl, HttpServletResponse response) throws Exception {
logger.debug("IN");
//Create task to run the report
logger.debug("design: " + design.getReportName());
IRunTask CSVtask = birtReportEngine.createRunTask(design);
//**** Set parameters for the report ****
CSVtask.setParameterValues(reportParams);
CSVtask.validateParameters();
//************************************
UUIDGenerator uuidGen = UUIDGenerator.getInstance();
UUID uuid_local = uuidGen.generateTimeBasedUUID();
String executionId = uuid_local.toString();
executionId = executionId.replaceAll("-", "");
String nameFile = getJRTempDirName(getServletContext(), executionId)+ "csvreport.rptdocument";
Map context = BirtUtility.getAppContext(request);
CSVtask.setAppContext(context);
//Run the report and create the rptdocument
CSVtask.run(nameFile);
//Open the rptdocument
IReportDocument rptdoc = birtReportEngine.openReportDocument(nameFile);
logger.debug(rptdoc.getPageCount());
//*** Create the data extraction task ****
IDataExtractionTask iDataExtract = birtReportEngine.createDataExtractionTask(rptdoc);
ArrayList resultSetList = (ArrayList)iDataExtract.getResultSetList( );
ICSVDataExtractionOption extractionOptions = new CSVDataExtractionOption();
OutputStream responseOut = response.getOutputStream();
extractionOptions.setOutputFormat("csv");
extractionOptions.setSeparator(";");
//flag for exportdata found
boolean ed_found = false;
//check if there is the ExportData element
for (int j=0; j<resultSetList.size();j++){
//get an item
IResultSetItem resultItem = (IResultSetItem)resultSetList.get(j);
//get the name of the resultSet
String dispName = resultItem.getResultSetName();
if (dispName.equalsIgnoreCase("exportdata")){
logger.debug("Found ExportData Element in report ");
ed_found = true;
//output directly on the response OutputStream
extractionOptions.setOutputStream(responseOut);
//Set the HTTP response
response.setContentType("text/csv");
response.setHeader("Content-disposition", "inline; filename=reportcsv.csv");
iDataExtract.selectResultSet( dispName );
iDataExtract.extract(extractionOptions);
logger.debug("Extraction successfull "+dispName);
break;
}
}
if (ed_found){
//close the extract
iDataExtract.close();
//close the task
CSVtask.close();
logger.debug("Finished");
logger.debug("OUT");
}
//ExtractData element not found, search all element to export
if (!ed_found) {
//check if there is only a result set and generate one CSV file
if (resultSetList.size() <= 1){
//output directly on the response OutputStream
extractionOptions.setOutputStream(responseOut);
//Set the HTTP response
response.setContentType("text/csv");
response.setHeader("Content-disposition", "inline; filename=reportcsv.csv");
IResultSetItem resultItem = (IResultSetItem)resultSetList.get(0);
//Set the name of the element you want to retrieve.
String dispName = resultItem.getResultSetName( );
iDataExtract.selectResultSet( dispName );
iDataExtract.extract(extractionOptions);
logger.debug("Extraction successfull "+dispName);
}
else {
//with more resultSet generate a zip file containing more CSV file
try {
//Set the HTTP response
response.setContentType("application/zip");
response.setHeader("Content-disposition", "attachment; filename=reportcsv.zip");
//ZipOutputStream directly on the response OutputStream
ZipOutputStream outZip = new ZipOutputStream(responseOut);
//temporary output buffer that contain a single csv
OutputStream tempOut = new ByteArrayOutputStream();
//temporary input buffer
InputStream tempIn;
// Create a buffer for reading the files
byte[] buf = new byte[1024];
//extracted csv is writed on the temp buffer
extractionOptions.setOutputStream(tempOut);
//iterate the resultSetList
for (int i=0; i<resultSetList.size();i++){
//get an item
IResultSetItem resultItem = (IResultSetItem)resultSetList.get(i);
//Set the name of the element you want to retrieve.
String dispName = resultItem.getResultSetName( );
iDataExtract.selectResultSet( dispName );
iDataExtract.extract(extractionOptions);
logger.debug("Extraction successfull "+dispName);
// Add ZIP entry to ZIP output stream
outZip.putNextEntry(new ZipEntry("reportcsv"+i+".csv"));
//convert temp outputStream to InputStream
tempIn = new ByteArrayInputStream (((ByteArrayOutputStream) tempOut).toByteArray());
// Transfer bytes from the temp buffer to the ZIP file
int len;
while ((len = tempIn.read(buf)) > 0) {
outZip.write(buf, 0, len);
}
// Complete the entry
outZip.closeEntry();
tempIn.close();
//reset the temp output buffer
((ByteArrayOutputStream)tempOut).reset();
}
//**************************
// Complete the ZIP file
outZip.close();
}
catch (IOException e){
logger.error("Error while generating csv zip file: " + e);
}
}
//close the extract
iDataExtract.close();
//close the task
CSVtask.close();
logger.debug("Finished");
logger.debug("OUT");
}
}