Search in sources :

Example 21 with QuestionDef

use of org.javarosa.core.model.QuestionDef 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)

Example 22 with QuestionDef

use of org.javarosa.core.model.QuestionDef in project javarosa by opendatakit.

the class OutputInComputedConstraintTextTest method buildIndexes.

private void buildIndexes() {
    ctrl.jumpToIndex(FormIndex.createBeginningOfFormIndex());
    do {
        FormEntryCaption fep = model.getCaptionPrompt();
        IFormElement formElement = fep.getFormElement();
        if (formElement instanceof QuestionDef)
            formIndexesById.put(formElement.getTextID(), fep.getIndex());
    } while (ctrl.stepToNextEvent() != FormEntryController.EVENT_END_OF_FORM);
}
Also used : IFormElement(org.javarosa.core.model.IFormElement) QuestionDef(org.javarosa.core.model.QuestionDef)

Example 23 with QuestionDef

use of org.javarosa.core.model.QuestionDef in project javarosa by opendatakit.

the class XFormParserTest method passedThroughAttributesHaveExpectedValues.

/**
 * Attributes that started being used by clients without being added as fields to DataBinding or QuestionDef should
 * be passed through and made available in the bindAttributes or additionalAttributes list.
 */
@Test
public void passedThroughAttributesHaveExpectedValues() throws IOException {
    ParseResult parseResult = parse(r("whitelisted-attributes.xml"));
    TreeElement instanceIDElement = parseResult.formDef.getMainInstance().getRoot().getChildAt(0).getChildAt(0);
    // Bind attributes
    String requiredMsg = instanceIDElement.getBindAttribute("", "requiredMsg").getValue().getDisplayText();
    assertEquals(requiredMsg, "this is required");
    String saveIncomplete = instanceIDElement.getBindAttribute("", "saveIncomplete").getValue().getDisplayText();
    assertEquals(saveIncomplete, "false");
    // Attributes specific to input form controls
    QuestionDef question = FormDef.findQuestionByRef(instanceIDElement.getRef(), parseResult.formDef);
    String rows = question.getAdditionalAttribute("", "rows");
    assertEquals(rows, "2");
    String query = question.getAdditionalAttribute("", "query");
    assertEquals(query, "instance('fake')/root/item[fake2 = /data/meta/instanceID");
}
Also used : ParseResult(org.javarosa.xform.parse.FormParserHelper.ParseResult) QuestionDef(org.javarosa.core.model.QuestionDef) TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement) Test(org.junit.Test)

Example 24 with QuestionDef

use of org.javarosa.core.model.QuestionDef in project javarosa by opendatakit.

the class FormDefTest method testAnswerConstraint.

public void testAnswerConstraint() {
    IntegerData ans = new IntegerData(13);
    FormEntryController fec = fpi.getFormEntryController();
    fec.jumpToIndex(FormIndex.createBeginningOfFormIndex());
    do {
        QuestionDef q = fpi.getCurrentQuestion();
        if (q == null || q.getTextID() == null || q.getTextID().length() == 0)
            continue;
        if (q.getTextID().equals("constraint-test")) {
            int response = fec.answerQuestion(ans, true);
            if (response == FormEntryController.ANSWER_CONSTRAINT_VIOLATED) {
                fail("Answer Constraint test failed.");
            } else if (response == FormEntryController.ANSWER_OK) {
                break;
            } else {
                fail("Bad response from fec.answerQuestion()");
            }
        }
    } while (fec.stepToNextEvent() != FormEntryController.EVENT_END_OF_FORM);
}
Also used : FormEntryController(org.javarosa.form.api.FormEntryController) IntegerData(org.javarosa.core.model.data.IntegerData) QuestionDef(org.javarosa.core.model.QuestionDef)

Example 25 with QuestionDef

use of org.javarosa.core.model.QuestionDef in project javarosa by opendatakit.

the class QuestionDefTest method testAccessorsModifiers.

@Test
public void testAccessorsModifiers() {
    QuestionDef q = new QuestionDef();
    q.setID(45);
    if (q.getID() != 45) {
        fail("ID getter/setter broken");
    }
    testSerialize(q, "c");
    IDataReference ref = newRef();
    q.setBind(ref);
    if (q.getBind() != ref) {
        fail("Ref getter/setter broken");
    }
    testSerialize(q, "e");
    q.setControlType(Constants.CONTROL_SELECT_ONE);
    if (q.getControlType() != Constants.CONTROL_SELECT_ONE) {
        fail("Control type getter/setter broken");
    }
    testSerialize(q, "g");
    q.setAppearanceAttr("minimal");
    if (!"minimal".equals(q.getAppearanceAttr())) {
        fail("Appearance getter/setter broken");
    }
    testSerialize(q, "h");
}
Also used : IDataReference(org.javarosa.core.model.IDataReference) QuestionDef(org.javarosa.core.model.QuestionDef) Test(org.junit.Test)

Aggregations

QuestionDef (org.javarosa.core.model.QuestionDef)30 IFormElement (org.javarosa.core.model.IFormElement)8 SelectChoice (org.javarosa.core.model.SelectChoice)7 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 GroupDef (org.javarosa.core.model.GroupDef)5 IDataReference (org.javarosa.core.model.IDataReference)4 Selection (org.javarosa.core.model.data.helper.Selection)4 TreeElement (org.javarosa.core.model.instance.TreeElement)4 FormEntryPrompt (org.javarosa.form.api.FormEntryPrompt)4 TreeReference (org.javarosa.core.model.instance.TreeReference)3 Localizer (org.javarosa.core.services.locale.Localizer)3 List (java.util.List)2 FormDef (org.javarosa.core.model.FormDef)2 ItemsetBinding (org.javarosa.core.model.ItemsetBinding)2 Constraint (org.javarosa.core.model.condition.Constraint)2 EvaluationContext (org.javarosa.core.model.condition.EvaluationContext)2 SelectMultiData (org.javarosa.core.model.data.SelectMultiData)2 SelectOneData (org.javarosa.core.model.data.SelectOneData)2 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)2