use of org.pentaho.platform.api.engine.IPentahoRequestContext in project pentaho-platform by pentaho.
the class ChartComponent method execute.
/**
* Called to process the chart definition and data set to produce a usable chart.
*
* @return state of execution. 'true' if execution was successful, otherwise false.
* @throws ChartBootException
* @throws ChartProcessingException
* @throws ResourceException
* @throws InvalidChartDefinition
* @throws IOException
* @throws PersistenceException
*/
public boolean execute() throws ChartBootException, ChartProcessingException, ResourceException, InvalidChartDefinition, IOException, PersistenceException {
if (bootException != null) {
throw new ChartBootException(bootException);
}
if (chartModel.getTheme() != null) {
AbstractChartThemeFactory chartThemeFactory = new AbstractChartThemeFactory() {
protected List<File> getThemeFiles() {
ArrayList<File> themeFiles = new ArrayList<File>();
themeFiles.add(new File(PentahoSystem.getApplicationContext().getSolutionPath(// $NON-NLS-1$
"system/chartbeans/themes/Theme1.xml")));
themeFiles.add(new File(PentahoSystem.getApplicationContext().getSolutionPath(// $NON-NLS-1$
"system/chartbeans/themes/Theme2.xml")));
themeFiles.add(new File(PentahoSystem.getApplicationContext().getSolutionPath(// $NON-NLS-1$
"system/chartbeans/themes/Theme3.xml")));
themeFiles.add(new File(PentahoSystem.getApplicationContext().getSolutionPath(// $NON-NLS-1$
"system/chartbeans/themes/Theme4.xml")));
themeFiles.add(new File(PentahoSystem.getApplicationContext().getSolutionPath(// $NON-NLS-1$
"system/chartbeans/themes/Theme5.xml")));
themeFiles.add(new File(PentahoSystem.getApplicationContext().getSolutionPath(// $NON-NLS-1$
"system/chartbeans/themes/Theme6.xml")));
themeFiles.add(new File(PentahoSystem.getApplicationContext().getSolutionPath(// $NON-NLS-1$
"system/chartbeans/themes/Theme7.xml")));
themeFiles.add(new File(PentahoSystem.getApplicationContext().getSolutionPath(// $NON-NLS-1$
"system/chartbeans/themes/Theme8.xml")));
return themeFiles;
}
};
if (!(chartModel.getPlot() instanceof DialPlot)) {
Theme chartTheme = chartThemeFactory.getTheme(chartModel.getTheme());
if (chartTheme != null) {
chartTheme.applyTo(chartModel);
}
}
}
// Make sure chart engine is loaded
loadChartEngine();
// Set chart engine on chartModel for the ChartFactory to use
chartModel.setChartEngineId(chartEngine);
InputStream is = null;
// Transform IPentahoResultSet to an object array
Object[][] data = processChartData(resultSet, valueColumn);
try {
IChartLinkGenerator chartLinkGenerator = contentLinkingTemplate == null ? null : new ChartLinkGenerator(contentLinkingTemplate);
IChartDataModel chartDataModel = ChartBeanFactory.createChartDataModel(data, scalingFactor, convertNullsToZero, valueColumn, seriesColumn, categoryColumn, chartModel, resultSet.getMetaData());
IOutput output = ChartBeanFactory.createChart(chartModel, chartDataModel, chartLinkGenerator);
// Wrap output as necessary
if (OpenFlashChartPlugin.PLUGIN_ID.equals(chartEngine)) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
output.persistChart(outputStream, getOutputType(), chartWidth, chartHeight);
// $NON-NLS-1$
String persistedChart = new String(outputStream.toByteArray(), "utf-8");
IPentahoRequestContext requestContext = PentahoRequestContextHolder.getRequestContext();
String flashContent = ChartBeansGeneratorUtil.mergeOpenFlashChartHtmlTemplate(persistedChart.replaceAll("\"", "\\\\\""), // $NON-NLS-1$ //$NON-NLS-2$
requestContext.getContextPath() + this.getSwfPath() + "/" + // $NON-NLS-1$
getSwfName());
// $NON-NLS-1$
is = new ByteArrayInputStream(flashContent.getBytes("utf-8"));
} else if (JFreeChartPlugin.PLUGIN_ID.equals(chartEngine)) {
if ("html".equals(outputType) || (chartLinkGenerator != null)) {
// $NON-NLS-1$
File imageFile = PentahoSystem.getApplicationContext().createTempFile(PentahoSessionHolder.getSession(), "tmp_chart_", ".png", // $NON-NLS-1$
false);
FileOutputStream outputStream = new FileOutputStream(imageFile);
output.persistChart(outputStream, OutputTypes.FILE_TYPE_PNG, chartWidth, chartHeight);
String imageMapName = null;
String imageMap = null;
if (chartLinkGenerator != null) {
imageMapName = imageFile.getName().substring(0, imageFile.getName().indexOf('.'));
imageMap = ((JFreeChartOutput) output).getMap(imageMapName);
}
String jFreeChartHtml = ChartBeansGeneratorUtil.mergeJFreeChartHtmlTemplate(imageFile, imageMap, imageMapName, chartWidth, chartHeight, PentahoRequestContextHolder.getRequestContext().getContextPath());
// $NON-NLS-1$
is = new ByteArrayInputStream(jFreeChartHtml.getBytes("utf-8"));
} else {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
output.persistChart(outputStream, getOutputType(), chartWidth, chartHeight);
is = new ByteArrayInputStream(outputStream.toByteArray());
}
}
int val = 0;
// TODO: Buffer for more efficiency
while ((val = is.read()) != -1) {
outputStream.write(val);
}
} catch (NoChartDataException ex) {
if (JFreeChartPlugin.PLUGIN_ID.equals(chartEngine)) {
BufferedImage image = new BufferedImage(chartWidth, chartHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = image.createGraphics();
// $NON-NLS-1$
graphics.setFont(new Font("serif", Font.BOLD, 14));
graphics.setColor(Color.BLACK);
// $NON-NLS-1$
graphics.drawString("The chart data query returned no data.", 40, 40);
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
String outputType = "png";
File imageFile = PentahoSystem.getApplicationContext().createTempFile(PentahoSessionHolder.getSession(), "tmp_chart_", ".png", // $NON-NLS-1$
false);
FileOutputStream fo = new FileOutputStream(imageFile);
ImageIO.write(image, "png", fo);
String jFreeChartHtml = ChartBeansGeneratorUtil.mergeJFreeChartHtmlTemplate(imageFile, null, null, chartWidth, chartHeight, PentahoRequestContextHolder.getRequestContext().getContextPath());
// $NON-NLS-1$
is = new ByteArrayInputStream(jFreeChartHtml.getBytes("utf-8"));
int val = 0;
while ((val = is.read()) != -1) {
outputStream.write(val);
}
} else {
String flashContent = ChartBeansGeneratorUtil.buildEmptyOpenFlashChartHtmlFragment("The chart data query returned no data.");
// $NON-NLS-1$
// $NON-NLS-1$
is = new ByteArrayInputStream(flashContent.getBytes("utf-8"));
int val = 0;
// TODO: Buffer for more efficiency
while ((val = is.read()) != -1) {
outputStream.write(val);
}
}
} catch (ChartDataOverflowException ex) {
if (JFreeChartPlugin.PLUGIN_ID.equals(chartEngine)) {
BufferedImage image = new BufferedImage(chartWidth, chartHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = image.createGraphics();
// $NON-NLS-1$
graphics.setFont(new Font("serif", Font.BOLD, 14));
graphics.setColor(Color.BLACK);
// $NON-NLS-1$
graphics.drawString(Messages.getInstance().getErrorString("ChartComponent.TOO_MANY_DATA_POINTS"), 5, 5);
graphics.drawString(Messages.getInstance().getErrorString("ChartComponent.MAX_ALLOWED_DATA_POINTS", Integer.toString(ex.getMaxAllowedDataPoints())), 5, // $NON-NLS-1$
25);
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
String outputType = getMimeType().equals("image/jpg") ? "jpeg" : "png";
ImageIO.write(image, outputType, outputStream);
} else {
String flashContent = ChartBeansGeneratorUtil.buildEmptyOpenFlashChartHtmlFragment(Messages.getInstance().getErrorString("ChartComponent.TOO_MANY_DATA_POINTS_HTML", // $NON-NLS-1$
Integer.toString(ex.getMaxAllowedDataPoints())));
// $NON-NLS-1$
is = new ByteArrayInputStream(flashContent.getBytes("utf-8"));
int val = 0;
// TODO: Buffer for more efficiency
while ((val = is.read()) != -1) {
outputStream.write(val);
}
}
}
return true;
}
use of org.pentaho.platform.api.engine.IPentahoRequestContext in project pentaho-platform by pentaho.
the class WidgetGridComponent method createDials.
protected Document createDials(final IPentahoResultSet resultSet, final WidgetDefinition widgetDefinition) {
if (resultSet == null) {
// $NON-NLS-1$
error(Messages.getInstance().getErrorString("WidgetGrid.ERROR_0001_NO_RESULTS_FROM_ACTION"));
return null;
}
if (valueItem == null) {
// $NON-NLS-1$
error(Messages.getInstance().getErrorString("WidgetGrid.ERROR_0002_NO_VALUE_ITEM"));
}
// Create a document that describes the result
Document result = DocumentHelper.createDocument();
IPentahoRequestContext requestContext = PentahoRequestContextHolder.getRequestContext();
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
setXslProperty("baseUrl", requestContext.getContextPath());
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
setXslProperty("fullyQualifiedServerUrl", PentahoSystem.getApplicationContext().getFullyQualifiedServerURL());
// $NON-NLS-1$
Element root = result.addElement("widgets");
IPentahoMetaData metaData = resultSet.getMetaData();
// TODO support multiple column headers / row headers
// TODO support an iteration across columns for a given row
// find the column that we have been told to you
Object[][] columnHeaders = metaData.getColumnHeaders();
int nameColumnNo = -1;
int valueColumnNo = -1;
for (int idx = 0; idx < columnHeaders[0].length; idx++) {
if (columnHeaders[0][idx].toString().equalsIgnoreCase(nameItem)) {
nameColumnNo = idx;
}
if (columnHeaders[0][idx].toString().equalsIgnoreCase(valueItem)) {
valueColumnNo = idx;
}
}
if (nameColumnNo == -1) {
// we did not find the specified name column
// $NON-NLS-1$
error(Messages.getInstance().getErrorString("WidgetGrid.ERROR_0004_NAME_COLUMN_MISSING", nameItem));
return null;
}
if (valueColumnNo == -1) {
// we did not find the specified name column
// $NON-NLS-1$
error(Messages.getInstance().getErrorString("WidgetGrid.ERROR_0005_VALUE_COLUMN_MISSING", valueItem));
return null;
}
double value;
String name;
Object[] row = resultSet.next();
while (row != null) {
name = row[nameColumnNo].toString();
try {
value = Double.parseDouble(row[valueColumnNo].toString());
createDial(value, name, root, widgetDefinition);
} catch (Exception e) {
// ignore
}
row = resultSet.next();
}
// $NON-NLS-1$ //$NON-NLS-2$
setXslProperty("urlTarget", "pentaho_popup");
// $NON-NLS-1$
setXslProperty("columns", Integer.toString(columns));
if (style != null) {
// $NON-NLS-1$
setXslProperty("style", style);
}
return result;
}
Aggregations