]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/RelatedObjectRemover.java
Fixed index query regression in L0.Entity instance queries
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / adapter / impl / RelatedObjectRemover.java
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management in
3  * Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.layer0.adapter.impl;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16
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;
23
24 /**
25  * A simple {@link Remover} implementation for removing a resource and its specified related objects using the {@link EntityRemover} logic.
26  * 
27  * @author Tuukka Lehtonen
28  */
29 public class RelatedObjectRemover extends AbstractRemover {
30
31     Resource[] relations;
32
33     public RelatedObjectRemover(ReadGraph graph, Resource component, String relationURI) throws DatabaseException {
34         super(component);
35         relations = new Resource[] { graph.getResource(relationURI) };
36     }
37
38     public RelatedObjectRemover(ReadGraph graph, Resource component, String relationURI, String relationURI2) throws DatabaseException {
39         this(graph, component, new String[] { relationURI, relationURI2 });
40     }
41
42     public RelatedObjectRemover(ReadGraph graph, Resource component, String relationURI, String relationURI2, String relationURI3) throws DatabaseException {
43         this(graph, component, new String[] { relationURI, relationURI2, relationURI3 });
44     }
45
46     public RelatedObjectRemover(ReadGraph graph, Resource component, String... relationURIs) throws DatabaseException {
47         super(component);
48         if (relationURIs.length == 0) {
49             relations = Resource.NONE;
50         } else {
51             relations = new Resource[relationURIs.length];
52             for (int i = 0; i < relationURIs.length; ++i)
53                 relations[i] = graph.getResource(relationURIs[i]);
54         }
55     }
56
57     @Override
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))
63                     continue;
64                 objects.add(object);
65             }
66         }
67
68         // Remove self first to prevent later actions for returning to it.
69         EntityRemover.remove(graph, resource);
70
71         // Finally, remove related matter.
72         for (Resource object : objects)
73             RemoverUtil.remove(graph, object);
74     }
75
76 }