1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.db.layer0.bindings;
\r
14 import org.simantics.databoard.Datatypes;
\r
15 import org.simantics.databoard.binding.LongBinding;
\r
16 import org.simantics.databoard.binding.error.BindingException;
\r
17 import org.simantics.databoard.type.Datatype;
\r
18 import org.simantics.databoard.type.LongType;
\r
19 import org.simantics.db.Resource;
\r
20 import org.simantics.db.Session;
\r
21 import org.simantics.db.exception.DatabaseException;
\r
22 import org.simantics.db.layer0.internal.SimanticsInternal;
\r
23 import org.simantics.db.service.SerialisationSupport;
\r
26 * This class binds Resource to LongType
\r
28 * @author toni.kalajainen
\r
30 public class ResourceBinding extends LongBinding {
\r
32 public static final Datatype RESOURCE_TYPE;
\r
34 //SerialisationSupport support;
\r
37 * Create resource binding. If session is not provided, this binding cannot instantiate resources.
\r
39 * @param session or null
\r
41 public ResourceBinding(Session session) {
\r
42 super(Datatypes.LONG);
\r
43 // if (session != null) {
\r
44 // support = session.peekService(SerialisationSupport.class);
\r
48 public ResourceBinding(SerialisationSupport serializationSupport) {
\r
49 super(Datatypes.LONG);
\r
50 // this.support = serializationSupport;
\r
54 * @return current {@link SerialisationSupport}
\r
55 * @throws BindingException
\r
56 * if not able to return a current {@link SerialisationSupport}
\r
58 private SerialisationSupport getCurrentSupport() throws BindingException {
\r
59 // FIXME: this is wrong but should be optimized if possible.
\r
60 // if (support != null)
\r
63 Session s = SimanticsInternal.peekSession();
\r
65 throw new BindingException("Cannot instantiate Resource without an alive database Session.");
\r
67 SerialisationSupport support = s.peekService(SerialisationSupport.class);
\r
68 if ( support == null ) {
\r
69 throw new BindingException("Cannot instantiate Resource without an alive database Session.");
\r
75 public Object create(long value) throws BindingException {
\r
76 if(value == 0) return null;
\r
77 SerialisationSupport support = getCurrentSupport();
\r
79 return support.getResource(value);
\r
80 } catch (DatabaseException e) {
\r
81 throw new BindingException(e);
\r
86 public Object create(Long value) throws BindingException {
\r
87 SerialisationSupport support = getCurrentSupport();
\r
89 return support.getResource(value.longValue());
\r
90 } catch (DatabaseException e) {
\r
91 throw new BindingException(e);
\r
96 public Object create(Number value) throws BindingException {
\r
97 SerialisationSupport support = getCurrentSupport();
\r
99 return support.getResource(value.longValue());
\r
100 } catch (DatabaseException e) {
\r
101 throw new BindingException(e);
\r
106 public Object create(String value) throws BindingException {
\r
107 SerialisationSupport support = getCurrentSupport();
\r
109 return support.getResource(Long.parseLong(value));
\r
110 } catch (NumberFormatException e) {
\r
111 throw new BindingException(e);
\r
112 } catch (DatabaseException e) {
\r
113 throw new BindingException(e);
\r
118 public Long getValue(Object o) throws BindingException {
\r
119 SerialisationSupport support = getCurrentSupport();
\r
120 // NOTE: r.getResourceId() is unsafe for this purpose, it will just return 0 if it fails, thus corrupting anything serialized with this method
\r
121 // Resource r = (Resource) o;
\r
122 // return r.getResourceId();
\r
124 return support.getRandomAccessId((Resource)o);
\r
125 } catch (DatabaseException e) {
\r
126 throw new BindingException(e);
\r
131 public long getValue_(Object o) throws BindingException {
\r
132 SerialisationSupport support = getCurrentSupport();
\r
133 // NOTE: r.getResourceId() is unsafe for this purpose, it will just return 0 if it fails, thus corrupting anything serialized with this method
\r
134 // Resource r = (Resource) o;
\r
135 // return r.getResourceId();
\r
137 return support.getRandomAccessId((Resource)o);
\r
138 } catch (DatabaseException e) {
\r
139 throw new BindingException(e);
\r
144 public void setValue(Object obj, Number value) throws BindingException {
\r
145 throw new BindingException("Resource is immutable");
\r
149 public void setValue(Object obj, long value) throws BindingException {
\r
150 throw new BindingException("Resource is immutable");
\r
154 public boolean isImmutable() {
\r
159 public boolean isInstance(Object obj) {
\r
160 return obj instanceof Resource;
\r
164 RESOURCE_TYPE = new LongType();
\r
165 RESOURCE_TYPE.metadata.put("resource", "true");
\r