]> gerrit.simantics Code Review - simantics/3d.git/commitdiff
Java object to ordered set mapping
authorluukkainen <luukkainen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 24 Sep 2013 12:47:29 +0000 (12:47 +0000)
committerluukkainen <luukkainen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Tue, 24 Sep 2013 12:47:29 +0000 (12:47 +0000)
fixes #4439

git-svn-id: https://www.simantics.org/svn/simantics/3d/trunk@27883 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/OrderedSetType.java [new file with mode: 0644]
org.simantics.objmap2/src/org/simantics/objmap/graph/schema/MappingSchemas.java
org.simantics.objmap2/src/org/simantics/objmap/graph/schema/OrderedSetSimpleLinkType.java [new file with mode: 0644]
org.simantics.objmap2/src/org/simantics/objmap/structural/schema/MappingSchemas.java
org.simantics.objmap2/src/org/simantics/objmap/structural/schema/OrderedSetSimpleLinkType.java [new file with mode: 0644]
org.simantics.objmap2/src/org/simantics/objmap/structural/schema/SimpleLinkType.java

diff --git a/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/OrderedSetType.java b/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/OrderedSetType.java
new file mode 100644 (file)
index 0000000..0ae08af
--- /dev/null
@@ -0,0 +1,23 @@
+/*******************************************************************************\r
+ * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.objmap.graph.annotations;\r
+\r
+import java.lang.annotation.ElementType;\r
+import java.lang.annotation.Retention;\r
+import java.lang.annotation.RetentionPolicy;\r
+import java.lang.annotation.Target;\r
+\r
+@Retention(RetentionPolicy.RUNTIME)\r
+@Target(ElementType.TYPE) \r
+public @interface OrderedSetType {\r
+       String value();\r
+}\r
index 0007d2bae54cac80b7f356a1538b78dc8daa070b..b91928492bf52a7609d7ebce30fe366b7fb48fc5 100644 (file)
@@ -28,6 +28,7 @@ import org.simantics.objmap.graph.annotations.HasCollectionAdder;
 import org.simantics.objmap.graph.annotations.HasCollectionRemover;\r
 import org.simantics.objmap.graph.annotations.HasSetter;\r
 import org.simantics.objmap.graph.annotations.OptionalRelatedElements;\r
+import org.simantics.objmap.graph.annotations.OrderedSetType;\r
 import org.simantics.objmap.graph.annotations.RelatedElements;\r
 import org.simantics.objmap.graph.annotations.RelatedElementsGet;\r
 import org.simantics.objmap.graph.annotations.RelatedGetObj;\r
@@ -80,6 +81,13 @@ public class MappingSchemas {
                    \r
                    return new DynamicSimpleLinkType<Object>(g.getResource(dynamicType.value()), clazz, rules);\r
            }\r
+           OrderedSetType orderedSetType = clazz.getAnnotation(OrderedSetType.class);\r
+           if (orderedSetType != null) {\r
+               ArrayList<IBidirectionalMappingRule<Resource, Object>> rules = new ArrayList<IBidirectionalMappingRule<Resource, Object>>();\r
+                   collectRulesFromAnnotations(g, clazz, rules);\r
+                   \r
+                   return new OrderedSetSimpleLinkType<Object>(g.getResource(orderedSetType.value()), clazz, rules);\r
+           }\r
            throw new IllegalArgumentException("Class " + clazz.toString() + " does not contain annotations.");\r
        }\r
        \r
diff --git a/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/OrderedSetSimpleLinkType.java b/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/OrderedSetSimpleLinkType.java
new file mode 100644 (file)
index 0000000..b912745
--- /dev/null
@@ -0,0 +1,34 @@
+package org.simantics.objmap.graph.schema;\r
+\r
+import java.util.ArrayList;\r
+\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.utils.OrderedSetUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.objmap.bidirectional.IBidirectionalMappingRule;\r
+import org.simantics.objmap.exceptions.MappingException;\r
+\r
+public class OrderedSetSimpleLinkType<Range> extends SimpleLinkType<Range> {\r
+\r
+       public OrderedSetSimpleLinkType(Resource domainType, Class<?> rangeType, ArrayList<IBidirectionalMappingRule<Resource, Range>> rules) {\r
+               super(domainType, rangeType, rules);\r
+       }\r
+\r
+       public OrderedSetSimpleLinkType(Resource domainType, Class<?> rangeType) {\r
+               super(domainType, rangeType);\r
+       }\r
+       \r
+       public Resource createDomainElement(org.simantics.db.WriteGraph g, Range rangeElement) throws org.simantics.objmap.exceptions.MappingException {\r
+                try {\r
+            if(LOGGER.isInfoEnabled())\r
+                LOGGER.info("SimpleLinkType.createDomainElement " +\r
+                        rangeElement.toString()\r
+                );\r
+            Resource result = OrderedSetUtils.create(g, domainType);\r
+            return result;\r
+        } catch(DatabaseException e) {\r
+            throw new MappingException(e);\r
+        }\r
+       };\r
+\r
+}\r
index 758db0e029e53ca2608ae7bcecf9e8e5aef980d4..3f9169ec9f569bdbb57b42a5d8804114066ad87d 100644 (file)
@@ -18,7 +18,6 @@ import java.util.ArrayList;
 import java.util.Collection;\r
 \r
 import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
 import org.simantics.db.exception.DatabaseException;\r
 import org.simantics.objmap.bidirectional.IBidirectionalMappingRule;\r
 import org.simantics.objmap.graph.annotations.GraphType;\r
@@ -26,6 +25,7 @@ import org.simantics.objmap.graph.annotations.HasCollectionAdder;
 import org.simantics.objmap.graph.annotations.HasCollectionRemover;\r
 import org.simantics.objmap.graph.annotations.HasSetter;\r
 import org.simantics.objmap.graph.annotations.OptionalRelatedElements;\r
+import org.simantics.objmap.graph.annotations.OrderedSetType;\r
 import org.simantics.objmap.graph.annotations.RelatedElements;\r
 import org.simantics.objmap.graph.annotations.RelatedElementsGet;\r
 import org.simantics.objmap.graph.annotations.RelatedGetObj;\r
@@ -33,6 +33,18 @@ import org.simantics.objmap.graph.annotations.RelatedGetValue;
 import org.simantics.objmap.graph.annotations.RelatedOrderedSetElements;\r
 import org.simantics.objmap.graph.annotations.RelatedValue;\r
 import org.simantics.objmap.graph.annotations.UpdateMethod;\r
+import org.simantics.objmap.graph.annotations.meta.IsClassRule;\r
+import org.simantics.objmap.graph.annotations.meta.IsCollectionRule;\r
+import org.simantics.objmap.graph.annotations.meta.IsFieldRule;\r
+import org.simantics.objmap.graph.annotations.meta.IsGetSetRule;\r
+import org.simantics.objmap.graph.annotations.meta.IsMethodRule;\r
+import org.simantics.objmap.graph.rules.factory.IClassRuleFactory;\r
+import org.simantics.objmap.graph.rules.factory.ICollectionRuleFactory;\r
+import org.simantics.objmap.graph.rules.factory.IFieldRuleFactory;\r
+import org.simantics.objmap.graph.rules.factory.IGetSetRuleFactory;\r
+import org.simantics.objmap.graph.rules.factory.IMethodRuleFactory;\r
+import org.simantics.objmap.structural.IStructuralObject;\r
+import org.simantics.objmap.structural.StructuralResource;\r
 import org.simantics.objmap.structural.annotations.StructuralRelatedElementsGet;\r
 import org.simantics.objmap.structural.annotations.StructuralRelatedGetObj;\r
 import org.simantics.objmap.structural.annotations.TypeRelatedElementsGet;\r
@@ -51,18 +63,6 @@ import org.simantics.objmap.structural.annotations.factories.TypeRelatedElements
 import org.simantics.objmap.structural.annotations.factories.TypeRelatedGetSetObjRuleFactory;\r
 import org.simantics.objmap.structural.annotations.factories.TypeRelatedGetSetValueRuleFactory;\r
 import org.simantics.objmap.structural.annotations.factories.UpdateMethodFactory;\r
-import org.simantics.objmap.graph.annotations.meta.IsClassRule;\r
-import org.simantics.objmap.graph.annotations.meta.IsCollectionRule;\r
-import org.simantics.objmap.graph.annotations.meta.IsFieldRule;\r
-import org.simantics.objmap.graph.annotations.meta.IsGetSetRule;\r
-import org.simantics.objmap.graph.annotations.meta.IsMethodRule;\r
-import org.simantics.objmap.graph.rules.factory.IClassRuleFactory;\r
-import org.simantics.objmap.graph.rules.factory.ICollectionRuleFactory;\r
-import org.simantics.objmap.graph.rules.factory.IFieldRuleFactory;\r
-import org.simantics.objmap.graph.rules.factory.IGetSetRuleFactory;\r
-import org.simantics.objmap.graph.rules.factory.IMethodRuleFactory;\r
-import org.simantics.objmap.structural.IStructuralObject;\r
-import org.simantics.objmap.structural.StructuralResource;\r
 \r
 \r
 public class MappingSchemas {\r
@@ -76,12 +76,20 @@ public class MappingSchemas {
        public static SimpleLinkType fromAnnotations(ReadGraph g, Class<?> clazz) throws DatabaseException, InstantiationException, IllegalAccessException {\r
            GraphType graphType = clazz.getAnnotation(GraphType.class);\r
            \r
-           ArrayList<IBidirectionalMappingRule<StructuralResource, IStructuralObject>> rules = new ArrayList<IBidirectionalMappingRule<StructuralResource, IStructuralObject>>();\r
-           collectRulesFromAnnotations(g, clazz, rules);\r
-           \r
-           return new SimpleLinkType(\r
-                   g.getResource(graphType.value()), \r
-                clazz, rules);    \r
+           if (graphType != null) {\r
+                   ArrayList<IBidirectionalMappingRule<StructuralResource, IStructuralObject>> rules = new ArrayList<IBidirectionalMappingRule<StructuralResource, IStructuralObject>>();\r
+                   collectRulesFromAnnotations(g, clazz, rules);\r
+                   \r
+                   return new SimpleLinkType(g.getResource(graphType.value()), clazz, rules);    \r
+           }\r
+           OrderedSetType orderedSetType = clazz.getAnnotation(OrderedSetType.class);\r
+           if (orderedSetType != null) {\r
+               ArrayList<IBidirectionalMappingRule<StructuralResource, IStructuralObject>> rules = new ArrayList<IBidirectionalMappingRule<StructuralResource, IStructuralObject>>();\r
+                   collectRulesFromAnnotations(g, clazz, rules);\r
+                   \r
+                   return new OrderedSetSimpleLinkType(g.getResource(orderedSetType.value()), clazz, rules);\r
+           }\r
+           throw new IllegalArgumentException("Class " + clazz.toString() + " does not contain annotations.");\r
        }\r
        \r
        public static void collectRulesFromAnnotations(ReadGraph g, Class<?> clazz, Collection<IBidirectionalMappingRule<StructuralResource, IStructuralObject>> rules) throws DatabaseException, InstantiationException, IllegalAccessException {\r
diff --git a/org.simantics.objmap2/src/org/simantics/objmap/structural/schema/OrderedSetSimpleLinkType.java b/org.simantics.objmap2/src/org/simantics/objmap/structural/schema/OrderedSetSimpleLinkType.java
new file mode 100644 (file)
index 0000000..6fd6a01
--- /dev/null
@@ -0,0 +1,28 @@
+package org.simantics.objmap.structural.schema;\r
+\r
+import java.util.ArrayList;\r
+\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.utils.OrderedSetUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.objmap.bidirectional.IBidirectionalMappingRule;\r
+import org.simantics.objmap.structural.IStructuralObject;\r
+import org.simantics.objmap.structural.StructuralResource;\r
+\r
+public class OrderedSetSimpleLinkType extends SimpleLinkType {\r
+\r
+       public OrderedSetSimpleLinkType(Resource domainType, Class<?> rangeType, ArrayList<IBidirectionalMappingRule<StructuralResource, IStructuralObject>> rules) {\r
+               super(domainType, rangeType, rules);\r
+       }\r
+\r
+       public OrderedSetSimpleLinkType(Resource domainType, Class<?> rangeType) {\r
+               super(domainType, rangeType);\r
+       }\r
+       \r
+       @Override\r
+       protected Resource newResource(WriteGraph g, Resource type) throws DatabaseException {\r
+               return OrderedSetUtils.create(g, type);\r
+       }\r
+\r
+}\r
index eb9d57c4c498f88eaa32807c62e56e10b7ca2475..7ad6b6d072060c1b8b93ef7e28265f537e961eaa 100644 (file)
@@ -69,15 +69,13 @@ public class SimpleLinkType implements ILinkType<StructuralResource,IStructuralO
                 );\r
             if (rangeElement.getContext().size() == 0) {\r
                // there is no context, this not a structural resource / object.\r
-               Resource result = g.newResource();\r
-               g.claim(result, Layer0.getInstance(g).InstanceOf, null, domainType);\r
+               Resource result = newResource(g, domainType);\r
                return new StructuralResource(g,result);\r
             } else {\r
                if (rangeElement.getContext().size() == 1 && rangeElement.getContext().get(0).equals(rangeElement)) {\r
                        // Structural object's context is itself, we are instantiating a new structural model.\r
                        Resource type = rangeElement.getType();\r
-                       Resource result = g.newResource();\r
-                       g.claim(result, Layer0.getInstance(g).InstanceOf, null, type);\r
+                       Resource result = newResource(g, type);\r
                        return new StructuralResource(g,result,result);\r
                } else {\r
                        // Structural object's context is not itself, which means that the object is inside of a structural model.\r
@@ -89,6 +87,12 @@ public class SimpleLinkType implements ILinkType<StructuralResource,IStructuralO
             throw new MappingException(e);\r
         }\r
     }\r
+    \r
+    protected Resource newResource(WriteGraph g, Resource type) throws DatabaseException {\r
+       Resource result = g.newResource();\r
+       g.claim(result, Layer0.getInstance(g).InstanceOf, null, type);\r
+       return result;\r
+    }\r
     @Override\r
     public IStructuralObject createRangeElement(ReadGraph g, StructuralResource domainElement)\r
             throws MappingException {\r