]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/bindings/ResourceBinding.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / bindings / ResourceBinding.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.db.layer0.bindings;\r
13 \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
24 \r
25 /**\r
26  * This class binds Resource to LongType\r
27  * \r
28  * @author toni.kalajainen\r
29  */\r
30 public class ResourceBinding extends LongBinding {\r
31 \r
32     public static final Datatype RESOURCE_TYPE;\r
33 \r
34     //SerialisationSupport support;\r
35 \r
36     /**\r
37      * Create resource binding. If session is not provided, this binding cannot instantiate resources.\r
38      * \r
39      * @param session or null\r
40      */\r
41     public ResourceBinding(Session session) {\r
42         super(Datatypes.LONG);\r
43 //        if (session != null) {\r
44 //            support = session.peekService(SerialisationSupport.class);\r
45 //        }\r
46     }\r
47 \r
48     public ResourceBinding(SerialisationSupport serializationSupport) {\r
49         super(Datatypes.LONG);\r
50 //        this.support = serializationSupport;\r
51     }\r
52 \r
53     /**\r
54      * @return current {@link SerialisationSupport}\r
55      * @throws BindingException\r
56      *             if not able to return a current {@link SerialisationSupport}\r
57      */\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
61 //            return support;\r
62 \r
63         Session s = SimanticsInternal.peekSession();\r
64         if ( s == null ) {\r
65             throw new BindingException("Cannot instantiate Resource without an alive database Session.");\r
66         }\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
70         }\r
71         return support;\r
72     }\r
73 \r
74     @Override\r
75     public Object create(long value) throws BindingException {\r
76         if(value == 0) return null;\r
77         SerialisationSupport support = getCurrentSupport();\r
78         try {\r
79             return support.getResource(value);\r
80         } catch (DatabaseException e) {\r
81             throw new BindingException(e);\r
82         }\r
83     }\r
84 \r
85     @Override\r
86     public Object create(Long value) throws BindingException {\r
87         SerialisationSupport support = getCurrentSupport();\r
88         try {\r
89             return support.getResource(value.longValue());\r
90         } catch (DatabaseException e) {\r
91             throw new BindingException(e);\r
92         }\r
93     }\r
94 \r
95     @Override\r
96     public Object create(Number value) throws BindingException {\r
97         SerialisationSupport support = getCurrentSupport();\r
98         try {\r
99             return support.getResource(value.longValue());\r
100         } catch (DatabaseException e) {\r
101             throw new BindingException(e);\r
102         }\r
103     }\r
104 \r
105     @Override\r
106     public Object create(String value) throws BindingException {\r
107         SerialisationSupport support = getCurrentSupport();\r
108         try {\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
114         }\r
115     }\r
116 \r
117     @Override\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
123         try {\r
124             return support.getRandomAccessId((Resource)o);\r
125         } catch (DatabaseException e) {\r
126             throw new BindingException(e);\r
127         }\r
128     }\r
129 \r
130     @Override\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
136         try {\r
137             return support.getRandomAccessId((Resource)o);\r
138         } catch (DatabaseException e) {\r
139             throw new BindingException(e);\r
140         }\r
141     }\r
142 \r
143     @Override\r
144     public void setValue(Object obj, Number value) throws BindingException {\r
145         throw new BindingException("Resource is immutable");\r
146     }\r
147 \r
148     @Override\r
149     public void setValue(Object obj, long value) throws BindingException {\r
150         throw new BindingException("Resource is immutable");\r
151     }\r
152     \r
153     @Override\r
154     public boolean isImmutable() {\r
155         return true;\r
156     }\r
157 \r
158     @Override\r
159     public boolean isInstance(Object obj) {\r
160         return obj instanceof Resource;\r
161     }\r
162     \r
163     static {\r
164         RESOURCE_TYPE = new LongType();\r
165         RESOURCE_TYPE.metadata.put("resource", "true");\r
166     }\r
167 }\r