Search in sources :

Example 1 with InvalidAggregationFunction

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

the class AbstractDBAggregationFunctionRow method loadFunction.

/**
 * @param tableSpec optional input {@link DataTableSpec}
 * @param functionProvider the {@link DBAggregationFunctionProvider}
 * @param cfg {@link NodeSettingsRO} to read from
 * @return the {@link DBAggregationFunction}
 * @throws InvalidSettingsException if the settings of the function are invalid
 */
public static DBAggregationFunction loadFunction(final DataTableSpec tableSpec, final DBAggregationFunctionProvider functionProvider, final NodeSettingsRO cfg) throws InvalidSettingsException {
    final String functionId = cfg.getString(CNFG_AGGR_COL_SECTION);
    DBAggregationFunction function = functionProvider.getFunction(functionId);
    if (function instanceof InvalidAggregationFunction) {
        final String errMsg = "Exception while loading database aggregation functions. " + ((InvalidAggregationFunction) function).getErrorMessage();
        LOGGER.warn(errMsg);
    } else {
        if (function.hasOptionalSettings()) {
            try {
                final NodeSettingsRO subSettings = cfg.getNodeSettings("functionSettings");
                if (tableSpec != null) {
                    // this method is called from the dialog
                    function.loadSettingsFrom(subSettings, tableSpec);
                } else {
                    // this method is called from the node model where we do not
                    // have the DataTableSpec
                    function.loadValidatedSettings(subSettings);
                }
            } catch (Exception e) {
                final String errMsg = "Exception while loading settings for aggreation function '" + function.getId() + "', reason: " + e.getMessage();
                function = new InvalidDBAggregationFunction(functionId, errMsg, null);
                LOGGER.error(errMsg);
            }
        }
    }
    return function;
}
Also used : InvalidAggregationFunction(org.knime.core.node.port.database.aggregation.InvalidAggregationFunction) InvalidDBAggregationFunction(org.knime.core.node.port.database.aggregation.InvalidDBAggregationFunction) DBAggregationFunction(org.knime.core.node.port.database.aggregation.DBAggregationFunction) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) InvalidDBAggregationFunction(org.knime.core.node.port.database.aggregation.InvalidDBAggregationFunction)

Example 2 with InvalidAggregationFunction

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

the class AggregationFunctionAndRowTableCellRenderer method getTableCellRendererComponent.

/**
 * {@inheritDoc}
 */
@Override
public Component getTableCellRendererComponent(final JTable table, final Object value, final boolean isSelected, final boolean hasFocus, final int row, final int column) {
    final Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    assert (c == this);
    final AggregationFunction function;
    if (value instanceof AggregationFunctionRow) {
        function = ((AggregationFunctionRow<?>) value).getFunction();
    } else if (value instanceof AggregationFunction) {
        function = (AggregationFunction) value;
    } else {
        function = null;
    }
    if (function != null) {
        if (function instanceof InvalidAggregationFunction) {
            // set a red border for invalid methods
            setBorder(BorderFactory.createLineBorder(Color.RED));
        } else {
            setBorder(null);
        }
        setText(function.getLabel());
        setToolTipText(function.getDescription());
    }
    return this;
}
Also used : InvalidAggregationFunction(org.knime.core.node.port.database.aggregation.InvalidAggregationFunction) AggregationFunction(org.knime.core.node.port.database.aggregation.AggregationFunction) InvalidAggregationFunction(org.knime.core.node.port.database.aggregation.InvalidAggregationFunction) Component(java.awt.Component)

Example 3 with InvalidAggregationFunction

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

the class AbstractAggregationPanel method createInvalidRowsSelectionMenu.

/**
 * @return the {@link JMenuItem} to select invalid rows or <code>null</code> if all rows are valid
 * @since 2.11
 */
protected JMenuItem createInvalidRowsSelectionMenu() {
    final List<R> rows = getTableModel().getRows();
    final List<Integer> idxs = new LinkedList<>();
    int idx = 0;
    for (R r : rows) {
        if (!r.isValid() || (r.getFunction() instanceof InvalidAggregationFunction)) {
            idxs.add(Integer.valueOf(idx));
        }
        idx++;
    }
    if (!idxs.isEmpty()) {
        final JMenuItem selectInvalidRows = new JMenuItem("Select all invalid rows");
        selectInvalidRows.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent e) {
                updateSelection(idxs);
            }
        });
        return selectInvalidRows;
    }
    return null;
}
Also used : ActionListener(java.awt.event.ActionListener) InvalidAggregationFunction(org.knime.core.node.port.database.aggregation.InvalidAggregationFunction) ActionEvent(java.awt.event.ActionEvent) JMenuItem(javax.swing.JMenuItem) LinkedList(java.util.LinkedList)

Example 4 with InvalidAggregationFunction

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

the class DBGroupByNodeModel2 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(null);
    final String dbIdentifier = connection.getDatabaseIdentifier();
    final List<DBColumnAggregationFunctionRow> columnFunctions = DBColumnAggregationFunctionRow.loadFunctions(m_settings, DBGroupByNodeModel2.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());
    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("Exception in aggregation function " + function.getLabel() + " of column " + row.getColumnSpec().getName() + ": " + e.getMessage());
            }
        }
        usedColNames.add(row.getColumnSpec().getName());
        m_aggregationFunction2Use.add(row);
    }
    final List<DBPatternAggregationFunctionRow> patternFunctions = DBPatternAggregationFunctionRow.loadFunctions(m_settings, CFG_PATTERN_AGGREGATION_FUNCTIONS, dbIdentifier, tableSpec);
    if (tableSpec.getNumColumns() > usedColNames.size() && !patternFunctions.isEmpty()) {
        for (final DataColumnSpec spec : tableSpec) {
            if (!usedColNames.contains(spec.getName())) {
                for (final DBPatternAggregationFunctionRow patternFunction : patternFunctions) {
                    final Pattern pattern = patternFunction.getRegexPattern();
                    final DBAggregationFunction function = patternFunction.getFunction();
                    if (pattern != null && pattern.matcher(spec.getName()).matches() && function.isCompatible(spec.getType())) {
                        final DBColumnAggregationFunctionRow row = new DBColumnAggregationFunctionRow(spec, patternFunction.getFunction());
                        m_aggregationFunction2Use.add(row);
                        usedColNames.add(spec.getName());
                    }
                }
            }
        }
    }
    final List<DBDataTypeAggregationFunctionRow> typeFunctions = DBDataTypeAggregationFunctionRow.loadFunctions(m_settings, CFG_TYPE_AGGREGATION_FUNCTIONS, dbIdentifier, tableSpec);
    // check if some columns are left
    if (tableSpec.getNumColumns() > usedColNames.size() && !typeFunctions.isEmpty()) {
        for (final DataColumnSpec spec : tableSpec) {
            if (!usedColNames.contains(spec.getName())) {
                final DataType dataType = spec.getType();
                for (final DBDataTypeAggregationFunctionRow typeAggregator : typeFunctions) {
                    if (typeAggregator.isCompatibleType(dataType)) {
                        final DBColumnAggregationFunctionRow row = new DBColumnAggregationFunctionRow(spec, typeAggregator.getFunction());
                        m_aggregationFunction2Use.add(row);
                        usedColNames.add(spec.getName());
                    }
                }
            }
        }
    }
    if (m_groupByCols.getIncludeList().isEmpty() && m_aggregationFunction2Use.isEmpty() && !m_addCountStar.getBooleanValue()) {
        throw new InvalidSettingsException("Please select at least one group or aggregation function or the " + "COUNT(*) option.");
    }
    if (!invalidColAggrs.isEmpty()) {
        setWarningMessage(invalidColAggrs.size() + " aggregation functions ignored due to incompatible columns.");
    }
    return new PortObjectSpec[] { createDbOutSpec(dbSpec, true) };
}
Also used : Pattern(java.util.regex.Pattern) 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) DBPatternAggregationFunctionRow(org.knime.base.node.io.database.groupby.dialog.pattern.DBPatternAggregationFunctionRow) 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) DBColumnAggregationFunctionRow(org.knime.base.node.io.database.groupby.dialog.column.DBColumnAggregationFunctionRow) DBAggregationFunction(org.knime.core.node.port.database.aggregation.DBAggregationFunction) DatabasePortObjectSpec(org.knime.core.node.port.database.DatabasePortObjectSpec) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) DatabasePortObjectSpec(org.knime.core.node.port.database.DatabasePortObjectSpec) DataType(org.knime.core.data.DataType) DBDataTypeAggregationFunctionRow(org.knime.base.node.io.database.groupby.dialog.type.DBDataTypeAggregationFunctionRow) HashSet(java.util.HashSet)

Example 5 with InvalidAggregationFunction

use of org.knime.core.node.port.database.aggregation.InvalidAggregationFunction 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

InvalidAggregationFunction (org.knime.core.node.port.database.aggregation.InvalidAggregationFunction)6 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)4 AggregationFunction (org.knime.core.node.port.database.aggregation.AggregationFunction)3 DBAggregationFunction (org.knime.core.node.port.database.aggregation.DBAggregationFunction)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 DBColumnAggregationFunctionRow (org.knime.base.node.io.database.groupby.dialog.column.DBColumnAggregationFunctionRow)2 DataColumnSpec (org.knime.core.data.DataColumnSpec)2 DataTableSpec (org.knime.core.data.DataTableSpec)2 NodeSettingsRO (org.knime.core.node.NodeSettingsRO)2 SettingsModelFilterString (org.knime.core.node.defaultnodesettings.SettingsModelFilterString)2 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)2 PortObjectSpec (org.knime.core.node.port.PortObjectSpec)2 DatabasePortObjectSpec (org.knime.core.node.port.database.DatabasePortObjectSpec)2 DatabaseQueryConnectionSettings (org.knime.core.node.port.database.DatabaseQueryConnectionSettings)2 Component (java.awt.Component)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 LinkedList (java.util.LinkedList)1 Pattern (java.util.regex.Pattern)1