]> gerrit.simantics Code Review - simantics/platform.git/blob
33fed9cc172a1386d3248b8748b2c5e4f680023a
[simantics/platform.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.db.layer0.adapter.impl;\r
13 \r
14 import java.lang.reflect.Array;\r
15 import java.util.List;\r
16 \r
17 import org.simantics.databoard.Bindings;\r
18 import org.simantics.databoard.binding.Binding;\r
19 import org.simantics.databoard.binding.error.BindingException;\r
20 import org.simantics.databoard.parser.repository.DataTypeSyntaxError;\r
21 import org.simantics.databoard.parser.repository.DataValueRepository;\r
22 import org.simantics.databoard.type.ArrayType;\r
23 import org.simantics.databoard.type.Datatype;\r
24 import org.simantics.db.ReadGraph;\r
25 import org.simantics.db.Resource;\r
26 import org.simantics.db.VirtualGraph;\r
27 import org.simantics.db.WriteGraph;\r
28 import org.simantics.db.common.request.WriteRequest;\r
29 import org.simantics.db.common.utils.NameUtils;\r
30 import org.simantics.db.common.utils.OrderedSetUtils;\r
31 import org.simantics.db.exception.DatabaseException;\r
32 import org.simantics.db.exception.ValidationException;\r
33 import org.simantics.db.layer0.adapter.StringIndexModifier;\r
34 import org.simantics.db.layer0.adapter.TObjectIntPair;\r
35 import org.simantics.db.layer0.util.PrimitiveValueParser;\r
36 import org.simantics.db.layer0.util.PrimitiveValueParser.BooleanValidator;\r
37 import org.simantics.db.layer0.util.PrimitiveValueParser.ByteValidator;\r
38 import org.simantics.db.layer0.util.PrimitiveValueParser.DoubleValidator;\r
39 import org.simantics.db.layer0.util.PrimitiveValueParser.FloatValidator;\r
40 import org.simantics.db.layer0.util.PrimitiveValueParser.IValidator;\r
41 import org.simantics.db.layer0.util.PrimitiveValueParser.IntegerValidator;\r
42 import org.simantics.db.layer0.util.PrimitiveValueParser.LongValidator;\r
43 import org.simantics.db.service.VirtualGraphSupport;\r
44 import org.simantics.layer0.Layer0;\r
45 \r
46 /**\r
47  * @author Tuukka Lehtonen\r
48  */\r
49 public final class StringIndexModifierImpl implements StringIndexModifier {\r
50 \r
51     final Resource resource;\r
52     IValidator     validator;\r
53 \r
54     public StringIndexModifierImpl(ReadGraph g, Resource resource) throws DatabaseException {\r
55         \r
56         this.resource = resource;\r
57         this.validator = null;\r
58 \r
59         // Resolve validator if any\r
60         Layer0 l0 = Layer0.getInstance(g);\r
61         if (g.isInstanceOf(resource, l0.DoubleArray)) {\r
62             validator = new DoubleValidator();\r
63         } else if (g.isInstanceOf(resource, l0.ByteArray)) {\r
64             validator = new ByteValidator();\r
65         } else if (g.isInstanceOf(resource, l0.BooleanArray)) {\r
66             validator = new BooleanValidator();\r
67         } else if (g.isInstanceOf(resource, l0.IntegerArray)) {\r
68             validator = new IntegerValidator();\r
69         } else if (g.isInstanceOf(resource, l0.LongArray)) {\r
70             validator = new LongValidator();\r
71         } else if (g.isInstanceOf(resource, l0.FloatArray)) {\r
72             validator = new FloatValidator();\r
73         }\r
74         \r
75     }\r
76 \r
77     @Override\r
78     public String getValue() {\r
79         throw new UnsupportedOperationException();\r
80     }\r
81     \r
82     @Override\r
83     public String isValid(TObjectIntPair<String> value) {\r
84         return validator == null ? null : validator.isValid(value.first);\r
85     }\r
86 \r
87     @Override\r
88     public void modify(WriteGraph graph, final TObjectIntPair<String> pair) throws DatabaseException {\r
89         VirtualGraphSupport vgs = graph.getService(VirtualGraphSupport.class);\r
90         VirtualGraph vg = vgs.getGraph(graph, resource);\r
91         if (vg != null) {\r
92             graph.syncRequest(new WriteRequest(vg) {\r
93                 @Override\r
94                 public void perform(WriteGraph graph) throws DatabaseException {\r
95                     modify0(graph, pair);\r
96                 }\r
97             });\r
98         } else {\r
99             modify0(graph, pair);\r
100         }\r
101     }\r
102 \r
103     public void modify0(WriteGraph graph, TObjectIntPair<String> pair) throws DatabaseException {\r
104         Layer0 l0 = Layer0.getInstance(graph);\r
105         String value = pair.first;\r
106         int index = pair.second;\r
107 \r
108         try {\r
109             if (graph.isInstanceOf(resource, l0.StringArray)) {\r
110                 String[] old = graph.getValue(resource);\r
111                 old[index] = value;\r
112                 graph.claimValue(resource, old);\r
113             } else if (graph.isInstanceOf(resource, l0.DoubleArray)) {\r
114                 double[] old = graph.getValue(resource);\r
115                 old[index] = PrimitiveValueParser.parseDouble(value);\r
116                 graph.claimValue(resource, old);\r
117             } else if (graph.isInstanceOf(resource, l0.ByteArray)) {\r
118                 byte[] old = graph.getValue(resource);\r
119                 old[index] = PrimitiveValueParser.parseByte(value);\r
120                 graph.claimValue(resource, old);\r
121             } else if (graph.isInstanceOf(resource, l0.BooleanArray)) {\r
122                 boolean[] old = graph.getValue(resource);\r
123                 old[index] = PrimitiveValueParser.parseBoolean(value);\r
124                 graph.claimValue(resource, old);\r
125             } else if (graph.isInstanceOf(resource, l0.IntegerArray)) {\r
126                 int[] old = graph.getValue(resource);\r
127                 old[index] = PrimitiveValueParser.parseInt(value);\r
128                 graph.claimValue(resource, old);\r
129             } else if (graph.isInstanceOf(resource, l0.LongArray)) {\r
130                 long[] old = graph.getValue(resource);\r
131                 old[index] = PrimitiveValueParser.parseLong(value);\r
132                 graph.claimValue(resource, old);\r
133             } else if (graph.isInstanceOf(resource, l0.FloatArray)) {\r
134                 float[] old = graph.getValue(resource);\r
135                 old[index] = PrimitiveValueParser.parseFloat(value);\r
136                 graph.claimValue(resource, old);\r
137             } else if(graph.isInstanceOf(resource, l0.OrderedSet)) {\r
138                 List<Resource> list = OrderedSetUtils.toList(graph, resource);\r
139                                 Resource el = graph.newResource();\r
140                                 graph.claimValue(el, value);\r
141                                 graph.claim(el, l0.InstanceOf, null, l0.String); // FIXME FIXME FIXME\r
142                 OrderedSetUtils.replace(graph, resource, list.get(index), el);\r
143             } else {\r
144                 Datatype dt = graph.getPossibleRelatedValue(resource, l0.HasDataType, Bindings.getBindingUnchecked(Datatype.class));\r
145                 if (dt instanceof ArrayType) {\r
146                     ArrayType at = (ArrayType) dt;\r
147                     Binding binding = Bindings.getBinding(at);\r
148                     Binding indexBinding = Bindings.getBinding(at.componentType());\r
149                     Object parsedValue = indexBinding.parseValue(value, new DataValueRepository());\r
150                     Object v = graph.getValue(resource, binding);\r
151                     if (v.getClass().isArray()) {\r
152                         Array.set(v, index, parsedValue);\r
153                         graph.claimValue(resource, v, binding);\r
154                     }\r
155                 } else {\r
156                     throw new ValidationException("Could not modify unrecognized resource '" + NameUtils.getSafeName(graph, resource) + "' with value " + value);\r
157                 }\r
158             }\r
159         } catch(IllegalArgumentException e) {\r
160             // value could not be modified\r
161         } catch (DataTypeSyntaxError e) {\r
162             throw new ValidationException("Could not modify unrecognized resource '" + NameUtils.getSafeName(graph, resource) + "' with value " + value, e);\r
163         } catch (BindingException e) {\r
164             throw new ValidationException("Could not modify unrecognized resource '" + NameUtils.getSafeName(graph, resource) + "' with value " + value, e);\r
165         }\r
166     }\r
167 \r
168 }\r