Search in sources :

Example 1 with TypedRecordUpdate

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));
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) QuantityType(org.activityinfo.model.type.number.QuantityType) FormClass(org.activityinfo.model.form.FormClass) JsonValue(org.activityinfo.json.JsonValue) FormField(org.activityinfo.model.form.FormField) TypedRecordUpdate(org.activityinfo.store.spi.TypedRecordUpdate) Test(org.junit.Test)

Example 2 with TypedRecordUpdate

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

Example 3 with TypedRecordUpdate

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)));
}
Also used : SerialNumber(org.activityinfo.model.type.SerialNumber) FormClass(org.activityinfo.model.form.FormClass) JsonValue(org.activityinfo.json.JsonValue) FieldValue(org.activityinfo.model.type.FieldValue) FormInstance(org.activityinfo.model.form.FormInstance) FormField(org.activityinfo.model.form.FormField) SerialNumberType(org.activityinfo.model.type.SerialNumberType) TypedRecordUpdate(org.activityinfo.store.spi.TypedRecordUpdate) Test(org.junit.Test)

Example 4 with TypedRecordUpdate

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)));
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) QuantityType(org.activityinfo.model.type.number.QuantityType) FormClass(org.activityinfo.model.form.FormClass) JsonValue(org.activityinfo.json.JsonValue) Quantity(org.activityinfo.model.type.number.Quantity) FieldValue(org.activityinfo.model.type.FieldValue) FormField(org.activityinfo.model.form.FormField) TypedRecordUpdate(org.activityinfo.store.spi.TypedRecordUpdate) Test(org.junit.Test)

Example 5 with TypedRecordUpdate

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)));
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) QuantityType(org.activityinfo.model.type.number.QuantityType) FormClass(org.activityinfo.model.form.FormClass) JsonValue(org.activityinfo.json.JsonValue) Quantity(org.activityinfo.model.type.number.Quantity) FieldValue(org.activityinfo.model.type.FieldValue) FormField(org.activityinfo.model.form.FormField) TypedRecordUpdate(org.activityinfo.store.spi.TypedRecordUpdate) Test(org.junit.Test)

Aggregations

TypedRecordUpdate (org.activityinfo.store.spi.TypedRecordUpdate)6 Test (org.junit.Test)6 FormClass (org.activityinfo.model.form.FormClass)5 FormField (org.activityinfo.model.form.FormField)5 JsonValue (org.activityinfo.json.JsonValue)4 ResourceId (org.activityinfo.model.resource.ResourceId)4 Quantity (org.activityinfo.model.type.number.Quantity)4 QuantityType (org.activityinfo.model.type.number.QuantityType)4 FieldValue (org.activityinfo.model.type.FieldValue)3 FormInstance (org.activityinfo.model.form.FormInstance)1 ColumnSet (org.activityinfo.model.query.ColumnSet)1 QueryModel (org.activityinfo.model.query.QueryModel)1 SerialNumber (org.activityinfo.model.type.SerialNumber)1 SerialNumberType (org.activityinfo.model.type.SerialNumberType)1 EnumValue (org.activityinfo.model.type.enumerated.EnumValue)1 CalculatedFieldType (org.activityinfo.model.type.expr.CalculatedFieldType)1 GeoPoint (org.activityinfo.model.type.geo.GeoPoint)1 SubFormReferenceType (org.activityinfo.model.type.subform.SubFormReferenceType)1 ColumnSetBuilder (org.activityinfo.store.query.server.ColumnSetBuilder)1 Updater (org.activityinfo.store.query.server.Updater)1