/******************************************************************************* * 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); } } }