Search in sources :

Example 1 with ColumnSetBuilder

use of org.activityinfo.store.query.server.ColumnSetBuilder in project activityinfo by bedatadriven.

the class ResourceLocatorSyncImpl method getReferenceChoices.

@Override
public List<ReferenceChoice> getReferenceChoices(Collection<ResourceId> range) {
    ResourceId formId = Iterables.getOnlyElement(range);
    QueryModel queryModel = new QueryModel(formId);
    queryModel.selectResourceId().as("id");
    queryModel.selectExpr("label").as("label");
    ColumnSetBuilder builder = new ColumnSetBuilder(catalog.get(), new NullFormSupervisor());
    ColumnSet columnSet = builder.build(queryModel);
    ColumnView id = columnSet.getColumnView("id");
    ColumnView label = columnSet.getColumnView("label");
    List<ReferenceChoice> choices = Lists.newArrayList();
    for (int i = 0; i < columnSet.getNumRows(); i++) {
        ResourceId choiceId = ResourceId.valueOf(id.getString(i));
        String choiceLabel = label.getString(i);
        choices.add(new ReferenceChoice(new RecordRef(formId, choiceId), choiceLabel));
    }
    return choices;
}
Also used : ColumnSetBuilder(org.activityinfo.store.query.server.ColumnSetBuilder) ResourceId(org.activityinfo.model.resource.ResourceId) ColumnView(org.activityinfo.model.query.ColumnView) RecordRef(org.activityinfo.model.type.RecordRef) ColumnSet(org.activityinfo.model.query.ColumnSet) NullFormSupervisor(org.activityinfo.store.query.shared.NullFormSupervisor) QueryModel(org.activityinfo.model.query.QueryModel)

Example 2 with ColumnSetBuilder

use of org.activityinfo.store.query.server.ColumnSetBuilder in project activityinfo by bedatadriven.

the class QueryResource method queryRows.

@POST
@Path("rows")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Operation(summary = "Executes a query over a set of forms in row format")
public Response queryRows(QueryModel model) {
    ColumnSetBuilder builder = backend.newQueryBuilder();
    final ColumnSet columnSet = builder.build(model);
    final StreamingOutput output = outputStream -> {
        RowBasedJsonWriter writer = new RowBasedJsonWriter(outputStream, Charsets.UTF_8);
        writer.write(columnSet);
        writer.flush();
    };
    return Response.ok(output).type(MediaType.APPLICATION_JSON_TYPE).build();
}
Also used : ColumnSetBuilder(org.activityinfo.store.query.server.ColumnSetBuilder) Charsets(com.google.common.base.Charsets) ColumnJsonWriter(org.activityinfo.store.query.output.ColumnJsonWriter) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Path(javax.ws.rs.Path) StreamingOutput(javax.ws.rs.core.StreamingOutput) Operation(io.swagger.v3.oas.annotations.Operation) MediaType(javax.ws.rs.core.MediaType) Consumes(javax.ws.rs.Consumes) Response(javax.ws.rs.core.Response) ColumnSet(org.activityinfo.model.query.ColumnSet) QueryModel(org.activityinfo.model.query.QueryModel) ColumnSetBuilder(org.activityinfo.store.query.server.ColumnSetBuilder) RowBasedJsonWriter(org.activityinfo.store.query.output.RowBasedJsonWriter) ColumnSet(org.activityinfo.model.query.ColumnSet) StreamingOutput(javax.ws.rs.core.StreamingOutput) RowBasedJsonWriter(org.activityinfo.store.query.output.RowBasedJsonWriter) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Operation(io.swagger.v3.oas.annotations.Operation)

Example 3 with ColumnSetBuilder

use of org.activityinfo.store.query.server.ColumnSetBuilder in project activityinfo by bedatadriven.

the class QueryResource method queryColumns.

@POST
@Path("columns")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Operation(summary = "Executes a query over a set of forms in columnar format")
public Response queryColumns(QueryModel model) {
    ColumnSetBuilder builder = backend.newQueryBuilder();
    final ColumnSet columnSet = builder.build(model);
    final StreamingOutput output = outputStream -> {
        ColumnJsonWriter columnSetWriter = new ColumnJsonWriter(outputStream, Charsets.UTF_8);
        columnSetWriter.write(columnSet);
        columnSetWriter.flush();
    };
    return Response.ok(output).type(MediaType.APPLICATION_JSON_TYPE).build();
}
Also used : ColumnSetBuilder(org.activityinfo.store.query.server.ColumnSetBuilder) Charsets(com.google.common.base.Charsets) ColumnJsonWriter(org.activityinfo.store.query.output.ColumnJsonWriter) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Path(javax.ws.rs.Path) StreamingOutput(javax.ws.rs.core.StreamingOutput) Operation(io.swagger.v3.oas.annotations.Operation) MediaType(javax.ws.rs.core.MediaType) Consumes(javax.ws.rs.Consumes) Response(javax.ws.rs.core.Response) ColumnSet(org.activityinfo.model.query.ColumnSet) QueryModel(org.activityinfo.model.query.QueryModel) ColumnSetBuilder(org.activityinfo.store.query.server.ColumnSetBuilder) RowBasedJsonWriter(org.activityinfo.store.query.output.RowBasedJsonWriter) ColumnJsonWriter(org.activityinfo.store.query.output.ColumnJsonWriter) ColumnSet(org.activityinfo.model.query.ColumnSet) StreamingOutput(javax.ws.rs.core.StreamingOutput) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Operation(io.swagger.v3.oas.annotations.Operation)

Example 4 with ColumnSetBuilder

use of org.activityinfo.store.query.server.ColumnSetBuilder 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 5 with ColumnSetBuilder

use of org.activityinfo.store.query.server.ColumnSetBuilder in project activityinfo by bedatadriven.

the class DateFunctionQueryTest method invalidArityTest.

@Test
public void invalidArityTest() {
    TestingStorageProvider catalog = new TestingStorageProvider();
    IntakeForm intakeForm = catalog.getIntakeForm();
    ColumnSetBuilder builder = new ColumnSetBuilder(catalog, new NullFormScanCache(), new NullFormSupervisor());
    QueryModel queryModel = new QueryModel(intakeForm.getFormId());
    queryModel.selectExpr("YEARFRAC(TODAY())").as("age");
    ColumnSet columnSet = builder.build(queryModel);
    ColumnView age = columnSet.getColumnView("age");
    assertThat(age.numRows(), equalTo(IntakeForm.ROW_COUNT));
    System.out.println(age);
}
Also used : ColumnSetBuilder(org.activityinfo.store.query.server.ColumnSetBuilder) NullFormScanCache(org.activityinfo.store.query.shared.NullFormScanCache) ColumnView(org.activityinfo.model.query.ColumnView) ColumnSet(org.activityinfo.model.query.ColumnSet) NullFormSupervisor(org.activityinfo.store.query.shared.NullFormSupervisor) QueryModel(org.activityinfo.model.query.QueryModel) Test(org.junit.Test)

Aggregations

ColumnSetBuilder (org.activityinfo.store.query.server.ColumnSetBuilder)20 NullFormSupervisor (org.activityinfo.store.query.shared.NullFormSupervisor)16 ColumnSet (org.activityinfo.model.query.ColumnSet)14 QueryModel (org.activityinfo.model.query.QueryModel)13 Test (org.junit.Test)11 ColumnView (org.activityinfo.model.query.ColumnView)8 NullFormScanCache (org.activityinfo.store.query.shared.NullFormScanCache)8 FormClass (org.activityinfo.model.form.FormClass)5 FormField (org.activityinfo.model.form.FormField)3 Quantity (org.activityinfo.model.type.number.Quantity)3 QuantityType (org.activityinfo.model.type.number.QuantityType)3 Charsets (com.google.common.base.Charsets)2 Operation (io.swagger.v3.oas.annotations.Operation)2 Consumes (javax.ws.rs.Consumes)2 POST (javax.ws.rs.POST)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 MediaType (javax.ws.rs.core.MediaType)2 Response (javax.ws.rs.core.Response)2 StreamingOutput (javax.ws.rs.core.StreamingOutput)2