]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/validation/L0Validations.java
99ef91b7aa0040815b23a4dae559fa1ad3d7d6dd
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / validation / L0Validations.java
1 package org.simantics.db.common.validation;
2
3 import org.simantics.databoard.Bindings;
4 import org.simantics.db.ReadGraph;
5 import org.simantics.db.Resource;
6 import org.simantics.db.common.utils.CommonDBUtils;
7 import org.simantics.db.common.utils.NameUtils;
8 import org.simantics.db.exception.DatabaseException;
9 import org.simantics.layer0.Layer0;
10 import org.simantics.scl.compiler.types.Type;
11
12 public class L0Validations {
13
14         public static String checkValueType(ReadGraph graph, Resource subject, Resource predicate) throws DatabaseException {
15                 
16                 if (subject == null)
17                         return null;
18                 if (predicate == null)
19                         return null;
20
21                 Layer0 L0 = Layer0.getInstance(graph);
22                 if(graph.isSubrelationOf(predicate, L0.HasProperty)) {
23                         Resource object = graph.getPossibleObject(subject, predicate);
24                         if(object == null) return null;
25                         String valueTypeText = graph.getPossibleRelatedValue(predicate, L0.RequiresValueType, Bindings.STRING);
26                         if(valueTypeText != null) {
27                                 Type valueType = CommonDBUtils.getSCLType(graph, subject, valueTypeText);
28                                 String valueTypeText2 = graph.getPossibleRelatedValue(object, L0.HasValueType, Bindings.STRING);
29                                 if(valueTypeText2 != null) {
30                                         Type valueType2 = CommonDBUtils.getSCLType(graph, subject, valueTypeText2);
31                                         if(!valueType.equals(valueType2)) {
32                                                 StringBuilder sb = new StringBuilder()
33                                                                 .append("The value type ")
34                                                                 .append(valueType)
35                                                                 .append(" of predicate ")
36                                                                 .append(NameUtils.getSafeName(graph, predicate, true))
37                                                                 .append(" does not match the value type ")
38                                                                 .append(valueType2)
39                                                                 .append(" of object ")
40                                                                 .append(NameUtils.getSafeName(graph, object, true))
41                                                                 .append(".");
42                                                 return sb.toString();
43                                         }
44                                 }
45                         }
46                 }
47                 
48                 return null;
49                 
50         }
51         
52 }