Search in sources :

Example 11 with FormStorage

use of org.activityinfo.store.spi.FormStorage in project activityinfo by bedatadriven.

the class ColumnSetBuilder method executeScan.

private void executeScan(FormScan scan) {
    if (scan.isEmpty()) {
        LOGGER.info("Skipping form scan of " + scan.getFormId() + ", fully resolved from cache");
        return;
    }
    Optional<FormStorage> form = catalog.getForm(scan.getFormId());
    if (!form.isPresent()) {
        throw new IllegalStateException("No storage for form " + scan.getFormId());
    }
    ColumnQueryBuilder queryBuilder = form.get().newColumnQuery();
    scan.prepare(queryBuilder);
    // Run the query
    Stopwatch stopwatch = Stopwatch.createStarted();
    queryBuilder.execute();
    LOGGER.info("Form scan of " + scan.getFormId() + " completed in " + stopwatch);
}
Also used : ColumnQueryBuilder(org.activityinfo.store.spi.ColumnQueryBuilder) FormStorage(org.activityinfo.store.spi.FormStorage) Stopwatch(com.google.common.base.Stopwatch)

Example 12 with FormStorage

use of org.activityinfo.store.spi.FormStorage in project activityinfo by bedatadriven.

the class FormResource method getRecord.

@GET
@NoCache
@Path("record/{recordId}")
@Produces(JSON_CONTENT_TYPE)
@Operation(summary = "Get a single record")
public FormRecord getRecord(@PathParam("recordId") String recordId) {
    FormStorage form = assertVisible(formId);
    Optional<FormRecord> record = form.get(ResourceId.valueOf(recordId));
    if (!record.isPresent()) {
        throw new NotFoundException("Record " + recordId + " does not exist.");
    }
    PermissionsEnforcer enforcer = backend.newPermissionsEnforcer();
    if (!enforcer.canView(record.get())) {
        throw new NotAuthorizedException();
    }
    return record.get();
}
Also used : VersionedFormStorage(org.activityinfo.store.spi.VersionedFormStorage) FormStorage(org.activityinfo.store.spi.FormStorage) NotFoundException(com.sun.jersey.api.NotFoundException) PermissionsEnforcer(org.activityinfo.store.query.server.PermissionsEnforcer) Operation(io.swagger.v3.oas.annotations.Operation)

Example 13 with FormStorage

use of org.activityinfo.store.spi.FormStorage in project activityinfo by bedatadriven.

the class FormResource method getMetadataResponse.

@GET
@NoCache
@Produces(JSON_CONTENT_TYPE)
public FormMetadata getMetadataResponse(@InjectParam DatabaseProviderImpl databaseProvider, @InjectParam AuthenticatedUser user, @QueryParam("localVersion") Long localVersion) {
    Optional<FormStorage> storage = backend.getStorage().getForm(formId);
    if (!storage.isPresent()) {
        return FormMetadata.notFound(formId);
    }
    ResourceId databaseId = storage.get().getFormClass().getDatabaseId();
    UserDatabaseMeta databaseMetadata;
    try {
        databaseMetadata = databaseProvider.getDatabaseMetadata(databaseId, user.getUserId());
    } catch (Exception e) {
        // We are initially using this just for locks,
        // not actually permissions, so just log the warning for now.
        LOGGER.log(Level.SEVERE, "Failed to retrieve metadata for database " + databaseId + " for user " + user.getUserId(), e);
        databaseMetadata = new UserDatabaseMeta.Builder().setDatabaseId(databaseId).setLabel("").setOwner(false).build();
    }
    FormPermissions permissions = backend.getFormSupervisor().getFormPermissions(formId);
    if (!permissions.isVisible()) {
        throw new WebApplicationException(Response.Status.FORBIDDEN);
    } else {
        // Workaround for sub form, which we don't yet have indexed to the
        // database in which they live.
        FormClass schema = storage.get().getFormClass();
        RecordLockSet locks;
        if (schema.isSubForm()) {
            locks = databaseMetadata.getEffectiveLocks(schema.getParentFormId().get());
        } else {
            locks = databaseMetadata.getEffectiveLocks(formId);
        }
        return new FormMetadata.Builder().setId(formId).setPermissions(permissions).setSchema(schema).setLocks(locks).setVersion(storage.get().cacheVersion()).build();
    }
}
Also used : VersionedFormStorage(org.activityinfo.store.spi.VersionedFormStorage) FormStorage(org.activityinfo.store.spi.FormStorage) ResourceId(org.activityinfo.model.resource.ResourceId) FormTreeBuilder(org.activityinfo.model.formTree.FormTreeBuilder) JsonFormTreeBuilder(org.activityinfo.model.formTree.JsonFormTreeBuilder) XlsFormBuilder(org.activityinfo.io.xlsform.XlsFormBuilder) RecordLockSet(org.activityinfo.model.database.RecordLockSet) UserDatabaseMeta(org.activityinfo.model.database.UserDatabaseMeta) NotFoundException(com.sun.jersey.api.NotFoundException) SQLException(java.sql.SQLException) InvalidUpdateException(org.activityinfo.store.query.server.InvalidUpdateException) ParseException(com.vividsolutions.jts.io.ParseException)

Example 14 with FormStorage

use of org.activityinfo.store.spi.FormStorage in project activityinfo by bedatadriven.

the class FormResource method updateGeometry.

@POST
@Path("record/{recordId}/field/{fieldId}/geometry")
public Response updateGeometry(@PathParam("recordId") String recordId, @PathParam("fieldId") String fieldId, byte[] binaryBody) {
    // Parse the Geometry
    WKBReader reader = new WKBReader(new GeometryFactory());
    Geometry geometry;
    try {
        geometry = reader.read(binaryBody);
    } catch (ParseException e) {
        return Response.status(Response.Status.BAD_REQUEST).entity("Could not parse WKB geometry: " + e.getMessage()).build();
    }
    geometry.normalize();
    if (!geometry.isValid()) {
        return Response.status(Response.Status.BAD_REQUEST).entity(geometry.getGeometryType() + " is not valid").build();
    }
    // Check first to see if this form exists
    Optional<FormStorage> storage = backend.getStorage().getForm(formId);
    if (!storage.isPresent()) {
        return Response.status(Response.Status.NOT_FOUND).entity("Form " + formId + " does not exist.").build();
    }
    // Find the field and verify that it's a GeoArea type
    FormField field;
    try {
        field = storage.get().getFormClass().getField(ResourceId.valueOf(fieldId));
    } catch (IllegalArgumentException e) {
        return Response.status(Response.Status.NOT_FOUND).entity("Record " + recordId + " does not exist.").build();
    }
    if (!(field.getType() instanceof GeoAreaType)) {
        return Response.status(Response.Status.BAD_REQUEST).entity("Field " + fieldId + " is not a GeoArea type").build();
    }
    try {
        storage.get().updateGeometry(ResourceId.valueOf(recordId), ResourceId.valueOf(fieldId), geometry);
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, "Failed to update geometry for record " + recordId, e);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
    }
    return Response.ok().build();
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) VersionedFormStorage(org.activityinfo.store.spi.VersionedFormStorage) FormStorage(org.activityinfo.store.spi.FormStorage) GeoAreaType(org.activityinfo.model.type.geo.GeoAreaType) ParseException(com.vividsolutions.jts.io.ParseException) WKBReader(com.vividsolutions.jts.io.WKBReader) NotFoundException(com.sun.jersey.api.NotFoundException) SQLException(java.sql.SQLException) InvalidUpdateException(org.activityinfo.store.query.server.InvalidUpdateException) ParseException(com.vividsolutions.jts.io.ParseException)

Example 15 with FormStorage

use of org.activityinfo.store.spi.FormStorage in project activityinfo by bedatadriven.

the class SubFormAggregationTest method subFormAggregationTest.

@Test
public void subFormAggregationTest() {
    // Typical scenario with a household interview form
    // and a repeating househould member form
    FormClass siteForm = new FormClass(ResourceId.generateId());
    siteForm.setParentFormId(ResourceId.ROOT_ID);
    FormClass monthlyForm = new FormClass(ResourceId.generateId());
    monthlyForm.setParentFormId(siteForm.getId());
    monthlyForm.setSubFormKind(SubFormKind.MONTHLY);
    siteForm.setLabel("Household interview");
    FormField villageField = siteForm.addField().setLabel("Village Name").setType(TextType.SIMPLE);
    siteForm.addField().setLabel("Maximum Beneficiaries").setCode("BENE").setType(new CalculatedFieldType("MAX(HH)"));
    siteForm.addField().setLabel("Monthly Activities").setType(new SubFormReferenceType(monthlyForm.getId()));
    monthlyForm.setLabel("Monthly Activities");
    FormField countField = monthlyForm.addField().setLabel("Number of Beneficiaries").setCode("HH").setType(new QuantityType("households"));
    HrdStorageProvider catalog = new HrdStorageProvider();
    catalog.create(siteForm);
    catalog.create(monthlyForm);
    TypedRecordUpdate v1 = new TypedRecordUpdate();
    v1.setUserId(userId);
    v1.setRecordId(ResourceId.generateSubmissionId(siteForm));
    v1.set(villageField.getId(), TextValue.valueOf("Rutshuru"));
    TypedRecordUpdate v2 = new TypedRecordUpdate();
    v2.setUserId(userId);
    v2.setRecordId(ResourceId.generateSubmissionId(siteForm));
    v2.set(villageField.getId(), TextValue.valueOf("Beni"));
    TypedRecordUpdate month1 = new TypedRecordUpdate();
    month1.setUserId(userId);
    month1.setRecordId(ResourceId.generateSubmissionId(monthlyForm));
    month1.setParentId(v1.getRecordId());
    month1.set(countField.getId(), new Quantity(40));
    TypedRecordUpdate month2 = new TypedRecordUpdate();
    month2.setUserId(userId);
    month2.setRecordId(ResourceId.generateSubmissionId(monthlyForm));
    month2.setParentId(v1.getRecordId());
    month2.set(countField.getId(), new Quantity(30));
    TypedRecordUpdate month3 = new TypedRecordUpdate();
    month3.setUserId(userId);
    month3.setRecordId(ResourceId.generateSubmissionId(monthlyForm));
    month3.setParentId(v2.getRecordId());
    month3.set(countField.getId(), new Quantity(47));
    FormStorage siteCollection = catalog.getForm(siteForm.getId()).get();
    siteCollection.add(v1);
    siteCollection.add(v2);
    Optional<FormStorage> monthCollection = catalog.getForm(monthlyForm.getId());
    assertTrue(monthCollection.isPresent());
    monthCollection.get().add(month1);
    monthCollection.get().add(month2);
    monthCollection.get().add(month3);
    QueryModel queryModel = new QueryModel(siteForm.getId());
    queryModel.selectResourceId().as("id");
    queryModel.selectField("Village Name").as("village");
    queryModel.selectField("BENE").as("max_hh");
    ColumnSetBuilder builder = new ColumnSetBuilder(catalog, new NullFormSupervisor());
    ColumnSet columnSet = builder.build(queryModel);
    System.out.println(columnSet);
    assertThat(columnSet.getNumRows(), equalTo(2));
}
Also used : CalculatedFieldType(org.activityinfo.model.type.expr.CalculatedFieldType) Quantity(org.activityinfo.model.type.number.Quantity) ColumnSet(org.activityinfo.model.query.ColumnSet) QueryModel(org.activityinfo.model.query.QueryModel) TypedRecordUpdate(org.activityinfo.store.spi.TypedRecordUpdate) SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) ColumnSetBuilder(org.activityinfo.store.query.server.ColumnSetBuilder) FormStorage(org.activityinfo.store.spi.FormStorage) QuantityType(org.activityinfo.model.type.number.QuantityType) FormClass(org.activityinfo.model.form.FormClass) NullFormSupervisor(org.activityinfo.store.query.shared.NullFormSupervisor) FormField(org.activityinfo.model.form.FormField) Test(org.junit.Test)

Aggregations

FormStorage (org.activityinfo.store.spi.FormStorage)25 ResourceId (org.activityinfo.model.resource.ResourceId)9 VersionedFormStorage (org.activityinfo.store.spi.VersionedFormStorage)9 Test (org.junit.Test)6 FormClass (org.activityinfo.model.form.FormClass)4 NotFoundException (com.sun.jersey.api.NotFoundException)3 FormField (org.activityinfo.model.form.FormField)3 SubFormReferenceType (org.activityinfo.model.type.subform.SubFormReferenceType)3 HrdFormStorage (org.activityinfo.store.hrd.HrdFormStorage)3 FormStorageProvider (org.activityinfo.store.spi.FormStorageProvider)3 GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)2 ParseException (com.vividsolutions.jts.io.ParseException)2 Operation (io.swagger.v3.oas.annotations.Operation)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 RecordLockSet (org.activityinfo.model.database.RecordLockSet)2 UserDatabaseMeta (org.activityinfo.model.database.UserDatabaseMeta)2 ColumnSet (org.activityinfo.model.query.ColumnSet)2 QueryModel (org.activityinfo.model.query.QueryModel)2 InvalidUpdateException (org.activityinfo.store.query.server.InvalidUpdateException)2