]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/EntityRemover.java
Sync git svn branch with SVN repository r33389.
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / adapter / impl / EntityRemover.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.db.layer0.adapter.impl;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.Collection;\r
16 import java.util.List;\r
17 \r
18 import org.simantics.db.Resource;\r
19 import org.simantics.db.Statement;\r
20 import org.simantics.db.WriteGraph;\r
21 import org.simantics.db.exception.DatabaseException;\r
22 import org.simantics.db.layer0.adapter.Remover;\r
23 import org.simantics.db.layer0.exception.CannotRemoveException;\r
24 import org.simantics.db.layer0.util.Layer0Utils;\r
25 import org.simantics.layer0.Layer0;\r
26 \r
27 /**\r
28  * @author Tuukka Lehtonen\r
29  */\r
30 public class EntityRemover extends AbstractRemover {\r
31 \r
32     public EntityRemover(Resource resource) {\r
33         super(resource);\r
34     }\r
35 \r
36     @Override\r
37     public void remove(WriteGraph graph) throws DatabaseException {\r
38         remove(graph, resource, true);\r
39     }\r
40 \r
41     public static void remove(WriteGraph graph, Resource resource) throws DatabaseException {\r
42         remove(graph, resource, true);\r
43     }\r
44 \r
45     public static void remove(WriteGraph graph, Resource resource, boolean useAdapters) throws DatabaseException {\r
46 \r
47         // Safety check\r
48         if (graph.isImmutable(resource))\r
49             throw new CannotRemoveException("Cannot remove immutable resources!");\r
50         \r
51         if (Layer0Utils.isContainerPublished(graph, resource))\r
52                 throw new CannotRemoveException("Items in published libraries cannot be removed. Please create a new version to perform modifications.");\r
53 \r
54         Layer0 l0 = Layer0.getInstance(graph);\r
55         Collection<Statement> stms = graph.getStatements(resource, l0.IsWeaklyRelatedTo);\r
56 \r
57         List<Resource> composedOf = null;\r
58 \r
59         // Remove the resource to the best of our ability.\r
60         // NOTE: this doesn't work correctly for ordered sets, which must be removed using\r
61         // OrderedSetUtils.\r
62         for (Statement stm : stms) {\r
63 \r
64             Resource subject = stm.getSubject();\r
65 \r
66             if (resource.equals(stm.getSubject())) {\r
67                 Resource predicate = stm.getPredicate();\r
68                 Resource object = stm.getObject();\r
69 \r
70                 Resource inverse = graph.getPossibleInverse(predicate);\r
71                 graph.deny(subject, predicate, inverse, object);\r
72 \r
73                 // TODO: deny value/file related to subject too ??\r
74                 graph.denyValue(subject);\r
75 \r
76                 if (!resource.equals(object) && graph.isSubrelationOf(predicate, l0.IsComposedOf)) {\r
77                     // Only remove entities that are not explicitly\r
78                     // defined as a part of something else when dealing with\r
79                     // L0.HasProperty.\r
80                     // TODO: This logic is a temporary workaround for ontology\r
81                     // problems. It will change in the future when we change\r
82                     // HasProperty to inherit DependsOn instead of IsComposedOf.\r
83                     if (graph.isSubrelationOf(predicate, l0.HasProperty)) {\r
84                         Resource partOf = graph.getPossibleObject(object, l0.PartOf);\r
85                         if (partOf != null && !partOf.equals(resource))\r
86                             continue;\r
87                     }\r
88 \r
89                     if (composedOf == null)\r
90                         composedOf = new ArrayList<Resource>(stms.size());\r
91                     composedOf.add(object);\r
92                 }\r
93             }\r
94         }\r
95 \r
96         if (composedOf == null)\r
97             return;\r
98 \r
99         // Recursively remove the objects this resource is composed of.\r
100         for (Resource object : composedOf) {\r
101             Remover remover = null;\r
102             if (useAdapters)\r
103                 remover = graph.getPossibleAdapter(object, Remover.class);\r
104 \r
105             if (remover != null)\r
106                 remover.remove(graph);\r
107             else\r
108                 remove(graph, object, useAdapters);\r
109         }\r
110     }\r
111 \r
112 }\r