]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/StringModifierImpl.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / adapter / impl / StringModifierImpl.java
index ab2d7a9c5dbdcf31fdb646f5996bf25468192b0e..d23fd1f27e5b1ad21940bdf886391f984504a858 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.db.layer0.adapter.impl;\r
-\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.databoard.binding.Binding;\r
-import org.simantics.databoard.binding.error.BindingException;\r
-import org.simantics.databoard.parser.repository.DataTypeSyntaxError;\r
-import org.simantics.databoard.parser.repository.DataValueRepository;\r
-import org.simantics.databoard.type.ArrayType;\r
-import org.simantics.databoard.type.BooleanType;\r
-import org.simantics.databoard.type.ByteType;\r
-import org.simantics.databoard.type.Datatype;\r
-import org.simantics.databoard.type.DoubleType;\r
-import org.simantics.databoard.type.FloatType;\r
-import org.simantics.databoard.type.IntegerType;\r
-import org.simantics.databoard.type.LongType;\r
-import org.simantics.databoard.type.StringType;\r
-import org.simantics.databoard.units.IUnitConverter;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.VirtualGraph;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.request.WriteRequest;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.exception.ValidationException;\r
-import org.simantics.db.layer0.adapter.AbstractStringModifier;\r
-import org.simantics.db.layer0.util.PrimitiveValueParser;\r
-import org.simantics.db.layer0.util.PrimitiveValueParser.BooleanArrayValidator;\r
-import org.simantics.db.layer0.util.PrimitiveValueParser.BooleanValidator;\r
-import org.simantics.db.layer0.util.PrimitiveValueParser.ByteArrayValidator;\r
-import org.simantics.db.layer0.util.PrimitiveValueParser.DoubleArrayValidator;\r
-import org.simantics.db.layer0.util.PrimitiveValueParser.DoubleValidator;\r
-import org.simantics.db.layer0.util.PrimitiveValueParser.FloatArrayValidator;\r
-import org.simantics.db.layer0.util.PrimitiveValueParser.FloatValidator;\r
-import org.simantics.db.layer0.util.PrimitiveValueParser.IValidator;\r
-import org.simantics.db.layer0.util.PrimitiveValueParser.IntegerArrayValidator;\r
-import org.simantics.db.layer0.util.PrimitiveValueParser.IntegerValidator;\r
-import org.simantics.db.layer0.util.PrimitiveValueParser.LongArrayValidator;\r
-import org.simantics.db.layer0.util.PrimitiveValueParser.LongValidator;\r
-import org.simantics.db.service.VirtualGraphSupport;\r
-import org.simantics.layer0.Layer0;\r
-\r
-public final class StringModifierImpl extends AbstractStringModifier {\r
-\r
-    final Resource resource;\r
-    final IUnitConverter converter;\r
-    IValidator     validator;\r
-\r
-    public StringModifierImpl(ReadGraph g, Resource resource) throws DatabaseException {\r
-        this(g, resource, null);\r
-    }\r
-\r
-    public StringModifierImpl(ReadGraph g, Resource resource, IUnitConverter converter) throws DatabaseException {\r
-       super(resource);\r
-        this.resource = resource;\r
-        this.validator = null;\r
-        this.converter = converter;\r
-\r
-        // Resolve validator if any\r
-        Layer0 l0 = Layer0.getInstance(g);\r
-        validator = PrimitiveValueParser.NON_MODIFIABLE_VALIDATOR;\r
-\r
-        Datatype type = g.getPossibleRelatedValue(resource, l0.HasDataType,\r
-                Bindings.getBindingUnchecked(Datatype.class));\r
-        if (type == null)\r
-            return;\r
-\r
-        if (type instanceof DoubleType) {\r
-            validator = new DoubleValidator();\r
-        } else if (type instanceof StringType) {\r
-            validator = null;\r
-        } else if (type instanceof ByteType) {\r
-            validator = new ByteArrayValidator();\r
-        } else if (type instanceof BooleanType) {\r
-            validator = new BooleanValidator();\r
-        } else if (type instanceof IntegerType) {\r
-            validator = new IntegerValidator();\r
-        } else if (type instanceof LongType) {\r
-            validator = new LongValidator();\r
-        } else if (type instanceof FloatType) {\r
-            validator = new FloatValidator();\r
-        } else if(type instanceof ArrayType) {\r
-            ArrayType arrayType = (ArrayType)type; \r
-            type = arrayType.componentType();\r
-            if (type instanceof DoubleType) {\r
-                validator = new DoubleArrayValidator();\r
-            } else if (type instanceof StringType) {\r
-                validator = null;\r
-            } else if (type instanceof ByteType) {\r
-                validator = new ByteArrayValidator();\r
-            } else if (type instanceof BooleanType) {\r
-                validator = new BooleanArrayValidator();\r
-            } else if (type instanceof IntegerType) {\r
-                validator = new IntegerArrayValidator();\r
-            } else if (type instanceof LongType) {\r
-                validator = new LongArrayValidator();\r
-            } else if (type instanceof FloatType) {\r
-                validator = new FloatArrayValidator();\r
-            }\r
-        }\r
-    }\r
-\r
-    @Override\r
-    public String isValid(String value) {\r
-        return validator == null ? null : validator.isValid(value);\r
-    }\r
-\r
-    @Override\r
-    public final void modify(WriteGraph graph, final String value) throws DatabaseException {\r
-        VirtualGraphSupport vgs = graph.getService(VirtualGraphSupport.class);\r
-        VirtualGraph vg = vgs.getGraph(graph, resource);\r
-        if (vg != null) {\r
-            graph.syncRequest(new WriteRequest(vg) {\r
-                @Override\r
-                public void perform(WriteGraph graph) throws DatabaseException {\r
-                    modify0(graph, value);\r
-                }\r
-            });\r
-        } else {\r
-            modify0(graph, value);\r
-        }\r
-    }\r
-\r
-    protected void modify0(WriteGraph graph, String value) throws DatabaseException {\r
-        Layer0 l0 = Layer0.getInstance(graph);\r
-\r
-        try {\r
-            // FIXME: no support for StringArray!\r
-            Datatype type = graph.getDataType(resource);\r
-            if (type == null)\r
-                return;\r
-\r
-            if (type instanceof StringType) {\r
-                // We do not want to parse string types with databoard since it requires the string to be within quotes "".\r
-                graph.claimValue(resource, value, Bindings.STRING);\r
-                return;\r
-            }\r
-\r
-//            } else if (type instanceof DoubleType) {\r
-//             double v = PrimitiveValueParser.parseDouble(value);\r
-//             if(converter != null) v = converter.convert(v);\r
-//                graph.claimValue(resource, v, Bindings.DOUBLE);\r
-//            } else if (type instanceof ByteType) {\r
-//                graph.claimValue(resource, PrimitiveValueParser.parseByte(value), Bindings.BYTE);\r
-//            } else if (type instanceof BooleanType) {\r
-//                graph.claimValue(resource, PrimitiveValueParser.parseBoolean(value), Bindings.BOOLEAN);\r
-//            } else if (type instanceof IntegerType) {\r
-//                graph.claimValue(resource, PrimitiveValueParser.parseInt(value), Bindings.INTEGER);\r
-//            } else if (type instanceof LongType) {\r
-//                graph.claimValue(resource, PrimitiveValueParser.parseLong(value), Bindings.LONG);\r
-//            } else if (type instanceof FloatType) {\r
-//                graph.claimValue(resource, PrimitiveValueParser.parseFloat(value), Bindings.FLOAT);\r
-//            } else if (graph.getPossibleRelatedValue(resource, l0.HasName) != null) {\r
-//                graph.claimLiteral(resource, l0.HasName, value, Bindings.STRING);\r
-\r
-            if (type instanceof ArrayType) {\r
-                ArrayType arrayType = (ArrayType)type; \r
-                type = arrayType.componentType();\r
-                value = "[" + value + "]";\r
-//            if (type instanceof BooleanType) {\r
-//             graph.claimValue(resource, PrimitiveValueParser.parseBooleanArray(value), Bindings.BOOLEAN_ARRAY);\r
-//            } else if (type instanceof IntegerType) {\r
-//             graph.claimValue(resource, PrimitiveValueParser.parseIntArray(value), Bindings.INT_ARRAY);\r
-//            } else if (type instanceof LongType) {\r
-//             graph.claimValue(resource, PrimitiveValueParser.parseLongArray(value), Bindings.LONG_ARRAY);\r
-//            } else if (type instanceof FloatType) {\r
-//             graph.claimValue(resource, PrimitiveValueParser.parseFloatArray(value), Bindings.FLOAT_ARRAY);\r
-//            } else if (type instanceof DoubleType) {\r
-////                   graph.claimValue(resource, PrimitiveValueParser.parseDoubleArray(value), Bindings.DOUBLE_ARRAY);\r
-////                Datatype dt = graph.getPossibleRelatedValue(resource, l0.HasDataType, Bindings.getBindingUnchecked(Datatype.class));\r
-////                Binding binding = Bindings.getBinding(dt);\r
-////                Object parsedValue = binding.parseValue("["+value+"]", new DataValueRepository());\r
-////                graph.claimValue(resource, parsedValue, binding);\r
-//            } else if (type instanceof ByteType) {\r
-//             graph.claimValue(resource, PrimitiveValueParser.parseByteArray(value), Bindings.BYTE_ARRAY);\r
-//            } else if (type instanceof StringType) {\r
-//                Datatype dt = graph.getPossibleRelatedValue(resource, l0.HasDataType, Bindings.getBindingUnchecked(Datatype.class));\r
-//                Binding binding = Bindings.getBinding(dt);\r
-//                Object parsedValue = binding.parseValue(value, new DataValueRepository());\r
-//                graph.claimValue(resource, parsedValue, binding);\r
-//            }\r
-            }\r
-\r
-            Datatype dt = graph.getPossibleRelatedValue(resource, l0.HasDataType, Bindings.getBindingUnchecked(Datatype.class));\r
-            Binding binding = Bindings.getBinding(dt);\r
-            Object parsedValue = binding.parseValue(value, new DataValueRepository());\r
-            graph.claimValue(resource, parsedValue, binding);\r
-        } catch(IllegalArgumentException e) {\r
-            // value could not be modified\r
-        } catch (DataTypeSyntaxError e) {\r
-            throw new ValidationException("Could not modify resource '" + resource.getResourceId() + "' with value " + value, e);\r
-        } catch (BindingException e) {\r
-            throw new ValidationException("Could not modify resource '" + resource.getResourceId() + "' with value " + value, e);\r
-        }\r
-    }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.db.layer0.adapter.impl;
+
+import org.simantics.databoard.Bindings;
+import org.simantics.databoard.binding.Binding;
+import org.simantics.databoard.binding.error.BindingException;
+import org.simantics.databoard.parser.repository.DataTypeSyntaxError;
+import org.simantics.databoard.parser.repository.DataValueRepository;
+import org.simantics.databoard.type.ArrayType;
+import org.simantics.databoard.type.BooleanType;
+import org.simantics.databoard.type.ByteType;
+import org.simantics.databoard.type.Datatype;
+import org.simantics.databoard.type.DoubleType;
+import org.simantics.databoard.type.FloatType;
+import org.simantics.databoard.type.IntegerType;
+import org.simantics.databoard.type.LongType;
+import org.simantics.databoard.type.StringType;
+import org.simantics.databoard.units.IUnitConverter;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.VirtualGraph;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.exception.ValidationException;
+import org.simantics.db.layer0.adapter.AbstractStringModifier;
+import org.simantics.db.layer0.util.PrimitiveValueParser;
+import org.simantics.db.layer0.util.PrimitiveValueParser.BooleanArrayValidator;
+import org.simantics.db.layer0.util.PrimitiveValueParser.BooleanValidator;
+import org.simantics.db.layer0.util.PrimitiveValueParser.ByteArrayValidator;
+import org.simantics.db.layer0.util.PrimitiveValueParser.DoubleArrayValidator;
+import org.simantics.db.layer0.util.PrimitiveValueParser.DoubleValidator;
+import org.simantics.db.layer0.util.PrimitiveValueParser.FloatArrayValidator;
+import org.simantics.db.layer0.util.PrimitiveValueParser.FloatValidator;
+import org.simantics.db.layer0.util.PrimitiveValueParser.IValidator;
+import org.simantics.db.layer0.util.PrimitiveValueParser.IntegerArrayValidator;
+import org.simantics.db.layer0.util.PrimitiveValueParser.IntegerValidator;
+import org.simantics.db.layer0.util.PrimitiveValueParser.LongArrayValidator;
+import org.simantics.db.layer0.util.PrimitiveValueParser.LongValidator;
+import org.simantics.db.service.VirtualGraphSupport;
+import org.simantics.layer0.Layer0;
+
+public final class StringModifierImpl extends AbstractStringModifier {
+
+    final Resource resource;
+    final IUnitConverter converter;
+    IValidator     validator;
+
+    public StringModifierImpl(ReadGraph g, Resource resource) throws DatabaseException {
+        this(g, resource, null);
+    }
+
+    public StringModifierImpl(ReadGraph g, Resource resource, IUnitConverter converter) throws DatabaseException {
+       super(resource);
+        this.resource = resource;
+        this.validator = null;
+        this.converter = converter;
+
+        // Resolve validator if any
+        Layer0 l0 = Layer0.getInstance(g);
+        validator = PrimitiveValueParser.NON_MODIFIABLE_VALIDATOR;
+
+        Datatype type = g.getPossibleRelatedValue(resource, l0.HasDataType,
+                Bindings.getBindingUnchecked(Datatype.class));
+        if (type == null)
+            return;
+
+        if (type instanceof DoubleType) {
+            validator = new DoubleValidator();
+        } else if (type instanceof StringType) {
+            validator = null;
+        } else if (type instanceof ByteType) {
+            validator = new ByteArrayValidator();
+        } else if (type instanceof BooleanType) {
+            validator = new BooleanValidator();
+        } else if (type instanceof IntegerType) {
+            validator = new IntegerValidator();
+        } else if (type instanceof LongType) {
+            validator = new LongValidator();
+        } else if (type instanceof FloatType) {
+            validator = new FloatValidator();
+        } else if(type instanceof ArrayType) {
+            ArrayType arrayType = (ArrayType)type; 
+            type = arrayType.componentType();
+            if (type instanceof DoubleType) {
+                validator = new DoubleArrayValidator();
+            } else if (type instanceof StringType) {
+                validator = null;
+            } else if (type instanceof ByteType) {
+                validator = new ByteArrayValidator();
+            } else if (type instanceof BooleanType) {
+                validator = new BooleanArrayValidator();
+            } else if (type instanceof IntegerType) {
+                validator = new IntegerArrayValidator();
+            } else if (type instanceof LongType) {
+                validator = new LongArrayValidator();
+            } else if (type instanceof FloatType) {
+                validator = new FloatArrayValidator();
+            }
+        }
+    }
+
+    @Override
+    public String isValid(String value) {
+        return validator == null ? null : validator.isValid(value);
+    }
+
+    @Override
+    public final void modify(WriteGraph graph, final String value) throws DatabaseException {
+        VirtualGraphSupport vgs = graph.getService(VirtualGraphSupport.class);
+        VirtualGraph vg = vgs.getGraph(graph, resource);
+        if (vg != null) {
+            graph.syncRequest(new WriteRequest(vg) {
+                @Override
+                public void perform(WriteGraph graph) throws DatabaseException {
+                    modify0(graph, value);
+                }
+            });
+        } else {
+            modify0(graph, value);
+        }
+    }
+
+    protected void modify0(WriteGraph graph, String value) throws DatabaseException {
+        Layer0 l0 = Layer0.getInstance(graph);
+
+        try {
+            // FIXME: no support for StringArray!
+            Datatype type = graph.getDataType(resource);
+            if (type == null)
+                return;
+
+            if (type instanceof StringType) {
+                // We do not want to parse string types with databoard since it requires the string to be within quotes "".
+                graph.claimValue(resource, value, Bindings.STRING);
+                return;
+            }
+
+//            } else if (type instanceof DoubleType) {
+//             double v = PrimitiveValueParser.parseDouble(value);
+//             if(converter != null) v = converter.convert(v);
+//                graph.claimValue(resource, v, Bindings.DOUBLE);
+//            } else if (type instanceof ByteType) {
+//                graph.claimValue(resource, PrimitiveValueParser.parseByte(value), Bindings.BYTE);
+//            } else if (type instanceof BooleanType) {
+//                graph.claimValue(resource, PrimitiveValueParser.parseBoolean(value), Bindings.BOOLEAN);
+//            } else if (type instanceof IntegerType) {
+//                graph.claimValue(resource, PrimitiveValueParser.parseInt(value), Bindings.INTEGER);
+//            } else if (type instanceof LongType) {
+//                graph.claimValue(resource, PrimitiveValueParser.parseLong(value), Bindings.LONG);
+//            } else if (type instanceof FloatType) {
+//                graph.claimValue(resource, PrimitiveValueParser.parseFloat(value), Bindings.FLOAT);
+//            } else if (graph.getPossibleRelatedValue(resource, l0.HasName) != null) {
+//                graph.claimLiteral(resource, l0.HasName, value, Bindings.STRING);
+
+            if (type instanceof ArrayType) {
+                ArrayType arrayType = (ArrayType)type; 
+                type = arrayType.componentType();
+                value = "[" + value + "]";
+//            if (type instanceof BooleanType) {
+//             graph.claimValue(resource, PrimitiveValueParser.parseBooleanArray(value), Bindings.BOOLEAN_ARRAY);
+//            } else if (type instanceof IntegerType) {
+//             graph.claimValue(resource, PrimitiveValueParser.parseIntArray(value), Bindings.INT_ARRAY);
+//            } else if (type instanceof LongType) {
+//             graph.claimValue(resource, PrimitiveValueParser.parseLongArray(value), Bindings.LONG_ARRAY);
+//            } else if (type instanceof FloatType) {
+//             graph.claimValue(resource, PrimitiveValueParser.parseFloatArray(value), Bindings.FLOAT_ARRAY);
+//            } else if (type instanceof DoubleType) {
+////                   graph.claimValue(resource, PrimitiveValueParser.parseDoubleArray(value), Bindings.DOUBLE_ARRAY);
+////                Datatype dt = graph.getPossibleRelatedValue(resource, l0.HasDataType, Bindings.getBindingUnchecked(Datatype.class));
+////                Binding binding = Bindings.getBinding(dt);
+////                Object parsedValue = binding.parseValue("["+value+"]", new DataValueRepository());
+////                graph.claimValue(resource, parsedValue, binding);
+//            } else if (type instanceof ByteType) {
+//             graph.claimValue(resource, PrimitiveValueParser.parseByteArray(value), Bindings.BYTE_ARRAY);
+//            } else if (type instanceof StringType) {
+//                Datatype dt = graph.getPossibleRelatedValue(resource, l0.HasDataType, Bindings.getBindingUnchecked(Datatype.class));
+//                Binding binding = Bindings.getBinding(dt);
+//                Object parsedValue = binding.parseValue(value, new DataValueRepository());
+//                graph.claimValue(resource, parsedValue, binding);
+//            }
+            }
+
+            Datatype dt = graph.getPossibleRelatedValue(resource, l0.HasDataType, Bindings.getBindingUnchecked(Datatype.class));
+            Binding binding = Bindings.getBinding(dt);
+            Object parsedValue = binding.parseValue(value, new DataValueRepository());
+            graph.claimValue(resource, parsedValue, binding);
+        } catch(IllegalArgumentException e) {
+            // value could not be modified
+        } catch (DataTypeSyntaxError e) {
+            throw new ValidationException("Could not modify resource '" + resource.getResourceId() + "' with value " + value, e);
+        } catch (BindingException e) {
+            throw new ValidationException("Could not modify resource '" + resource.getResourceId() + "' with value " + value, e);
+        }
+    }
+
+}