]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/validation/L0Validations.java
Merge "Revert "Support enumerated property types in UC interface""
[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                                 if(valueType == null) {
29                                         Resource range = graph.getPossibleObject(predicate, L0.HasRange);
30                                         if(range != null) {
31                                                 return null;
32                                         } else {
33                                                 StringBuilder sb = new StringBuilder()
34                                                                 .append("The value type ")
35                                                                 .append(valueType)
36                                                                 .append(" of predicate ")
37                                                                 .append(NameUtils.getSafeName(graph, predicate, true))
38                                                                 .append(" cannot be resolved.")
39                                                                 .append(NameUtils.getSafeName(graph, object, true))
40                                                                 .append(".");
41                                                 return sb.toString();
42                                         }
43                                 }
44                                 String valueTypeText2 = graph.getPossibleRelatedValue(object, L0.HasValueType, Bindings.STRING);
45                                 if(valueTypeText2 != null) {
46                                         Type valueType2 = CommonDBUtils.getSCLType(graph, subject, valueTypeText2);
47                                         if(valueType2 == null) {
48                                                 StringBuilder sb = new StringBuilder()
49                                                                 .append("The value type ")
50                                                                 .append(valueType2)
51                                                                 .append(" of object ")
52                                                                 .append(NameUtils.getSafeName(graph, object, true))
53                                                                 .append(" cannot be resolved.");
54                                                 return sb.toString();
55                                         }
56                                         if(!valueType.equals(valueType2)) {
57                                                 StringBuilder sb = new StringBuilder()
58                                                                 .append("The value type ")
59                                                                 .append(valueType)
60                                                                 .append(" of predicate ")
61                                                                 .append(NameUtils.getSafeName(graph, predicate, true))
62                                                                 .append(" does not match the value type ")
63                                                                 .append(valueType2)
64                                                                 .append(" of object ")
65                                                                 .append(NameUtils.getSafeName(graph, object, true))
66                                                                 .append(".");
67                                                 return sb.toString();
68                                         }
69                                 }
70                         }
71                 }
72
73                 return null;
74
75         }
76
77 }