]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/ModelDependenciesBean.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / util / ModelDependenciesBean.java
index 8b8e86b52adcbc2e24a932d9f7bd57fbf774490a..f941b8bf326afca87ac16e0e56d5f163c91f4a3d 100644 (file)
@@ -1,5 +1,6 @@
 package org.simantics.db.layer0.util;
 
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.LinkedList;
@@ -7,6 +8,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
+import org.eclipse.core.runtime.NullProgressMonitor;
 import org.simantics.databoard.Bindings;
 import org.simantics.databoard.adapter.AdaptException;
 import org.simantics.databoard.binding.Binding;
@@ -46,7 +48,7 @@ public class ModelDependenciesBean {
                collectDependencies(graph, resource, order, new HashSet<>());
                return order;
        }
-       
+
        private static void collectDependencies(ReadGraph graph, Resource resource, LinkedList<Resource> order, Set<Resource> visited) throws DatabaseException {
                Layer0 L0 = Layer0.getInstance(graph);
                for(Resource library : graph.syncRequest(new ObjectsWithType(resource, L0.IsLinkedTo, L0.SharedOntology))) {
@@ -57,19 +59,19 @@ public class ModelDependenciesBean {
                        order.addFirst(library);
                }
        }
-       
+
        private static List<ModelDependency> collectModelDependencies(ReadGraph graph, Resource resource) throws DatabaseException {
                List<Resource> order = collectDependencies(graph, resource);
                Collections.reverse(order);
 
-               List<ModelDependency> modelDependencies = new LinkedList<>();
+               List<ModelDependency> modelDependencies = new ArrayList<>(order.size());
 
                for (Resource library : order) {
                        String uri = graph.getPossibleURI(library);
                        if(uri == null) continue;
                        CopyHandler ch = graph.adapt(library, CopyHandler.class);
                        SimanticsClipboardImpl clipboard = new SimanticsClipboardImpl();
-                       ch.copyToClipboard(graph, clipboard);
+                       ch.copyToClipboard(graph, clipboard, new NullProgressMonitor());
                        for (Set<Representation> object : clipboard.getContents()) {
                                TransferableGraph1 tg = ClipboardUtils.accept(graph, object, SimanticsKeys.KEY_TRANSFERABLE_GRAPH);
                                if(tg != null) {
@@ -78,27 +80,17 @@ public class ModelDependenciesBean {
                        }
                }
                return modelDependencies;
-
        }
 
        public static ModelDependenciesBean create(ReadGraph graph, Resource resource) throws DatabaseException {
-
                List<ModelDependency> dependencies = collectModelDependencies(graph, resource);
                return new ModelDependenciesBean(dependencies.toArray(new ModelDependency[dependencies.size()]));
-
        }
 
-       public static ModelDependenciesBean fromMigrationState(MigrationState state) throws DatabaseException {
+       public static ModelDependenciesBean fromMigrationState(MigrationState state) throws DatabaseException, AdaptException {
                Map<String,Variant> extensions = state.getProperty(MigrationStateKeys.TG_EXTENSIONS);
                final Variant variant = extensions.get(ModelDependenciesBean.EXTENSION_KEY);
-               if (variant != null) {
-                       try {
-                               return (ModelDependenciesBean) variant.getValue(ModelDependenciesBean.BINDING);
-                       } catch (AdaptException e) {
-                               e.printStackTrace();
-                       }
-               }
-               return null;
-       }       
-       
+               return variant != null ? (ModelDependenciesBean) variant.getValue(ModelDependenciesBean.BINDING) : null;
+       }
+
 }