1 /*******************************************************************************
2 * Copyright (c) 2012, 2013, 2019 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 * Semantum oy - linked list mapping API and implementation
12 *******************************************************************************/
13 package org.simantics.objmap.graph.schema;
15 import java.lang.annotation.Annotation;
16 import java.lang.reflect.Field;
17 import java.lang.reflect.Method;
18 import java.util.ArrayList;
19 import java.util.Collection;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.Resource;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.objmap.bidirectional.IBidirectionalMappingRule;
25 import org.simantics.objmap.graph.annotations.CompoundRelatedGetValue;
26 import org.simantics.objmap.graph.annotations.DynamicGraphType;
27 import org.simantics.objmap.graph.annotations.GraphType;
28 import org.simantics.objmap.graph.annotations.HasCollectionAdder;
29 import org.simantics.objmap.graph.annotations.HasCollectionRemover;
30 import org.simantics.objmap.graph.annotations.HasSetter;
31 import org.simantics.objmap.graph.annotations.LinkedListGet;
32 import org.simantics.objmap.graph.annotations.LinkedList;
33 import org.simantics.objmap.graph.annotations.OptionalRelatedElements;
34 import org.simantics.objmap.graph.annotations.OrderedElementsGet;
35 import org.simantics.objmap.graph.annotations.OrderedSetType;
36 import org.simantics.objmap.graph.annotations.RelatedElement;
37 import org.simantics.objmap.graph.annotations.RelatedElements;
38 import org.simantics.objmap.graph.annotations.RelatedElementsGet;
39 import org.simantics.objmap.graph.annotations.RelatedGetObj;
40 import org.simantics.objmap.graph.annotations.RelatedGetValue;
41 import org.simantics.objmap.graph.annotations.RelatedOrderedSetElements;
42 import org.simantics.objmap.graph.annotations.RelatedValue;
43 import org.simantics.objmap.graph.annotations.UpdateMethod;
44 import org.simantics.objmap.graph.annotations.factories.CompoundRelatedGetSetValueRuleFactory;
45 import org.simantics.objmap.graph.annotations.factories.LinkedListRuleFactory;
46 import org.simantics.objmap.graph.annotations.factories.LinkedListRuleFactory2;
47 import org.simantics.objmap.graph.annotations.factories.OptionalRelatedElementsRuleFactory;
48 import org.simantics.objmap.graph.annotations.factories.OrderedElementsRuleFactory;
49 import org.simantics.objmap.graph.annotations.factories.RelatedElementRuleFactory;
50 import org.simantics.objmap.graph.annotations.factories.RelatedElementsRuleFactory;
51 import org.simantics.objmap.graph.annotations.factories.RelatedElementsRuleFactory2;
52 import org.simantics.objmap.graph.annotations.factories.RelatedGetSetObjRuleFactory;
53 import org.simantics.objmap.graph.annotations.factories.RelatedGetSetValueRuleFactory;
54 import org.simantics.objmap.graph.annotations.factories.RelatedOrderedSetElementsRuleFactory;
55 import org.simantics.objmap.graph.annotations.factories.RelatedValueRuleFactory;
56 import org.simantics.objmap.graph.annotations.factories.UpdateMethodFactory;
57 import org.simantics.objmap.graph.annotations.meta.IsClassRule;
58 import org.simantics.objmap.graph.annotations.meta.IsCollectionRule;
59 import org.simantics.objmap.graph.annotations.meta.IsFieldRule;
60 import org.simantics.objmap.graph.annotations.meta.IsGetSetRule;
61 import org.simantics.objmap.graph.annotations.meta.IsMethodRule;
62 import org.simantics.objmap.graph.rules.factory.IClassRuleFactory;
63 import org.simantics.objmap.graph.rules.factory.ICollectionRuleFactory;
64 import org.simantics.objmap.graph.rules.factory.IFieldRuleFactory;
65 import org.simantics.objmap.graph.rules.factory.IGetSetRuleFactory;
66 import org.simantics.objmap.graph.rules.factory.IMethodRuleFactory;
67 import org.slf4j.Logger;
68 import org.slf4j.LoggerFactory;
71 public class MappingSchemas {
72 private static final Logger LOGGER = LoggerFactory.getLogger(MappingSchemas.class);
75 * Creates a new SimpleLinkType based on the annotations in the given class.
76 * @throws IllegalAccessException
77 * @throws InstantiationException
81 public static SimpleLinkType<Object> fromAnnotations(ReadGraph g, Class<?> clazz) throws DatabaseException, InstantiationException, IllegalAccessException {
82 GraphType graphType = clazz.getAnnotation(GraphType.class);
84 if (graphType != null) {
85 ArrayList<IBidirectionalMappingRule<Resource, Object>> rules = new ArrayList<IBidirectionalMappingRule<Resource, Object>>();
86 collectRulesFromAnnotations(g, clazz, rules);
88 return new SimpleLinkType<Object>(g.getResource(graphType.value()), clazz, rules);
90 DynamicGraphType dynamicType = clazz.getAnnotation(DynamicGraphType.class);
91 if (dynamicType != null) {
92 ArrayList<IBidirectionalMappingRule<Resource, Object>> rules = new ArrayList<IBidirectionalMappingRule<Resource, Object>>();
93 collectRulesFromAnnotations(g, clazz, rules);
95 return new DynamicSimpleLinkType<Object>(g.getResource(dynamicType.value()), clazz, rules);
97 OrderedSetType orderedSetType = clazz.getAnnotation(OrderedSetType.class);
98 if (orderedSetType != null) {
99 ArrayList<IBidirectionalMappingRule<Resource, Object>> rules = new ArrayList<IBidirectionalMappingRule<Resource, Object>>();
100 collectRulesFromAnnotations(g, clazz, rules);
102 return new OrderedSetSimpleLinkType<Object>(g.getResource(orderedSetType.value()), clazz, rules);
104 throw new IllegalArgumentException("Class " + clazz.toString() + " does not contain annotations.");
107 public static void collectRulesFromAnnotations(ReadGraph g, Class<?> clazz, Collection<IBidirectionalMappingRule<Resource, Object>> rules) throws DatabaseException, InstantiationException, IllegalAccessException {
108 Class<?> superclass = clazz.getSuperclass();
109 if(superclass != null)
110 collectRulesFromAnnotations(g, superclass, rules);
112 for(Annotation annotation : clazz.getAnnotations()) {
114 IsClassRule tag = annotation.annotationType().getAnnotation(IsClassRule.class);
116 final IClassRuleFactory<Resource, Object> ruleFactory = createClassRule(g, annotation, clazz);
117 if (ruleFactory != null)
118 rules.add(ruleFactory.create(g, annotation, clazz));
120 LOGGER.warn("No rule factory found for {}", annotation);
124 for(Field f : clazz.getDeclaredFields()) {
125 f.setAccessible(true);
127 for(Annotation annotation : f.getAnnotations()) {
129 IsFieldRule tag = annotation.annotationType().getAnnotation(IsFieldRule.class);
131 final IFieldRuleFactory<Resource, Object> ruleFactory = createFieldRule(g, annotation, f);
132 if (ruleFactory != null)
133 rules.add(ruleFactory.create(g, annotation, f));
135 LOGGER.warn("No rule factory found for {}", annotation);
140 for(Method m : clazz.getDeclaredMethods()) {
141 m.setAccessible(true);
143 for(Annotation annotation : m.getAnnotations()) {
145 annotation.annotationType().getAnnotation(IsMethodRule.class);
147 final IMethodRuleFactory<Resource, Object> ruleFactory = createMethodRule(g, annotation, m);
148 if (ruleFactory != null)
149 rules.add(ruleFactory.create(g, annotation, m));
151 LOGGER.warn("No rule factory found for {}", annotation);
156 for (Method m : clazz.getDeclaredMethods()) {
157 m.setAccessible(true);
158 for (Annotation annotation : m.getAnnotations()) {
159 Class<? extends Annotation> annotationType = annotation.annotationType();
162 annotationType.getAnnotation(IsGetSetRule.class);
165 HasSetter setterAnnType = annotationType.getAnnotation(HasSetter.class);
167 Class<? extends Annotation> setterAnn = setterAnnType.value();
171 IGetSetRuleFactory<Resource,Object> ruleFactory = createGetSetRuleFactory(g, annotation, getter);
172 if (ruleFactory != null) {
173 Method setter = null;
175 for (Method m2 : clazz.getDeclaredMethods()) {
176 Annotation set = m2.getAnnotation(setterAnn);
177 if (set != null && ruleFactory.isSetter(annotation, set))
181 rules.add(ruleFactory.create(g, annotation, getter, setter));
184 LOGGER.warn("No rule factory found for {}", annotation);
191 for (Method m : clazz.getDeclaredMethods()) {
192 m.setAccessible(true);
193 for (Annotation annotation : m.getAnnotations()) {
194 Class<? extends Annotation> annotationType = annotation.annotationType();
196 IsCollectionRule tag =
197 annotationType.getAnnotation(IsCollectionRule.class);
200 HasCollectionAdder adderAnnType = annotationType.getAnnotation(HasCollectionAdder.class);
201 HasCollectionRemover removerAnnType = annotationType.getAnnotation(HasCollectionRemover.class);
203 Class<? extends Annotation> adderAnn = adderAnnType.value();
204 Class<? extends Annotation> removerAnn = removerAnnType.value();
208 ICollectionRuleFactory<Resource,Object> ruleFactory = createCollectionRuleFactory(g, annotation, getter);
209 if (ruleFactory != null) {
211 Method remover = null;
213 for (Method m2 : clazz.getDeclaredMethods()) {
214 Annotation add = m2.getAnnotation(adderAnn);
215 Annotation rem = m2.getAnnotation(removerAnn);
216 if (add != null && ruleFactory.isAdder(annotation, add))
218 if (rem != null && ruleFactory.isRemover(annotation, rem))
222 rules.add(ruleFactory.create(g, annotation, getter,adder,remover));
225 LOGGER.warn("No rule factory found for {}", annotation);
233 public static IClassRuleFactory<Resource, Object> createClassRule(ReadGraph g, Annotation annotation, Class<?> clazz) {
237 public static IFieldRuleFactory<Resource,Object> createFieldRule(ReadGraph g, Annotation annotation, Field field) {
238 if (annotation.annotationType().equals(RelatedElement.class))
239 return new RelatedElementRuleFactory<Object>();
240 if (annotation.annotationType().equals(RelatedElements.class))
241 return new RelatedElementsRuleFactory<Object>();
242 if (annotation.annotationType().equals(RelatedValue.class))
243 return new RelatedValueRuleFactory<Object>();
244 if (annotation.annotationType().equals(OptionalRelatedElements.class))
245 return new OptionalRelatedElementsRuleFactory<Object>();
246 if (annotation.annotationType().equals(RelatedOrderedSetElements.class))
247 return new RelatedOrderedSetElementsRuleFactory<Object>();
248 if (annotation.annotationType().equals(LinkedList.class))
249 return new LinkedListRuleFactory<Object>();
253 public static IMethodRuleFactory<Resource, Object> createMethodRule(ReadGraph g, Annotation annotation, Method m) {
254 if (annotation.annotationType().equals(UpdateMethod.class))
255 return new UpdateMethodFactory<Resource,Object>();
259 public static IGetSetRuleFactory<Resource,Object> createGetSetRuleFactory(ReadGraph g, Annotation annotation, Method getter) {
260 if (annotation.annotationType().equals(RelatedGetValue.class))
261 return new RelatedGetSetValueRuleFactory<Object>();
262 if (annotation.annotationType().equals(RelatedGetObj.class))
263 return new RelatedGetSetObjRuleFactory<Object>();
264 if (annotation.annotationType().equals(CompoundRelatedGetValue.class))
265 return new CompoundRelatedGetSetValueRuleFactory<Object>();
269 public static ICollectionRuleFactory<Resource,Object> createCollectionRuleFactory(ReadGraph g, Annotation annotation, Method getter) {
270 if (annotation.annotationType().equals(RelatedElementsGet.class))
271 return new RelatedElementsRuleFactory2<Object>();
272 if (annotation.annotationType().equals(OrderedElementsGet.class))
273 return new OrderedElementsRuleFactory<Object>();
274 if (annotation.annotationType().equals(LinkedListGet.class))
275 return new LinkedListRuleFactory2<>();
280 * Creates a new SimpleLinkType based on the annotations in the given class.
281 * @throws IllegalAccessException
282 * @throws InstantiationException
287 public static AdaptedLinkType<Object> fromAdaptable(ReadGraph g, String type, Class<?> clazz) throws DatabaseException, InstantiationException, IllegalAccessException {
290 return new AdaptedLinkType<Object>(g.getResource(type), clazz);