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);
}
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();
}
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();
}
}
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();
}
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));
}
Aggregations