Search in sources :

Example 26 with IAnswerData

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());
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : IAnswerData(org.javarosa.core.model.data.IAnswerData) IFormElement(org.javarosa.core.model.IFormElement) TreeReference(org.javarosa.core.model.instance.TreeReference) GroupDef(org.javarosa.core.model.GroupDef) InvalidReferenceException(org.javarosa.core.model.instance.InvalidReferenceException) TreeElement(org.javarosa.core.model.instance.TreeElement)

Example 27 with IAnswerData

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();
    }
}
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 28 with IAnswerData

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?
}
Also used : IAnswerData(org.javarosa.core.model.data.IAnswerData) XFormAnswerDataSerializer(org.javarosa.xform.util.XFormAnswerDataSerializer)

Example 29 with IAnswerData

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);
}
Also used : IAnswerData(org.javarosa.core.model.data.IAnswerData) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement) TreeReference(org.javarosa.core.model.instance.TreeReference) EvaluationContext(org.javarosa.core.model.condition.EvaluationContext)

Example 30 with IAnswerData

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);
}
Also used : IAnswerData(org.javarosa.core.model.data.IAnswerData) InstanceInitializationFactory(org.javarosa.core.model.instance.InstanceInitializationFactory) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FormDef(org.javarosa.core.model.FormDef) File(java.io.File) FileInputStream(java.io.FileInputStream)

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