]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/StringModifierImpl.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / adapter / impl / StringModifierImpl.java
diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/StringModifierImpl.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/StringModifierImpl.java
new file mode 100644 (file)
index 0000000..ab2d7a9
--- /dev/null
@@ -0,0 +1,207 @@
+/*******************************************************************************\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