Search in sources :

Example 41 with TreeReference

use of org.javarosa.core.model.instance.TreeReference in project collect by opendatakit.

the class FormLoaderTask method importData.

static void importData(File instanceFile, FormEntryController fec) {
    // convert files into a byte array
    byte[] fileBytes = FileUtils.getFileAsBytes(instanceFile);
    // get the root of the saved and template instances
    TreeElement savedRoot = XFormParser.restoreDataModel(fileBytes, null).getRoot();
    TreeElement templateRoot = fec.getModel().getForm().getInstance().getRoot().deepCopy(true);
    // weak check for matching forms
    if (!savedRoot.getName().equals(templateRoot.getName()) || savedRoot.getMult() != 0) {
        Timber.e("Saved form instance does not match template form definition");
        return;
    }
    // populate the data model
    TreeReference tr = TreeReference.rootRef();
    tr.add(templateRoot.getName(), TreeReference.INDEX_UNBOUND);
    // Here we set the Collect's implementation of the IAnswerResolver.
    // We set it back to the default after select choices have been populated.
    XFormParser.setAnswerResolver(new ExternalAnswerResolver());
    templateRoot.populate(savedRoot, fec.getModel().getForm());
    XFormParser.setAnswerResolver(new DefaultAnswerResolver());
    // populated model to current form
    fec.getModel().getForm().getInstance().setRoot(templateRoot);
    // http://bitbucket.org/javarosa/main/issue/5/itext-n-appearing-in-restored-instances
    if (fec.getModel().getLanguages() != null) {
        fec.getModel().getForm().localeChanged(fec.getModel().getLanguage(), fec.getModel().getForm().getLocalizer());
    }
    Timber.i("Done importing data");
}
Also used : TreeReference(org.javarosa.core.model.instance.TreeReference) ExternalAnswerResolver(org.odk.collect.android.external.ExternalAnswerResolver) DefaultAnswerResolver(org.javarosa.core.model.instance.utils.DefaultAnswerResolver) TreeElement(org.javarosa.core.model.instance.TreeElement)

Example 42 with TreeReference

use of org.javarosa.core.model.instance.TreeReference in project collect by opendatakit.

the class ODKView method setDataForFields.

public void setDataForFields(Bundle bundle) throws JavaRosaException {
    if (bundle == null) {
        return;
    }
    FormController formController = Collect.getInstance().getFormController();
    Set<String> keys = bundle.keySet();
    for (String key : keys) {
        for (QuestionWidget questionWidget : widgets) {
            FormEntryPrompt prompt = questionWidget.getFormEntryPrompt();
            TreeReference treeReference = (TreeReference) prompt.getFormElement().getBind().getReference();
            if (treeReference.getNameLast().equals(key)) {
                switch(prompt.getDataType()) {
                    case Constants.DATATYPE_TEXT:
                        formController.saveAnswer(prompt.getIndex(), ExternalAppsUtils.asStringData(bundle.get(key)));
                        break;
                    case Constants.DATATYPE_INTEGER:
                        formController.saveAnswer(prompt.getIndex(), ExternalAppsUtils.asIntegerData(bundle.get(key)));
                        break;
                    case Constants.DATATYPE_DECIMAL:
                        formController.saveAnswer(prompt.getIndex(), ExternalAppsUtils.asDecimalData(bundle.get(key)));
                        break;
                    default:
                        throw new RuntimeException(getContext().getString(R.string.ext_assign_value_error, treeReference.toString(false)));
                }
                break;
            }
        }
    }
}
Also used : FormController(org.odk.collect.android.logic.FormController) FormEntryPrompt(org.javarosa.form.api.FormEntryPrompt) TreeReference(org.javarosa.core.model.instance.TreeReference) QuestionWidget(org.odk.collect.android.widgets.QuestionWidget)

Example 43 with TreeReference

use of org.javarosa.core.model.instance.TreeReference in project javarosa by opendatakit.

the class XFormParser method getFormElementRef.

private TreeReference getFormElementRef(IFormElement fe) {
    if (fe instanceof FormDef) {
        TreeReference ref = TreeReference.rootRef();
        ref.add(mainInstanceNode.getName(), 0);
        return ref;
    } else {
        return (TreeReference) fe.getBind().getReference();
    }
}
Also used : FormDef(org.javarosa.core.model.FormDef) TreeReference(org.javarosa.core.model.instance.TreeReference)

Example 44 with TreeReference

use of org.javarosa.core.model.instance.TreeReference in project javarosa by opendatakit.

the class XFormParser method parseSetValueAction.

private void parseSetValueAction(FormDef form, Element e) {
    String ref = e.getAttributeValue(null, REF_ATTR);
    String bind = e.getAttributeValue(null, BIND_ATTR);
    String event = e.getAttributeValue(null, "event");
    IDataReference dataRef = null;
    boolean refFromBind = false;
    // TODO: There is a _lot_ of duplication of this code, fix that!
    if (bind != null) {
        DataBinding binding = bindingsByID.get(bind);
        if (binding == null) {
            throw new XFormParseException("XForm Parse: invalid binding ID in submit'" + bind + "'", e);
        }
        dataRef = binding.getReference();
        refFromBind = true;
    } else if (ref != null) {
        dataRef = new XPathReference(ref);
    } else {
        throw new XFormParseException("setvalue action with no target!", e);
    }
    if (dataRef != null) {
        if (!refFromBind) {
            dataRef = FormDef.getAbsRef(dataRef, TreeReference.rootRef());
        }
    }
    String valueRef = e.getAttributeValue(null, "value");
    Action action;
    TreeReference treeref = FormInstance.unpackReference(dataRef);
    actionTargets.add(treeref);
    if (valueRef == null) {
        if (e.getChildCount() == 0 || !e.isText(0)) {
            throw new XFormParseException("No 'value' attribute and no inner value set in <setvalue> associated with: " + treeref, e);
        }
        // Set expression
        action = new SetValueAction(treeref, e.getText(0));
    } else {
        try {
            action = new SetValueAction(treeref, XPathParseTool.parseXPath(valueRef));
        } catch (XPathSyntaxException e1) {
            Std.printStack(e1);
            throw new XFormParseException("Invalid XPath in value set action declaration: '" + valueRef + "'", e);
        }
    }
    form.registerEventListener(event, action);
}
Also used : Action(org.javarosa.core.model.Action) SetValueAction(org.javarosa.core.model.actions.SetValueAction) XPathSyntaxException(org.javarosa.xpath.parser.XPathSyntaxException) IDataReference(org.javarosa.core.model.IDataReference) TreeReference(org.javarosa.core.model.instance.TreeReference) DataBinding(org.javarosa.core.model.DataBinding) SetValueAction(org.javarosa.core.model.actions.SetValueAction) XPathReference(org.javarosa.model.xform.XPathReference)

Example 45 with TreeReference

use of org.javarosa.core.model.instance.TreeReference in project javarosa by opendatakit.

the class XFormParser method loadXmlInstance.

/**
 * Loads a compatible xml instance into FormDef f
 *
 * call before f.initialize()!
 */
private static void loadXmlInstance(FormDef f, Document xmlInst) {
    TreeElement savedRoot = XFormParser.restoreDataModel(xmlInst, null).getRoot();
    TreeElement templateRoot = f.getMainInstance().getRoot().deepCopy(true);
    // TODO: should check that namespaces match?
    if (!savedRoot.getName().equals(templateRoot.getName()) || savedRoot.getMult() != 0) {
        throw new RuntimeException("Saved form instance does not match template form definition");
    }
    // populate the data model
    TreeReference tr = TreeReference.rootRef();
    tr.add(templateRoot.getName(), TreeReference.INDEX_UNBOUND);
    templateRoot.populate(savedRoot, f);
    // populated model to current form
    f.getMainInstance().setRoot(templateRoot);
// if the new instance is inserted into the formdef before f.initialize() is called, this
// locale refresh is unnecessary
// Localizer loc = f.getLocalizer();
// if (loc != null) {
// f.localeChanged(loc.getLocale(), loc);
// }
}
Also used : TreeReference(org.javarosa.core.model.instance.TreeReference) TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement)

Aggregations

TreeReference (org.javarosa.core.model.instance.TreeReference)85 ArrayList (java.util.ArrayList)30 TreeElement (org.javarosa.core.model.instance.TreeElement)29 EvaluationContext (org.javarosa.core.model.condition.EvaluationContext)16 Constraint (org.javarosa.core.model.condition.Constraint)12 HashSet (java.util.HashSet)11 Test (org.junit.Test)9 FormInstance (org.javarosa.core.model.instance.FormInstance)8 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)7 IFormElement (org.javarosa.core.model.IFormElement)6 Condition (org.javarosa.core.model.condition.Condition)6 IAnswerData (org.javarosa.core.model.data.IAnswerData)6 InstanceInitializationFactory (org.javarosa.core.model.instance.InstanceInitializationFactory)6 GroupDef (org.javarosa.core.model.GroupDef)5 XPathReference (org.javarosa.model.xform.XPathReference)5 DataBinding (org.javarosa.core.model.DataBinding)4 IDataReference (org.javarosa.core.model.IDataReference)4 DataInstance (org.javarosa.core.model.instance.DataInstance)4 EvaluationResult (org.javarosa.debug.EvaluationResult)4 XPathException (org.javarosa.xpath.XPathException)4