]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/schema/MappingSchemas.java
Fixed SCL Console Clear Console active-state when imports are erroneous
[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.RelatedElements;
34 import org.simantics.objmap.graph.annotations.RelatedElementsGet;
35 import org.simantics.objmap.graph.annotations.RelatedGetObj;
36 import org.simantics.objmap.graph.annotations.RelatedGetValue;
37 import org.simantics.objmap.graph.annotations.RelatedOrderedSetElements;
38 import org.simantics.objmap.graph.annotations.RelatedValue;
39 import org.simantics.objmap.graph.annotations.UpdateMethod;
40 import org.simantics.objmap.graph.annotations.factories.CompoundRelatedGetSetValueRuleFactory;
41 import org.simantics.objmap.graph.annotations.factories.OptionalRelatedElementsRuleFactory;
42 import org.simantics.objmap.graph.annotations.factories.OrderedElementsRuleFactory;
43 import org.simantics.objmap.graph.annotations.factories.RelatedElementsRuleFactory;
44 import org.simantics.objmap.graph.annotations.factories.RelatedElementsRuleFactory2;
45 import org.simantics.objmap.graph.annotations.factories.RelatedGetSetObjRuleFactory;
46 import org.simantics.objmap.graph.annotations.factories.RelatedGetSetValueRuleFactory;
47 import org.simantics.objmap.graph.annotations.factories.RelatedOrderedSetElementsRuleFactory;
48 import org.simantics.objmap.graph.annotations.factories.RelatedValueRuleFactory;
49 import org.simantics.objmap.graph.annotations.factories.UpdateMethodFactory;
50 import org.simantics.objmap.graph.annotations.meta.IsClassRule;
51 import org.simantics.objmap.graph.annotations.meta.IsCollectionRule;
52 import org.simantics.objmap.graph.annotations.meta.IsFieldRule;
53 import org.simantics.objmap.graph.annotations.meta.IsGetSetRule;
54 import org.simantics.objmap.graph.annotations.meta.IsMethodRule;
55 import org.simantics.objmap.graph.rules.factory.IClassRuleFactory;
56 import org.simantics.objmap.graph.rules.factory.ICollectionRuleFactory;
57 import org.simantics.objmap.graph.rules.factory.IFieldRuleFactory;
58 import org.simantics.objmap.graph.rules.factory.IGetSetRuleFactory;
59 import org.simantics.objmap.graph.rules.factory.IMethodRuleFactory;
60
61
62 public class MappingSchemas {
63         /**
64      * Creates a new SimpleLinkType based on the annotations in the given class.
65      * @throws IllegalAccessException 
66      * @throws InstantiationException 
67      * @see GraphType
68      * @see RelatedValue
69      */
70         public static SimpleLinkType<Object> fromAnnotations(ReadGraph g, Class<?> clazz) throws DatabaseException, InstantiationException, IllegalAccessException {
71             GraphType graphType = clazz.getAnnotation(GraphType.class);
72             
73             if (graphType != null) {
74                     ArrayList<IBidirectionalMappingRule<Resource, Object>> rules = new ArrayList<IBidirectionalMappingRule<Resource, Object>>();
75                     collectRulesFromAnnotations(g, clazz, rules);
76                     
77                     return new SimpleLinkType<Object>(g.getResource(graphType.value()), clazz, rules);  
78             }
79             DynamicGraphType dynamicType = clazz.getAnnotation(DynamicGraphType.class);
80             if (dynamicType != null) {
81                 ArrayList<IBidirectionalMappingRule<Resource, Object>> rules = new ArrayList<IBidirectionalMappingRule<Resource, Object>>();
82                     collectRulesFromAnnotations(g, clazz, rules);
83                     
84                     return new DynamicSimpleLinkType<Object>(g.getResource(dynamicType.value()), clazz, rules);
85             }
86             OrderedSetType orderedSetType = clazz.getAnnotation(OrderedSetType.class);
87             if (orderedSetType != null) {
88                 ArrayList<IBidirectionalMappingRule<Resource, Object>> rules = new ArrayList<IBidirectionalMappingRule<Resource, Object>>();
89                     collectRulesFromAnnotations(g, clazz, rules);
90                     
91                     return new OrderedSetSimpleLinkType<Object>(g.getResource(orderedSetType.value()), clazz, rules);
92             }
93             throw new IllegalArgumentException("Class " + clazz.toString() + " does not contain annotations.");
94         }
95         
96         public static void collectRulesFromAnnotations(ReadGraph g, Class<?> clazz, Collection<IBidirectionalMappingRule<Resource, Object>> rules) throws DatabaseException, InstantiationException, IllegalAccessException {
97             Class<?> superclass = clazz.getSuperclass();
98             if(superclass != null)
99                 collectRulesFromAnnotations(g, superclass, rules);
100                 
101         for(Annotation annotation : clazz.getAnnotations()) {
102
103                 IsClassRule tag = annotation.annotationType().getAnnotation(IsClassRule.class);
104             if(tag!= null) {
105                 rules.add(createClassRule(g, annotation, clazz).create(g, annotation, clazz));
106             }
107         }
108
109         for(Field f : clazz.getDeclaredFields()) {
110             f.setAccessible(true);
111
112             for(Annotation annotation : f.getAnnotations()) {
113
114                 IsFieldRule tag = annotation.annotationType().getAnnotation(IsFieldRule.class);
115                 if(tag != null) {
116                     rules.add(createFieldRule(g, annotation, f).create(g, annotation, f));
117                 }
118             }
119         }
120
121         for(Method m : clazz.getDeclaredMethods()) {
122             m.setAccessible(true);
123
124             for(Annotation annotation : m.getAnnotations()) {
125                 IsMethodRule tag = 
126                         annotation.annotationType().getAnnotation(IsMethodRule.class);
127                 if(tag != null) {
128                         rules.add(createMethodRule(g, annotation, m).create(g, annotation, m));
129                 }
130             }
131         }
132         
133         for (Method m : clazz.getDeclaredMethods()) {
134                 m.setAccessible(true);
135                 for (Annotation annotation : m.getAnnotations()) {
136                         Class<? extends Annotation> annotationType = annotation.annotationType();
137
138                          IsGetSetRule tag = 
139                          annotationType.getAnnotation(IsGetSetRule.class);
140                          if (tag != null) {
141                                  
142                                  HasSetter setterAnnType = annotationType.getAnnotation(HasSetter.class);
143                                  
144                                  Class<? extends Annotation> setterAnn = setterAnnType.value();
145                                  
146                                  Method getter = m;
147                                  
148                                  IGetSetRuleFactory<Resource,Object> ruleFactory = createGetSetRuleFactory(g, annotation, getter);
149                                  
150                                  
151                                  Method setter = null;
152                                  
153                                  for (Method m2 : clazz.getDeclaredMethods()) {
154                                          Annotation set = m2.getAnnotation(setterAnn);
155                                          if (set != null && ruleFactory.isSetter(annotation, set))
156                                                  setter = m2;
157                                  }
158
159                                  rules.add(ruleFactory.create(g, annotation, getter, setter));
160                          }
161                 
162                 }
163         }
164         
165         for (Method m : clazz.getDeclaredMethods()) {
166                 m.setAccessible(true);
167                 for (Annotation annotation : m.getAnnotations()) {
168                         Class<? extends Annotation> annotationType = annotation.annotationType();
169
170                          IsCollectionRule tag = 
171                          annotationType.getAnnotation(IsCollectionRule.class);
172                          if (tag != null) {
173                                  
174                                  HasCollectionAdder adderAnnType = annotationType.getAnnotation(HasCollectionAdder.class);
175                                  HasCollectionRemover removerAnnType = annotationType.getAnnotation(HasCollectionRemover.class);
176                 
177                                  Class<? extends Annotation> adderAnn = adderAnnType.value();
178                                  Class<? extends Annotation> removerAnn = removerAnnType.value();
179                                  
180                                  Method getter = m;
181                                  
182                                  ICollectionRuleFactory<Resource,Object> ruleFactory = createCollectionRuleFactory(g, annotation, getter);
183                                  
184                                  
185                                  Method adder = null;
186                                  Method remover = null;
187                                  
188                                  for (Method m2 : clazz.getDeclaredMethods()) {
189                                          Annotation add = m2.getAnnotation(adderAnn);
190                                          Annotation rem = m2.getAnnotation(removerAnn);
191                                          if (add != null && ruleFactory.isAdder(annotation, add))
192                                                  adder = m2;
193                                          if (rem != null && ruleFactory.isRemover(annotation, rem))
194                                                  remover = m2;
195                                  }
196                                  
197                                  
198                                  
199                                  rules.add(ruleFactory.create(g, annotation, getter,adder,remover));
200                          }
201                 
202                 }
203         }
204     }
205         
206         public static IClassRuleFactory<Resource, Object> createClassRule(ReadGraph g, Annotation annotation, Class<?> clazz) {
207                 return null;
208         }
209         
210         public static IFieldRuleFactory<Resource,Object> createFieldRule(ReadGraph g, Annotation annotation, Field field) {
211                 if (annotation.annotationType().equals(RelatedElements.class))
212                         return new RelatedElementsRuleFactory<Object>();
213                 if (annotation.annotationType().equals(RelatedValue.class))
214                         return new RelatedValueRuleFactory<Object>();
215                 if (annotation.annotationType().equals(OptionalRelatedElements.class))
216                         return new OptionalRelatedElementsRuleFactory<Object>();
217                 if (annotation.annotationType().equals(RelatedOrderedSetElements.class))
218                         return new RelatedOrderedSetElementsRuleFactory<Object>();
219                 return null;
220         }
221         
222         public static IMethodRuleFactory<Resource, Object> createMethodRule(ReadGraph g, Annotation annotation, Method m) {
223                 if (annotation.annotationType().equals(UpdateMethod.class))
224                         return new UpdateMethodFactory<Resource,Object>();
225                 return null;
226         }
227         
228         public static IGetSetRuleFactory<Resource,Object> createGetSetRuleFactory(ReadGraph g, Annotation annotation, Method getter) {
229                 if (annotation.annotationType().equals(RelatedGetValue.class))
230                         return new RelatedGetSetValueRuleFactory<Object>();
231                 if (annotation.annotationType().equals(RelatedGetObj.class))
232                         return new RelatedGetSetObjRuleFactory<Object>();
233                 if (annotation.annotationType().equals(CompoundRelatedGetValue.class))
234                         return new CompoundRelatedGetSetValueRuleFactory<Object>();
235                 return null;
236         }
237         
238         public static ICollectionRuleFactory<Resource,Object> createCollectionRuleFactory(ReadGraph g, Annotation annotation, Method getter) {
239                 if (annotation.annotationType().equals(RelatedElementsGet.class))
240                         return new RelatedElementsRuleFactory2<Object>();
241                 if (annotation.annotationType().equals(OrderedElementsGet.class))
242                         return new OrderedElementsRuleFactory<Object>();
243                 return null;
244         }
245         
246         /**
247      * Creates a new SimpleLinkType based on the annotations in the given class.
248      * @throws IllegalAccessException 
249      * @throws InstantiationException 
250      * @see GraphType
251      * @see RelatedValue
252      */
253         
254         public static AdaptedLinkType<Object> fromAdaptable(ReadGraph g, String type, Class<?> clazz) throws DatabaseException, InstantiationException, IllegalAccessException {
255             
256             
257             return new AdaptedLinkType<Object>(g.getResource(type), clazz);    
258         }
259         
260         
261 }