]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/bindings/ResourceBinding.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / bindings / ResourceBinding.java
diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/bindings/ResourceBinding.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/bindings/ResourceBinding.java
new file mode 100644 (file)
index 0000000..18eca47
--- /dev/null
@@ -0,0 +1,167 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 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.db.layer0.bindings;\r
+\r
+import org.simantics.databoard.Datatypes;\r
+import org.simantics.databoard.binding.LongBinding;\r
+import org.simantics.databoard.binding.error.BindingException;\r
+import org.simantics.databoard.type.Datatype;\r
+import org.simantics.databoard.type.LongType;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.internal.SimanticsInternal;\r
+import org.simantics.db.service.SerialisationSupport;\r
+\r
+/**\r
+ * This class binds Resource to LongType\r
+ * \r
+ * @author toni.kalajainen\r
+ */\r
+public class ResourceBinding extends LongBinding {\r
+\r
+    public static final Datatype RESOURCE_TYPE;\r
+\r
+    //SerialisationSupport support;\r
+\r
+    /**\r
+     * Create resource binding. If session is not provided, this binding cannot instantiate resources.\r
+     * \r
+     * @param session or null\r
+     */\r
+    public ResourceBinding(Session session) {\r
+        super(Datatypes.LONG);\r
+//        if (session != null) {\r
+//            support = session.peekService(SerialisationSupport.class);\r
+//        }\r
+    }\r
+\r
+    public ResourceBinding(SerialisationSupport serializationSupport) {\r
+        super(Datatypes.LONG);\r
+//        this.support = serializationSupport;\r
+    }\r
+\r
+    /**\r
+     * @return current {@link SerialisationSupport}\r
+     * @throws BindingException\r
+     *             if not able to return a current {@link SerialisationSupport}\r
+     */\r
+    private SerialisationSupport getCurrentSupport() throws BindingException {\r
+        // FIXME: this is wrong but should be optimized if possible.\r
+//        if (support != null)\r
+//            return support;\r
+\r
+        Session s = SimanticsInternal.peekSession();\r
+        if ( s == null ) {\r
+            throw new BindingException("Cannot instantiate Resource without an alive database Session.");\r
+        }\r
+        SerialisationSupport support = s.peekService(SerialisationSupport.class);\r
+        if ( support == null ) {\r
+            throw new BindingException("Cannot instantiate Resource without an alive database Session.");\r
+        }\r
+        return support;\r
+    }\r
+\r
+    @Override\r
+    public Object create(long value) throws BindingException {\r
+       if(value == 0) return null;\r
+       SerialisationSupport support = getCurrentSupport();\r
+        try {\r
+            return support.getResource(value);\r
+        } catch (DatabaseException e) {\r
+            throw new BindingException(e);\r
+        }\r
+    }\r
+\r
+    @Override\r
+    public Object create(Long value) throws BindingException {\r
+       SerialisationSupport support = getCurrentSupport();\r
+        try {\r
+            return support.getResource(value.longValue());\r
+        } catch (DatabaseException e) {\r
+            throw new BindingException(e);\r
+        }\r
+    }\r
+\r
+    @Override\r
+    public Object create(Number value) throws BindingException {\r
+       SerialisationSupport support = getCurrentSupport();\r
+        try {\r
+            return support.getResource(value.longValue());\r
+        } catch (DatabaseException e) {\r
+            throw new BindingException(e);\r
+        }\r
+    }\r
+\r
+    @Override\r
+    public Object create(String value) throws BindingException {\r
+       SerialisationSupport support = getCurrentSupport();\r
+        try {\r
+            return support.getResource(Long.parseLong(value));\r
+        } catch (NumberFormatException e) {\r
+            throw new BindingException(e);\r
+        } catch (DatabaseException e) {\r
+            throw new BindingException(e);\r
+        }\r
+    }\r
+\r
+    @Override\r
+    public Long getValue(Object o) throws BindingException {\r
+       SerialisationSupport support = getCurrentSupport();\r
+        // NOTE: r.getResourceId() is unsafe for this purpose, it will just return 0 if it fails, thus corrupting anything serialized with this method\r
+//     Resource r = (Resource) o;\r
+//     return r.getResourceId();\r
+        try {\r
+            return support.getRandomAccessId((Resource)o);\r
+        } catch (DatabaseException e) {\r
+            throw new BindingException(e);\r
+        }\r
+    }\r
+\r
+    @Override\r
+    public long getValue_(Object o) throws BindingException {\r
+       SerialisationSupport support = getCurrentSupport();\r
+        // NOTE: r.getResourceId() is unsafe for this purpose, it will just return 0 if it fails, thus corrupting anything serialized with this method\r
+//     Resource r = (Resource) o;\r
+//     return r.getResourceId();\r
+        try {\r
+            return support.getRandomAccessId((Resource)o);\r
+        } catch (DatabaseException e) {\r
+            throw new BindingException(e);\r
+        }\r
+    }\r
+\r
+    @Override\r
+    public void setValue(Object obj, Number value) throws BindingException {\r
+        throw new BindingException("Resource is immutable");\r
+    }\r
+\r
+    @Override\r
+    public void setValue(Object obj, long value) throws BindingException {\r
+        throw new BindingException("Resource is immutable");\r
+    }\r
+    \r
+    @Override\r
+    public boolean isImmutable() {\r
+       return true;\r
+    }\r
+\r
+    @Override\r
+    public boolean isInstance(Object obj) {\r
+        return obj instanceof Resource;\r
+    }\r
+    \r
+    static {\r
+       RESOURCE_TYPE = new LongType();\r
+       RESOURCE_TYPE.metadata.put("resource", "true");\r
+    }\r
+}\r