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.structural.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.exception.DatabaseException;
22 import org.simantics.objmap.bidirectional.IBidirectionalMappingRule;
23 import org.simantics.objmap.graph.annotations.GraphType;
24 import org.simantics.objmap.graph.annotations.HasCollectionAdder;
25 import org.simantics.objmap.graph.annotations.HasCollectionRemover;
26 import org.simantics.objmap.graph.annotations.HasSetter;
27 import org.simantics.objmap.graph.annotations.OptionalRelatedElements;
28 import org.simantics.objmap.graph.annotations.OrderedSetType;
29 import org.simantics.objmap.graph.annotations.RelatedElements;
30 import org.simantics.objmap.graph.annotations.RelatedElementsGet;
31 import org.simantics.objmap.graph.annotations.RelatedGetObj;
32 import org.simantics.objmap.graph.annotations.RelatedGetValue;
33 import org.simantics.objmap.graph.annotations.RelatedOrderedSetElements;
34 import org.simantics.objmap.graph.annotations.RelatedValue;
35 import org.simantics.objmap.graph.annotations.UpdateMethod;
36 import org.simantics.objmap.graph.annotations.meta.IsClassRule;
37 import org.simantics.objmap.graph.annotations.meta.IsCollectionRule;
38 import org.simantics.objmap.graph.annotations.meta.IsFieldRule;
39 import org.simantics.objmap.graph.annotations.meta.IsGetSetRule;
40 import org.simantics.objmap.graph.annotations.meta.IsMethodRule;
41 import org.simantics.objmap.graph.rules.factory.IClassRuleFactory;
42 import org.simantics.objmap.graph.rules.factory.ICollectionRuleFactory;
43 import org.simantics.objmap.graph.rules.factory.IFieldRuleFactory;
44 import org.simantics.objmap.graph.rules.factory.IGetSetRuleFactory;
45 import org.simantics.objmap.graph.rules.factory.IMethodRuleFactory;
46 import org.simantics.objmap.structural.IStructuralObject;
47 import org.simantics.objmap.structural.StructuralResource;
48 import org.simantics.objmap.structural.annotations.StructuralRelatedElementsGet;
49 import org.simantics.objmap.structural.annotations.StructuralRelatedGetObj;
50 import org.simantics.objmap.structural.annotations.TypeRelatedElementsGet;
51 import org.simantics.objmap.structural.annotations.TypeRelatedGetObj;
52 import org.simantics.objmap.structural.annotations.TypeRelatedGetValue;
53 import org.simantics.objmap.structural.annotations.factories.OptionalRelatedElementsRuleFactory;
54 import org.simantics.objmap.structural.annotations.factories.RelatedElementsRuleFactory;
55 import org.simantics.objmap.structural.annotations.factories.RelatedElementsRuleFactory2;
56 import org.simantics.objmap.structural.annotations.factories.RelatedGetSetObjRuleFactory;
57 import org.simantics.objmap.structural.annotations.factories.RelatedGetSetValueRuleFactory;
58 import org.simantics.objmap.structural.annotations.factories.RelatedOrderedSetElementsRuleFactory;
59 import org.simantics.objmap.structural.annotations.factories.RelatedValueRuleFactory;
60 import org.simantics.objmap.structural.annotations.factories.StructuralRelatedElementsRuleFactory2;
61 import org.simantics.objmap.structural.annotations.factories.StructuralRelatedGetSetObjRuleFactory;
62 import org.simantics.objmap.structural.annotations.factories.TypeRelatedElementsRuleFactory2;
63 import org.simantics.objmap.structural.annotations.factories.TypeRelatedGetSetObjRuleFactory;
64 import org.simantics.objmap.structural.annotations.factories.TypeRelatedGetSetValueRuleFactory;
65 import org.simantics.objmap.structural.annotations.factories.UpdateMethodFactory;
68 public class MappingSchemas {
70 * Creates a new SimpleLinkType based on the annotations in the given class.
71 * @throws IllegalAccessException
72 * @throws InstantiationException
76 public static SimpleLinkType fromAnnotations(ReadGraph g, Class<?> clazz) throws DatabaseException, InstantiationException, IllegalAccessException {
77 GraphType graphType = clazz.getAnnotation(GraphType.class);
79 if (graphType != null) {
80 ArrayList<IBidirectionalMappingRule<StructuralResource, IStructuralObject>> rules = new ArrayList<IBidirectionalMappingRule<StructuralResource, IStructuralObject>>();
81 collectRulesFromAnnotations(g, clazz, rules);
83 return new SimpleLinkType(g.getResource(graphType.value()), clazz, rules);
85 OrderedSetType orderedSetType = clazz.getAnnotation(OrderedSetType.class);
86 if (orderedSetType != null) {
87 ArrayList<IBidirectionalMappingRule<StructuralResource, IStructuralObject>> rules = new ArrayList<IBidirectionalMappingRule<StructuralResource, IStructuralObject>>();
88 collectRulesFromAnnotations(g, clazz, rules);
90 return new OrderedSetSimpleLinkType(g.getResource(orderedSetType.value()), clazz, rules);
92 throw new IllegalArgumentException("Class " + clazz.toString() + " does not contain annotations.");
95 public static void collectRulesFromAnnotations(ReadGraph g, Class<?> clazz, Collection<IBidirectionalMappingRule<StructuralResource, IStructuralObject>> rules) throws DatabaseException, InstantiationException, IllegalAccessException {
96 Class<?> superclass = clazz.getSuperclass();
97 if(superclass != null)
98 collectRulesFromAnnotations(g, superclass, rules);
100 for(Annotation annotation : clazz.getAnnotations()) {
102 IsClassRule tag = annotation.annotationType().getAnnotation(IsClassRule.class);
104 rules.add(createClassRule(g, annotation, clazz).create(g, annotation, clazz));
108 for(Field f : clazz.getDeclaredFields()) {
109 f.setAccessible(true);
111 for(Annotation annotation : f.getAnnotations()) {
113 IsFieldRule tag = annotation.annotationType().getAnnotation(IsFieldRule.class);
115 rules.add(createFieldRule(g, annotation, f).create(g, annotation, f));
120 for(Method m : clazz.getDeclaredMethods()) {
121 m.setAccessible(true);
123 for(Annotation annotation : m.getAnnotations()) {
125 annotation.annotationType().getAnnotation(IsMethodRule.class);
127 rules.add(createMethodRule(g, annotation, m).create(g, annotation, m));
132 for (Method m : clazz.getDeclaredMethods()) {
133 m.setAccessible(true);
134 for (Annotation annotation : m.getAnnotations()) {
135 Class<? extends Annotation> annotationType = annotation.annotationType();
138 annotationType.getAnnotation(IsGetSetRule.class);
141 HasSetter setterAnnType = annotationType.getAnnotation(HasSetter.class);
143 Class<? extends Annotation> setterAnn = setterAnnType.value();
147 IGetSetRuleFactory<StructuralResource, IStructuralObject> ruleFactory = createGetSetRuleFactory(g, annotation, getter);
150 Method setter = null;
152 for (Method m2 : clazz.getDeclaredMethods()) {
153 Annotation set = m2.getAnnotation(setterAnn);
154 if (set != null && ruleFactory.isSetter(annotation, set))
158 rules.add(ruleFactory.create(g, annotation, getter, setter));
164 for (Method m : clazz.getDeclaredMethods()) {
165 m.setAccessible(true);
166 for (Annotation annotation : m.getAnnotations()) {
167 Class<? extends Annotation> annotationType = annotation.annotationType();
169 IsCollectionRule tag =
170 annotationType.getAnnotation(IsCollectionRule.class);
173 HasCollectionAdder adderAnnType = annotationType.getAnnotation(HasCollectionAdder.class);
174 HasCollectionRemover removerAnnType = annotationType.getAnnotation(HasCollectionRemover.class);
176 Class<? extends Annotation> adderAnn = adderAnnType.value();
177 Class<? extends Annotation> removerAnn = removerAnnType.value();
181 ICollectionRuleFactory<StructuralResource, IStructuralObject> ruleFactory = createCollectionRuleFactory(g, annotation, getter);
185 Method remover = null;
187 for (Method m2 : clazz.getDeclaredMethods()) {
188 Annotation add = m2.getAnnotation(adderAnn);
189 Annotation rem = m2.getAnnotation(removerAnn);
190 if (add != null && ruleFactory.isAdder(annotation, add))
192 if (rem != null && ruleFactory.isRemover(annotation, rem))
198 rules.add(ruleFactory.create(g, annotation, getter,adder,remover));
205 public static IClassRuleFactory<StructuralResource, IStructuralObject> createClassRule(ReadGraph g, Annotation annotation, Class<?> clazz) {
209 public static IFieldRuleFactory<StructuralResource, IStructuralObject> createFieldRule(ReadGraph g, Annotation annotation, Field field) {
210 if (annotation.annotationType().equals(RelatedElements.class))
211 return new RelatedElementsRuleFactory<IStructuralObject>();
212 if (annotation.annotationType().equals(RelatedValue.class))
213 return new RelatedValueRuleFactory<IStructuralObject>();
214 if (annotation.annotationType().equals(OptionalRelatedElements.class))
215 return new OptionalRelatedElementsRuleFactory<IStructuralObject>();
216 if (annotation.annotationType().equals(RelatedOrderedSetElements.class))
217 return new RelatedOrderedSetElementsRuleFactory<IStructuralObject>();
221 public static IMethodRuleFactory<StructuralResource, IStructuralObject> createMethodRule(ReadGraph g, Annotation annotation, Method m) {
222 if (annotation.annotationType().equals(UpdateMethod.class))
223 return new UpdateMethodFactory<StructuralResource,IStructuralObject>();
227 public static IGetSetRuleFactory<StructuralResource, IStructuralObject> createGetSetRuleFactory(ReadGraph g, Annotation annotation, Method getter) {
228 if (annotation.annotationType().equals(RelatedGetValue.class))
229 return new RelatedGetSetValueRuleFactory<IStructuralObject>();
230 if (annotation.annotationType().equals(RelatedGetObj.class))
231 return new RelatedGetSetObjRuleFactory<IStructuralObject>();
232 if (annotation.annotationType().equals(TypeRelatedGetValue.class))
233 return new TypeRelatedGetSetValueRuleFactory<IStructuralObject>();
234 if (annotation.annotationType().equals(TypeRelatedGetObj.class))
235 return new TypeRelatedGetSetObjRuleFactory<IStructuralObject>();
236 if (annotation.annotationType().equals(StructuralRelatedGetObj.class))
237 return new StructuralRelatedGetSetObjRuleFactory<IStructuralObject>();
241 public static ICollectionRuleFactory<StructuralResource, IStructuralObject> createCollectionRuleFactory(ReadGraph g, Annotation annotation, Method getter) {
242 if (annotation.annotationType().equals(RelatedElementsGet.class))
243 return new RelatedElementsRuleFactory2<IStructuralObject>();
244 if (annotation.annotationType().equals(TypeRelatedElementsGet.class))
245 return new TypeRelatedElementsRuleFactory2<IStructuralObject>();
246 if (annotation.annotationType().equals(StructuralRelatedElementsGet.class))
247 return new StructuralRelatedElementsRuleFactory2<IStructuralObject>();
253 * Creates a new SimpleLinkType based on the annotations in the given class.
254 * @throws IllegalAccessException
255 * @throws InstantiationException
259 public static AdaptedLinkType fromAdaptable(ReadGraph g, String type, Class<?> clazz) throws DatabaseException, InstantiationException, IllegalAccessException {
262 return new AdaptedLinkType(g.getResource(type), clazz);