Search in sources :

Example 1 with ExternalAnswerResolver

use of org.odk.collect.android.external.ExternalAnswerResolver 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)

Aggregations

TreeElement (org.javarosa.core.model.instance.TreeElement)1 TreeReference (org.javarosa.core.model.instance.TreeReference)1 DefaultAnswerResolver (org.javarosa.core.model.instance.utils.DefaultAnswerResolver)1 ExternalAnswerResolver (org.odk.collect.android.external.ExternalAnswerResolver)1