From 7461e507cc76fa26456ba6f8177f11e7ea0c7941 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jani=20M=C3=A4kinen?= Date: Mon, 24 Aug 2020 17:03:57 +0300 Subject: [PATCH] Update module graph structure, deny deleting modules in use under model gitlab #52 Change-Id: I16df1a66dddb0a7fc039d739b73a3c5616066e48 --- .../graph/Sysdyn.pgraph | 4 +- bundles/org.simantics.sysdyn.ui/adapters.xml | 2 +- .../newActions/NewModuleTypeAction.java | 1 - .../actions/remove/ModuleSymbolRemover.java | 55 ++++++++++++------- 4 files changed, 39 insertions(+), 23 deletions(-) diff --git a/bundles/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph b/bundles/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph index 8037fece..8ed1e078 100644 --- a/bundles/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph +++ b/bundles/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph @@ -215,7 +215,9 @@ SYSDYN.Module -- SYSDYN.Module.redeclaration --> SYSDYN.Redeclaration -- SYSDYN.Module.parameterOverride --> SYSDYN.Module.ParameterOverride - diff --git a/bundles/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/actions/newActions/NewModuleTypeAction.java b/bundles/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/actions/newActions/NewModuleTypeAction.java index ac524380..20cae87d 100644 --- a/bundles/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/actions/newActions/NewModuleTypeAction.java +++ b/bundles/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/actions/newActions/NewModuleTypeAction.java @@ -94,7 +94,6 @@ public class NewModuleTypeAction implements ActionFactory{ g.claimLiteral(moduleSymbol, l0.HasName, name + " Symbol"); g.claimLiteral(moduleSymbol, l0.HasLabel, name + " Symbol"); g.claim(moduleSymbol, l0.Inherits, sr.ModuleSymbol); - g.claim(moduleSymbol, l0.InstanceOf, sr.ModuleSymbol); g.claim(moduleSymbol, mr.SymbolToComponentType, moduleType); g.claim(moduleSymbol, l0.PartOf, moduleType); diff --git a/bundles/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/actions/remove/ModuleSymbolRemover.java b/bundles/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/actions/remove/ModuleSymbolRemover.java index 1b5becef..4f718fe5 100644 --- a/bundles/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/actions/remove/ModuleSymbolRemover.java +++ b/bundles/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/actions/remove/ModuleSymbolRemover.java @@ -11,44 +11,59 @@ *******************************************************************************/ package org.simantics.sysdyn.ui.browser.actions.remove; +import java.util.Collection; +import java.util.Map; + +import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.PossibleIndexRoot; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.indexing.IndexUtils; import org.simantics.db.layer0.adapter.impl.AbstractRemover; import org.simantics.db.layer0.adapter.impl.EntityRemover; import org.simantics.layer0.Layer0; import org.simantics.sysdyn.SysdynResource; /** - * + * * @author Miro Eklund * */ public class ModuleSymbolRemover extends AbstractRemover { public ModuleSymbolRemover(Resource resource) { - super(resource); + super(resource); + } + + @Override + public String canRemove(ReadGraph graph, Map aux) throws DatabaseException { + Resource model = graph.syncRequest(new PossibleIndexRoot(resource)); + if(model == null) return null; + Collection symbolsInUse = IndexUtils.findByType(graph, model, resource); + if(symbolsInUse.isEmpty()) return null; + return "Module you are attemping to delete is use in the model."; } - + @Override public void remove(WriteGraph g) throws DatabaseException { - g.markUndoPoint(); - - Layer0 L0 = Layer0.getInstance(g); - SysdynResource sr = SysdynResource.getInstance(g); - - //We need to remove the Module object, not just the symbol. - //Thus, if the resource is the symbol, find the parent Module first - if(g.isInstanceOf(resource, sr.ModuleSymbol)) { - Resource module = g.getPossibleObject(resource, L0.PartOf); - if(module != null) { - EntityRemover.remove(g, module, false); //Remove Module instead - was of type ModuleSymbol - } else { - EntityRemover.remove(g, resource, false); - } - } else { - EntityRemover.remove(g, resource, false); //Remove resource - not of type ModuleSymbol - } + g.markUndoPoint(); + + Layer0 L0 = Layer0.getInstance(g); + SysdynResource sr = SysdynResource.getInstance(g); + + //We need to remove the Module object, not just the symbol. + //Thus, if the resource is the symbol, find the parent Module first + if(g.isInstanceOf(resource, sr.ModuleSymbolType)) { + Resource module = g.getPossibleObject(resource, L0.PartOf); + if(module != null) { + EntityRemover.remove(g, module, false); //Remove Module instead - was of type ModuleSymbol + } else { + EntityRemover.remove(g, resource, false); + } + } else { + EntityRemover.remove(g, resource, false); //Remove resource - not of type ModuleSymbol + } } @Override -- 2.47.1