Example usage for org.apache.commons.collections.keyvalue MultiKey getKey

List of usage examples for org.apache.commons.collections.keyvalue MultiKey getKey

Introduction

In this page you can find the example usage for org.apache.commons.collections.keyvalue MultiKey getKey.

Prototype

public Object getKey(int index) 

Source Link

Document

Gets the key at the specified index.

Usage

From source file:com.nextep.designer.sqlgen.helpers.ColumnsSorter.java

public IBasicColumn[] getColumnsSortedArray() {
    IBasicColumn[] columnsArray = new IBasicColumn[columns.size()];

    int pos = 0;/*from   w  w  w.ja  v  a 2  s.  com*/
    for (MultiKey key : columns) {
        columnsArray[pos++] = (IBasicColumn) key.getKey(0);
    }

    return columnsArray;
}

From source file:com.level3.tca.schema.migration.ResourceMgr.java

public Map<String, String> makeResourceMap(String circuit, String concatField) {
    Map<String, String> map = new HashMap<>();
    MultiKey key = makeResourceKey(circuit, concatField);

    String uuid = resources.get(key);
    map.put(CIRCUIT, (String) key.getKey(0));
    map.put(VCIRCUIT, (String) key.getKey(1));
    map.put(CLLI, (String) key.getKey(2));
    map.put(GUID, uuid);/*from   w w w.j a  v a  2  s  .com*/
    return map;

}

From source file:com.level3.tca.schema.migration.ResourceMgr.java

public String makeInserts() throws SQLException {

    StringBuilder buf = new StringBuilder();
    for (Map.Entry pair : resources.entrySet()) {
        MultiKey key = (MultiKey) pair.getKey();

        buf.append("INSERT INTO resource ( guid, circuit, virtual_circuit, bclli, rgroup ) values ( "
                + Util.stringize(pair.getValue()) + Util.stringize((String) key.getKey(0))
                + Util.stringize((String) key.getKey(1)) + Util.stringize((String) key.getKey(2))
                + Util.stringize("", false) // groupId not used
                + " );\n");

        buf.append("-- DELETE from resource where guid = " + Util.stringize(pair.getValue(), false) + ";\n");
    }//www .j a  va  2 s . c o m

    return buf.toString();
}

From source file:com.nextep.designer.sqlgen.db2.impl.DB2Capturer.java

@Override
public Collection<IIndex> getIndexes(ICaptureContext context, IProgressMonitor monitor) {
    Map<MultiKey, IIndex> indexes = new HashMap<MultiKey, IIndex>();
    final Connection conn = (Connection) context.getConnectionObject();
    IFormatter formatter = context.getConnection().getDBVendor().getNameFormatter();

    Statement stmt = null;//from  ww  w.j  a v a 2 s  .  c  om
    ResultSet rset = null;
    try {
        stmt = conn.createStatement();

        rset = stmt.executeQuery("SELECT i.tabname, i.indname, i.indextype, i.uniquerule, i.remarks, " //$NON-NLS-1$
                + "c.colname, c.colseq, c.colorder " //$NON-NLS-1$
                + "FROM syscat.indexes i, syscat.indexcoluse c " //$NON-NLS-1$
                + "WHERE i.indschema = '" + context.getSchema() + "' AND i.tabschema = i.indschema " //$NON-NLS-1$ //$NON-NLS-2$
                + "AND i.uniquerule <> 'P' AND i.indextype = 'REG' " //$NON-NLS-1$
                + "AND c.indname = i.indname AND c.indschema = i.indschema " //$NON-NLS-1$
                + "ORDER BY i.tabname, i.indname, c.colseq"); //$NON-NLS-1$
        CaptureHelper.updateMonitor(monitor, getCounter(), 1, 1);

        IIndex currIndex = null;
        String currIndexName = null;
        boolean indexIsValid = false;

        while (rset.next()) {
            final String tableName = rset.getString("tabname"); //$NON-NLS-1$
            final String indexName = rset.getString("indname"); //$NON-NLS-1$
            final String indexType = rset.getString("indextype"); //$NON-NLS-1$
            final String uniqueRule = rset.getString("uniquerule"); //$NON-NLS-1$
            final String indexDesc = rset.getString("remarks"); //$NON-NLS-1$
            final String indexColumnName = rset.getString("colname"); //$NON-NLS-1$
            final int indexColumnSeq = rset.getInt("colseq"); //$NON-NLS-1$
            final String ascOrDesc = rset.getString("colorder"); //$NON-NLS-1$

            if (indexName != null && !"".equals(indexName.trim())) { //$NON-NLS-1$
                if (LOGGER.isDebugEnabled()) {
                    String logPrefix = "[" + indexName + "]"; //$NON-NLS-1$ //$NON-NLS-2$
                    LOGGER.debug("= " + logPrefix + " Index Metadata ="); //$NON-NLS-1$ //$NON-NLS-2$
                    LOGGER.debug(logPrefix + "[SYSCAT.INDEXES.TABNAME] " + tableName); //$NON-NLS-1$
                    LOGGER.debug(logPrefix + "[SYSCAT.INDEXES.INDEXTYPE] " + indexType); //$NON-NLS-1$
                    LOGGER.debug(logPrefix + "[SYSCAT.INDEXES.UNIQUERULE] " + uniqueRule); //$NON-NLS-1$
                    LOGGER.debug(logPrefix + "[SYSCAT.INDEXES.REMARKS] " + indexDesc); //$NON-NLS-1$
                    LOGGER.debug(logPrefix + "[SYSCAT.INDEXCOLUSE.COLNAME] " + indexColumnName); //$NON-NLS-1$
                    LOGGER.debug(logPrefix + "[SYSCAT.INDEXCOLUSE.COLSEQ] " + indexColumnSeq); //$NON-NLS-1$
                    LOGGER.debug(logPrefix + "[SYSCAT.INDEXCOLUSE.COLORDER] " + ascOrDesc); //$NON-NLS-1$
                }

                if (null == currIndexName || !currIndexName.equals(indexName) || indexIsValid) {
                    currIndexName = indexName;
                    final String formatTableName = formatter.format(tableName);
                    final String formatIndexName = formatter.format(indexName);
                    final String formatIndexColumnName = formatter.format(indexColumnName);

                    if (null == currIndex || !formatIndexName.equals(currIndex.getIndexName())) {
                        IVersionable<IIndex> v = VersionableFactory.createVersionable(IIndex.class);
                        currIndex = v.getVersionnedObject().getModel();
                        currIndex.setName(formatIndexName);
                        currIndex.setDescription(indexDesc);
                        currIndex.setIndexType("U".equals(uniqueRule) ? IndexType.UNIQUE //$NON-NLS-1$
                                : IndexType.NON_UNIQUE);
                        indexes.put(new MultiKey(formatTableName, formatIndexName), currIndex);
                        indexIsValid = true;
                        CaptureHelper.updateMonitor(monitor, getCounter(), 5, 1);
                    }

                    final IBasicColumn column = (IBasicColumn) context.getCapturedObject(
                            IElementType.getInstance(IBasicColumn.TYPE_ID),
                            CaptureHelper.getUniqueObjectName(formatTableName, formatIndexColumnName));
                    if (column != null) {
                        /*
                         * Columns are ordered by SYSCAT.INDEXCOLUSE.COLSEQ in the returned
                         * Resultset, so we don't have to specify the position of the index
                         * column when adding it to the index.
                         */
                        currIndex.addColumnRef(column.getReference());
                    } else {
                        LOGGER.warn("Index [" + formatIndexName
                                + "] has been ignored during import because the referencing column ["
                                + formatTableName + "[" + formatIndexColumnName //$NON-NLS-1$
                                + "]] could not be found in the current workspace");
                        indexIsValid = false;

                        /*
                         * Now the index is invalid, we remove it from the indexes list that
                         * will be returned to the caller of this method.
                         */
                        indexes.remove(new MultiKey(formatTableName, formatIndexName));
                    }
                }
            }
        }

        /*
         * Once the list of valid indexes has been set, we can link the indexes with their
         * corresponding table.
         */
        for (MultiKey key : indexes.keySet()) {
            String tableName = (String) key.getKey(0);
            IIndex index = indexes.get(key);
            final IBasicTable table = context.getTable(tableName);
            index.setIndexedTableRef(table.getReference());
            table.addIndex(index);
        }
    } catch (SQLException sqle) {
        LOGGER.error("Unable to fetch indexes from DB2 server: " + sqle.getMessage(), sqle);
    } finally {
        CaptureHelper.safeClose(rset, stmt);
    }

    return indexes.values();
}

From source file:com.nextep.designer.sqlgen.mysql.impl.MySqlCapturer.java

/**
 * Builds the map of unique keys for the specified table. Note that the
 * fetched unique keys will be added to the table
 * /*  www .j a  v  a2s.  c o  m*/
 * @param md
 *            {@link DatabaseMetaData} of the underlying database connection
 * @param monitor
 *            a {@link IProgressMonitor} to report progress to
 * @param table
 *            the {@link IBasicTable} to fetch unique keys for
 * @param columnsMap
 *            the map of {@link IBasicColumn} hashed by their unique name
 * @return a map of {@link IKeyConstraint} hashed by their unique name
 * @throws SQLException
 */
private Map<String, IKeyConstraint> buildUniqueKeyMap(DatabaseMetaData md, IProgressMonitor monitor,
        IBasicTable table, Map<String, IBasicColumn> columnsMap) throws SQLException {
    final Map<String, IKeyConstraint> keysMap = new HashMap<String, IKeyConstraint>();
    final String tabName = table.getName();
    ResultSet rset = null;
    try {
        // Creating primary keys for this table
        rset = md.getPrimaryKeys(null, null, tabName);
        IKeyConstraint uk = null;
        List<MultiKey> pkCols = new ArrayList<MultiKey>();
        // Because JDBC may not give us a sorted list, we first fill
        // a list with all pk columns, we sort it by KEY_SEQ, and we
        // fill our neXtep PK.
        while (rset.next()) {
            monitor.worked(1);
            final String pkName = rset.getString("PK_NAME"); //$NON-NLS-1$
            final String colName = rset.getString("COLUMN_NAME"); //$NON-NLS-1$
            final int colIndex = rset.getInt("KEY_SEQ") - 1; //$NON-NLS-1$
            pkCols.add(new MultiKey(pkName, colIndex, colName));
        }
        Collections.sort(pkCols, new Comparator<MultiKey>() {

            @Override
            public int compare(MultiKey o1, MultiKey o2) {
                if ((Integer) o1.getKeys()[1] > (Integer) o2.getKeys()[1]) {
                    return 1;
                }
                return -1;
            }
        });
        for (MultiKey pkCol : pkCols) {
            final String pkName = (String) pkCol.getKey(0);
            final String colName = (String) pkCol.getKey(2);

            monitor.worked(1);
            if (uk == null) {
                uk = new UniqueKeyConstraint(pkName, "", table); //$NON-NLS-1$
                uk.setConstraintType(ConstraintType.PRIMARY);
                table.addConstraint(uk);
                keysMap.put(tabName.toUpperCase(), uk);
            }
            // Retrieving UK column and adding it to UK
            final String columnKey = CaptureHelper.getUniqueObjectName(tabName, colName);
            final IBasicColumn ukColumn = columnsMap.get(columnKey);
            if (ukColumn != null) {
                uk.addColumn(ukColumn);
            } else {
                LOGGER.warn(MessageFormat.format(MySQLMessages.getString("capturer.mysql.uniqueKeyNotFound"), //$NON-NLS-1$
                        columnKey));
            }

        }
    } finally {
        CaptureHelper.safeClose(rset, null);
    }
    return keysMap;
}

From source file:com.nextep.designer.sqlgen.mssql.impl.MSSQLCapturer.java

/**
 * @param context a {@link ICaptureContext} representing the current capture context
 * @param monitor the {@link IProgressMonitor} to notify while capturing objects
 * @param capturedTables a <code>Map</code> of all tables previously captured hashed by a
 *        <code>MultiKey</code> containing their schema name and their object name
 *///ww w .  j av  a 2  s .com
private void updateTablesDescriptions(ICaptureContext context, IProgressMonitor monitor,
        Map<MultiKey, IBasicTable> capturedTables) {
    monitor.subTask("Retrieving tables and columns descriptions...");

    // We define the list of distinct schema names for all tables previously captured
    Set<String> schemaNames = new HashSet<String>();
    for (MultiKey key : capturedTables.keySet()) {
        schemaNames.add((String) key.getKey(0));
    }

    // For each unique schema, we fetch the extended properties of all tables
    for (String schemaName : schemaNames) {
        final Map<String, String> tablesDescriptions = getSchemaTablesDescriptions(context, monitor,
                schemaName);

        for (String tableName : tablesDescriptions.keySet()) {
            final IBasicTable table = capturedTables.get(new MultiKey(schemaName, tableName));
            if (table != null) {
                table.setDescription(tablesDescriptions.get(tableName));
            } else {
                LOGGER.warn("Table [" + schemaName + "." + tableName
                        + "] description has been ignored during import because the referenced "
                        + "table could not be found in the current workspace");
            }
        }
    }

    // For each captured table, we fetch the extended properties of all columns
    for (MultiKey key : capturedTables.keySet()) {
        final String schemaName = (String) key.getKey(0);
        final String tableName = (String) key.getKey(1);
        final IBasicTable table = capturedTables.get(key);
        final Map<String, String> columnsDescriptions = getTableColumnsDescriptions(context, monitor,
                schemaName, tableName);

        if (!columnsDescriptions.isEmpty()) {
            // We create a map of the table's columns hashed by their name so we can easily
            // retrieve them to set their description.
            Map<String, IBasicColumn> tableColumns = new HashMap<String, IBasicColumn>();
            for (IBasicColumn column : table.getColumns()) {
                tableColumns.put(column.getName(), column);
            }

            // We browse the list of retrieved column descriptions to set each column
            // description.
            for (String columnName : columnsDescriptions.keySet()) {
                final IBasicColumn column = tableColumns.get(columnName);
                if (column != null) {
                    column.setDescription(columnsDescriptions.get(columnName));
                } else {
                    LOGGER.warn("Column [" + schemaName + "." + tableName + "." + columnName
                            + "] description has been ignored during import because the "
                            + "referenced column could not be found in the current workspace");
                }
            }
        }
    }
}

From source file:com.nextep.designer.sqlgen.mssql.impl.MSSQLCapturer.java

/**
 * @param context a {@link ICaptureContext} representing the current capture context
 * @param monitor the {@link IProgressMonitor} to notify while capturing objects
 * @param capturedTables a <code>Map</code> of all tables previously captured hashed by a
 *        <code>MultiKey</code> containing their schema name and their object name
 *//*w  w  w. j  a v a2 s  .c om*/
private void updateColumnsProperties(ICaptureContext context, IProgressMonitor monitor,
        Map<MultiKey, IBasicTable> capturedTables) {
    /*
     * Retrieving identity columns attributes (seed and increment) to set accordingly the length
     * and precision attributes of the corresponding columns. This is a temporary workaround to
     * handle the columns IDENTITY property until we implement a MSSQL specific model to store
     * it properly in the repository.
     */
    final Map<MultiKey, MultiKey> identityColumnsAttributes = getIdentityColumnsAttributes(context, monitor);

    identityColsLoop: for (MultiKey tableKey : identityColumnsAttributes.keySet()) {
        final IBasicTable table = capturedTables.get(tableKey);

        if (table != null) {
            MultiKey identityColKey = identityColumnsAttributes.get(tableKey);
            String identityColName = (String) identityColKey.getKey(0);

            // TODO [BGA] Add a getColumn(String columnName) method to the IBasicTable interface
            for (IBasicColumn column : table.getColumns()) {
                final String columnName = column.getName();

                if (columnName.equals(identityColName)) {
                    final IDatatype colDatatype = column.getDatatype();

                    if (colDatatype.getName().contains("IDENTITY")) { //$NON-NLS-1$
                        final int seed = (Integer) identityColKey.getKey(1);
                        final int increment = (Integer) identityColKey.getKey(2);
                        colDatatype.setLength(seed);
                        colDatatype.setPrecision(increment);
                    } else {
                        LOGGER.warn("Identity column [" + tableKey.getKey(0) + "." + tableKey.getKey(1) + "."
                                + identityColName + "] attributes have been discarded during import because "
                                + "the IDENTITY property of the referenced column "
                                + "could not be fetched from SQL Server server");
                    }

                    // Once we have found the corresponding IDENTITY column, we resume the
                    // identity columns loop.
                    continue identityColsLoop;
                }
            }

            // No corresponding column has been found in the captured columns of the referenced
            // table, we raise a warning.
            LOGGER.warn("Identity column [" + identityColName
                    + "] attributes have been discarded during import because "
                    + "the corresponding column could not be found in the current workspace "
                    + "for the referenced table [" + tableKey.getKey(0) + "." + tableKey.getKey(1) + "]");
        } else {
            LOGGER.warn("Identity column [" + identityColumnsAttributes.get(tableKey).getKey(0)
                    + "] attributes have been discarded during import because " + "the referenced table ["
                    + tableKey.getKey(0) + "." + tableKey.getKey(1)
                    + "] could not be found in the current workspace");
        }
    }

    // TODO [BGA] Retrieve other columns properties (FILESTREAM,ROWGUIDCOL,etc.)
}

From source file:com.qq.tars.service.monitor.TARSPropertyMonitorService.java

private void dimension(TARSPropertyMonitorCondition condition, MultiKey key, TARSPropertyMonitorDataRow row) {
    if (!"master_name".equals(condition.getGroupBy())) {
        row.setMasterName(null != condition.getMasterName() ? condition.getMasterName() : "%");
    }//  www  .j a v a 2  s .c  om

    if (!"master_ip".equals(condition.getGroupBy())) {
        row.setMasterIp(null != condition.getMasterIp() ? condition.getMasterIp() : "%");
    }

    if (!"property_name".equals(condition.getGroupBy())) {
        row.setPropertyName(null != condition.getPropertyName() ? condition.getPropertyName() : "%");
    }

    if (!"policy".equals(condition.getGroupBy())) {
        row.setPolicy(null != condition.getPolicy() ? condition.getPolicy() : "%");
    }

    List<String> callGroupBy = getCallGroupBy(condition);
    for (int i = 0; i < callGroupBy.size(); i++) {
        String callGroup = callGroupBy.get(i);
        String callGroupValue = (String) key.getKey(i);

        if ("f_date".equals(callGroup)) {
            row.setShowDate(callGroupValue);
        } else if ("f_tflag".equals(callGroup)) {
            row.setShowTime(callGroupValue);
        } else if ("master_name".equals(callGroup)) {
            row.setMasterName(callGroupValue);
        } else if ("master_ip".equals(callGroup)) {
            row.setMasterIp(callGroupValue);
        } else if ("property_name".equals(callGroup)) {
            row.setPropertyName(callGroupValue);
        } else if ("policy".equals(callGroup)) {
            row.setPolicy(callGroupValue);
        }
    }
}

From source file:com.qq.tars.service.monitor.TARSStatMonitorService.java

private void dimension(TARSStatMonitorCondition condition, MultiKey key, TARSStatMonitorDataRow row) {
    if (!"master_name".equals(condition.getGroupBy())) {
        row.setMasterName(null != condition.getMasterName() ? condition.getMasterName() : "%");
    }//from   w  w w.  ja  v  a 2s  . c o m

    if (!"slave_name".equals(condition.getGroupBy())) {
        row.setSlaveName(null != condition.getSlaveName() ? condition.getSlaveName() : "%");
    }

    if (!"interface_name".equals(condition.getGroupBy())) {
        row.setInterfaceName(null != condition.getInterfaceName() ? condition.getInterfaceName() : "%");
    }

    if (!"master_ip".equals(condition.getGroupBy())) {
        row.setMasterIp(null != condition.getMasterIp() ? condition.getMasterIp() : "%");
    }

    if (!"slave_ip".equals(condition.getGroupBy())) {
        row.setSlaveIp(null != condition.getSlaveIp() ? condition.getSlaveIp() : "%");
    }

    List<String> callGroupBy = getCallGroupBy(condition);
    for (int i = 0; i < callGroupBy.size(); i++) {
        String callGroup = callGroupBy.get(i);
        String callGroupValue = (String) key.getKey(i);

        if ("f_date".equals(callGroup)) {
            row.setShowDate(callGroupValue);
        } else if ("f_tflag".equals(callGroup)) {
            row.setShowTime(callGroupValue);
        } else if ("master_name".equals(callGroup)) {
            row.setMasterName(callGroupValue);
        } else if ("slave_name".equals(callGroup)) {
            row.setSlaveName(callGroupValue);
        } else if ("interface_name".equals(callGroup)) {
            row.setInterfaceName(callGroupValue);
        } else if ("master_ip".equals(callGroup)) {
            row.setMasterIp(callGroupValue);
        } else if ("slave_ip".equals(callGroup)) {
            row.setSlaveIp(callGroupValue);
        }
    }
}

From source file:org.apromore.dao.jpa.FragmentDistanceRepositoryCustomImpl.java

@Override
@Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false)
public void saveDistances(final MultiKeyMap distanceMap) {
    MapIterator mi = distanceMap.mapIterator();
    List<DistanceDO> distances = new ArrayList<>();
    while (mi.hasNext()) {
        MultiKey fragmentIds = (MultiKey) mi.next();
        Double ged = (Double) mi.getValue();

        if (getDistance((Integer) fragmentIds.getKey(0), (Integer) fragmentIds.getKey(1)) == null) {
            distances//from  w w w  .  ja  v a 2 s  . c  o m
                    .add(new DistanceDO((Integer) fragmentIds.getKey(0), (Integer) fragmentIds.getKey(1), ged));
        }
    }
    persistDistance(distances);
}