use of org.javarosa.core.model.data.IAnswerData in project javarosa by opendatakit.
the class FormEntryModel method createModelIfNecessary.
/**
* For the current index: Checks whether the index represents a node which
* should exist given a non-interactive repeat, along with a count for that
* repeat which is beneath the dynamic level specified.
*
* If this index does represent such a node, the new model for the repeat is
* created behind the scenes and the index for the initial question is
* returned.
*
* Note: This method will not prevent the addition of new repeat elements in
* the interface, it will merely use the xforms repeat hint to create new
* nodes that are assumed to exist
*
* @param index The index to be evaluated as to whether the underlying model is
* hinted to exist
*/
private void createModelIfNecessary(FormIndex index) {
if (index.isInForm()) {
IFormElement e = getForm().getChild(index);
if (e instanceof GroupDef) {
GroupDef g = (GroupDef) e;
if (g.getRepeat() && g.getCountReference() != null) {
// Lu Gram: repeat count XPath needs to be contextualized for nested repeat groups
TreeReference countRef = FormInstance.unpackReference(g.getCountReference());
TreeReference contextualized = countRef.contextualize(index.getReference());
IAnswerData count = getForm().getMainInstance().resolveReference(contextualized).getValue();
if (count != null) {
long fullcount = ((Integer) count.getValue()).intValue();
TreeReference ref = getForm().getChildInstanceRef(index);
TreeElement element = getForm().getMainInstance().resolveReference(ref);
if (element == null) {
if (index.getTerminal().getInstanceIndex() < fullcount) {
try {
getForm().createNewRepeat(index);
} catch (InvalidReferenceException ire) {
Std.printStack(ire);
throw new RuntimeException("Invalid Reference while creting new repeat!" + ire.getMessage());
}
}
}
}
}
}
}
}
use of org.javarosa.core.model.data.IAnswerData 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.data.IAnswerData in project javarosa by opendatakit.
the class FormDef method setValue.
public void setValue(IAnswerData data, TreeReference ref, TreeElement node, boolean midSurvey) {
IAnswerData oldValue = node.getValue();
IAnswerDataSerializer answerDataSerializer = new XFormAnswerDataSerializer();
if (midSurvey && dagImpl.shouldTrustPreviouslyCommittedAnswer() && objectEquals(answerDataSerializer.serializeAnswerData(oldValue), answerDataSerializer.serializeAnswerData(data))) {
return;
}
setAnswer(data, node);
Collection<QuickTriggerable> qts = triggerTriggerables(ref, midSurvey);
dagImpl.publishSummary("New value", ref, qts);
// TODO: pre-populate fix-count repeats here?
}
use of org.javarosa.core.model.data.IAnswerData in project javarosa by opendatakit.
the class SetValueAction method processAction.
public void processAction(FormDef model, TreeReference contextRef) {
// Qualify the reference if necessary
TreeReference qualifiedReference = contextRef == null ? target : target.contextualize(contextRef);
// insert events should only trigger on the right nodes
if (contextRef != null) {
// strategy
if (!contextRef.isParentOf(qualifiedReference, false)) {
return;
}
}
// TODO: either the target or the value's node might not exist here, catch and throw
// reasonably
EvaluationContext context = new EvaluationContext(model.getEvaluationContext(), qualifiedReference);
Object result;
if (explicitValue != null) {
result = explicitValue;
} else {
result = XPathFuncExpr.unpack(value.eval(model.getMainInstance(), context));
}
AbstractTreeElement node = context.resolveReference(qualifiedReference);
if (node == null) {
throw new NullPointerException("Target of TreeReference " + qualifiedReference.toString(true) + " could not be resolved!");
}
int dataType = node.getDataType();
IAnswerData val = Recalculate.wrapData(result, dataType);
model.setValue(val == null ? null : AnswerDataFactory.templateByDataType(dataType).cast(val.uncast()), qualifiedReference, true);
}
use of org.javarosa.core.model.data.IAnswerData in project javarosa by opendatakit.
the class GeoShapeAreaTest method testGeoShapeSupportForEnclosedArea.
public void testGeoShapeSupportForEnclosedArea() throws Exception {
// Read the form definition
String FORM_NAME = (new File(PathConst.getTestResourcePath(), "area.xml")).getAbsolutePath();
InputStream is = null;
FormDef formDef = null;
is = new FileInputStream(new File(FORM_NAME));
formDef = XFormUtils.getFormFromInputStream(is);
// trigger all calculations
formDef.initialize(true, new InstanceInitializationFactory());
// get the calculated area
IAnswerData areaResult = formDef.getMainInstance().getRoot().getChildAt(1).getValue();
assertTrue((int) Math.rint((Double) areaResult.getValue()) == 151452);
}
Aggregations