use of org.javarosa.core.model.data.helper.Selection in project collect by opendatakit.
the class GeneralSelectMultiWidgetTest method getAnswerShouldReflectWhichSelectionsWereMade.
@Test
public void getAnswerShouldReflectWhichSelectionsWereMade() {
W widget = getWidget();
assertNull(widget.getAnswer());
List<SelectChoice> selectChoices = getSelectChoices();
List<String> selectedValues = new ArrayList<>();
boolean atLeastOneSelected = false;
for (int i = 0; i < widget.getChoiceCount(); i++) {
boolean shouldBeSelected = random.nextBoolean();
widget.setChoiceSelected(i, shouldBeSelected);
atLeastOneSelected = atLeastOneSelected || shouldBeSelected;
if (shouldBeSelected) {
SelectChoice selectChoice = selectChoices.get(i);
selectedValues.add(selectChoice.getValue());
}
}
// null answer case:
if (!atLeastOneSelected) {
int randomIndex = (Math.abs(random.nextInt()) % widget.getChoiceCount());
widget.setChoiceSelected(randomIndex, true);
SelectChoice selectChoice = selectChoices.get(randomIndex);
selectedValues.add(selectChoice.getValue());
}
SelectMultiData answer = (SelectMultiData) widget.getAnswer();
@SuppressWarnings("unchecked") List<Selection> answerSelections = (List<Selection>) answer.getValue();
List<String> answerValues = selectionsToValues(answerSelections);
for (String selectedValue : selectedValues) {
assertTrue(answerValues.contains(selectedValue));
}
}
use of org.javarosa.core.model.data.helper.Selection in project collect by opendatakit.
the class GeneralSelectOneWidgetTest method getNextAnswer.
@NonNull
@Override
public SelectOneData getNextAnswer() {
List<SelectChoice> selectChoices = getSelectChoices();
int selectedIndex = Math.abs(random.nextInt()) % selectChoices.size();
SelectChoice selectChoice = selectChoices.get(selectedIndex);
Selection selection = new Selection(selectChoice);
return new SelectOneData(selection);
}
use of org.javarosa.core.model.data.helper.Selection in project javarosa by opendatakit.
the class XFormAnswerDataParser method getSelections.
private static List<Selection> getSelections(String text, QuestionDef q) {
List<String> choices = DateUtils.split(text, XFormAnswerDataSerializer.DELIMITER, true);
// assume they are all still valid...
List<Selection> v = new ArrayList<Selection>(choices.size());
for (int i = 0; i < choices.size(); i++) {
Selection s = getSelection((String) choices.get(i), q);
if (s != null)
v.add(s);
}
return v;
}
use of org.javarosa.core.model.data.helper.Selection in project javarosa by opendatakit.
the class XFormAnswerDataParser method getSelection.
private static Selection getSelection(String choiceValue, QuestionDef q) {
Selection s;
if (q == null || q.getDynamicChoices() != null) {
s = new Selection(choiceValue);
} else {
SelectChoice choice = q.getChoiceForValue(choiceValue);
s = (choice != null ? choice.selection() : null);
}
return s;
}
use of org.javarosa.core.model.data.helper.Selection in project javarosa by opendatakit.
the class FormEntryPrompt method getAnswerValue.
// note: code overlap with FormDef.copyItemsetAnswer
public IAnswerData getAnswerValue() {
QuestionDef q = getQuestion();
ItemsetBinding itemset = q.getDynamicChoices();
if (itemset != null) {
if (itemset.valueRef != null) {
List<SelectChoice> choices = getSelectChoices();
List<String> preselectedValues = new ArrayList<String>();
// determine which selections are already present in the answer
if (itemset.copyMode) {
TreeReference destRef = itemset.getDestRef().contextualize(mTreeElement.getRef());
List<TreeReference> subNodes = form.getEvaluationContext().expandReference(destRef);
for (int i = 0; i < subNodes.size(); i++) {
TreeElement node = form.getMainInstance().resolveReference(subNodes.get(i));
String value = itemset.getRelativeValue().evalReadable(form.getMainInstance(), new EvaluationContext(form.getEvaluationContext(), node.getRef()));
preselectedValues.add(value);
}
} else {
List<Selection> sels;
IAnswerData data = mTreeElement.getValue();
if (data instanceof SelectMultiData) {
sels = (List<Selection>) data.getValue();
} else if (data instanceof SelectOneData) {
sels = new ArrayList<Selection>(1);
sels.add((Selection) data.getValue());
} else {
sels = new ArrayList<Selection>(0);
}
for (int i = 0; i < sels.size(); i++) {
preselectedValues.add(sels.get(i).xmlValue);
}
}
// populate 'selection' with the corresponding choices (matching 'value') from the dynamic choiceset
List<Selection> selection = new ArrayList<Selection>();
for (int i = 0; i < preselectedValues.size(); i++) {
String value = preselectedValues.get(i);
SelectChoice choice = null;
for (int j = 0; j < choices.size(); j++) {
SelectChoice ch = choices.get(j);
if (value.equals(ch.getValue())) {
choice = ch;
break;
}
}
// will no longer be an option this go around
if (choice != null) {
selection.add(choice.selection());
}
}
// convert to IAnswerData
if (selection.size() == 0) {
return null;
} else if (q.getControlType() == Constants.CONTROL_SELECT_MULTI) {
return new SelectMultiData(selection);
} else if (q.getControlType() == Constants.CONTROL_SELECT_ONE) {
// do something if more than one selected?
return new SelectOneData(selection.get(0));
} else {
throw new RuntimeException("can't happen");
}
} else {
// cannot map up selections without <value>
return null;
}
} else {
// static choices
return mTreeElement.getValue();
}
}
Aggregations