]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/VariableBinding.java
Merge commit 'ad8333027322fda6b9a8a524c7a7e15a54c52f38'
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / variable / VariableBinding.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
3  * 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.variable;\r
13 \r
14 import org.simantics.databoard.Datatypes;\r
15 import org.simantics.databoard.binding.StringBinding;\r
16 import org.simantics.databoard.binding.error.BindingException;\r
17 import org.simantics.db.Session;\r
18 import org.simantics.db.exception.DatabaseException;\r
19 import org.simantics.db.layer0.request.ResourceURIToVariable;\r
20 import org.simantics.db.layer0.request.VariableURI;\r
21 \r
22 /**\r
23  * This class binds Variable to StringType\r
24  *\r
25  * @author toni.kalajainen\r
26  */\r
27 public class VariableBinding extends StringBinding {\r
28 \r
29     Session session;\r
30 \r
31     public VariableBinding(Session session) {\r
32         super(Datatypes.STRING);\r
33         this.session = session;\r
34     }\r
35 \r
36     @Override\r
37     public boolean isImmutable() {\r
38         return true;\r
39     }\r
40 \r
41         @Override\r
42         public Object create(String value) throws BindingException {\r
43                 if ( session == null ) throw new BindingException("Cannot create Variable without a Session");\r
44                 try {\r
45                         return session.sync( new ResourceURIToVariable( value ) );\r
46                 } catch (DatabaseException e) {\r
47                         throw new BindingException(e);\r
48                 }\r
49         }\r
50 \r
51         @Override\r
52         public String getValue(Object o) throws BindingException {\r
53                 if ( session == null ) throw new BindingException("Cannot create Variable without a Session");\r
54                 if (o instanceof Variable==false) throw new BindingException("Not a variable");\r
55                 Variable v = (Variable) o;\r
56                 try {\r
57                         return (String) session.sync( new VariableURI( v ) );\r
58                 } catch (DatabaseException e) {\r
59                         throw new BindingException( e );\r
60                 }\r
61         }\r
62 \r
63         @Override\r
64         public void setValue(Object o, String newValue) throws BindingException {\r
65                 throw new BindingException("Cannot set URI to Variable. URI is immutable");\r
66         }\r
67 \r
68         @Override\r
69         public boolean isInstance(Object obj) {\r
70                 return obj instanceof Variable;\r
71         }\r
72 \r
73 }\r