]> gerrit.simantics Code Review - simantics/3d.git/blob
333686f294c3d0516545865bf7c148bff3ceb03d
[simantics/3d.git] /
1 package org.simantics.objmap.graph.annotations.factories;\r
2 \r
3 import java.lang.annotation.Annotation;\r
4 import java.lang.reflect.Method;\r
5 \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.CompoundRelatedGetValue;\r
12 import org.simantics.objmap.graph.annotations.CompoundRelatedSetValue;\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.CompoundValueAccessor;\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.CompoundGetSetValueAccessor;\r
20 import org.simantics.objmap.graph.rules.range.IRangeAccessor;\r
21 \r
22 /**\r
23  * Rule factory for mapped value using Getter/Setter-methods.\r
24  * \r
25  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
26  *\r
27  */\r
28 public class CompoundRelatedGetSetValueRuleFactory<Range> implements IGetSetRuleFactory<Resource,Range> {\r
29         \r
30         @Override\r
31         public IBidirectionalMappingRule<Resource, Range> create(ReadGraph g, Annotation annotation,\r
32                         Method getter, Method setter)\r
33                         throws DatabaseException {\r
34                 CompoundRelatedGetValue getterAnn = (CompoundRelatedGetValue)annotation;\r
35                 \r
36 //              Class<? extends ValueAdapter> adapterClass = getterAnn.adapter();\r
37                 IRangeAccessor<Range,Object> rangeAccessor = new CompoundGetSetValueAccessor<Range,Object>(getter, setter);\r
38 //        Resource valueType;\r
39 //        if (adapterClass == IdentityAdapter.class) {\r
40 //            valueType = dataTypeOfClass(g, getter.getReturnType());\r
41 //        } else {\r
42 //              try{\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
50 //             }\r
51 //        }\r
52                 return new ValueRule<Resource,Range>(new CompoundValueAccessor(g.getResource(getterAnn.objRelation()),\r
53                                                                                                                                            g.getResource(getterAnn.objType()),\r
54                                                                                                                                            g.getResource(getterAnn.valRelation())),\r
55                                         rangeAccessor);\r
56         }\r
57         \r
58         @Override\r
59         public boolean isSetter(Annotation getterAnnotation, Annotation annotation) {\r
60                 CompoundRelatedGetValue getterAnn = (CompoundRelatedGetValue)getterAnnotation;\r
61                 CompoundRelatedSetValue setterAnn = (CompoundRelatedSetValue)annotation;\r
62                 return getterAnn.objRelation().equals(setterAnn.value());\r
63         }\r
64         \r
65         public static Resource dataTypeOfClass(ReadGraph g, Class<?> clazz) {\r
66         Layer0 b = Layer0.getInstance(g);\r
67         if(clazz.equals(Double.class) || clazz.equals(double.class))\r
68             return b.Double;\r
69         else if(clazz.equals(String.class))\r
70             return b.String;\r
71         else if(clazz.equals(Integer.class) || clazz.equals(int.class))\r
72             return b.Integer;\r
73         else if(clazz.equals(Float.class) || clazz.equals(float.class))\r
74             return b.Float;\r
75         else if(clazz.equals(Boolean.class) || clazz.equals(boolean.class))\r
76             return b.Boolean;\r
77         else if(clazz.equals(Long.class) || clazz.equals(long.class))\r
78             return b.Long;\r
79         else if(clazz.equals(Byte.class) || clazz.equals(byte.class))\r
80             return b.Byte;\r
81         \r
82         else if(clazz.equals(double[].class))\r
83             return b.DoubleArray;\r
84         else if(clazz.equals(int[].class))\r
85             return b.IntegerArray;\r
86         else if(clazz.equals(byte[].class))\r
87             return b.ByteArray;\r
88         else if(clazz.equals(float[].class))\r
89             return b.FloatArray;\r
90         else if(clazz.equals(boolean[].class))\r
91             return b.BooleanArray;\r
92         else if(clazz.equals(String[].class))\r
93             return b.StringArray;\r
94         else if(clazz.equals(long[].class))\r
95             return b.LongArray;\r
96         else {\r
97                 System.out.println("Couldn't find a data type for " + clazz);\r
98             return null;\r
99         }\r
100     }\r
101 \r
102 }\r