Search in sources :

Example 6 with AggregationFunction

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

Example 7 with AggregationFunction

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

the class AggregationFunctionComboBox method update.

/**
 * @param type the {@link DataType} used to initialize this combobox
 * @param list {@link List} of {@link AggregationMethod}s the user can choose from
 * @param selected the current selected method
 */
public void update(final DataType type, final List<? extends AggregationFunction> list, final AggregationFunction selected) {
    if (m_type == null || !m_type.equals(type)) {
        // recreate the combo box if the type has change
        removeAllItems();
        for (final AggregationFunction method : list) {
            addItem(method);
        }
        // save the current type for comparison
        m_type = type;
    }
    // select the previous selected item
    setSelectedItem(selected);
}
Also used : AggregationFunction(org.knime.core.node.port.database.aggregation.AggregationFunction)

Example 8 with AggregationFunction

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

the class AggregationFunctionComboBox method setSelectedItem.

@Override
public void setSelectedItem(final Object anObject) {
    Object objectToSelect = null;
    if (anObject != null && !isEditable()) {
        final AggregationFunction functionToSelect;
        if (anObject instanceof AggregationFunctionRow<?>) {
            functionToSelect = ((AggregationFunctionRow<?>) anObject).getFunction();
        } else if (anObject instanceof AggregationFunction) {
            functionToSelect = (AggregationFunction) anObject;
        } else {
            functionToSelect = null;
        }
        if (functionToSelect != null) {
            // For non editable combo boxes, an invalid selection
            // will be rejected.
            boolean found = false;
            for (int i = 0; i < dataModel.getSize(); i++) {
                AggregationFunction element = dataModel.getElementAt(i);
                if (functionToSelect.getId().equals(element.getId())) {
                    found = true;
                    objectToSelect = element;
                    break;
                }
            }
            if (!found) {
                return;
            }
        }
        // Must toggle the state of this flag since this method
        // call may result in ListDataEvents being fired.
        m_selectingItem = true;
        dataModel.setSelectedItem(objectToSelect);
        m_selectingItem = false;
        if (selectedItemReminder != dataModel.getSelectedItem()) {
            // in case a users implementation of ComboBoxModel
            // doesn't fire a ListDataEvent when the selection
            // changes.
            selectedItemChanged();
        }
    }
    fireActionEvent();
}
Also used : AggregationFunction(org.knime.core.node.port.database.aggregation.AggregationFunction)

Aggregations

AggregationFunction (org.knime.core.node.port.database.aggregation.AggregationFunction)8 InvalidAggregationFunction (org.knime.core.node.port.database.aggregation.InvalidAggregationFunction)3 Component (java.awt.Component)2 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 DataType (org.knime.core.data.DataType)2 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)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 DBAggregationFunction (org.knime.core.node.port.database.aggregation.DBAggregationFunction)2 Pattern (java.util.regex.Pattern)1 DBPatternAggregationFunctionRow (org.knime.base.node.io.database.groupby.dialog.pattern.DBPatternAggregationFunctionRow)1 DBDataTypeAggregationFunctionRow (org.knime.base.node.io.database.groupby.dialog.type.DBDataTypeAggregationFunctionRow)1 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)1