|
1
|
|
|
package easytests.personal.validators; |
|
2
|
|
|
|
|
3
|
|
|
import easytests.common.exceptions.NotFoundException; |
|
4
|
|
|
import easytests.common.validators.AbstractDtoValidator; |
|
5
|
|
|
import easytests.core.services.QuestionsService; |
|
6
|
|
|
import easytests.personal.dto.AnswerDto; |
|
7
|
|
|
import java.util.HashSet; |
|
8
|
|
|
import java.util.List; |
|
9
|
|
|
import java.util.Set; |
|
10
|
|
|
|
|
11
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
12
|
|
|
import org.springframework.stereotype.Component; |
|
13
|
|
|
import org.springframework.validation.Errors; |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @author rezenbekk |
|
18
|
|
|
*/ |
|
19
|
|
|
@SuppressWarnings({ |
|
20
|
|
|
"checkstyle:LineLength", |
|
21
|
|
|
"checkstyle:CyclomaticComplexity" }) |
|
22
|
|
|
@Component |
|
23
|
|
|
public class AnswerDtoValidator extends AbstractDtoValidator { |
|
24
|
|
|
|
|
25
|
|
|
@Autowired |
|
26
|
|
|
private QuestionsService questionsService; |
|
27
|
|
|
|
|
28
|
|
|
public boolean supports(Class className) { |
|
29
|
|
|
return AnswerDto.class.isAssignableFrom(className); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public void validate(Object object, Errors errors) { |
|
33
|
|
|
final AnswerDto answerDto = (AnswerDto) object; |
|
34
|
|
|
final String emptyTextError = "emptyText"; |
|
35
|
|
|
final String textTooLongError = "textTooLong"; |
|
36
|
|
|
final String invalidQuestionError = "invalidQuestion"; |
|
37
|
|
|
|
|
38
|
|
|
if (answerDto.getTxt() == null || answerDto.getTxt().isEmpty()) { |
|
39
|
|
|
errors.reject(emptyTextError, "Answer text can not be empty"); |
|
40
|
|
|
} |
|
41
|
|
|
if (answerDto.getTxt().length() > 255) { |
|
42
|
|
|
errors.reject(textTooLongError, "Answer text too long"); |
|
43
|
|
|
} |
|
44
|
|
|
if (answerDto.getQuestionId() == null || questionsService.find(answerDto.getQuestionId()) == null) { |
|
45
|
|
|
errors.reject(invalidQuestionError, "Invalid question settings"); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public void validateWithQuestionType(Object object, Integer questionType, Errors errors) { |
|
50
|
|
|
final List<AnswerDto> answers = (List<AnswerDto>) object; |
|
51
|
|
|
if (answers == null) { |
|
52
|
|
|
return; |
|
53
|
|
|
} |
|
54
|
|
|
Integer rightCount = 0; |
|
55
|
|
|
Boolean serialNumbersConflict = false; |
|
56
|
|
|
final Set<Integer> serialNumbers = new HashSet<Integer>(); |
|
57
|
|
|
for (AnswerDto answer |
|
58
|
|
|
: answers) { |
|
59
|
|
|
if (answer.getRight() != null) { |
|
60
|
|
|
if (answer.getRight().equals("on")) { |
|
61
|
|
|
rightCount = rightCount + 1; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
if (serialNumbers.contains(answer.getSerialNumber()) || answer.getSerialNumber() == null) { |
|
65
|
|
|
serialNumbersConflict = true; |
|
66
|
|
|
} else { |
|
67
|
|
|
serialNumbers.add(answer.getSerialNumber()); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
final String rightCountError = "right"; |
|
71
|
|
|
final String serialNumberConflictError = "serialNumber"; |
|
72
|
|
|
final String answersCountError = "answersCount"; |
|
73
|
|
|
switch (questionType) { |
|
74
|
|
|
//Один ответ |
|
75
|
|
|
case 1: |
|
76
|
|
|
if (rightCount != 1) { |
|
77
|
|
|
errors.reject(rightCountError, "Not one answer in an one-answer question"); |
|
78
|
|
|
} |
|
79
|
|
|
break; |
|
80
|
|
|
//Много ответов |
|
81
|
|
|
case 2: |
|
82
|
|
|
break; |
|
83
|
|
|
//Нумерация |
|
84
|
|
|
case 3: |
|
85
|
|
|
if (serialNumbersConflict) { |
|
86
|
|
|
errors.reject(serialNumberConflictError, "Invalid numeration. Check for repeating and missing order values"); |
|
87
|
|
|
} |
|
88
|
|
|
break; |
|
89
|
|
|
//Текст |
|
90
|
|
|
case 4: |
|
91
|
|
|
if (answers.size() != 1) { |
|
92
|
|
|
errors.reject(answersCountError, "There is more than one answer"); |
|
93
|
|
|
} |
|
94
|
|
|
break; |
|
95
|
|
|
default: |
|
96
|
|
|
throw new NotFoundException(); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|