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();
}
}
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);
}
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");
}
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);
}
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");
}
Aggregations