]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.objmap2/src/org/simantics/objmap/structural/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 / structural / 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.structural.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.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;
66
67
68 public class MappingSchemas {
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 fromAnnotations(ReadGraph g, Class<?> clazz) throws DatabaseException, InstantiationException, IllegalAccessException {
77             GraphType graphType = clazz.getAnnotation(GraphType.class);
78             
79             if (graphType != null) {
80                     ArrayList<IBidirectionalMappingRule<StructuralResource, IStructuralObject>> rules = new ArrayList<IBidirectionalMappingRule<StructuralResource, IStructuralObject>>();
81                     collectRulesFromAnnotations(g, clazz, rules);
82                     
83                     return new SimpleLinkType(g.getResource(graphType.value()), clazz, rules);    
84             }
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);
89                     
90                     return new OrderedSetSimpleLinkType(g.getResource(orderedSetType.value()), clazz, rules);
91             }
92             throw new IllegalArgumentException("Class " + clazz.toString() + " does not contain annotations.");
93         }
94         
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);
99                         
100                 for(Annotation annotation : clazz.getAnnotations()) {
101
102                         IsClassRule tag = annotation.annotationType().getAnnotation(IsClassRule.class);
103                     if(tag!= null) {
104                         rules.add(createClassRule(g, annotation, clazz).create(g, annotation, clazz));
105                     }
106                 }
107
108                 for(Field f : clazz.getDeclaredFields()) {
109                     f.setAccessible(true);
110
111                     for(Annotation annotation : f.getAnnotations()) {
112
113                         IsFieldRule tag = annotation.annotationType().getAnnotation(IsFieldRule.class);
114                         if(tag != null) {
115                             rules.add(createFieldRule(g, annotation, f).create(g, annotation, f));
116                         }
117                     }
118                 }
119
120                 for(Method m : clazz.getDeclaredMethods()) {
121                     m.setAccessible(true);
122
123                     for(Annotation annotation : m.getAnnotations()) {
124                         IsMethodRule tag = 
125                                 annotation.annotationType().getAnnotation(IsMethodRule.class);
126                         if(tag != null) {
127                                 rules.add(createMethodRule(g, annotation, m).create(g, annotation, m));
128                         }
129                     }
130                 }
131                 
132                 for (Method m : clazz.getDeclaredMethods()) {
133                         m.setAccessible(true);
134                         for (Annotation annotation : m.getAnnotations()) {
135                                 Class<? extends Annotation> annotationType = annotation.annotationType();
136
137                                  IsGetSetRule tag = 
138                                  annotationType.getAnnotation(IsGetSetRule.class);
139                                  if (tag != null) {
140                                          
141                                          HasSetter setterAnnType = annotationType.getAnnotation(HasSetter.class);
142                                          
143                                          Class<? extends Annotation> setterAnn = setterAnnType.value();
144                                          
145                                          Method getter = m;
146                                          
147                                          IGetSetRuleFactory<StructuralResource, IStructuralObject> ruleFactory = createGetSetRuleFactory(g, annotation, getter);
148                                          
149                                          
150                                          Method setter = null;
151                                          
152                                          for (Method m2 : clazz.getDeclaredMethods()) {
153                                                  Annotation set = m2.getAnnotation(setterAnn);
154                                                  if (set != null && ruleFactory.isSetter(annotation, set))
155                                                          setter = m2;
156                                          }
157
158                                          rules.add(ruleFactory.create(g, annotation, getter, setter));
159                                  }
160                         
161                         }
162                 }
163                 
164                 for (Method m : clazz.getDeclaredMethods()) {
165                         m.setAccessible(true);
166                         for (Annotation annotation : m.getAnnotations()) {
167                                 Class<? extends Annotation> annotationType = annotation.annotationType();
168
169                                  IsCollectionRule tag = 
170                                  annotationType.getAnnotation(IsCollectionRule.class);
171                                  if (tag != null) {
172                                          
173                                          HasCollectionAdder adderAnnType = annotationType.getAnnotation(HasCollectionAdder.class);
174                                          HasCollectionRemover removerAnnType = annotationType.getAnnotation(HasCollectionRemover.class);
175                         
176                                          Class<? extends Annotation> adderAnn = adderAnnType.value();
177                                          Class<? extends Annotation> removerAnn = removerAnnType.value();
178                                          
179                                          Method getter = m;
180                                          
181                                          ICollectionRuleFactory<StructuralResource, IStructuralObject> ruleFactory = createCollectionRuleFactory(g, annotation, getter);
182                                          
183                                          
184                                          Method adder = null;
185                                          Method remover = null;
186                                          
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))
191                                                          adder = m2;
192                                                  if (rem != null && ruleFactory.isRemover(annotation, rem))
193                                                          remover = m2;
194                                          }
195                                          
196                                          
197                                          
198                                          rules.add(ruleFactory.create(g, annotation, getter,adder,remover));
199                                  }
200                         
201                         }
202                 }
203             }
204                 
205                 public static IClassRuleFactory<StructuralResource, IStructuralObject> createClassRule(ReadGraph g, Annotation annotation, Class<?> clazz) {
206                         return null;
207                 }
208                 
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>();
218                         return null;
219                 }
220                 
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>();
224                         return null;
225                 }
226                 
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>();
238                         return null;
239                 }
240                 
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>();
248                         return null;
249                 }
250         
251         
252         /**
253      * Creates a new SimpleLinkType based on the annotations in the given class.
254      * @throws IllegalAccessException 
255      * @throws InstantiationException 
256      * @see GraphType
257      * @see RelatedValue
258      */
259         public static AdaptedLinkType fromAdaptable(ReadGraph g, String type, Class<?> clazz) throws DatabaseException, InstantiationException, IllegalAccessException {
260             
261             
262             return new AdaptedLinkType(g.getResource(type), clazz);    
263         }
264         
265         
266 }