Search in sources :

Example 6 with ForeignKeyElement

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

the class EntityToMLD method handleMultipleForeignKeys.

private void handleMultipleForeignKeys(Table table) {
    // multipleFKs contains foreign keys targetting a same table (the table is used as key)
    Map<Table, List<ForeignKey>> multipleFKs = new HashMap<Table, List<ForeignKey>>();
    for (ForeignKey fk : table.getForeignKeys()) {
        for (ForeignKey otherFK : table.getForeignKeys()) {
            Table targetTable = fk.getTargetTable();
            if (fk != otherFK && targetTable == otherFK.getTargetTable()) {
                if (multipleFKs.get(targetTable) == null) {
                    multipleFKs.put(targetTable, new ArrayList<ForeignKey>());
                }
                if (!multipleFKs.get(targetTable).contains(otherFK)) {
                    multipleFKs.get(targetTable).add(otherFK);
                }
            }
        }
    }
    // Rename columns targetted by the FKs
    for (List<ForeignKey> fks : multipleFKs.values()) {
        int counter = 0;
        // Sort FKs
        List<ForeignKey> sortedFks = new ArrayList<ForeignKey>(fks);
        Collections.sort(sortedFks, new Comparator<ForeignKey>() {

            @Override
            public int compare(ForeignKey fk1, ForeignKey fk2) {
                return columnIndex(fk1.getElements().get(0).getFkColumn()) - columnIndex(fk2.getElements().get(0).getFkColumn());
            }

            private int columnIndex(Column column) {
                return column.getOwner().getColumns().indexOf(column);
            }
        });
        for (ForeignKey fk : sortedFks) {
            counter = counter + 1;
            for (ForeignKeyElement fkElt : fk.getElements()) {
                fkElt.getFkColumn().getOwner().getColumns().indexOf(fkElt.getFkColumn());
                fkElt.getFkColumn().setName(getFKColumnName(fkElt, counter));
            }
        }
    }
}
Also used : ForeignKeyElement(org.obeonetwork.dsl.database.ForeignKeyElement) Table(org.obeonetwork.dsl.database.Table) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) ForeignKey(org.obeonetwork.dsl.database.ForeignKey) Constraint(org.obeonetwork.dsl.database.Constraint) Column(org.obeonetwork.dsl.database.Column) List(java.util.List) ArrayList(java.util.ArrayList)

Example 7 with ForeignKeyElement

use of org.obeonetwork.dsl.database.ForeignKeyElement 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 8 with ForeignKeyElement

use of org.obeonetwork.dsl.database.ForeignKeyElement 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)

Example 9 with ForeignKeyElement

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

the class ForeignKeyPropertiesEditionComponent method updateSemanticModel.

/**
 * {@inheritDoc}
 * @see org.eclipse.emf.eef.runtime.impl.components.StandardPropertiesEditionComponent#updateSemanticModel(org.eclipse.emf.eef.runtime.api.notify.IPropertiesEditionEvent)
 */
public void updateSemanticModel(final IPropertiesEditionEvent event) {
    ForeignKey foreignKey = (ForeignKey) semanticObject;
    if (DatabaseViewsRepository.ForeignKey.Properties.name == event.getAffectedEditor()) {
        foreignKey.setName((java.lang.String) EEFConverterUtil.createFromString(EcorePackage.Literals.ESTRING, (String) event.getNewValue()));
    }
    if (DatabaseViewsRepository.ForeignKey.Properties.target == event.getAffectedEditor()) {
        if (event.getKind() == PropertiesEditionEvent.SET) {
            targetSettings.setToReference((Table) event.getNewValue());
        } else if (event.getKind() == PropertiesEditionEvent.ADD) {
            Table eObject = DatabaseFactory.eINSTANCE.createTable();
            EObjectPropertiesEditionContext context = new EObjectPropertiesEditionContext(editingContext, this, eObject, editingContext.getAdapterFactory());
            PropertiesEditingProvider provider = (PropertiesEditingProvider) editingContext.getAdapterFactory().adapt(eObject, PropertiesEditingProvider.class);
            if (provider != null) {
                PropertiesEditingPolicy policy = provider.getPolicy(context);
                if (policy != null) {
                    policy.execute();
                }
            }
            targetSettings.setToReference(eObject);
        }
    }
    if (DatabaseViewsRepository.ForeignKey.Properties.elements == event.getAffectedEditor()) {
        if (event.getKind() == PropertiesEditionEvent.ADD) {
            EReferencePropertiesEditionContext context = new EReferencePropertiesEditionContext(editingContext, this, elementsSettings, editingContext.getAdapterFactory());
            PropertiesEditingProvider provider = (PropertiesEditingProvider) editingContext.getAdapterFactory().adapt(semanticObject, PropertiesEditingProvider.class);
            if (provider != null) {
                PropertiesEditingPolicy policy = provider.getPolicy(context);
                if (policy instanceof CreateEditingPolicy) {
                    policy.execute();
                }
            }
        } else if (event.getKind() == PropertiesEditionEvent.EDIT) {
            EObjectPropertiesEditionContext context = new EObjectPropertiesEditionContext(editingContext, this, (EObject) event.getNewValue(), editingContext.getAdapterFactory());
            PropertiesEditingProvider provider = (PropertiesEditingProvider) editingContext.getAdapterFactory().adapt((EObject) event.getNewValue(), PropertiesEditingProvider.class);
            if (provider != null) {
                PropertiesEditingPolicy editionPolicy = provider.getPolicy(context);
                if (editionPolicy != null) {
                    editionPolicy.execute();
                }
            }
        } else if (event.getKind() == PropertiesEditionEvent.REMOVE) {
            elementsSettings.removeFromReference((EObject) event.getNewValue());
        } else if (event.getKind() == PropertiesEditionEvent.MOVE) {
            elementsSettings.move(event.getNewIndex(), (ForeignKeyElement) event.getNewValue());
        }
    }
    if (DatabaseViewsRepository.ForeignKey.Properties.comments == event.getAffectedEditor()) {
        foreignKey.setComments((java.lang.String) EEFConverterUtil.createFromString(EcorePackage.Literals.ESTRING, (String) event.getNewValue()));
    }
}
Also used : ForeignKeyElement(org.obeonetwork.dsl.database.ForeignKeyElement) EObjectPropertiesEditionContext(org.eclipse.emf.eef.runtime.context.impl.EObjectPropertiesEditionContext) Table(org.obeonetwork.dsl.database.Table) PropertiesEditingProvider(org.eclipse.emf.eef.runtime.providers.PropertiesEditingProvider) EObject(org.eclipse.emf.ecore.EObject) PropertiesEditingPolicy(org.eclipse.emf.eef.runtime.policies.PropertiesEditingPolicy) CreateEditingPolicy(org.eclipse.emf.eef.runtime.policies.impl.CreateEditingPolicy) ForeignKey(org.obeonetwork.dsl.database.ForeignKey) EReferencePropertiesEditionContext(org.eclipse.emf.eef.runtime.context.impl.EReferencePropertiesEditionContext)

Example 10 with ForeignKeyElement

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

the class ForeignKeyPropertiesEditionComponent 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)
 */
public void initPart(Object key, int kind, EObject elt, ResourceSet allResource) {
    setInitializing(true);
    if (editingPart != null && key == partKey) {
        editingPart.setContext(elt, allResource);
        final ForeignKey foreignKey = (ForeignKey) elt;
        final ForeignKeyPropertiesEditionPart foreignKeyPart = (ForeignKeyPropertiesEditionPart) editingPart;
        // init values
        if (isAccessible(DatabaseViewsRepository.ForeignKey.Properties.name))
            foreignKeyPart.setName(EEFConverterUtil.convertToString(EcorePackage.Literals.ESTRING, foreignKey.getName()));
        if (isAccessible(DatabaseViewsRepository.ForeignKey.Properties.target)) {
            // init part
            targetSettings = new EObjectFlatComboSettings(foreignKey, DatabasePackage.eINSTANCE.getForeignKey_Target());
            foreignKeyPart.initTarget(targetSettings);
            // set the button mode
            foreignKeyPart.setTargetButtonMode(ButtonsModeEnum.BROWSE);
        }
        if (isAccessible(DatabaseViewsRepository.ForeignKey.Properties.elements)) {
            elementsSettings = new ReferencesTableSettings(foreignKey, DatabasePackage.eINSTANCE.getForeignKey_Elements());
            foreignKeyPart.initElements(elementsSettings);
        }
        if (isAccessible(DatabaseViewsRepository.ForeignKey.Properties.comments))
            foreignKeyPart.setComments(EcoreUtil.convertToString(EcorePackage.Literals.ESTRING, foreignKey.getComments()));
        if (isAccessible(DatabaseViewsRepository.ForeignKey.Properties.target)) {
            foreignKeyPart.addFilterToTarget(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 Table);
                }
            });
        // Start of user code for additional businessfilters for target
        // End of user code
        }
        if (isAccessible(DatabaseViewsRepository.ForeignKey.Properties.elements)) {
            foreignKeyPart.addFilterToElements(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 ForeignKeyElement);
                }
            });
        // Start of user code for additional businessfilters for elements
        // End of user code
        }
    // init values for referenced views
    // init filters for referenced views
    }
    setInitializing(false);
}
Also used : ForeignKeyElement(org.obeonetwork.dsl.database.ForeignKeyElement) ReferencesTableSettings(org.eclipse.emf.eef.runtime.ui.widgets.referencestable.ReferencesTableSettings) Table(org.obeonetwork.dsl.database.Table) ViewerFilter(org.eclipse.jface.viewers.ViewerFilter) ForeignKeyPropertiesEditionPart(org.obeonetwork.dsl.database.parts.ForeignKeyPropertiesEditionPart) Viewer(org.eclipse.jface.viewers.Viewer) EObject(org.eclipse.emf.ecore.EObject) EObjectFlatComboSettings(org.eclipse.emf.eef.runtime.ui.widgets.eobjflatcombo.EObjectFlatComboSettings) ForeignKey(org.obeonetwork.dsl.database.ForeignKey)

Aggregations

ForeignKeyElement (org.obeonetwork.dsl.database.ForeignKeyElement)11 ForeignKey (org.obeonetwork.dsl.database.ForeignKey)8 Table (org.obeonetwork.dsl.database.Table)7 Column (org.obeonetwork.dsl.database.Column)6 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 EObject (org.eclipse.emf.ecore.EObject)3 Map (java.util.Map)2 IStatus (org.eclipse.core.runtime.IStatus)2 Status (org.eclipse.core.runtime.Status)2 EObjectFlatComboSettings (org.eclipse.emf.eef.runtime.ui.widgets.eobjflatcombo.EObjectFlatComboSettings)2 Viewer (org.eclipse.jface.viewers.Viewer)2 ViewerFilter (org.eclipse.jface.viewers.ViewerFilter)2 AbstractTable (org.obeonetwork.dsl.database.AbstractTable)2 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 List (java.util.List)1 EReference (org.eclipse.emf.ecore.EReference)1 EObjectPropertiesEditionContext (org.eclipse.emf.eef.runtime.context.impl.EObjectPropertiesEditionContext)1