]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/MappingSchemas.java
cf2f3bf45065c5bee4951da094d67b8e94686767
[simantics/platform.git] / bundles / org.simantics.objmap2 / src / org / simantics / objmap / graph / schema / MappingSchemas.java
1 /*******************************************************************************
2  * Copyright (c) 2012, 2013, 2019 Association for Decentralized Information Management in
3  * Industry THTH ry.
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
8  *
9  * Contributors:
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;
14
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;
20
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;
69
70
71 public class MappingSchemas {
72     private static final Logger LOGGER = LoggerFactory.getLogger(MappingSchemas.class);
73
74     /**
75      * Creates a new SimpleLinkType based on the annotations in the given class.
76      * @throws IllegalAccessException 
77      * @throws InstantiationException 
78      * @see GraphType
79      * @see RelatedValue
80      */
81     public static SimpleLinkType<Object> fromAnnotations(ReadGraph g, Class<?> clazz) throws DatabaseException, InstantiationException, IllegalAccessException {
82         GraphType graphType = clazz.getAnnotation(GraphType.class);
83
84         if (graphType != null) {
85             ArrayList<IBidirectionalMappingRule<Resource, Object>> rules = new ArrayList<IBidirectionalMappingRule<Resource, Object>>();
86             collectRulesFromAnnotations(g, clazz, rules);
87
88             return new SimpleLinkType<Object>(g.getResource(graphType.value()), clazz, rules);  
89         }
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);
94
95             return new DynamicSimpleLinkType<Object>(g.getResource(dynamicType.value()), clazz, rules);
96         }
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);
101
102             return new OrderedSetSimpleLinkType<Object>(g.getResource(orderedSetType.value()), clazz, rules);
103         }
104         throw new IllegalArgumentException("Class " + clazz.toString() + " does not contain annotations.");
105     }
106
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);
111
112         for(Annotation annotation : clazz.getAnnotations()) {
113
114             IsClassRule tag = annotation.annotationType().getAnnotation(IsClassRule.class);
115             if(tag!= null) {
116                 final IClassRuleFactory<Resource, Object> ruleFactory = createClassRule(g, annotation, clazz);
117                 if (ruleFactory != null)
118                     rules.add(ruleFactory.create(g, annotation, clazz));
119                 else
120                     LOGGER.warn("No rule factory found for {}", annotation);
121             }
122         }
123
124         for(Field f : clazz.getDeclaredFields()) {
125             f.setAccessible(true);
126
127             for(Annotation annotation : f.getAnnotations()) {
128
129                 IsFieldRule tag = annotation.annotationType().getAnnotation(IsFieldRule.class);
130                 if(tag != null) {
131                     final IFieldRuleFactory<Resource, Object> ruleFactory = createFieldRule(g, annotation, f);
132                     if (ruleFactory != null)
133                         rules.add(ruleFactory.create(g, annotation, f));
134                     else
135                         LOGGER.warn("No rule factory found for {}", annotation);
136                 }
137             }
138         }
139
140         for(Method m : clazz.getDeclaredMethods()) {
141             m.setAccessible(true);
142
143             for(Annotation annotation : m.getAnnotations()) {
144                 IsMethodRule tag = 
145                         annotation.annotationType().getAnnotation(IsMethodRule.class);
146                 if(tag != null) {
147                     final IMethodRuleFactory<Resource, Object> ruleFactory = createMethodRule(g, annotation, m);
148                     if (ruleFactory != null)
149                         rules.add(ruleFactory.create(g, annotation, m));
150                     else
151                         LOGGER.warn("No rule factory found for {}", annotation);
152                 }
153             }
154         }
155
156         for (Method m : clazz.getDeclaredMethods()) {
157             m.setAccessible(true);
158             for (Annotation annotation : m.getAnnotations()) {
159                 Class<? extends Annotation> annotationType = annotation.annotationType();
160
161                 IsGetSetRule tag = 
162                         annotationType.getAnnotation(IsGetSetRule.class);
163                 if (tag != null) {
164
165                     HasSetter setterAnnType = annotationType.getAnnotation(HasSetter.class);
166
167                     Class<? extends Annotation> setterAnn = setterAnnType.value();
168
169                     Method getter = m;
170
171                     IGetSetRuleFactory<Resource,Object> ruleFactory = createGetSetRuleFactory(g, annotation, getter);
172                     if (ruleFactory != null) {
173                         Method setter = null;
174     
175                         for (Method m2 : clazz.getDeclaredMethods()) {
176                             Annotation set = m2.getAnnotation(setterAnn);
177                             if (set != null && ruleFactory.isSetter(annotation, set))
178                                 setter = m2;
179                         }
180     
181                         rules.add(ruleFactory.create(g, annotation, getter, setter));
182                     }
183                     else {
184                         LOGGER.warn("No rule factory found for {}", annotation);
185                     }
186                 }
187
188             }
189         }
190
191         for (Method m : clazz.getDeclaredMethods()) {
192             m.setAccessible(true);
193             for (Annotation annotation : m.getAnnotations()) {
194                 Class<? extends Annotation> annotationType = annotation.annotationType();
195
196                 IsCollectionRule tag = 
197                         annotationType.getAnnotation(IsCollectionRule.class);
198                 if (tag != null) {
199
200                     HasCollectionAdder adderAnnType = annotationType.getAnnotation(HasCollectionAdder.class);
201                     HasCollectionRemover removerAnnType = annotationType.getAnnotation(HasCollectionRemover.class);
202
203                     Class<? extends Annotation> adderAnn = adderAnnType.value();
204                     Class<? extends Annotation> removerAnn = removerAnnType.value();
205
206                     Method getter = m;
207
208                     ICollectionRuleFactory<Resource,Object> ruleFactory = createCollectionRuleFactory(g, annotation, getter);
209                     if (ruleFactory != null) {
210                         Method adder = null;
211                         Method remover = null;
212     
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))
217                                 adder = m2;
218                             if (rem != null && ruleFactory.isRemover(annotation, rem))
219                                 remover = m2;
220                         }
221     
222                         rules.add(ruleFactory.create(g, annotation, getter,adder,remover));
223                     }
224                     else {
225                         LOGGER.warn("No rule factory found for {}", annotation);
226                     }
227                 }
228
229             }
230         }
231     }
232
233     public static IClassRuleFactory<Resource, Object> createClassRule(ReadGraph g, Annotation annotation, Class<?> clazz) {
234         return null;
235     }
236
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>();
250         return null;
251     }
252
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>();
256         return null;
257     }
258
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>();
266         return null;
267     }
268
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<>();
276         return null;
277     }
278
279     /**
280      * Creates a new SimpleLinkType based on the annotations in the given class.
281      * @throws IllegalAccessException 
282      * @throws InstantiationException 
283      * @see GraphType
284      * @see RelatedValue
285      */
286
287     public static AdaptedLinkType<Object> fromAdaptable(ReadGraph g, String type, Class<?> clazz) throws DatabaseException, InstantiationException, IllegalAccessException {
288
289
290         return new AdaptedLinkType<Object>(g.getResource(type), clazz);    
291     }
292
293
294 }