1 /*******************************************************************************
\r
2 * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.objmap.graph.schema;
\r
14 import java.lang.annotation.Annotation;
\r
15 import java.lang.reflect.Field;
\r
16 import java.lang.reflect.Method;
\r
17 import java.util.ArrayList;
\r
18 import java.util.Collection;
\r
20 import org.simantics.db.ReadGraph;
\r
21 import org.simantics.db.Resource;
\r
22 import org.simantics.db.exception.DatabaseException;
\r
23 import org.simantics.objmap.bidirectional.IBidirectionalMappingRule;
\r
24 import org.simantics.objmap.graph.annotations.CompoundRelatedGetValue;
\r
25 import org.simantics.objmap.graph.annotations.DynamicGraphType;
\r
26 import org.simantics.objmap.graph.annotations.GraphType;
\r
27 import org.simantics.objmap.graph.annotations.HasCollectionAdder;
\r
28 import org.simantics.objmap.graph.annotations.HasCollectionRemover;
\r
29 import org.simantics.objmap.graph.annotations.HasSetter;
\r
30 import org.simantics.objmap.graph.annotations.OptionalRelatedElements;
\r
31 import org.simantics.objmap.graph.annotations.RelatedElements;
\r
32 import org.simantics.objmap.graph.annotations.RelatedElementsGet;
\r
33 import org.simantics.objmap.graph.annotations.RelatedGetObj;
\r
34 import org.simantics.objmap.graph.annotations.RelatedGetValue;
\r
35 import org.simantics.objmap.graph.annotations.RelatedOrderedSetElements;
\r
36 import org.simantics.objmap.graph.annotations.RelatedValue;
\r
37 import org.simantics.objmap.graph.annotations.UpdateMethod;
\r
38 import org.simantics.objmap.graph.annotations.factories.CompoundRelatedGetSetValueRuleFactory;
\r
39 import org.simantics.objmap.graph.annotations.factories.OptionalRelatedElementsRuleFactory;
\r
40 import org.simantics.objmap.graph.annotations.factories.RelatedElementsRuleFactory;
\r
41 import org.simantics.objmap.graph.annotations.factories.RelatedElementsRuleFactory2;
\r
42 import org.simantics.objmap.graph.annotations.factories.RelatedGetSetObjRuleFactory;
\r
43 import org.simantics.objmap.graph.annotations.factories.RelatedGetSetValueRuleFactory;
\r
44 import org.simantics.objmap.graph.annotations.factories.RelatedOrderedSetElementsRuleFactory;
\r
45 import org.simantics.objmap.graph.annotations.factories.RelatedValueRuleFactory;
\r
46 import org.simantics.objmap.graph.annotations.factories.UpdateMethodFactory;
\r
47 import org.simantics.objmap.graph.annotations.meta.IsClassRule;
\r
48 import org.simantics.objmap.graph.annotations.meta.IsCollectionRule;
\r
49 import org.simantics.objmap.graph.annotations.meta.IsFieldRule;
\r
50 import org.simantics.objmap.graph.annotations.meta.IsGetSetRule;
\r
51 import org.simantics.objmap.graph.annotations.meta.IsMethodRule;
\r
52 import org.simantics.objmap.graph.rules.factory.IClassRuleFactory;
\r
53 import org.simantics.objmap.graph.rules.factory.ICollectionRuleFactory;
\r
54 import org.simantics.objmap.graph.rules.factory.IFieldRuleFactory;
\r
55 import org.simantics.objmap.graph.rules.factory.IGetSetRuleFactory;
\r
56 import org.simantics.objmap.graph.rules.factory.IMethodRuleFactory;
\r
59 public class MappingSchemas {
\r
61 * Creates a new SimpleLinkType based on the annotations in the given class.
\r
62 * @throws IllegalAccessException
\r
63 * @throws InstantiationException
\r
67 public static SimpleLinkType<Object> fromAnnotations(ReadGraph g, Class<?> clazz) throws DatabaseException, InstantiationException, IllegalAccessException {
\r
68 GraphType graphType = clazz.getAnnotation(GraphType.class);
\r
70 if (graphType != null) {
\r
71 ArrayList<IBidirectionalMappingRule<Resource, Object>> rules = new ArrayList<IBidirectionalMappingRule<Resource, Object>>();
\r
72 collectRulesFromAnnotations(g, clazz, rules);
\r
74 return new SimpleLinkType<Object>(g.getResource(graphType.value()), clazz, rules);
\r
76 DynamicGraphType dynamicType = clazz.getAnnotation(DynamicGraphType.class);
\r
77 if (dynamicType != null) {
\r
78 ArrayList<IBidirectionalMappingRule<Resource, Object>> rules = new ArrayList<IBidirectionalMappingRule<Resource, Object>>();
\r
79 collectRulesFromAnnotations(g, clazz, rules);
\r
81 return new DynamicSimpleLinkType<Object>(g.getResource(dynamicType.value()), clazz, rules);
\r
83 throw new IllegalArgumentException("Class " + clazz.toString() + " does not contain annotations.");
\r
86 public static void collectRulesFromAnnotations(ReadGraph g, Class<?> clazz, Collection<IBidirectionalMappingRule<Resource, Object>> rules) throws DatabaseException, InstantiationException, IllegalAccessException {
\r
87 Class<?> superclass = clazz.getSuperclass();
\r
88 if(superclass != null)
\r
89 collectRulesFromAnnotations(g, superclass, rules);
\r
91 for(Annotation annotation : clazz.getAnnotations()) {
\r
93 IsClassRule tag = annotation.annotationType().getAnnotation(IsClassRule.class);
\r
95 rules.add(createClassRule(g, annotation, clazz).create(g, annotation, clazz));
\r
99 for(Field f : clazz.getDeclaredFields()) {
\r
100 f.setAccessible(true);
\r
102 for(Annotation annotation : f.getAnnotations()) {
\r
104 IsFieldRule tag = annotation.annotationType().getAnnotation(IsFieldRule.class);
\r
106 rules.add(createFieldRule(g, annotation, f).create(g, annotation, f));
\r
111 for(Method m : clazz.getDeclaredMethods()) {
\r
112 m.setAccessible(true);
\r
114 for(Annotation annotation : m.getAnnotations()) {
\r
115 IsMethodRule tag =
\r
116 annotation.annotationType().getAnnotation(IsMethodRule.class);
\r
118 rules.add(createMethodRule(g, annotation, m).create(g, annotation, m));
\r
123 for (Method m : clazz.getDeclaredMethods()) {
\r
124 m.setAccessible(true);
\r
125 for (Annotation annotation : m.getAnnotations()) {
\r
126 Class<? extends Annotation> annotationType = annotation.annotationType();
\r
128 IsGetSetRule tag =
\r
129 annotationType.getAnnotation(IsGetSetRule.class);
\r
132 HasSetter setterAnnType = annotationType.getAnnotation(HasSetter.class);
\r
134 Class<? extends Annotation> setterAnn = setterAnnType.value();
\r
138 IGetSetRuleFactory<Resource,Object> ruleFactory = createGetSetRuleFactory(g, annotation, getter);
\r
141 Method setter = null;
\r
143 for (Method m2 : clazz.getDeclaredMethods()) {
\r
144 Annotation set = m2.getAnnotation(setterAnn);
\r
145 if (set != null && ruleFactory.isSetter(annotation, set))
\r
149 rules.add(ruleFactory.create(g, annotation, getter, setter));
\r
155 for (Method m : clazz.getDeclaredMethods()) {
\r
156 m.setAccessible(true);
\r
157 for (Annotation annotation : m.getAnnotations()) {
\r
158 Class<? extends Annotation> annotationType = annotation.annotationType();
\r
160 IsCollectionRule tag =
\r
161 annotationType.getAnnotation(IsCollectionRule.class);
\r
164 HasCollectionAdder adderAnnType = annotationType.getAnnotation(HasCollectionAdder.class);
\r
165 HasCollectionRemover removerAnnType = annotationType.getAnnotation(HasCollectionRemover.class);
\r
167 Class<? extends Annotation> adderAnn = adderAnnType.value();
\r
168 Class<? extends Annotation> removerAnn = removerAnnType.value();
\r
172 ICollectionRuleFactory<Resource,Object> ruleFactory = createCollectionRuleFactory(g, annotation, getter);
\r
175 Method adder = null;
\r
176 Method remover = null;
\r
178 for (Method m2 : clazz.getDeclaredMethods()) {
\r
179 Annotation add = m2.getAnnotation(adderAnn);
\r
180 Annotation rem = m2.getAnnotation(removerAnn);
\r
181 if (add != null && ruleFactory.isAdder(annotation, add))
\r
183 if (rem != null && ruleFactory.isRemover(annotation, rem))
\r
189 rules.add(ruleFactory.create(g, annotation, getter,adder,remover));
\r
196 public static IClassRuleFactory<Resource, Object> createClassRule(ReadGraph g, Annotation annotation, Class<?> clazz) {
\r
200 public static IFieldRuleFactory<Resource,Object> createFieldRule(ReadGraph g, Annotation annotation, Field field) {
\r
201 if (annotation.annotationType().equals(RelatedElements.class))
\r
202 return new RelatedElementsRuleFactory<Object>();
\r
203 if (annotation.annotationType().equals(RelatedValue.class))
\r
204 return new RelatedValueRuleFactory<Object>();
\r
205 if (annotation.annotationType().equals(OptionalRelatedElements.class))
\r
206 return new OptionalRelatedElementsRuleFactory<Object>();
\r
207 if (annotation.annotationType().equals(RelatedOrderedSetElements.class))
\r
208 return new RelatedOrderedSetElementsRuleFactory<Object>();
\r
212 public static IMethodRuleFactory<Resource, Object> createMethodRule(ReadGraph g, Annotation annotation, Method m) {
\r
213 if (annotation.annotationType().equals(UpdateMethod.class))
\r
214 return new UpdateMethodFactory<Resource,Object>();
\r
218 public static IGetSetRuleFactory<Resource,Object> createGetSetRuleFactory(ReadGraph g, Annotation annotation, Method getter) {
\r
219 if (annotation.annotationType().equals(RelatedGetValue.class))
\r
220 return new RelatedGetSetValueRuleFactory<Object>();
\r
221 if (annotation.annotationType().equals(RelatedGetObj.class))
\r
222 return new RelatedGetSetObjRuleFactory<Object>();
\r
223 if (annotation.annotationType().equals(CompoundRelatedGetValue.class))
\r
224 return new CompoundRelatedGetSetValueRuleFactory<Object>();
\r
228 public static ICollectionRuleFactory<Resource,Object> createCollectionRuleFactory(ReadGraph g, Annotation annotation, Method getter) {
\r
229 if (annotation.annotationType().equals(RelatedElementsGet.class))
\r
230 return new RelatedElementsRuleFactory2<Object>();
\r
235 * Creates a new SimpleLinkType based on the annotations in the given class.
\r
236 * @throws IllegalAccessException
\r
237 * @throws InstantiationException
\r
239 * @see RelatedValue
\r
242 public static AdaptedLinkType<Object> fromAdaptable(ReadGraph g, String type, Class<?> clazz) throws DatabaseException, InstantiationException, IllegalAccessException {
\r
245 return new AdaptedLinkType<Object>(g.getResource(type), clazz);
\r