Search in sources :

Example 16 with ForeignKey

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

the class MLDToEntity method addPhysicalUnique.

private void addPhysicalUnique(Index index) {
    if (index.isUnique()) {
        if (index.getElements().size() == 1) {
            // Add annotation on Attribute
            IndexElement indexElement = index.getElements().get(0);
            String annotationValue = indexElement.isAsc() ? "ASC" : "DESC";
            Attribute uniqueAttribute = getFromOutputTraceabilityMap(indexElement.getColumn(), EnvironmentPackage.Literals.ATTRIBUTE);
            if (uniqueAttribute != null) {
                AnnotationHelper.setPhysicalUniqueAnnotation(uniqueAttribute, annotationValue);
            } else {
                // We can only handle the FK situation
                if (indexElement.getColumn().isInForeignKey()) {
                    // Search the reference corresponding to a FK
                    for (ForeignKey fk : indexElement.getColumn().getForeignKeys()) {
                        Reference reference = getFromOutputTraceabilityMap(fk, EnvironmentPackage.Literals.REFERENCE);
                        if (reference != null) {
                            AnnotationHelper.setPhysicalUniqueAnnotation(reference, annotationValue);
                        }
                    }
                }
            }
        } else {
            // Add annotation on Table
            Entity entity = getFromOutputTraceabilityMap(index.getOwner(), EntityPackage.Literals.ENTITY);
            // Retrieve the current value of the annotation.
            String annotationValue = AnnotationHelper.getPhysicalUnique(entity);
            // Construct the term corresponding to the index being considered.
            String annotationTerm = "";
            int i = 0;
            for (IndexElement indexElement : index.getElements()) {
                if (i > 0) {
                    annotationTerm += ",";
                }
                annotationTerm += indexElement.getColumn().getName() + ":";
                if (indexElement.isAsc()) {
                    annotationTerm += "ASC";
                } else {
                    annotationTerm += "DESC";
                }
                i++;
            }
            if (annotationValue != null && !annotationValue.isEmpty()) {
                annotationValue += " | ";
            } else if (annotationValue == null) {
                annotationValue = "";
            }
            annotationValue += annotationTerm;
            AnnotationHelper.setPhysicalUniqueAnnotation(entity, annotationValue);
        }
    }
}
Also used : Entity(org.obeonetwork.dsl.entity.Entity) Attribute(org.obeonetwork.dsl.environment.Attribute) Reference(org.obeonetwork.dsl.environment.Reference) ForeignKey(org.obeonetwork.dsl.database.ForeignKey) Constraint(org.obeonetwork.dsl.database.Constraint) IndexElement(org.obeonetwork.dsl.database.IndexElement)

Example 17 with ForeignKey

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

the class MLDToEntity method getOppositeFKOnJoinTable.

private ForeignKey getOppositeFKOnJoinTable(ForeignKey foreignKey) {
    Table sourceTable = foreignKey.getSourceTable();
    if (isJoinTable(sourceTable)) {
        ForeignKey fk1 = sourceTable.getForeignKeys().get(0);
        ForeignKey fk2 = sourceTable.getForeignKeys().get(1);
        if (foreignKey.equals(fk1)) {
            return fk2;
        } else if (foreignKey.equals(fk2)) {
            return fk1;
        }
    }
    return null;
}
Also used : Table(org.obeonetwork.dsl.database.Table) AbstractTable(org.obeonetwork.dsl.database.AbstractTable) ForeignKey(org.obeonetwork.dsl.database.ForeignKey)

Example 18 with ForeignKey

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

the class MpdToMldBidiRules method createFK.

private void createFK(ForeignKey sourceFK, Table targetTable) {
    if (sourceFK.getTarget() == null) {
        return;
    }
    ForeignKey targetFK = getFromInputTraceabilityMap(sourceFK, DatabasePackage.Literals.FOREIGN_KEY);
    if (targetFK != null) {
    // Reuse existing FK
    } else {
        // We have to create a new FK
        targetFK = DatabaseFactory.eINSTANCE.createForeignKey();
        targetTable.getForeignKeys().add(targetFK);
    }
    addToOutputTraceability(sourceFK, targetFK);
    targetFK.setName(sourceFK.getName());
    targetFK.setComments(sourceFK.getComments());
    // Try to retrieve the table referenced by the FK
    Table fkTargetTable = getFromOutputTraceabilityMap(sourceFK.getTargetTable(), DatabasePackage.Literals.TABLE);
    if (fkTargetTable == null) {
        // We look into the additional resources
        String fkTargetTableKey = ModelUtils.getKey(sourceFK.getTarget());
        fkTargetTable = (Table) getAdditionalResourcesMap().get(fkTargetTableKey);
    }
    if (fkTargetTable == null) {
        Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Cannot resolve table: " + sourceFK.getTargetTable().getName() + " for ForeignKey: " + sourceFK.getName() + "!"));
    }
    targetFK.setTarget(fkTargetTable);
    for (ForeignKeyElement sourceFkElement : sourceFK.getElements()) {
        createFKElement(sourceFkElement, targetFK);
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) ForeignKeyElement(org.obeonetwork.dsl.database.ForeignKeyElement) Table(org.obeonetwork.dsl.database.Table) AbstractTable(org.obeonetwork.dsl.database.AbstractTable) ForeignKey(org.obeonetwork.dsl.database.ForeignKey)

Example 19 with ForeignKey

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

the class MpdToMldBidiRules method createForeignKeys.

private void createForeignKeys(TableContainer sourceTableContainer) {
    // create all foreignkeys
    for (AbstractTable sourceAbstractTable : sourceTableContainer.getTables()) {
        if (sourceAbstractTable instanceof Table) {
            Table sourceTable = (Table) sourceAbstractTable;
            Table targetTable = getFromOutputTraceabilityMap(sourceTable, DatabasePackage.Literals.TABLE);
            for (ForeignKey foreignKey : sourceTable.getForeignKeys()) {
                createFK(foreignKey, targetTable);
            }
        }
    }
    if (sourceTableContainer instanceof DataBase) {
        DataBase sourceDataBase = (DataBase) sourceTableContainer;
        for (Schema sourceSchema : sourceDataBase.getSchemas()) {
            createForeignKeys(sourceSchema);
        }
    }
}
Also used : AbstractTable(org.obeonetwork.dsl.database.AbstractTable) Table(org.obeonetwork.dsl.database.Table) AbstractTable(org.obeonetwork.dsl.database.AbstractTable) Schema(org.obeonetwork.dsl.database.Schema) ForeignKey(org.obeonetwork.dsl.database.ForeignKey) DataBase(org.obeonetwork.dsl.database.DataBase)

Example 20 with ForeignKey

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

the class ForeignKeyElementPropertiesEditionComponent method initPart.

/**
 * {@inheritDoc}
 *
 * @see org.eclipse.emf.eef.runtime.api.component.IPropertiesEditionComponent#initPart(java.lang.Object, int, org.eclipse.emf.ecore.EObject,
 *      org.eclipse.emf.ecore.resource.ResourceSet)
 * @generated NOT
 */
public void initPart(Object key, int kind, EObject elt, ResourceSet allResource) {
    setInitializing(true);
    if (editingPart != null && key == partKey) {
        editingPart.setContext(elt, allResource);
        final ForeignKeyElement foreignKeyElement = (ForeignKeyElement) elt;
        final ForeignKeyElementPropertiesEditionPart foreignKeyElementPart = (ForeignKeyElementPropertiesEditionPart) editingPart;
        // init values
        if (foreignKeyElement.getComments() != null && isAccessible(DatabaseViewsRepository.ForeignKeyElement.Properties.comments))
            foreignKeyElementPart.setComments(EcoreUtil.convertToString(EcorePackage.Literals.ESTRING, foreignKeyElement.getComments()));
        if (isAccessible(DatabaseViewsRepository.ForeignKeyElement.Properties.fKColumn)) {
            // init part
            fkColumnSettings = new EObjectFlatComboSettings(foreignKeyElement, DatabasePackage.eINSTANCE.getForeignKeyElement_FkColumn());
            foreignKeyElementPart.initFKColumn(fkColumnSettings);
            // set the button mode
            foreignKeyElementPart.setFKColumnButtonMode(ButtonsModeEnum.BROWSE);
        }
        if (isAccessible(DatabaseViewsRepository.ForeignKeyElement.Properties.pKColumn)) {
            // init part
            pkColumnSettings = new EObjectFlatComboSettings(foreignKeyElement, DatabasePackage.eINSTANCE.getForeignKeyElement_PkColumn());
            foreignKeyElementPart.initPKColumn(pkColumnSettings);
            // set the button mode
            foreignKeyElementPart.setPKColumnButtonMode(ButtonsModeEnum.BROWSE);
        }
        // Custom code to prevent NPE when creating new FKElement
        ForeignKeyElement fkElt = (ForeignKeyElement) semanticObject;
        if (fkElt.getFkColumn() != null) {
            if (sourceTableSettings.getValue() != null && isAccessible(DatabaseViewsRepository.ForeignKeyElement.Properties.sourceTable))
                foreignKeyElementPart.setSourceTable(EEFConverterUtil.convertToString(EcorePackage.Literals.ESTRING, sourceTableSettings.getValue()));
        } else {
            EObject container = fkElt.eContainer();
            if (container instanceof ForeignKey) {
                Table table = ((ForeignKey) container).getSourceTable();
                if (table != null) {
                    foreignKeyElementPart.setSourceTable(table.getName());
                }
            }
        }
        if (fkElt.getPkColumn() != null) {
            if (targetTableSettings.getValue() != null && isAccessible(DatabaseViewsRepository.ForeignKeyElement.Properties.targetTable))
                foreignKeyElementPart.setTargetTable(EEFConverterUtil.convertToString(EcorePackage.Literals.ESTRING, targetTableSettings.getValue()));
        } else {
            EObject container = fkElt.eContainer();
            if (container instanceof ForeignKey) {
                Table table = ((ForeignKey) container).getTargetTable();
                if (table != null) {
                    foreignKeyElementPart.setTargetTable(table.getName());
                }
            }
        }
        if (isAccessible(DatabaseViewsRepository.ForeignKeyElement.Properties.fKColumn)) {
            foreignKeyElementPart.addFilterToFKColumn(new ViewerFilter() {

                /**
                 * {@inheritDoc}
                 *
                 * @see org.eclipse.jface.viewers.ViewerFilter#select(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
                 */
                public boolean select(Viewer viewer, Object parentElement, Object element) {
                    // $NON-NLS-1$
                    return (element instanceof String && element.equals("")) || (element instanceof Column);
                }
            });
        // Start of user code for additional businessfilters for fkColumn
        // End of user code
        }
        if (isAccessible(DatabaseViewsRepository.ForeignKeyElement.Properties.pKColumn)) {
            foreignKeyElementPart.addFilterToPKColumn(new ViewerFilter() {

                /**
                 * {@inheritDoc}
                 *
                 * @see org.eclipse.jface.viewers.ViewerFilter#select(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
                 */
                public boolean select(Viewer viewer, Object parentElement, Object element) {
                    // $NON-NLS-1$
                    return (element instanceof String && element.equals("")) || (element instanceof Column);
                }
            });
        // Start of user code for additional businessfilters for pkColumn
        // End of user code
        }
    // init values for referenced views
    // init filters for referenced views
    }
    setInitializing(false);
}
Also used : ForeignKeyElement(org.obeonetwork.dsl.database.ForeignKeyElement) Table(org.obeonetwork.dsl.database.Table) ViewerFilter(org.eclipse.jface.viewers.ViewerFilter) Column(org.obeonetwork.dsl.database.Column) EObject(org.eclipse.emf.ecore.EObject) Viewer(org.eclipse.jface.viewers.Viewer) EObject(org.eclipse.emf.ecore.EObject) EObjectFlatComboSettings(org.eclipse.emf.eef.runtime.ui.widgets.eobjflatcombo.EObjectFlatComboSettings) ForeignKeyElementPropertiesEditionPart(org.obeonetwork.dsl.database.parts.ForeignKeyElementPropertiesEditionPart) 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