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