Search in sources :

Example 21 with Selection

use of org.javarosa.core.model.data.helper.Selection in project javarosa by opendatakit.

the class FormDef method copyItemsetAnswer.

public void copyItemsetAnswer(QuestionDef q, TreeElement targetNode, IAnswerData data, boolean midSurvey) throws InvalidReferenceException {
    ItemsetBinding itemset = q.getDynamicChoices();
    TreeReference targetRef = targetNode.getRef();
    TreeReference destRef = itemset.getDestRef().contextualize(targetRef);
    List<Selection> selections = null;
    if (data instanceof SelectMultiData) {
        selections = (List<Selection>) data.getValue();
    } else if (data instanceof SelectOneData) {
        selections = new ArrayList<Selection>(1);
        selections.add((Selection) data.getValue());
    }
    List<String> selectedValues;
    if (itemset.valueRef != null) {
        selectedValues = new ArrayList<String>(selections.size());
        for (Selection selection : selections) {
            selectedValues.add(selection.choice.getValue());
        }
    } else {
        selectedValues = new ArrayList<String>(0);
    }
    // delete existing dest nodes that are not in the answer selection
    HashMap<String, TreeElement> existingValues = new HashMap<String, TreeElement>();
    List<TreeReference> existingNodes = exprEvalContext.expandReference(destRef);
    for (TreeReference existingNode : existingNodes) {
        TreeElement node = getMainInstance().resolveReference(existingNode);
        if (itemset.valueRef != null) {
            String value = itemset.getRelativeValue().evalReadable(this.getMainInstance(), new EvaluationContext(exprEvalContext, node.getRef()));
            if (selectedValues.contains(value)) {
                // cache node if in selection
                existingValues.put(value, node);
            // and already exists
            }
        }
        // delete from target
        targetNode.removeChild(node);
    }
    // copy in nodes for new answer; preserve ordering in answer
    for (int i = 0; i < selections.size(); i++) {
        Selection s = selections.get(i);
        SelectChoice ch = s.choice;
        TreeElement cachedNode = null;
        if (itemset.valueRef != null) {
            String value = ch.getValue();
            if (existingValues.containsKey(value)) {
                cachedNode = existingValues.get(value);
            }
        }
        if (cachedNode != null) {
            cachedNode.setMult(i);
            targetNode.addChild(cachedNode);
        } else {
            getMainInstance().copyItemsetNode(ch.copyNode, destRef, this);
        }
    }
    dagImpl.copyItemsetAnswer(getMainInstance(), getEvaluationContext(), destRef, targetNode, midSurvey);
}
Also used : SelectOneData(org.javarosa.core.model.data.SelectOneData) HashMap(java.util.HashMap) Selection(org.javarosa.core.model.data.helper.Selection) ArrayList(java.util.ArrayList) 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)

Example 22 with Selection

use of org.javarosa.core.model.data.helper.Selection in project javarosa by opendatakit.

the class SelectMultiData method cast.

@Override
public SelectMultiData cast(UncastData data) throws IllegalArgumentException {
    List<String> choices = DateUtils.split(data.value, " ", true);
    List<Selection> v = new ArrayList<Selection>(choices.size());
    for (String s : choices) {
        v.add(new Selection(s));
    }
    return new SelectMultiData(v);
}
Also used : Selection(org.javarosa.core.model.data.helper.Selection) ArrayList(java.util.ArrayList)

Example 23 with Selection

use of org.javarosa.core.model.data.helper.Selection in project javarosa by opendatakit.

the class SelectMultiDataTests method assertVectorIdentity.

private void assertVectorIdentity(String messageHeader, List<Selection> v, Selection[] a) {
    assertEquals(messageHeader + "SelectMultiData's internal representation was violated. Vector size changed.", v.size(), a.length);
    for (int i = 0; i < v.size(); ++i) {
        Selection internalValue = v.get(i);
        Selection copyValue = a[i];
        assertEquals(messageHeader + "SelectMultiData's internal representation was violated. Element " + i + "changed.", internalValue, copyValue);
    }
}
Also used : Selection(org.javarosa.core.model.data.helper.Selection)

Example 24 with Selection

use of org.javarosa.core.model.data.helper.Selection in project javarosa by opendatakit.

the class SelectMultiDataTests method testVectorImmutability.

public void testVectorImmutability() {
    SelectMultiData data = new SelectMultiData(firstTwo);
    Selection[] copy = new Selection[firstTwo.size()];
    firstTwo.toArray(copy);
    firstTwo.set(0, two);
    firstTwo.remove(1);
    List<Selection> internal = (List<Selection>) data.getValue();
    assertVectorIdentity("External Reference: ", internal, copy);
    data.setValue(lastTwo);
    List<Selection> start = (List<Selection>) data.getValue();
    Selection[] external = new Selection[start.size()];
    start.toArray(external);
    start.remove(1);
    start.set(0, one);
    assertVectorIdentity("Internal Reference: ", (List<Selection>) data.getValue(), external);
}
Also used : SelectMultiData(org.javarosa.core.model.data.SelectMultiData) Selection(org.javarosa.core.model.data.helper.Selection) List(java.util.List) ArrayList(java.util.ArrayList)

Example 25 with Selection

use of org.javarosa.core.model.data.helper.Selection in project javarosa by opendatakit.

the class SelectMultiDataTests method setUp.

/* (non-Javadoc)
     * @see j2meunit.framework.TestCase#setUp()
     */
public void setUp() throws Exception {
    super.setUp();
    question = new QuestionDef();
    for (int i = 0; i < 4; i++) {
        question.addSelectChoice(new SelectChoice("", "Selection" + i, "Selection " + i, false));
    }
    one = new Selection("Selection 1");
    one.attachChoice(question);
    two = new Selection("Selection 2");
    two.attachChoice(question);
    three = new Selection("Selection 3");
    three.attachChoice(question);
    firstTwo = new ArrayList<Selection>();
    firstTwo.add(one);
    firstTwo.add(two);
    lastTwo = new ArrayList<Selection>();
    lastTwo.add(two);
    lastTwo.add(three);
    invalid = new ArrayList<Object>();
    invalid.add(three);
    invalid.add(new Integer(12));
    invalid.add(one);
}
Also used : SelectChoice(org.javarosa.core.model.SelectChoice) Selection(org.javarosa.core.model.data.helper.Selection) 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