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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.db.layer0.adapter.impl;
\r
14 import java.util.ArrayList;
\r
15 import java.util.Collection;
\r
16 import java.util.List;
\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
28 * @author Tuukka Lehtonen
\r
30 public class EntityRemover extends AbstractRemover {
\r
32 public EntityRemover(Resource resource) {
\r
37 public void remove(WriteGraph graph) throws DatabaseException {
\r
38 remove(graph, resource, true);
\r
41 public static void remove(WriteGraph graph, Resource resource) throws DatabaseException {
\r
42 remove(graph, resource, true);
\r
45 public static void remove(WriteGraph graph, Resource resource, boolean useAdapters) throws DatabaseException {
\r
48 if (graph.isImmutable(resource))
\r
49 throw new CannotRemoveException("Cannot remove immutable resources!");
\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
54 Layer0 l0 = Layer0.getInstance(graph);
\r
55 Collection<Statement> stms = graph.getStatements(resource, l0.IsWeaklyRelatedTo);
\r
57 List<Resource> composedOf = null;
\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
62 for (Statement stm : stms) {
\r
64 Resource subject = stm.getSubject();
\r
66 if (resource.equals(stm.getSubject())) {
\r
67 Resource predicate = stm.getPredicate();
\r
68 Resource object = stm.getObject();
\r
70 Resource inverse = graph.getPossibleInverse(predicate);
\r
71 graph.deny(subject, predicate, inverse, object);
\r
73 // TODO: deny value/file related to subject too ??
\r
74 graph.denyValue(subject);
\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
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
89 if (composedOf == null)
\r
90 composedOf = new ArrayList<Resource>(stms.size());
\r
91 composedOf.add(object);
\r
96 if (composedOf == null)
\r
99 // Recursively remove the objects this resource is composed of.
\r
100 for (Resource object : composedOf) {
\r
101 Remover remover = null;
\r
103 remover = graph.getPossibleAdapter(object, Remover.class);
\r
105 if (remover != null)
\r
106 remover.remove(graph);
\r
108 remove(graph, object, useAdapters);
\r