1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 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;
34 //SerialisationSupport support;
37 * Create resource binding. If session is not provided, this binding cannot instantiate resources.
39 * @param session or null
41 public ResourceBinding(Session session) {
42 super(Datatypes.LONG);
43 // if (session != null) {
44 // support = session.peekService(SerialisationSupport.class);
48 public ResourceBinding(SerialisationSupport serializationSupport) {
49 super(Datatypes.LONG);
50 // this.support = serializationSupport;
54 * @return current {@link SerialisationSupport}
55 * @throws BindingException
56 * if not able to return a current {@link SerialisationSupport}
58 private SerialisationSupport getCurrentSupport() throws BindingException {
59 // FIXME: this is wrong but should be optimized if possible.
60 // if (support != null)
63 Session s = SimanticsInternal.peekSession();
65 throw new BindingException("Cannot instantiate Resource without an alive database Session.");
67 SerialisationSupport support = s.peekService(SerialisationSupport.class);
68 if ( support == null ) {
69 throw new BindingException("Cannot instantiate Resource without an alive database Session.");
75 public Object create(long value) throws BindingException {
76 if(value == 0) return null;
77 SerialisationSupport support = getCurrentSupport();
79 return support.getResource(value);
80 } catch (DatabaseException e) {
81 throw new BindingException(e);
86 public Object create(Long value) throws BindingException {
87 SerialisationSupport support = getCurrentSupport();
89 return support.getResource(value.longValue());
90 } catch (DatabaseException e) {
91 throw new BindingException(e);
96 public Object create(Number value) throws BindingException {
97 SerialisationSupport support = getCurrentSupport();
99 return support.getResource(value.longValue());
100 } catch (DatabaseException e) {
101 throw new BindingException(e);
106 public Object create(String value) throws BindingException {
107 SerialisationSupport support = getCurrentSupport();
109 return support.getResource(Long.parseLong(value));
110 } catch (NumberFormatException e) {
111 throw new BindingException(e);
112 } catch (DatabaseException e) {
113 throw new BindingException(e);
118 public Long getValue(Object o) throws BindingException {
119 SerialisationSupport support = getCurrentSupport();
120 // NOTE: r.getResourceId() is unsafe for this purpose, it will just return 0 if it fails, thus corrupting anything serialized with this method
121 // Resource r = (Resource) o;
122 // return r.getResourceId();
124 return support.getRandomAccessId((Resource)o);
125 } catch (DatabaseException e) {
126 throw new BindingException(e);
131 public long getValue_(Object o) throws BindingException {
132 SerialisationSupport support = getCurrentSupport();
133 // NOTE: r.getResourceId() is unsafe for this purpose, it will just return 0 if it fails, thus corrupting anything serialized with this method
134 // Resource r = (Resource) o;
135 // return r.getResourceId();
137 return support.getRandomAccessId((Resource)o);
138 } catch (DatabaseException e) {
139 throw new BindingException(e);
144 public void setValue(Object obj, Number value) throws BindingException {
145 throw new BindingException("Resource is immutable");
149 public void setValue(Object obj, long value) throws BindingException {
150 throw new BindingException("Resource is immutable");
154 public boolean isImmutable() {
159 public boolean isInstance(Object obj) {
160 return obj instanceof Resource;
164 RESOURCE_TYPE = new LongType();
165 RESOURCE_TYPE.metadata.put("resource", "true");