]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/EntitySubgraphExtent.java
Prevent paste to resources that are `L0.Entity_published`
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / adapter / impl / EntitySubgraphExtent.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in 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.Collections;
16 import java.util.Set;
17 import java.util.concurrent.atomic.AtomicInteger;
18
19 import org.simantics.db.AsyncReadGraph;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.Statement;
23 import org.simantics.db.common.primitiverequest.Superrelations;
24 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncMultiListener;
25 import org.simantics.db.common.request.ReadRequest;
26 import org.simantics.db.common.request.ResourceAsyncMultiRead;
27 import org.simantics.db.common.utils.NameUtils;
28 import org.simantics.db.exception.DatabaseException;
29 import org.simantics.db.procedure.AsyncMultiProcedure;
30 import org.simantics.db.procedure.AsyncProcedure;
31 import org.simantics.layer0.Layer0;
32
33 public class EntitySubgraphExtent extends TypeSubgraphExtent {
34
35         public static boolean DEBUG = false;
36
37         static class DefinedRelationSet extends ResourceAsyncMultiRead<Resource> {
38
39                 public DefinedRelationSet(Resource type) {
40                         super(type);
41                 }
42
43                 public void forType(AsyncReadGraph graph, Resource type, final AsyncMultiProcedure<Resource> procedure, final AtomicInteger ready) {
44
45                         final Layer0 l0 = graph.getService(Layer0.class);
46
47                         ready.incrementAndGet();
48
49                         graph.forEachObject(type, l0.HasPropertyDefinition, new AsyncMultiProcedure<Resource>() {
50
51                                 @Override
52                                 public void execute(AsyncReadGraph graph, Resource def) {
53
54                                         ready.incrementAndGet();
55
56                                         graph.forPossibleObject(def, l0.ConcernsRelation, new AsyncProcedure<Resource>() {
57
58                                                 @Override
59                                                 public void execute(AsyncReadGraph graph, Resource relation) {
60                                                         if(relation != null) procedure.execute(graph, relation);
61                                                         if(ready.decrementAndGet() == 0) procedure.finished(graph);
62                                                 }
63
64                                                 @Override
65                                                 public void exception(AsyncReadGraph graph, Throwable throwable) {
66                                                         throwable.printStackTrace();
67                                                 }
68
69                                         });
70
71                                 }
72
73                                 @Override
74                                 public void finished(AsyncReadGraph graph) {
75
76                                         if(ready.decrementAndGet() == 0) procedure.finished(graph);
77
78                                 }
79
80                                 @Override
81                                 public void exception(AsyncReadGraph graph, Throwable throwable) {
82                                         throwable.printStackTrace();
83                                 }
84
85                         });
86
87                 }
88
89                 @Override
90                 public void perform(AsyncReadGraph graph, final AsyncMultiProcedure<Resource> procedure) {
91
92                         final AtomicInteger ready = new AtomicInteger(1);
93
94                         forType(graph, resource, procedure, ready);
95
96                         graph.forSupertypes(resource, new AsyncProcedure<Set<Resource>>() {
97
98                                 @Override
99                                 public void execute(AsyncReadGraph graph, Set<Resource> types) {
100
101                                         for(Resource type : types) forType(graph, type, procedure, ready);
102
103                                         if(ready.decrementAndGet() == 0) procedure.finished(graph);
104
105                                 }
106
107                                 @Override
108                                 public void exception(AsyncReadGraph graph, Throwable throwable) {
109                                         throwable.printStackTrace();
110                                 }
111
112                         });
113
114                 }
115
116         }
117
118         @Override
119         public void accept(AsyncReadGraph graph, final Resource resource, final AsyncProcedure<Classifier> procedure, final Callback callback) {
120
121                 final AtomicInteger ready = new AtomicInteger(1);
122
123                 class Result extends TransientCacheAsyncMultiListener<Resource> implements Classifier {
124
125                         final ArrayList<Resource> predicates = new ArrayList<Resource>();
126
127                         @Override
128                         public void exception(AsyncReadGraph graph, Throwable throwable) {
129                                 throwable.printStackTrace();
130                         }
131
132                         @Override
133                         public void execute(AsyncReadGraph graph, Resource predicate) {
134                                 synchronized(predicates) {
135                                         predicates.add(predicate);
136                                 }
137                         }
138
139                         @Override
140                         public void finished(AsyncReadGraph graph) {
141                                 if(ready.decrementAndGet() == 0) procedure.execute(graph, this);
142                         }
143
144                         @Override
145                         public void classify(AsyncReadGraph graph, final Statement statement, ExtentStatus objectExtent, final Callback callback) {
146
147                                 if(DEBUG) {
148                                         graph.asyncRequest(new ReadRequest() {
149
150                                                 @Override
151                                                 public void run(ReadGraph graph) throws DatabaseException {
152                                                         System.out.println("classify " + NameUtils.toString(graph, statement));
153                                                 }
154
155                                         });
156                                 }
157
158                                 // TODO haxx
159                                 final Resource p = statement.getPredicate();
160                                 if(graph.getService(Layer0.class).PartOf.equals(p) && !ExtentStatus.INTERNAL.equals(objectExtent)) return; 
161                                 // TODO haxx2
162                                 if(ExtentStatus.EXCLUDED.equals(objectExtent)) return;
163
164                                 if(predicates.contains(p)) {
165                                         callback.statement(statement, true);
166
167                                         if(DEBUG) {
168                                                 graph.asyncRequest(new ReadRequest() {
169
170                                                         @Override
171                                                         public void run(ReadGraph graph) throws DatabaseException {
172                                                                 System.out.println("-accept " + NameUtils.toString(graph, statement));
173                                                         }
174
175                                                 });
176                                         }
177
178                                         return;
179                                 }
180
181                                 graph.forDirectSuperrelations(p, new AsyncMultiProcedure<Resource>() {
182
183                                         @Override
184                                         public void exception(AsyncReadGraph graph, Throwable throwable) {
185                                                 throwable.printStackTrace();
186                                         }
187
188                                         @Override
189                                         public void execute(AsyncReadGraph graph, final Resource superRelation) {
190
191
192                                                 if(predicates.contains(superRelation)) {
193                                                         callback.statement(statement, true);
194
195                                                         if(DEBUG) {
196                                                                 graph.asyncRequest(new ReadRequest() {
197
198                                                                         @Override
199                                                                         public void run(ReadGraph graph) throws DatabaseException {
200                                                                                 System.out.println("-accept " + NameUtils.toString(graph, statement));
201                                                                         }
202
203                                                                 });
204                                                         }
205
206                                                         return;
207                                                 }
208
209                                                 graph.asyncRequest(new Superrelations(superRelation), new AsyncProcedure<Set<Resource>>() {
210
211                                                         @Override
212                                                         public void exception(AsyncReadGraph graph, Throwable t) {
213                                                                 t.printStackTrace();
214                                                         }
215
216                                                         @Override
217                                                         public void execute(AsyncReadGraph graph, Set<Resource> supers) {
218
219                                                                 if(!Collections.disjoint(supers, predicates)) {
220
221                                                                         if(DEBUG) {
222                                                                                 graph.asyncRequest(new ReadRequest() {
223
224                                                                                         @Override
225                                                                                         public void run(ReadGraph graph) throws DatabaseException {
226                                                                                                 System.out.println("-accept " + NameUtils.toString(graph, statement));
227                                                                                         }
228
229                                                                                 });
230                                                                         }
231
232                                                                         callback.statement(statement, true);
233
234                                                                 }
235
236                                                         }
237
238                                                 });
239
240                                         }
241
242                                         @Override
243                                         public void finished(AsyncReadGraph graph) {
244                                         }
245
246                                 });
247
248                         }
249
250                 };
251
252                 final Result result = new Result();
253
254                 graph.forEachPrincipalType(resource, new AsyncMultiProcedure<Resource>() {
255
256                         @Override
257                         public void execute(AsyncReadGraph graph, Resource type) {
258                                 ready.incrementAndGet();
259                                 graph.asyncRequest(new DefinedRelationSet(type), result);
260                         }
261
262                         @Override
263                         public void finished(AsyncReadGraph graph) {
264                                 if(ready.decrementAndGet() == 0) procedure.execute(graph, result);
265                         }
266
267                         @Override
268                         public void exception(AsyncReadGraph graph, Throwable throwable) {
269                                 throwable.printStackTrace();
270                         }
271
272                 });
273
274         }
275
276 }