]> gerrit.simantics Code Review - simantics/platform.git/blob
994811feef05cc85385ccce8e0d504cad3035b79
[simantics/platform.git] /
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.annotations.factories;
13
14 import java.lang.annotation.Annotation;
15 import java.lang.reflect.Method;
16
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.layer0.Layer0;
21 import org.simantics.objmap.bidirectional.IBidirectionalMappingRule;
22 import org.simantics.objmap.graph.annotations.RelatedGetValue;
23 import org.simantics.objmap.graph.annotations.RelatedSetValue;
24 import org.simantics.objmap.graph.rules.ValueRule;
25 import org.simantics.objmap.graph.rules.adapters.IdentityAdapter;
26 import org.simantics.objmap.graph.rules.adapters.ValueAdapter;
27 import org.simantics.objmap.graph.rules.domain.RelatedValueAccessor;
28 import org.simantics.objmap.graph.rules.factory.IGetSetRuleFactory;
29 import org.simantics.objmap.graph.rules.range.AdaptedRangeAccessor;
30 import org.simantics.objmap.graph.rules.range.GetSetValueAccessor;
31 import org.simantics.objmap.graph.rules.range.IRangeAccessor;
32
33 /**
34  * Rule factory for mapped value using Getter/Setter-methods.
35  * 
36  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
37  *
38  */
39 public class RelatedGetSetValueRuleFactory<Range> implements IGetSetRuleFactory<Resource,Range> {
40         
41         @Override
42         public IBidirectionalMappingRule<Resource, Range> create(ReadGraph g, Annotation annotation,
43                         Method getter, Method setter)
44                         throws DatabaseException {
45                 RelatedGetValue getterAnn = (RelatedGetValue)annotation;
46                 
47                 Class<? extends ValueAdapter> adapterClass = getterAnn.adapter();
48                  IRangeAccessor<Range,Object> rangeAccessor = new GetSetValueAccessor<Range,Object>(getter, setter);
49         Resource valueType;
50         if (adapterClass == IdentityAdapter.class) {
51             valueType = dataTypeOfClass(g, getter.getReturnType());
52         } else {
53                 try{
54                          ValueAdapter adapter = adapterClass.newInstance();
55                  rangeAccessor = new AdaptedRangeAccessor<Range>(rangeAccessor, adapter);
56                  valueType = adapter.rangeTypeToDomainType(g, getter.getReturnType());
57              } catch (InstantiationException e) {
58                  throw new RuntimeException(e);
59              } catch (IllegalAccessException e) {
60                  throw new RuntimeException(e);
61              }
62         }
63                 return new ValueRule<Resource,Range>(new RelatedValueAccessor(g.getResource(getterAnn.value()), valueType),
64                                         rangeAccessor);
65         }
66         
67         @Override
68         public boolean isSetter(Annotation getterAnnotation, Annotation annotation) {
69                 RelatedGetValue getterAnn = (RelatedGetValue)getterAnnotation;
70                 RelatedSetValue setterAnn = (RelatedSetValue)annotation;
71                 return getterAnn.value().equals(setterAnn.value());
72         }
73         
74         public static Resource dataTypeOfClass(ReadGraph g, Class<?> clazz) {
75         Layer0 b = Layer0.getInstance(g);
76         if(clazz.equals(Double.class) || clazz.equals(double.class))
77             return b.Double;
78         else if(clazz.equals(String.class))
79             return b.String;
80         else if(clazz.equals(Integer.class) || clazz.equals(int.class))
81             return b.Integer;
82         else if(clazz.equals(Float.class) || clazz.equals(float.class))
83             return b.Float;
84         else if(clazz.equals(Boolean.class) || clazz.equals(boolean.class))
85             return b.Boolean;
86         else if(clazz.equals(Long.class) || clazz.equals(long.class))
87             return b.Long;
88         else if(clazz.equals(Byte.class) || clazz.equals(byte.class))
89             return b.Byte;
90         
91         else if(clazz.equals(double[].class))
92             return b.DoubleArray;
93         else if(clazz.equals(int[].class))
94             return b.IntegerArray;
95         else if(clazz.equals(byte[].class))
96             return b.ByteArray;
97         else if(clazz.equals(float[].class))
98             return b.FloatArray;
99         else if(clazz.equals(boolean[].class))
100             return b.BooleanArray;
101         else if(clazz.equals(String[].class))
102             return b.StringArray;
103         else if(clazz.equals(long[].class))
104             return b.LongArray;
105         else {
106                 System.out.println("Couldn't find a data type for " + clazz);
107             return null;
108         }
109     }
110
111 }