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 {
}
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) {
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);
+
+ }
}