Example usage for org.apache.commons.collections.map ListOrderedMap entrySet

List of usage examples for org.apache.commons.collections.map ListOrderedMap entrySet

Introduction

In this page you can find the example usage for org.apache.commons.collections.map ListOrderedMap entrySet.

Prototype

public Set entrySet() 

Source Link

Usage

From source file:org.apache.ddlutils.io.DatabaseDataIO.java

/**
 * Sorts the given table according to their foreign key order.
 * //from   ww  w .ja  v a2 s .c o m
 * @param tables The tables
 * @return The sorted tables
 */
private List sortTables(Table[] tables) {
    ArrayList result = new ArrayList();
    HashSet processed = new HashSet();
    ListOrderedMap pending = new ListOrderedMap();

    for (int idx = 0; idx < tables.length; idx++) {
        Table table = tables[idx];

        if (table.getForeignKeyCount() == 0) {
            result.add(table);
            processed.add(table);
        } else {
            HashSet waitedFor = new HashSet();

            for (int fkIdx = 0; fkIdx < table.getForeignKeyCount(); fkIdx++) {
                Table waitedForTable = table.getForeignKey(fkIdx).getForeignTable();

                if (!table.equals(waitedForTable)) {
                    waitedFor.add(waitedForTable);
                }
            }
            pending.put(table, waitedFor);
        }
    }

    HashSet newProcessed = new HashSet();

    while (!processed.isEmpty() && !pending.isEmpty()) {
        newProcessed.clear();
        for (Iterator it = pending.entrySet().iterator(); it.hasNext();) {
            Map.Entry entry = (Map.Entry) it.next();
            Table table = (Table) entry.getKey();
            HashSet waitedFor = (HashSet) entry.getValue();

            waitedFor.removeAll(processed);
            if (waitedFor.isEmpty()) {
                it.remove();
                result.add(table);
                newProcessed.add(table);
            }
        }
        processed.clear();

        HashSet tmp = processed;

        processed = newProcessed;
        newProcessed = tmp;
    }
    // the remaining are within circular dependencies
    for (Iterator it = pending.keySet().iterator(); it.hasNext();) {
        result.add(it.next());
    }
    return result;
}

From source file:org.apache.ddlutils.platform.SqlBuilder.java

/**
 * Writes a statement that copies the data from the source to the target table. Note
 * that this copies only those columns that are in both tables.
 * Database-specific implementations might redefine this method though it usually
 * suffices to redefine the {@link #writeCastExpression(Column, Column)} method.
 * /*from w w w  . jav  a2 s . c  om*/
 * @param sourceTable The source table
 * @param targetTable The target table
 */
protected void copyData(Table sourceTable, Table targetTable) throws IOException {
    ListOrderedMap columns = new ListOrderedMap();

    for (int idx = 0; idx < sourceTable.getColumnCount(); idx++) {
        Column sourceColumn = sourceTable.getColumn(idx);
        Column targetColumn = targetTable.findColumn(sourceColumn.getName(),
                getPlatform().isDelimitedIdentifierModeOn());

        if (targetColumn != null) {
            columns.put(sourceColumn, targetColumn);
        }
    }

    print("INSERT INTO ");
    printIdentifier(getTableName(targetTable));
    print(" (");
    for (Iterator columnIt = columns.keySet().iterator(); columnIt.hasNext();) {
        printIdentifier(getColumnName((Column) columnIt.next()));
        if (columnIt.hasNext()) {
            print(",");
        }
    }
    print(") SELECT ");
    for (Iterator columnsIt = columns.entrySet().iterator(); columnsIt.hasNext();) {
        Map.Entry entry = (Map.Entry) columnsIt.next();

        writeCastExpression((Column) entry.getKey(), (Column) entry.getValue());
        if (columnsIt.hasNext()) {
            print(",");
        }
    }
    print(" FROM ");
    printIdentifier(getTableName(sourceTable));
    printEndOfStatement();
}