1 /*******************************************************************************
2 * Copyright (c) 2007, 2018 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.layer0.bindings;
14 import org.simantics.databoard.Datatypes;
15 import org.simantics.databoard.binding.LongBinding;
16 import org.simantics.databoard.binding.error.BindingException;
17 import org.simantics.databoard.type.Datatype;
18 import org.simantics.databoard.type.LongType;
19 import org.simantics.db.Resource;
20 import org.simantics.db.Session;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.layer0.internal.SimanticsInternal;
23 import org.simantics.db.service.SerialisationSupport;
26 * This class binds Resource to LongType
28 * @author toni.kalajainen
30 public class ResourceBinding extends LongBinding {
32 public static final Datatype RESOURCE_TYPE;
35 * Create resource binding.
37 * @param session or null
39 public ResourceBinding() {
40 super(Datatypes.LONG);
44 * @return current {@link SerialisationSupport}
45 * @throws BindingException
46 * if not able to return a current {@link SerialisationSupport}
48 private SerialisationSupport getCurrentSupport() throws BindingException {
49 Session s = SimanticsInternal.peekSession();
51 throw new BindingException("Cannot instantiate Resource without an alive database Session.");
53 SerialisationSupport support = s.peekService(SerialisationSupport.class);
54 if ( support == null ) {
55 throw new BindingException("Cannot instantiate Resource without an alive database Session.");
61 public Object create(long value) throws BindingException {
62 if(value == 0) return null;
63 SerialisationSupport support = getCurrentSupport();
65 return support.getResource(value);
66 } catch (DatabaseException e) {
67 throw new BindingException(e);
72 public Object create(Long value) throws BindingException {
73 SerialisationSupport support = getCurrentSupport();
75 return support.getResource(value.longValue());
76 } catch (DatabaseException e) {
77 throw new BindingException(e);
82 public Object create(Number value) throws BindingException {
83 SerialisationSupport support = getCurrentSupport();
85 return support.getResource(value.longValue());
86 } catch (DatabaseException e) {
87 throw new BindingException(e);
92 public Object create(String value) throws BindingException {
93 SerialisationSupport support = getCurrentSupport();
95 return support.getResource(Long.parseLong(value));
96 } catch (NumberFormatException e) {
97 throw new BindingException(e);
98 } catch (DatabaseException e) {
99 throw new BindingException(e);
104 public Long getValue(Object o) throws BindingException {
105 SerialisationSupport support = getCurrentSupport();
106 // NOTE: r.getResourceId() is unsafe for this purpose, it will just return 0 if it fails, thus corrupting anything serialized with this method
108 return support.getRandomAccessId((Resource)o);
109 } catch (DatabaseException e) {
110 throw new BindingException(e);
115 public long getValue_(Object o) throws BindingException {
116 SerialisationSupport support = getCurrentSupport();
117 // NOTE: r.getResourceId() is unsafe for this purpose, it will just return 0 if it fails, thus corrupting anything serialized with this method
119 return support.getRandomAccessId((Resource)o);
120 } catch (DatabaseException e) {
121 throw new BindingException(e);
126 public void setValue(Object obj, Number value) throws BindingException {
127 throw new BindingException("Resource is immutable");
131 public void setValue(Object obj, long value) throws BindingException {
132 throw new BindingException("Resource is immutable");
136 public boolean isImmutable() {
141 public boolean isInstance(Object obj) {
142 return obj instanceof Resource;
146 RESOURCE_TYPE = new LongType();
147 RESOURCE_TYPE.metadata.put("resource", "true");