]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.server/src/org/simantics/document/server/request/NodesRequest2.java
Merge remote-tracking branch 'origin/master' into private/antti2
[simantics/platform.git] / bundles / org.simantics.document.server / src / org / simantics / document / server / request / NodesRequest2.java
1 package org.simantics.document.server.request;
2
3 import gnu.trove.set.hash.THashSet;
4
5 import java.util.Collection;
6 import java.util.Collections;
7 import java.util.Set;
8
9 import org.simantics.databoard.Bindings;
10 import org.simantics.db.AsyncReadGraph;
11 import org.simantics.db.ReadGraph;
12 import org.simantics.db.Resource;
13 import org.simantics.db.common.request.AsyncReadRequest;
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.db.layer0.request.VariableChildren;
16 import org.simantics.db.layer0.request.VariableRead;
17 import org.simantics.db.layer0.variable.Variable;
18 import org.simantics.db.procedure.AsyncProcedure;
19 import org.simantics.document.base.ontology.DocumentationResource;
20 import org.simantics.utils.threads.logger.ITask;
21 import org.simantics.utils.threads.logger.ThreadLogger;
22
23 public class NodesRequest2 extends VariableRead<Set<Variable>> {
24
25     public NodesRequest2(Variable var) {
26         super(var);
27     }
28
29     @Override
30     public Set<Variable> perform(ReadGraph graph) throws DatabaseException {
31
32         ITask task = DocumentRequest.PROFILE ? ThreadLogger.task(this) : null;
33
34         DocumentationResource DOC = DocumentationResource.getInstance(graph);
35
36         Resource type = variable.getPossibleType(graph);
37         if(type == null) {
38             if(DocumentRequest.PROFILE) task.finish();
39             return Collections.emptySet();
40         }
41
42         if(!graph.isInheritedFrom(type, DOC.Components_Component)) {
43             if(DocumentRequest.PROFILE) task.finish();
44             return Collections.emptySet();
45         }
46
47         Boolean pathExists = variable.getPossiblePropertyValue(graph, DOC.Properties_pathExists, Bindings.BOOLEAN);
48         if(pathExists != null && !pathExists) {
49             if(DocumentRequest.PROFILE) task.finish();
50             return Collections.emptySet();
51         }
52
53         if(graph.isInheritedFrom(type, DOC.Components_PrimitiveComponent)) {
54             if(DocumentRequest.PROFILE) task.finish();
55             return Collections.singleton(variable);
56         } else {
57             Set<Variable> result = new THashSet<Variable>();
58             Collection<Variable> children = graph.syncRequest(new VariableChildren(variable));
59             graph.syncRequest(new AsyncReadRequest() {
60
61                 @Override
62                 public void run(AsyncReadGraph graph) throws DatabaseException {
63
64                     for(Variable child : children) {
65                         graph.asyncRequest(new NodesRequest2(child), new AsyncProcedure<Set<Variable>>() {
66
67                             @Override
68                             public void execute(AsyncReadGraph graph, Set<Variable> vars) {
69                                 synchronized(result) {
70                                     result.addAll(vars);
71                                 }
72                             }
73
74                             @Override
75                             public void exception(AsyncReadGraph graph, Throwable throwable) {
76                             }
77                             
78                         });
79                     }
80
81                 }
82
83             });
84             
85             if(DocumentRequest.PROFILE) task.finish();
86             return result;
87             
88         }
89
90     }
91
92 }