X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.layer0%2Fsrc%2Forg%2Fsimantics%2Fdb%2Flayer0%2Fvariable%2FSCLValueAccessor.java;h=082caa603ba486bf4319f102434d184c5fdedd14;hb=15af8a20abe8b2ba24b52c9da8bce6c92351dc43;hp=82dcec1c63485c4dc38565a5f886dd2f8dc2861e;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/SCLValueAccessor.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/SCLValueAccessor.java index 82dcec1c6..082caa603 100644 --- a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/SCLValueAccessor.java +++ b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/SCLValueAccessor.java @@ -1,96 +1,99 @@ -package org.simantics.db.layer0.variable; - -import org.simantics.databoard.binding.Binding; -import org.simantics.databoard.type.Datatype; -import org.simantics.db.ReadGraph; -import org.simantics.db.WriteGraph; -import org.simantics.db.exception.DatabaseException; -import org.simantics.scl.runtime.SCLContext; -import org.simantics.scl.runtime.function.Function1; -import org.simantics.scl.runtime.function.Function2; -import org.simantics.scl.runtime.function.Function3; - -public class SCLValueAccessor implements ValueAccessor { - - private final Function1 getValue1; - private final Function2 getValue2; - private final Function2 setValue2; - private final Function3 setValue3; - private final Function1 getDatatype; - - public SCLValueAccessor(Function1 getValue1, Function2 getValue2, - Function2 setValue2, Function3 setValue3, - Function1 getDatatype) { - this.getValue1 = getValue1; - this.getValue2 = getValue2; - this.setValue2 = setValue2; - this.setValue3 = setValue3; - this.getDatatype = getDatatype; - } - - @Override - public Object getValue(ReadGraph graph, Variable context) throws DatabaseException { - SCLContext sclContext = SCLContext.getCurrent(); - Object oldGraph = sclContext.put("graph", graph); - try { - return getValue1.apply(context); - } catch (Throwable t) { - throw new DatabaseException(t); - } finally { - sclContext.put("graph", oldGraph); - } - } - - @Override - public Object getValue(ReadGraph graph, Variable context, Binding binding) throws DatabaseException { - SCLContext sclContext = SCLContext.getCurrent(); - Object oldGraph = sclContext.put("graph", graph); - try { - return getValue2.apply(context, binding); - } catch (Throwable t) { - throw new DatabaseException(t); - } finally { - sclContext.put("graph", oldGraph); - } - } - - @Override - public void setValue(WriteGraph graph, Variable context, Object value) throws DatabaseException { - SCLContext sclContext = SCLContext.getCurrent(); - Object oldGraph = sclContext.put("graph", graph); - try { - setValue2.apply(context, value); - } catch (Throwable t) { - throw new DatabaseException(t); - } finally { - sclContext.put("graph", oldGraph); - } - } - - @Override - public void setValue(WriteGraph graph, Variable context, Object value, Binding binding) throws DatabaseException { - SCLContext sclContext = SCLContext.getCurrent(); - Object oldGraph = sclContext.put("graph", graph); - try { - setValue3.apply(context, value, binding); - } catch (Throwable t) { - throw new DatabaseException(t); - } finally { - sclContext.put("graph", oldGraph); - } - } - - @Override - public Datatype getDatatype(ReadGraph graph, Variable context) throws DatabaseException { - SCLContext sclContext = SCLContext.getCurrent(); - Object oldGraph = sclContext.put("graph", graph); - try { - return getDatatype.apply(context); - } catch (Throwable t) { - throw new DatabaseException(t); - } finally { - sclContext.put("graph", oldGraph); - } - } - -} +package org.simantics.db.layer0.variable; + +import org.simantics.databoard.binding.Binding; +import org.simantics.databoard.type.Datatype; +import org.simantics.db.ReadGraph; +import org.simantics.db.WriteGraph; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.exception.DatatypeNotFoundException; +import org.simantics.db.layer0.exception.MissingVariableValueException; +import org.simantics.db.layer0.exception.NonWritableVariableException; +import org.simantics.scl.runtime.SCLContext; +import org.simantics.scl.runtime.function.Function1; +import org.simantics.scl.runtime.function.Function2; +import org.simantics.scl.runtime.function.Function3; + +public class SCLValueAccessor implements ValueAccessor { + + private final Function1 getValue1; + private final Function2 getValue2; + private final Function2 setValue2; + private final Function3 setValue3; + private final Function1 getDatatype; + + public SCLValueAccessor(Function1 getValue1, Function2 getValue2, + Function2 setValue2, Function3 setValue3, + Function1 getDatatype) { + this.getValue1 = getValue1; + this.getValue2 = getValue2; + this.setValue2 = setValue2; + this.setValue3 = setValue3; + this.getDatatype = getDatatype; + } + + @Override + public Object getValue(ReadGraph graph, Variable context) throws DatabaseException { + SCLContext sclContext = SCLContext.getCurrent(); + Object oldGraph = sclContext.put("graph", graph); + try { + return getValue1.apply(context); + } catch (Throwable t) { + throw new MissingVariableValueException("Could not get value for " + String.valueOf(context.getRepresents(graph)), t); + } finally { + sclContext.put("graph", oldGraph); + } + } + + @Override + public Object getValue(ReadGraph graph, Variable context, Binding binding) throws DatabaseException { + SCLContext sclContext = SCLContext.getCurrent(); + Object oldGraph = sclContext.put("graph", graph); + try { + return getValue2.apply(context, binding); + } catch (Throwable t) { + throw new MissingVariableValueException("Could not get value for " + String.valueOf(context.getRepresents(graph)) + " with binding " + binding, t); + } finally { + sclContext.put("graph", oldGraph); + } + } + + @Override + public void setValue(WriteGraph graph, Variable context, Object value) throws DatabaseException { + SCLContext sclContext = SCLContext.getCurrent(); + Object oldGraph = sclContext.put("graph", graph); + try { + setValue2.apply(context, value); + } catch (Throwable t) { + throw new NonWritableVariableException("Could not write value " + String.valueOf(value) + " for " + String.valueOf(context.getRepresents(graph)), t); + } finally { + sclContext.put("graph", oldGraph); + } + } + + @Override + public void setValue(WriteGraph graph, Variable context, Object value, Binding binding) throws DatabaseException { + SCLContext sclContext = SCLContext.getCurrent(); + Object oldGraph = sclContext.put("graph", graph); + try { + setValue3.apply(context, value, binding); + } catch (Throwable t) { + throw new NonWritableVariableException("Could not write value " + String.valueOf(value) + " for " + String.valueOf(context.getRepresents(graph)) + " with binding " + binding, t); + } finally { + sclContext.put("graph", oldGraph); + } + } + + @Override + public Datatype getDatatype(ReadGraph graph, Variable context) throws DatabaseException { + SCLContext sclContext = SCLContext.getCurrent(); + Object oldGraph = sclContext.put("graph", graph); + try { + return getDatatype.apply(context); + } catch (Throwable t) { + throw new DatatypeNotFoundException("Could not find datatype for " + String.valueOf(context.getRepresents(graph)), t); + } finally { + sclContext.put("graph", oldGraph); + } + } + +}