Search in sources :

Example 1 with ForeignKey

use of orgomg.cwm.resource.relational.ForeignKey in project tdq-studio-se by Talend.

the class AddTdRelationalSwitch method caseForeignKey.

@Override
public Boolean caseForeignKey(ForeignKey object) {
    ColumnSet columnSet = null;
    TdColumn tdColumn = null;
    if (leftElement instanceof TdColumn) {
        tdColumn = (TdColumn) leftElement;
        columnSet = ColumnHelper.getColumnOwnerAsColumnSet(tdColumn);
    }
    if (columnSet == null) {
        return Boolean.FALSE;
    }
    String fkName = object.getName();
    ForeignKey foreignKey = orgomg.cwm.resource.relational.RelationalFactory.eINSTANCE.createForeignKey();
    foreignKey.setName(fkName);
    if (columnSet instanceof Table) {
        foreignKey = TableHelper.addForeignKey((TdTable) columnSet, foreignKey);
        if (tdColumn != null) {
            tdColumn.getKeyRelationship().add(foreignKey);
        }
    }
    return Boolean.TRUE;
}
Also used : TdColumn(org.talend.cwm.relational.TdColumn) Table(orgomg.cwm.resource.relational.Table) TdTable(org.talend.cwm.relational.TdTable) TdTable(org.talend.cwm.relational.TdTable) ColumnSet(orgomg.cwm.resource.relational.ColumnSet) ForeignKey(orgomg.cwm.resource.relational.ForeignKey)

Example 2 with ForeignKey

use of orgomg.cwm.resource.relational.ForeignKey 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 3 with ForeignKey

use of orgomg.cwm.resource.relational.ForeignKey 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

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