]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.common/src/org/simantics/db/common/request/AdaptValue.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / request / AdaptValue.java
diff --git a/bundles/org.simantics.db.common/src/org/simantics/db/common/request/AdaptValue.java b/bundles/org.simantics.db.common/src/org/simantics/db/common/request/AdaptValue.java
new file mode 100644 (file)
index 0000000..82b6bf9
--- /dev/null
@@ -0,0 +1,47 @@
+package org.simantics.db.common.request;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.utils.Functions;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.exception.RuntimeDatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.scl.reflection.ReflectionUtils;\r
+import org.simantics.scl.reflection.ValueNotFoundException;\r
+import org.simantics.scl.runtime.function.FunctionImpl3;\r
+\r
+/**\r
+ * Adapts a resource to (SCL) value.\r
+ * @author Hannu Niemistö\r
+ */\r
+public class AdaptValue extends ResourceRead<Object> {\r
+    \r
+    public AdaptValue(Resource resource) {\r
+        super(resource);\r
+    }\r
+\r
+    private static final FunctionImpl3<ReadGraph,Resource,Object,Object> functionApplication = new FunctionImpl3<ReadGraph,Resource,Object,Object>() {\r
+\r
+               @Override\r
+               public Object apply(ReadGraph graph, Resource resource, Object context) {\r
+                       try {\r
+                               return Functions.exec(graph, resource, graph, resource, context);\r
+                       } catch (DatabaseException e) {\r
+                               throw new RuntimeDatabaseException(e);\r
+                       }\r
+               }\r
+\r
+       };\r
+    \r
+    @Override\r
+    public Object perform(ReadGraph graph) throws DatabaseException {\r
+        String uri = graph.getURI(resource);        \r
+        try {\r
+                       if(Layer0.URIs.Functions_functionApplication.equals(uri)) return functionApplication;\r
+            return ReflectionUtils.getValue(uri).getValue();\r
+        } catch (ValueNotFoundException e) {\r
+            throw new DatabaseException("Couldn't adapt the value " + uri, e);\r
+        }\r
+    }\r
+\r
+}\r