}
private SDKExecutedDocumentContent executeKpi(SDKDocument document, BIObject biobj, String userId,String ouputType){
logger.debug("IN");
SDKExecutedDocumentContent toReturn=null;
SourceBean request = null;
SourceBean resp = null;
EMFErrorHandler errorHandler = null;
try {
request = new SourceBean("");
resp = new SourceBean("");
} catch (SourceBeanException e1) {
logger.error("Source Bean Exception");
return null;
}
RequestContainer reqContainer = new RequestContainer();
ResponseContainer resContainer = new ResponseContainer();
reqContainer.setServiceRequest(request);
resContainer.setServiceResponse(resp);
DefaultRequestContext defaultRequestContext = new DefaultRequestContext(
reqContainer, resContainer);
resContainer.setErrorHandler(new EMFErrorHandler());
RequestContainer.setRequestContainer(reqContainer);
ResponseContainer.setResponseContainer(resContainer);
SessionContainer session = new SessionContainer(true);
reqContainer.setSessionContainer(session);
errorHandler = defaultRequestContext.getErrorHandler();
Engine engine;
try {
engine = DAOFactory.getEngineDAO().loadEngineByID(document.getEngineId());
} catch (EMFUserError e1) {
logger.error("Error while retrieving engine", e1);
return null;
}
if(engine==null){
logger.error("No engine found");
return null;
}
String className = engine.getClassName();
logger.debug("Try instantiating class " + className
+ " for internal engine " + engine.getName() + "...");
InternalEngineIFace internalEngine = null;
// tries to instantiate the class for the internal engine
try {
if (className == null && className.trim().equals("")) throw new ClassNotFoundException();
internalEngine = (InternalEngineIFace) Class.forName(className).newInstance();
} catch (ClassNotFoundException cnfe) {
logger.error("The class ['" + className
+ "'] for internal engine " + engine.getName()
+ " was not found.", cnfe);
return null;
} catch (Exception e) {
logger.error("Error while instantiating class " + className, e);
return null;
}
// result of the Kpi
List<KpiResourceBlock> blocksList=null;
try {
blocksList=((SpagoBIKpiInternalEngine)internalEngine).executeCode(reqContainer, biobj, resp, userId);
if(blocksList==null){
logger.error("No result returned by kpi execution");
return null;
}
else{
logger.debug("Kpi executed and result returned");
}
} catch (EMFUserError e) {
logger.error("Error during engine execution", e);
return null;
} catch (Exception e) {
logger.error("Error while engine execution", e);
return null;
}
File tmpFile=null;
String mimeType = "application/pdf";
logger.debug("setting object to return of type SDKExecuteDocumentContent");
toReturn=new SDKExecutedDocumentContent();
// call exporter!
try{
KpiExporter exporter=new KpiExporter();
if(ouputType.equals("PDF")){
logger.debug("call PDF Exporter");
tmpFile=exporter.getKpiReportPDF(blocksList, biobj, userId);
toReturn.setFileName(biobj.getLabel()+".pdf");
}else if (ouputType.equals("XML")){
mimeType = "text/xml";
logger.debug("call XML Exporter");
tmpFile=exporter.getKpiExportXML(blocksList, biobj, userId);
toReturn.setFileName(biobj.getLabel()+".xml");
}
}
catch (Exception e) {
logger.error("error while exporting",e);
return null;
}
if(tmpFile==null){
logger.error("file not created");
return null;
}
else{
logger.debug("file created");
}
try{
FileDataSource mods = new FileDataSource(tmpFile);
toReturn.setFileType(mimeType);
DataHandler dhSource = new DataHandler(mods);
toReturn.setContent(dhSource);
}
finally{
logger.debug("deleting file Tmp");
logger.debug("file Tmp deleted");