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));
}
}
}
}
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);
}
}
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);
}
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()));
}
}
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);
}
Aggregations