-/*******************************************************************************\r
- * Copyright (c) 2007, 2013 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.objmap.graph.annotations.factories;\r
-\r
-import java.lang.annotation.Annotation;\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
-\r
-import org.simantics.objmap.bidirectional.IBidirectionalMappingRule;\r
-import org.simantics.objmap.graph.annotations.RelatedValue;\r
-import org.simantics.objmap.graph.rules.ValueRule;\r
-import org.simantics.objmap.graph.rules.adapters.IdentityAdapter;\r
-import org.simantics.objmap.graph.rules.adapters.ValueAdapter;\r
-import org.simantics.objmap.graph.rules.domain.RelatedValueAccessor;\r
-import org.simantics.objmap.graph.rules.factory.IFieldRuleFactory;\r
-import org.simantics.objmap.graph.rules.range.AdaptedRangeAccessor;\r
-import org.simantics.objmap.graph.rules.range.FieldAccessor;\r
-import org.simantics.objmap.graph.rules.range.IRangeAccessor;\r
-\r
-\r
-public class RelatedValueRuleFactory<Range> implements IFieldRuleFactory<Resource, Range> {\r
-\r
- @Override\r
- public IBidirectionalMappingRule<Resource, Range> create(ReadGraph g, Annotation _annotation, Field field) throws ResourceNotFoundException,\r
- ValidationException, ServiceException {\r
- RelatedValue annotation = (RelatedValue) _annotation;\r
- Class<? extends ValueAdapter> adapterClass = annotation.adapter();\r
- IRangeAccessor<Range,Object> rangeAccessor = new FieldAccessor<Range,Object>(field);\r
- Resource valueType;\r
- if (adapterClass == IdentityAdapter.class) {\r
- valueType = DataTypeUtils.dataTypeOfClass(g, field.getType());\r
- } else {\r
- try {\r
- ValueAdapter adapter = adapterClass.newInstance();\r
- rangeAccessor = new AdaptedRangeAccessor<Range>(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
- }\r
- return new ValueRule<Resource,Range>(new RelatedValueAccessor(g.getResource(annotation.value()), valueType), rangeAccessor);\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 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.Field;
+
+import org.simantics.databoard.Bindings;
+import org.simantics.databoard.binding.Binding;
+import org.simantics.databoard.binding.error.BindingConstructionException;
+import org.simantics.databoard.binding.reflection.BindingRequest;
+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.bidirectional.IBidirectionalMappingRule;
+import org.simantics.objmap.graph.annotations.RelatedValue;
+import org.simantics.objmap.graph.rules.ValueRule;
+import org.simantics.objmap.graph.rules.adapters.IdentityAdapter;
+import org.simantics.objmap.graph.rules.adapters.ValueAdapter;
+import org.simantics.objmap.graph.rules.domain.RelatedValueAccessor;
+import org.simantics.objmap.graph.rules.factory.IFieldRuleFactory;
+import org.simantics.objmap.graph.rules.range.AdaptedRangeAccessor;
+import org.simantics.objmap.graph.rules.range.FieldAccessor;
+import org.simantics.objmap.graph.rules.range.IRangeAccessor;
+
+
+public class RelatedValueRuleFactory<Range> implements IFieldRuleFactory<Resource, Range> {
+
+ @Override
+ public IBidirectionalMappingRule<Resource, Range> create(ReadGraph g, Annotation _annotation, Field field) throws ResourceNotFoundException,
+ ValidationException, ServiceException {
+ RelatedValue annotation = (RelatedValue) _annotation;
+ Class<? extends ValueAdapter> adapterClass = annotation.adapter();
+ IRangeAccessor<Range,Object> rangeAccessor = new FieldAccessor<Range,Object>(field);
+ Resource valueType;
+ Binding valueBinding = null;
+ if (adapterClass == IdentityAdapter.class) {
+ try {
+ valueBinding = Bindings.getBinding(BindingRequest.create(field));
+ valueType = DataTypeUtils.dataTypeOfDatatype(g, valueBinding.type());
+ } catch (BindingConstructionException e) {
+ return null;
+ }
+ } else {
+ try {
+ ValueAdapter adapter = adapterClass.newInstance();
+ rangeAccessor = new AdaptedRangeAccessor<Range>(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<Resource,Range>(new RelatedValueAccessor(g.getResource(annotation.value()), valueType, valueBinding), rangeAccessor);
+ }
+
+}