From: hoharri Date: Tue, 10 May 2011 09:23:34 +0000 (+0000) Subject: Add optional parameter to RelatedValue-annotation for providing an adapter class... X-Git-Tag: simantics-1.4RC1~14 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=c2438300d52ec291ba75ee2ef01ed1b44106db30;p=simantics%2Fsysdyn.git Add optional parameter to RelatedValue-annotation for providing an adapter class (for custom data type conversions between java objects and db) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@20851 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.objmap/META-INF/MANIFEST.MF b/org.simantics.objmap/META-INF/MANIFEST.MF index 89cfd50b..52d144d7 100644 --- a/org.simantics.objmap/META-INF/MANIFEST.MF +++ b/org.simantics.objmap/META-INF/MANIFEST.MF @@ -12,6 +12,7 @@ Export-Package: org.simantics.objmap, org.simantics.objmap.annotations, org.simantics.objmap.annotations.meta, org.simantics.objmap.rules, + org.simantics.objmap.rules.adapters, org.simantics.objmap.rules.domain, org.simantics.objmap.rules.factory, org.simantics.objmap.rules.range, diff --git a/org.simantics.objmap/src/org/simantics/objmap/annotations/RelatedValue.java b/org.simantics.objmap/src/org/simantics/objmap/annotations/RelatedValue.java index 98dfcca4..23d3e9a8 100644 --- a/org.simantics.objmap/src/org/simantics/objmap/annotations/RelatedValue.java +++ b/org.simantics.objmap/src/org/simantics/objmap/annotations/RelatedValue.java @@ -18,6 +18,8 @@ import java.lang.annotation.Target; import org.simantics.objmap.annotations.factories.RelatedValueRuleFactory; import org.simantics.objmap.annotations.meta.HasFieldRuleFactory; +import org.simantics.objmap.rules.adapters.IdentityAdapter; +import org.simantics.objmap.rules.adapters.ValueAdapter; /** * Specifies a correspondence between a field and @@ -28,5 +30,6 @@ import org.simantics.objmap.annotations.meta.HasFieldRuleFactory; @Target(ElementType.FIELD) @HasFieldRuleFactory(RelatedValueRuleFactory.class) public @interface RelatedValue { - String value(); + String value(); + Class adapter() default IdentityAdapter.class; } diff --git a/org.simantics.objmap/src/org/simantics/objmap/annotations/factories/RelatedValueRuleFactory.java b/org.simantics.objmap/src/org/simantics/objmap/annotations/factories/RelatedValueRuleFactory.java index 5843fb1b..37cd9812 100644 --- a/org.simantics.objmap/src/org/simantics/objmap/annotations/factories/RelatedValueRuleFactory.java +++ b/org.simantics.objmap/src/org/simantics/objmap/annotations/factories/RelatedValueRuleFactory.java @@ -15,29 +15,46 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Field; import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; import org.simantics.db.exception.ResourceNotFoundException; import org.simantics.db.exception.ServiceException; import org.simantics.db.exception.ValidationException; import org.simantics.objmap.IMappingRule; import org.simantics.objmap.annotations.RelatedValue; import org.simantics.objmap.rules.ValueRule; +import org.simantics.objmap.rules.adapters.IdentityAdapter; +import org.simantics.objmap.rules.adapters.ValueAdapter; import org.simantics.objmap.rules.domain.RelatedValueAccessor; import org.simantics.objmap.rules.factory.IFieldRuleFactory; +import org.simantics.objmap.rules.range.AdaptedRangeAccessor; import org.simantics.objmap.rules.range.FieldAccessor; +import org.simantics.objmap.rules.range.IRangeAccessor; public class RelatedValueRuleFactory implements IFieldRuleFactory { @Override public IMappingRule create(ReadGraph g, Annotation _annotation, Field field) throws ResourceNotFoundException, ValidationException, ServiceException { RelatedValue annotation = (RelatedValue)_annotation; - //Builtins b = g.getBuiltins(); + Class adapterClass = annotation.adapter(); + IRangeAccessor rangeAccessor = new FieldAccessor(field); + Resource valueType = DataTypeUtils.dataTypeOfClass(g, field.getType()); + if(adapterClass != IdentityAdapter.class) + try { + ValueAdapter adapter = adapterClass.newInstance(); + rangeAccessor = new AdaptedRangeAccessor(rangeAccessor, adapter); + valueType = adapter.rangeTypeToDomainType(g, field.getType()); + } catch (InstantiationException e) { + throw new RuntimeException(e); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } return new ValueRule( new RelatedValueAccessor( g.getResource(annotation.value()), - DataTypeUtils.dataTypeOfClass(g, field.getType()) - ), - new FieldAccessor(field) - ); + valueType + ), + rangeAccessor + ); } } diff --git a/org.simantics.objmap/src/org/simantics/objmap/rules/adapters/IdentityAdapter.java b/org.simantics.objmap/src/org/simantics/objmap/rules/adapters/IdentityAdapter.java index 3d4b6913..913357b2 100644 --- a/org.simantics.objmap/src/org/simantics/objmap/rules/adapters/IdentityAdapter.java +++ b/org.simantics.objmap/src/org/simantics/objmap/rules/adapters/IdentityAdapter.java @@ -1,5 +1,8 @@ package org.simantics.objmap.rules.adapters; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; + public enum IdentityAdapter implements ValueAdapter { INSTANCE; @@ -12,4 +15,9 @@ public enum IdentityAdapter implements ValueAdapter { public Object rangeToDomain(Object rangeValue) { return rangeValue; } + + @Override + public Resource rangeTypeToDomainType(ReadGraph graph, Class rangeType) { + return null; + } } diff --git a/org.simantics.objmap/src/org/simantics/objmap/rules/adapters/ValueAdapter.java b/org.simantics.objmap/src/org/simantics/objmap/rules/adapters/ValueAdapter.java index c8d0d2d0..85e40169 100644 --- a/org.simantics.objmap/src/org/simantics/objmap/rules/adapters/ValueAdapter.java +++ b/org.simantics.objmap/src/org/simantics/objmap/rules/adapters/ValueAdapter.java @@ -1,6 +1,10 @@ package org.simantics.objmap.rules.adapters; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; + public interface ValueAdapter { + Resource rangeTypeToDomainType(ReadGraph graph, Class rangeType); Object domainToRange(Object domainValue); Object rangeToDomain(Object rangeValue); }