use of org.activityinfo.model.form.FormSyncSet in project activityinfo by bedatadriven.
the class Snapshot method compute.
public static Observable<Snapshot> compute(Observable<Set<ResourceId>> offlineForms, HttpStore httpStore) {
// We start with the "offlineForm" set which contains the set
// of forms the user has explicitly asked to cache.
// In order to find the related forms, we need the complete form trees of each of the
// selected forms.
Observable<List<FormTree>> formTrees = flatMap(offlineForms, httpStore::getFormTree);
// Together, all the related forms constitute the set of forms we need for
// a complete offline snapshot
Observable<Set<ResourceId>> completeSet = formTrees.transform(trees -> {
Set<ResourceId> set = new HashSet<>();
for (FormTree tree : trees) {
for (FormMetadata form : tree.getForms()) {
if (!isBuiltinForm(form.getId())) {
set.add(form.getId());
}
}
}
return set;
});
// Now need fetch the latest version numbers of each of these forms
Observable<List<FormMetadata>> metadata = flatMap(completeSet, httpStore::getFormMetadata);
// And finally fetch any difference between our current snapshot and the latest version of the new snapshot
return metadata.join(forms -> {
List<Observable<FormSyncSet>> recordSets = new ArrayList<>();
for (FormMetadata form : forms) {
recordSets.add(httpStore.getVersionRange(form.getId(), 0, form.getVersion()));
}
return Observable.flatten(recordSets).transform(x -> new Snapshot(forms, x));
});
}
use of org.activityinfo.model.form.FormSyncSet in project activityinfo by bedatadriven.
the class HrdCatalogTest method versionRangeTest.
@Test
public void versionRangeTest() {
ResourceId collectionId = ResourceId.generateId();
ResourceId villageField = ResourceId.valueOf("FV");
ResourceId countField = ResourceId.valueOf("FC");
FormClass formClass = new FormClass(collectionId);
formClass.setParentFormId(ResourceId.valueOf("foo"));
formClass.setLabel("NFI Distributions");
formClass.addField(villageField).setLabel("Village name").setCode("VILLAGE").setType(TextType.SIMPLE);
formClass.addField(countField).setLabel("Number of Beneficiaries").setCode("BENE").setType(new QuantityType("Beneficiaries"));
HrdStorageProvider catalog = new HrdStorageProvider();
catalog.create(formClass);
VersionedFormStorage formStorage = (VersionedFormStorage) catalog.getForm(collectionId).get();
// Initially, with no records added, the form should be at version 1
// and the version range (0, 1] should be empty.
assertThat(formStorage.cacheVersion(), equalTo(1L));
FormSyncSet updatedRecords = formStorage.getVersionRange(0, 1L, resourceId -> true);
assertTrue(updatedRecords.isEmpty());
// Add a new record
TypedRecordUpdate firstUpdate = new TypedRecordUpdate();
firstUpdate.setUserId(1);
firstUpdate.setRecordId(ResourceId.generateId());
firstUpdate.set(villageField, TextValue.valueOf("Goma"));
catalog.getForm(collectionId).get().add(firstUpdate);
// Verify that the version is incremented and the version range
// (0, 2] includes our new record
assertThat(formStorage.cacheVersion(), equalTo(2L));
FormSyncSet updated = formStorage.getVersionRange(0, 2L, resourceId -> true);
assertThat(updated.getUpdatedRecordCount(), equalTo(1));
// Update the first record and add a new one
TypedRecordUpdate secondUpdate = new TypedRecordUpdate();
secondUpdate.setUserId(1);
secondUpdate.setRecordId(ResourceId.generateId());
secondUpdate.set(villageField, TextValue.valueOf("Rutshuru"));
catalog.getForm(collectionId).get().add(firstUpdate);
// Verify that the version is incremented and the version range
// (1, 2] includes our new record
assertThat(formStorage.cacheVersion(), equalTo(3L));
updated = formStorage.getVersionRange(2L, 3L, resourceId -> true);
assertThat(updated.getUpdatedRecordCount(), equalTo(1));
}
Aggregations