Search in sources :

Example 46 with DatabasePortObjectSpec

use of org.knime.core.node.port.database.DatabasePortObjectSpec in project knime-core by knime.

the class DBPivotNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    final DatabasePortObjectSpec dbSpec = (DatabasePortObjectSpec) inSpecs[0];
    final DataTableSpec tableSpec = dbSpec.getDataTableSpec();
    final DatabaseQueryConnectionSettings connection = dbSpec.getConnectionSettings(getCredentialsProvider());
    final String dbIdentifier = connection.getDatabaseIdentifier();
    final List<DBColumnAggregationFunctionRow> columnFunctions = DBColumnAggregationFunctionRow.loadFunctions(m_settings, DBPivotNodeModel.CFG_AGGREGATION_FUNCTIONS, dbIdentifier, tableSpec);
    final ArrayList<DBColumnAggregationFunctionRow> invalidColAggrs = new ArrayList<>(1);
    final Set<String> usedColNames = new HashSet<>(tableSpec.getNumColumns());
    usedColNames.addAll(m_groupByCols.getIncludeList());
    usedColNames.addAll(m_pivotCols.getIncludeList());
    m_aggregationFunction2Use.clear();
    for (DBColumnAggregationFunctionRow row : columnFunctions) {
        final DataColumnSpec columnSpec = row.getColumnSpec();
        final DataColumnSpec inputSpec = tableSpec.getColumnSpec(columnSpec.getName());
        final AggregationFunction function = row.getFunction();
        if (inputSpec == null || !inputSpec.getType().equals(columnSpec.getType())) {
            invalidColAggrs.add(row);
            continue;
        }
        if (function instanceof InvalidAggregationFunction) {
            throw new InvalidSettingsException(((InvalidAggregationFunction) function).getErrorMessage());
        }
        if (function.hasOptionalSettings()) {
            try {
                function.configure(tableSpec);
            } catch (InvalidSettingsException e) {
                throw new InvalidSettingsException("Wrong aggregation function configuration '" + function.getLabel() + "' of column '" + row.getColumnSpec().getName() + "': " + e.getMessage(), e);
            }
        }
        usedColNames.add(row.getColumnSpec().getName());
        m_aggregationFunction2Use.add(row);
    }
    if (m_aggregationFunction2Use.isEmpty()) {
        throw new InvalidSettingsException("No aggregation columns selected.");
    }
    if (m_groupByCols.getIncludeList().isEmpty()) {
        setWarningMessage("No grouping column included. Aggregate complete table");
    }
    if (m_pivotCols.getIncludeList().isEmpty()) {
        throw new InvalidSettingsException("No pivot columns selected.");
    }
    if (!invalidColAggrs.isEmpty()) {
        setWarningMessage(invalidColAggrs.size() + " aggregation functions ignored due to incompatible columns.");
    }
    final DatabasePortObjectSpec resultSpec;
    if (connection.getRetrieveMetadataInConfigure()) {
        try {
            resultSpec = createDbOutSpec(dbSpec, new ExecutionMonitor());
        } catch (CanceledExecutionException e) {
            throw new InvalidSettingsException(e.getMessage());
        }
    } else {
        resultSpec = null;
    }
    return new PortObjectSpec[] { resultSpec };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) InvalidAggregationFunction(org.knime.core.node.port.database.aggregation.InvalidAggregationFunction) ArrayList(java.util.ArrayList) SettingsModelFilterString(org.knime.core.node.defaultnodesettings.SettingsModelFilterString) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) InvalidAggregationFunction(org.knime.core.node.port.database.aggregation.InvalidAggregationFunction) DBAggregationFunction(org.knime.core.node.port.database.aggregation.DBAggregationFunction) AggregationFunction(org.knime.core.node.port.database.aggregation.AggregationFunction) DatabaseQueryConnectionSettings(org.knime.core.node.port.database.DatabaseQueryConnectionSettings) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) DBColumnAggregationFunctionRow(org.knime.base.node.io.database.groupby.dialog.column.DBColumnAggregationFunctionRow) DatabasePortObjectSpec(org.knime.core.node.port.database.DatabasePortObjectSpec) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) DatabasePortObjectSpec(org.knime.core.node.port.database.DatabasePortObjectSpec) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) HashSet(java.util.HashSet)

Aggregations

DatabasePortObjectSpec (org.knime.core.node.port.database.DatabasePortObjectSpec)46 DataTableSpec (org.knime.core.data.DataTableSpec)36 DatabaseQueryConnectionSettings (org.knime.core.node.port.database.DatabaseQueryConnectionSettings)33 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)29 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)14 DatabasePortObject (org.knime.core.node.port.database.DatabasePortObject)13 PortObjectSpec (org.knime.core.node.port.PortObjectSpec)12 DBReader (org.knime.core.node.port.database.reader.DBReader)12 PortObject (org.knime.core.node.port.PortObject)10 SettingsModelFilterString (org.knime.core.node.defaultnodesettings.SettingsModelFilterString)9 StatementManipulator (org.knime.core.node.port.database.StatementManipulator)8 SQLException (java.sql.SQLException)7 NotConfigurableException (org.knime.core.node.NotConfigurableException)5 DatabaseConnectionSettings (org.knime.core.node.port.database.DatabaseConnectionSettings)3 PMMLPortObject (org.knime.core.node.port.pmml.PMMLPortObject)3 GridBagConstraints (java.awt.GridBagConstraints)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 DBBinnerMaps (org.knime.base.node.io.database.binning.DBBinnerMaps)2 DBAggregationFunctionProvider (org.knime.base.node.io.database.groupby.dialog.DBAggregationFunctionProvider)2