]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/SharedOntologyImportAdvisor.java
Import/export changes. A load.
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / adapter / impl / SharedOntologyImportAdvisor.java
index e16d07cf0469ace76097b80204a264476a5755aa..2a575a578769db92b912ca66e1c02ed427aab314 100644 (file)
@@ -33,6 +33,7 @@ import org.simantics.graph.db.MissingDependencyException;
 import org.simantics.graph.db.TransferableGraphImporter;
 import org.simantics.graph.representation.Root;
 import org.simantics.layer0.Layer0;
+import org.simantics.utils.datastructures.Pair;
 import org.simantics.utils.datastructures.Triple;
 
 public class SharedOntologyImportAdvisor extends AbstractImportAdvisor2 {
@@ -87,7 +88,15 @@ public class SharedOntologyImportAdvisor extends AbstractImportAdvisor2 {
                }
                
                Resource existing = graph.getPossibleResource(uri);
-               if(existing != null) throw new DatabaseException("Shared library " + uri + " exists already.");
+               if(existing != null) {
+                       if(graph.isInstanceOf(existing, L0.ExternalEntity)) {
+                               created.put(r, existing);
+                   addRootInfo(r, r.name, existing);
+                               return null;
+                       } else {
+                               throw new DatabaseException("Shared library " + uri + " exists already.");
+                       }
+               }
                
                Resource type = graph.getPossibleResource(r.type);
                if(type == null) { 
@@ -124,43 +133,62 @@ public class SharedOntologyImportAdvisor extends AbstractImportAdvisor2 {
     public void beforeWrite(WriteOnlyGraph graph, TransferableGraphImporter process)
             throws DatabaseException {
        
-        graph.markUndoPoint();
-        if(published) {
-               XSupport xs = graph.getService(XSupport.class);
-               xs.setServiceMode(false, true);
-        }
-        
-        graph.setClusterSet4NewResource(graph.getRootLibrary());
-        graph.flushCluster();
-
-        for(Map.Entry<Root, Triple<Resource, Resource, List<String>>> entry : toCreate.entrySet()) {
-               
-            Triple<Resource, Resource, List<String>> recipe = entry.getValue();
-
-            Resource base = recipe.first;
-            for(int i=0;i<recipe.third.size()-1;i++) {
-                Resource lib = graph.newResource();
-                graph.claim(lib, L0.InstanceOf, null, L0.Library);
-                graph.addLiteral(lib, L0.HasName, L0.NameOf, URIStringUtils.unescape( recipe.third.get(i) ), Bindings.STRING);
-                graph.claim(base, L0.ConsistsOf, L0.PartOf, lib);
-                base = lib;
-            }
+       XSupport xs = graph.getService(XSupport.class);
 
-            Resource lib = graph.newResource();
-            graph.newClusterSet(lib);
-            graph.setClusterSet4NewResource(lib);
-            graph.claim(lib, L0.InstanceOf, null, recipe.second);
-            String name = URIStringUtils.unescape( recipe.third.get(recipe.third.size()-1) );
-            graph.addLiteral(lib, L0.HasName, L0.NameOf, name, Bindings.STRING);
-            graph.claim(base, L0.ConsistsOf, L0.PartOf, lib);
+       Pair<Boolean,Boolean> mode = xs.getServiceMode();
+       
+       try {
+
+               graph.markUndoPoint();
+               
+               if(published) {
+                       xs.setServiceMode(true, true);
+               } else {
+                       xs.setServiceMode(true, mode.second);
+               }
+               
+               graph.setClusterSet4NewResource(graph.getRootLibrary());
+               graph.flushCluster();
+               
+               for(RootInfo info : getRootInfo()) {
+                       // At this stage these are existing L0.ExternalEntity instances that are now being imported.
+                       graph.deny(info.resource, L0.InstanceOf, null, L0.ExternalEntity, null);
+               }
+               
+               for(Map.Entry<Root, Triple<Resource, Resource, List<String>>> entry : toCreate.entrySet()) {
+                       
+                   Triple<Resource, Resource, List<String>> recipe = entry.getValue();
+       
+                   Resource base = recipe.first;
+                   for(int i=0;i<recipe.third.size()-1;i++) {
+                       Resource lib = graph.newResource();
+                       graph.claim(lib, L0.InstanceOf, null, L0.Library);
+                       graph.addLiteral(lib, L0.HasName, L0.NameOf, URIStringUtils.unescape( recipe.third.get(i) ), Bindings.STRING);
+                       graph.claim(base, L0.ConsistsOf, L0.PartOf, lib);
+                       base = lib;
+                   }
+       
+                   Resource lib = graph.newResource();
+                   graph.newClusterSet(lib);
+                   graph.setClusterSet4NewResource(lib);
+                   graph.claim(lib, L0.InstanceOf, null, recipe.second);
+                   String name = URIStringUtils.unescape( recipe.third.get(recipe.third.size()-1) );
+                   graph.addLiteral(lib, L0.HasName, L0.NameOf, name, Bindings.STRING);
+                   graph.claim(base, L0.ConsistsOf, L0.PartOf, lib);
+       
+                   addRootInfo(entry.getKey(), name, lib);
+                   
+                   created.put(entry.getKey(), lib);
+                   
+               }
+       
+               graph.flushCluster();
+               
+       } finally {
 
-            addRootInfo(entry.getKey(), name, lib);
-            
-            created.put(entry.getKey(), lib);
-            
-        }
-        
-        graph.flushCluster();
+               xs.setServiceMode(mode.first, mode.second);
+               
+       }
         
     }