X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=bundles%2Forg.simantics.objmap2%2Fsrc%2Forg%2Fsimantics%2Fobjmap%2Fgraph%2Fannotations%2Ffactories%2FCompoundRelatedGetSetValueRuleFactory.java;fp=bundles%2Forg.simantics.objmap2%2Fsrc%2Forg%2Fsimantics%2Fobjmap%2Fgraph%2Fannotations%2Ffactories%2FCompoundRelatedGetSetValueRuleFactory.java;h=2120b50fe8fe1f3f0831f28867db7a2d9076e93c;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/factories/CompoundRelatedGetSetValueRuleFactory.java b/bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/factories/CompoundRelatedGetSetValueRuleFactory.java new file mode 100644 index 000000000..2120b50fe --- /dev/null +++ b/bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/factories/CompoundRelatedGetSetValueRuleFactory.java @@ -0,0 +1,110 @@ +/******************************************************************************* + * Copyright (c) 2012, 2013 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.objmap.graph.annotations.factories; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Method; + +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.layer0.Layer0; +import org.simantics.objmap.bidirectional.IBidirectionalMappingRule; +import org.simantics.objmap.graph.annotations.CompoundRelatedGetValue; +import org.simantics.objmap.graph.annotations.CompoundRelatedSetValue; +import org.simantics.objmap.graph.rules.ValueRule; +import org.simantics.objmap.graph.rules.domain.CompoundValueAccessor; +import org.simantics.objmap.graph.rules.factory.IGetSetRuleFactory; +import org.simantics.objmap.graph.rules.range.CompoundGetSetValueAccessor; +import org.simantics.objmap.graph.rules.range.IRangeAccessor; + +/** + * Rule factory for mapped value using Getter/Setter-methods. + * + * @author Marko Luukkainen + * + */ +public class CompoundRelatedGetSetValueRuleFactory implements IGetSetRuleFactory { + + @Override + public IBidirectionalMappingRule create(ReadGraph g, Annotation annotation, + Method getter, Method setter) + throws DatabaseException { + CompoundRelatedGetValue getterAnn = (CompoundRelatedGetValue)annotation; + +// Class adapterClass = getterAnn.adapter(); + IRangeAccessor rangeAccessor = new CompoundGetSetValueAccessor(getter, setter); +// Resource valueType; +// if (adapterClass == IdentityAdapter.class) { +// valueType = dataTypeOfClass(g, getter.getReturnType()); +// } else { +// try{ +// ValueAdapter adapter = adapterClass.newInstance(); +// rangeAccessor = new AdaptedRangeAccessor(rangeAccessor, adapter); +// valueType = adapter.rangeTypeToDomainType(g, getter.getReturnType()); +// } catch (InstantiationException e) { +// throw new RuntimeException(e); +// } catch (IllegalAccessException e) { +// throw new RuntimeException(e); +// } +// } + return new ValueRule(new CompoundValueAccessor(g.getResource(getterAnn.objRelation()), + g.getResource(getterAnn.objType()), + g.getResource(getterAnn.valRelation())), + rangeAccessor); + } + + @Override + public boolean isSetter(Annotation getterAnnotation, Annotation annotation) { + CompoundRelatedGetValue getterAnn = (CompoundRelatedGetValue)getterAnnotation; + CompoundRelatedSetValue setterAnn = (CompoundRelatedSetValue)annotation; + return getterAnn.objRelation().equals(setterAnn.value()); + } + + public static Resource dataTypeOfClass(ReadGraph g, Class clazz) { + Layer0 b = Layer0.getInstance(g); + if(clazz.equals(Double.class) || clazz.equals(double.class)) + return b.Double; + else if(clazz.equals(String.class)) + return b.String; + else if(clazz.equals(Integer.class) || clazz.equals(int.class)) + return b.Integer; + else if(clazz.equals(Float.class) || clazz.equals(float.class)) + return b.Float; + else if(clazz.equals(Boolean.class) || clazz.equals(boolean.class)) + return b.Boolean; + else if(clazz.equals(Long.class) || clazz.equals(long.class)) + return b.Long; + else if(clazz.equals(Byte.class) || clazz.equals(byte.class)) + return b.Byte; + + else if(clazz.equals(double[].class)) + return b.DoubleArray; + else if(clazz.equals(int[].class)) + return b.IntegerArray; + else if(clazz.equals(byte[].class)) + return b.ByteArray; + else if(clazz.equals(float[].class)) + return b.FloatArray; + else if(clazz.equals(boolean[].class)) + return b.BooleanArray; + else if(clazz.equals(String[].class)) + return b.StringArray; + else if(clazz.equals(long[].class)) + return b.LongArray; + else { + System.out.println("Couldn't find a data type for " + clazz); + return null; + } + } + +}