d910f13ff946a77e40d9b2dce830c2020c678f40
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / util / ConsistsOfProcess.java
1 package org.simantics.db.layer0.util;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.List;
6 import java.util.Set;
7
8 import org.simantics.db.AsyncReadGraph;
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.ResourceMap;
12 import org.simantics.db.common.request.ReadRequest;
13 import org.simantics.db.common.utils.Logger;
14 import org.simantics.db.exception.DatabaseException;
15 import org.simantics.db.layer0.adapter.SubgraphExtent.ExtentStatus;
16 import org.simantics.db.procedure.AsyncContextMultiProcedure;
17 import org.simantics.db.procedure.Procedure;
18 import org.simantics.db.service.DirectQuerySupport;
19 import org.simantics.layer0.Layer0;
20
21 class ConsistsOfProcess {
22
23         final List<InternalEntry> result;
24         final AsyncContextMultiProcedure<InternalEntry, Resource> structure;
25         final AsyncContextMultiProcedure<InternalEntry, Resource> names;
26
27     public static List<InternalEntry> walk(ReadGraph graph, ResourceMap<ExtentStatus> status, Collection<Resource> resources, Set<Resource> exclusions, boolean ignoreVirtual) throws DatabaseException {
28         ConsistsOfProcess process = new ConsistsOfProcess(graph, status, resources, exclusions, ignoreVirtual);
29         return process.result;
30     }
31     
32     static class InternalEntry {
33         public InternalEntry parent;
34         public Resource resource;
35         public String name;
36         InternalEntry(InternalEntry parent, Resource resource, String name) {
37                 this.parent = parent;
38                 this.resource = resource;
39                 this.name = name;
40         }
41     }
42         
43     private ConsistsOfProcess(ReadGraph graph, ResourceMap<ExtentStatus> status, final Collection<Resource> resources, final Set<Resource> exclusions, final boolean ignoreVirtual) throws DatabaseException {
44
45                 final Layer0 L0 = Layer0.getInstance(graph);
46                 final DirectQuerySupport dqs = graph.getService(DirectQuerySupport.class);
47                 
48                 result = new ArrayList<InternalEntry>();
49                 
50                 names = dqs.compileForEachObject(graph, L0.HasName, new AsyncContextMultiProcedure<InternalEntry, Resource>() {
51
52                         @Override
53                         public void execute(AsyncReadGraph graph, InternalEntry entry, Resource nameResource) {
54                 
55                                 if(status != null)
56                                         status.put(nameResource, ExtentStatus.EXCLUDED);
57                                 
58                                 graph.forPossibleValue(nameResource, new Procedure<String>() {
59
60                                         @Override
61                                         public void execute(String result) {
62                                                 entry.name = result;
63                                         }
64
65                                         @Override
66                                         public void exception(Throwable t) {
67                                                 Logger.defaultLogError(t);
68                                         }
69                                         
70                                 });
71                         }
72
73                         @Override
74                         public void exception(AsyncReadGraph graph, Throwable throwable) {
75                                 Logger.defaultLogError(throwable);
76                         }
77
78                         @Override
79                         public void finished(AsyncReadGraph graph) {
80                         }
81
82                 });
83                 
84                 structure = dqs.compileForEachObject(graph, L0.ConsistsOf, new AsyncContextMultiProcedure<InternalEntry, Resource>() {
85
86                         @Override
87                         public void execute(AsyncReadGraph graph, InternalEntry parent, Resource child) {
88                                 
89                                 if(exclusions.contains(child)) return;
90                                 
91                                 if(!ignoreVirtual || child.isPersistent()) {
92                                         InternalEntry entry = new InternalEntry(parent, child, null);
93                                         if(result.add(entry)) {
94                                                 dqs.forEachObjectCompiled(graph, child, entry, structure);
95                                                 dqs.forEachObjectCompiled(graph, child, entry, names);
96                                         }
97                                 }
98                                 
99                         }
100
101                         @Override
102                         public void finished(AsyncReadGraph graph) {
103                         }
104
105                         @Override
106                         public void exception(AsyncReadGraph graph, Throwable throwable) {
107                                 Logger.defaultLogError(throwable);
108                         }
109
110                 });
111                 
112                 graph.syncRequest(new ReadRequest() {
113
114                         @Override
115                         public void run(ReadGraph graph) throws DatabaseException {
116                                 for(Resource r  : resources) {
117                                         InternalEntry root = new InternalEntry(null, r, null);
118                                         dqs.forEachObjectCompiled(graph, r, root, structure);
119                                 }
120                         }
121                         
122                 });
123                 
124         }
125         
126     
127 }