Search in sources :

Example 56 with QueryModel

use of org.activityinfo.model.query.QueryModel in project activityinfo by bedatadriven.

the class LookupKeySetTest method overlappingHierarchiesKeyQuery.

@Test
public void overlappingHierarchiesKeyQuery() {
    TestingStorageProvider catalog = setup.getCatalog();
    LocaliteForm localiteForm = catalog.getLocaliteForm();
    FormTree formTree = setup.getFormTree(localiteForm.getFormId());
    LookupKeySet lookupKeySet = new LookupKeySet(formTree, localiteForm.getAdminField());
    Map<LookupKey, FormulaNode> formulas = lookupKeySet.getKeyFormulas(localiteForm.getAdminField().getId());
    QueryModel queryModel = new QueryModel(localiteForm.getFormId());
    queryModel.selectResourceId().as("id");
    queryModel.selectExpr(formulas.get(lookupKeySet.getKey(0))).as("province");
    queryModel.selectExpr(formulas.get(lookupKeySet.getKey(1))).as("territory");
    queryModel.selectExpr(formulas.get(lookupKeySet.getKey(2))).as("zs");
    Connection<ColumnSet> table = setup.connect(setup.getFormStore().query(queryModel));
    ColumnSet columnSet = table.assertLoaded();
    ColumnView id = columnSet.getColumnView("id");
    ColumnView province = columnSet.getColumnView("province");
    ColumnView territory = columnSet.getColumnView("territory");
    ColumnView zs = columnSet.getColumnView("zs");
    // First row references a Zone de Sante
    assertThat(id.getString(0), equalTo("c0"));
    assertThat(province.getString(0), equalTo("Province 14"));
    assertThat(territory.getString(0), nullValue());
    assertThat(zs.getString(0), equalTo("Zone de Sante 56"));
    // Second row references only a Province
    assertThat(id.getString(1), equalTo("c1"));
    assertThat(province.getString(1), equalTo("Province 8"));
    assertThat(territory.getString(1), nullValue());
    assertThat(zs.getString(1), nullValue());
    // Fifth row references a territory
    assertThat(id.getString(4), equalTo("c4"));
    assertThat(province.getString(4), equalTo("Province 15"));
    assertThat(territory.getString(4), equalTo("Territory 42"));
    assertThat(zs.getString(4), nullValue());
}
Also used : LookupKeySet(org.activityinfo.model.formTree.LookupKeySet) FormulaNode(org.activityinfo.model.formula.FormulaNode) FormTree(org.activityinfo.model.formTree.FormTree) LookupKey(org.activityinfo.model.formTree.LookupKey) ColumnView(org.activityinfo.model.query.ColumnView) ColumnSet(org.activityinfo.model.query.ColumnSet) QueryModel(org.activityinfo.model.query.QueryModel) Test(org.junit.Test)

Example 57 with QueryModel

use of org.activityinfo.model.query.QueryModel in project activityinfo by bedatadriven.

the class FormStoreTest method serialNumberOffline.

@Test
public void serialNumberOffline() {
    TestSetup setup = new TestSetup();
    IntakeForm intakeForm = setup.getCatalog().getIntakeForm();
    // Synchronize the intake form
    setup.setConnected(true);
    setup.getFormStore().setFormOffline(intakeForm.getFormId(), true);
    setup.runScheduled();
    // Go offline
    setup.setConnected(false);
    // Create a new intake record
    FormInstance newRecord = intakeForm.getGenerator().get();
    newRecord.set(intakeForm.getProtectionCodeFieldId(), (FieldValue) null);
    Promise<Void> update = setup.getFormStore().updateRecords(new RecordTransactionBuilder().create(newRecord).build());
    assertThat(update.getState(), equalTo(Promise.State.FULFILLED));
    // Verify that we can read the new record offline
    QueryModel queryModel = new QueryModel(intakeForm.getFormId());
    queryModel.selectResourceId().as("id");
    queryModel.selectField(intakeForm.getProtectionCodeFieldId()).as("serial");
    queryModel.setFilter(Formulas.equals(new SymbolNode("_id"), new ConstantNode(newRecord.getId().asString())));
    Connection<ColumnSet> view = setup.connect(setup.getFormStore().query(queryModel));
    assertThat(view.assertLoaded().getNumRows(), equalTo(1));
    assertThat(view.assertLoaded().getColumnView("serial").getString(0), nullValue());
    // Now go online...
    setup.setConnected(true);
    setup.getOfflineStore().syncChanges();
    ;
    setup.runScheduled();
    // Check that the serial number has been updated with the value from the server
    assertThat(view.assertLoaded().getColumnView("serial").getString(0), not(nullValue()));
}
Also used : RecordTransactionBuilder(org.activityinfo.model.resource.RecordTransactionBuilder) SymbolNode(org.activityinfo.model.formula.SymbolNode) ConstantNode(org.activityinfo.model.formula.ConstantNode) ColumnSet(org.activityinfo.model.query.ColumnSet) FormInstance(org.activityinfo.model.form.FormInstance) QueryModel(org.activityinfo.model.query.QueryModel) Test(org.junit.Test)

Example 58 with QueryModel

use of org.activityinfo.model.query.QueryModel in project activityinfo by bedatadriven.

the class FormStoreTest method offlineColumnQuery.

@Test
public void offlineColumnQuery() {
    TestSetup setup = new TestSetup();
    Survey survey = setup.getSurveyForm();
    setup.getFormStore().setFormOffline(survey.getFormId(), true);
    setup.runScheduled();
    setup.setConnected(false);
    Connection<SnapshotStatus> snapshot = setup.connect(setup.getOfflineStore().getCurrentSnapshot());
    assertTrue(snapshot.assertLoaded().isFormCached(survey.getFormId()));
    QueryModel queryModel = new QueryModel(survey.getFormId());
    queryModel.selectResourceId().as("id");
    queryModel.selectField(survey.getNameFieldId()).as("name");
    queryModel.selectField(survey.getAgeFieldId()).as("age");
    ColumnSet columnSet = setup.connect(setup.getFormStore().query(queryModel)).assertLoaded();
    assertThat(columnSet.getNumRows(), equalTo(survey.getRowCount()));
    assertThat(columnSet.getColumnView("name").get(0), equalTo("Melanie"));
    assertThat(columnSet.getColumnView("name").get(1), equalTo("Joe"));
    assertThat(columnSet.getColumnView("name").get(2), equalTo("Matilda"));
}
Also used : ColumnSet(org.activityinfo.model.query.ColumnSet) QueryModel(org.activityinfo.model.query.QueryModel) Test(org.junit.Test)

Example 59 with QueryModel

use of org.activityinfo.model.query.QueryModel in project activityinfo by bedatadriven.

the class FormStoreTest method newRecordHitsQuery.

@Test
public void newRecordHitsQuery() {
    TestingStorageProvider catalog = new TestingStorageProvider();
    Survey survey = catalog.getSurvey();
    AsyncClientStub client = new AsyncClientStub(catalog);
    HttpStore httpStore = new HttpStore(client, scheduler);
    OfflineStore offlineStore = new OfflineStore(httpStore, new IDBFactoryStub());
    FormStoreImpl formStore = new FormStoreImpl(httpStore, offlineStore, scheduler);
    // Open a query on a set of records
    QueryModel queryModel = new QueryModel(survey.getFormId());
    queryModel.selectResourceId().as("id");
    Connection<ColumnSet> tableView = connect(formStore.query(queryModel));
    tableView.assertLoaded();
    // Add an new record to Survey
    tableView.resetChangeCounter();
    formStore.updateRecords(new RecordTransactionBuilder().add(catalog.addNew(survey.getFormId())).build());
    // Verify that the table view has been updated
    tableView.assertLoaded();
    tableView.assertChanged();
    assertThat(tableView.assertLoaded().getNumRows(), equalTo(survey.getRowCount() + 1));
}
Also used : IDBFactoryStub(org.activityinfo.indexedb.IDBFactoryStub) RecordTransactionBuilder(org.activityinfo.model.resource.RecordTransactionBuilder) HttpStore(org.activityinfo.ui.client.store.http.HttpStore) ColumnSet(org.activityinfo.model.query.ColumnSet) QueryModel(org.activityinfo.model.query.QueryModel) Test(org.junit.Test)

Aggregations

QueryModel (org.activityinfo.model.query.QueryModel)59 ColumnSet (org.activityinfo.model.query.ColumnSet)40 Test (org.junit.Test)30 ColumnView (org.activityinfo.model.query.ColumnView)21 ColumnSetBuilder (org.activityinfo.store.query.server.ColumnSetBuilder)13 NullFormSupervisor (org.activityinfo.store.query.shared.NullFormSupervisor)11 FormTree (org.activityinfo.model.formTree.FormTree)9 ResourceId (org.activityinfo.model.resource.ResourceId)9 FormClass (org.activityinfo.model.form.FormClass)7 Nullable (javax.annotation.Nullable)6 FormField (org.activityinfo.model.form.FormField)6 ColumnModel (org.activityinfo.model.query.ColumnModel)6 NullFormScanCache (org.activityinfo.store.query.shared.NullFormScanCache)6 QuantityType (org.activityinfo.model.type.number.QuantityType)5 DimensionCategory (org.activityinfo.legacy.shared.reports.content.DimensionCategory)4 RowBasedJsonWriter (org.activityinfo.store.query.output.RowBasedJsonWriter)4 Charsets (com.google.common.base.Charsets)3 Operation (io.swagger.v3.oas.annotations.Operation)3 FieldPath (org.activityinfo.model.formTree.FieldPath)3 FormTreeBuilder (org.activityinfo.model.formTree.FormTreeBuilder)3