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