Search in sources :

Example 1 with RowBasedJsonWriter

use of org.activityinfo.store.query.output.RowBasedJsonWriter 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 2 with RowBasedJsonWriter

use of org.activityinfo.store.query.output.RowBasedJsonWriter in project activityinfo by bedatadriven.

the class MySqlCatalogIntegrationTest method testActivity.

@Test
public void testActivity() throws IOException {
    QueryModel model = new QueryModel(activityFormClass(33));
    model.selectField("date1");
    model.selectField("date2");
    model.selectExpr("Partner.name");
    FormTree formTree = new FormTreeBuilder(catalog).queryTree(activityFormClass(33));
    FormTreePrettyPrinter.print(formTree);
    FormClass formClass = catalog.getForm(activityFormClass(33)).get().getFormClass();
    for (FormField field : formClass.getFields()) {
        if (field.getType() instanceof QuantityType) {
            model.selectField(field.getId()).as("I" + CuidAdapter.getLegacyIdFromCuid(field.getId()));
        }
    }
    Stopwatch stopwatch = Stopwatch.createStarted();
    ColumnSet columnSet = columnSetBuilder.build(model);
    System.out.println("Query executed in " + stopwatch.elapsed(TimeUnit.MILLISECONDS) + " ms");
    assertThat(columnSet.getNumRows(), equalTo(652));
    StringWriter stringWriter = new StringWriter();
    RowBasedJsonWriter writer = new RowBasedJsonWriter(stringWriter);
    writer.write(columnSet);
    System.out.println(stringWriter.toString());
}
Also used : FormTree(org.activityinfo.model.formTree.FormTree) StringWriter(java.io.StringWriter) QuantityType(org.activityinfo.model.type.number.QuantityType) FormClass(org.activityinfo.model.form.FormClass) CuidAdapter.activityFormClass(org.activityinfo.model.legacy.CuidAdapter.activityFormClass) Stopwatch(com.google.common.base.Stopwatch) ColumnSet(org.activityinfo.model.query.ColumnSet) RowBasedJsonWriter(org.activityinfo.store.query.output.RowBasedJsonWriter) QueryModel(org.activityinfo.model.query.QueryModel) FormField(org.activityinfo.model.form.FormField) FormTreeBuilder(org.activityinfo.model.formTree.FormTreeBuilder) Test(org.junit.Test)

Example 3 with RowBasedJsonWriter

use of org.activityinfo.store.query.output.RowBasedJsonWriter in project activityinfo by bedatadriven.

the class FormResource method queryRows.

@GET
@NoCache
@Path("query/rows")
@Produces(MediaType.APPLICATION_JSON)
public Response queryRows(@Context UriInfo uriInfo) {
    final ColumnSet columnSet = query(uriInfo);
    LOGGER.info("Query completed with " + columnSet.getNumRows() + " rows.");
    final StreamingOutput output = outputStream -> {
        RowBasedJsonWriter writer = new RowBasedJsonWriter(outputStream, Charsets.UTF_8);
        writer.write(columnSet);
        writer.flush();
    };
    return Response.ok(output).type(JSON_CONTENT_TYPE).build();
}
Also used : ColumnView(org.activityinfo.model.query.ColumnView) ColumnJsonWriter(org.activityinfo.store.query.output.ColumnJsonWriter) FormTreeBuilder(org.activityinfo.model.formTree.FormTreeBuilder) JsonFormTreeBuilder(org.activityinfo.model.formTree.JsonFormTreeBuilder) GeoAreaType(org.activityinfo.model.type.geo.GeoAreaType) ResourceId.valueOf(org.activityinfo.model.resource.ResourceId.valueOf) UserDatabaseMeta(org.activityinfo.model.database.UserDatabaseMeta) WKBReader(com.vividsolutions.jts.io.WKBReader) PermissionsEnforcer(org.activityinfo.store.query.server.PermissionsEnforcer) org.activityinfo.model.form(org.activityinfo.model.form) NotFoundException(com.sun.jersey.api.NotFoundException) Level(java.util.logging.Level) HashSet(java.util.HashSet) Operation(io.swagger.v3.oas.annotations.Operation) SQLException(java.sql.SQLException) ResourceId(org.activityinfo.model.resource.ResourceId) Optional(com.google.common.base.Optional) Geometry(com.vividsolutions.jts.geom.Geometry) RecordRef(org.activityinfo.model.type.RecordRef) JsonValue(org.activityinfo.json.JsonValue) FormTreePrettyPrinter(org.activityinfo.model.formTree.FormTreePrettyPrinter) VersionedFormStorage(org.activityinfo.store.spi.VersionedFormStorage) RecordLockSet(org.activityinfo.model.database.RecordLockSet) InvalidUpdateException(org.activityinfo.store.query.server.InvalidUpdateException) PrintWriter(java.io.PrintWriter) Charsets(com.google.common.base.Charsets) ParseException(com.vividsolutions.jts.io.ParseException) StringWriter(java.io.StringWriter) Set(java.util.Set) javax.ws.rs.core(javax.ws.rs.core) Logger(java.util.logging.Logger) String.format(java.lang.String.format) FormStorage(org.activityinfo.store.spi.FormStorage) RecordLock(org.activityinfo.model.database.RecordLock) List(java.util.List) javax.ws.rs(javax.ws.rs) ColumnSet(org.activityinfo.model.query.ColumnSet) QueryModel(org.activityinfo.model.query.QueryModel) Tag(io.swagger.v3.oas.annotations.tags.Tag) XlsFormBuilder(org.activityinfo.io.xlsform.XlsFormBuilder) AuthenticatedUser(org.activityinfo.legacy.shared.AuthenticatedUser) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) InjectParam(com.sun.jersey.api.core.InjectParam) FormTree(org.activityinfo.model.formTree.FormTree) RowBasedJsonWriter(org.activityinfo.store.query.output.RowBasedJsonWriter) ColumnSet(org.activityinfo.model.query.ColumnSet) RowBasedJsonWriter(org.activityinfo.store.query.output.RowBasedJsonWriter)

Aggregations

ColumnSet (org.activityinfo.model.query.ColumnSet)3 QueryModel (org.activityinfo.model.query.QueryModel)3 Charsets (com.google.common.base.Charsets)2 Operation (io.swagger.v3.oas.annotations.Operation)2 StringWriter (java.io.StringWriter)2 FormTree (org.activityinfo.model.formTree.FormTree)2 FormTreeBuilder (org.activityinfo.model.formTree.FormTreeBuilder)2 ColumnJsonWriter (org.activityinfo.store.query.output.ColumnJsonWriter)2 RowBasedJsonWriter (org.activityinfo.store.query.output.RowBasedJsonWriter)2 Optional (com.google.common.base.Optional)1 Stopwatch (com.google.common.base.Stopwatch)1 NotFoundException (com.sun.jersey.api.NotFoundException)1 InjectParam (com.sun.jersey.api.core.InjectParam)1 Geometry (com.vividsolutions.jts.geom.Geometry)1 GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)1 ParseException (com.vividsolutions.jts.io.ParseException)1 WKBReader (com.vividsolutions.jts.io.WKBReader)1 Tag (io.swagger.v3.oas.annotations.tags.Tag)1 PrintWriter (java.io.PrintWriter)1 String.format (java.lang.String.format)1