Search in sources :

Example 16 with Selection

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));
    }
}
Also used : SelectChoice(org.javarosa.core.model.SelectChoice) Selection(org.javarosa.core.model.data.helper.Selection) ArrayList(java.util.ArrayList) SelectMultiData(org.javarosa.core.model.data.SelectMultiData) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 17 with Selection

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);
}
Also used : SelectOneData(org.javarosa.core.model.data.SelectOneData) SelectChoice(org.javarosa.core.model.SelectChoice) Selection(org.javarosa.core.model.data.helper.Selection) NonNull(android.support.annotation.NonNull)

Example 18 with 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;
}
Also used : Selection(org.javarosa.core.model.data.helper.Selection) ArrayList(java.util.ArrayList)

Example 19 with Selection

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;
}
Also used : SelectChoice(org.javarosa.core.model.SelectChoice) Selection(org.javarosa.core.model.data.helper.Selection)

Example 20 with Selection

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();
    }
}
Also used : IAnswerData(org.javarosa.core.model.data.IAnswerData) SelectOneData(org.javarosa.core.model.data.SelectOneData) SelectChoice(org.javarosa.core.model.SelectChoice) Selection(org.javarosa.core.model.data.helper.Selection) ArrayList(java.util.ArrayList) ItemsetBinding(org.javarosa.core.model.ItemsetBinding) ConstraintHint(org.javarosa.core.model.condition.pivot.ConstraintHint) Constraint(org.javarosa.core.model.condition.Constraint) TreeElement(org.javarosa.core.model.instance.TreeElement) SelectMultiData(org.javarosa.core.model.data.SelectMultiData) TreeReference(org.javarosa.core.model.instance.TreeReference) EvaluationContext(org.javarosa.core.model.condition.EvaluationContext) QuestionDef(org.javarosa.core.model.QuestionDef)

Aggregations

Selection (org.javarosa.core.model.data.helper.Selection)26 SelectMultiData (org.javarosa.core.model.data.SelectMultiData)14 ArrayList (java.util.ArrayList)13 SelectChoice (org.javarosa.core.model.SelectChoice)12 List (java.util.List)8 SelectOneData (org.javarosa.core.model.data.SelectOneData)8 QuestionDef (org.javarosa.core.model.QuestionDef)4 Constraint (org.javarosa.core.model.condition.Constraint)4 IAnswerData (org.javarosa.core.model.data.IAnswerData)4 SuppressLint (android.annotation.SuppressLint)3 NonNull (android.support.annotation.NonNull)2 EvaluationContext (org.javarosa.core.model.condition.EvaluationContext)2 ConstraintHint (org.javarosa.core.model.condition.pivot.ConstraintHint)2 DateData (org.javarosa.core.model.data.DateData)2 DateTimeData (org.javarosa.core.model.data.DateTimeData)2 TreeElement (org.javarosa.core.model.instance.TreeElement)2 TreeReference (org.javarosa.core.model.instance.TreeReference)2 Test (org.junit.Test)2 ImmutableList (com.google.common.collect.ImmutableList)1 BigDecimal (java.math.BigDecimal)1