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