use of org.javarosa.core.services.storage.Persistable 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());
}
}
}
Aggregations