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,
\r
import org.simantics.objmap.annotations.factories.RelatedValueRuleFactory;\r
import org.simantics.objmap.annotations.meta.HasFieldRuleFactory;\r
+import org.simantics.objmap.rules.adapters.IdentityAdapter;\r
+import org.simantics.objmap.rules.adapters.ValueAdapter;\r
\r
/**\r
* Specifies a correspondence between a field and \r
@Target(ElementType.FIELD)\r
@HasFieldRuleFactory(RelatedValueRuleFactory.class)\r
public @interface RelatedValue {\r
- String value();\r
+ String value();\r
+ Class<? extends ValueAdapter> adapter() default IdentityAdapter.class;\r
}\r
import java.lang.reflect.Field;\r
\r
import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
import org.simantics.db.exception.ResourceNotFoundException;\r
import org.simantics.db.exception.ServiceException;\r
import org.simantics.db.exception.ValidationException;\r
import org.simantics.objmap.IMappingRule;\r
import org.simantics.objmap.annotations.RelatedValue;\r
import org.simantics.objmap.rules.ValueRule;\r
+import org.simantics.objmap.rules.adapters.IdentityAdapter;\r
+import org.simantics.objmap.rules.adapters.ValueAdapter;\r
import org.simantics.objmap.rules.domain.RelatedValueAccessor;\r
import org.simantics.objmap.rules.factory.IFieldRuleFactory;\r
+import org.simantics.objmap.rules.range.AdaptedRangeAccessor;\r
import org.simantics.objmap.rules.range.FieldAccessor;\r
+import org.simantics.objmap.rules.range.IRangeAccessor;\r
\r
public class RelatedValueRuleFactory implements IFieldRuleFactory {\r
\r
@Override\r
public IMappingRule create(ReadGraph g, Annotation _annotation, Field field) throws ResourceNotFoundException, ValidationException, ServiceException {\r
RelatedValue annotation = (RelatedValue)_annotation;\r
- //Builtins b = g.getBuiltins();\r
+ Class<? extends ValueAdapter> adapterClass = annotation.adapter();\r
+ IRangeAccessor<Object> rangeAccessor = new FieldAccessor<Object>(field);\r
+ Resource valueType = DataTypeUtils.dataTypeOfClass(g, field.getType());\r
+ if(adapterClass != IdentityAdapter.class)\r
+ try {\r
+ ValueAdapter adapter = adapterClass.newInstance();\r
+ rangeAccessor = new AdaptedRangeAccessor(rangeAccessor, adapter);\r
+ valueType = adapter.rangeTypeToDomainType(g, field.getType());\r
+ } catch (InstantiationException e) {\r
+ throw new RuntimeException(e);\r
+ } catch (IllegalAccessException e) {\r
+ throw new RuntimeException(e);\r
+ } \r
return new ValueRule(\r
new RelatedValueAccessor(\r
g.getResource(annotation.value()), \r
- DataTypeUtils.dataTypeOfClass(g, field.getType())\r
- ),\r
- new FieldAccessor<Object>(field)\r
- );\r
+ valueType\r
+ ),\r
+ rangeAccessor\r
+ );\r
}\r
\r
}\r