logger.debug("IN");
File toReturn = null;
Locale locale = GeneralUtilities.getDefaultLocale();
DatasetMap datasets = null;
ChartImpl sbi = null;
String documentId=obj.getId().toString();
// **************get the template*****************
logger.debug("getting template");
SourceBean serviceRequest=requestContainer.getServiceRequest();
try{
//SourceBean content = getTemplate(documentId);
SourceBean content = null;
byte[] contentBytes = null;
try{
ObjTemplate template = DAOFactory.getObjTemplateDAO().getBIObjectActiveTemplate(Integer.valueOf(documentId));
if(template==null) throw new Exception("Active Template null");
contentBytes = template.getContent();
if(contentBytes==null) {
logger.error("TEMPLATE DOESN'T EXIST !!!!!!!!!!!!!!!!!!!!!!!!!!!");
EMFUserError userError = new EMFUserError(EMFErrorSeverity.ERROR, 2007);
userError.setBundle("messages");
throw userError;
}
// get bytes of template and transform them into a SourceBean
String contentStr = new String(contentBytes);
content = SourceBean.fromXMLString(contentStr);
} catch (Exception e) {
logger.error("Error while converting the Template bytes into a SourceBean object");
EMFUserError userError = new EMFUserError(EMFErrorSeverity.ERROR, 2003);
userError.setBundle("messages");
throw userError;
}
// **************take informations on the chart type*****************
String type=content.getName();
String subtype = (String)content.getAttribute("type");
//if it's an highcharts document get the png file by the svg content presents into the
// description of the biobject
if (type != null && type.startsWith(HIGHCHART_TEMPLATE)){
//create the png file using the svg
InputStream inputStream = null;
OutputStream outputStream = null;
try{
String svg = obj.getDescription();
//svg = svg.replaceAll("\\\\", "");
inputStream = new ByteArrayInputStream(svg.getBytes("UTF-8"));
File dir = new File(System.getProperty("java.io.tmpdir"));
Random generator = new Random();
int randomInt = generator.nextInt();
toReturn = File.createTempFile(Integer.valueOf(randomInt).toString(), ".png", dir);
outputStream = new FileOutputStream(toReturn);
ExportHighCharts.transformSVGIntoPNG(inputStream, outputStream);
} catch (Exception e) {
logger.error(e);
return null;
}finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
logger.error(e);
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
logger.error(e);
}
}
logger.debug("OUT");
}
logger.debug("OUT");
return toReturn;
}
String data="";
try{
logger.debug("Getting Data Set ID");
data=obj.getDataSetId().toString();
}catch (Exception e) {
logger.error("Error while getting the dataset ",e);
return null;
}
//Map parametersMap=getParametersCode(obj);
Map parametersMap=getParameters(obj);
try{
logger.debug("create the chart");
// set the right chart type
sbi=ChartImpl.createChart(type, subtype);
sbi.setProfile(userProfile);
sbi.setType(type);
sbi.setSubtype(subtype);
sbi.setData(data);
sbi.setLocale(locale);
sbi.setParametersObject(parametersMap);
// configure the chart with template parameters
sbi.configureChart(content);
sbi.setLocalizedTitle();
// Don't care for linkable charts configuration because we are building for static exporting
}
catch (Exception e) {
logger.error("Error while creating the chart",e);
EMFUserError userError = new EMFUserError(EMFErrorSeverity.ERROR, 2004);
userError.setBundle("messages");
throw userError;
}
datasets=retrieveDatasetValue(sbi);
String series=null;
String categories=null;
String catGroups=null;
String n_visualization=null;
//check if serie or categories or cat_group have been set, here you have to check among parameters
if(parametersMap.get("serie")!=null)
{// check what happens in case of multivalue!
series=parametersMap.get("serie").toString();
}
if(parametersMap.get("cat_group")!=null)
{// check what happens in case of multivalue!
catGroups=parametersMap.get("cat_group").toString();
}
if(parametersMap.get("categoryAll")!=null)
{
categories="0";
}
else if(parametersMap.get("category")!=null){
categories=parametersMap.get("category").toString();
if(categories.equals("0")) categories="1";
}
if(parametersMap.get("n_visualization")!=null)
{// check what happens in case of multivalue!
n_visualization=parametersMap.get("n_visualization").toString();
}
// Now I have to filter the dataset and draw the image!
AttributesContainer attCont=new AttributesContainer(parametersMap);
DatasetMap copyDatasets=null;
// use the same code used in chart.jsp to filter various type of chart
datasets.setSelectedSeries(new Vector());
if(sbi.getType().equalsIgnoreCase("BARCHART") || sbi.getType().equalsIgnoreCase("CLUSTERCHART")){
//if(sbi.getSubtype().equalsIgnoreCase("simplebar") || sbi.getSubtype().equalsIgnoreCase("linkableBar") || sbi.getSubtype().equalsIgnoreCase("stacked_bar") || sbi.getSubtype().equalsIgnoreCase("stacked_bar_group")){
if(sbi.getSubtype().equalsIgnoreCase("simplebar") || sbi.getSubtype().equalsIgnoreCase("linkableBar") || sbi.getSubtype().equalsIgnoreCase("stacked_bar")){
// returns a new datasets map filtered
copyDatasets=datasets.filteringSimpleBarChartUtil(attCont,attCont,(BarCharts)sbi,"WEB",true);
}
else if(sbi.getSubtype().equalsIgnoreCase("overlaid_barline") || sbi.getSubtype().equalsIgnoreCase("overlaid_stackedbarline") || sbi.getSubtype().equalsIgnoreCase("combined_category_bar")){
copyDatasets=datasets.filteringMultiDatasetBarChartUtil(attCont,attCont,(BarCharts)sbi,"WEB",true);
}
else if(sbi.getSubtype().equalsIgnoreCase("simplecluster")){
copyDatasets=datasets.filteringClusterChartUtil(attCont,(ClusterCharts)sbi,"WEB",true);
}
else if(sbi.getSubtype().equalsIgnoreCase("stacked_bar_group")){
copyDatasets=datasets.filteringGroupedBarChartUtil(attCont,attCont,(StackedBarGroup)sbi,"WEB",true);
}
}
else {copyDatasets=datasets;}
// TODO MultiCHart export
if(sbi.getMultichart()){
logger.debug("no treated yet multichart export");
}
else{
JFreeChart chart=null;
chart = sbi.createChart(copyDatasets);
String dir=System.getProperty("java.io.tmpdir");
Random generator = new Random();
int randomInt = generator.nextInt();
String path=dir+"/"+Integer.valueOf(randomInt).toString()+".png";
//String path=dir+"/"+executionId+".png";
toReturn = new java.io.File(path);
ChartUtilities.saveChartAsPNG(toReturn, chart, sbi.getWidth(), sbi.getHeight(), null);
}
}