cc.protea.forms.model.questions.YesNoQuestionMethods Maven / Gradle / Ivy
                 Go to download
                
        
                    Show more of this group  Show more artifacts with this name
Show all versions of forms Show documentation
                Show all versions of forms Show documentation
Add server-side dynamic form handling
                
             The newest version!
        
        package cc.protea.forms.model.questions;
import org.apache.commons.lang3.StringUtils;
import cc.protea.forms.model.Answer;
import cc.protea.forms.model.FormQuestion;
import cc.protea.forms.model.FormQuestion.OptionsDisplay;
import cc.protea.foundation.integrations.JsonUtil;
public class YesNoQuestionMethods extends BaseQuestionMethods {
	public String getTypeSpecificError(FormQuestion question) {
		return null;
	}
	public String getTypeSpecificError(FormQuestion question, Answer answer) {
		if (answer.parts != null && ! answer.parts.isEmpty()) {
			return "'" + StringUtils.trimToEmpty(question.label) + "' should not be answered in parts";
		}
		if (StringUtils.isBlank(answer.answerString)) {
			return null;
		}
		if (get(answer) == null) {
			return "'" + StringUtils.trimToEmpty(question.label) + "': '" + answer.answerString + "' not understood";
		}
		if (question.optionsDisplay == null) {
			return "'" + StringUtils.trimToEmpty(question.label) + "' should have optionsDisplay set";
		}
		switch (question.optionsDisplay) {
			case RADIO_BUTTONS:
			case CHECKBOXES:
			case DROPDOWN:
				break;
			default:
				return "'" + StringUtils.trimToEmpty(question.label) + "' has an invalid optionsDisplay set";
		}
		return null;
	}
	public boolean isAnswered(FormQuestion question, Answer answer) {
		Boolean choice = get(answer);
		if (OptionsDisplay.CHECKBOXES.equals(question.optionsDisplay)) {
			return Boolean.TRUE.equals(choice);
		}
		return (choice != null);
	}
	public String toString(FormQuestion question, Answer answer) {
		return Boolean.TRUE.equals(get(answer)) ? "TRUE" : "FALSE";
	}
	public Boolean get(Answer answer) {
		if (answer == null || (answer.parts != null && !answer.parts.isEmpty())) {
			return null;
		}
		if (! QuestionType.YES_NO.equals(answer.questionType)) {
			return null;
		}
		if (StringUtils.isBlank(answer.answerString)) {
			return null;
		}
		return JsonUtil.fromJson(answer.answerString, Boolean.class);
	}
}
     © 2015 - 2025 Weber Informatics LLC | Privacy Policy