]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/ModelTransferableGraphSource.java
Allow external customization fo TG Root type URIs.
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / util / ModelTransferableGraphSource.java
index cbd3130eea6d737f2c431b685e47f6c306576399..f0a0893eb447d40455835d39fdd41cc40d6e3c5f 100644 (file)
@@ -27,16 +27,20 @@ import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.exception.RuntimeDatabaseException;
 import org.simantics.db.exception.ValidationException;
 import org.simantics.db.layer0.adapter.SubgraphExtent.ExtentStatus;
+import org.simantics.db.layer0.util.ConsistsOfProcess.InternalEntry;
 import org.simantics.db.layer0.util.TransferableGraphConfiguration2.RootSpec;
 import org.simantics.db.service.SerialisationSupport;
 import org.simantics.graph.db.TransferableGraphSource;
 import org.simantics.graph.representation.External;
 import org.simantics.graph.representation.Identity;
+import org.simantics.graph.representation.Internal;
 import org.simantics.graph.representation.Root;
 import org.simantics.graph.representation.Value;
 import org.simantics.layer0.Layer0;
 
 import gnu.trove.list.array.TIntArrayList;
+import gnu.trove.map.TIntObjectMap;
+import gnu.trove.map.hash.TIntObjectHashMap;
 import gnu.trove.procedure.TIntIntProcedure;
 
 public class ModelTransferableGraphSource implements TransferableGraphSource {
@@ -51,7 +55,7 @@ public class ModelTransferableGraphSource implements TransferableGraphSource {
        private volatile boolean closed = false;
 
        TIntArrayList externalParents = new TIntArrayList();
-       ArrayList<String> externalNames = new ArrayList<String>();
+       ArrayList<String> externalNames = new ArrayList<>();
        TreeMap<String,String> downloads = new TreeMap<String,String>();
 
        public ModelTransferableGraphSource(final ReadGraph graph, TransferableGraphConfiguration2 configuration, final DomainProcessorState state, File ... fs) throws DatabaseException {
@@ -80,7 +84,7 @@ public class ModelTransferableGraphSource implements TransferableGraphSource {
                
                this.externalBase = state.id;
 
-               final Collection<String> errors = new HashSet<String>();
+               final Collection<String> errors = new HashSet<>();
 
                // All resource considered as not internal by domain processor. Can also contain roots.
                int[] externals = state.externals.toArray();
@@ -106,7 +110,7 @@ public class ModelTransferableGraphSource implements TransferableGraphSource {
                });
                
                if(!errors.isEmpty()) {
-                       ArrayList<String> sorted = new ArrayList<String>(errors);
+                       ArrayList<String> sorted = new ArrayList<>(errors);
                        Collections.sort(sorted);
                        StringBuilder message = new StringBuilder();
                        message.append("Errors in exported model:\n");
@@ -224,7 +228,7 @@ public class ModelTransferableGraphSource implements TransferableGraphSource {
        
        @Override
        public int getIdentityCount() {
-               return configuration.roots.size() + state.externals.size() + 1;
+               return configuration.roots.size() + state.externals.size() + state.internalEntries.size() + 1;
        }
        
        @Override
@@ -388,13 +392,20 @@ public class ModelTransferableGraphSource implements TransferableGraphSource {
                // TODO: this should be Root with name ""
                procedure.execute(getRootIdentity(state, support, graph.getRootLibrary()));
 
+               TIntObjectMap<Identity> internalMap = new TIntObjectHashMap<>(100, 0.5f, Integer.MIN_VALUE);
+
                // Declare internal and external roots
                for(RootSpec r : configuration.roots) {
-                       Resource type = graph.getPossibleType(r.resource, L0.Entity);
-                       if(type == null) type = L0.Entity;
-                       procedure.execute(new Identity(
-                                       state.ids.get(support.getTransientId(r.resource)),
-                                       new Root(r.name, graph.getURI(type))));
+                       String typeId = r.type;
+                       if (typeId == null) {
+                               Resource type = graph.getPossibleType(r.resource, L0.Entity);
+                               typeId = type != null ? graph.getURI(type) : Layer0.URIs.Entity;
+                       }
+                       int id = state.ids.get(support.getTransientId(r.resource));
+                       Root root = new Root(r.name, typeId);
+                       Identity rootId = new Identity(id,root);
+                       internalMap.put(id, rootId);
+                       procedure.execute(rootId);
                }
 
                for(int i = 0; i < state.externals.size() ; i++) {
@@ -402,8 +413,29 @@ public class ModelTransferableGraphSource implements TransferableGraphSource {
                        String name = externalNames.get(i);
                        procedure.execute(new Identity(externalBase + i, new External(parent,name)));
        }
+               
+               if(state.internalEntries != null) {
+                       for(InternalEntry ie : state.internalEntries) {
+                               if(ie.parent != null && ie.name != null) {
+                                       procedure.execute(resolveInternal(graph, support, ie, internalMap));
+                               } else {
+                                       throw new DatabaseException("Invalid internal entry " + ie);
+                               }
+                       }
+               }
        
        }
+       
+       private Identity resolveInternal(ReadGraph graph, SerialisationSupport ss, InternalEntry entry, TIntObjectMap<Identity> internalMap) throws DatabaseException {
+               int id = state.ids.get(ss.getTransientId(entry.resource));
+               Identity existing = internalMap.get(id);
+               if(existing != null) return existing;
+               Identity parent = resolveInternal(graph, ss, entry.parent, internalMap);
+               Identity result = new Identity(id,
+                               new Internal(parent.resource, entry.name));
+               internalMap.put(id, result);
+               return result;
+       }
 
        @Override
        public TreeMap<String, Variant> getExtensions() {