]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Allow external customization fo TG Root type URIs. 49/649/1
authorTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Wed, 21 Jun 2017 20:57:13 +0000 (23:57 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Wed, 21 Jun 2017 20:57:13 +0000 (23:57 +0300)
The customization happens by writing the type URIs into
TransferableGraphConfiguration2.RootSpec instances which then get
translated into TG Root identity definitions by
ModelTransferableGraphSource.

refs #7328

Change-Id: I3ba23ac39009ecc6486b4ef55ead4d5971e5f130
(manually picked from commit f0546930e05223a2575b37ff9a6de08322542b5c)

bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/ModelTransferableGraphSource.java
bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/TGConfigurer.java
bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/TransferableGraphConfiguration2.java
bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/CompositeCopyHandler.java

index 2d0fbcfe18282dc733a8e6be4596a5c344be2bca..f0a0893eb447d40455835d39fdd41cc40d6e3c5f 100644 (file)
@@ -396,10 +396,13 @@ public class ModelTransferableGraphSource implements TransferableGraphSource {
 
                // Declare internal and external roots
                for(RootSpec r : configuration.roots) {
-                       Resource type = graph.getPossibleType(r.resource, L0.Entity);
-                       if(type == null) type = L0.Entity;
+                       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, graph.getURI(type));
+                       Root root = new Root(r.name, typeId);
                        Identity rootId = new Identity(id,root);
                        internalMap.put(id, rootId);
                        procedure.execute(rootId);
index 222a28924ca05ced998d8b834b955609763c8b41..350f4ca31d966b5bda61e7e570286e4488a8a0e6 100644 (file)
@@ -47,7 +47,7 @@ public class TGConfigurer {
                        Resource nameResource = graph.getPossibleObject(root.resource, L0.HasName);
                        if(nameResource != null) preStatus.put(nameResource, ExtentStatus.EXCLUDED);
 //                     String name = graph.getValue(nameResource, Bindings.STRING);
-                       roots.add(new RootSpec(root.resource, root.name, root.internal));
+                       roots.add(new RootSpec(root.resource, root.name, root.internal, root.type));
                        preStatus.put(root.resource, ExtentStatus.INTERNAL);
                }
                return this;
index 94c6084f294417efaa8b6232c2625e78c014e951..2203ecaeb6bd792635aa1648495f20c3b5f03956 100644 (file)
@@ -24,8 +24,16 @@ public class TransferableGraphConfiguration2 {
                public final Resource resource;
                public final String name;
                public final boolean internal;
+               /**
+                * Optional, may be null.
+                */
+               public final String type;
 
                public RootSpec(Resource resource, String name, boolean internal) {
+                       this(resource, name, internal, null);
+               }
+
+               public RootSpec(Resource resource, String name, boolean internal, String type) {
                        if (resource == null)
                                throw new NullPointerException("null resource");
                        if (name == null)
@@ -33,11 +41,12 @@ public class TransferableGraphConfiguration2 {
                        this.resource = resource;
                        this.name = name;
                        this.internal = internal;
+                       this.type = type;
                }
 
                @Override
                public String toString() {
-                       return "RootSpec[" + name + ", " + resource + ", " + internal + "]"; 
+                       return "RootSpec[" + name + ", " + resource + ", " + internal + ", " + type + "]"; 
                }
 
                @Override
@@ -47,6 +56,7 @@ public class TransferableGraphConfiguration2 {
                        result = prime * result + (internal ? 1231 : 1237);
                        result = prime * result + resource.hashCode();
                        result = prime * result + name.hashCode();
+                       result = prime * result + (type != null ? type.hashCode() : 0);
                        return result;
                }
 
@@ -59,10 +69,18 @@ public class TransferableGraphConfiguration2 {
                        if (getClass() != obj.getClass())
                                return false;
                        RootSpec other = (RootSpec) obj;
-                       return internal == other.internal && resource.equals(other.resource) && name.equals(other.name);
+                       return internal == other.internal && resource.equals(other.resource) && name.equals(other.name)
+                                       && objectEquals(type, other.type);
+               }
+
+               private boolean objectEquals(Object o1, Object o2) {
+                       if (o1 == o2) return true;
+                       if (o1 == null && o2 == null) return true;
+                       if (o1 == null || o2 == null) return false;
+                       return o1.equals(o2);
                }
        }
-       
+
        final public TreeMap<String, Variant> baseExtensions = new TreeMap<String,Variant>();
        final public Resource indexRoot;
        final public Collection<RootSpec> roots;
index 144448d9cff358a9e26a78126a278a04cf9b80a1..ae65e87f88476bf6b2059e8899d5a63266816fee 100644 (file)
@@ -60,6 +60,10 @@ public class CompositeCopyHandler extends DefaultCopyHandler {
         Set<Resource> externals = new HashSet<>();
         List<RootSpec> roots = new ArrayList<>();
 
+        Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(resources.iterator().next()));
+        if(indexRoot == null) throw new DatabaseException("Composite is not part of any index root");
+        String indexRootUri = graph.getURI(indexRoot);
+
         for(Resource resource : resources) {
             // Process all connection joins.
             // This is the only way to access all of them.
@@ -113,28 +117,20 @@ public class CompositeCopyHandler extends DefaultCopyHandler {
             }
 
             // Include resource as root
-//            GUID rootId = graph.getRelatedValue(resource, L0.identifier, GUID.BINDING);
-//            String rootName = graph.getRelatedValue(resource, L0.HasName, Bindings.STRING);
-//            String escapedRootName = URIStringUtils.escape(rootName);
-//            String escapedPath = ModelingUtils.getDiagramCompositePath(graph, resource);
             CompositeInfo info = CompositeInfo.fromResource(graph, resource);
-            roots.add(new RootSpec(resource, info.getTGName(), true));
+            roots.add(new RootSpec(resource, info.getTGName(), true, typeId(graph, L0, indexRootUri, resource)));
                Resource id = graph.getPossibleObject(resource, L0.identifier);
                if(id != null) exclusions.add(id);
             // Include components as roots
             for(Resource child : graph.sync(new ObjectsWithType(resource, L0.ConsistsOf, SR.Component))) {
                DiagramComponentInfo cinfo = DiagramComponentInfo.fromResource(graph, info, child);
-//             GUID childId = graph.getRelatedValue(resource, L0.identifier, GUID.BINDING);
-//                String childName = graph.getRelatedValue(child, L0.HasName, Bindings.STRING);
                id = graph.getPossibleObject(child, L0.identifier);
                if(id != null) exclusions.add(id);
-                roots.add(new RootSpec(child, cinfo.getTGName(info), true));
+                roots.add(new RootSpec(child, cinfo.getTGName(info), true, typeId(graph, L0, indexRootUri, child)));
             }
         }
 
-        Resource model = graph.syncRequest(new PossibleIndexRoot(resources.iterator().next()));
-        if(model == null) throw new DatabaseException("Composite is not part of any index root");
-        roots.add(new RootSpec(model, "%model", false));
+        roots.add(new RootSpec(indexRoot, "%model", false));
 
         TransferableGraphConfiguration2 config = TransferableGraphConfiguration2.createWithNames2(graph, roots, exclusions, true, false);
         for (Resource external : externals)
@@ -142,4 +138,14 @@ public class CompositeCopyHandler extends DefaultCopyHandler {
         return config;
     }
 
+    private static String typeId(ReadGraph graph, Layer0 L0, String indexRootUri, Resource r) throws DatabaseException {
+        Resource type = graph.getPossibleType(r, L0.Entity);
+        if (type == null)
+            return Layer0.URIs.Entity;
+        String typeUri = graph.getPossibleURI(type);
+        if (typeUri == null || !typeUri.startsWith(indexRootUri))
+            return typeUri;
+        return "%model" + typeUri.substring(indexRootUri.length());
+    }
+
 }