use of org.activityinfo.model.form.FormClass 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));
}
use of org.activityinfo.model.form.FormClass in project activityinfo by bedatadriven.
the class HrdCatalogTest method versionRangeTest.
@Test
public void versionRangeTest() {
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);
VersionedFormStorage formStorage = (VersionedFormStorage) catalog.getForm(collectionId).get();
// Initially, with no records added, the form should be at version 1
// and the version range (0, 1] should be empty.
assertThat(formStorage.cacheVersion(), equalTo(1L));
FormSyncSet updatedRecords = formStorage.getVersionRange(0, 1L, resourceId -> true);
assertTrue(updatedRecords.isEmpty());
// Add a new record
TypedRecordUpdate firstUpdate = new TypedRecordUpdate();
firstUpdate.setUserId(1);
firstUpdate.setRecordId(ResourceId.generateId());
firstUpdate.set(villageField, TextValue.valueOf("Goma"));
catalog.getForm(collectionId).get().add(firstUpdate);
// Verify that the version is incremented and the version range
// (0, 2] includes our new record
assertThat(formStorage.cacheVersion(), equalTo(2L));
FormSyncSet updated = formStorage.getVersionRange(0, 2L, resourceId -> true);
assertThat(updated.getUpdatedRecordCount(), equalTo(1));
// Update the first record and add a new one
TypedRecordUpdate secondUpdate = new TypedRecordUpdate();
secondUpdate.setUserId(1);
secondUpdate.setRecordId(ResourceId.generateId());
secondUpdate.set(villageField, TextValue.valueOf("Rutshuru"));
catalog.getForm(collectionId).get().add(firstUpdate);
// Verify that the version is incremented and the version range
// (1, 2] includes our new record
assertThat(formStorage.cacheVersion(), equalTo(3L));
updated = formStorage.getVersionRange(2L, 3L, resourceId -> true);
assertThat(updated.getUpdatedRecordCount(), equalTo(1));
}
use of org.activityinfo.model.form.FormClass in project activityinfo by bedatadriven.
the class FixSubForm method maybeFixForm.
private boolean maybeFixForm(final PrintWriter logger, MySqlQueryExecutor executor, ResourceId parentFormId) {
logger.println("Fixing " + parentFormId + "...");
final MySqlStorageProvider catalog = new MySqlStorageProvider(executor);
FormStorage parentForm = catalog.getForm(parentFormId).get();
final FormClass formClass = parentForm.getFormClass();
logger.println("Loaded activity " + parentForm.getFormClass().getLabel());
final List<FormField> updated = new ArrayList<>();
ObjectifyService.run(new VoidWork() {
@Override
public void vrun() {
Hrd.ofy().transact(new VoidWork() {
@Override
public void vrun() {
for (FormField formField : formClass.getFields()) {
if (formField.getType() instanceof SubFormReferenceType) {
if (maybeFixForm(logger, formClass, formField)) {
updated.add(formField);
}
}
}
}
});
}
});
logger.println("TX COMPLETED!");
if (!updated.isEmpty()) {
logger.println("Updating parent form schema...");
catalog.createOrUpdateFormSchema(formClass);
return true;
} else {
return false;
}
}
use of org.activityinfo.model.form.FormClass in project activityinfo by bedatadriven.
the class FormFolder method getChildren.
public List<CatalogEntry> getChildren(ResourceId formId) {
Optional<FormStorage> storage = catalog.getForm(formId);
if (!storage.isPresent()) {
return Collections.emptyList();
}
List<CatalogEntry> entries = new ArrayList<>();
FormClass formClass = storage.get().getFormClass();
for (FormField formField : formClass.getFields()) {
if (formField.getType() instanceof SubFormReferenceType) {
SubFormReferenceType subFormType = (SubFormReferenceType) formField.getType();
ResourceId subFormId = subFormType.getClassId();
CatalogEntry catalogEntry = new CatalogEntry(subFormId.asString(), formField.getLabel(), CatalogEntryType.FORM);
catalogEntry.setLeaf(true);
entries.add(catalogEntry);
}
}
return entries;
}
use of org.activityinfo.model.form.FormClass in project activityinfo by bedatadriven.
the class ExprQueryBuilder method computeFormClass.
private FormSymbolTable computeFormClass(FormSymbolTable parent, FormulaNode formulaNode) {
if (formulaNode instanceof SymbolNode) {
FormField field = parent.resolveSymbol((SymbolNode) formulaNode);
if (field.getType() instanceof RecordFieldType) {
RecordFieldType recordFieldType = (RecordFieldType) field.getType();
FormClass recordFormClass = recordFieldType.getFormClass();
return new FormSymbolTable(recordFormClass);
} else {
throw new IllegalStateException(field.getName() + " is not a record type field.");
}
} else if (formulaNode instanceof CompoundExpr) {
CompoundExpr compoundExpr = (CompoundExpr) formulaNode;
FormSymbolTable child = computeFormClass(parent, compoundExpr.getValue());
return computeFormClass(child, compoundExpr.getValue());
} else {
throw new UnsupportedOperationException("exprNode: " + formulaNode);
}
}
Aggregations