Search in sources :

Example 21 with QuantityType

use of org.activityinfo.model.type.number.QuantityType in project activityinfo by bedatadriven.

the class StatFunction method computeUnits.

protected final String computeUnits(List<FieldType> argumentTypes) {
    Set<String> units = Sets.newHashSet();
    for (FieldType argumentType : argumentTypes) {
        if (argumentType instanceof QuantityType) {
            QuantityType quantityType = (QuantityType) argumentType;
            units.add(quantityType.getUnits());
        }
    }
    if (units.size() == 1) {
        return units.iterator().next();
    } else {
        return QuantityType.UNKNOWN_UNITS;
    }
}
Also used : QuantityType(org.activityinfo.model.type.number.QuantityType) FieldType(org.activityinfo.model.type.FieldType)

Example 22 with QuantityType

use of org.activityinfo.model.type.number.QuantityType in project activityinfo by bedatadriven.

the class QuantityTypeTest method deserialization.

/**
 * Ensure QuantityType is parsed correctly from JSON.
 * In particular, ensure that the "aggregation" field is correctly initialised from a legacy schema with no
 * aggregation field within Quantity JSON element.
 *
 * @throws IOException
 */
@Test
public void deserialization() throws IOException {
    FormClass formClass = parseResource();
    ResourceId quantityFieldId = ResourceId.valueOf("i0000006090");
    FormField quantityField = formClass.getField(quantityFieldId);
    // Quantity Checks
    assertThat(quantityField.getLabel(), equalTo("Number of water points constructed"));
    assertThat(quantityField.isRequired(), is(false));
    assertThat(quantityField.isVisible(), is(true));
    // QuantityType Checks
    assertThat(quantityField.getType(), instanceOf(QuantityType.class));
    QuantityType quantityType = (QuantityType) quantityField.getType();
    assertThat(quantityType.getUnits(), equalTo("Waterpoints"));
    assertThat(quantityType.getAggregation(), is(notNullValue()));
    assertThat(quantityType.getAggregation(), equalTo(QuantityType.Aggregation.SUM));
    assertThat(quantityType.getAggregation().ordinal(), equalTo(0));
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) QuantityType(org.activityinfo.model.type.number.QuantityType) FormClass(org.activityinfo.model.form.FormClass) FormField(org.activityinfo.model.form.FormField) Test(org.junit.Test)

Example 23 with QuantityType

use of org.activityinfo.model.type.number.QuantityType in project activityinfo by bedatadriven.

the class HrdCatalogTest method subFormTest.

@Test
public void subFormTest() {
    // Typical scenario with a household interview form
    // and a repeating househould member form
    FormClass hhForm = new FormClass(ResourceId.generateId());
    FormClass memberForm = new FormClass(ResourceId.generateId());
    memberForm.setParentFormId(hhForm.getId());
    hhForm.setParentFormId(ResourceId.valueOf("foo"));
    hhForm.setLabel("Household interview");
    FormField hhIdField = hhForm.addField().setLabel("Household ID").setType(TextType.SIMPLE);
    hhForm.addField().setLabel("Household Memmbers").setType(new SubFormReferenceType(memberForm.getId()));
    memberForm.setLabel("Household Members");
    FormField nameField = memberForm.addField().setLabel("Name").setType(TextType.SIMPLE);
    FormField ageField = memberForm.addField().setLabel("Age").setType(new QuantityType("years"));
    HrdStorageProvider catalog = new HrdStorageProvider();
    catalog.create(hhForm);
    catalog.create(memberForm);
    TypedRecordUpdate hh1 = new TypedRecordUpdate();
    hh1.setUserId(userId);
    hh1.setRecordId(ResourceId.generateSubmissionId(hhForm));
    hh1.set(hhIdField.getId(), TextValue.valueOf("HH1"));
    TypedRecordUpdate hh2 = new TypedRecordUpdate();
    hh2.setUserId(userId);
    hh2.setRecordId(ResourceId.generateSubmissionId(hhForm));
    hh2.set(hhIdField.getId(), TextValue.valueOf("HH2"));
    TypedRecordUpdate father1 = new TypedRecordUpdate();
    father1.setUserId(userId);
    father1.setRecordId(ResourceId.generateSubmissionId(memberForm));
    father1.setParentId(hh1.getRecordId());
    father1.set(nameField.getId(), TextValue.valueOf("Homer"));
    father1.set(ageField.getId(), new Quantity(40));
    TypedRecordUpdate father2 = new TypedRecordUpdate();
    father2.setUserId(userId);
    father2.setRecordId(ResourceId.generateSubmissionId(memberForm));
    father2.setParentId(hh2.getRecordId());
    father2.set(nameField.getId(), TextValue.valueOf("Ned"));
    father2.set(ageField.getId(), new Quantity(41));
    Optional<FormStorage> hhCollection = catalog.getForm(hhForm.getId());
    assertTrue(hhCollection.isPresent());
    hhCollection.get().add(hh1);
    hhCollection.get().add(hh2);
    Optional<FormStorage> memberCollection = catalog.getForm(memberForm.getId());
    assertTrue(memberCollection.isPresent());
    memberCollection.get().add(father1);
    memberCollection.get().add(father2);
    QueryModel queryModel = new QueryModel(memberForm.getId());
    queryModel.selectResourceId().as("id");
    queryModel.selectField("Household ID").as("hh_id");
    queryModel.selectField("Name").as("member_name");
    queryModel.selectField("Age").as("member_age");
    ColumnSetBuilder builder = new ColumnSetBuilder(catalog, new NullFormSupervisor());
    ColumnSet columnSet = builder.build(queryModel);
    System.out.println(columnSet);
    assertThat(columnSet.getNumRows(), equalTo(2));
}
Also used : Quantity(org.activityinfo.model.type.number.Quantity) ColumnSet(org.activityinfo.model.query.ColumnSet) QueryModel(org.activityinfo.model.query.QueryModel) SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) ColumnSetBuilder(org.activityinfo.store.query.server.ColumnSetBuilder) 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 24 with QuantityType

use of org.activityinfo.model.type.number.QuantityType 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 25 with QuantityType

use of org.activityinfo.model.type.number.QuantityType in project activityinfo by bedatadriven.

the class CustomerCalcIndicatorTest method createFormClass.

private FormClass createFormClass() {
    SchemaDTO schema = execute(new GetSchema());
    UserDatabaseDTO db = schema.getDatabaseById(1);
    LocationTypeDTO locType = schema.getCountryById(1).getLocationTypes().get(0);
    ActivityFormDTO act = new ActivityFormDTO();
    act.setName("Calculated indicators");
    act.setLocationType(locType);
    act.setReportingFrequency(ActivityFormDTO.REPORT_ONCE);
    CreateResult createResult = execute(CreateEntity.Activity(db, act));
    ResourceId classId = activityFormClass(createResult.getNewId());
    FormClass formClass = assertResolves(locator.getFormClass(classId));
    FormField typeField = new FormField(ResourceId.generateFieldId(EnumType.TYPE_CLASS));
    typeField.setType(new EnumType(Cardinality.SINGLE, Arrays.asList(new EnumItem(EnumItem.generateId(), "Budgeted"), new EnumItem(EnumItem.generateId(), "Spent"))));
    typeField.setLabel("Typology");
    formClass.addElement(typeField);
    FormField expField = new FormField(quantityId());
    expField.setType(new QuantityType().setUnits("currency"));
    expField.setLabel("Expenditure");
    expField.setCode("EXP");
    FormField waterAllocField = new FormField(quantityId());
    waterAllocField.setType(new QuantityType().setUnits("%"));
    waterAllocField.setLabel("Allocation watter programme");
    waterAllocField.setCode("WATER_ALLOC");
    FormField pctInitialField = new FormField(quantityId());
    pctInitialField.setType(new QuantityType().setUnits("%"));
    pctInitialField.setLabel("Initial Cost - Not specified");
    pctInitialField.setCode("PCT_INITIAL");
    FormField pctInitialHardField = new FormField(quantityId());
    pctInitialHardField.setType(new QuantityType().setUnits("%"));
    pctInitialHardField.setLabel("Initial Cost - Cap Hard");
    pctInitialHardField.setCode("PCT_INITIAL_HARD");
    FormField pctInitialSoftField = new FormField(quantityId());
    pctInitialSoftField.setType(new QuantityType().setUnits("%"));
    pctInitialSoftField.setLabel("Initial Cost - Cap Soft");
    pctInitialSoftField.setCode("PCT_INITIAL_SOFT");
    FormField pctExtensionField = new FormField(quantityId());
    pctExtensionField.setType(new QuantityType().setUnits("%"));
    pctExtensionField.setLabel("Extension Cost - Not specified");
    pctExtensionField.setCode("PCT_EXTENSION");
    FormField pctExtensionHardField = new FormField(quantityId());
    pctExtensionHardField.setType(new QuantityType().setUnits("%"));
    pctExtensionHardField.setLabel("Extension Cost - Hard");
    pctExtensionHardField.setCode("PCT_EXTENSION_HARD");
    FormField pctExtensionSoftField = new FormField(quantityId());
    pctExtensionSoftField.setType(new QuantityType().setUnits("%"));
    pctExtensionSoftField.setLabel("Extension Cost - Soft");
    pctExtensionSoftField.setCode("PCT_EXTENSION_SOFT");
    FormField pctOpField = new FormField(quantityId());
    pctOpField.setType(new QuantityType().setUnits("%"));
    pctOpField.setLabel("Operational Cost");
    pctOpField.setCode("PCT_OP");
    FormField pctMaintenanceField = new FormField(quantityId());
    pctMaintenanceField.setType(new QuantityType().setUnits("%"));
    pctMaintenanceField.setLabel("Maintenance Cost");
    pctMaintenanceField.setCode("PCT_MAINTENANCE");
    FormField pctOpMaintField = new FormField(quantityId());
    pctOpMaintField.setType(new QuantityType().setUnits("%"));
    pctOpMaintField.setLabel("Operational & Maintenance Cost");
    pctOpMaintField.setCode("PCT_OP_MAINT");
    FormField waterExpField = new FormField(quantityId());
    waterExpField.setType(new QuantityType().setUnits("%"));
    waterExpField.setLabel("Expenditure on water programme");
    waterExpField.setCode("WATER_EXP");
    waterExpField.setType(new CalculatedFieldType("{EXP}*({WATER_ALLOC}/100)"));
    FormField initialField = new FormField(quantityId());
    initialField.setType(new QuantityType().setUnits("%"));
    initialField.setLabel("Value of Initial Cost - Not specified");
    initialField.setCode("INITIAL");
    initialField.setType(new CalculatedFieldType("{WATER_EXP}*({PCT_INITIAL}/100)"));
    FormField initialHardField = new FormField(quantityId());
    initialHardField.setType(new QuantityType().setUnits("%"));
    initialHardField.setLabel("Value of Initial Cost - Cap Hard");
    initialHardField.setCode("INITIAL_HARD");
    initialHardField.setType(new CalculatedFieldType("{WATER_EXP}*({PCT_INITIAL_HARD}/100)"));
    FormField initialSoftField = new FormField(quantityId());
    initialSoftField.setType(new QuantityType().setUnits("%"));
    initialSoftField.setLabel("Value of Initial Cost – Cap Soft");
    initialSoftField.setCode("INITIAL_SOFT");
    initialSoftField.setType(new CalculatedFieldType("{WATER_EXP}*({PCT_INITIAL_SOFT}/100)"));
    FormField initialTotalField = new FormField(quantityId());
    initialTotalField.setType(new QuantityType().setUnits("%"));
    initialTotalField.setLabel("Total Value of Initial Cost");
    initialTotalField.setCode("INITIAL_TOTAL");
    initialTotalField.setType(new CalculatedFieldType("{INITIAL}+{INITIAL_HARD}+{INITIAL_SOFT}"));
    formClass.addElement(expField);
    formClass.addElement(waterAllocField);
    formClass.addElement(pctInitialField);
    formClass.addElement(pctInitialHardField);
    formClass.addElement(pctInitialSoftField);
    formClass.addElement(pctExtensionField);
    formClass.addElement(pctExtensionHardField);
    formClass.addElement(pctExtensionSoftField);
    formClass.addElement(pctOpField);
    formClass.addElement(pctMaintenanceField);
    formClass.addElement(pctOpMaintField);
    formClass.addElement(waterExpField);
    formClass.addElement(initialField);
    formClass.addElement(initialHardField);
    formClass.addElement(initialSoftField);
    formClass.addElement(initialTotalField);
    assertResolves(locator.persist(formClass));
    FormClass reform = assertResolves(locator.getFormClass(formClass.getId()));
    assertHasFieldWithLabel(reform, "Expenditure");
    assertHasFieldWithLabel(reform, "Allocation watter programme");
    assertHasFieldWithLabel(reform, "Initial Cost - Not specified");
    assertHasFieldWithLabel(reform, "Initial Cost - Cap Hard");
    assertHasFieldWithLabel(reform, "Initial Cost - Cap Soft");
    assertHasFieldWithLabel(reform, "Extension Cost - Not specified");
    assertHasFieldWithLabel(reform, "Extension Cost - Hard");
    assertHasFieldWithLabel(reform, "Extension Cost - Soft");
    assertHasFieldWithLabel(reform, "Operational Cost");
    assertHasFieldWithLabel(reform, "Maintenance Cost");
    assertHasFieldWithLabel(reform, "Operational & Maintenance Cost");
    assertHasFieldWithLabel(reform, "Expenditure on water programme");
    assertHasFieldWithLabel(reform, "Value of Initial Cost - Not specified");
    assertHasFieldWithLabel(reform, "Value of Initial Cost - Cap Hard");
    assertHasFieldWithLabel(reform, "Value of Initial Cost – Cap Soft");
    assertHasFieldWithLabel(reform, "Total Value of Initial Cost");
    return reform;
}
Also used : CalculatedFieldType(org.activityinfo.model.type.expr.CalculatedFieldType) CreateResult(org.activityinfo.legacy.shared.command.result.CreateResult) ResourceId(org.activityinfo.model.resource.ResourceId) QuantityType(org.activityinfo.model.type.number.QuantityType) FormClass(org.activityinfo.model.form.FormClass) CuidAdapter.activityFormClass(org.activityinfo.model.legacy.CuidAdapter.activityFormClass) EnumType(org.activityinfo.model.type.enumerated.EnumType) EnumItem(org.activityinfo.model.type.enumerated.EnumItem) FormField(org.activityinfo.model.form.FormField)

Aggregations

QuantityType (org.activityinfo.model.type.number.QuantityType)34 FormClass (org.activityinfo.model.form.FormClass)20 FormField (org.activityinfo.model.form.FormField)19 Test (org.junit.Test)18 ResourceId (org.activityinfo.model.resource.ResourceId)13 CalculatedFieldType (org.activityinfo.model.type.expr.CalculatedFieldType)12 EnumType (org.activityinfo.model.type.enumerated.EnumType)8 EnumItem (org.activityinfo.model.type.enumerated.EnumItem)7 Quantity (org.activityinfo.model.type.number.Quantity)7 QueryModel (org.activityinfo.model.query.QueryModel)6 SubFormReferenceType (org.activityinfo.model.type.subform.SubFormReferenceType)6 JsonValue (org.activityinfo.json.JsonValue)5 CuidAdapter.activityFormClass (org.activityinfo.model.legacy.CuidAdapter.activityFormClass)5 ColumnSet (org.activityinfo.model.query.ColumnSet)5 ColumnSetBuilder (org.activityinfo.store.query.server.ColumnSetBuilder)4 NullFormSupervisor (org.activityinfo.store.query.shared.NullFormSupervisor)4 TypedRecordUpdate (org.activityinfo.store.spi.TypedRecordUpdate)4 CreateResult (org.activityinfo.legacy.shared.command.result.CreateResult)3 FieldType (org.activityinfo.model.type.FieldType)3 TextType (org.activityinfo.model.type.primitive.TextType)3