use of org.javarosa.core.model.instance.TreeElement in project javarosa by opendatakit.
the class CompactInstanceWrapper method readExternal.
/**
* deserialize a compact instance. note the retrieval of the template data instance
*/
public void readExternal(DataInputStream in, PrototypeFactory pf) throws IOException, DeserializationException {
int formID = ExtUtil.readInt(in);
instance = getTemplateInstance(formID).clone();
instance.setID(ExtUtil.readInt(in));
instance.setDateSaved((Date) ExtUtil.read(in, new ExtWrapNullable(Date.class)));
// formID, name, schema, versions, and namespaces are all invariants of the template instance
TreeElement root = instance.getRoot();
readTreeElement(root, in, pf);
}
use of org.javarosa.core.model.instance.TreeElement in project javarosa by opendatakit.
the class CompactInstanceWrapper method readTreeElement.
/**
* recursively read in a node of the instance, by filling out the template instance
* @param e
* @param ref
* @param in
* @param pf
* @throws IOException
* @throws DeserializationException
*/
private void readTreeElement(TreeElement e, DataInputStream in, PrototypeFactory pf) throws IOException, DeserializationException {
TreeElement templ = instance.getTemplatePath(e.getRef());
boolean isGroup = !templ.isLeaf();
if (isGroup) {
List<String> childTypes = new ArrayList<String>(templ.getNumChildren());
for (int i = 0; i < templ.getNumChildren(); i++) {
String childName = templ.getChildAt(i).getName();
if (!childTypes.contains(childName)) {
childTypes.add(childName);
}
}
for (int i = 0; i < childTypes.size(); i++) {
String childName = childTypes.get(i);
TreeReference childTemplRef = e.getRef().extendRef(childName, 0);
TreeElement childTempl = instance.getTemplatePath(childTemplRef);
boolean repeatable = childTempl.isRepeatable();
int n = ExtUtil.readInt(in);
boolean relevant = (n > 0);
if (!repeatable && n > 1) {
throw new DeserializationException("Detected repeated instances of a non-repeatable node");
}
if (repeatable) {
int mult = e.getChildMultiplicity(childName);
for (int j = mult - 1; j >= 0; j--) {
e.removeChild(childName, j);
}
for (int j = 0; j < n; j++) {
TreeReference dstRef = e.getRef().extendRef(childName, j);
try {
instance.copyNode(childTempl, dstRef);
} catch (InvalidReferenceException ire) {
// If there is an invalid reference, this is a malformed instance,
// so we'll throw a Deserialization exception.
TreeReference r = ire.getInvalidReference();
if (r == null) {
throw new DeserializationException("Null Reference while attempting to deserialize! " + ire.getMessage());
} else {
throw new DeserializationException("Invalid Reference while attemtping to deserialize! Reference: " + r.toString(true) + " | " + ire.getMessage());
}
}
TreeElement child = e.getChild(childName, j);
child.setRelevant(true);
readTreeElement(child, in, pf);
}
} else {
TreeElement child = e.getChild(childName, 0);
child.setRelevant(relevant);
if (relevant) {
readTreeElement(child, in, pf);
}
}
}
} else {
e.setValue((IAnswerData) ExtUtil.read(in, new ExtWrapAnswerData(e.getDataType())));
}
}
use of org.javarosa.core.model.instance.TreeElement in project javarosa by opendatakit.
the class TreeElementChildrenList method addAll.
/**
* Adds all of the provided children
*/
public void addAll(Iterable<TreeElement> childIterable) {
for (TreeElement child : childIterable) {
checkAndSetSameNameAndNormalMult(child.getName(), child.getMultiplicity());
children.add(child);
}
}
use of org.javarosa.core.model.instance.TreeElement in project javarosa by opendatakit.
the class RestoreUtils method importRMS.
public static void importRMS(FormInstance dm, IStorageUtility storage, Class type, String path) {
if (!Externalizable.class.isAssignableFrom(type) || !Restorable.class.isAssignableFrom(type)) {
return;
}
boolean idMatters = Persistable.class.isAssignableFrom(type);
String childName = ((Restorable) PrototypeFactory.getInstance(type)).getRestorableType();
TreeElement e = dm.resolveReference(absRef(path, dm));
List<TreeElement> children = e.getChildrenWithName(childName);
for (int i = 0; i < children.size(); i++) {
FormInstance child = subDataModel(children.get(i));
Restorable inst = (Restorable) PrototypeFactory.getInstance(type);
// restore record id first so 'importData' has access to it
int recID = -1;
if (idMatters) {
recID = ((Integer) getValue(RECORD_ID_TAG, child)).intValue();
((Persistable) inst).setID(recID);
}
inst.importData(child);
try {
if (idMatters) {
storage.write((Persistable) inst);
} else {
storage.add((Externalizable) inst);
}
} catch (Exception ex) {
throw new RuntimeException("Error importing RMS during restore! [" + type.getName() + ":" + recID + "]; " + ex.getMessage());
}
}
}
use of org.javarosa.core.model.instance.TreeElement in project javarosa by opendatakit.
the class RestoreUtils method applyDataType.
public static void applyDataType(FormInstance dm, String path, TreeReference parent, int dataType) {
TreeReference ref = childRef(path, parent);
List<TreeReference> v = new EvaluationContext(dm).expandReference(ref);
for (int i = 0; i < v.size(); i++) {
TreeElement e = dm.resolveReference(v.get(i));
e.setDataType(dataType);
}
}
Aggregations