-
Notifications
You must be signed in to change notification settings - Fork 86
/
NumericRangeValidator.java
83 lines (65 loc) · 2.54 KB
/
NumericRangeValidator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package edu.cornell.mannlib.vitro.webapp.dynapi.components.validators;
import java.util.List;
import edu.cornell.mannlib.vitro.webapp.dynapi.data.ArrayView;
import edu.cornell.mannlib.vitro.webapp.dynapi.data.Data;
import edu.cornell.mannlib.vitro.webapp.dynapi.data.JsonContainerView;
import edu.cornell.mannlib.vitro.webapp.dynapi.data.SimpleDataView;
import edu.cornell.mannlib.vitro.webapp.utils.configuration.Property;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class NumericRangeValidator extends IsNotBlank {
private static final Log log = LogFactory.getLog(NumericRangeValidator.class);
private Float minValue;
private Float maxValue;
public Float getMinValue() {
return minValue;
}
@Property(uri = "https://vivoweb.org/ontology/vitro-dynamic-api#minValue", maxOccurs = 1)
public void setMinValue(float minValue) {
this.minValue = minValue;
}
public Float getMaxValue() {
return maxValue;
}
@Property(uri = "https://vivoweb.org/ontology/vitro-dynamic-api#maxValue", maxOccurs = 1)
public void setMaxValue(float maxValue) {
this.maxValue = maxValue;
}
@Override
public boolean isValid(String name, Data data) {
if (!super.isValid(name, data)) {
return false;
}
if (JsonContainerView.isJsonArray(data.getParam())) {
List array = ArrayView.getArray(data);
for (Object value : array) {
if (!isInRange(value.toString())) {
log.debug("Length of " + name + " is not in range [" + ((minValue != null) ? minValue : " ") + "-"
+ ((maxValue != null) ? maxValue : " ") + "].");
return false;
}
}
} else {
if (!isInRange(SimpleDataView.getStringRepresentation(data))) {
log.debug("Length of " + name + " is not in range [" + ((minValue != null) ? minValue : " ") + "-"
+ ((maxValue != null) ? maxValue : " ") + "].");
return false;
}
}
return true;
}
private boolean isInRange(String string) {
if (NumberUtils.isParsable(string)) {
double value = NumberUtils.toDouble(string);
if ((minValue != null) && (value < minValue)) {
return false;
}
if ((maxValue != null) && (value > maxValue)) {
return false;
}
return true;
}
return false;
}
}