]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.graph.db/src/org/simantics/graph/db/StreamingTransferableGraphImportProcess.java
TransferableGraph import to VG with different name
[simantics/platform.git] / bundles / org.simantics.graph.db / src / org / simantics / graph / db / StreamingTransferableGraphImportProcess.java
index 754ae61233f1e2b36460801a2743495b967af439..e61937fc9e90aaf92d2dc57767d35c4545d6b91e 100644 (file)
@@ -163,8 +163,8 @@ public class StreamingTransferableGraphImportProcess implements TransferableGrap
        void addMissing(int handleIndex, String external) {
                allMissingExternals.put(external, handleIndex);
                Set<String> removals = new HashSet<>();
-               for(String ext : missingExternals) if(ext.startsWith(external)) return;
-               for(String ext : missingExternals) if(external.startsWith(ext)) removals.add(ext);
+               for(String ext : missingExternals) if(ext.startsWith(external + "/")) return;
+               for(String ext : missingExternals) if(external.startsWith(ext + "/")) removals.add(ext);
                missingExternals.removeAll(removals);
                missingExternals.add(external);
        }
@@ -188,6 +188,21 @@ public class StreamingTransferableGraphImportProcess implements TransferableGrap
                this.handles = new int[resourceCount];
                TIntObjectMap<Identity> identityMap = TransferableGraphUtils.mapIdentities(identities);
                
+               // We must process roots first, because internal identifiers depend on them.
+               for(Identity identity : identities) {
+                       IdentityDefinition definition = identity.definition;
+                       if (definition instanceof Root) {
+                               Root root = (Root) definition;
+                               if (root.name.equals(""))
+                                       handles[identity.resource] = builder.handle(RootLibrary);
+                               else {
+                                       Resource existing = advisor.analyzeRoot(graph, root);
+                                       if (existing != null)
+                                               handles[identity.resource] = builder.handle(existing);
+                               }
+                       }
+               }
+               
                for(Identity identity : identities) {
                        IdentityDefinition definition = identity.definition;
                        if(definition instanceof External) {
@@ -210,33 +225,23 @@ public class StreamingTransferableGraphImportProcess implements TransferableGrap
                                                    TransientCacheAsyncListener.instance()); 
                                                        Resource child = childMap.get(def.name); 
                                                        if(child == null) {
-                                                               addMissing(identity.resource, graph.getURI(parent) + "/" + def.name);
+                                                               addMissing(identity.resource, graph.getURI(parent) + "/" + URIStringUtils.escape(def.name));
                                                        } else {
                                                                handles[identity.resource] = builder.handle(child);
                                                        }
                                                } else {
-                                                   addMissing(identity.resource, TransferableGraphUtils.getURI(resourceCount, identityMap, def.parent) + "/" + def.name);
+                                                   addMissing(identity.resource, TransferableGraphUtils.getURI(resourceCount, identityMap, def.parent) + "/" + URIStringUtils.escape(def.name));
                                                }
                                        }
                                }
                        }
                        else if(definition instanceof Internal) {
-                               String uri = TransferableGraphUtils.getTrueURI(resourceCount, identityMap, identity.resource);
+                               String uri = TransferableGraphUtils.getURI(resourceCount, identityMap, identity.resource);
                                Resource existing = graph.getPossibleResource(uri);
                                if(existing != null) {
                                        existingInternalMap.put(identity.resource, existing);
                                }
                        }
-                       else if(definition instanceof Root) {
-                               Root root = (Root)definition;
-                               if(root.name.equals(""))
-                                   handles[identity.resource] = builder.handle(RootLibrary);
-                               else  {
-                                       Resource existing = advisor.analyzeRoot(graph, root);
-                                       if(existing != null)
-                                           handles[identity.resource] = builder.handle(existing);
-                               }
-                       }
                        else if(definition instanceof Optional) {
                                External def = (External)definition;
                                Resource parent = builder.resource(handles[def.parent]);
@@ -244,22 +249,33 @@ public class StreamingTransferableGraphImportProcess implements TransferableGrap
                                        handles[identity.resource] = builder.handle(graph.syncRequest(new UnescapedChildMapOfResource(parent)).get(def.name));                          
                        }
                }               
-               
-               //if(!missingExternals.isEmpty()) throw new MissingDependencyException(this);
-               
+
+               if (!missingExternals.isEmpty() && failOnMissingEntities())
+                       throw new MissingDependencyException(this);
+       }
+
+       private boolean failOnMissingEntities() {
+               return "true".equalsIgnoreCase(
+                               System.getProperty(
+                                               "org.simantics.tg.import.failOnMissingEntities",
+                                               "false") );
        }
 
        @Override
        public Resource createChild(WriteOnlyGraph graph, Resource parent, Resource child, String name) throws DatabaseException {
+               //System.err.println("child " + parent + " - " + child + " = " + name);
            if(child == null) child = graph.newResource();
                Resource nameResource = graph.newResource();
                graph.claim(nameResource, InstanceOf, null, String);
                graph.claimValue(nameResource, name, WriteBindings.STRING);
                graph.claim(child, HasName, NameOf, nameResource);
+               graph.claim(parent,  ConsistsOf, PartOf, child);
                return child;
        }
 
        int[] getClustering() {
+               if (vg != null)
+                       return null;
                Variant v = extensions.get(Extensions.CLUSTERING);
                if(v == null) return null;
                try {
@@ -271,6 +287,8 @@ public class StreamingTransferableGraphImportProcess implements TransferableGrap
        }
 
        int[] getClusterSets() {
+               if (vg != null)
+                       return null;
                Variant v = extensions.get(Extensions.CLUSTER_SETS);
                if(v == null) return null;
                try {
@@ -289,7 +307,7 @@ public class StreamingTransferableGraphImportProcess implements TransferableGrap
                ClusteringSupport support = graph.getService(ClusteringSupport.class);
                if(id == Extensions.ROOT_LIBRARY_CLUSTER_SET || id == Extensions.INDEX_ROOT_CLUSTER_SET) return;
                Resource indexRootClusterSetResource = rootLibrary;
-               if(support.isClusterSet(indexRoot)) {
+               if(indexRoot != null && support.isClusterSet(indexRoot)) {
                        indexRootClusterSetResource = indexRoot;
                } else {
                        graph.setClusterSet4NewResource(rootLibrary);
@@ -339,11 +357,15 @@ public class StreamingTransferableGraphImportProcess implements TransferableGrap
                        Collections.sort(missing);
                        for(String uri : missing) {
                                String[] parts = URIStringUtils.splitURI(uri);
+                               // URIStringUtils.splitURI returns root URI in non-standard format, so fix it manually as a workaround
+                               if (parts[0].equals("http://")) {
+                                       parts[0] = "http:/";
+                               }
 
                                Resource parent = resolvedParents.get(parts[0]);
                                // TODO: proper exception message
                                if(parent == null) {
-                                       throw new IllegalStateException("!!");
+                                       throw new IllegalStateException("Missing URI: " + uri);
                                }
 
                                Resource childResource = graph.newResource();
@@ -351,7 +373,7 @@ public class StreamingTransferableGraphImportProcess implements TransferableGrap
 
                                Resource nameResource = graph.newResource();
                                graph.claim(nameResource, InstanceOf, null, String);
-                               graph.claimValue(nameResource, parts[1], WriteBindings.STRING);
+                               graph.claimValue(nameResource, URIStringUtils.unescape(parts[1]), WriteBindings.STRING);
                                graph.claim(childResource, HasName, NameOf, nameResource);
 
                                graph.claim(parent, ConsistsOf, PartOf, childResource);
@@ -453,7 +475,6 @@ public class StreamingTransferableGraphImportProcess implements TransferableGrap
                        }
                        else if(definition instanceof Internal) {
                                Internal def = (Internal)definition;
-                               
                                Resource external = existingInternalMap.get(identity.resource);
                                if(external != null) {
                                        handles[identity.resource] = builder.handle(external);
@@ -593,7 +614,11 @@ public class StreamingTransferableGraphImportProcess implements TransferableGrap
                tg.forValues2(null, new ValueProcedure());
                
                for(Resource r : existingInternalMap.valueCollection()) {
-                       graph.deny(r, InstanceOf, null, ExternalEntity, null);
+                       try {
+                               graph.deny(r, InstanceOf, null, ExternalEntity, null);
+                       } catch (DatabaseException e) {
+                               graph.deny(r, InstanceOf, null, ExternalEntity, vg);
+                       }
                }
                
        }