use of orgomg.cwm.resource.relational.PrimaryKey in project tdq-studio-se by Talend.
the class AddTdRelationalSwitch method casePrimaryKey.
@Override
public Boolean casePrimaryKey(PrimaryKey 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 pkName = object.getName();
PrimaryKey primaryKey = orgomg.cwm.resource.relational.RelationalFactory.eINSTANCE.createPrimaryKey();
primaryKey.setName(pkName);
if (columnSet instanceof Table) {
primaryKey = TableHelper.addPrimaryKey((TdTable) columnSet, primaryKey);
if (tdColumn != null) {
tdColumn.getUniqueKey().add(primaryKey);
}
}
return Boolean.TRUE;
}
use of orgomg.cwm.resource.relational.PrimaryKey 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);
}
Aggregations