1 package org.simantics.objmap.graph.annotations.factories;
\r
3 import java.lang.annotation.Annotation;
\r
4 import java.lang.reflect.Method;
\r
6 import org.simantics.db.ReadGraph;
\r
7 import org.simantics.db.Resource;
\r
8 import org.simantics.db.exception.DatabaseException;
\r
9 import org.simantics.layer0.Layer0;
\r
10 import org.simantics.objmap.bidirectional.IBidirectionalMappingRule;
\r
11 import org.simantics.objmap.graph.annotations.RelatedGetValue;
\r
12 import org.simantics.objmap.graph.annotations.RelatedSetValue;
\r
13 import org.simantics.objmap.graph.rules.ValueRule;
\r
14 import org.simantics.objmap.graph.rules.adapters.IdentityAdapter;
\r
15 import org.simantics.objmap.graph.rules.adapters.ValueAdapter;
\r
16 import org.simantics.objmap.graph.rules.domain.RelatedValueAccessor;
\r
17 import org.simantics.objmap.graph.rules.factory.IGetSetRuleFactory;
\r
18 import org.simantics.objmap.graph.rules.range.AdaptedRangeAccessor;
\r
19 import org.simantics.objmap.graph.rules.range.GetSetValueAccessor;
\r
20 import org.simantics.objmap.graph.rules.range.IRangeAccessor;
\r
23 * Rule factory for mapped value using Getter/Setter-methods.
\r
25 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
\r
28 public class RelatedGetSetValueRuleFactory<Range> implements IGetSetRuleFactory<Resource,Range> {
\r
31 public IBidirectionalMappingRule<Resource, Range> create(ReadGraph g, Annotation annotation,
\r
32 Method getter, Method setter)
\r
33 throws DatabaseException {
\r
34 RelatedGetValue getterAnn = (RelatedGetValue)annotation;
\r
36 Class<? extends ValueAdapter> adapterClass = getterAnn.adapter();
\r
37 IRangeAccessor<Range,Object> rangeAccessor = new GetSetValueAccessor<Range,Object>(getter, setter);
\r
39 if (adapterClass == IdentityAdapter.class) {
\r
40 valueType = dataTypeOfClass(g, getter.getReturnType());
\r
43 ValueAdapter adapter = adapterClass.newInstance();
\r
44 rangeAccessor = new AdaptedRangeAccessor<Range>(rangeAccessor, adapter);
\r
45 valueType = adapter.rangeTypeToDomainType(g, getter.getReturnType());
\r
46 } catch (InstantiationException e) {
\r
47 throw new RuntimeException(e);
\r
48 } catch (IllegalAccessException e) {
\r
49 throw new RuntimeException(e);
\r
52 return new ValueRule<Resource,Range>(new RelatedValueAccessor(g.getResource(getterAnn.value()), valueType),
\r
57 public boolean isSetter(Annotation getterAnnotation, Annotation annotation) {
\r
58 RelatedGetValue getterAnn = (RelatedGetValue)getterAnnotation;
\r
59 RelatedSetValue setterAnn = (RelatedSetValue)annotation;
\r
60 return getterAnn.value().equals(setterAnn.value());
\r
63 public static Resource dataTypeOfClass(ReadGraph g, Class<?> clazz) {
\r
64 Layer0 b = Layer0.getInstance(g);
\r
65 if(clazz.equals(Double.class) || clazz.equals(double.class))
\r
67 else if(clazz.equals(String.class))
\r
69 else if(clazz.equals(Integer.class) || clazz.equals(int.class))
\r
71 else if(clazz.equals(Float.class) || clazz.equals(float.class))
\r
73 else if(clazz.equals(Boolean.class) || clazz.equals(boolean.class))
\r
75 else if(clazz.equals(Long.class) || clazz.equals(long.class))
\r
77 else if(clazz.equals(Byte.class) || clazz.equals(byte.class))
\r
80 else if(clazz.equals(double[].class))
\r
81 return b.DoubleArray;
\r
82 else if(clazz.equals(int[].class))
\r
83 return b.IntegerArray;
\r
84 else if(clazz.equals(byte[].class))
\r
86 else if(clazz.equals(float[].class))
\r
87 return b.FloatArray;
\r
88 else if(clazz.equals(boolean[].class))
\r
89 return b.BooleanArray;
\r
90 else if(clazz.equals(String[].class))
\r
91 return b.StringArray;
\r
92 else if(clazz.equals(long[].class))
\r
95 System.out.println("Couldn't find a data type for " + clazz);
\r