cc.protea.forms.model.questions.NameQuestionMethods 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.Name;
public class NameQuestionMethods extends BaseQuestionMethods {
	public String getTypeSpecificError(FormQuestion question) {
		if (question.options != null && ! question.options.isEmpty()) {
			return "'" + StringUtils.trimToEmpty(question.label) + "' should not contain any options";
		}
		if (question.otherRules != null) {
			return "'" + StringUtils.trimToEmpty(question.label) + "' should not contain 'other rules'";
		}
		return null;
	}
	public String getTypeSpecificError(FormQuestion question, Answer answer) {
		for (String key : answer.parts.keySet()) {
			switch (key) {
				case "FIRST_NAME":
				case "LAST_NAME":
					break;
				default:
					return "'" + question.label + "' contains unexpected answer part '" + key + "'";
			}
		}
		return null;
	}
	public boolean isAnswered(FormQuestion question, Answer answer) {
		return get(answer) != null;
	}
	public String toString(FormQuestion question, Answer answer) {
		Name name = get(answer);
		return name == null ? "" : name.toString();
	}
	public Name get(Answer answer) {
		if (answer == null) {
			return null;
		}
		Name name = new Name();
		if (QuestionType.STRING.equals(answer.questionType)) {
			name.first = StringUtils.isBlank(answer.answerString) ? null : StringUtils.substringBefore(answer.answerString, " ");
			name.last = StringUtils.isBlank(answer.answerString) ? null : StringUtils.substringAfter(answer.answerString, " ");
			return name;
		}
		if (answer.parts.get("FIRST_NAME") != null) {
			name.first = StringUtils.trimToNull(answer.parts.get("FIRST_NAME").answerString);
		}
		if (answer.parts.get("LAST_NAME") != null) {
			name.last = StringUtils.trimToNull(answer.parts.get("LAST_NAME").answerString);
		}
		if (StringUtils.isBlank(name.toString())) {
			return null;
		}
		return name;
	}
}
     © 2015 - 2025 Weber Informatics LLC | Privacy Policy