1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.layer0.adapter.impl;
14 import org.simantics.databoard.Bindings;
15 import org.simantics.databoard.binding.Binding;
16 import org.simantics.databoard.binding.error.BindingException;
17 import org.simantics.databoard.parser.repository.DataTypeSyntaxError;
18 import org.simantics.databoard.parser.repository.DataValueRepository;
19 import org.simantics.databoard.type.ArrayType;
20 import org.simantics.databoard.type.BooleanType;
21 import org.simantics.databoard.type.ByteType;
22 import org.simantics.databoard.type.Datatype;
23 import org.simantics.databoard.type.DoubleType;
24 import org.simantics.databoard.type.FloatType;
25 import org.simantics.databoard.type.IntegerType;
26 import org.simantics.databoard.type.LongType;
27 import org.simantics.databoard.type.StringType;
28 import org.simantics.databoard.units.IUnitConverter;
29 import org.simantics.db.ReadGraph;
30 import org.simantics.db.Resource;
31 import org.simantics.db.VirtualGraph;
32 import org.simantics.db.WriteGraph;
33 import org.simantics.db.common.request.WriteRequest;
34 import org.simantics.db.exception.DatabaseException;
35 import org.simantics.db.exception.ValidationException;
36 import org.simantics.db.layer0.adapter.AbstractStringModifier;
37 import org.simantics.db.layer0.util.PrimitiveValueParser;
38 import org.simantics.db.layer0.util.PrimitiveValueParser.BooleanArrayValidator;
39 import org.simantics.db.layer0.util.PrimitiveValueParser.BooleanValidator;
40 import org.simantics.db.layer0.util.PrimitiveValueParser.ByteArrayValidator;
41 import org.simantics.db.layer0.util.PrimitiveValueParser.DoubleArrayValidator;
42 import org.simantics.db.layer0.util.PrimitiveValueParser.DoubleValidator;
43 import org.simantics.db.layer0.util.PrimitiveValueParser.FloatArrayValidator;
44 import org.simantics.db.layer0.util.PrimitiveValueParser.FloatValidator;
45 import org.simantics.db.layer0.util.PrimitiveValueParser.IValidator;
46 import org.simantics.db.layer0.util.PrimitiveValueParser.IntegerArrayValidator;
47 import org.simantics.db.layer0.util.PrimitiveValueParser.IntegerValidator;
48 import org.simantics.db.layer0.util.PrimitiveValueParser.LongArrayValidator;
49 import org.simantics.db.layer0.util.PrimitiveValueParser.LongValidator;
50 import org.simantics.db.service.VirtualGraphSupport;
51 import org.simantics.layer0.Layer0;
53 public final class StringModifierImpl extends AbstractStringModifier {
55 final Resource resource;
56 final IUnitConverter converter;
59 public StringModifierImpl(ReadGraph g, Resource resource) throws DatabaseException {
60 this(g, resource, null);
63 public StringModifierImpl(ReadGraph g, Resource resource, IUnitConverter converter) throws DatabaseException {
65 this.resource = resource;
66 this.validator = null;
67 this.converter = converter;
69 // Resolve validator if any
70 Layer0 l0 = Layer0.getInstance(g);
71 validator = PrimitiveValueParser.NON_MODIFIABLE_VALIDATOR;
73 Datatype type = g.getPossibleRelatedValue(resource, l0.HasDataType,
74 Bindings.getBindingUnchecked(Datatype.class));
78 if (type instanceof DoubleType) {
79 validator = new DoubleValidator();
80 } else if (type instanceof StringType) {
82 } else if (type instanceof ByteType) {
83 validator = new ByteArrayValidator();
84 } else if (type instanceof BooleanType) {
85 validator = new BooleanValidator();
86 } else if (type instanceof IntegerType) {
87 validator = new IntegerValidator();
88 } else if (type instanceof LongType) {
89 validator = new LongValidator();
90 } else if (type instanceof FloatType) {
91 validator = new FloatValidator();
92 } else if(type instanceof ArrayType) {
93 ArrayType arrayType = (ArrayType)type;
94 type = arrayType.componentType();
95 if (type instanceof DoubleType) {
96 validator = new DoubleArrayValidator();
97 } else if (type instanceof StringType) {
99 } else if (type instanceof ByteType) {
100 validator = new ByteArrayValidator();
101 } else if (type instanceof BooleanType) {
102 validator = new BooleanArrayValidator();
103 } else if (type instanceof IntegerType) {
104 validator = new IntegerArrayValidator();
105 } else if (type instanceof LongType) {
106 validator = new LongArrayValidator();
107 } else if (type instanceof FloatType) {
108 validator = new FloatArrayValidator();
114 public String isValid(String value) {
115 return validator == null ? null : validator.isValid(value);
119 public final void modify(WriteGraph graph, final String value) throws DatabaseException {
120 VirtualGraphSupport vgs = graph.getService(VirtualGraphSupport.class);
121 VirtualGraph vg = vgs.getGraph(graph, resource);
123 graph.syncRequest(new WriteRequest(vg) {
125 public void perform(WriteGraph graph) throws DatabaseException {
126 modify0(graph, value);
130 modify0(graph, value);
134 protected void modify0(WriteGraph graph, String value) throws DatabaseException {
135 Layer0 l0 = Layer0.getInstance(graph);
138 // FIXME: no support for StringArray!
139 Datatype type = graph.getDataType(resource);
143 if (type instanceof StringType) {
144 // We do not want to parse string types with databoard since it requires the string to be within quotes "".
145 graph.claimValue(resource, value, Bindings.STRING);
149 // } else if (type instanceof DoubleType) {
150 // double v = PrimitiveValueParser.parseDouble(value);
151 // if(converter != null) v = converter.convert(v);
152 // graph.claimValue(resource, v, Bindings.DOUBLE);
153 // } else if (type instanceof ByteType) {
154 // graph.claimValue(resource, PrimitiveValueParser.parseByte(value), Bindings.BYTE);
155 // } else if (type instanceof BooleanType) {
156 // graph.claimValue(resource, PrimitiveValueParser.parseBoolean(value), Bindings.BOOLEAN);
157 // } else if (type instanceof IntegerType) {
158 // graph.claimValue(resource, PrimitiveValueParser.parseInt(value), Bindings.INTEGER);
159 // } else if (type instanceof LongType) {
160 // graph.claimValue(resource, PrimitiveValueParser.parseLong(value), Bindings.LONG);
161 // } else if (type instanceof FloatType) {
162 // graph.claimValue(resource, PrimitiveValueParser.parseFloat(value), Bindings.FLOAT);
163 // } else if (graph.getPossibleRelatedValue(resource, l0.HasName) != null) {
164 // graph.claimLiteral(resource, l0.HasName, value, Bindings.STRING);
166 if (type instanceof ArrayType) {
167 ArrayType arrayType = (ArrayType)type;
168 type = arrayType.componentType();
169 value = "[" + value + "]";
170 // if (type instanceof BooleanType) {
171 // graph.claimValue(resource, PrimitiveValueParser.parseBooleanArray(value), Bindings.BOOLEAN_ARRAY);
172 // } else if (type instanceof IntegerType) {
173 // graph.claimValue(resource, PrimitiveValueParser.parseIntArray(value), Bindings.INT_ARRAY);
174 // } else if (type instanceof LongType) {
175 // graph.claimValue(resource, PrimitiveValueParser.parseLongArray(value), Bindings.LONG_ARRAY);
176 // } else if (type instanceof FloatType) {
177 // graph.claimValue(resource, PrimitiveValueParser.parseFloatArray(value), Bindings.FLOAT_ARRAY);
178 // } else if (type instanceof DoubleType) {
179 //// graph.claimValue(resource, PrimitiveValueParser.parseDoubleArray(value), Bindings.DOUBLE_ARRAY);
180 //// Datatype dt = graph.getPossibleRelatedValue(resource, l0.HasDataType, Bindings.getBindingUnchecked(Datatype.class));
181 //// Binding binding = Bindings.getBinding(dt);
182 //// Object parsedValue = binding.parseValue("["+value+"]", new DataValueRepository());
183 //// graph.claimValue(resource, parsedValue, binding);
184 // } else if (type instanceof ByteType) {
185 // graph.claimValue(resource, PrimitiveValueParser.parseByteArray(value), Bindings.BYTE_ARRAY);
186 // } else if (type instanceof StringType) {
187 // Datatype dt = graph.getPossibleRelatedValue(resource, l0.HasDataType, Bindings.getBindingUnchecked(Datatype.class));
188 // Binding binding = Bindings.getBinding(dt);
189 // Object parsedValue = binding.parseValue(value, new DataValueRepository());
190 // graph.claimValue(resource, parsedValue, binding);
194 Datatype dt = graph.getPossibleRelatedValue(resource, l0.HasDataType, Bindings.getBindingUnchecked(Datatype.class));
195 Binding binding = Bindings.getBinding(dt);
196 Object parsedValue = binding.parseValue(value, new DataValueRepository());
197 graph.claimValue(resource, parsedValue, binding);
198 } catch(IllegalArgumentException e) {
199 // value could not be modified
200 } catch (DataTypeSyntaxError e) {
201 throw new ValidationException("Could not modify resource '" + resource.getResourceId() + "' with value " + value, e);
202 } catch (BindingException e) {
203 throw new ValidationException("Could not modify resource '" + resource.getResourceId() + "' with value " + value, e);