All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cc.protea.forms.model.questions.PickManyQuestionMethods Maven / Gradle / Ivy

The newest version!
package cc.protea.forms.model.questions;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import cc.protea.forms.model.Answer;
import cc.protea.forms.model.Answer.AnswerPart;
import cc.protea.forms.model.FormQuestion;

public class PickManyQuestionMethods extends BaseQuestionMethods> {

	class StringHolder { String string = null; }

	public String getTypeSpecificError(FormQuestion question) {
		if (question.options == null || question.options.isEmpty()) {
			return "'" + StringUtils.trimToEmpty(question.label) + "' should contain at least one option";
		}
		if (question.optionsDisplay == null) {
			return "'" + StringUtils.trimToEmpty(question.label) + "' should have optionsDisplay set";
		}
		switch(question.optionsDisplay) {
			case CHECKBOXES:
				break;
			default:
				return "'" + StringUtils.trimToEmpty(question.label) + "' has an invalid optionsDisplay set";
		}
		return null;
	}

	public String getTypeSpecificError(FormQuestion question, Answer answer) {
		if (answer.parts == null) {
			return "'" + StringUtils.trimToEmpty(question.label) + "' should be answered in parts";
		}
		final StringHolder ret = new StringHolder();
		answer.parts.forEach((key, value) -> {
			if (question.options.stream().anyMatch(option -> key.equals(option.value))) {
				return;
			}
			if (question.otherRules != null && key.equals(question.otherRules.value)) {
				return;
			}
			ret.string = "'" + question.label + "' contains unexpected answer part '" + key + "'";
		});
		// Check that there are no unexpected parts (unless "Other")
		return ret.string;
	}

	public boolean isAnswered(FormQuestion question, Answer answer) {
		Map map = get(answer);
		if (map == null || map.isEmpty()) {
			return false;
		}
		return true;
	}

	public String toString(FormQuestion question, Answer answer) {
		Map map = get(answer);
		if (map == null) {
			return "";
		}
		StringBuilder sb = new StringBuilder();
		question.options.forEach(option ->  {
			if (map.containsKey(option.value)) {
				if (sb.length() > 0) {
					sb.append(", ");
				}
				sb.append(option.label);
			}
		});
		if (question.otherRules != null && map.containsKey(question.otherRules.value)) {
			if (sb.length() > 0) {
				sb.append(", ");
			}
			sb.append(question.otherRules.label).append(": ").append(map.get(question.otherRules.value));
		}
		return sb.toString();
	}

	public long price(FormQuestion question, Answer answer) {
		Map map = get(answer);
		long price = 0L;
		if (map == null) {
			return price;
		}
		for (FormQuestion.Option option : question.options) {
			if (map.containsKey(option.value) && option.priceCents != null) {
				price += option.priceCents;
			}
		}
		if (question.otherRules != null) {
			if (question.otherRules.type == FormQuestion.OtherRules.Type.PRICE) {
				if (answer.parts.containsKey(question.otherRules.value) && answer.parts.get(question.otherRules.value).amountInCents != null) {
					price += answer.parts.get(question.otherRules.value).amountInCents;
				}
			} else if (map.containsKey(question.otherRules.value) && question.otherRules.priceCents != null) {
				price += question.otherRules.priceCents;
			}
		}
		return price;
	}

	public Map get(Answer answer) {
		if (answer == null || answer.parts == null || answer.parts.isEmpty()) {
			return null;
		}
		Map map = new HashMap<>(answer.parts.size());
		for (String key : answer.parts.keySet()) {
			Answer.AnswerPart part = answer.parts.get(key);
			map.put(key, part.answerString);
		}
		return map;
	}
	
	public FormQuestion.Option getOption(FormQuestion question, AnswerPart answerPart) {
		if (question.options == null || question.options.isEmpty()) {
			return null;
		}
		if (answerPart == null) {
			return null;
		}
		for (FormQuestion.Option option : question.options) { 
			if (StringUtils.equals(option.value, answerPart.answerString)) {
				return option;
			}
		}
		if (question.otherRules != null && StringUtils.equals(question.otherRules.value, answerPart.answerString)) {
			return question.otherRules;
		}
		return null;
	}

	
	public FormQuestion.Option getOption(FormQuestion question, Answer answer) {
		return null;
	}

	public boolean isOption(FormQuestion question, Answer answer) {
		if (super.isOption(question, answer)) {
			return true;
		}
		for (AnswerPart part : answer.parts.values()) {
			if (getOption(question, part) != null) {
				return true;
			}
		}
		return false;
	}



}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy