]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/ReferenceElementRemover.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / adapters / ReferenceElementRemover.java
diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/ReferenceElementRemover.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/ReferenceElementRemover.java
new file mode 100644 (file)
index 0000000..513e368
--- /dev/null
@@ -0,0 +1,50 @@
+/*******************************************************************************\r
+ * Copyright (c) 2013 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     Semantum Oy - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.modeling.adapters;\r
+\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.modeling.ModelingResources;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class ReferenceElementRemover extends ElementRemover {\r
+\r
+    public ReferenceElementRemover(Resource referenceElement) {\r
+        super(referenceElement);\r
+    }\r
+\r
+    @Override\r
+    public void remove(WriteGraph graph) throws DatabaseException {\r
+        ModelingResources MOD = ModelingResources.getInstance(graph);\r
+\r
+        Resource referencedComponent = graph.getPossibleObject(resource, MOD.HasParentComponent);\r
+        Resource referenceRelation = graph.getPossibleObject(resource, MOD.HasReferenceRelation);\r
+        //System.out.println("references component : " + NameUtils.getSafeName(graph, referencedComponent));\r
+        //System.out.println("reference relation: " + NameUtils.getSafeName(graph, referenceRelation));\r
+\r
+        if (referencedComponent != null && referenceRelation != null) {\r
+            // Make sure that all referenceRelations from parentComponent are\r
+            // removed since removing this reference element will also remove\r
+            // the referenceRelation which would break the statements into\r
+            // unusable form if the relation was still used anywhere.\r
+            graph.deny(referencedComponent, referenceRelation);\r
+        }\r
+\r
+        // Only after successfully denying all uses of referenceRelation\r
+        // can we delete the reference element itself.\r
+        super.remove(graph);\r
+    }\r
+\r
+}\r