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