Search in sources :

Example 21 with IPentahoMetaData

use of org.pentaho.commons.connection.IPentahoMetaData in project pentaho-platform by pentaho.

the class TemplateUtilTest method createMockResultSet.

private IPentahoResultSet createMockResultSet() {
    final Object[] mockRow = new Object[] { "key Value", "field Value" };
    IPentahoResultSet mockPentahoResultSet = mock(IPentahoResultSet.class);
    IPentahoMetaData mockPentahoMetaData = mock(IPentahoMetaData.class);
    when(mockPentahoResultSet.getColumnCount()).thenReturn(mockRow.length);
    when(mockPentahoResultSet.getDataRow(0)).thenReturn(mockRow);
    when(mockPentahoResultSet.getMetaData()).thenReturn(mockPentahoMetaData);
    when(mockPentahoResultSet.getRowCount()).thenReturn(1);
    when(mockPentahoResultSet.getValueAt(0, 0)).thenReturn(mockRow[0]);
    when(mockPentahoResultSet.getValueAt(0, 1)).thenReturn(mockRow[1]);
    when(mockPentahoMetaData.getColumnIndex("keycol")).thenReturn(0);
    when(mockPentahoMetaData.getColumnIndex("valcol")).thenReturn(1);
    return mockPentahoResultSet;
}
Also used : IPentahoResultSet(org.pentaho.commons.connection.IPentahoResultSet) IPentahoMetaData(org.pentaho.commons.connection.IPentahoMetaData)

Example 22 with IPentahoMetaData

use of org.pentaho.commons.connection.IPentahoMetaData in project pentaho-platform by pentaho.

the class SQLBaseComponent method resolveParameter.

/**
 * This method is called when TemplateUtil.applyTemplate() encounters a parameter. TemplateUtil.applyTemplate is
 * called when someone makes a call to applyInputsToFormat() In this class it is called in the above "runQuery()"
 * method.
 *
 * @param template
 *          the source string
 * @param parameter
 *          the parameter value
 * @param parameterMatcher
 *          the regex parameter matcher
 * @param copyStart
 *          the start of the copy
 * @param results
 *          the output result
 * @return the next copystart
 */
@Override
public int resolveParameter(final String template, final String parameter, final Matcher parameterMatcher, int copyStart, final StringBuffer results) {
    // $NON-NLS-1$
    StringTokenizer tokenizer = new StringTokenizer(parameter, ":");
    if (tokenizer.countTokens() == 2) {
        // Currently, the component only handles one bit of metadata
        String parameterPrefix = tokenizer.nextToken();
        String inputName = tokenizer.nextToken();
        // mark a spot in the preparedParameters list and move on.
        if (parameterPrefix.equals(IPreparedComponent.PREPARE_LATER_PREFIX)) {
            if (!isDefinedOutput(IPreparedComponent.PREPARED_COMPONENT_NAME)) {
                // $NON-NLS-1$
                error(Messages.getInstance().getErrorString("IPreparedComponent.ERROR_0003_INVALID_PARAMETER_STATE"));
                return -1;
            }
            preparedParameters.add(IPreparedComponent.PREPARE_LATER_PLACEHOLDER);
            int start = parameterMatcher.start();
            int end = parameterMatcher.end();
            results.append(template.substring(copyStart, start));
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            results.append("{" + IPreparedComponent.PREPARE_LATER_INTER_PREFIX + ":" + inputName + "}");
            return end;
        }
        if (parameterPrefix.equals(SQLBaseComponent.PREPARE_PARAMETER_PREFIX)) {
            // We know this parameter is for us.
            // First, is this a special input
            Object parameterValue = TemplateUtil.getSystemInput(inputName, getRuntimeContext());
            if ((parameterValue == null) && isDefinedInput(inputName)) {
                parameterValue = this.getInputValue(inputName);
            }
            if (parameterValue != null) {
                // We have a parameter value - now, it's time to create a parameter and build up the
                // parameter string
                int start = parameterMatcher.start();
                int end = parameterMatcher.end();
                // First, find out if the parameter was quoted...
                if ((start > 0) && (end < template.length())) {
                    if ((template.charAt(start - 1) == '\'') && (template.charAt(end) == '\'')) {
                        // Ok, the parameter was quoted as near as we can tell. So, we need
                        // to increase the size of the amount we overwrite by one in each
                        // direction. This is for backward compatibility.
                        start--;
                        end++;
                    }
                }
                // We now have a valid start and end. It's time to see whether we're dealing
                // with an array, a result set, or a scalar.
                StringBuffer parameterBuffer = new StringBuffer();
                if (parameterValue instanceof String) {
                    preparedParameters.add(parameterValue);
                    parameterBuffer.append('?');
                } else if (parameterValue instanceof Object[]) {
                    Object[] pObj = (Object[]) parameterValue;
                    for (Object element : pObj) {
                        preparedParameters.add(element);
                        // $NON-NLS-1$ //$NON-NLS-2$
                        parameterBuffer.append((parameterBuffer.length() == 0) ? "?" : ",?");
                    }
                } else if (parameterValue instanceof IPentahoResultSet) {
                    IPentahoResultSet rs = (IPentahoResultSet) parameterValue;
                    // See if we can find a column in the metadata with the same
                    // name as the input
                    IPentahoMetaData md = rs.getMetaData();
                    int columnIdx = -1;
                    if (md.getColumnCount() == 1) {
                        columnIdx = 0;
                    } else {
                        columnIdx = md.getColumnIndex(new String[] { parameter });
                    }
                    if (columnIdx < 0) {
                        // $NON-NLS-1$
                        error(Messages.getInstance().getErrorString("Template.ERROR_0005_COULD_NOT_DETERMINE_COLUMN"));
                        return -1;
                    }
                    int rowCount = rs.getRowCount();
                    Object valueCell = null;
                    // TODO support non-string columns
                    for (int i = 0; i < rowCount; i++) {
                        valueCell = rs.getValueAt(i, columnIdx);
                        preparedParameters.add(valueCell);
                        // $NON-NLS-1$ //$NON-NLS-2$
                        parameterBuffer.append((parameterBuffer.length() == 0) ? "?" : ",?");
                    }
                } else if (parameterValue instanceof List) {
                    List pObj = (List) parameterValue;
                    for (int i = 0; i < pObj.size(); i++) {
                        preparedParameters.add(pObj.get(i));
                        // $NON-NLS-1$ //$NON-NLS-2$
                        parameterBuffer.append((parameterBuffer.length() == 0) ? "?" : ",?");
                    }
                } else {
                    // If we're here, we know parameterValue is not null and not a string
                    this.preparedParameters.add(parameterValue);
                    parameterBuffer.append('?');
                }
                // OK - We have a parameterBuffer and have filled out the preparedParameters
                // list. It's time to change the SQL to insert our parameter marker and tell
                // the caller we've done our job.
                results.append(template.substring(copyStart, start));
                copyStart = end;
                results.append(parameterBuffer);
                return copyStart;
            }
        }
    }
    // Nothing here for us - let default behavior through
    return -1;
}
Also used : IPentahoResultSet(org.pentaho.commons.connection.IPentahoResultSet) StringTokenizer(java.util.StringTokenizer) ArrayList(java.util.ArrayList) List(java.util.List) IPentahoMetaData(org.pentaho.commons.connection.IPentahoMetaData)

Example 23 with IPentahoMetaData

use of org.pentaho.commons.connection.IPentahoMetaData in project pentaho-platform by pentaho.

the class JFreeReportComponent method setReportConfigParameters.

private void setReportConfigParameters(final MasterReport report, final IPentahoResultSet values) {
    int rowCount = values.getRowCount();
    int colCount = values.getColumnCount();
    ModifiableConfiguration config = report.getReportConfiguration();
    if (colCount >= 2) {
        IPentahoMetaData md = values.getMetaData();
        // $NON-NLS-1$
        int nameIdx = md.getColumnIndex("name");
        // $NON-NLS-1$
        int valIdx = md.getColumnIndex("value");
        if (nameIdx < 0) {
            nameIdx = 0;
        }
        if (valIdx < 0) {
            valIdx = 1;
        }
        for (int i = 0; i < rowCount; i++) {
            Object[] aRow = values.getDataRow(i);
            if ((aRow[nameIdx] != null) && (aRow[valIdx] != null)) {
                config.setConfigProperty(aRow[nameIdx].toString(), applyInputsToFormat(aRow[valIdx].toString()));
            }
        }
    } else {
        // $NON-NLS-1$
        error(Messages.getInstance().getErrorString("JFreeReport.ERROR_0025_INVALID_REPORT_CONFIGURATION_PARAMETERS"));
    }
}
Also used : ModifiableConfiguration(org.pentaho.reporting.libraries.base.config.ModifiableConfiguration) IPentahoMetaData(org.pentaho.commons.connection.IPentahoMetaData)

Example 24 with IPentahoMetaData

use of org.pentaho.commons.connection.IPentahoMetaData in project pentaho-platform by pentaho.

the class JFreeReportConfigParameterComponent method setReportConfigParameters.

private void setReportConfigParameters(final MasterReport report, final IPentahoResultSet values) {
    int rowCount = values.getRowCount();
    int colCount = values.getColumnCount();
    ModifiableConfiguration config = report.getReportConfiguration();
    if (colCount >= 2) {
        IPentahoMetaData md = values.getMetaData();
        // $NON-NLS-1$
        int nameIdx = md.getColumnIndex("name");
        // $NON-NLS-1$
        int valIdx = md.getColumnIndex("value");
        if (nameIdx < 0) {
            nameIdx = 0;
        }
        if (valIdx < 0) {
            valIdx = 1;
        }
        for (int i = 0; i < rowCount; i++) {
            Object[] aRow = values.getDataRow(i);
            if ((aRow[nameIdx] != null) && (aRow[valIdx] != null)) {
                config.setConfigProperty(aRow[nameIdx].toString(), applyInputsToFormat(aRow[valIdx].toString()));
            }
        }
    } else {
        // $NON-NLS-1$
        error(Messages.getInstance().getErrorString("JFreeReport.ERROR_0025_INVALID_REPORT_CONFIGURATION_PARAMETERS"));
    }
}
Also used : ModifiableConfiguration(org.pentaho.reporting.libraries.base.config.ModifiableConfiguration) IPentahoMetaData(org.pentaho.commons.connection.IPentahoMetaData)

Example 25 with IPentahoMetaData

use of org.pentaho.commons.connection.IPentahoMetaData 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;
}
Also used : IPentahoRequestContext(org.pentaho.platform.api.engine.IPentahoRequestContext) Element(org.dom4j.Element) IPentahoMetaData(org.pentaho.commons.connection.IPentahoMetaData) Document(org.dom4j.Document) IOException(java.io.IOException)

Aggregations

IPentahoMetaData (org.pentaho.commons.connection.IPentahoMetaData)26 IPentahoResultSet (org.pentaho.commons.connection.IPentahoResultSet)12 MemoryMetaData (org.pentaho.commons.connection.memory.MemoryMetaData)9 MemoryResultSet (org.pentaho.commons.connection.memory.MemoryResultSet)7 ArrayList (java.util.ArrayList)4 List (java.util.List)4 SQLConnection (org.pentaho.platform.plugin.services.connections.sql.SQLConnection)4 IOException (java.io.IOException)3 SQLException (java.sql.SQLException)3 MarshallableResultSet (org.pentaho.commons.connection.marshal.MarshallableResultSet)3 IRuntimeContext (org.pentaho.platform.api.engine.IRuntimeContext)3 SQLMetaData (org.pentaho.platform.plugin.services.connections.sql.SQLMetaData)3 StringTokenizer (java.util.StringTokenizer)2 Element (org.dom4j.Element)2 Test (org.junit.Test)2 MarshallableRow (org.pentaho.commons.connection.marshal.MarshallableRow)2 SerializedResultSet (org.pentaho.platform.dataaccess.datasource.beans.SerializedResultSet)2 DatasourceServiceException (org.pentaho.platform.dataaccess.datasource.wizard.service.DatasourceServiceException)2 ModifiableConfiguration (org.pentaho.reporting.libraries.base.config.ModifiableConfiguration)2 FileNotFoundException (java.io.FileNotFoundException)1