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