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