Search in sources :

Example 1 with StructuralFeature

use of orgomg.cwm.objectmodel.core.StructuralFeature in project tdq-studio-se by Talend.

the class UpdateRelationalSwitch method caseForeignKey.

@Override
public Boolean caseForeignKey(ForeignKey object) {
    ColumnSet columnSet = (ColumnSet) object.eContainer();
    if (columnSet == null) {
        return Boolean.FALSE;
    }
    ForeignKey rightColumn = null;
    if (rightElement instanceof ForeignKey) {
        rightColumn = (ForeignKey) rightElement;
    } else {
        return Boolean.FALSE;
    }
    object.setName(rightColumn.getName());
    object.getFeature().clear();
    List<StructuralFeature> refColumns = new ArrayList<StructuralFeature>();
    for (StructuralFeature newColumn : rightColumn.getFeature()) {
        for (Feature column : columnSet.getFeature()) {
            if (column.getName().equalsIgnoreCase(newColumn.getName())) {
                refColumns.add((StructuralFeature) column);
                break;
            }
        }
    }
    object.getFeature().addAll(refColumns);
    return Boolean.TRUE;
}
Also used : StructuralFeature(orgomg.cwm.objectmodel.core.StructuralFeature) ArrayList(java.util.ArrayList) ColumnSet(orgomg.cwm.resource.relational.ColumnSet) ForeignKey(orgomg.cwm.resource.relational.ForeignKey) Feature(orgomg.cwm.objectmodel.core.Feature) StructuralFeature(orgomg.cwm.objectmodel.core.StructuralFeature)

Example 2 with StructuralFeature

use of orgomg.cwm.objectmodel.core.StructuralFeature in project tdq-studio-se by Talend.

the class UniquePkAndFkTask method checkPkAndFk.

/**
 * DOC zshen Comment method "checkPkAndFk".
 *
 * @param element
 * @return merge pks or fks if their name is same each other
 */
private Boolean checkPkAndFk(TdTable element) {
    boolean needToSave = false;
    // case pk
    if (TableHelper.getPrimaryKeys(element).size() > 1) {
        List<PrimaryKey> primaryKeyList = TableHelper.getPrimaryKeys(element);
        for (int i = 1; i < primaryKeyList.size(); i++) {
            PrimaryKey pk = primaryKeyList.get(i);
            TdColumn column = (TdColumn) pk.getFeature().get(pk.getFeature().size() - 1);
            // remove old pk from column
            ColumnHelper.removePrimaryKey(column);
            // add new pk to column
            column.getUniqueKey().add(primaryKeyList.get(0));
            // add column to new pk
            primaryKeyList.get(0).getFeature().add(column);
            // remove old pk from table
            TableHelper.removeTableKey(element, pk);
        }
        needToSave = true;
    }
    // case fk
    if (TableHelper.getForeignKeys(element).size() > 1) {
        Map<String, ForeignKey> foreignKeySet = new HashMap<String, ForeignKey>();
        List<ForeignKey> foreignKeyList = TableHelper.getForeignKeys(element);
        for (int i = 0; i < foreignKeyList.size(); i++) {
            ForeignKey fk = foreignKeyList.get(i);
            if (foreignKeySet.get(fk.getName()) != null) {
                foreignKeySet.get(fk.getName()).getFeature().addAll(fk.getFeature());
                StructuralFeature[] structFeatureArray = fk.getFeature().toArray(new StructuralFeature[fk.getFeature().size()]);
                for (StructuralFeature fkFeature : structFeatureArray) {
                    TdColumn fkColumn = (TdColumn) fkFeature;
                    fkColumn.getKeyRelationship().remove(fk);
                    fkColumn.getKeyRelationship().add(foreignKeySet.get(fk.getName()));
                }
                // remove old fk from table
                TableHelper.removeForeignKey(element, fk);
                needToSave = true;
            } else {
                foreignKeySet.put(fk.getName(), fk);
            }
        }
    }
    return Boolean.valueOf(needToSave);
}
Also used : TdColumn(org.talend.cwm.relational.TdColumn) StructuralFeature(orgomg.cwm.objectmodel.core.StructuralFeature) HashMap(java.util.HashMap) PrimaryKey(orgomg.cwm.resource.relational.PrimaryKey) ForeignKey(orgomg.cwm.resource.relational.ForeignKey)

Aggregations

StructuralFeature (orgomg.cwm.objectmodel.core.StructuralFeature)2 ForeignKey (orgomg.cwm.resource.relational.ForeignKey)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 TdColumn (org.talend.cwm.relational.TdColumn)1 Feature (orgomg.cwm.objectmodel.core.Feature)1 ColumnSet (orgomg.cwm.resource.relational.ColumnSet)1 PrimaryKey (orgomg.cwm.resource.relational.PrimaryKey)1