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