Search in sources :

Example 11 with ColumnSet

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

the class ResourceLocatorAdaptorTest method deleteLocation.

@Test
public void deleteLocation() {
    ResourceId instanceToDelete = CuidAdapter.locationInstanceId(1);
    assertResolves(locator.remove(CuidAdapter.locationFormClass(1), instanceToDelete));
    QueryModel queryModel = new QueryModel(CuidAdapter.locationFormClass(1));
    queryModel.selectResourceId().as("id");
    ColumnSet columnSet = assertResolves(locator.queryTable(queryModel));
    ColumnView idColumn = columnSet.getColumnView("id");
    for (int i = 0; i < idColumn.numRows(); i++) {
        if (idColumn.getString(i).equals(instanceToDelete.asString())) {
            throw new AssertionError();
        }
    }
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) ColumnView(org.activityinfo.model.query.ColumnView) ColumnSet(org.activityinfo.model.query.ColumnSet) QueryModel(org.activityinfo.model.query.QueryModel) GeoPoint(org.activityinfo.model.type.geo.GeoPoint) Test(org.junit.Test)

Example 12 with ColumnSet

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

the class HrdCatalogTest method simpleFormTest.

@Test
public void simpleFormTest() {
    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);
    Optional<FormStorage> storage = catalog.getForm(collectionId);
    assertTrue(storage.isPresent());
    TypedRecordUpdate village1 = new TypedRecordUpdate();
    village1.setUserId(userId);
    village1.setRecordId(ResourceId.generateSubmissionId(formClass));
    village1.set(villageField, TextValue.valueOf("Rutshuru"));
    village1.set(countField, new Quantity(1000));
    TypedRecordUpdate village2 = new TypedRecordUpdate();
    village2.setUserId(userId);
    village2.setRecordId(ResourceId.generateSubmissionId(formClass));
    village2.set(villageField, TextValue.valueOf("Beni"));
    village2.set(countField, new Quantity(230));
    storage.get().add(village1);
    storage.get().add(village2);
    QueryModel queryModel = new QueryModel(collectionId);
    queryModel.selectResourceId().as("id");
    queryModel.selectField("VILLAGE").as("village");
    queryModel.selectField("BENE").as("family_count");
    queryModel.selectExpr("BENE*5").as("individual_count");
    ColumnSetBuilder builder = new ColumnSetBuilder(catalog, new NullFormSupervisor());
    ColumnSet columnSet = builder.build(queryModel);
    System.out.println(columnSet);
    assertThat(columnSet.getNumRows(), equalTo(2));
    List<RecordVersion> versions1 = ((VersionedFormStorage) storage.get()).getVersions(village1.getRecordId());
    assertThat(versions1, hasSize(1));
    RecordVersion version = versions1.get(0);
    assertThat(version.getRecordId(), equalTo(village1.getRecordId()));
    assertThat(version.getUserId(), equalTo((long) userId));
    assertThat(version.getType(), equalTo(RecordChangeType.CREATED));
}
Also used : Quantity(org.activityinfo.model.type.number.Quantity) ColumnSet(org.activityinfo.model.query.ColumnSet) QueryModel(org.activityinfo.model.query.QueryModel) ColumnSetBuilder(org.activityinfo.store.query.server.ColumnSetBuilder) ResourceId(org.activityinfo.model.resource.ResourceId) QuantityType(org.activityinfo.model.type.number.QuantityType) FormClass(org.activityinfo.model.form.FormClass) NullFormSupervisor(org.activityinfo.store.query.shared.NullFormSupervisor) Test(org.junit.Test)

Example 13 with ColumnSet

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

the class ImportSimpleTest method locationWithMissingAdminLevel.

@Test
public void locationWithMissingAdminLevel() throws IOException {
    FormTree formTree = assertResolves(locator.getFormTree(VILLAGE_FORM_ID));
    FormTreePrettyPrinter.print(formTree);
    importModel = new ImportModel(formTree);
    // Step 1: User pastes in data to import
    PastedTable source = new PastedTable(Resources.toString(getResource(getClass(), "qis-villages.csv"), Charsets.UTF_8));
    source.parseAllRows();
    assertThat(source.getRows().size(), equalTo(1));
    importModel.setSource(source);
    importer = new Importer(locator, formTree, FieldImportStrategies.get(JvmConverterFactory.get()));
    dumpList("COLUMNS", source.getColumns());
    // Step 2: User maps imported columns to FormFields
    List<ImportTarget> targets = importer.getImportTargets();
    dumpList("FIELDS", targets);
    importModel.setColumnAction(columnIndex("Name"), target("Name"));
    importModel.setColumnAction(columnIndex("District"), target("District Name"));
    // Step 3: Validate for user
    ValidatedRowTable validatedResult = assertResolves(importer.validateRows(importModel));
    showValidationGrid(validatedResult);
    assertResolves(importer.persist(importModel));
    // AND... verify
    QueryModel queryModel = new QueryModel(VILLAGE_FORM_ID);
    queryModel.selectExpr("Name").as("name");
    queryModel.selectField(CuidAdapter.field(VILLAGE_FORM_ID, CuidAdapter.ADMIN_FIELD)).as("admin");
    ColumnSet columnSet = assertResolves(locator.queryTable(queryModel));
    assertThat(columnSet.getNumRows(), equalTo(1));
    assertThat(columnSet.getColumnView("name").getString(0), equalTo("Village 1"));
    assertThat(columnSet.getColumnView("admin").getString(0), equalTo(CuidAdapter.cuid(CuidAdapter.ADMIN_ENTITY_DOMAIN, 2).asString()));
}
Also used : FormTree(org.activityinfo.model.formTree.FormTree) PastedTable(org.activityinfo.ui.client.component.importDialog.model.source.PastedTable) ImportTarget(org.activityinfo.ui.client.component.importDialog.model.strategy.ImportTarget) ValidatedRowTable(org.activityinfo.ui.client.component.importDialog.model.validation.ValidatedRowTable) ColumnSet(org.activityinfo.model.query.ColumnSet) ImportModel(org.activityinfo.ui.client.component.importDialog.model.ImportModel) QueryModel(org.activityinfo.model.query.QueryModel) Test(org.junit.Test)

Example 14 with ColumnSet

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

the class SerialNumberTest method testWithPrefix.

@Test
public void testWithPrefix() {
    TestDatabase database = harness.createDatabase();
    ActivityInfoClient client = harness.client();
    FormClass formClass = new FormClass(harness.newFormId());
    formClass.setLabel("Test Prefix");
    formClass.setDatabaseId(database.getId());
    FormField partnerField = CuidAdapter.partnerField(formClass);
    formClass.addElement(partnerField);
    FormField provinceField = formClass.addField(harness.newFieldId()).setType(TextType.SIMPLE).setLabel("Province Code").setCode("PROVINCE").setRequired(true);
    FormField fileNumberField = formClass.addField(harness.newFieldId()).setCode("SN").setType(new SerialNumberType("PROVINCE", 5)).setLabel("Record Number");
    client.createForm(formClass);
    FormInstance kunduz1 = new FormInstance(harness.newRecordId(), formClass.getId());
    kunduz1.set(provinceField.getId(), "KUNDUZ");
    kunduz1.set(partnerField.getId(), database.getDefaultPartner());
    client.createRecord(kunduz1);
    FormInstance kunduz2 = new FormInstance(harness.newRecordId(), formClass.getId());
    kunduz2.set(provinceField.getId(), "KUNDUZ");
    kunduz2.set(partnerField.getId(), database.getDefaultPartner());
    client.createRecord(kunduz2);
    FormInstance takhar = new FormInstance(harness.newRecordId(), formClass.getId());
    takhar.set(provinceField.getId(), "TAKHAR");
    takhar.set(partnerField.getId(), database.getDefaultPartner());
    client.createRecord(takhar);
    // Verify that the serial numbers have been assigned
    kunduz1 = client.getTypedRecord(formClass, kunduz1.getId());
    kunduz2 = client.getTypedRecord(formClass, kunduz2.getId());
    takhar = client.getTypedRecord(formClass, takhar.getId());
    assertThat(kunduz1.get(fileNumberField.getId()), equalTo((FieldValue) new SerialNumber("KUNDUZ", 1)));
    assertThat(kunduz2.get(fileNumberField.getId()), equalTo((FieldValue) new SerialNumber("KUNDUZ", 2)));
    assertThat(takhar.get(fileNumberField.getId()), equalTo((FieldValue) new SerialNumber("TAKHAR", 1)));
    // Now verify that we can query a table with serial numbers
    QueryModel queryModel = new QueryModel(formClass.getId());
    queryModel.selectResourceId().as("id");
    queryModel.selectField("SN");
    ColumnSet columnSet = client.queryTable(queryModel);
    ColumnView sn = columnSet.getColumnView("SN");
}
Also used : SerialNumber(org.activityinfo.model.type.SerialNumber) ActivityInfoClient(org.activityinfo.client.ActivityInfoClient) FormClass(org.activityinfo.model.form.FormClass) ColumnView(org.activityinfo.model.query.ColumnView) ColumnSet(org.activityinfo.model.query.ColumnSet) FieldValue(org.activityinfo.model.type.FieldValue) FormInstance(org.activityinfo.model.form.FormInstance) FormField(org.activityinfo.model.form.FormField) SerialNumberType(org.activityinfo.model.type.SerialNumberType) QueryModel(org.activityinfo.model.query.QueryModel) Test(org.junit.Test)

Example 15 with ColumnSet

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

the class SurveyTest method updateAndQuery.

@Test
public void updateAndQuery() {
    // Post a number of records
    RecordGenerator generator = survey.getGenerator();
    int numRows = 20;
    for (int i = 0; i < numRows; i++) {
        client.createRecord(generator.get());
    }
    // Now query a few records
    QueryModel queryModel = new QueryModel(survey.getFormId());
    queryModel.selectField(survey.getGenderFieldId()).as("gender");
    queryModel.selectField(survey.getAgeFieldId()).as("age");
    queryModel.selectField(survey.getDobFieldId()).as("dob");
    ColumnSet columnSet = client.queryTable(queryModel);
    ColumnView gender = columnSet.getColumnView("gender");
    ColumnView age = columnSet.getColumnView("age");
    ColumnView dob = columnSet.getColumnView("dob");
    assertThat(gender.numRows(), equalTo(numRows));
    assertThat(gender.getType(), equalTo(ColumnType.STRING));
    assertThat(age.numRows(), equalTo(numRows));
}
Also used : ColumnView(org.activityinfo.model.query.ColumnView) ColumnSet(org.activityinfo.model.query.ColumnSet) RecordGenerator(org.activityinfo.store.testing.RecordGenerator) QueryModel(org.activityinfo.model.query.QueryModel) Test(org.junit.Test)

Aggregations

ColumnSet (org.activityinfo.model.query.ColumnSet)50 QueryModel (org.activityinfo.model.query.QueryModel)42 Test (org.junit.Test)28 ColumnView (org.activityinfo.model.query.ColumnView)24 ColumnSetBuilder (org.activityinfo.store.query.server.ColumnSetBuilder)14 FormTree (org.activityinfo.model.formTree.FormTree)12 NullFormSupervisor (org.activityinfo.store.query.shared.NullFormSupervisor)12 ResourceId (org.activityinfo.model.resource.ResourceId)10 FormClass (org.activityinfo.model.form.FormClass)7 NullFormScanCache (org.activityinfo.store.query.shared.NullFormScanCache)7 Nullable (javax.annotation.Nullable)6 Charsets (com.google.common.base.Charsets)5 Operation (io.swagger.v3.oas.annotations.Operation)5 FormTreeBuilder (org.activityinfo.model.formTree.FormTreeBuilder)5 ColumnModel (org.activityinfo.model.query.ColumnModel)5 RowBasedJsonWriter (org.activityinfo.store.query.output.RowBasedJsonWriter)5 PrintWriter (java.io.PrintWriter)4 DimensionCategory (org.activityinfo.legacy.shared.reports.content.DimensionCategory)4 FormField (org.activityinfo.model.form.FormField)4 RecordRef (org.activityinfo.model.type.RecordRef)4