-/*******************************************************************************\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
+/*******************************************************************************
+ * Copyright (c) 2007, 2018 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.db.layer0.bindings;
+
+import org.simantics.databoard.Datatypes;
+import org.simantics.databoard.binding.LongBinding;
+import org.simantics.databoard.binding.error.BindingException;
+import org.simantics.databoard.type.Datatype;
+import org.simantics.databoard.type.LongType;
+import org.simantics.db.Resource;
+import org.simantics.db.Session;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.internal.SimanticsInternal;
+import org.simantics.db.service.SerialisationSupport;
+
+/**
+ * This class binds Resource to LongType
+ *
+ * @author toni.kalajainen
+ */
+public class ResourceBinding extends LongBinding {
+
+ public static final Datatype RESOURCE_TYPE;
+
+ /**
+ * Create resource binding.
+ *
+ * @param session or null
+ */
+ public ResourceBinding() {
+ super(Datatypes.LONG);
+ }
+
+ /**
+ * @return current {@link SerialisationSupport}
+ * @throws BindingException
+ * if not able to return a current {@link SerialisationSupport}
+ */
+ private SerialisationSupport getCurrentSupport() throws BindingException {
+ Session s = SimanticsInternal.peekSession();
+ if ( s == null ) {
+ throw new BindingException("Cannot instantiate Resource without an alive database Session.");
+ }
+ SerialisationSupport support = s.peekService(SerialisationSupport.class);
+ if ( support == null ) {
+ throw new BindingException("Cannot instantiate Resource without an alive database Session.");
+ }
+ return support;
+ }
+
+ @Override
+ public Object create(long value) throws BindingException {
+ if(value == 0) return null;
+ SerialisationSupport support = getCurrentSupport();
+ try {
+ return support.getResource(value);
+ } catch (DatabaseException e) {
+ throw new BindingException(e);
+ }
+ }
+
+ @Override
+ public Object create(Long value) throws BindingException {
+ SerialisationSupport support = getCurrentSupport();
+ try {
+ return support.getResource(value.longValue());
+ } catch (DatabaseException e) {
+ throw new BindingException(e);
+ }
+ }
+
+ @Override
+ public Object create(Number value) throws BindingException {
+ SerialisationSupport support = getCurrentSupport();
+ try {
+ return support.getResource(value.longValue());
+ } catch (DatabaseException e) {
+ throw new BindingException(e);
+ }
+ }
+
+ @Override
+ public Object create(String value) throws BindingException {
+ SerialisationSupport support = getCurrentSupport();
+ try {
+ return support.getResource(Long.parseLong(value));
+ } catch (NumberFormatException e) {
+ throw new BindingException(e);
+ } catch (DatabaseException e) {
+ throw new BindingException(e);
+ }
+ }
+
+ @Override
+ public Long getValue(Object o) throws BindingException {
+ SerialisationSupport support = getCurrentSupport();
+ // NOTE: r.getResourceId() is unsafe for this purpose, it will just return 0 if it fails, thus corrupting anything serialized with this method
+ try {
+ return support.getRandomAccessId((Resource)o);
+ } catch (DatabaseException e) {
+ throw new BindingException(e);
+ }
+ }
+
+ @Override
+ public long getValue_(Object o) throws BindingException {
+ SerialisationSupport support = getCurrentSupport();
+ // NOTE: r.getResourceId() is unsafe for this purpose, it will just return 0 if it fails, thus corrupting anything serialized with this method
+ try {
+ return support.getRandomAccessId((Resource)o);
+ } catch (DatabaseException e) {
+ throw new BindingException(e);
+ }
+ }
+
+ @Override
+ public void setValue(Object obj, Number value) throws BindingException {
+ throw new BindingException("Resource is immutable");
+ }
+
+ @Override
+ public void setValue(Object obj, long value) throws BindingException {
+ throw new BindingException("Resource is immutable");
+ }
+
+ @Override
+ public boolean isImmutable() {
+ return true;
+ }
+
+ @Override
+ public boolean isInstance(Object obj) {
+ return obj instanceof Resource;
+ }
+
+ static {
+ RESOURCE_TYPE = new LongType();
+ RESOURCE_TYPE.metadata.put("resource", "true");
+ }
+}