Remove dangling references [message #1766870] |
Wed, 28 June 2017 07:57  |
Eclipse User |
|
|
|
Hello,
I have a bidirectional link between 2 classes.
The cardinality is (m,n).
Ex:
- root
- node A
- node B (contains a link to C)
- node C (levels: contains a link to B)
if I remove Node A or B, the Node C contains a dangling reference.
It prevents the user to save the document (but the modification star disapears, which is then dangerous).
If I read this document (https://eclipsesource.com/blogs/2015/05/26/emf-dos-and-donts-11/) I thought the bad link should be removed as well.
I read on other forum that I can easily find the dangling references and remove them by myself with EcoreUtil.UnresolvedProxyCrossReferencer.
I tried to find the dangling references but nothing with that code below.
For the argument of the find method, I tested on the root, on the node C, and even on the levels nodes.
Map<EObject,Collection<EStructuralFeature.Setting>> unresolvedProxies=EcoreUtil.UnresolvedProxyCrossReferencer.find(node);
if (unresolvedProxies.size() > 0) {
Please help.
Jim
|
|
|
|
|
|
|
|
Re: Remove dangling references [message #1766993 is a reply to message #1766962] |
Thu, 29 June 2017 07:18  |
Eclipse User |
|
|
|
@Andreas: I cannot use a derived attribute, because it can be calculated (by using links) or entered manually.
@ed: it works perfectly Thx.
Here is the code for all:
private void checkDanglingReferences(Object object){
if (!(object instanceof PriceParent))
return;
// Go to the root node
Quote quote = ((PriceParentImpl)object).getQuote();
ResourceSet resourceSet = quote.eResource().getResourceSet();
DanglingCrossReferencer danglingReferencer = new DanglingCrossReferencer(resourceSet);
Map<EObject,Collection<EStructuralFeature.Setting>> danglingReferences=danglingReferencer.findDanglingCrossReferences();
if (danglingReferences.size() > 0) {
for (Map.Entry<EObject, Collection<EStructuralFeature.Setting>> danglingReferenceEntry : danglingReferences
.entrySet()) {
BasicEObjectImpl danglingReference = (BasicEObjectImpl) danglingReferenceEntry.getKey();
for (EStructuralFeature.Setting setting : danglingReferenceEntry.getValue()) {
EObject objectWithDanglingRefs = setting.getEObject();
// Engineering Item
if (isReferenceToCheck(objectWithDanglingRefs, setting.getEStructuralFeature())) {
EditingDomain domain = AdapterFactoryEditingDomain.getEditingDomainFor(objectWithDanglingRefs);
Command addThePart = RemoveCommand.create(domain, objectWithDanglingRefs,
setting.getEStructuralFeature(), Collections.singleton(danglingReference));
domain.getCommandStack().execute(addThePart);
}
}
}
}
}
}
|
|
|
Powered by
FUDForum. Page generated in 0.04314 seconds