1 /*******************************************************************************
\r
2 * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.objmap.graph.annotations.factories;
\r
14 import java.lang.annotation.Annotation;
\r
15 import java.lang.reflect.Method;
\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.RelatedGetValue;
\r
23 import org.simantics.objmap.graph.annotations.RelatedSetValue;
\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.RelatedValueAccessor;
\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.GetSetValueAccessor;
\r
31 import org.simantics.objmap.graph.rules.range.IRangeAccessor;
\r
34 * Rule factory for mapped value using Getter/Setter-methods.
\r
36 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
\r
39 public class RelatedGetSetValueRuleFactory<Range> implements IGetSetRuleFactory<Resource,Range> {
\r
42 public IBidirectionalMappingRule<Resource, Range> create(ReadGraph g, Annotation annotation,
\r
43 Method getter, Method setter)
\r
44 throws DatabaseException {
\r
45 RelatedGetValue getterAnn = (RelatedGetValue)annotation;
\r
47 Class<? extends ValueAdapter> adapterClass = getterAnn.adapter();
\r
48 IRangeAccessor<Range,Object> rangeAccessor = new GetSetValueAccessor<Range,Object>(getter, setter);
\r
50 if (adapterClass == IdentityAdapter.class) {
\r
51 valueType = dataTypeOfClass(g, getter.getReturnType());
\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
63 return new ValueRule<Resource,Range>(new RelatedValueAccessor(g.getResource(getterAnn.value()), valueType),
\r
68 public boolean isSetter(Annotation getterAnnotation, Annotation annotation) {
\r
69 RelatedGetValue getterAnn = (RelatedGetValue)getterAnnotation;
\r
70 RelatedSetValue setterAnn = (RelatedSetValue)annotation;
\r
71 return getterAnn.value().equals(setterAnn.value());
\r
74 public static Resource dataTypeOfClass(ReadGraph g, Class<?> clazz) {
\r
75 Layer0 b = Layer0.getInstance(g);
\r
76 if(clazz.equals(Double.class) || clazz.equals(double.class))
\r
78 else if(clazz.equals(String.class))
\r
80 else if(clazz.equals(Integer.class) || clazz.equals(int.class))
\r
82 else if(clazz.equals(Float.class) || clazz.equals(float.class))
\r
84 else if(clazz.equals(Boolean.class) || clazz.equals(boolean.class))
\r
86 else if(clazz.equals(Long.class) || clazz.equals(long.class))
\r
88 else if(clazz.equals(Byte.class) || clazz.equals(byte.class))
\r
91 else if(clazz.equals(double[].class))
\r
92 return b.DoubleArray;
\r
93 else if(clazz.equals(int[].class))
\r
94 return b.IntegerArray;
\r
95 else if(clazz.equals(byte[].class))
\r
97 else if(clazz.equals(float[].class))
\r
98 return b.FloatArray;
\r
99 else if(clazz.equals(boolean[].class))
\r
100 return b.BooleanArray;
\r
101 else if(clazz.equals(String[].class))
\r
102 return b.StringArray;
\r
103 else if(clazz.equals(long[].class))
\r
104 return b.LongArray;
\r
106 System.out.println("Couldn't find a data type for " + clazz);
\r