]> gerrit.simantics Code Review - simantics/3d.git/blob
6d625abeaff0efa3ea47dbd1ff8ebbd8f78f5696
[simantics/3d.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.objmap.graph.annotations.factories;\r
13 \r
14 import java.lang.annotation.Annotation;\r
15 import java.lang.reflect.Method;\r
16 \r
17 import org.simantics.db.ReadGraph;\r
18 import org.simantics.db.Resource;\r
19 import org.simantics.db.exception.DatabaseException;\r
20 import org.simantics.layer0.Layer0;\r
21 import org.simantics.objmap.bidirectional.IBidirectionalMappingRule;\r
22 import org.simantics.objmap.graph.annotations.CompoundRelatedGetValue;\r
23 import org.simantics.objmap.graph.annotations.CompoundRelatedSetValue;\r
24 import org.simantics.objmap.graph.rules.ValueRule;\r
25 import org.simantics.objmap.graph.rules.adapters.IdentityAdapter;\r
26 import org.simantics.objmap.graph.rules.adapters.ValueAdapter;\r
27 import org.simantics.objmap.graph.rules.domain.CompoundValueAccessor;\r
28 import org.simantics.objmap.graph.rules.factory.IGetSetRuleFactory;\r
29 import org.simantics.objmap.graph.rules.range.AdaptedRangeAccessor;\r
30 import org.simantics.objmap.graph.rules.range.CompoundGetSetValueAccessor;\r
31 import org.simantics.objmap.graph.rules.range.IRangeAccessor;\r
32 \r
33 /**\r
34  * Rule factory for mapped value using Getter/Setter-methods.\r
35  * \r
36  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
37  *\r
38  */\r
39 public class CompoundRelatedGetSetValueRuleFactory<Range> implements IGetSetRuleFactory<Resource,Range> {\r
40         \r
41         @Override\r
42         public IBidirectionalMappingRule<Resource, Range> create(ReadGraph g, Annotation annotation,\r
43                         Method getter, Method setter)\r
44                         throws DatabaseException {\r
45                 CompoundRelatedGetValue getterAnn = (CompoundRelatedGetValue)annotation;\r
46                 \r
47 //              Class<? extends ValueAdapter> adapterClass = getterAnn.adapter();\r
48                 IRangeAccessor<Range,Object> rangeAccessor = new CompoundGetSetValueAccessor<Range,Object>(getter, setter);\r
49 //        Resource valueType;\r
50 //        if (adapterClass == IdentityAdapter.class) {\r
51 //            valueType = dataTypeOfClass(g, getter.getReturnType());\r
52 //        } else {\r
53 //              try{\r
54 //                       ValueAdapter adapter = adapterClass.newInstance();\r
55 //                 rangeAccessor = new AdaptedRangeAccessor<Range>(rangeAccessor, adapter);\r
56 //                 valueType = adapter.rangeTypeToDomainType(g, getter.getReturnType());\r
57 //             } catch (InstantiationException e) {\r
58 //                 throw new RuntimeException(e);\r
59 //             } catch (IllegalAccessException e) {\r
60 //                 throw new RuntimeException(e);\r
61 //             }\r
62 //        }\r
63                 return new ValueRule<Resource,Range>(new CompoundValueAccessor(g.getResource(getterAnn.objRelation()),\r
64                                                                                                                                            g.getResource(getterAnn.objType()),\r
65                                                                                                                                            g.getResource(getterAnn.valRelation())),\r
66                                         rangeAccessor);\r
67         }\r
68         \r
69         @Override\r
70         public boolean isSetter(Annotation getterAnnotation, Annotation annotation) {\r
71                 CompoundRelatedGetValue getterAnn = (CompoundRelatedGetValue)getterAnnotation;\r
72                 CompoundRelatedSetValue setterAnn = (CompoundRelatedSetValue)annotation;\r
73                 return getterAnn.objRelation().equals(setterAnn.value());\r
74         }\r
75         \r
76         public static Resource dataTypeOfClass(ReadGraph g, Class<?> clazz) {\r
77         Layer0 b = Layer0.getInstance(g);\r
78         if(clazz.equals(Double.class) || clazz.equals(double.class))\r
79             return b.Double;\r
80         else if(clazz.equals(String.class))\r
81             return b.String;\r
82         else if(clazz.equals(Integer.class) || clazz.equals(int.class))\r
83             return b.Integer;\r
84         else if(clazz.equals(Float.class) || clazz.equals(float.class))\r
85             return b.Float;\r
86         else if(clazz.equals(Boolean.class) || clazz.equals(boolean.class))\r
87             return b.Boolean;\r
88         else if(clazz.equals(Long.class) || clazz.equals(long.class))\r
89             return b.Long;\r
90         else if(clazz.equals(Byte.class) || clazz.equals(byte.class))\r
91             return b.Byte;\r
92         \r
93         else if(clazz.equals(double[].class))\r
94             return b.DoubleArray;\r
95         else if(clazz.equals(int[].class))\r
96             return b.IntegerArray;\r
97         else if(clazz.equals(byte[].class))\r
98             return b.ByteArray;\r
99         else if(clazz.equals(float[].class))\r
100             return b.FloatArray;\r
101         else if(clazz.equals(boolean[].class))\r
102             return b.BooleanArray;\r
103         else if(clazz.equals(String[].class))\r
104             return b.StringArray;\r
105         else if(clazz.equals(long[].class))\r
106             return b.LongArray;\r
107         else {\r
108                 System.out.println("Couldn't find a data type for " + clazz);\r
109             return null;\r
110         }\r
111     }\r
112 \r
113 }\r