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.RelatedElement;
34 import org.simantics.objmap.graph.annotations.RelatedElements;
35 import org.simantics.objmap.graph.annotations.RelatedElementsGet;
36 import org.simantics.objmap.graph.annotations.RelatedGetObj;
37 import org.simantics.objmap.graph.annotations.RelatedGetValue;
38 import org.simantics.objmap.graph.annotations.RelatedOrderedSetElements;
39 import org.simantics.objmap.graph.annotations.RelatedValue;
40 import org.simantics.objmap.graph.annotations.UpdateMethod;
41 import org.simantics.objmap.graph.annotations.factories.CompoundRelatedGetSetValueRuleFactory;
42 import org.simantics.objmap.graph.annotations.factories.OptionalRelatedElementsRuleFactory;
43 import org.simantics.objmap.graph.annotations.factories.OrderedElementsRuleFactory;
44 import org.simantics.objmap.graph.annotations.factories.RelatedElementRuleFactory;
45 import org.simantics.objmap.graph.annotations.factories.RelatedElementsRuleFactory;
46 import org.simantics.objmap.graph.annotations.factories.RelatedElementsRuleFactory2;
47 import org.simantics.objmap.graph.annotations.factories.RelatedGetSetObjRuleFactory;
48 import org.simantics.objmap.graph.annotations.factories.RelatedGetSetValueRuleFactory;
49 import org.simantics.objmap.graph.annotations.factories.RelatedOrderedSetElementsRuleFactory;
50 import org.simantics.objmap.graph.annotations.factories.RelatedValueRuleFactory;
51 import org.simantics.objmap.graph.annotations.factories.UpdateMethodFactory;
52 import org.simantics.objmap.graph.annotations.meta.IsClassRule;
53 import org.simantics.objmap.graph.annotations.meta.IsCollectionRule;
54 import org.simantics.objmap.graph.annotations.meta.IsFieldRule;
55 import org.simantics.objmap.graph.annotations.meta.IsGetSetRule;
56 import org.simantics.objmap.graph.annotations.meta.IsMethodRule;
57 import org.simantics.objmap.graph.rules.factory.IClassRuleFactory;
58 import org.simantics.objmap.graph.rules.factory.ICollectionRuleFactory;
59 import org.simantics.objmap.graph.rules.factory.IFieldRuleFactory;
60 import org.simantics.objmap.graph.rules.factory.IGetSetRuleFactory;
61 import org.simantics.objmap.graph.rules.factory.IMethodRuleFactory;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
66 public class MappingSchemas {
67 private static final Logger LOGGER = LoggerFactory.getLogger(MappingSchemas.class);
70 * Creates a new SimpleLinkType based on the annotations in the given class.
71 * @throws IllegalAccessException
72 * @throws InstantiationException
76 public static SimpleLinkType<Object> fromAnnotations(ReadGraph g, Class<?> clazz) throws DatabaseException, InstantiationException, IllegalAccessException {
77 GraphType graphType = clazz.getAnnotation(GraphType.class);
79 if (graphType != null) {
80 ArrayList<IBidirectionalMappingRule<Resource, Object>> rules = new ArrayList<IBidirectionalMappingRule<Resource, Object>>();
81 collectRulesFromAnnotations(g, clazz, rules);
83 return new SimpleLinkType<Object>(g.getResource(graphType.value()), clazz, rules);
85 DynamicGraphType dynamicType = clazz.getAnnotation(DynamicGraphType.class);
86 if (dynamicType != null) {
87 ArrayList<IBidirectionalMappingRule<Resource, Object>> rules = new ArrayList<IBidirectionalMappingRule<Resource, Object>>();
88 collectRulesFromAnnotations(g, clazz, rules);
90 return new DynamicSimpleLinkType<Object>(g.getResource(dynamicType.value()), clazz, rules);
92 OrderedSetType orderedSetType = clazz.getAnnotation(OrderedSetType.class);
93 if (orderedSetType != null) {
94 ArrayList<IBidirectionalMappingRule<Resource, Object>> rules = new ArrayList<IBidirectionalMappingRule<Resource, Object>>();
95 collectRulesFromAnnotations(g, clazz, rules);
97 return new OrderedSetSimpleLinkType<Object>(g.getResource(orderedSetType.value()), clazz, rules);
99 throw new IllegalArgumentException("Class " + clazz.toString() + " does not contain annotations.");
102 public static void collectRulesFromAnnotations(ReadGraph g, Class<?> clazz, Collection<IBidirectionalMappingRule<Resource, Object>> rules) throws DatabaseException, InstantiationException, IllegalAccessException {
103 Class<?> superclass = clazz.getSuperclass();
104 if(superclass != null)
105 collectRulesFromAnnotations(g, superclass, rules);
107 for(Annotation annotation : clazz.getAnnotations()) {
109 IsClassRule tag = annotation.annotationType().getAnnotation(IsClassRule.class);
111 final IClassRuleFactory<Resource, Object> ruleFactory = createClassRule(g, annotation, clazz);
112 if (ruleFactory != null)
113 rules.add(ruleFactory.create(g, annotation, clazz));
115 LOGGER.warn("No rule factory found for {}", annotation);
119 for(Field f : clazz.getDeclaredFields()) {
120 f.setAccessible(true);
122 for(Annotation annotation : f.getAnnotations()) {
124 IsFieldRule tag = annotation.annotationType().getAnnotation(IsFieldRule.class);
126 final IFieldRuleFactory<Resource, Object> ruleFactory = createFieldRule(g, annotation, f);
127 if (ruleFactory != null)
128 rules.add(ruleFactory.create(g, annotation, f));
130 LOGGER.warn("No rule factory found for {}", annotation);
135 for(Method m : clazz.getDeclaredMethods()) {
136 m.setAccessible(true);
138 for(Annotation annotation : m.getAnnotations()) {
140 annotation.annotationType().getAnnotation(IsMethodRule.class);
142 final IMethodRuleFactory<Resource, Object> ruleFactory = createMethodRule(g, annotation, m);
143 if (ruleFactory != null)
144 rules.add(ruleFactory.create(g, annotation, m));
146 LOGGER.warn("No rule factory found for {}", annotation);
151 for (Method m : clazz.getDeclaredMethods()) {
152 m.setAccessible(true);
153 for (Annotation annotation : m.getAnnotations()) {
154 Class<? extends Annotation> annotationType = annotation.annotationType();
157 annotationType.getAnnotation(IsGetSetRule.class);
160 HasSetter setterAnnType = annotationType.getAnnotation(HasSetter.class);
162 Class<? extends Annotation> setterAnn = setterAnnType.value();
166 IGetSetRuleFactory<Resource,Object> ruleFactory = createGetSetRuleFactory(g, annotation, getter);
167 if (ruleFactory != null) {
168 Method setter = null;
170 for (Method m2 : clazz.getDeclaredMethods()) {
171 Annotation set = m2.getAnnotation(setterAnn);
172 if (set != null && ruleFactory.isSetter(annotation, set))
176 rules.add(ruleFactory.create(g, annotation, getter, setter));
179 LOGGER.warn("No rule factory found for {}", annotation);
186 for (Method m : clazz.getDeclaredMethods()) {
187 m.setAccessible(true);
188 for (Annotation annotation : m.getAnnotations()) {
189 Class<? extends Annotation> annotationType = annotation.annotationType();
191 IsCollectionRule tag =
192 annotationType.getAnnotation(IsCollectionRule.class);
195 HasCollectionAdder adderAnnType = annotationType.getAnnotation(HasCollectionAdder.class);
196 HasCollectionRemover removerAnnType = annotationType.getAnnotation(HasCollectionRemover.class);
198 Class<? extends Annotation> adderAnn = adderAnnType.value();
199 Class<? extends Annotation> removerAnn = removerAnnType.value();
203 ICollectionRuleFactory<Resource,Object> ruleFactory = createCollectionRuleFactory(g, annotation, getter);
204 if (ruleFactory != null) {
206 Method remover = null;
208 for (Method m2 : clazz.getDeclaredMethods()) {
209 Annotation add = m2.getAnnotation(adderAnn);
210 Annotation rem = m2.getAnnotation(removerAnn);
211 if (add != null && ruleFactory.isAdder(annotation, add))
213 if (rem != null && ruleFactory.isRemover(annotation, rem))
217 rules.add(ruleFactory.create(g, annotation, getter,adder,remover));
220 LOGGER.warn("No rule factory found for {}", annotation);
228 public static IClassRuleFactory<Resource, Object> createClassRule(ReadGraph g, Annotation annotation, Class<?> clazz) {
232 public static IFieldRuleFactory<Resource,Object> createFieldRule(ReadGraph g, Annotation annotation, Field field) {
233 if (annotation.annotationType().equals(RelatedElement.class))
234 return new RelatedElementRuleFactory<Object>();
235 if (annotation.annotationType().equals(RelatedElements.class))
236 return new RelatedElementsRuleFactory<Object>();
237 if (annotation.annotationType().equals(RelatedValue.class))
238 return new RelatedValueRuleFactory<Object>();
239 if (annotation.annotationType().equals(OptionalRelatedElements.class))
240 return new OptionalRelatedElementsRuleFactory<Object>();
241 if (annotation.annotationType().equals(RelatedOrderedSetElements.class))
242 return new RelatedOrderedSetElementsRuleFactory<Object>();
246 public static IMethodRuleFactory<Resource, Object> createMethodRule(ReadGraph g, Annotation annotation, Method m) {
247 if (annotation.annotationType().equals(UpdateMethod.class))
248 return new UpdateMethodFactory<Resource,Object>();
252 public static IGetSetRuleFactory<Resource,Object> createGetSetRuleFactory(ReadGraph g, Annotation annotation, Method getter) {
253 if (annotation.annotationType().equals(RelatedGetValue.class))
254 return new RelatedGetSetValueRuleFactory<Object>();
255 if (annotation.annotationType().equals(RelatedGetObj.class))
256 return new RelatedGetSetObjRuleFactory<Object>();
257 if (annotation.annotationType().equals(CompoundRelatedGetValue.class))
258 return new CompoundRelatedGetSetValueRuleFactory<Object>();
262 public static ICollectionRuleFactory<Resource,Object> createCollectionRuleFactory(ReadGraph g, Annotation annotation, Method getter) {
263 if (annotation.annotationType().equals(RelatedElementsGet.class))
264 return new RelatedElementsRuleFactory2<Object>();
265 if (annotation.annotationType().equals(OrderedElementsGet.class))
266 return new OrderedElementsRuleFactory<Object>();
271 * Creates a new SimpleLinkType based on the annotations in the given class.
272 * @throws IllegalAccessException
273 * @throws InstantiationException
278 public static AdaptedLinkType<Object> fromAdaptable(ReadGraph g, String type, Class<?> clazz) throws DatabaseException, InstantiationException, IllegalAccessException {
281 return new AdaptedLinkType<Object>(g.getResource(type), clazz);