Search in sources :

Example 11 with PentahoEntityResolver

use of org.pentaho.platform.engine.services.solution.PentahoEntityResolver in project pentaho-platform by pentaho.

the class OpenFlashChartComponent method executeAction.

protected boolean executeAction() {
    // $NON-NLS-1$
    log.debug("start to run open flash chart component......");
    // input data
    IPentahoResultSet data = (IPentahoResultSet) getInputValue(CHART_DATA);
    if (!data.isScrollable()) {
        // $NON-NLS-1$
        getLogger().debug("ResultSet is not scrollable. Copying into memory");
        IPentahoResultSet memSet = data.memoryCopy();
        data.close();
        data = memSet;
    }
    // chart width
    String chartWidth = null;
    String inputWidth = getInputStringValue(CHART_WIDTH);
    if (inputWidth == null) {
        chartWidth = DEFAULT_WIDTH;
    } else {
        chartWidth = inputWidth;
    }
    // chart height
    String chartHeight = null;
    String inputHeight = getInputStringValue(CHART_HEIGHT);
    if (null == inputHeight) {
        chartHeight = DEFAULT_HEIGHT;
    } else {
        chartHeight = inputHeight;
    }
    // swf file location
    String ofcURL = getInputStringValue(OFC_URL);
    if (ofcURL == null || "".equals(ofcURL)) {
        // $NON-NLS-1$
        ofcURL = PentahoRequestContextHolder.getRequestContext().getContextPath() + DEFAULT_FLASH_LOC;
    }
    // swf file name
    String ofclibname = getInputStringValue(OFC_LIB_NAME);
    if (ofclibname == null || "".equals(ofclibname)) {
        // $NON-NLS-1$
        ofclibname = DEFAULT_FLASH_SWF;
    }
    // chart definition
    String chartAttributeString = null;
    if (getInputNames().contains(CHART_ATTRIBUTES)) {
        chartAttributeString = getInputStringValue(CHART_ATTRIBUTES);
    } else if (isDefinedResource(CHART_ATTRIBUTES)) {
        IActionSequenceResource resource = getResource(CHART_ATTRIBUTES);
        chartAttributeString = getResourceAsString(resource);
    }
    Node chartNode = null;
    if (chartAttributeString != null) {
        // apply any additional inputs to the chart definition
        chartAttributeString = applyInputsToFormat(chartAttributeString);
        try {
            Document chartDocument = XmlDom4JHelper.getDocFromString(chartAttributeString, new PentahoEntityResolver());
            chartNode = chartDocument.selectSingleNode(CHART_NODE_LOC);
            if (chartNode == null) {
                chartNode = chartDocument.selectSingleNode(CHART_ATTRIBUTES);
            }
        } catch (XmlParseException e) {
            getLogger().error(Messages.getInstance().getErrorString("OpenFlashChartComponent.ERROR_0001_CANT_DOCUMENT_FROM_STRING"), // $NON-NLS-1$
            e);
            return false;
        }
    } else {
        // see if the chart-attributes node is available in the component definition
        chartNode = getComponentDefinition(true).selectSingleNode(CHART_ATTRIBUTES);
    }
    if (chartNode == null) {
        getLogger().error(// $NON-NLS-1$
        Messages.getInstance().getErrorString("OpenFlashChartComponent.ERROR_0002_CHART_DEFINITION_NOT_FOUND"));
        return false;
    }
    // Determine if we are going to read the chart data set by row or by column
    boolean byRow = false;
    if (getInputStringValue(BY_ROW_PROP) != null) {
        byRow = Boolean.valueOf(getInputStringValue(BY_ROW_PROP)).booleanValue();
    }
    String chartJson = generateChartJson(data, chartNode, byRow);
    // generate a unique name for the function
    // $NON-NLS-1$ //$NON-NLS-2$
    String chartId = UUIDUtil.getUUIDAsString().replaceAll("[^\\w]", "");
    // populate the flash html template
    Properties props = new Properties();
    // $NON-NLS-1$
    props.setProperty("chartId", chartId);
    // $NON-NLS-1$ //$NON-NLS-2$
    props.setProperty("dataFunction", "getData" + chartId);
    // $NON-NLS-1$
    props.setProperty("chart-width", chartWidth);
    // $NON-NLS-1$
    props.setProperty("chart-height", chartHeight);
    // $NON-NLS-1$
    props.setProperty("ofc-url", ofcURL);
    // $NON-NLS-1$
    props.setProperty("ofc-libname", ofclibname);
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    props.setProperty("chartJson", chartJson.replaceAll("\"", "\\\\\""));
    String flashContent = TemplateUtil.applyTemplate(getFlashFragment(), props, null);
    // set output value
    // $NON-NLS-1$
    log.debug("html_fragment=" + flashContent);
    if (this.isDefinedOutput("html_fragment")) {
        // $NON-NLS-1$
        // $NON-NLS-1$
        this.setOutputValue("html_fragment", flashContent);
    }
    if (this.isDefinedOutput("image-tag")) {
        // $NON-NLS-1$
        // $NON-NLS-1$
        this.setOutputValue("image-tag", flashContent);
    }
    return true;
}
Also used : IPentahoResultSet(org.pentaho.commons.connection.IPentahoResultSet) Node(org.dom4j.Node) XmlParseException(org.pentaho.platform.api.util.XmlParseException) Document(org.dom4j.Document) Properties(java.util.Properties) PentahoEntityResolver(org.pentaho.platform.engine.services.solution.PentahoEntityResolver) IActionSequenceResource(org.pentaho.platform.api.engine.IActionSequenceResource)

Example 12 with PentahoEntityResolver

use of org.pentaho.platform.engine.services.solution.PentahoEntityResolver in project pentaho-platform by pentaho.

the class JFreeReportGeneratorComponent method executeAction.

@Override
protected boolean executeAction() {
    /*
     * The on-the-fly JFreeReport generator will expect the following: =============================================
     * 
     * INPUT: Path (String) to Existing JFreeReport to use as "template" OutputStream to write generated report
     * IPentahoResultSet List of Columns to "Group" List of column widths
     * 
     * OUTPUT: JFreeReport XML to provided Path or OutputStream
     * 
     * ASSUMPTIONS: List of column widths - last provided item is to be repeated for all remaining columns Perform
     * ItemSumFunction on all numeric columns per group and grand total Perform ItemSumFunction on calculated column per
     * group and grand total Groups and Items will be removed from template (we will retain font/color data)
     * =============================================
     * 
     * public OnTheFlyJFreeReportGenerator(String path, IPentahoResultSet set, List groupLabels, List widths,
     * OutputStream stream) ------ public void process()
     */
    JFreeReportGenAction genAction = null;
    if (!(getActionDefinition() instanceof JFreeReportGenAction)) {
        error(Messages.getInstance().getErrorString("JFreeReportGeneratorComponent.ERROR_0001_UNKNOWN_ACTION_TYPE", // $NON-NLS-1$
        getActionDefinition().getElement().asXML()));
        return false;
    } else {
        genAction = (JFreeReportGenAction) getActionDefinition();
        resultSet = (IPentahoResultSet) genAction.getResultSet().getValue();
        Node componentNode = null;
        String settingsFromActionSequence = null;
        try {
            settingsFromActionSequence = genAction.getComponentSettings().getStringValue();
        } catch (Exception ex) {
        // ignore
        }
        if (settingsFromActionSequence != null) {
            try {
                Document settingsDoc = XmlDom4JHelper.getDocFromString(settingsFromActionSequence, new PentahoEntityResolver());
                componentNode = settingsDoc.getRootElement();
            } catch (Exception e) {
                // $NON-NLS-1$
                error("Could not get settings from action sequence document", e);
                return false;
            }
        } else {
            componentNode = getComponentDefinition();
        }
        try {
            compPath = genAction.getTemplatePath().getStringValue();
            path = PentahoSystem.getApplicationContext().getSolutionPath(compPath);
            orientation = genAction.getOrientation().getStringValue();
            nullString = genAction.getNullString().getStringValue();
            horizontalOffset = genAction.getHorizontalOffset().getIntValue(horizontalOffset);
            reportName = genAction.getReportName().getStringValue();
            createSubTotals = genAction.getCreateSubTotals().getBooleanValue(false);
            createGrandTotals = genAction.getCreateGrandTotals().getBooleanValue(false);
            createRowBanding = genAction.getCreateRowBanding().getBooleanValue(false);
            createTotalColumn = genAction.getCreateTotalColumn().getBooleanValue(false);
            totalColumnName = genAction.getTotalColumnName().getStringValue();
            totalColumnWidth = genAction.getTotalColumnWidth().getIntValue(totalColumnWidth);
            totalColumnFormat = genAction.getTotalColumnFormat().getStringValue();
            rowBandingColor = genAction.getRowBandingColor().getStringValue();
            spacerWidth = genAction.getSpacerWidth().getIntValue(spacerWidth);
            columnHeaderBackgroundColor = genAction.getColumnHeaderBackgroundColor().getStringValue();
            columnHeaderForegroundColor = genAction.getColumnHeaderForegroundColor().getStringValue();
            columnHeaderFontFace = genAction.getColumnHeaderFontFace().getStringValue();
            columnHeaderFontSize = genAction.getColumnHeaderFontSize().getStringValue();
            columnHeaderGap = genAction.getColumnHeaderGap().getIntValue(columnHeaderGap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // Get the group display labels
        List groupLabelNodes = componentNode.selectNodes(JFreeReportGeneratorComponent.GROUP_LABELS_PROP + "/" + // $NON-NLS-1$
        JFreeReportGeneratorComponent.GROUP_LABEL_PROP);
        if (groupLabelNodes != null) {
            groupLabels = new String[groupLabelNodes.size()];
            for (int i = 0; i < groupLabels.length; i++) {
                groupLabels[i] = ((Node) groupLabelNodes.get(i)).getText();
            }
        }
        // Get the grouped columns indices
        List groupedColumnsIndicesNodes = componentNode.selectNodes(JFreeReportGeneratorComponent.GROUPED_COLUMNS_PROP + "/" + // $NON-NLS-1$
        JFreeReportGeneratorComponent.GROUPED_COLUMN_INDICES_PROP);
        if (groupedColumnsIndicesNodes != null) {
            groupIndices = new int[groupedColumnsIndicesNodes.size()];
            for (int i = 0; i < groupIndices.length; i++) {
                groupIndices[i] = Integer.parseInt(((Node) groupedColumnsIndicesNodes.get(i)).getText()) - 1;
            // I am zero based, this is not
            }
        }
        // get display names
        List displayNameNodes = componentNode.selectNodes(JFreeReportGeneratorComponent.COLUMN_NAMES_PROP + "/" + // $NON-NLS-1$
        JFreeReportGeneratorComponent.COLUMN_NAME_PROP);
        if (displayNameNodes != null) {
            displayNames = new String[displayNameNodes.size()];
            for (int i = 0; i < displayNames.length; i++) {
                displayNames[i] = ((Node) displayNameNodes.get(i)).getText();
            }
        }
        // get column alignments
        List columnAlignmentNodes = componentNode.selectNodes(JFreeReportGeneratorComponent.COLUMN_ALIGNMENTS_PROP + "/" + // $NON-NLS-1$
        JFreeReportGeneratorComponent.COLUMN_ALIGNMENT_PROP);
        if (columnAlignmentNodes != null) {
            columnAlignments = new String[columnAlignmentNodes.size()];
            for (int i = 0; i < columnAlignments.length; i++) {
                columnAlignments[i] = ((Node) columnAlignmentNodes.get(i)).getText();
            }
        }
        // Get the column widths
        List widthNodes = componentNode.selectNodes(JFreeReportGeneratorComponent.COLUMN_WIDTHS_PROP + "/" + // $NON-NLS-1$
        JFreeReportGeneratorComponent.WIDTH_PROP);
        if (widthNodes != null) {
            widths = new int[widthNodes.size()];
            for (int i = 0; i < widths.length; i++) {
                widths[i] = Integer.valueOf(((Node) widthNodes.get(i)).getText()).intValue();
            }
        }
        // Get the column item hides
        List itemHideNodes = componentNode.selectNodes(JFreeReportGeneratorComponent.ITEM_HIDES_PROP + "/" + // $NON-NLS-1$
        JFreeReportGeneratorComponent.ITEM_HIDE_PROP);
        if (itemHideNodes != null) {
            itemHides = new boolean[itemHideNodes.size()];
            for (int i = 0; i < itemHides.length; i++) {
                itemHides[i] = Boolean.valueOf(((Node) itemHideNodes.get(i)).getText()).booleanValue();
            }
        }
        // Get the column formats
        List formatNodes = componentNode.selectNodes(JFreeReportGeneratorComponent.COLUMN_FORMATS_PROP + "/" + // $NON-NLS-1$
        JFreeReportGeneratorComponent.FORMAT_PROP);
        if (formatNodes != null) {
            formats = new String[formatNodes.size()];
            for (int i = 0; i < formats.length; i++) {
                formats[i] = ((Node) formatNodes.get(i)).getText();
            }
        }
    }
    String reportDefinition = process();
    if (reportDefinition != null) {
        if (genAction.getOutputReportDefinition() != null) {
            genAction.getOutputReportDefinition().setValue(reportDefinition);
        } else {
            // This is to support the old way where
            // we did not check if report-definition existed in the output section
            setOutputValue(JFreeReportGenAction.REPORT_DEFINITION, reportDefinition);
        }
    }
    return true;
}
Also used : JFreeReportGenAction(org.pentaho.actionsequence.dom.actions.JFreeReportGenAction) Node(org.dom4j.Node) List(java.util.List) LinkedList(java.util.LinkedList) Document(org.dom4j.Document) PentahoEntityResolver(org.pentaho.platform.engine.services.solution.PentahoEntityResolver)

Aggregations

PentahoEntityResolver (org.pentaho.platform.engine.services.solution.PentahoEntityResolver)12 Document (org.dom4j.Document)10 Node (org.dom4j.Node)7 XmlParseException (org.pentaho.platform.api.util.XmlParseException)6 List (java.util.List)5 File (java.io.File)3 ArrayList (java.util.ArrayList)3 Iterator (java.util.Iterator)3 FileWriter (java.io.FileWriter)2 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 StringWriter (java.io.StringWriter)2 HashMap (java.util.HashMap)2 Properties (java.util.Properties)2 Element (org.dom4j.Element)2 Test (org.junit.Test)2 IPentahoResultSet (org.pentaho.commons.connection.IPentahoResultSet)2 IActionSequenceResource (org.pentaho.platform.api.engine.IActionSequenceResource)2 IApplicationContext (org.pentaho.platform.api.engine.IApplicationContext)2 BufferedWriter (java.io.BufferedWriter)1