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.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;
31 * Rule factory for mapped value using Getter/Setter-methods.
33 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
36 public class CompoundRelatedGetSetValueRuleFactory<Range> implements IGetSetRuleFactory<Resource,Range> {
39 public IBidirectionalMappingRule<Resource, Range> create(ReadGraph g, Annotation annotation,
40 Method getter, Method setter)
41 throws DatabaseException {
42 CompoundRelatedGetValue getterAnn = (CompoundRelatedGetValue)annotation;
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());
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);
60 return new ValueRule<Resource,Range>(new CompoundValueAccessor(g.getResource(getterAnn.objRelation()),
61 g.getResource(getterAnn.objType()),
62 g.getResource(getterAnn.valRelation())),
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());
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))
77 else if(clazz.equals(String.class))
79 else if(clazz.equals(Integer.class) || clazz.equals(int.class))
81 else if(clazz.equals(Float.class) || clazz.equals(float.class))
83 else if(clazz.equals(Boolean.class) || clazz.equals(boolean.class))
85 else if(clazz.equals(Long.class) || clazz.equals(long.class))
87 else if(clazz.equals(Byte.class) || clazz.equals(byte.class))
90 else if(clazz.equals(double[].class))
92 else if(clazz.equals(int[].class))
93 return b.IntegerArray;
94 else if(clazz.equals(byte[].class))
96 else if(clazz.equals(float[].class))
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))
105 System.out.println("Couldn't find a data type for " + clazz);