package com.vst.webapp.validators;
import com.vst.model.Answer;
import com.vst.model.BuildingObject;
import com.vst.model.ObjectAnswer;
import com.vst.service.QuestionManager;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import java.util.List;
/**
* Created by IntelliJ IDEA.
* User: ALEXEI
* Date: 05.01.2008
* Time: 0:16:07
* To change this template use File | Settings | File Templates.
*/
public class DocumentationQuestionsValidator implements Validator {
private QuestionManager questionManager = null;
public void setQuestionManager(QuestionManager questionManager) {
this.questionManager = questionManager;
}
public boolean supports(Class candidate) {
return BuildingObject.class.isAssignableFrom(candidate);
}
public void validate(Object obj, Errors errors) {
BuildingObject buildingObject = (BuildingObject) obj;
//checking if all answers were given and if there are necessary questions tha were not answered
List objectAnswerList = buildingObject.getCurrentDocumentationQuestions();
boolean allAnswers = true;
boolean noNecessaryAnswers = false;
boolean dataTooLong = false;
boolean commentDataTooLong = false;
for (int i = 0; i < objectAnswerList.size(); i++) {
ObjectAnswer objectAnswer = (ObjectAnswer) objectAnswerList.get(i);
objectAnswer.setQuestion(questionManager.getQuestion(objectAnswer.getQuestion().getQuestionId().toString()));
//with variants of answers
if (objectAnswer.getQuestion().getAnswers().size() > 0) {
List answerList = objectAnswer.getAnswers();
if (!objectAnswer.getQuestion().isFewAnswers()) {
//only one answer is possible
for (int j = 0; j < answerList.size(); j++) {
Answer answer = (Answer) answerList.get(j);
if (answer.getAnswerId() == null || answer.getAnswerId().equals(new Long(-1))) {
allAnswers = false;
if (objectAnswer.getQuestion().isNecessary()) {
noNecessaryAnswers = true;
}
}
}
} else {
//a few answers are possible
boolean noAnswers = true;
for (int j = 0; j < answerList.size(); j++) {
Answer answer = (Answer) answerList.get(j);
if (answer.getAnswerId() != null && !answer.getAnswerId().equals(new Long(-1))) {
noAnswers = false;
break;
}
}
if (noAnswers) {
allAnswers = false;
if (objectAnswer.getQuestion().isNecessary()) {
noNecessaryAnswers = true;
}
}
}
} else {
//without variants
if (objectAnswer.getAnswerContents() == null || objectAnswer.getAnswerContents().trim().equals("")) {
allAnswers = false;
if (objectAnswer.getQuestion().isNecessary()) {
noNecessaryAnswers = true;
}
}
if(objectAnswer.getAnswerContents() != null && objectAnswer.getAnswerContents().length()>500){
dataTooLong=true;
}
if(objectAnswer.getAnswerComment()!=null && objectAnswer.getAnswerComment().length()>500){
commentDataTooLong=true;
}
}
}
//System.out.println("dataTooLong======"+dataTooLong);
if(dataTooLong){
errors.rejectValue("currentDocumentationQuestions", "documentQuestions.tooLongAnswer");
}
if(commentDataTooLong){
errors.rejectValue("currentDocumentationQuestions", "documentQuestions.commentTooLongAnswer");
}
if (!noNecessaryAnswers && !allAnswers) {
errors.rejectValue("currentDocumentationQuestions", "documentQuestions.notAllAnswers");
}
if (noNecessaryAnswers) {
errors.rejectValue("currentDocumentationQuestions", "documentQuestions.notAllNecessaryAnswers");
}
}
}