Search in sources :

Example 26 with IPentahoRequestContext

use of org.pentaho.platform.api.engine.IPentahoRequestContext in project pentaho-platform by pentaho.

the class ViewAction method doGet.

@Override
protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
    PentahoSystem.systemEntryPoint();
    try {
        IPentahoSession userSession = getPentahoSession(request);
        if (!doBackgroundExecution(request, response, userSession)) {
            OutputStream outputStream = getOutputStream(response, doMessages(request));
            ActionSequenceJCRHelper actionHelper = new ActionSequenceJCRHelper(userSession);
            // $NON-NLS-1$
            String actionPath = request.getParameter("path");
            IActionSequence actionSequence = actionHelper.getActionSequence(actionPath, PentahoSystem.loggingLevel, RepositoryFilePermission.READ);
            String fileName = null;
            if (actionSequence != null) {
                String title = actionSequence.getTitle();
                if ((title != null) && (title.length() > 0)) {
                    fileName = title;
                } else {
                    String sequenceName = actionSequence.getSequenceName();
                    if ((sequenceName != null) && (sequenceName.length() > 0)) {
                        fileName = sequenceName;
                    } else {
                        List actionDefinitionsList = actionSequence.getActionDefinitionsAndSequences();
                        int i = 0;
                        boolean done = false;
                        while ((actionDefinitionsList.size() > i) && !done) {
                            IActionDefinition actionDefinition = (IActionDefinition) actionDefinitionsList.get(i);
                            String componentName = actionDefinition.getComponentName();
                            if ((componentName != null) && (componentName.length() > 0)) {
                                fileName = componentName;
                                done = true;
                            } else {
                                i++;
                            }
                        }
                    }
                }
            }
            IPentahoRequestContext requestContext = PentahoRequestContextHolder.getRequestContext();
            HttpOutputHandler outputHandler = createOutputHandler(response, outputStream);
            outputHandler.setSession(userSession);
            IMimeTypeListener listener = new HttpMimeTypeListener(request, response, fileName);
            outputHandler.setMimeTypeListener(listener);
            // $NON-NLS-1$
            SimpleUrlFactory urlFactory = new SimpleUrlFactory(requestContext.getContextPath() + "ViewAction?");
            IParameterProvider requestParameters = new HttpRequestParameterProvider(request);
            HttpServletRequestHandler requestHandler = getRequestHandler(request, response, userSession, requestParameters, outputStream, outputHandler, urlFactory);
            handleActionRequest(request, response, outputHandler, requestHandler, outputStream, null);
        }
    } finally {
        PentahoSystem.systemExitPoint();
    }
}
Also used : IMimeTypeListener(org.pentaho.platform.api.engine.IMimeTypeListener) IActionSequence(org.pentaho.platform.api.engine.IActionSequence) IActionDefinition(org.pentaho.actionsequence.dom.IActionDefinition) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) HttpOutputHandler(org.pentaho.platform.web.http.HttpOutputHandler) IPentahoRequestContext(org.pentaho.platform.api.engine.IPentahoRequestContext) IParameterProvider(org.pentaho.platform.api.engine.IParameterProvider) HttpRequestParameterProvider(org.pentaho.platform.web.http.request.HttpRequestParameterProvider) List(java.util.List) ActionSequenceJCRHelper(org.pentaho.platform.engine.services.ActionSequenceJCRHelper) SimpleUrlFactory(org.pentaho.platform.util.web.SimpleUrlFactory)

Example 27 with IPentahoRequestContext

use of org.pentaho.platform.api.engine.IPentahoRequestContext in project pentaho-platform by pentaho.

the class ReportingIT method testActionComponent.

/*
   * public void testBIRTReport1() { startTest(); SimpleParameterProvider parameterProvider = new
   * SimpleParameterProvider(); parameterProvider.setParameter("type", "html"); //$NON-NLS-1$ //$NON-NLS-2$ OutputStream
   * outputStream = getOutputStream("ReportingTest.testBIRTReport1-a", ".html"); //$NON-NLS-1$ //$NON-NLS-2$
   * SimpleOutputHandler outputHandler = new SimpleOutputHandler(outputStream, true); StandaloneSession session = new
   * StandaloneSession(Messages.getString("BaseTest.DEBUG_JUNIT_SESSION")); //$NON-NLS-1$ IRuntimeContext context = run(
   * "test", "reporting", "quadrant-budget-hsql.xaction", null, false, parameterProvider, outputHandler, session);
   * //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ assertEquals(
   * Messages.getString("BaseTest.USER_RUNNING_ACTION_SEQUENCE"), IRuntimeContext.RUNTIME_STATUS_SUCCESS,
   * context.getStatus()); //$NON-NLS-1$ outputStream = getOutputStream("ReportingTest.testBIRTReport1-b", ".pdf");
   * //$NON-NLS-1$ //$NON-NLS-2$ outputHandler = new SimpleOutputHandler(outputStream, true);
   * parameterProvider.setParameter("type", "pdf"); //$NON-NLS-1$ //$NON-NLS-2$ context = run( "test", "reporting",
   * "quadrant-budget-hsql.xaction", null, false, parameterProvider, outputHandler, session); //$NON-NLS-1$
   * //$NON-NLS-2$ //$NON-NLS-3$ assertEquals( Messages.getString("BaseTest.USER_RUNNING_ACTION_SEQUENCE"),
   * IRuntimeContext.RUNTIME_STATUS_SUCCESS, context.getStatus()); //$NON-NLS-1$ finishTest(); }
   * 
   * public void testBIRTReport4() { startTest(); SimpleParameterProvider parameterProvider = new
   * SimpleParameterProvider(); parameterProvider.setParameter("type", "html"); //$NON-NLS-1$ //$NON-NLS-2$
   * parameterProvider.setParameter("REGION", "Eastern"); //$NON-NLS-1$ //$NON-NLS-2$
   * parameterProvider.setParameter("DEPARTMENT", "Finance"); //$NON-NLS-1$ //$NON-NLS-2$ OutputStream outputStream =
   * getOutputStream("ReportingTest.testBIRTReport4", ".html"); //$NON-NLS-1$ //$NON-NLS-2$ SimpleOutputHandler
   * outputHandler = new SimpleOutputHandler(outputStream, true); StandaloneSession session = new
   * StandaloneSession(Messages.getString("BaseTest.DEBUG_JUNIT_SESSION")); //$NON-NLS-1$ IRuntimeContext context = run(
   * "test", "reporting", "quadrant-budget-for-region-and-dept-to-repository.xaction", null, false, parameterProvider,
   * outputHandler, session); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ assertEquals(
   * Messages.getString("BaseTest.USER_RUNNING_ACTION_SEQUENCE"), IRuntimeContext.RUNTIME_STATUS_SUCCESS,
   * context.getStatus()); //$NON-NLS-1$ // TODO need some validation of success finishTest(); }
   * 
   * public void testBIRTIntparm() { startTest(); SimpleParameterProvider parameterProvider = new
   * SimpleParameterProvider(); parameterProvider.setParameter("type", "html"); //$NON-NLS-1$ //$NON-NLS-2$
   * parameterProvider.setParameter("intparm", "300000"); //$NON-NLS-1$ //$NON-NLS-2$ OutputStream outputStream =
   * getOutputStream("ReportingTest.testIntParm", ".html"); //$NON-NLS-1$ //$NON-NLS-2$ SimpleOutputHandler
   * outputHandler = new SimpleOutputHandler(outputStream, true); StandaloneSession session = new
   * StandaloneSession(Messages.getString("BaseTest.DEBUG_JUNIT_SESSION")); //$NON-NLS-1$ IRuntimeContext context = run(
   * "test", "reporting", "BIRT-quadrant-budget-hsql-intparm.xaction", null, false, parameterProvider, outputHandler,
   * session); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ assertEquals(
   * Messages.getString("BaseTest.USER_RUNNING_ACTION_SEQUENCE"), IRuntimeContext.RUNTIME_STATUS_SUCCESS,
   * context.getStatus()); //$NON-NLS-1$ // TODO need some validation of success finishTest(); }
   */
/*
   * public void testJasperReports1() { startTest(); SimpleParameterProvider parameterProvider = new
   * SimpleParameterProvider(); parameterProvider.setParameter("REGION", "Eastern"); //$NON-NLS-1$ //$NON-NLS-2$
   * OutputStream outputStream = getOutputStream("ReportingTest.testJasperReports1", ".html"); //$NON-NLS-1$
   * //$NON-NLS-2$ SimpleOutputHandler outputHandler = new SimpleOutputHandler(outputStream, true); StandaloneSession
   * session = new StandaloneSession(Messages.getString("BaseTest.DEBUG_JUNIT_SESSION")); //$NON-NLS-1$ IRuntimeContext
   * context = run( "test", "reporting", "jasper-reports-test-1.xaction", null, false, parameterProvider, outputHandler,
   * session); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ assertEquals(
   * Messages.getString("BaseTest.USER_RUNNING_ACTION_SEQUENCE"), IRuntimeContext.RUNTIME_STATUS_SUCCESS,
   * context.getStatus()); //$NON-NLS-1$ // TODO need some validation of success finishTest(); }
   * 
   * public void testJasperReports2() { startTest(); SimpleParameterProvider parameterProvider = new
   * SimpleParameterProvider(); parameterProvider.setParameter("REGION", "Eastern"); //$NON-NLS-1$ //$NON-NLS-2$
   * parameterProvider.setParameter("type", "pdf"); //$NON-NLS-1$ //$NON-NLS-2$ OutputStream outputStream =
   * getOutputStream("ReportingTest.testJasperReports2", ".pdf"); //$NON-NLS-1$ //$NON-NLS-2$ SimpleOutputHandler
   * outputHandler = new SimpleOutputHandler(outputStream, true); StandaloneSession session = new
   * StandaloneSession(Messages.getString("BaseTest.DEBUG_JUNIT_SESSION")); //$NON-NLS-1$ IRuntimeContext context = run(
   * "test", "reporting", "jasper-reports-test-2.xaction", null, false, parameterProvider, outputHandler, session);
   * //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ assertEquals(
   * Messages.getString("BaseTest.USER_RUNNING_ACTION_SEQUENCE"), IRuntimeContext.RUNTIME_STATUS_SUCCESS,
   * context.getStatus()); //$NON-NLS-1$ // TODO need some validation of success finishTest(); }
   */
/*
   * public void testBIRTReport3() { startTest(); SimpleParameterProvider parameterProvider = new
   * SimpleParameterProvider(); parameterProvider.setParameter("type", "pdf"); //$NON-NLS-1$ //$NON-NLS-2$ OutputStream
   * outputStream = getOutputStream("ReportingTest.testBIRTReport3", ".pdf"); //$NON-NLS-1$ //$NON-NLS-2$
   * SimpleOutputHandler outputHandler = new SimpleOutputHandler(outputStream, true); StandaloneSession session = new
   * StandaloneSession(Messages.getString("BaseTest.DEBUG_JUNIT_SESSION")); //$NON-NLS-1$ IRuntimeContext context = run(
   * "test", "reporting", "quadrant-budget-for-region-hsql.xaction", null, false, parameterProvider, outputHandler,
   * session); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ assertEquals(
   * Messages.getString("BaseTest.USER_RUNNING_ACTION_SEQUENCE"), IRuntimeContext.RUNTIME_STATUS_SUCCESS,
   * context.getStatus()); //$NON-NLS-1$ finishTest(); }
   */
public void testActionComponent() {
    startTest();
    IPentahoRequestContext requestContext = PentahoRequestContextHolder.getRequestContext();
    IPentahoUrlFactory urlFactory = new SimpleUrlFactory(requestContext.getContextPath());
    ArrayList messages = new ArrayList();
    SimpleParameterProvider parameterProvider = new SimpleParameterProvider();
    // $NON-NLS-1$ //$NON-NLS-2$
    parameterProvider.setParameter("solution", "test");
    // $NON-NLS-1$ //$NON-NLS-2$
    parameterProvider.setParameter("path", "reporting");
    // $NON-NLS-1$ //$NON-NLS-2$
    parameterProvider.setParameter("action", "custom-parameter-page-example.xaction");
    ActionComponent component = new ActionComponent("test/reporting/custom-parameter-page-example.xaction", null, IOutputHandler.OUTPUT_TYPE_DEFAULT, urlFactory, // $NON-NLS-1$
    messages);
    component.setParameterProvider(IParameterProvider.SCOPE_REQUEST, parameterProvider);
    StandaloneSession session = // $NON-NLS-1$
    new StandaloneSession(Messages.getInstance().getString("BaseTest.DEBUG_JUNIT_SESSION"));
    component.validate(session, null);
    // $NON-NLS-1$ //$NON-NLS-2$
    OutputStream outputStream = getOutputStream("ReportingTest.testActionComponent", ".html");
    // $NON-NLS-1$
    String content = component.getContent("text/html");
    try {
        outputStream.write(content.getBytes());
    } catch (Exception e) {
    // ignore
    }
    finishTest();
}
Also used : IPentahoRequestContext(org.pentaho.platform.api.engine.IPentahoRequestContext) IPentahoUrlFactory(org.pentaho.platform.api.engine.IPentahoUrlFactory) StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) ActionComponent(org.pentaho.platform.uifoundation.component.ActionComponent) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) SimpleUrlFactory(org.pentaho.platform.util.web.SimpleUrlFactory) SimpleParameterProvider(org.pentaho.platform.engine.core.solution.SimpleParameterProvider)

Example 28 with IPentahoRequestContext

use of org.pentaho.platform.api.engine.IPentahoRequestContext in project pentaho-platform by pentaho.

the class DefaultChartBeansGenerator method createChartAsHtml.

public String createChartAsHtml(IPentahoSession userSession, Map<String, Object> parameterMap, String serializedChartDataDefinition, String serializedChartModel, int chartWidth, int chartHeight) throws IOException {
    ChartModel chartModel = ChartSerializer.deSerialize(serializedChartModel, ChartSerializationFormat.JSON);
    String html = null;
    if (chartModel.getChartEngineId() == null) {
        // Load default value from system setting or take hard coded
        // Hard coded final fall back is Open Flash Chart
        String defaultChartEngine = PentahoSystem.getSystemSetting("chartbeans/chartbeans_config.xml", "default-chart-engine", // $NON-NLS-1$ //$NON-NLS-2$
        OpenFlashChartPlugin.PLUGIN_ID);
        if (defaultChartEngine == null) {
            defaultChartEngine = OpenFlashChartPlugin.PLUGIN_ID;
        }
        chartModel.setChartEngineId(defaultChartEngine);
    }
    // Check for render engine override
    String override = (String) parameterMap.get("renderEngine");
    if (override != null) {
        chartModel.setChartEngineId(override);
    }
    serializedChartModel = ChartSerializer.serialize(chartModel, ChartSerializationFormat.JSON);
    if (JFreeChartPlugin.PLUGIN_ID.equals(chartModel.getChartEngineId())) {
        // $NON-NLS-1$
        final String SOLUTION_TMP_DIR = "system/tmp/";
        File chartFileOnServer = new File(new File(PentahoSystem.getApplicationContext().getFileOutputPath(SOLUTION_TMP_DIR)), java.util.UUID.randomUUID().toString());
        BufferedOutputStream bos = null;
        try {
            bos = new BufferedOutputStream(new FileOutputStream(chartFileOnServer));
            this.internalCreateChart(userSession, parameterMap, serializedChartDataDefinition, serializedChartModel, chartWidth, chartHeight, null, bos);
        } finally {
            IOUtils.closeQuietly(bos);
        }
        IPentahoRequestContext requestContext = PentahoRequestContextHolder.getRequestContext();
        String contextPath = requestContext.getContextPath();
        // $NON-NLS-1$ //$NON-NLS-2$
        String url = contextPath + "/";
        // $NON-NLS-1$
        final String IMAGE_URL_TEMPLATE = "{0}getImage?image={1}";
        final String imageUrl = MessageFormat.format(IMAGE_URL_TEMPLATE, new String[] { url, chartFileOnServer.getName() });
        html = this.mergeStaticImageHtmlTemplate(imageUrl);
    } else if (OpenFlashChartPlugin.PLUGIN_ID.equals(chartModel.getChartEngineId())) {
        ByteArrayOutputStream tmpOut = new ByteArrayOutputStream();
        this.internalCreateChart(userSession, parameterMap, serializedChartDataDefinition, serializedChartModel, chartWidth, chartHeight, null, tmpOut);
        // $NON-NLS-1$
        final String ENCODING = "UTF-8";
        ByteArrayInputStream in = new ByteArrayInputStream(tmpOut.toByteArray());
        IOUtils.closeQuietly(tmpOut);
        html = IOUtils.toString(in, ENCODING);
        IOUtils.closeQuietly(in);
    } else {
        // $NON-NLS-1$
        throw new IllegalArgumentException("unrecognized chart engine");
    }
    return html;
}
Also used : IPentahoRequestContext(org.pentaho.platform.api.engine.IPentahoRequestContext) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) ChartModel(org.pentaho.chart.model.ChartModel) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 29 with IPentahoRequestContext

use of org.pentaho.platform.api.engine.IPentahoRequestContext in project pentaho-platform by pentaho.

the class DefaultChartBeansGenerator method runActionSequence.

/**
 * Executes an action sequence from an <code>ActionSequenceDocument</code>.
 *
 * @param pentahoSession
 *          current <code>IPentahoSession</code>
 * @param parameterProviders
 *          map of parameter providers; there should a single entry with "request" as the key
 * @param outputHandler
 *          output handler
 * @param doc
 *          action sequence document
 * @throws RuntimeException
 *           if anything goes wrong
 */
protected void runActionSequence(final IPentahoSession pentahoSession, final Map<String, IParameterProvider> parameterProviders, final IOutputHandler outputHandler, final ActionSequenceDocument doc) throws RuntimeException {
    // Get the solution engine
    ISolutionEngine solutionEngine = PentahoSystem.get(ISolutionEngine.class, pentahoSession);
    if (solutionEngine == null) {
        // $NON-NLS-1$
        throw new RuntimeException("solutionEngine is null");
    }
    solutionEngine.setLoggingLevel(ILogger.DEBUG);
    solutionEngine.init(pentahoSession);
    IPentahoRequestContext requestContext = PentahoRequestContextHolder.getRequestContext();
    String contextPath = requestContext.getContextPath();
    // $NON-NLS-1$
    IPentahoUrlFactory urlFactory = new SimpleUrlFactory(contextPath);
    IRuntimeContext runtime;
    IParameterProvider requestParmProvider = parameterProviders.get("request");
    if (requestParmProvider.hasParameter("obj_id")) {
        final String obj_id = (String) requestParmProvider.getParameter("obj_id");
        final String msg_name = (String) requestParmProvider.getParameter("message_name");
        final String job_id = (String) requestParmProvider.getParameter("job_id");
        runtime = // $NON-NLS-1$ //$NON-NLS-2$
        solutionEngine.execute(// $NON-NLS-1$ //$NON-NLS-2$
        doc.toString(), // $NON-NLS-1$ //$NON-NLS-2$
        obj_id, // $NON-NLS-1$ //$NON-NLS-2$
        job_id, // $NON-NLS-1$ //$NON-NLS-2$
        false, // $NON-NLS-1$ //$NON-NLS-2$
        true, msg_name, true, parameterProviders, outputHandler, null, urlFactory, // $NON-NLS-1$
        new ArrayList());
    } else {
        runtime = // $NON-NLS-1$ //$NON-NLS-2$
        solutionEngine.execute(// $NON-NLS-1$ //$NON-NLS-2$
        doc.toString(), // $NON-NLS-1$ //$NON-NLS-2$
        "chartbeans_mql", // $NON-NLS-1$ //$NON-NLS-2$
        "myprocessid", // $NON-NLS-1$ //$NON-NLS-2$
        false, // $NON-NLS-1$ //$NON-NLS-2$
        true, "myinstanceid", true, parameterProviders, outputHandler, null, urlFactory, // $NON-NLS-1$
        new ArrayList());
    }
    if ((runtime != null) && (runtime.getStatus() != IRuntimeContext.RUNTIME_STATUS_SUCCESS)) {
        StringBuilder buf = new StringBuilder();
        boolean firstIteration = true;
        for (Object /* String */
        message : runtime.getMessages()) {
            if (message instanceof Exception) {
                Exception ex = (Exception) message;
                if (ex.getCause() instanceof RuntimeException) {
                    throw (RuntimeException) ex.getCause();
                }
            }
            if (!firstIteration) {
                // $NON-NLS-1$
                buf.append(" \\\\ ");
            }
            buf.append(message);
        }
        String errorStr;
        if (buf.indexOf("action_sequence_failed") > -1 && buf.indexOf("MQLRelationalDataComponent") > -1) {
            errorStr = Messages.getInstance().getString("DefaultChartBeansGenerator.ERROR_0001_SECURITY_ERROR");
        } else {
            errorStr = Messages.getInstance().getString("DefaultChartBeansGenerator.ERROR_0002_UNKNOWN_ERROR");
        }
        throw new RuntimeException(errorStr);
    }
}
Also used : ISolutionEngine(org.pentaho.platform.api.engine.ISolutionEngine) IPentahoUrlFactory(org.pentaho.platform.api.engine.IPentahoUrlFactory) ArrayList(java.util.ArrayList) IOException(java.io.IOException) IPentahoRequestContext(org.pentaho.platform.api.engine.IPentahoRequestContext) IParameterProvider(org.pentaho.platform.api.engine.IParameterProvider) SimpleUrlFactory(org.pentaho.platform.util.web.SimpleUrlFactory) IRuntimeContext(org.pentaho.platform.api.engine.IRuntimeContext)

Example 30 with IPentahoRequestContext

use of org.pentaho.platform.api.engine.IPentahoRequestContext in project pentaho-platform by pentaho.

the class ChartAction method execute.

/**
 * Called to process the chart definition and data set to produce a usable chart.
 *
 * @throws ChartBootException
 * @throws ChartProcessingException
 * @throws ResourceException
 * @throws InvalidChartDefinition
 * @throws IOException
 * @throws PersistenceException
 * @see org.pentaho.platform.api.action.IAction#execute()
 */
public void execute() throws Exception {
    // 
    // Runtime value validation is now part of the execute operation
    // 
    validate();
    if (bootException != null) {
        throw new ChartBootException(bootException);
    }
    // Transform IPentahoResultSet to an object array
    Object[][] data = processChartData(chartData, valueColumn);
    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")));
                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;
    try {
        IChartLinkGenerator chartLinkGenerator = contentLinkingTemplate == null ? null : new ChartLinkGenerator(contentLinkingTemplate);
        is = ChartBeanFactory.createChart(data, scalingFactor, convertNullsToZero, valueColumn, seriesColumn, categoryColumn, chartModel, chartLinkGenerator, chartWidth, chartHeight, getOutputType());
        // Wrap output as necessary
        if (OpenFlashChartPlugin.PLUGIN_ID.equals(chartEngine)) {
            // Convert stream to string, insert into HTML fragment and re-stream it
            StringBuilder sb = new StringBuilder();
            int c = 0;
            // Build string
            while ((c = is.read()) >= 0) {
                sb.append((char) c);
            }
            IPentahoRequestContext requestContext = PentahoRequestContextHolder.getRequestContext();
            String contextPath = requestContext.getContextPath();
            String flashContent = ChartBeansGeneratorUtil.mergeOpenFlashChartHtmlTemplate(// $NON-NLS-1$ //$NON-NLS-2$
            sb.toString().replaceAll("\"", "\\\\\""), // $NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
            contextPath + "/" + this.getSwfPath() + "/" + getSwfName());
            // $NON-NLS-1$
            is = new ByteArrayInputStream(flashContent.getBytes("utf-8"));
        }
        int val = 0;
        // TODO: Buffer for more efficiency
        while ((val = is.read()) != -1) {
            chartContentStream.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.", 5, 5);
            String outputType = // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            getMimeType(null).equals("image/jpg") ? "jpeg" : "png";
            ImageIO.write(image, outputType, chartContentStream);
        } else {
            String flashContent = ChartBeansGeneratorUtil.buildEmptyOpenFlashChartHtmlFragment(// $NON-NLS-1$
            "The chart data query returned no data.");
            // $NON-NLS-1$
            is = new ByteArrayInputStream(flashContent.getBytes("utf-8"));
            int val = 0;
            // TODO: Buffer for more efficiency
            while ((val = is.read()) != -1) {
                chartContentStream.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);
            graphics.drawString(Messages.getInstance().getErrorString("ChartAction.TOO_MANY_DATA_POINTS"), 5, // $NON-NLS-1$
            5);
            graphics.drawString(Messages.getInstance().getErrorString("ChartAction.MAX_ALLOWED_DATA_POINTS", Integer.toString(ex.getMaxAllowedDataPoints())), 5, // $NON-NLS-1$
            25);
            String outputType = // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            getMimeType(null).equals("image/jpg") ? "jpeg" : "png";
            ImageIO.write(image, outputType, chartContentStream);
        } else {
            String flashContent = ChartBeansGeneratorUtil.buildEmptyOpenFlashChartHtmlFragment(Messages.getInstance().getErrorString("ChartAction.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) {
                chartContentStream.write(val);
            }
        }
    }
}
Also used : IChartLinkGenerator(org.pentaho.chart.IChartLinkGenerator) ChartDataOverflowException(org.pentaho.chart.plugin.ChartDataOverflowException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NoChartDataException(org.pentaho.chart.plugin.NoChartDataException) ArrayList(java.util.ArrayList) IChartLinkGenerator(org.pentaho.chart.IChartLinkGenerator) AbstractChartThemeFactory(org.pentaho.chart.AbstractChartThemeFactory) DialPlot(org.pentaho.chart.model.DialPlot) BufferedImage(java.awt.image.BufferedImage) Font(java.awt.Font) Graphics2D(java.awt.Graphics2D) IPentahoRequestContext(org.pentaho.platform.api.engine.IPentahoRequestContext) ByteArrayInputStream(java.io.ByteArrayInputStream) Theme(org.pentaho.chart.model.Theme) File(java.io.File)

Aggregations

IPentahoRequestContext (org.pentaho.platform.api.engine.IPentahoRequestContext)32 SimpleUrlFactory (org.pentaho.platform.util.web.SimpleUrlFactory)11 ArrayList (java.util.ArrayList)9 Document (org.dom4j.Document)9 Element (org.dom4j.Element)9 IOException (java.io.IOException)8 PrintWriter (java.io.PrintWriter)8 StringWriter (java.io.StringWriter)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 ChartRenderingInfo (org.jfree.chart.ChartRenderingInfo)7 StandardEntityCollection (org.jfree.chart.entity.StandardEntityCollection)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 File (java.io.File)5 OutputStream (java.io.OutputStream)5 List (java.util.List)5 IParameterProvider (org.pentaho.platform.api.engine.IParameterProvider)5 SimpleParameterProvider (org.pentaho.platform.engine.core.solution.SimpleParameterProvider)5 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)4 IPentahoUrlFactory (org.pentaho.platform.api.engine.IPentahoUrlFactory)4