Search in sources :

Example 1 with FormSyncSet

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));
    });
}
Also used : HashSet(java.util.HashSet) FormSyncSet(org.activityinfo.model.form.FormSyncSet) Set(java.util.Set) ArrayList(java.util.ArrayList) Observable(org.activityinfo.observable.Observable) FormMetadata(org.activityinfo.model.form.FormMetadata) FormTree(org.activityinfo.model.formTree.FormTree) ResourceId(org.activityinfo.model.resource.ResourceId) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 2 with FormSyncSet

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));
}
Also used : SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) org.activityinfo.store.spi(org.activityinfo.store.spi) BeforeClass(org.junit.BeforeClass) Cardinality(org.activityinfo.model.type.Cardinality) QuantityType(org.activityinfo.model.type.number.QuantityType) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) EnumType(org.activityinfo.model.type.enumerated.EnumType) Quantity(org.activityinfo.model.type.number.Quantity) Assert.assertThat(org.junit.Assert.assertThat) LocalDatastoreServiceTestConfig(com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig) ResourceId(org.activityinfo.model.resource.ResourceId) FormClass(org.activityinfo.model.form.FormClass) NullFormSupervisor(org.activityinfo.store.query.shared.NullFormSupervisor) Optional(com.google.common.base.Optional) After(org.junit.After) TextValue(org.activityinfo.model.type.primitive.TextValue) Matchers.hasSize(org.hamcrest.Matchers.hasSize) VoidWork(com.googlecode.objectify.VoidWork) LocalServiceTestHelper(com.google.appengine.tools.development.testing.LocalServiceTestHelper) EnumItem(org.activityinfo.model.type.enumerated.EnumItem) Before(org.junit.Before) ObjectifyService(com.googlecode.objectify.ObjectifyService) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Updater(org.activityinfo.store.query.server.Updater) FormSyncSet(org.activityinfo.model.form.FormSyncSet) List(java.util.List) FormField(org.activityinfo.model.form.FormField) TextType(org.activityinfo.model.type.primitive.TextType) LocaleProxy(net.lightoze.gwt.i18n.server.LocaleProxy) ColumnSet(org.activityinfo.model.query.ColumnSet) QueryModel(org.activityinfo.model.query.QueryModel) Closeable(com.googlecode.objectify.util.Closeable) Collections(java.util.Collections) ColumnSetBuilder(org.activityinfo.store.query.server.ColumnSetBuilder) ResourceId(org.activityinfo.model.resource.ResourceId) QuantityType(org.activityinfo.model.type.number.QuantityType) FormSyncSet(org.activityinfo.model.form.FormSyncSet) FormClass(org.activityinfo.model.form.FormClass) Test(org.junit.Test)

Aggregations

List (java.util.List)2 FormSyncSet (org.activityinfo.model.form.FormSyncSet)2 ResourceId (org.activityinfo.model.resource.ResourceId)2 LocalDatastoreServiceTestConfig (com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig)1 LocalServiceTestHelper (com.google.appengine.tools.development.testing.LocalServiceTestHelper)1 Optional (com.google.common.base.Optional)1 ObjectifyService (com.googlecode.objectify.ObjectifyService)1 VoidWork (com.googlecode.objectify.VoidWork)1 Closeable (com.googlecode.objectify.util.Closeable)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 LocaleProxy (net.lightoze.gwt.i18n.server.LocaleProxy)1 FormClass (org.activityinfo.model.form.FormClass)1 FormField (org.activityinfo.model.form.FormField)1 FormMetadata (org.activityinfo.model.form.FormMetadata)1 FormTree (org.activityinfo.model.formTree.FormTree)1 ColumnSet (org.activityinfo.model.query.ColumnSet)1 QueryModel (org.activityinfo.model.query.QueryModel)1