]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/EntityRemover.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / adapter / impl / EntityRemover.java
diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/EntityRemover.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/EntityRemover.java
new file mode 100644 (file)
index 0000000..ce0b44a
--- /dev/null
@@ -0,0 +1,112 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in 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
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.db.layer0.adapter.impl;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.List;\r
+\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Statement;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.adapter.Remover;\r
+import org.simantics.db.layer0.exception.CannotRemoveException;\r
+import org.simantics.db.layer0.util.Layer0Utils;\r
+import org.simantics.layer0.Layer0;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class EntityRemover extends AbstractRemover {\r
+\r
+    public EntityRemover(Resource resource) {\r
+        super(resource);\r
+    }\r
+\r
+    @Override\r
+    public void remove(WriteGraph graph) throws DatabaseException {\r
+        remove(graph, resource, true);\r
+    }\r
+\r
+    public static void remove(WriteGraph graph, Resource resource) throws DatabaseException {\r
+        remove(graph, resource, true);\r
+    }\r
+\r
+    public static void remove(WriteGraph graph, Resource resource, boolean useAdapters) throws DatabaseException {\r
+\r
+        // Safety check\r
+        if (graph.isImmutable(resource))\r
+            throw new CannotRemoveException("Cannot remove immutable resources!");\r
+        \r
+        if (Layer0Utils.isContainerPublished(graph, resource))\r
+               throw new CannotRemoveException("Items in published libraries cannot be removed. Please create a new version to perform modifications.");\r
+\r
+        Layer0 l0 = Layer0.getInstance(graph);\r
+        Collection<Statement> stms = graph.getStatements(resource, l0.IsWeaklyRelatedTo);\r
+\r
+        List<Resource> composedOf = null;\r
+\r
+        // Remove the resource to the best of our ability.\r
+        // NOTE: this doesn't work correctly for ordered sets, which must be removed using\r
+        // OrderedSetUtils.\r
+        for (Statement stm : stms) {\r
+\r
+            Resource subject = stm.getSubject();\r
+\r
+            if (resource.equals(stm.getSubject())) {\r
+                Resource predicate = stm.getPredicate();\r
+                Resource object = stm.getObject();\r
+\r
+                Resource inverse = graph.getPossibleInverse(predicate);\r
+                graph.deny(subject, predicate, inverse, object);\r
+\r
+                // TODO: deny value/file related to subject too ??\r
+                graph.denyValue(subject);\r
+\r
+                if (!resource.equals(object) && graph.isSubrelationOf(predicate, l0.IsComposedOf)) {\r
+                    // Only remove entities that are not explicitly\r
+                    // defined as a part of something else when dealing with\r
+                    // L0.HasProperty.\r
+                    // TODO: This logic is a temporary workaround for ontology\r
+                    // problems. It will change in the future when we change\r
+                    // HasProperty to inherit DependsOn instead of IsComposedOf.\r
+                    if (graph.isSubrelationOf(predicate, l0.HasProperty)) {\r
+                        Resource partOf = graph.getPossibleObject(object, l0.PartOf);\r
+                        if (partOf != null && !partOf.equals(resource))\r
+                            continue;\r
+                    }\r
+\r
+                    if (composedOf == null)\r
+                        composedOf = new ArrayList<Resource>(stms.size());\r
+                    composedOf.add(object);\r
+                }\r
+            }\r
+        }\r
+\r
+        if (composedOf == null)\r
+            return;\r
+\r
+        // Recursively remove the objects this resource is composed of.\r
+        for (Resource object : composedOf) {\r
+            Remover remover = null;\r
+            if (useAdapters)\r
+                remover = graph.getPossibleAdapter(object, Remover.class);\r
+\r
+            if (remover != null)\r
+                remover.remove(graph);\r
+            else\r
+                remove(graph, object, useAdapters);\r
+        }\r
+    }\r
+\r
+}\r