--- /dev/null
+package org.simantics.objmap.structural.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.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.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.structural.annotations.StructuralRelatedElementsGet;\r
+import org.simantics.objmap.structural.annotations.StructuralRelatedGetObj;\r
+import org.simantics.objmap.structural.annotations.TypeRelatedElementsGet;\r
+import org.simantics.objmap.structural.annotations.TypeRelatedGetObj;\r
+import org.simantics.objmap.structural.annotations.TypeRelatedGetValue;\r
+import org.simantics.objmap.structural.annotations.factories.OptionalRelatedElementsRuleFactory;\r
+import org.simantics.objmap.structural.annotations.factories.RelatedElementsRuleFactory;\r
+import org.simantics.objmap.structural.annotations.factories.RelatedElementsRuleFactory2;\r
+import org.simantics.objmap.structural.annotations.factories.RelatedGetSetObjRuleFactory;\r
+import org.simantics.objmap.structural.annotations.factories.RelatedGetSetValueRuleFactory;\r
+import org.simantics.objmap.structural.annotations.factories.RelatedOrderedSetElementsRuleFactory;\r
+import org.simantics.objmap.structural.annotations.factories.RelatedValueRuleFactory;\r
+import org.simantics.objmap.structural.annotations.factories.StructuralRelatedElementsRuleFactory2;\r
+import org.simantics.objmap.structural.annotations.factories.StructuralRelatedGetSetObjRuleFactory;\r
+import org.simantics.objmap.structural.annotations.factories.TypeRelatedElementsRuleFactory2;\r
+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
+ /**\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 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
+ }\r
+ \r
+ public static void collectRulesFromAnnotations(ReadGraph g, Class<?> clazz, Collection<IBidirectionalMappingRule<StructuralResource, IStructuralObject>> 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<StructuralResource, IStructuralObject> 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<StructuralResource, IStructuralObject> 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<StructuralResource, IStructuralObject> createClassRule(ReadGraph g, Annotation annotation, Class<?> clazz) {\r
+ return null;\r
+ }\r
+ \r
+ public static IFieldRuleFactory<StructuralResource, IStructuralObject> createFieldRule(ReadGraph g, Annotation annotation, Field field) {\r
+ if (annotation.annotationType().equals(RelatedElements.class))\r
+ return new RelatedElementsRuleFactory<IStructuralObject>();\r
+ if (annotation.annotationType().equals(RelatedValue.class))\r
+ return new RelatedValueRuleFactory<IStructuralObject>();\r
+ if (annotation.annotationType().equals(OptionalRelatedElements.class))\r
+ return new OptionalRelatedElementsRuleFactory<IStructuralObject>();\r
+ if (annotation.annotationType().equals(RelatedOrderedSetElements.class))\r
+ return new RelatedOrderedSetElementsRuleFactory<IStructuralObject>();\r
+ return null;\r
+ }\r
+ \r
+ public static IMethodRuleFactory<StructuralResource, IStructuralObject> createMethodRule(ReadGraph g, Annotation annotation, Method m) {\r
+ if (annotation.annotationType().equals(UpdateMethod.class))\r
+ return new UpdateMethodFactory<StructuralResource,IStructuralObject>();\r
+ return null;\r
+ }\r
+ \r
+ public static IGetSetRuleFactory<StructuralResource, IStructuralObject> createGetSetRuleFactory(ReadGraph g, Annotation annotation, Method getter) {\r
+ if (annotation.annotationType().equals(RelatedGetValue.class))\r
+ return new RelatedGetSetValueRuleFactory<IStructuralObject>();\r
+ if (annotation.annotationType().equals(RelatedGetObj.class))\r
+ return new RelatedGetSetObjRuleFactory<IStructuralObject>();\r
+ if (annotation.annotationType().equals(TypeRelatedGetValue.class))\r
+ return new TypeRelatedGetSetValueRuleFactory<IStructuralObject>();\r
+ if (annotation.annotationType().equals(TypeRelatedGetObj.class))\r
+ return new TypeRelatedGetSetObjRuleFactory<IStructuralObject>();\r
+ if (annotation.annotationType().equals(StructuralRelatedGetObj.class))\r
+ return new StructuralRelatedGetSetObjRuleFactory<IStructuralObject>();\r
+ return null;\r
+ }\r
+ \r
+ public static ICollectionRuleFactory<StructuralResource, IStructuralObject> createCollectionRuleFactory(ReadGraph g, Annotation annotation, Method getter) {\r
+ if (annotation.annotationType().equals(RelatedElementsGet.class))\r
+ return new RelatedElementsRuleFactory2<IStructuralObject>();\r
+ if (annotation.annotationType().equals(TypeRelatedElementsGet.class))\r
+ return new TypeRelatedElementsRuleFactory2<IStructuralObject>();\r
+ if (annotation.annotationType().equals(StructuralRelatedElementsGet.class))\r
+ return new StructuralRelatedElementsRuleFactory2<IStructuralObject>();\r
+ return null;\r
+ }\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
+ public static AdaptedLinkType fromAdaptable(ReadGraph g, String type, Class<?> clazz) throws DatabaseException, InstantiationException, IllegalAccessException {\r
+ \r
+ \r
+ return new AdaptedLinkType(g.getResource(type), clazz); \r
+ }\r
+ \r
+ \r
+}\r