use of org.activityinfo.store.spi.TypedRecordUpdate in project activityinfo by bedatadriven.
the class UpdaterTest method missingValue.
@Test
public void missingValue() throws JsonMappingException {
ResourceId fieldId = ResourceId.valueOf("Q1");
FormClass formClass = new FormClass(ResourceId.valueOf("XYZ123"));
formClass.addElement(new FormField(fieldId).setType(new QuantityType("meters")));
JsonValue fields = createObject();
fields.put("Q1", Json.createNull());
JsonValue change = createObject();
change.put("recordId", "A");
change.put("formId", "XYZ123");
change.put("fields", fields);
TypedRecordUpdate update = Updater.parseChange(formClass, change, userId);
assertTrue(update.getChangedFieldValues().containsKey(fieldId));
}
use of org.activityinfo.store.spi.TypedRecordUpdate 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));
}
use of org.activityinfo.store.spi.TypedRecordUpdate in project activityinfo by bedatadriven.
the class UpdaterTest method serialNumber.
@Test
public void serialNumber() throws JsonMappingException {
FormClass formClass = new FormClass(ResourceId.valueOf("FORM1"));
formClass.addField(ResourceId.valueOf("FIELD0")).setType(TextType.SIMPLE).setLabel("Province Code").setCode("PROVINCE").setRequired(true);
FormField serialNumberField = formClass.addField(ResourceId.valueOf("FIELD1")).setType(new SerialNumberType("PROVINCE", 5)).setRequired(true).setLabel("File Number").setCode("SN");
JsonValue fields = Json.createObject();
fields.put("PROVINCE", "KUNDUZ");
JsonValue change = createObject();
change.put("recordId", "A");
change.put("formId", "FORM1");
change.put("fields", fields);
TypedRecordUpdate update = Updater.parseChange(formClass, change, userId);
FormInstance effectiveRecord = updater.computeEffectiveRecord(formClass, Optional.<FormRecord>absent(), update);
updater.generateSerialNumber(formClass, serialNumberField, effectiveRecord, update);
FieldValue serialValue = update.getChangedFieldValues().get(serialNumberField.getId());
assertThat(serialValue, equalTo((FieldValue) new SerialNumber("KUNDUZ", 1)));
}
use of org.activityinfo.store.spi.TypedRecordUpdate in project activityinfo by bedatadriven.
the class UpdaterTest method validQuantity.
@Test
public void validQuantity() throws JsonMappingException {
ResourceId fieldId = ResourceId.valueOf("Q1");
FormClass formClass = new FormClass(ResourceId.valueOf("XYZ123"));
formClass.addElement(new FormField(fieldId).setType(new QuantityType("meters")));
JsonValue fields = createObject();
fields.put("Q1", 41.3);
JsonValue change = createObject();
change.put("recordId", "A");
change.put("formId", "XYZ123");
change.put("fields", fields);
TypedRecordUpdate update = Updater.parseChange(formClass, change, userId);
assertThat(update.getChangedFieldValues().get(fieldId), equalTo((FieldValue) new Quantity(41.3)));
}
use of org.activityinfo.store.spi.TypedRecordUpdate in project activityinfo by bedatadriven.
the class UpdaterTest method parsedQuantity.
@Test
public void parsedQuantity() throws JsonMappingException {
ResourceId fieldId = ResourceId.valueOf("Q1");
FormClass formClass = new FormClass(ResourceId.valueOf("XYZ123"));
formClass.addElement(new FormField(fieldId).setType(new QuantityType("meters")));
JsonValue fields = Json.createObject();
fields.put("Q1", "41.3");
JsonValue change = createObject();
change.put("recordId", "A");
change.put("formId", "XYZ123");
change.put("fields", fields);
TypedRecordUpdate update = Updater.parseChange(formClass, change, userId);
assertThat(update.getChangedFieldValues().get(fieldId), equalTo((FieldValue) new Quantity(41.3)));
}
Aggregations