]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/MappingSchemas.java
56aa5da205ed21fb0cde31f9e3fd325d18a0a7f3
[simantics/platform.git] / bundles / org.simantics.objmap2 / src / org / simantics / objmap / graph / schema / MappingSchemas.java
1 /*******************************************************************************
2  * Copyright (c) 2012, 2013 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  *******************************************************************************/
12 package org.simantics.objmap.graph.schema;
13
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;
19
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;
64
65
66 public class MappingSchemas {
67     private static final Logger LOGGER = LoggerFactory.getLogger(MappingSchemas.class);
68
69     /**
70      * Creates a new SimpleLinkType based on the annotations in the given class.
71      * @throws IllegalAccessException 
72      * @throws InstantiationException 
73      * @see GraphType
74      * @see RelatedValue
75      */
76     public static SimpleLinkType<Object> fromAnnotations(ReadGraph g, Class<?> clazz) throws DatabaseException, InstantiationException, IllegalAccessException {
77         GraphType graphType = clazz.getAnnotation(GraphType.class);
78
79         if (graphType != null) {
80             ArrayList<IBidirectionalMappingRule<Resource, Object>> rules = new ArrayList<IBidirectionalMappingRule<Resource, Object>>();
81             collectRulesFromAnnotations(g, clazz, rules);
82
83             return new SimpleLinkType<Object>(g.getResource(graphType.value()), clazz, rules);  
84         }
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);
89
90             return new DynamicSimpleLinkType<Object>(g.getResource(dynamicType.value()), clazz, rules);
91         }
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);
96
97             return new OrderedSetSimpleLinkType<Object>(g.getResource(orderedSetType.value()), clazz, rules);
98         }
99         throw new IllegalArgumentException("Class " + clazz.toString() + " does not contain annotations.");
100     }
101
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);
106
107         for(Annotation annotation : clazz.getAnnotations()) {
108
109             IsClassRule tag = annotation.annotationType().getAnnotation(IsClassRule.class);
110             if(tag!= null) {
111                 final IClassRuleFactory<Resource, Object> ruleFactory = createClassRule(g, annotation, clazz);
112                 if (ruleFactory != null)
113                     rules.add(ruleFactory.create(g, annotation, clazz));
114                 else
115                     LOGGER.warn("No rule factory found for {}", annotation);
116             }
117         }
118
119         for(Field f : clazz.getDeclaredFields()) {
120             f.setAccessible(true);
121
122             for(Annotation annotation : f.getAnnotations()) {
123
124                 IsFieldRule tag = annotation.annotationType().getAnnotation(IsFieldRule.class);
125                 if(tag != null) {
126                     final IFieldRuleFactory<Resource, Object> ruleFactory = createFieldRule(g, annotation, f);
127                     if (ruleFactory != null)
128                         rules.add(ruleFactory.create(g, annotation, f));
129                     else
130                         LOGGER.warn("No rule factory found for {}", annotation);
131                 }
132             }
133         }
134
135         for(Method m : clazz.getDeclaredMethods()) {
136             m.setAccessible(true);
137
138             for(Annotation annotation : m.getAnnotations()) {
139                 IsMethodRule tag = 
140                         annotation.annotationType().getAnnotation(IsMethodRule.class);
141                 if(tag != null) {
142                     final IMethodRuleFactory<Resource, Object> ruleFactory = createMethodRule(g, annotation, m);
143                     if (ruleFactory != null)
144                         rules.add(ruleFactory.create(g, annotation, m));
145                     else
146                         LOGGER.warn("No rule factory found for {}", annotation);
147                 }
148             }
149         }
150
151         for (Method m : clazz.getDeclaredMethods()) {
152             m.setAccessible(true);
153             for (Annotation annotation : m.getAnnotations()) {
154                 Class<? extends Annotation> annotationType = annotation.annotationType();
155
156                 IsGetSetRule tag = 
157                         annotationType.getAnnotation(IsGetSetRule.class);
158                 if (tag != null) {
159
160                     HasSetter setterAnnType = annotationType.getAnnotation(HasSetter.class);
161
162                     Class<? extends Annotation> setterAnn = setterAnnType.value();
163
164                     Method getter = m;
165
166                     IGetSetRuleFactory<Resource,Object> ruleFactory = createGetSetRuleFactory(g, annotation, getter);
167                     if (ruleFactory != null) {
168                         Method setter = null;
169     
170                         for (Method m2 : clazz.getDeclaredMethods()) {
171                             Annotation set = m2.getAnnotation(setterAnn);
172                             if (set != null && ruleFactory.isSetter(annotation, set))
173                                 setter = m2;
174                         }
175     
176                         rules.add(ruleFactory.create(g, annotation, getter, setter));
177                     }
178                     else {
179                         LOGGER.warn("No rule factory found for {}", annotation);
180                     }
181                 }
182
183             }
184         }
185
186         for (Method m : clazz.getDeclaredMethods()) {
187             m.setAccessible(true);
188             for (Annotation annotation : m.getAnnotations()) {
189                 Class<? extends Annotation> annotationType = annotation.annotationType();
190
191                 IsCollectionRule tag = 
192                         annotationType.getAnnotation(IsCollectionRule.class);
193                 if (tag != null) {
194
195                     HasCollectionAdder adderAnnType = annotationType.getAnnotation(HasCollectionAdder.class);
196                     HasCollectionRemover removerAnnType = annotationType.getAnnotation(HasCollectionRemover.class);
197
198                     Class<? extends Annotation> adderAnn = adderAnnType.value();
199                     Class<? extends Annotation> removerAnn = removerAnnType.value();
200
201                     Method getter = m;
202
203                     ICollectionRuleFactory<Resource,Object> ruleFactory = createCollectionRuleFactory(g, annotation, getter);
204                     if (ruleFactory != null) {
205                         Method adder = null;
206                         Method remover = null;
207     
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))
212                                 adder = m2;
213                             if (rem != null && ruleFactory.isRemover(annotation, rem))
214                                 remover = m2;
215                         }
216     
217                         rules.add(ruleFactory.create(g, annotation, getter,adder,remover));
218                     }
219                     else {
220                         LOGGER.warn("No rule factory found for {}", annotation);
221                     }
222                 }
223
224             }
225         }
226     }
227
228     public static IClassRuleFactory<Resource, Object> createClassRule(ReadGraph g, Annotation annotation, Class<?> clazz) {
229         return null;
230     }
231
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>();
243         return null;
244     }
245
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>();
249         return null;
250     }
251
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>();
259         return null;
260     }
261
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>();
267         return null;
268     }
269
270     /**
271      * Creates a new SimpleLinkType based on the annotations in the given class.
272      * @throws IllegalAccessException 
273      * @throws InstantiationException 
274      * @see GraphType
275      * @see RelatedValue
276      */
277
278     public static AdaptedLinkType<Object> fromAdaptable(ReadGraph g, String type, Class<?> clazz) throws DatabaseException, InstantiationException, IllegalAccessException {
279
280
281         return new AdaptedLinkType<Object>(g.getResource(type), clazz);    
282     }
283
284
285 }