]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/factories/RelatedValueRuleFactory.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.objmap2 / src / org / simantics / objmap / graph / annotations / factories / RelatedValueRuleFactory.java
diff --git a/bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/factories/RelatedValueRuleFactory.java b/bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/annotations/factories/RelatedValueRuleFactory.java
new file mode 100644 (file)
index 0000000..52d6caa
--- /dev/null
@@ -0,0 +1,60 @@
+/*******************************************************************************\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