Fail safe import fixes made by Antti
[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.HashSet;
6 import java.util.List;
7 import java.util.Set;
8
9 import org.simantics.db.AsyncReadGraph;
10 import org.simantics.db.ReadGraph;
11 import org.simantics.db.Resource;
12 import org.simantics.db.ResourceMap;
13 import org.simantics.db.common.request.ReadRequest;
14 import org.simantics.db.common.utils.Logger;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.db.layer0.adapter.SubgraphExtent.ExtentStatus;
17 import org.simantics.db.procedure.AsyncContextMultiProcedure;
18 import org.simantics.db.procedure.Procedure;
19 import org.simantics.db.service.DirectQuerySupport;
20 import org.simantics.layer0.Layer0;
21 import org.simantics.utils.datastructures.Pair;
22
23 class ConsistsOfProcess {
24
25         final List<InternalEntry> result;
26         final Set<Resource> childrenWithNoName;
27         final AsyncContextMultiProcedure<InternalEntry, Resource> structure;
28         final AsyncContextMultiProcedure<InternalEntry, Resource> names;
29
30         public static Pair<List<InternalEntry>,Set<Resource>> walk(ReadGraph graph, ResourceMap<ExtentStatus> status, Collection<Resource> resources, Set<Resource> exclusions, boolean ignoreVirtual) throws DatabaseException {
31         ConsistsOfProcess process = new ConsistsOfProcess(graph, status, resources, exclusions, ignoreVirtual);
32         return Pair.make(process.result, process.childrenWithNoName);
33     }
34     
35     static class InternalEntry {
36         public InternalEntry parent;
37         public Resource resource;
38         public String name;
39         public boolean valid = true;
40         InternalEntry(InternalEntry parent, Resource resource, String name) {
41                 this.parent = parent;
42                 this.resource = resource;
43                 this.name = name;
44         }
45     }
46         
47     private ConsistsOfProcess(ReadGraph graph, ResourceMap<ExtentStatus> status, final Collection<Resource> resources, final Set<Resource> exclusions, final boolean ignoreVirtual) throws DatabaseException {
48
49                 final Layer0 L0 = Layer0.getInstance(graph);
50                 final DirectQuerySupport dqs = graph.getService(DirectQuerySupport.class);
51                 
52                 result = new ArrayList<InternalEntry>();
53                 childrenWithNoName = new HashSet<>();
54                 names = dqs.compileForEachObject(graph, L0.HasName, new AsyncContextMultiProcedure<InternalEntry, Resource>() {
55
56                         @Override
57                         public void execute(AsyncReadGraph graph, InternalEntry entry, Resource nameResource) {
58                 
59                                 if(status != null)
60                                         status.put(nameResource, ExtentStatus.EXCLUDED);
61                                 
62                                 graph.forPossibleValue(nameResource, new Procedure<String>() {
63
64                                         @Override
65                                         public void execute(String name) {
66                                             if(!entry.valid) return;
67                                             
68                                             if(name == null) {
69                                                 entry.valid = false;
70                                             } else if (entry.name != null) {
71                                                 entry.valid = false;
72                                             } else {
73                                                 entry.name = name;
74                                             }
75                                         }
76
77                                         @Override
78                                         public void exception(Throwable t) {
79                                                 Logger.defaultLogError(t);
80                                         }
81                                         
82                                 });
83                         }
84
85                         @Override
86                         public void exception(AsyncReadGraph graph, Throwable throwable) {
87                                 Logger.defaultLogError(throwable);
88                         }
89
90                         @Override
91                         public void finished(AsyncReadGraph graph, InternalEntry entry) {
92                             if(entry.valid) {
93                                 if(entry.name != null) {
94                                     result.add(entry);
95                                 } else {
96                                     // This one did not have a name - not a valid internal
97                                     childrenWithNoName.add(entry.resource);
98                                 }
99                             } else {
100                                 // Something wrong has happened. Do not treat as valid internal
101                                 childrenWithNoName.add(entry.resource);
102                             }
103                         }
104                 });
105                 
106                 structure = dqs.compileForEachObject(graph, L0.ConsistsOf, new AsyncContextMultiProcedure<InternalEntry, Resource>() {
107
108                         @Override
109                         public void execute(AsyncReadGraph graph, InternalEntry parent, Resource child) {
110                                 
111                                 if(exclusions.contains(child)) return;
112                                 
113                                 if(!ignoreVirtual || child.isPersistent()) {
114                                         InternalEntry entry = new InternalEntry(parent, child, null);
115                                         dqs.forEachObjectCompiled(graph, child, entry, structure);
116                                         dqs.forEachObjectCompiled(graph, child, entry, names);
117                                 }
118                                 
119                         }
120
121                         @Override
122                         public void finished(AsyncReadGraph graph, InternalEntry parent) {
123                         }
124
125                         @Override
126                         public void exception(AsyncReadGraph graph, Throwable throwable) {
127                                 Logger.defaultLogError(throwable);
128                         }
129
130                 });
131                 
132                 graph.syncRequest(new ReadRequest() {
133
134                         @Override
135                         public void run(ReadGraph graph) throws DatabaseException {
136                                 for(Resource r  : resources) {
137                                         InternalEntry root = new InternalEntry(null, r, null);
138                                         dqs.forEachObjectCompiled(graph, r, root, structure);
139                                 }
140                         }
141                         
142                 });
143                 
144         }
145         
146     
147 }