Search in sources :

Example 1 with DBColumn

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

the class KeysPanel method createSelectKeysContain.

/**
 * Creates table pop up menu item to select keys that contain the specified column
 *
 * @param menu the parent pop up menu to attach the menu items
 */
private void createSelectKeysContain(final JPopupMenu menu) {
    final JMenu subMenu = new JMenu("Select keys contain column");
    menu.add(subMenu);
    Map<String, List<Integer>> columnKeyIdxMapping = new TreeMap<>();
    for (int i = 0; i < getTableModel().getRowCount(); i++) {
        KeyElement key = (KeyElement) getTableModel().getElement(i);
        for (DBColumn col : key.getColumns()) {
            final String colName = col.getName();
            List<Integer> indices = columnKeyIdxMapping.get(colName);
            if (indices == null) {
                indices = new ArrayList<>();
                indices.add(i);
                columnKeyIdxMapping.put(colName, indices);
            } else {
                indices.add(i);
            }
        }
    }
    for (Entry<String, List<Integer>> entry : columnKeyIdxMapping.entrySet()) {
        final JMenuItem item = new JMenuItem(entry.getKey());
        item.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent e) {
                getTable().clearSelection();
                for (int idx : entry.getValue()) {
                    getTable().addRowSelectionInterval(idx, idx);
                }
            }
        });
        subMenu.add(item);
    }
}
Also used : ActionEvent(java.awt.event.ActionEvent) TreeMap(java.util.TreeMap) ActionListener(java.awt.event.ActionListener) ArrayList(java.util.ArrayList) JList(javax.swing.JList) List(java.util.List) DBColumn(org.knime.core.node.port.database.tablecreator.DBColumn) JMenuItem(javax.swing.JMenuItem) JMenu(javax.swing.JMenu)

Example 2 with DBColumn

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

the class DBTableCreatorConfiguration method createRowElement.

/**
 * A helper method to create a new instance of RowElement from NodeSettingsRO instance
 *
 * @param cfgKey key to determine which kind of RowElement to create
 * @param settings NodeSettingsRO instance used to create a new RowElement
 * @return a new instance of RowElement
 */
private RowElement createRowElement(final String cfgKey, final NodeSettingsRO settings) {
    switch(cfgKey) {
        case CFG_COLUMNS_SETTINGS:
            return new ColumnElement(settings);
        case CFG_KEYS_SETTINGS:
            KeyElement elem = new KeyElement(settings);
            Set<ColumnElement> columns = new HashSet<>();
            for (DBColumn dbCol : elem.getColumns()) {
                boolean isFound = false;
                for (RowElement el : getRowElements(CFG_COLUMNS_SETTINGS)) {
                    ColumnElement colElem = (ColumnElement) el;
                    if (dbCol.getName().equalsIgnoreCase(colElem.getName())) {
                        columns.add(colElem);
                        isFound = true;
                        break;
                    }
                }
                if (!isFound) {
                    throw new IllegalArgumentException(String.format("Column '%s' is undefined", dbCol.getName()));
                }
            }
            elem.setColumnElements(columns);
            return elem;
        case CFG_NAME_BASED_TYPE_MAPPING:
            return new NameBasedMappingElement(settings);
        case CFG_KNIME_BASED_TYPE_MAPPING:
            return new KNIMEBasedMappingElement(settings);
        case CFG_NAME_BASED_KEYS:
            return new NameBasedKeysElement(settings);
        default:
            return null;
    }
}
Also used : DBColumn(org.knime.core.node.port.database.tablecreator.DBColumn) HashSet(java.util.HashSet)

Example 3 with DBColumn

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

the class KeyElement method loadSettingsFrom.

/**
 * {@inheritDoc}
 */
@Override
protected void loadSettingsFrom(final NodeSettingsRO settings) {
    try {
        final String name = settings.getString(CFG_KEY_NAME);
        // Load all columns
        final NodeSettingsRO cfg = settings.getNodeSettings(CFG_KEY_COLUMNS);
        final Set<DBColumn> dbColumns = new LinkedHashSet<>();
        for (String settingsKey : cfg.keySet()) {
            final String colName = cfg.getString(settingsKey);
            DBColumn col = new DBColumn(colName, "", false);
            dbColumns.add(col);
        }
        final boolean primaryKey = settings.getBoolean(CFG_KEY_PRIMARY);
        m_key = new DBKey(name, dbColumns, primaryKey);
    } catch (InvalidSettingsException ex) {
    // Do nothing if no settings are found
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) DBColumn(org.knime.core.node.port.database.tablecreator.DBColumn) DBKey(org.knime.core.node.port.database.tablecreator.DBKey)

Example 4 with DBColumn

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

the class KeyElement method getColumnsString.

/**
 * Returns a string representation of the columns used to define this key
 *
 * @return a string representation of the columns used to define this key
 */
String getColumnsString() {
    final StringBuilder builder = new StringBuilder();
    List<DBColumn> columns = new ArrayList<>(getColumns());
    Collections.sort(columns, new Comparator<DBColumn>() {

        @Override
        public int compare(final DBColumn o1, final DBColumn o2) {
            return o1.getName().compareTo(o2.getName());
        }
    });
    for (DBColumn column : columns) {
        // remove ', ' at the end of the string
        builder.append(column.getName() + ", ");
    }
    final String columnsString = builder.toString();
    return columnsString.substring(0, columnsString.length() - 2);
}
Also used : ArrayList(java.util.ArrayList) DBColumn(org.knime.core.node.port.database.tablecreator.DBColumn)

Example 5 with DBColumn

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

the class DBTableCreatorNodeModel method configure.

/**
 * {@inheritDoc}
 */
@Override
protected PortObjectSpec[] configure(final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    if (inSpecs[0] == null && !(inSpecs[0] instanceof DatabaseConnectionPortObjectSpec)) {
        throw new InvalidSettingsException("No valid database connection available.");
    }
    final DatabaseConnectionPortObjectSpec dbSpec = (DatabaseConnectionPortObjectSpec) inSpecs[0];
    m_config.setTableSpec((DataTableSpec) inSpecs[1]);
    final boolean isColumnsEmpty = m_config.getColumns().isEmpty();
    if (m_config.getTableSpec() != null && (m_config.useDynamicSettings() || isColumnsEmpty)) {
        m_config.loadColumnSettingsFromTableSpec(m_config.getTableSpec());
        m_config.updateKeysWithDynamicSettings();
    }
    if (m_config.getTableSpec() == null && m_config.useDynamicSettings()) {
        throw new InvalidSettingsException("Dynamic settings enabled but no input table available.");
    }
    if (isColumnsEmpty) {
        throw new InvalidSettingsException("At least one column must be defined.");
    }
    final DatabaseConnectionSettings conn = dbSpec.getConnectionSettings(getCredentialsProvider());
    final DBTableCreator tableCreator = conn.getUtility().getTableCreator(m_config.getSchema(), getTableName(), m_config.isTempTable());
    final List<DBColumn> columns = m_config.getColumns();
    final List<DBKey> keys = m_config.getKeys();
    try {
        tableCreator.validateSettings(m_config.ifNotExists(), columns.toArray(new DBColumn[columns.size()]), keys.toArray(new DBKey[keys.size()]), m_config.getAdditionalOptions());
    } catch (Exception e) {
        throw new InvalidSettingsException(e.getMessage());
    }
    pushFlowVariables(tableCreator.getSchema(), tableCreator.getTableName());
    return new PortObjectSpec[] { dbSpec };
}
Also used : DBTableCreator(org.knime.core.node.port.database.tablecreator.DBTableCreator) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DatabaseConnectionSettings(org.knime.core.node.port.database.DatabaseConnectionSettings) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) DatabaseConnectionPortObjectSpec(org.knime.core.node.port.database.DatabaseConnectionPortObjectSpec) DBColumn(org.knime.core.node.port.database.tablecreator.DBColumn) DBKey(org.knime.core.node.port.database.tablecreator.DBKey) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DatabaseConnectionPortObjectSpec(org.knime.core.node.port.database.DatabaseConnectionPortObjectSpec)

Aggregations

DBColumn (org.knime.core.node.port.database.tablecreator.DBColumn)8 DBKey (org.knime.core.node.port.database.tablecreator.DBKey)3 ArrayList (java.util.ArrayList)2 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)2 DatabaseConnectionSettings (org.knime.core.node.port.database.DatabaseConnectionSettings)2 DBTableCreator (org.knime.core.node.port.database.tablecreator.DBTableCreator)2 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 TreeMap (java.util.TreeMap)1 JList (javax.swing.JList)1 JMenu (javax.swing.JMenu)1 JMenuItem (javax.swing.JMenuItem)1 NodeSettingsRO (org.knime.core.node.NodeSettingsRO)1 NodeSettingsWO (org.knime.core.node.NodeSettingsWO)1 PortObject (org.knime.core.node.port.PortObject)1 PortObjectSpec (org.knime.core.node.port.PortObjectSpec)1