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