1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management in
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;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.WriteGraph;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.db.layer0.adapter.Remover;
22 import org.simantics.db.layer0.util.RemoverUtil;
25 * A simple {@link Remover} implementation for removing a resource and its specified related objects using the {@link EntityRemover} logic.
27 * @author Tuukka Lehtonen
29 public class RelatedObjectRemover extends AbstractRemover {
33 public RelatedObjectRemover(ReadGraph graph, Resource component, String relationURI) throws DatabaseException {
35 relations = new Resource[] { graph.getResource(relationURI) };
38 public RelatedObjectRemover(ReadGraph graph, Resource component, String relationURI, String relationURI2) throws DatabaseException {
39 this(graph, component, new String[] { relationURI, relationURI2 });
42 public RelatedObjectRemover(ReadGraph graph, Resource component, String relationURI, String relationURI2, String relationURI3) throws DatabaseException {
43 this(graph, component, new String[] { relationURI, relationURI2, relationURI3 });
46 public RelatedObjectRemover(ReadGraph graph, Resource component, String... relationURIs) throws DatabaseException {
48 if (relationURIs.length == 0) {
49 relations = Resource.NONE;
51 relations = new Resource[relationURIs.length];
52 for (int i = 0; i < relationURIs.length; ++i)
53 relations[i] = graph.getResource(relationURIs[i]);
58 public void remove(WriteGraph graph) throws DatabaseException {
59 Collection<Resource> objects = new ArrayList<Resource>();
60 for (Resource relation : relations) {
61 for (Resource object : graph.getObjects(resource, relation)) {
62 if (object.equals(resource))
68 // Remove self first to prevent later actions for returning to it.
69 EntityRemover.remove(graph, resource);
71 // Finally, remove related matter.
72 for (Resource object : objects)
73 RemoverUtil.remove(graph, object);