Fail safe import fixes made by Antti
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / util / ConsistsOfProcess.java
index d910f13ff946a77e40d9b2dce830c2020c678f40..a2e64868f7c21d43c1456c4662810c20f6ca4ed6 100644 (file)
@@ -2,6 +2,7 @@ package org.simantics.db.layer0.util;
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 
@@ -17,22 +18,25 @@ import org.simantics.db.procedure.AsyncContextMultiProcedure;
 import org.simantics.db.procedure.Procedure;
 import org.simantics.db.service.DirectQuerySupport;
 import org.simantics.layer0.Layer0;
+import org.simantics.utils.datastructures.Pair;
 
 class ConsistsOfProcess {
 
        final List<InternalEntry> result;
+       final Set<Resource> childrenWithNoName;
        final AsyncContextMultiProcedure<InternalEntry, Resource> structure;
        final AsyncContextMultiProcedure<InternalEntry, Resource> names;
 
-    public static List<InternalEntry> walk(ReadGraph graph, ResourceMap<ExtentStatus> status, Collection<Resource> resources, Set<Resource> exclusions, boolean ignoreVirtual) throws DatabaseException {
+       public static Pair<List<InternalEntry>,Set<Resource>> walk(ReadGraph graph, ResourceMap<ExtentStatus> status, Collection<Resource> resources, Set<Resource> exclusions, boolean ignoreVirtual) throws DatabaseException {
        ConsistsOfProcess process = new ConsistsOfProcess(graph, status, resources, exclusions, ignoreVirtual);
-       return process.result;
+       return Pair.make(process.result, process.childrenWithNoName);
     }
     
     static class InternalEntry {
        public InternalEntry parent;
        public Resource resource;
        public String name;
+       public boolean valid = true;
        InternalEntry(InternalEntry parent, Resource resource, String name) {
                this.parent = parent;
                this.resource = resource;
@@ -46,7 +50,7 @@ class ConsistsOfProcess {
                final DirectQuerySupport dqs = graph.getService(DirectQuerySupport.class);
                
                result = new ArrayList<InternalEntry>();
-               
+               childrenWithNoName = new HashSet<>();
                names = dqs.compileForEachObject(graph, L0.HasName, new AsyncContextMultiProcedure<InternalEntry, Resource>() {
 
                        @Override
@@ -58,8 +62,16 @@ class ConsistsOfProcess {
                                graph.forPossibleValue(nameResource, new Procedure<String>() {
 
                                        @Override
-                                       public void execute(String result) {
-                                               entry.name = result;
+                                       public void execute(String name) {
+                                           if(!entry.valid) return;
+                                           
+                                           if(name == null) {
+                                               entry.valid = false;
+                                           } else if (entry.name != null) {
+                                               entry.valid = false;
+                                           } else {
+                                               entry.name = name;
+                                           }
                                        }
 
                                        @Override
@@ -76,9 +88,19 @@ class ConsistsOfProcess {
                        }
 
                        @Override
-                       public void finished(AsyncReadGraph graph) {
+                       public void finished(AsyncReadGraph graph, InternalEntry entry) {
+                           if(entry.valid) {
+                               if(entry.name != null) {
+                                   result.add(entry);
+                               } else {
+                                   // This one did not have a name - not a valid internal
+                                   childrenWithNoName.add(entry.resource);
+                               }
+                           } else {
+                               // Something wrong has happened. Do not treat as valid internal
+                               childrenWithNoName.add(entry.resource);
+                           }
                        }
-
                });
                
                structure = dqs.compileForEachObject(graph, L0.ConsistsOf, new AsyncContextMultiProcedure<InternalEntry, Resource>() {
@@ -90,16 +112,14 @@ class ConsistsOfProcess {
                                
                                if(!ignoreVirtual || child.isPersistent()) {
                                        InternalEntry entry = new InternalEntry(parent, child, null);
-                                       if(result.add(entry)) {
-                                               dqs.forEachObjectCompiled(graph, child, entry, structure);
-                                               dqs.forEachObjectCompiled(graph, child, entry, names);
-                                       }
+                                       dqs.forEachObjectCompiled(graph, child, entry, structure);
+                                       dqs.forEachObjectCompiled(graph, child, entry, names);
                                }
                                
                        }
 
                        @Override
-                       public void finished(AsyncReadGraph graph) {
+                       public void finished(AsyncReadGraph graph, InternalEntry parent) {
                        }
 
                        @Override