Package com.vst.webapp.validators

Source Code of com.vst.webapp.validators.ConstructionQuestionsValidator

package com.vst.webapp.validators;


import com.vst.model.Answer;
import com.vst.model.BuildingObject;
import com.vst.model.ObjectAnswer;
import com.vst.model.ObjectConstruction;
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 ConstructionQuestionsValidator implements Validator {
    private QuestionManager questionManager = null;


    public void setQuestionManager(QuestionManager questionManager) {
        this.questionManager = questionManager;
    }

    public boolean supports(Class candidate) {
        return ObjectConstruction.class.isAssignableFrom(candidate);
    }

    public void validate(Object obj, Errors errors) {
        ObjectConstruction objectConstruction = (ObjectConstruction) obj;
        //checking if all answers were given and if there are necessary questions tha were not answered
        List objectAnswerList = objectConstruction.getCurrentDocumentationQuestions();
        boolean allAnswers = true;
        boolean noNecessaryAnswers = false;
        boolean dataTooLong = 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 (dataTooLong) {
            errors.rejectValue("currentDocumentationQuestions", "documentQuestions.tooLongAnswer");
        }
        if (!noNecessaryAnswers && !allAnswers) {
            errors.rejectValue("currentDocumentationQuestions", "documentQuestions.notAllAnswers");
        }
        if (noNecessaryAnswers) {
            errors.rejectValue("currentDocumentationQuestions", "documentQuestions.notAllNecessaryAnswers");
        }
    }


}
TOP

Related Classes of com.vst.webapp.validators.ConstructionQuestionsValidator

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.