Search in sources :

Example 6 with ForeignKey

use of org.obeonetwork.dsl.database.ForeignKey in project InformationSystem by ObeoNetwork.

the class MLDToEntity method createReference.

private void createReference(ForeignKey foreignKey) {
    Table sourceTable = foreignKey.getSourceTable();
    boolean isJoinTable = isJoinTable(sourceTable);
    if (isJoinTable) {
        sourceTable = getOppositeFKOnJoinTable(foreignKey).getTargetTable();
    }
    Table targetTable = foreignKey.getTargetTable();
    Entity sourceEntity = getFromOutputTraceabilityMap(targetTable, EntityPackage.Literals.ENTITY);
    Entity targetEntity = getFromOutputTraceabilityMap(sourceTable, EntityPackage.Literals.ENTITY);
    if (targetEntity == null) {
        // Entity was not found in the newly created entities
        // try to retrieve it from additional resources
        String targetTableKey = LabelProvider.getEntityKeyForTable(targetTable);
        EObject sourceObject = getAdditionalResourcesMap().get(targetTableKey);
        if (sourceObject instanceof Entity) {
            targetEntity = (Entity) sourceObject;
        } else {
            Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Cannot resolve entity: " + targetTableKey + " for Reference!"));
            return;
        }
    }
    // Try to retrieve existing reference
    Reference reference = getFromInputTraceabilityMap(foreignKey, EnvironmentPackage.Literals.REFERENCE);
    if (reference != null) {
        // Ensure it is contained by the right entity
        if (!EcoreUtil.equals(reference.getContainingType(), sourceEntity)) {
            sourceEntity.getOwnedReferences().add(reference);
        }
        // Ensure it references the right entity
        if (!EcoreUtil.equals(reference.getReferencedType(), targetEntity)) {
            reference.setReferencedType(targetEntity);
        }
    } else {
        boolean nullable = !areAllColumnsNonNullable(foreignKey);
        String referenceName = LabelProvider.getReferenceNameFromFK(foreignKey);
        Reference reference1 = EnvironmentFactory.eINSTANCE.createReference();
        sourceEntity.getOwnedReferences().add(reference1);
        reference1.setReferencedType(targetEntity);
        reference1.setName(referenceName);
        reference1.setNavigable(true);
        // Define the required multiplicity
        MultiplicityKind multiplicity = (nullable || isJoinTable) ? MultiplicityKind.ZERO_STAR_LITERAL : MultiplicityKind.ONE_STAR_LITERAL;
        reference1.setMultiplicity(multiplicity);
        // The reference does not already exist, we have to crate a new one
        reference = reference1;
    }
    addToOutputTraceability(foreignKey, reference);
    // The following properties are modified even if they already existed
    reference.setDescription("Contrainte FK avec la table " + sourceTable.getName());
    AnnotationHelper.setPhysicalNameAnnotation(reference, LabelProvider.getReferencePhysicalNameFromFK(foreignKey));
    // For join tables, set the oppositeOf property
    if (isJoinTable) {
        ForeignKey oppositeFK = getOppositeFKOnJoinTable(foreignKey);
        // Check if the reference corresponding to the opposite FK has already been created
        Reference oppositeReference = getFromOutputTraceabilityMap(oppositeFK, EnvironmentPackage.Literals.REFERENCE);
        if (oppositeReference != null) {
            reference.setOppositeOf(oppositeReference);
        }
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) Entity(org.obeonetwork.dsl.entity.Entity) MultiplicityKind(org.obeonetwork.dsl.environment.MultiplicityKind) Table(org.obeonetwork.dsl.database.Table) AbstractTable(org.obeonetwork.dsl.database.AbstractTable) Reference(org.obeonetwork.dsl.environment.Reference) EObject(org.eclipse.emf.ecore.EObject) ForeignKey(org.obeonetwork.dsl.database.ForeignKey)

Example 7 with ForeignKey

use of org.obeonetwork.dsl.database.ForeignKey in project InformationSystem by ObeoNetwork.

the class MySQLDataBaseBuilder method removeIndexForFK.

private void removeIndexForFK(TableContainer owner) {
    HashSet<String> fkNames = new HashSet<String>();
    for (AbstractTable table : owner.getTables()) {
        if (table instanceof Table) {
            for (ForeignKey foreignKey : ((Table) table).getForeignKeys()) {
                fkNames.add(foreignKey.getName());
            }
        }
    }
    for (AbstractTable t : owner.getTables()) {
        if (t instanceof Table) {
            Table table = (Table) t;
            ArrayList<Index> indices = new ArrayList<Index>();
            if (table.getIndexes().size() > 0) {
                for (Index index : table.getIndexes()) {
                    if (!fkNames.contains(index.getName())) {
                        indices.add(index);
                    } else {
                        for (IndexElement indexElt : index.getElements()) {
                            indexElt.getColumn().getIndexElements().remove(indexElt);
                        }
                    }
                }
                table.getIndexes().clear();
                table.getIndexes().addAll(indices);
            }
        }
    }
}
Also used : AbstractTable(org.obeonetwork.dsl.database.AbstractTable) Table(org.obeonetwork.dsl.database.Table) AbstractTable(org.obeonetwork.dsl.database.AbstractTable) ArrayList(java.util.ArrayList) Index(org.obeonetwork.dsl.database.Index) ForeignKey(org.obeonetwork.dsl.database.ForeignKey) HashSet(java.util.HashSet) IndexElement(org.obeonetwork.dsl.database.IndexElement)

Example 8 with ForeignKey

use of org.obeonetwork.dsl.database.ForeignKey in project InformationSystem by ObeoNetwork.

the class CreationUtils method createForeignKey.

public static ForeignKey createForeignKey(Table table) {
    ForeignKey foreignKey = DatabaseFactory.eINSTANCE.createForeignKey();
    table.getForeignKeys().add(foreignKey);
    return foreignKey;
}
Also used : ForeignKey(org.obeonetwork.dsl.database.ForeignKey)

Example 9 with ForeignKey

use of org.obeonetwork.dsl.database.ForeignKey in project InformationSystem by ObeoNetwork.

the class RemoveForeignKeyItemProvider method getText.

/**
 * This returns the label text for the adapted class.
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated NOT
 */
@Override
public String getText(Object object) {
    RemoveForeignKey removeForeignKey = (RemoveForeignKey) object;
    ForeignKey foreignKey = removeForeignKey.getForeignKey();
    return getString("_UI_RemoveForeignKey_type") + " " + foreignKey.getName();
}
Also used : RemoveForeignKey(org.obeonetwork.dsl.database.dbevolution.RemoveForeignKey) RemoveForeignKey(org.obeonetwork.dsl.database.dbevolution.RemoveForeignKey) ForeignKey(org.obeonetwork.dsl.database.ForeignKey)

Example 10 with ForeignKey

use of org.obeonetwork.dsl.database.ForeignKey in project InformationSystem by ObeoNetwork.

the class UpdateForeignKeyItemProvider method getText.

/**
 * This returns the label text for the adapted class.
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated NOT
 */
@Override
public String getText(Object object) {
    UpdateForeignKey updateForeignKey = (UpdateForeignKey) object;
    ForeignKey foreignKey = updateForeignKey.getForeignKey();
    return getString("_UI_UpdateForeignKey_type") + " " + foreignKey.getName();
}
Also used : UpdateForeignKey(org.obeonetwork.dsl.database.dbevolution.UpdateForeignKey) UpdateForeignKey(org.obeonetwork.dsl.database.dbevolution.UpdateForeignKey) ForeignKey(org.obeonetwork.dsl.database.ForeignKey)

Aggregations

ForeignKey (org.obeonetwork.dsl.database.ForeignKey)27 Table (org.obeonetwork.dsl.database.Table)16 EObject (org.eclipse.emf.ecore.EObject)8 AbstractTable (org.obeonetwork.dsl.database.AbstractTable)8 ForeignKeyElement (org.obeonetwork.dsl.database.ForeignKeyElement)8 Column (org.obeonetwork.dsl.database.Column)7 ArrayList (java.util.ArrayList)6 Reference (org.obeonetwork.dsl.environment.Reference)4 HashMap (java.util.HashMap)3 IStatus (org.eclipse.core.runtime.IStatus)3 Status (org.eclipse.core.runtime.Status)3 Viewer (org.eclipse.jface.viewers.Viewer)3 ViewerFilter (org.eclipse.jface.viewers.ViewerFilter)3 Constraint (org.obeonetwork.dsl.database.Constraint)3 Index (org.obeonetwork.dsl.database.Index)3 IndexElement (org.obeonetwork.dsl.database.IndexElement)3 Entity (org.obeonetwork.dsl.entity.Entity)3 Attribute (org.obeonetwork.dsl.environment.Attribute)3 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2