| ... |
... |
@@ -22,9 +22,13 @@ package fr.ird.observe.dto.referential.seine; |
|
22
|
22
|
* #L%
|
|
23
|
23
|
*/
|
|
24
|
24
|
|
|
25
|
|
-import fr.ird.observe.dto.referential.FormulaHelper;
|
|
26
|
25
|
import org.apache.commons.lang3.StringUtils;
|
|
27
|
26
|
|
|
|
27
|
+import javax.script.Bindings;
|
|
|
28
|
+import javax.script.ScriptContext;
|
|
|
29
|
+import javax.script.ScriptEngine;
|
|
|
30
|
+import javax.script.ScriptEngineManager;
|
|
|
31
|
+
|
|
28
|
32
|
public class ObjectMaterialDto extends GeneratedObjectMaterialDto {
|
|
29
|
33
|
|
|
30
|
34
|
private static final long serialVersionUID = 1L;
|
| ... |
... |
@@ -48,7 +52,15 @@ public class ObjectMaterialDto extends GeneratedObjectMaterialDto { |
|
48
|
52
|
@Override
|
|
49
|
53
|
public void setValidation(String validation) {
|
|
50
|
54
|
super.setValidation(validation);
|
|
51
|
|
- boolean result = FormulaHelper.validateObjectMaterialValidation(validation, 10);
|
|
|
55
|
+ Object value = "10";
|
|
|
56
|
+ if (isBoolean()) {
|
|
|
57
|
+ value = Boolean.TRUE;
|
|
|
58
|
+ } else if (isInteger()) {
|
|
|
59
|
+ value = 10;
|
|
|
60
|
+ }else if (isFloat()) {
|
|
|
61
|
+ value = 10f;
|
|
|
62
|
+ }
|
|
|
63
|
+ boolean result = validateObjectMaterialValidation(validation, value);
|
|
52
|
64
|
setValidationValid(result);
|
|
53
|
65
|
}
|
|
54
|
66
|
|
| ... |
... |
@@ -64,4 +76,34 @@ public class ObjectMaterialDto extends GeneratedObjectMaterialDto { |
|
64
|
76
|
return parent != null && parent.getParentId() != null;
|
|
65
|
77
|
}
|
|
66
|
78
|
|
|
|
79
|
+ /** moteur d'évaluation d'expression */
|
|
|
80
|
+ private static ScriptEngine scriptEngine;
|
|
|
81
|
+
|
|
|
82
|
+
|
|
|
83
|
+ private static ScriptEngine getScriptEngine() {
|
|
|
84
|
+ if (scriptEngine == null) {
|
|
|
85
|
+ ScriptEngineManager factory = new ScriptEngineManager();
|
|
|
86
|
+
|
|
|
87
|
+ scriptEngine = factory.getEngineByExtension("js");
|
|
|
88
|
+ }
|
|
|
89
|
+ return scriptEngine;
|
|
|
90
|
+ }
|
|
|
91
|
+
|
|
|
92
|
+ public static boolean validateObjectMaterialValidation(String relation, Object value) {
|
|
|
93
|
+ if (!StringUtils.isEmpty(relation)) {
|
|
|
94
|
+
|
|
|
95
|
+ ScriptEngine engine = getScriptEngine();
|
|
|
96
|
+ Bindings bindings = engine.createBindings();
|
|
|
97
|
+ bindings.put("x", value);
|
|
|
98
|
+
|
|
|
99
|
+ try {
|
|
|
100
|
+ engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
|
|
|
101
|
+ Boolean o = (Boolean) engine.eval(relation);
|
|
|
102
|
+ return true;
|
|
|
103
|
+ } catch (Exception e) {
|
|
|
104
|
+ return false;
|
|
|
105
|
+ }
|
|
|
106
|
+ }
|
|
|
107
|
+ return false;
|
|
|
108
|
+ }
|
|
67
|
109
|
}
|