]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/variable/VariableWrite2.java
possibleObjectForType utility
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / variable / VariableWrite2.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.layer0.variable;
13
14 import org.simantics.databoard.Bindings;
15 import org.simantics.databoard.binding.Binding;
16 import org.simantics.databoard.binding.BooleanBinding;
17 import org.simantics.databoard.binding.NumberBinding;
18 import org.simantics.databoard.binding.StringBinding;
19 import org.simantics.databoard.binding.error.BindingException;
20 import org.simantics.databoard.binding.mutable.MutableStringBinding;
21 import org.simantics.databoard.parser.repository.DataTypeSyntaxError;
22 import org.simantics.databoard.parser.repository.DataValueRepository;
23 import org.simantics.databoard.primitives.MutableString;
24 import org.simantics.databoard.type.Datatype;
25 import org.simantics.databoard.type.DoubleType;
26 import org.simantics.databoard.type.FloatType;
27 import org.simantics.databoard.type.IntegerType;
28 import org.simantics.databoard.type.NumberType;
29 import org.simantics.databoard.units.IUnitConverter;
30 import org.simantics.databoard.units.internal.UnitParseException;
31 import org.simantics.databoard.util.ObjectUtils;
32 import org.simantics.db.VirtualGraph;
33 import org.simantics.db.WriteGraph;
34 import org.simantics.db.common.request.WriteRequest;
35 import org.simantics.db.exception.DatabaseException;
36 import org.simantics.db.layer0.util.Layer0Utils;
37 import org.simantics.db.layer0.util.PrimitiveValueParser;
38
39 /**
40  * @deprecated exactly the same as {@link VariableWrite}, should be removed. Not
41  *             used in the platform at least.
42  */
43 public class VariableWrite2 extends WriteRequest {
44
45     final private Variable variable;
46     final private String label;
47     final private String targetUnit;
48
49     public VariableWrite2(Variable variable, String label) {
50         this(variable, label, null, null);
51     }
52
53     public VariableWrite2(Variable variable, String label, VirtualGraph provider, String targetUnit) {
54         super(provider);
55         this.variable = variable;
56         this.label = label;
57         this.targetUnit = targetUnit;
58     }
59
60     public static IUnitConverter converter(final String unit, final String targetUnit) {
61
62         return new IUnitConverter() {
63
64             @Override
65             public double convert(double value) {
66
67                 if(targetUnit == null) return value;
68
69                 if("K".equals(unit)) {
70                     if("C".equals(targetUnit)) value = value - 273.15;
71                     else if("F".equals(targetUnit)) value = (value - 273.15) * 9.0/5.0 + 32.0;
72                 } else if ("C".equals(unit)) {
73                     if("K".equals(targetUnit)) value = value + 273.15;
74                 } else if ("F".equals(unit)) {
75                     if("K".equals(targetUnit)) value = (value - 32.0) * 5.0/9.0 + 273.15;
76                 } else {
77                     try {
78                         value = org.simantics.databoard.Units.convert(value, unit, targetUnit);
79                     } catch (UnitParseException e) {
80                         e.printStackTrace();
81                     }
82                 }
83
84                 return value;
85
86             }
87
88         };
89
90
91     }
92
93     @Override
94     public void perform(WriteGraph graph) throws DatabaseException {
95         graph.markUndoPoint();
96         try {
97             //System.err.println("VariableWrite " + variable.getURI(graph) + " => " + label);
98             Datatype type = variable.getPossibleDatatype(graph);
99             if (type == null) {
100                 String uri = null;
101                 try {
102                     uri = variable.getURI(graph);
103                 } catch (DatabaseException e) {
104                 }
105                 throw new org.simantics.db.exception.BindingException("no datatype for variable " + variable + " (URI=" + uri + ")", null);
106             }
107
108             Binding binding = Bindings.getBinding(type);
109
110             String parsedLabel = label;
111             Object value = null;
112
113             if (binding instanceof StringBinding) {
114                 if (binding instanceof MutableStringBinding)
115                     value = new MutableString(label);
116                 else
117                     value = label;
118             } else if (binding instanceof BooleanBinding) {
119                 // This parses a bit more than just "true"/"false".
120                 try {
121                     value = PrimitiveValueParser.parseBoolean(label);
122                 } catch (IllegalArgumentException e) {
123                     throw new DatabaseException(e);
124                 }
125             } else {
126                 if (binding instanceof NumberBinding) {
127                     parsedLabel = label.replace(",", ".");
128                 }
129
130                 value = binding.parseValue(parsedLabel, new DataValueRepository());
131                 
132                 // FIXME: this is Balas-specific
133                 if (targetUnit != null) {
134                     if (type instanceof NumberType) {
135                         String unit = Variables.getPossibleUnit(graph, variable);
136                         if (unit != null) {
137                             IUnitConverter converter = converter(targetUnit, unit);
138                             if(type instanceof FloatType) {
139                                 float converted = (float)converter.convert(((Number)value).floatValue());
140                                 value = binding.parseValue(Float.toString(converted), new DataValueRepository());
141                             } else if(type instanceof DoubleType) {
142                                 double converted = converter.convert(((Number)value).doubleValue());
143                                 value = binding.parseValue(Double.toString(converted), new DataValueRepository());
144                             } else if(type instanceof IntegerType) {
145                                 value = binding.parseValue(Integer.toString(((Number)value).intValue()), new DataValueRepository());
146                             }
147                         }
148                     }
149                 }
150             }
151
152             //System.out.println("VariableWrite " + ObjectUtils.toString(value));
153             variable.setValue(graph, value, binding);
154
155             String[] varParts = variable.getParent(graph).getURI(graph).split("/");
156             String varName = varParts[varParts.length - 1];
157             
158             // Add a comment to metadata.
159             Layer0Utils.addCommentMetadata(graph, "Modified variable " + varName + " to " + ObjectUtils.toString(value));
160         } catch (DataTypeSyntaxError e) {
161             throw new DatabaseException(e);
162         } catch (BindingException e) {
163             throw new DatabaseException(e);
164         }
165
166     }
167
168 }