1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.objmap.graph.schema;
14 import java.lang.annotation.Annotation;
15 import java.lang.reflect.Field;
16 import java.lang.reflect.Method;
17 import java.util.ArrayList;
18 import java.util.Collection;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.objmap.bidirectional.IBidirectionalMappingRule;
24 import org.simantics.objmap.graph.annotations.CompoundRelatedGetValue;
25 import org.simantics.objmap.graph.annotations.DynamicGraphType;
26 import org.simantics.objmap.graph.annotations.GraphType;
27 import org.simantics.objmap.graph.annotations.HasCollectionAdder;
28 import org.simantics.objmap.graph.annotations.HasCollectionRemover;
29 import org.simantics.objmap.graph.annotations.HasSetter;
30 import org.simantics.objmap.graph.annotations.OptionalRelatedElements;
31 import org.simantics.objmap.graph.annotations.OrderedElementsGet;
32 import org.simantics.objmap.graph.annotations.OrderedSetType;
33 import org.simantics.objmap.graph.annotations.RelatedElements;
34 import org.simantics.objmap.graph.annotations.RelatedElementsGet;
35 import org.simantics.objmap.graph.annotations.RelatedGetObj;
36 import org.simantics.objmap.graph.annotations.RelatedGetValue;
37 import org.simantics.objmap.graph.annotations.RelatedOrderedSetElements;
38 import org.simantics.objmap.graph.annotations.RelatedValue;
39 import org.simantics.objmap.graph.annotations.UpdateMethod;
40 import org.simantics.objmap.graph.annotations.factories.CompoundRelatedGetSetValueRuleFactory;
41 import org.simantics.objmap.graph.annotations.factories.OptionalRelatedElementsRuleFactory;
42 import org.simantics.objmap.graph.annotations.factories.OrderedElementsRuleFactory;
43 import org.simantics.objmap.graph.annotations.factories.RelatedElementsRuleFactory;
44 import org.simantics.objmap.graph.annotations.factories.RelatedElementsRuleFactory2;
45 import org.simantics.objmap.graph.annotations.factories.RelatedGetSetObjRuleFactory;
46 import org.simantics.objmap.graph.annotations.factories.RelatedGetSetValueRuleFactory;
47 import org.simantics.objmap.graph.annotations.factories.RelatedOrderedSetElementsRuleFactory;
48 import org.simantics.objmap.graph.annotations.factories.RelatedValueRuleFactory;
49 import org.simantics.objmap.graph.annotations.factories.UpdateMethodFactory;
50 import org.simantics.objmap.graph.annotations.meta.IsClassRule;
51 import org.simantics.objmap.graph.annotations.meta.IsCollectionRule;
52 import org.simantics.objmap.graph.annotations.meta.IsFieldRule;
53 import org.simantics.objmap.graph.annotations.meta.IsGetSetRule;
54 import org.simantics.objmap.graph.annotations.meta.IsMethodRule;
55 import org.simantics.objmap.graph.rules.factory.IClassRuleFactory;
56 import org.simantics.objmap.graph.rules.factory.ICollectionRuleFactory;
57 import org.simantics.objmap.graph.rules.factory.IFieldRuleFactory;
58 import org.simantics.objmap.graph.rules.factory.IGetSetRuleFactory;
59 import org.simantics.objmap.graph.rules.factory.IMethodRuleFactory;
62 public class MappingSchemas {
64 * Creates a new SimpleLinkType based on the annotations in the given class.
65 * @throws IllegalAccessException
66 * @throws InstantiationException
70 public static SimpleLinkType<Object> fromAnnotations(ReadGraph g, Class<?> clazz) throws DatabaseException, InstantiationException, IllegalAccessException {
71 GraphType graphType = clazz.getAnnotation(GraphType.class);
73 if (graphType != null) {
74 ArrayList<IBidirectionalMappingRule<Resource, Object>> rules = new ArrayList<IBidirectionalMappingRule<Resource, Object>>();
75 collectRulesFromAnnotations(g, clazz, rules);
77 return new SimpleLinkType<Object>(g.getResource(graphType.value()), clazz, rules);
79 DynamicGraphType dynamicType = clazz.getAnnotation(DynamicGraphType.class);
80 if (dynamicType != null) {
81 ArrayList<IBidirectionalMappingRule<Resource, Object>> rules = new ArrayList<IBidirectionalMappingRule<Resource, Object>>();
82 collectRulesFromAnnotations(g, clazz, rules);
84 return new DynamicSimpleLinkType<Object>(g.getResource(dynamicType.value()), clazz, rules);
86 OrderedSetType orderedSetType = clazz.getAnnotation(OrderedSetType.class);
87 if (orderedSetType != null) {
88 ArrayList<IBidirectionalMappingRule<Resource, Object>> rules = new ArrayList<IBidirectionalMappingRule<Resource, Object>>();
89 collectRulesFromAnnotations(g, clazz, rules);
91 return new OrderedSetSimpleLinkType<Object>(g.getResource(orderedSetType.value()), clazz, rules);
93 throw new IllegalArgumentException("Class " + clazz.toString() + " does not contain annotations.");
96 public static void collectRulesFromAnnotations(ReadGraph g, Class<?> clazz, Collection<IBidirectionalMappingRule<Resource, Object>> rules) throws DatabaseException, InstantiationException, IllegalAccessException {
97 Class<?> superclass = clazz.getSuperclass();
98 if(superclass != null)
99 collectRulesFromAnnotations(g, superclass, rules);
101 for(Annotation annotation : clazz.getAnnotations()) {
103 IsClassRule tag = annotation.annotationType().getAnnotation(IsClassRule.class);
105 rules.add(createClassRule(g, annotation, clazz).create(g, annotation, clazz));
109 for(Field f : clazz.getDeclaredFields()) {
110 f.setAccessible(true);
112 for(Annotation annotation : f.getAnnotations()) {
114 IsFieldRule tag = annotation.annotationType().getAnnotation(IsFieldRule.class);
116 rules.add(createFieldRule(g, annotation, f).create(g, annotation, f));
121 for(Method m : clazz.getDeclaredMethods()) {
122 m.setAccessible(true);
124 for(Annotation annotation : m.getAnnotations()) {
126 annotation.annotationType().getAnnotation(IsMethodRule.class);
128 rules.add(createMethodRule(g, annotation, m).create(g, annotation, m));
133 for (Method m : clazz.getDeclaredMethods()) {
134 m.setAccessible(true);
135 for (Annotation annotation : m.getAnnotations()) {
136 Class<? extends Annotation> annotationType = annotation.annotationType();
139 annotationType.getAnnotation(IsGetSetRule.class);
142 HasSetter setterAnnType = annotationType.getAnnotation(HasSetter.class);
144 Class<? extends Annotation> setterAnn = setterAnnType.value();
148 IGetSetRuleFactory<Resource,Object> ruleFactory = createGetSetRuleFactory(g, annotation, getter);
151 Method setter = null;
153 for (Method m2 : clazz.getDeclaredMethods()) {
154 Annotation set = m2.getAnnotation(setterAnn);
155 if (set != null && ruleFactory.isSetter(annotation, set))
159 rules.add(ruleFactory.create(g, annotation, getter, setter));
165 for (Method m : clazz.getDeclaredMethods()) {
166 m.setAccessible(true);
167 for (Annotation annotation : m.getAnnotations()) {
168 Class<? extends Annotation> annotationType = annotation.annotationType();
170 IsCollectionRule tag =
171 annotationType.getAnnotation(IsCollectionRule.class);
174 HasCollectionAdder adderAnnType = annotationType.getAnnotation(HasCollectionAdder.class);
175 HasCollectionRemover removerAnnType = annotationType.getAnnotation(HasCollectionRemover.class);
177 Class<? extends Annotation> adderAnn = adderAnnType.value();
178 Class<? extends Annotation> removerAnn = removerAnnType.value();
182 ICollectionRuleFactory<Resource,Object> ruleFactory = createCollectionRuleFactory(g, annotation, getter);
186 Method remover = null;
188 for (Method m2 : clazz.getDeclaredMethods()) {
189 Annotation add = m2.getAnnotation(adderAnn);
190 Annotation rem = m2.getAnnotation(removerAnn);
191 if (add != null && ruleFactory.isAdder(annotation, add))
193 if (rem != null && ruleFactory.isRemover(annotation, rem))
199 rules.add(ruleFactory.create(g, annotation, getter,adder,remover));
206 public static IClassRuleFactory<Resource, Object> createClassRule(ReadGraph g, Annotation annotation, Class<?> clazz) {
210 public static IFieldRuleFactory<Resource,Object> createFieldRule(ReadGraph g, Annotation annotation, Field field) {
211 if (annotation.annotationType().equals(RelatedElements.class))
212 return new RelatedElementsRuleFactory<Object>();
213 if (annotation.annotationType().equals(RelatedValue.class))
214 return new RelatedValueRuleFactory<Object>();
215 if (annotation.annotationType().equals(OptionalRelatedElements.class))
216 return new OptionalRelatedElementsRuleFactory<Object>();
217 if (annotation.annotationType().equals(RelatedOrderedSetElements.class))
218 return new RelatedOrderedSetElementsRuleFactory<Object>();
222 public static IMethodRuleFactory<Resource, Object> createMethodRule(ReadGraph g, Annotation annotation, Method m) {
223 if (annotation.annotationType().equals(UpdateMethod.class))
224 return new UpdateMethodFactory<Resource,Object>();
228 public static IGetSetRuleFactory<Resource,Object> createGetSetRuleFactory(ReadGraph g, Annotation annotation, Method getter) {
229 if (annotation.annotationType().equals(RelatedGetValue.class))
230 return new RelatedGetSetValueRuleFactory<Object>();
231 if (annotation.annotationType().equals(RelatedGetObj.class))
232 return new RelatedGetSetObjRuleFactory<Object>();
233 if (annotation.annotationType().equals(CompoundRelatedGetValue.class))
234 return new CompoundRelatedGetSetValueRuleFactory<Object>();
238 public static ICollectionRuleFactory<Resource,Object> createCollectionRuleFactory(ReadGraph g, Annotation annotation, Method getter) {
239 if (annotation.annotationType().equals(RelatedElementsGet.class))
240 return new RelatedElementsRuleFactory2<Object>();
241 if (annotation.annotationType().equals(OrderedElementsGet.class))
242 return new OrderedElementsRuleFactory<Object>();
247 * Creates a new SimpleLinkType based on the annotations in the given class.
248 * @throws IllegalAccessException
249 * @throws InstantiationException
254 public static AdaptedLinkType<Object> fromAdaptable(ReadGraph g, String type, Class<?> clazz) throws DatabaseException, InstantiationException, IllegalAccessException {
257 return new AdaptedLinkType<Object>(g.getResource(type), clazz);