fieldsReader = new SQLFieldsReader(sqlQuery, transaction.getSQLConnection());
try {
extractedFields = fieldsReader.readFields();
} catch (Exception e) {
logger.debug("Impossible to extract fields from query");
throw new SpagoBIEngineException("Impossible to extract fields from query: " + jpaQueryStr, e);
}
logger.debug("Fields extracted succesfully");
Assert.assertTrue(getEngineInstance().getActiveQuery().getDataMartSelectFields(true).size()+getEngineInstance().getActiveQuery().getInLineCalculatedSelectFields(true).size() == extractedFields.size(),
"The number of fields extracted from query resultset cannot be different from the number of fields specified into the query select clause");
decorateExtractedFields( extractedFields );
params = new HashMap();
params.put("pagination", getPaginationParamVaue(mimeType) );
SourceBean config = (SourceBean)ConfigSingleton.getInstance();
SourceBean baseTemplateFileSB = (SourceBean)config.getAttribute("QBE.TEMPLATE-BUILDER.BASE-TEMPLATE");
String baseTemplateFileStr = null;
if(baseTemplateFileSB != null) baseTemplateFileStr = baseTemplateFileSB.getCharacters();
File baseTemplateFile = null;
if(baseTemplateFileStr != null) baseTemplateFile = new File(baseTemplateFileStr);
templateBuilder = new TemplateBuilder(sqlQuery, extractedFields, params, baseTemplateFile);
templateContent = templateBuilder.buildTemplate();
if( !"text/jrxml".equalsIgnoreCase( mimeType ) ) {
if( "application/vnd.ms-excel".equalsIgnoreCase( mimeType ) ) {
IDataStore dataStore = null;
if (!isFormEngineInstance) {
// case of standard QBE
IDataSet dataSet = null;
Integer limit = 0;
Integer start = 0;
Integer maxSize = QbeEngineConfig.getInstance().getResultLimit();
boolean isMaxResultsLimitBlocking = QbeEngineConfig.getInstance().isMaxResultLimitBlocking();
dataSet = QbeDatasetFactory.createDataSet(statement);
dataSet.setAbortOnOverflow(isMaxResultsLimitBlocking);
Map userAttributes = new HashMap();
UserProfile profile = (UserProfile)this.getEnv().get(EngineConstants.ENV_USER_PROFILE);
Iterator it = profile.getUserAttributeNames().iterator();
while(it.hasNext()) {
String attributeName = (String)it.next();
Object attributeValue = profile.getUserAttribute(attributeName);
userAttributes.put(attributeName, attributeValue);
}
dataSet.addBinding("attributes", userAttributes);
dataSet.addBinding("parameters", this.getEnv());
logger.debug("Executing query ...");
dataSet.loadData(start, limit, (maxSize == null? -1: maxSize.intValue()));
dataStore = dataSet.getDataStore();
} else {
// case of FormEngine
JDBCDataSet dataset = new JDBCDataSet();
IDataSource datasource = (IDataSource) this.getEnv().get( EngineConstants.ENV_DATASOURCE );
dataset.setDataSource(datasource);
dataset.setUserProfileAttributes(UserProfileUtils.getProfileAttributes( (UserProfile) this.getEnv().get(EngineConstants.ENV_USER_PROFILE)));
dataset.setQuery(sqlQuery);
logger.debug("Executing query ...");
dataset.loadData();
dataStore = dataset.getDataStore();
}
Exporter exp = new Exporter(dataStore);
exp.setExtractedFields(extractedFields);
Workbook wb = exp.exportInExcel();
File file = File.createTempFile("workbook", ".xls");
FileOutputStream stream = new FileOutputStream(file);
wb.write(stream);
stream.flush();
stream.close();
try {
writeBackToClient(file, null, writeBackResponseInline, "workbook.xls", mimeType);
} catch (IOException ioe) {
throw new SpagoBIEngineException("Impossible to write back the responce to the client", ioe);
} finally{
if(file != null && file.exists()) {
try {
file.delete();
} catch (Exception e) {
logger.warn("Impossible to delete temporary file " + file, e);
}
}
}
}else{
try {
reportFile = File.createTempFile("report", ".rpt");
} catch (IOException ioe) {
throw new SpagoBIEngineException("Impossible to create a temporary file to store the template generated on the fly", ioe);
}
setJasperClasspath();
connection = transaction.getSQLConnection();
runner = new ReportRunner( );
Locale locale = this.getLocale();
try {
runner.run( templateContent, reportFile, mimeType, connection, locale);
} catch (Exception e) {
throw new SpagoBIEngineException("Impossible compile or to export the report", e);
}
try {
writeBackToClient(reportFile, null, writeBackResponseInline, "report." + fileExtension, mimeType);
} catch (IOException ioe) {
throw new SpagoBIEngineException("Impossible to write back the responce to the client", ioe);
}
}
} else {
try {
writeBackToClient(200, templateContent, writeBackResponseInline, "report." + fileExtension, mimeType);
} catch (IOException e) {
throw new SpagoBIEngineException("Impossible to write back the responce to the client", e);
}
}
} catch (Throwable t) {
throw SpagoBIEngineServiceExceptionHandler.getInstance().getWrappedException(getActionName(), getEngineInstance(), t);
} finally {