]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/bindings/ResourceBinding.java
Fail safe import fixes made by Antti
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / bindings / ResourceBinding.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 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     //SerialisationSupport support;
35
36     /**
37      * Create resource binding. If session is not provided, this binding cannot instantiate resources.
38      * 
39      * @param session or null
40      */
41     public ResourceBinding(Session session) {
42         super(Datatypes.LONG);
43 //        if (session != null) {
44 //            support = session.peekService(SerialisationSupport.class);
45 //        }
46     }
47
48     public ResourceBinding(SerialisationSupport serializationSupport) {
49         super(Datatypes.LONG);
50 //        this.support = serializationSupport;
51     }
52
53     /**
54      * @return current {@link SerialisationSupport}
55      * @throws BindingException
56      *             if not able to return a current {@link SerialisationSupport}
57      */
58     private SerialisationSupport getCurrentSupport() throws BindingException {
59         // FIXME: this is wrong but should be optimized if possible.
60 //        if (support != null)
61 //            return support;
62
63         Session s = SimanticsInternal.peekSession();
64         if ( s == null ) {
65             throw new BindingException("Cannot instantiate Resource without an alive database Session.");
66         }
67         SerialisationSupport support = s.peekService(SerialisationSupport.class);
68         if ( support == null ) {
69             throw new BindingException("Cannot instantiate Resource without an alive database Session.");
70         }
71         return support;
72     }
73
74     @Override
75     public Object create(long value) throws BindingException {
76         if(value == 0) return null;
77         SerialisationSupport support = getCurrentSupport();
78         try {
79             return support.getResource(value);
80         } catch (DatabaseException e) {
81             throw new BindingException(e);
82         }
83     }
84
85     @Override
86     public Object create(Long value) throws BindingException {
87         SerialisationSupport support = getCurrentSupport();
88         try {
89             return support.getResource(value.longValue());
90         } catch (DatabaseException e) {
91             throw new BindingException(e);
92         }
93     }
94
95     @Override
96     public Object create(Number value) throws BindingException {
97         SerialisationSupport support = getCurrentSupport();
98         try {
99             return support.getResource(value.longValue());
100         } catch (DatabaseException e) {
101             throw new BindingException(e);
102         }
103     }
104
105     @Override
106     public Object create(String value) throws BindingException {
107         SerialisationSupport support = getCurrentSupport();
108         try {
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);
114         }
115     }
116
117     @Override
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();
123         try {
124             return support.getRandomAccessId((Resource)o);
125         } catch (DatabaseException e) {
126             throw new BindingException(e);
127         }
128     }
129
130     @Override
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();
136         try {
137             return support.getRandomAccessId((Resource)o);
138         } catch (DatabaseException e) {
139             throw new BindingException(e);
140         }
141     }
142
143     @Override
144     public void setValue(Object obj, Number value) throws BindingException {
145         throw new BindingException("Resource is immutable");
146     }
147
148     @Override
149     public void setValue(Object obj, long value) throws BindingException {
150         throw new BindingException("Resource is immutable");
151     }
152     
153     @Override
154     public boolean isImmutable() {
155         return true;
156     }
157
158     @Override
159     public boolean isInstance(Object obj) {
160         return obj instanceof Resource;
161     }
162     
163     static {
164         RESOURCE_TYPE = new LongType();
165         RESOURCE_TYPE.metadata.put("resource", "true");
166     }
167 }