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