Search in sources :

Example 6 with IAnswerData

use of org.javarosa.core.model.data.IAnswerData in project collect by opendatakit.

the class GeneralStringWidgetTest method getAnswerShouldReturnNewAnswerWhenTextFieldIsUpdated.

@Test
public void getAnswerShouldReturnNewAnswerWhenTextFieldIsUpdated() {
    // Make sure it starts null:
    super.getAnswerShouldReturnNullIfPromptDoesNotHaveExistingAnswer();
    W widget = getWidget();
    IAnswerData answer = getNextAnswer();
    widget.getAnswerTextField().setText(answer.getDisplayText());
    IAnswerData computedAnswer = widget.getAnswer();
    assertEquals(answer.getDisplayText(), computedAnswer.getDisplayText());
}
Also used : IAnswerData(org.javarosa.core.model.data.IAnswerData) Test(org.junit.Test)

Example 7 with IAnswerData

use of org.javarosa.core.model.data.IAnswerData in project collect by opendatakit.

the class RangeWidgetTest method getAnswerShouldReflectActualValueSetViaSeekBar.

@Test
public void getAnswerShouldReflectActualValueSetViaSeekBar() {
    W widget = getWidget();
    assertNull(widget.getAnswer());
    int progress = Math.abs(random.nextInt()) % widget.getElementCount();
    widget.onProgressChanged(widget.getSeekBar(), progress, true);
    BigDecimal actualValue;
    if (rangeStart.compareTo(rangeEnd) == -1) {
        actualValue = rangeStart.add(new BigDecimal(progress).multiply(rangeStep));
    } else {
        actualValue = rangeStart.subtract(new BigDecimal(progress).multiply(rangeStep));
    }
    IAnswerData answer = widget.getAnswer();
    IAnswerData compareTo;
    if (answer instanceof DecimalData) {
        compareTo = new DecimalData(actualValue.doubleValue());
    } else {
        compareTo = new IntegerData(actualValue.intValue());
    }
    assertEquals(answer.getDisplayText(), compareTo.getDisplayText());
}
Also used : IAnswerData(org.javarosa.core.model.data.IAnswerData) DecimalData(org.javarosa.core.model.data.DecimalData) IntegerData(org.javarosa.core.model.data.IntegerData) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 8 with IAnswerData

use of org.javarosa.core.model.data.IAnswerData in project collect by opendatakit.

the class ODKView method getAnswers.

/**
 * @return a HashMap of answers entered by the user for this set of widgets
 */
public HashMap<FormIndex, IAnswerData> getAnswers() {
    HashMap<FormIndex, IAnswerData> answers = new LinkedHashMap<>();
    for (QuestionWidget q : widgets) {
        /*
             * The FormEntryPrompt has the FormIndex, which is where the answer gets stored. The
             * QuestionWidget has the answer the user has entered.
             */
        FormEntryPrompt p = q.getFormEntryPrompt();
        answers.put(p.getIndex(), q.getAnswer());
    }
    return answers;
}
Also used : IAnswerData(org.javarosa.core.model.data.IAnswerData) FormEntryPrompt(org.javarosa.form.api.FormEntryPrompt) FormIndex(org.javarosa.core.model.FormIndex) QuestionWidget(org.odk.collect.android.widgets.QuestionWidget) LinkedHashMap(java.util.LinkedHashMap)

Example 9 with IAnswerData

use of org.javarosa.core.model.data.IAnswerData in project collect by opendatakit.

the class FormController method getSubmissionMetadata.

/**
 * Get the OpenRosa required metadata of the portion of the form beng submitted
 */
public InstanceMetadata getSubmissionMetadata() {
    FormDef formDef = formEntryController.getModel().getForm();
    TreeElement rootElement = formDef.getInstance().getRoot();
    TreeElement trueSubmissionElement;
    // Determine the information about the submission...
    SubmissionProfile p = formDef.getSubmissionProfile();
    if (p == null || p.getRef() == null) {
        trueSubmissionElement = rootElement;
    } else {
        IDataReference ref = p.getRef();
        trueSubmissionElement = formDef.getInstance().resolveReference(ref);
        // resolveReference returns null if the reference is to the root element...
        if (trueSubmissionElement == null) {
            trueSubmissionElement = rootElement;
        }
    }
    // and find the depth-first meta block in this...
    TreeElement e = findDepthFirst(trueSubmissionElement, "meta");
    String instanceId = null;
    String instanceName = null;
    boolean audit = false;
    if (e != null) {
        List<TreeElement> v;
        // instance id...
        v = e.getChildrenWithName(INSTANCE_ID);
        if (v.size() == 1) {
            IAnswerData sa = v.get(0).getValue();
            if (sa != null) {
                instanceId = sa.getDisplayText();
            }
        }
        // instance name...
        v = e.getChildrenWithName(INSTANCE_NAME);
        if (v.size() == 1) {
            IAnswerData sa = v.get(0).getValue();
            if (sa != null) {
                instanceName = sa.getDisplayText();
            }
        }
        // timing element...
        v = e.getChildrenWithName(AUDIT);
        if (v.size() == 1) {
            audit = true;
            IAnswerData answerData = new StringData();
            answerData.setValue(AUDIT_FILE_NAME);
            v.get(0).setValue(answerData);
        }
    }
    return new InstanceMetadata(instanceId, instanceName, audit);
}
Also used : IAnswerData(org.javarosa.core.model.data.IAnswerData) FormDef(org.javarosa.core.model.FormDef) IDataReference(org.javarosa.core.model.IDataReference) SubmissionProfile(org.javarosa.core.model.SubmissionProfile) StringData(org.javarosa.core.model.data.StringData) TreeElement(org.javarosa.core.model.instance.TreeElement)

Example 10 with IAnswerData

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

the class TreeElement method populate.

// rebuilding a node from an imported instance
// there's a lot of error checking we could do on the received instance, but it's
// easier to just ignore the parts that are incorrect
public void populate(TreeElement incoming, FormDef f) {
    if (this.isLeaf()) {
        // check that incoming doesn't have children?
        IAnswerData value = incoming.getValue();
        if (value == null) {
            this.setValue(null);
        } else if (this.dataType == Constants.DATATYPE_TEXT || this.dataType == Constants.DATATYPE_NULL) {
            // value is a StringData
            this.setValue(value);
        } else {
            String textVal = (String) value.getValue();
            // if there is no other IAnswerResolver, use the default one.
            IAnswerResolver answerResolver = XFormParser.getAnswerResolver();
            if (answerResolver == null) {
                answerResolver = new DefaultAnswerResolver();
            }
            this.setValue(answerResolver.resolveAnswer(textVal, this, f));
        }
    } else {
        List<String> names = new ArrayList<String>(this.getNumChildren());
        for (int i = 0; i < this.getNumChildren(); i++) {
            TreeElement child = this.getChildAt(i);
            if (!names.contains(child.getName())) {
                names.add(child.getName());
            }
        }
        // remove all default repetitions from skeleton data model (_preserving_ templates, though)
        for (int i = 0; i < this.getNumChildren(); i++) {
            TreeElement child = this.getChildAt(i);
            if (child.getMaskVar(MASK_REPEATABLE) && child.getMult() != TreeReference.INDEX_TEMPLATE) {
                this.removeChildAt(i);
                i--;
            }
        }
        // make sure ordering is preserved (needed for compliance with xsd schema)
        if (this.getNumChildren() != names.size()) {
            throw new RuntimeException("sanity check failed");
        }
        for (int i = 0; i < this.getNumChildren(); i++) {
            TreeElement child = this.getChildAt(i);
            String expectedName = names.get(i);
            if (!child.getName().equals(expectedName)) {
                TreeElement child2 = null;
                int j;
                for (j = i + 1; j < this.getNumChildren(); j++) {
                    child2 = this.getChildAt(j);
                    if (child2.getName().equals(expectedName)) {
                        break;
                    }
                }
                if (j == this.getNumChildren()) {
                    throw new RuntimeException("sanity check failed");
                }
                this.removeChildAt(j);
                this.children.add(i, child2);
            }
        }
        for (int i = 0; i < this.getNumChildren(); i++) {
            TreeElement child = this.getChildAt(i);
            List<TreeElement> newChildren = incoming.getChildrenWithName(child.getName());
            if (child.getMaskVar(MASK_REPEATABLE)) {
                for (int k = 0; k < newChildren.size(); k++) {
                    TreeElement newChild = child.deepCopy(true);
                    newChild.setMult(k);
                    this.children.add(i + k + 1, newChild);
                    newChild.populate(newChildren.get(k), f);
                }
                i += newChildren.size();
            } else {
                if (newChildren.size() == 0) {
                    child.setRelevant(false);
                } else {
                    child.populate(newChildren.get(0), f);
                }
            }
        }
    }
    for (int i = 0; i < incoming.getAttributeCount(); i++) {
        String name = incoming.getAttributeName(i);
        String ns = incoming.getAttributeNamespace(i);
        String value = incoming.getAttributeValue(i);
        this.setAttribute(ns, name, value);
    }
}
Also used : IAnswerData(org.javarosa.core.model.data.IAnswerData) IAnswerResolver(org.javarosa.core.model.instance.utils.IAnswerResolver) ArrayList(java.util.ArrayList) DefaultAnswerResolver(org.javarosa.core.model.instance.utils.DefaultAnswerResolver) Constraint(org.javarosa.core.model.condition.Constraint)

Aggregations

IAnswerData (org.javarosa.core.model.data.IAnswerData)31 Test (org.junit.Test)8 Constraint (org.javarosa.core.model.condition.Constraint)6 TreeReference (org.javarosa.core.model.instance.TreeReference)6 SelectMultiData (org.javarosa.core.model.data.SelectMultiData)5 ArrayList (java.util.ArrayList)4 SelectOneData (org.javarosa.core.model.data.SelectOneData)4 Selection (org.javarosa.core.model.data.helper.Selection)4 TreeElement (org.javarosa.core.model.instance.TreeElement)4 List (java.util.List)3 FormDef (org.javarosa.core.model.FormDef)3 StringData (org.javarosa.core.model.data.StringData)3 BigDecimal (java.math.BigDecimal)2 FormIndex (org.javarosa.core.model.FormIndex)2 SelectChoice (org.javarosa.core.model.SelectChoice)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 DecimalData (org.javarosa.core.model.data.DecimalData)2