]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/bindings/ResourceBinding.java
Initial support for concurrency in databoard, bindings and serializers
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / bindings / ResourceBinding.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2018 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.layer0.bindings;
13
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;
24
25 /**
26  * This class binds Resource to LongType
27  * 
28  * @author toni.kalajainen
29  */
30 public class ResourceBinding extends LongBinding {
31
32     public static final Datatype RESOURCE_TYPE;
33
34     /**
35      * Create resource binding.
36      * 
37      * @param session or null
38      */
39     public ResourceBinding() {
40         super(Datatypes.LONG);
41     }
42
43     /**
44      * @return current {@link SerialisationSupport}
45      * @throws BindingException
46      *             if not able to return a current {@link SerialisationSupport}
47      */
48     private SerialisationSupport getCurrentSupport() throws BindingException {
49         Session s = SimanticsInternal.peekSession();
50         if ( s == null ) {
51             throw new BindingException("Cannot instantiate Resource without an alive database Session.");
52         }
53         SerialisationSupport support = s.peekService(SerialisationSupport.class);
54         if ( support == null ) {
55             throw new BindingException("Cannot instantiate Resource without an alive database Session.");
56         }
57         return support;
58     }
59
60     @Override
61     public Object create(long value) throws BindingException {
62         if(value == 0) return null;
63         SerialisationSupport support = getCurrentSupport();
64         try {
65             return support.getResource(value);
66         } catch (DatabaseException e) {
67             throw new BindingException(e);
68         }
69     }
70
71     @Override
72     public Object create(Long value) throws BindingException {
73         SerialisationSupport support = getCurrentSupport();
74         try {
75             return support.getResource(value.longValue());
76         } catch (DatabaseException e) {
77             throw new BindingException(e);
78         }
79     }
80
81     @Override
82     public Object create(Number value) throws BindingException {
83         SerialisationSupport support = getCurrentSupport();
84         try {
85             return support.getResource(value.longValue());
86         } catch (DatabaseException e) {
87             throw new BindingException(e);
88         }
89     }
90
91     @Override
92     public Object create(String value) throws BindingException {
93         SerialisationSupport support = getCurrentSupport();
94         try {
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);
100         }
101     }
102
103     @Override
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
107         try {
108             return support.getRandomAccessId((Resource)o);
109         } catch (DatabaseException e) {
110             throw new BindingException(e);
111         }
112     }
113
114     @Override
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
118         try {
119             return support.getRandomAccessId((Resource)o);
120         } catch (DatabaseException e) {
121             throw new BindingException(e);
122         }
123     }
124
125     @Override
126     public void setValue(Object obj, Number value) throws BindingException {
127         throw new BindingException("Resource is immutable");
128     }
129
130     @Override
131     public void setValue(Object obj, long value) throws BindingException {
132         throw new BindingException("Resource is immutable");
133     }
134     
135     @Override
136     public boolean isImmutable() {
137         return true;
138     }
139
140     @Override
141     public boolean isInstance(Object obj) {
142         return obj instanceof Resource;
143     }
144     
145     static {
146         RESOURCE_TYPE = new LongType();
147         RESOURCE_TYPE.metadata.put("resource", "true");
148     }
149 }