X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.db.layer0%2Fsrc%2Forg%2Fsimantics%2Fdb%2Flayer0%2Fadapter%2Fimpl%2FEntityRemover.java;fp=bundles%2Forg.simantics.db.layer0%2Fsrc%2Forg%2Fsimantics%2Fdb%2Flayer0%2Fadapter%2Fimpl%2FEntityRemover.java;h=8dc9f6df00343f0cd8c768b55f880a29ce105cfc;hp=ce0b44af74dbd9f2a248b99ba34358f932afbdb5;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hpb=24e2b34260f219f0d1644ca7a138894980e25b14 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 index ce0b44af7..8dc9f6df0 100644 --- 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 @@ -1,112 +1,112 @@ -/******************************************************************************* - * Copyright (c) 2007, 2010 Association for Decentralized Information Management - * in Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.db.layer0.adapter.impl; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -import org.simantics.db.Resource; -import org.simantics.db.Statement; -import org.simantics.db.WriteGraph; -import org.simantics.db.exception.DatabaseException; -import org.simantics.db.layer0.adapter.Remover; -import org.simantics.db.layer0.exception.CannotRemoveException; -import org.simantics.db.layer0.util.Layer0Utils; -import org.simantics.layer0.Layer0; - -/** - * @author Tuukka Lehtonen - */ -public class EntityRemover extends AbstractRemover { - - public EntityRemover(Resource resource) { - super(resource); - } - - @Override - public void remove(WriteGraph graph) throws DatabaseException { - remove(graph, resource, true); - } - - public static void remove(WriteGraph graph, Resource resource) throws DatabaseException { - remove(graph, resource, true); - } - - public static void remove(WriteGraph graph, Resource resource, boolean useAdapters) throws DatabaseException { - - // Safety check - if (graph.isImmutable(resource)) - throw new CannotRemoveException("Cannot remove immutable resources!"); - - if (Layer0Utils.isContainerPublished(graph, resource)) - throw new CannotRemoveException("Items in published libraries cannot be removed. Please create a new version to perform modifications."); - - Layer0 l0 = Layer0.getInstance(graph); - Collection stms = graph.getStatements(resource, l0.IsWeaklyRelatedTo); - - List composedOf = null; - - // Remove the resource to the best of our ability. - // NOTE: this doesn't work correctly for ordered sets, which must be removed using - // OrderedSetUtils. - for (Statement stm : stms) { - - Resource subject = stm.getSubject(); - - if (resource.equals(stm.getSubject())) { - Resource predicate = stm.getPredicate(); - Resource object = stm.getObject(); - - Resource inverse = graph.getPossibleInverse(predicate); - graph.deny(subject, predicate, inverse, object); - - // TODO: deny value/file related to subject too ?? - graph.denyValue(subject); - - if (!resource.equals(object) && graph.isSubrelationOf(predicate, l0.IsComposedOf)) { - // Only remove entities that are not explicitly - // defined as a part of something else when dealing with - // L0.HasProperty. - // TODO: This logic is a temporary workaround for ontology - // problems. It will change in the future when we change - // HasProperty to inherit DependsOn instead of IsComposedOf. - if (graph.isSubrelationOf(predicate, l0.HasProperty)) { - Resource partOf = graph.getPossibleObject(object, l0.PartOf); - if (partOf != null && !partOf.equals(resource)) - continue; - } - - if (composedOf == null) - composedOf = new ArrayList(stms.size()); - composedOf.add(object); - } - } - } - - if (composedOf == null) - return; - - // Recursively remove the objects this resource is composed of. - for (Resource object : composedOf) { - Remover remover = null; - if (useAdapters) - remover = graph.getPossibleAdapter(object, Remover.class); - - if (remover != null) - remover.remove(graph); - else - remove(graph, object, useAdapters); - } - } - -} +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.db.layer0.adapter.impl; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import org.simantics.db.Resource; +import org.simantics.db.Statement; +import org.simantics.db.WriteGraph; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.adapter.Remover; +import org.simantics.db.layer0.exception.CannotRemoveException; +import org.simantics.db.layer0.util.Layer0Utils; +import org.simantics.layer0.Layer0; + +/** + * @author Tuukka Lehtonen + */ +public class EntityRemover extends AbstractRemover { + + public EntityRemover(Resource resource) { + super(resource); + } + + @Override + public void remove(WriteGraph graph) throws DatabaseException { + remove(graph, resource, true); + } + + public static void remove(WriteGraph graph, Resource resource) throws DatabaseException { + remove(graph, resource, true); + } + + public static void remove(WriteGraph graph, Resource resource, boolean useAdapters) throws DatabaseException { + + // Safety check + if (graph.isImmutable(resource)) + throw new CannotRemoveException("Cannot remove immutable resources!"); + + if (Layer0Utils.isContainerPublished(graph, resource)) + throw new CannotRemoveException("Items in published libraries cannot be removed. Please create a new version to perform modifications."); + + Layer0 l0 = Layer0.getInstance(graph); + Collection stms = graph.getStatements(resource, l0.IsWeaklyRelatedTo); + + List composedOf = null; + + // Remove the resource to the best of our ability. + // NOTE: this doesn't work correctly for ordered sets, which must be removed using + // OrderedSetUtils. + for (Statement stm : stms) { + + Resource subject = stm.getSubject(); + + if (resource.equals(stm.getSubject())) { + Resource predicate = stm.getPredicate(); + Resource object = stm.getObject(); + + Resource inverse = graph.getPossibleInverse(predicate); + graph.deny(subject, predicate, inverse, object); + + // TODO: deny value/file related to subject too ?? + graph.denyValue(subject); + + if (!resource.equals(object) && graph.isSubrelationOf(predicate, l0.IsComposedOf)) { + // Only remove entities that are not explicitly + // defined as a part of something else when dealing with + // L0.HasProperty. + // TODO: This logic is a temporary workaround for ontology + // problems. It will change in the future when we change + // HasProperty to inherit DependsOn instead of IsComposedOf. + if (graph.isSubrelationOf(predicate, l0.HasProperty)) { + Resource partOf = graph.getPossibleObject(object, l0.PartOf); + if (partOf != null && !partOf.equals(resource)) + continue; + } + + if (composedOf == null) + composedOf = new ArrayList(stms.size()); + composedOf.add(object); + } + } + } + + if (composedOf == null) + return; + + // Recursively remove the objects this resource is composed of. + for (Resource object : composedOf) { + Remover remover = null; + if (useAdapters) + remover = graph.getPossibleAdapter(object, Remover.class); + + if (remover != null) + remover.remove(graph); + else + remove(graph, object, useAdapters); + } + } + +}