]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/MappingSchemas.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.objmap2 / src / org / simantics / objmap / graph / schema / MappingSchemas.java
diff --git a/bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/MappingSchemas.java b/bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/MappingSchemas.java
new file mode 100644 (file)
index 0000000..b863fba
--- /dev/null
@@ -0,0 +1,261 @@
+/*******************************************************************************\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.schema;\r
+\r
+import java.lang.annotation.Annotation;\r
+import java.lang.reflect.Field;\r
+import java.lang.reflect.Method;\r
+import java.util.ArrayList;\r
+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.CompoundRelatedGetValue;\r
+import org.simantics.objmap.graph.annotations.DynamicGraphType;\r
+import org.simantics.objmap.graph.annotations.GraphType;\r
+import org.simantics.objmap.graph.annotations.HasCollectionAdder;\r
+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.OrderedElementsGet;\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
+import org.simantics.objmap.graph.annotations.RelatedGetValue;\r
+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.factories.CompoundRelatedGetSetValueRuleFactory;\r
+import org.simantics.objmap.graph.annotations.factories.OptionalRelatedElementsRuleFactory;\r
+import org.simantics.objmap.graph.annotations.factories.OrderedElementsRuleFactory;\r
+import org.simantics.objmap.graph.annotations.factories.RelatedElementsRuleFactory;\r
+import org.simantics.objmap.graph.annotations.factories.RelatedElementsRuleFactory2;\r
+import org.simantics.objmap.graph.annotations.factories.RelatedGetSetObjRuleFactory;\r
+import org.simantics.objmap.graph.annotations.factories.RelatedGetSetValueRuleFactory;\r
+import org.simantics.objmap.graph.annotations.factories.RelatedOrderedSetElementsRuleFactory;\r
+import org.simantics.objmap.graph.annotations.factories.RelatedValueRuleFactory;\r
+import org.simantics.objmap.graph.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
+\r
+\r
+public class MappingSchemas {\r
+       /**\r
+     * Creates a new SimpleLinkType based on the annotations in the given class.\r
+     * @throws IllegalAccessException \r
+     * @throws InstantiationException \r
+     * @see GraphType\r
+     * @see RelatedValue\r
+     */\r
+       public static SimpleLinkType<Object> fromAnnotations(ReadGraph g, Class<?> clazz) throws DatabaseException, InstantiationException, IllegalAccessException {\r
+           GraphType graphType = clazz.getAnnotation(GraphType.class);\r
+           \r
+           if (graphType != null) {\r
+                   ArrayList<IBidirectionalMappingRule<Resource, Object>> rules = new ArrayList<IBidirectionalMappingRule<Resource, Object>>();\r
+                   collectRulesFromAnnotations(g, clazz, rules);\r
+                   \r
+                   return new SimpleLinkType<Object>(g.getResource(graphType.value()), clazz, rules);  \r
+           }\r
+           DynamicGraphType dynamicType = clazz.getAnnotation(DynamicGraphType.class);\r
+           if (dynamicType != null) {\r
+               ArrayList<IBidirectionalMappingRule<Resource, Object>> rules = new ArrayList<IBidirectionalMappingRule<Resource, Object>>();\r
+                   collectRulesFromAnnotations(g, clazz, rules);\r
+                   \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
+       public static void collectRulesFromAnnotations(ReadGraph g, Class<?> clazz, Collection<IBidirectionalMappingRule<Resource, Object>> rules) throws DatabaseException, InstantiationException, IllegalAccessException {\r
+           Class<?> superclass = clazz.getSuperclass();\r
+           if(superclass != null)\r
+               collectRulesFromAnnotations(g, superclass, rules);\r
+               \r
+        for(Annotation annotation : clazz.getAnnotations()) {\r
+\r
+               IsClassRule tag = annotation.annotationType().getAnnotation(IsClassRule.class);\r
+            if(tag!= null) {\r
+               rules.add(createClassRule(g, annotation, clazz).create(g, annotation, clazz));\r
+            }\r
+        }\r
+\r
+        for(Field f : clazz.getDeclaredFields()) {\r
+            f.setAccessible(true);\r
+\r
+            for(Annotation annotation : f.getAnnotations()) {\r
+\r
+               IsFieldRule tag = annotation.annotationType().getAnnotation(IsFieldRule.class);\r
+                if(tag != null) {\r
+                    rules.add(createFieldRule(g, annotation, f).create(g, annotation, f));\r
+                }\r
+            }\r
+        }\r
+\r
+        for(Method m : clazz.getDeclaredMethods()) {\r
+            m.setAccessible(true);\r
+\r
+            for(Annotation annotation : m.getAnnotations()) {\r
+               IsMethodRule tag = \r
+                        annotation.annotationType().getAnnotation(IsMethodRule.class);\r
+                if(tag != null) {\r
+                       rules.add(createMethodRule(g, annotation, m).create(g, annotation, m));\r
+                }\r
+            }\r
+        }\r
+        \r
+        for (Method m : clazz.getDeclaredMethods()) {\r
+               m.setAccessible(true);\r
+               for (Annotation annotation : m.getAnnotations()) {\r
+                       Class<? extends Annotation> annotationType = annotation.annotationType();\r
+\r
+                        IsGetSetRule tag = \r
+                         annotationType.getAnnotation(IsGetSetRule.class);\r
+                        if (tag != null) {\r
+                                \r
+                                HasSetter setterAnnType = annotationType.getAnnotation(HasSetter.class);\r
+                                \r
+                                Class<? extends Annotation> setterAnn = setterAnnType.value();\r
+                                \r
+                                Method getter = m;\r
+                                \r
+                                IGetSetRuleFactory<Resource,Object> ruleFactory = createGetSetRuleFactory(g, annotation, getter);\r
+                                \r
+                                \r
+                                Method setter = null;\r
+                                \r
+                                for (Method m2 : clazz.getDeclaredMethods()) {\r
+                                        Annotation set = m2.getAnnotation(setterAnn);\r
+                                        if (set != null && ruleFactory.isSetter(annotation, set))\r
+                                                setter = m2;\r
+                                }\r
+\r
+                                rules.add(ruleFactory.create(g, annotation, getter, setter));\r
+                        }\r
+                \r
+               }\r
+        }\r
+        \r
+        for (Method m : clazz.getDeclaredMethods()) {\r
+               m.setAccessible(true);\r
+               for (Annotation annotation : m.getAnnotations()) {\r
+                       Class<? extends Annotation> annotationType = annotation.annotationType();\r
+\r
+                        IsCollectionRule tag = \r
+                         annotationType.getAnnotation(IsCollectionRule.class);\r
+                        if (tag != null) {\r
+                                \r
+                                HasCollectionAdder adderAnnType = annotationType.getAnnotation(HasCollectionAdder.class);\r
+                                HasCollectionRemover removerAnnType = annotationType.getAnnotation(HasCollectionRemover.class);\r
+                \r
+                                Class<? extends Annotation> adderAnn = adderAnnType.value();\r
+                                Class<? extends Annotation> removerAnn = removerAnnType.value();\r
+                                \r
+                                Method getter = m;\r
+                                \r
+                                ICollectionRuleFactory<Resource,Object> ruleFactory = createCollectionRuleFactory(g, annotation, getter);\r
+                                \r
+                                \r
+                                Method adder = null;\r
+                                Method remover = null;\r
+                                \r
+                                for (Method m2 : clazz.getDeclaredMethods()) {\r
+                                        Annotation add = m2.getAnnotation(adderAnn);\r
+                                        Annotation rem = m2.getAnnotation(removerAnn);\r
+                                        if (add != null && ruleFactory.isAdder(annotation, add))\r
+                                                adder = m2;\r
+                                        if (rem != null && ruleFactory.isRemover(annotation, rem))\r
+                                                remover = m2;\r
+                                }\r
+                                \r
+                                \r
+                                \r
+                                rules.add(ruleFactory.create(g, annotation, getter,adder,remover));\r
+                        }\r
+                \r
+               }\r
+        }\r
+    }\r
+       \r
+       public static IClassRuleFactory<Resource, Object> createClassRule(ReadGraph g, Annotation annotation, Class<?> clazz) {\r
+               return null;\r
+       }\r
+       \r
+       public static IFieldRuleFactory<Resource,Object> createFieldRule(ReadGraph g, Annotation annotation, Field field) {\r
+               if (annotation.annotationType().equals(RelatedElements.class))\r
+                       return new RelatedElementsRuleFactory<Object>();\r
+               if (annotation.annotationType().equals(RelatedValue.class))\r
+                       return new RelatedValueRuleFactory<Object>();\r
+               if (annotation.annotationType().equals(OptionalRelatedElements.class))\r
+                       return new OptionalRelatedElementsRuleFactory<Object>();\r
+               if (annotation.annotationType().equals(RelatedOrderedSetElements.class))\r
+                       return new RelatedOrderedSetElementsRuleFactory<Object>();\r
+               return null;\r
+       }\r
+       \r
+       public static IMethodRuleFactory<Resource, Object> createMethodRule(ReadGraph g, Annotation annotation, Method m) {\r
+               if (annotation.annotationType().equals(UpdateMethod.class))\r
+                       return new UpdateMethodFactory<Resource,Object>();\r
+               return null;\r
+       }\r
+       \r
+       public static IGetSetRuleFactory<Resource,Object> createGetSetRuleFactory(ReadGraph g, Annotation annotation, Method getter) {\r
+               if (annotation.annotationType().equals(RelatedGetValue.class))\r
+                       return new RelatedGetSetValueRuleFactory<Object>();\r
+               if (annotation.annotationType().equals(RelatedGetObj.class))\r
+                       return new RelatedGetSetObjRuleFactory<Object>();\r
+               if (annotation.annotationType().equals(CompoundRelatedGetValue.class))\r
+                       return new CompoundRelatedGetSetValueRuleFactory<Object>();\r
+               return null;\r
+       }\r
+       \r
+       public static ICollectionRuleFactory<Resource,Object> createCollectionRuleFactory(ReadGraph g, Annotation annotation, Method getter) {\r
+               if (annotation.annotationType().equals(RelatedElementsGet.class))\r
+                       return new RelatedElementsRuleFactory2<Object>();\r
+               if (annotation.annotationType().equals(OrderedElementsGet.class))\r
+                       return new OrderedElementsRuleFactory<Object>();\r
+               return null;\r
+       }\r
+       \r
+       /**\r
+     * Creates a new SimpleLinkType based on the annotations in the given class.\r
+     * @throws IllegalAccessException \r
+     * @throws InstantiationException \r
+     * @see GraphType\r
+     * @see RelatedValue\r
+     */\r
+       \r
+       public static AdaptedLinkType<Object> fromAdaptable(ReadGraph g, String type, Class<?> clazz) throws DatabaseException, InstantiationException, IllegalAccessException {\r
+           \r
+           \r
+           return new AdaptedLinkType<Object>(g.getResource(type), clazz);    \r
+       }\r
+       \r
+       \r
+}\r