1
|
package vldc.aswi.validators;
|
2
|
|
3
|
import org.springframework.beans.factory.annotation.Autowired;
|
4
|
import org.springframework.stereotype.Component;
|
5
|
import org.springframework.util.NumberUtils;
|
6
|
import org.springframework.validation.Errors;
|
7
|
import org.springframework.validation.Validator;
|
8
|
import vldc.aswi.domain.Assembly;
|
9
|
import vldc.aswi.domain.Configuration;
|
10
|
import vldc.aswi.domain.parameter.Parameter;
|
11
|
import vldc.aswi.domain.parameter.ParameterInConfiguration;
|
12
|
import vldc.aswi.service.SqlQueryManager;
|
13
|
|
14
|
import java.text.ParseException;
|
15
|
import java.text.SimpleDateFormat;
|
16
|
import java.util.Date;
|
17
|
import java.util.List;
|
18
|
|
19
|
@Component
|
20
|
public class ConfigurationValidator implements Validator {
|
21
|
|
22
|
/**
|
23
|
* Validator for supporting this class.
|
24
|
* @param aClass - Class.
|
25
|
* @return true if sent class match Configuration class.
|
26
|
*/
|
27
|
@Override
|
28
|
public boolean supports(Class<?> aClass) {
|
29
|
return Configuration.class.isAssignableFrom(aClass);
|
30
|
}
|
31
|
|
32
|
/**
|
33
|
* Validates the given configuration object.
|
34
|
* @param obj Configuration object to be validated.
|
35
|
* @param errors Errors variable where errors are stored.
|
36
|
*/
|
37
|
@Override
|
38
|
public void validate(Object obj, Errors errors) {
|
39
|
Configuration configuration = (Configuration) obj;
|
40
|
|
41
|
List<ParameterInConfiguration> parametersInConfiguration = configuration.getParametersInConfiguration();
|
42
|
for (int i = 0; i < parametersInConfiguration.size(); i++) {
|
43
|
ParameterInConfiguration parameterInConfiguration = parametersInConfiguration.get(i);
|
44
|
if (parameterInConfiguration.getOperator() == null || parameterInConfiguration.getOperatorValue() == null ||
|
45
|
parameterInConfiguration.getOperatorValue().equals("")) {
|
46
|
continue;
|
47
|
}
|
48
|
if (parameterInConfiguration.getOperator().getName().equals("BETWEEN") ||
|
49
|
parameterInConfiguration.getOperator().getName().equals("NOT BETWEEN")) {
|
50
|
validateBETWEEN(parameterInConfiguration, errors, i, parameterInConfiguration.getOperator().getName());
|
51
|
}
|
52
|
else if (parameterInConfiguration.getOperator().getName().equals("IN") ||
|
53
|
parameterInConfiguration.getOperator().getName().equals("NOT IN")) {
|
54
|
validateIN(parameterInConfiguration, errors, i, parameterInConfiguration.getOperator().getName());
|
55
|
}
|
56
|
else if (!parameterInConfiguration.getOperator().getName().equals("LIKE") ||
|
57
|
parameterInConfiguration.getOperator().getName().equals("NOT LIKE")) {
|
58
|
validateOthersExceptLIKE(parameterInConfiguration, errors, i, parameterInConfiguration.getOperator().getName());
|
59
|
}
|
60
|
}
|
61
|
|
62
|
}
|
63
|
|
64
|
/**
|
65
|
* Validates BETWEEN and NOT BETWEEN operator.
|
66
|
* @param parameterInConfiguration Parameter in configuration to be validated.
|
67
|
* @param errors Errors variable where errors are stored.
|
68
|
* @param i Index of the parameter.
|
69
|
* @param operator Operator of the parameter in configuration.
|
70
|
*/
|
71
|
private void validateBETWEEN(ParameterInConfiguration parameterInConfiguration, Errors errors, int i, String operator) {
|
72
|
if (!parameterInConfiguration.getOperatorValue().matches("\\w+;\\w+")) {
|
73
|
errors.rejectValue("parametersInConfiguration[" + i + "].operatorValue", "Nesprávně oddělené hodnoty" +
|
74
|
" filtru " + operator +"! Použijte ';' pro oddělení hodnot!");
|
75
|
}
|
76
|
else {
|
77
|
validateValuesInList(parameterInConfiguration, errors, i, operator);
|
78
|
}
|
79
|
}
|
80
|
|
81
|
/**
|
82
|
* Validates values in list.
|
83
|
* @param parameterInConfiguration Parameter in configuration to be validated.
|
84
|
* @param errors Errors variable where errors are stored.
|
85
|
* @param i Index of the parameter.
|
86
|
* @param operator Operator of the parameter in configuration.
|
87
|
*/
|
88
|
private void validateValuesInList(ParameterInConfiguration parameterInConfiguration, Errors errors, int i, String operator) {
|
89
|
String[] values = parameterInConfiguration.getOperatorValue().split(";");
|
90
|
for (String value : values) {
|
91
|
if (parameterInConfiguration.getParameter().getParameterType().getName().equals("Datum")) {
|
92
|
validateDate(value, errors, i, operator);
|
93
|
}
|
94
|
if (parameterInConfiguration.getParameter().getParameterType().getName().equals("Číslo")) {
|
95
|
validateNumber(value, errors, i, operator);
|
96
|
}
|
97
|
}
|
98
|
}
|
99
|
|
100
|
/**
|
101
|
* Validates IN and NOT IN operator.
|
102
|
* @param parameterInConfiguration Parameter in configuration to be validated.
|
103
|
* @param errors Errors variable where errors are stored.
|
104
|
* @param i Index of the parameter.
|
105
|
* @param operator Operator of the parameter in configuration.
|
106
|
*/
|
107
|
private void validateIN(ParameterInConfiguration parameterInConfiguration, Errors errors, int i, String operator) {
|
108
|
if (!parameterInConfiguration.getOperatorValue().matches("\\w+(;\\w+)*$")) {
|
109
|
errors.rejectValue("parametersInConfiguration[" + i + "].operatorValue", "Špatně zadaná hodnota" +
|
110
|
" filtru " + operator + "! Použijte ';' pro oddělení hodnot!");
|
111
|
}
|
112
|
else {
|
113
|
validateValuesInList(parameterInConfiguration, errors, i, operator);
|
114
|
}
|
115
|
}
|
116
|
|
117
|
/**
|
118
|
* Validates other operators.
|
119
|
* @param parameterInConfiguration Parameter in configuration to be validated.
|
120
|
* @param errors Errors variable where errors are stored.
|
121
|
* @param i Index of the parameter.
|
122
|
* @param operator Operator of the parameter in configuration.
|
123
|
*/
|
124
|
private void validateOthersExceptLIKE(ParameterInConfiguration parameterInConfiguration, Errors errors, int i, String operator) {
|
125
|
if (parameterInConfiguration.getParameter().getParameterType().getName().equals("Datum")) {
|
126
|
validateDate(parameterInConfiguration.getOperatorValue(), errors, i, operator);
|
127
|
}
|
128
|
if (parameterInConfiguration.getParameter().getParameterType().getName().equals("Číslo")) {
|
129
|
validateNumber(parameterInConfiguration.getOperatorValue(), errors, i, operator);
|
130
|
}
|
131
|
}
|
132
|
|
133
|
/**
|
134
|
* Validates numeric value.
|
135
|
* @param value Value to be validated.
|
136
|
* @param errors Errors variable where errors are stored.
|
137
|
* @param i Index of hte parameter.
|
138
|
* @param operator operator Operator of the parameter in configuration.
|
139
|
*/
|
140
|
private void validateNumber(String value, Errors errors, int i, String operator) {
|
141
|
if (!value.matches("-?\\d+(\\.\\d+)?")) {
|
142
|
errors.rejectValue("parametersInConfiguration[" + i + "].operatorValue", "Špatně zadaná hodnota" +
|
143
|
" filtru " + operator + "! Očekává se číslo!");
|
144
|
}
|
145
|
}
|
146
|
|
147
|
/**
|
148
|
* Validates date value.
|
149
|
* @param value Value to be validated.
|
150
|
* @param errors Errors variable where errors are stored.
|
151
|
* @param i Index of hte parameter.
|
152
|
* @param operator operator Operator of the parameter in configuration.
|
153
|
*/
|
154
|
private void validateDate(String value, Errors errors, int i, String operator) {
|
155
|
try {
|
156
|
new SimpleDateFormat("yyyy-MM-dd").parse(value);
|
157
|
} catch (ParseException nfe) {
|
158
|
errors.rejectValue("parametersInConfiguration[" + i + "].operatorValue", "Špatně zadaná hodnota" +
|
159
|
" filtru " + operator + "! Očekává se datum!");
|
160
|
}
|
161
|
}
|
162
|
}
|