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.structural.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.rules.ValueRule;
\r
23 import org.simantics.objmap.graph.rules.adapters.IdentityAdapter;
\r
24 import org.simantics.objmap.graph.rules.adapters.ValueAdapter;
\r
25 import org.simantics.objmap.graph.rules.factory.IGetSetRuleFactory;
\r
26 import org.simantics.objmap.graph.rules.range.AdaptedRangeAccessor;
\r
27 import org.simantics.objmap.graph.rules.range.GetSetValueAccessor;
\r
28 import org.simantics.objmap.graph.rules.range.IRangeAccessor;
\r
29 import org.simantics.objmap.structural.StructuralResource;
\r
30 import org.simantics.objmap.structural.annotations.TypeRelatedGetValue;
\r
31 import org.simantics.objmap.structural.annotations.TypeRelatedSetValue;
\r
32 import org.simantics.objmap.structural.rules.domain.RelatedValueAccessor;
\r
35 * Rule factory for mapped value using Getter/Setter-methods.
\r
37 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
\r
40 public class TypeRelatedGetSetValueRuleFactory<Range> implements IGetSetRuleFactory<StructuralResource,Range> {
\r
43 public IBidirectionalMappingRule<StructuralResource, Range> create(ReadGraph g, Annotation annotation,
\r
44 Method getter, Method setter)
\r
45 throws DatabaseException {
\r
46 TypeRelatedGetValue getterAnn = (TypeRelatedGetValue)annotation;
\r
48 Class<? extends ValueAdapter> adapterClass = getterAnn.adapter();
\r
49 IRangeAccessor<Range,Object> rangeAccessor = new GetSetValueAccessor<Range,Object>(getter, setter);
\r
51 if (adapterClass == IdentityAdapter.class) {
\r
52 valueType = dataTypeOfClass(g, getter.getReturnType());
\r
55 ValueAdapter adapter = adapterClass.newInstance();
\r
56 rangeAccessor = new AdaptedRangeAccessor<Range>(rangeAccessor, adapter);
\r
57 valueType = adapter.rangeTypeToDomainType(g, getter.getReturnType());
\r
58 } catch (InstantiationException e) {
\r
59 throw new RuntimeException(e);
\r
60 } catch (IllegalAccessException e) {
\r
61 throw new RuntimeException(e);
\r
64 return new ValueRule<StructuralResource,Range>(new RelatedValueAccessor(g.getResource(getterAnn.value()), valueType,true,true,true),
\r
69 public boolean isSetter(Annotation getterAnnotation, Annotation annotation) {
\r
70 TypeRelatedGetValue getterAnn = (TypeRelatedGetValue)getterAnnotation;
\r
71 TypeRelatedSetValue setterAnn = (TypeRelatedSetValue)annotation;
\r
72 return getterAnn.value().equals(setterAnn.value());
\r
75 public static Resource dataTypeOfClass(ReadGraph g, Class<?> clazz) {
\r
76 Layer0 b = Layer0.getInstance(g);
\r
77 if(clazz.equals(Double.class) || clazz.equals(double.class))
\r
79 else if(clazz.equals(String.class))
\r
81 else if(clazz.equals(Integer.class) || clazz.equals(int.class))
\r
83 else if(clazz.equals(Float.class) || clazz.equals(float.class))
\r
85 else if(clazz.equals(Boolean.class) || clazz.equals(boolean.class))
\r
87 else if(clazz.equals(Long.class) || clazz.equals(long.class))
\r
89 else if(clazz.equals(Byte.class) || clazz.equals(byte.class))
\r
92 else if(clazz.equals(double[].class))
\r
93 return b.DoubleArray;
\r
94 else if(clazz.equals(int[].class))
\r
95 return b.IntegerArray;
\r
96 else if(clazz.equals(byte[].class))
\r
98 else if(clazz.equals(float[].class))
\r
99 return b.FloatArray;
\r
100 else if(clazz.equals(boolean[].class))
\r
101 return b.BooleanArray;
\r
102 else if(clazz.equals(String[].class))
\r
103 return b.StringArray;
\r
104 else if(clazz.equals(long[].class))
\r
105 return b.LongArray;
\r
107 System.out.println("Couldn't find a data type for " + clazz);
\r