blob: 8a62e1aa93e13880c86d252b0ba7e7ea7c71b27f [file] [log] [blame]
/**
*
* Copyright (c) 2011, 2016 - Lunifera GmbH (Gross Enzersdorf), Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Florian Pirchner (Lunifera GmbH) - initial implementation
*/
package org.eclipse.osbp.tools.graphical.entity.lib;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.edit.command.AddCommand;
import org.eclipse.emf.transaction.NotificationFilter;
import org.eclipse.emf.transaction.ResourceSetChangeEvent;
import org.eclipse.emf.transaction.ResourceSetListener;
import org.eclipse.emf.transaction.RollbackException;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.osbp.dsl.semantic.common.types.LScalarType;
import org.eclipse.osbp.dsl.semantic.common.types.LType;
import org.eclipse.osbp.dsl.semantic.common.types.LTypedPackage;
import org.eclipse.osbp.dsl.semantic.common.types.OSBPTypesPackage;
import org.eclipse.osbp.dsl.semantic.entity.LEntity;
import org.eclipse.osbp.dsl.semantic.entity.LEntityModel;
import org.eclipse.osbp.dsl.semantic.entity.OSBPEntityPackage;
import org.eclipse.osbp.xtext.oxtype.oxtype.OXImportDeclaration;
import org.eclipse.osbp.xtext.oxtype.oxtype.OXtypeFactory;
import org.eclipse.xtext.xtype.XImportDeclaration;
import org.eclipse.xtext.xtype.XImportSection;
import org.eclipse.xtext.xtype.XtypePackage;
public class TransactionListener implements ResourceSetListener {
@Override
public NotificationFilter getFilter() {
return null;
}
@Override
public Command transactionAboutToCommit(ResourceSetChangeEvent event) throws RollbackException {
TransactionalEditingDomain editingDomain = event.getEditingDomain();
for (Notification notification : event.getNotifications()) {
if (notification.getEventType() == Notification.SET && notification.getNotifier() instanceof EObject) {
EObject eObject = (EObject) notification.getNotifier();
EClass eClass = eObject.eClass();
if (OSBPEntityPackage.eNS_URI.equals(eClass.getEPackage().getNsURI())) {
// handle imports
if (notification.getFeature() == OSBPTypesPackage.Literals.LATTRIBUTE__TYPE
|| notification.getFeature() == OSBPEntityPackage.Literals.LENTITY_REFERENCE__TYPE
|| notification.getFeature() == OSBPEntityPackage.Literals.LBEAN_REFERENCE__TYPE) {
if (notification.getNewValue() != null) {
if (notification.getNewValue() instanceof LScalarType) {
LScalarType type = (LScalarType) notification.getNewValue();
if (!containsImport(eObject, type)) {
LEntityModel entityModel = findEntityModel(eObject);
OXImportDeclaration importDecl = OXtypeFactory.eINSTANCE
.createOXImportDeclaration();
importDecl.setFqnImport(true);
importDecl.setImportedFullyQualifiedName(toTypeName(type));
AddCommand addCommand = (AddCommand) AddCommand.create(editingDomain,
entityModel.getImportSection(),
XtypePackage.Literals.XIMPORT_SECTION__IMPORT_DECLARATIONS, importDecl);
return addCommand;
}
} else if (notification.getNewValue() instanceof LEntity) {
LEntity type = (LEntity) notification.getNewValue();
if (!containsImport(eObject, type)) {
LEntityModel entityModel = findEntityModel(eObject);
OXImportDeclaration importDecl = OXtypeFactory.eINSTANCE
.createOXImportDeclaration();
importDecl.setFqnImport(true);
importDecl.setImportedFullyQualifiedName(toTypeName(type));
AddCommand addCommand = (AddCommand) AddCommand.create(editingDomain,
entityModel.getImportSection(),
XtypePackage.Literals.XIMPORT_SECTION__IMPORT_DECLARATIONS, importDecl);
return addCommand;
}
}
}
}
}
}
}
return null;
}
private boolean containsImport(EObject element, LType type) {
String typeName = toTypeName(type);
LEntityModel model = findEntityModel(element);
if (model == null) {
return false;
}
XImportSection importSection = model.getImportSection();
if (importSection == null) {
return false;
}
for (XImportDeclaration desc : importSection.getImportDeclarations()) {
OXImportDeclaration casted = (OXImportDeclaration) desc;
if (casted.isFqnImport() && casted.getImportedFullyQualifiedName().equals(typeName)) {
return true;
}
}
return false;
}
protected LTypedPackage findPackage(EObject eObject) {
if (eObject == null) {
return null;
}
if (eObject instanceof LTypedPackage) {
return (LTypedPackage) eObject;
} else {
return findPackage(eObject.eContainer());
}
}
protected LEntityModel findEntityModel(EObject eObject) {
if (eObject == null) {
return null;
}
if (eObject instanceof LEntityModel) {
return (LEntityModel) eObject;
} else {
return findEntityModel(eObject.eContainer());
}
}
protected String toTypeName(LType type) {
LTypedPackage pkg = findPackage(type);
return pkg.getName() + "." + type.getName();
}
@Override
public void resourceSetChanged(ResourceSetChangeEvent event) {
}
@Override
public boolean isAggregatePrecommitListener() {
return false;
}
@Override
public boolean isPrecommitOnly() {
return true;
}
@Override
public boolean isPostcommitOnly() {
return false;
}
}