Search in sources :

Example 26 with EnumType

use of org.activityinfo.model.type.enumerated.EnumType in project activityinfo by bedatadriven.

the class CloneDatabaseHandler method cloneEnumType.

/**
 * Copies an {@link EnumType}, generating a new unique identifier each
 * enumerated item, since the MySQL backend requires them to be globally unique.
 */
private FieldType cloneEnumType(EnumType sourceEnumType) {
    List<EnumItem> targetValues = Lists.newArrayList();
    for (EnumItem sourceValue : sourceEnumType.getValues()) {
        ResourceId targetValueId = newEnumId(sourceValue);
        targetValues.add(new EnumItem(targetValueId, sourceValue.getLabel()));
        typeIdMapping.put(sourceValue.getId(), targetValueId);
    }
    return new EnumType(sourceEnumType.getCardinality(), targetValues);
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) EnumType(org.activityinfo.model.type.enumerated.EnumType) EnumItem(org.activityinfo.model.type.enumerated.EnumItem)

Example 27 with EnumType

use of org.activityinfo.model.type.enumerated.EnumType in project activityinfo by bedatadriven.

the class RelevanceExpressionTest method createFormClass.

private static FormClass createFormClass() {
    EnumItem male = new EnumItem(ResourceId.generateId(), "Male");
    EnumItem female = new EnumItem(ResourceId.generateId(), "Female");
    EnumItem pregnantYes = new EnumItem(ResourceId.generateId(), "Yes");
    EnumItem pregnantNo = new EnumItem(ResourceId.generateId(), "No");
    FormField genderField = new FormField(GENDER_FIELD_ID);
    genderField.setLabel("Gender");
    genderField.setType(new EnumType(Cardinality.SINGLE, Arrays.asList(male, female)));
    FormField pregnantField = new FormField(PREGNANT_FIELD_ID);
    pregnantField.setLabel("are you currently pregnant?");
    pregnantField.setType(new EnumType(Cardinality.SINGLE, Arrays.asList(pregnantYes, pregnantNo)));
    FormField textField = new FormField(TEXT_FIELD_ID);
    textField.setLabel("Text");
    textField.setType(TextType.SIMPLE);
    FormField quantityField = new FormField(QUANTITY_FIELD_ID);
    quantityField.setLabel("Quantity");
    quantityField.setType(QuantityType.TYPE_CLASS.createType());
    final FormClass formClass = new FormClass(CuidAdapter.activityFormClass(1));
    formClass.addElement(genderField);
    formClass.addElement(pregnantField);
    formClass.addElement(textField);
    formClass.addElement(quantityField);
    return formClass;
}
Also used : EnumType(org.activityinfo.model.type.enumerated.EnumType) FormClass(org.activityinfo.model.form.FormClass) EnumItem(org.activityinfo.model.type.enumerated.EnumItem) FormField(org.activityinfo.model.form.FormField)

Example 28 with EnumType

use of org.activityinfo.model.type.enumerated.EnumType in project activityinfo by bedatadriven.

the class SkipExpressionTest method createFormClass.

private static FormClass createFormClass() {
    EnumItem male = new EnumItem(ResourceId.generateId(), "Male");
    EnumItem female = new EnumItem(ResourceId.generateId(), "Female");
    EnumItem pregnantYes = new EnumItem(ResourceId.generateId(), "Yes");
    EnumItem pregnantNo = new EnumItem(ResourceId.generateId(), "No");
    FormField genderField = new FormField(GENDER_FIELD_ID);
    genderField.setLabel("Gender");
    genderField.setType(new EnumType(Cardinality.SINGLE, Arrays.asList(male, female)));
    FormField pregnantField = new FormField(PREGNANT_FIELD_ID);
    pregnantField.setLabel("are you currently pregnant?");
    pregnantField.setType(new EnumType(Cardinality.SINGLE, Arrays.asList(pregnantYes, pregnantNo)));
    FormField textField = new FormField(TEXT_FIELD_ID);
    textField.setLabel("Text");
    textField.setType(TextType.SIMPLE);
    FormField quantityField = new FormField(QUANTITY_FIELD_ID);
    quantityField.setLabel("Quantity");
    quantityField.setType(QuantityType.TYPE_CLASS.createType());
    final FormClass formClass = new FormClass(CuidAdapter.activityFormClass(1));
    formClass.addElement(genderField);
    formClass.addElement(pregnantField);
    formClass.addElement(textField);
    formClass.addElement(quantityField);
    return formClass;
}
Also used : EnumType(org.activityinfo.model.type.enumerated.EnumType) FormClass(org.activityinfo.model.form.FormClass) EnumItem(org.activityinfo.model.type.enumerated.EnumItem) FormField(org.activityinfo.model.form.FormField)

Example 29 with EnumType

use of org.activityinfo.model.type.enumerated.EnumType 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)

Example 30 with EnumType

use of org.activityinfo.model.type.enumerated.EnumType in project activityinfo by bedatadriven.

the class ActivityTest method createAttributeGroup.

@Test
public void createAttributeGroup() {
    FormClass formClass = assertResolves(locator.getFormClass(CuidAdapter.activityFormClass(1)));
    FormField newField = new FormField(ResourceId.generateFieldId(EnumType.TYPE_CLASS));
    newField.setLabel("New Group");
    EnumItem yes = new EnumItem(EnumItem.generateId(), "Yes");
    EnumItem no = new EnumItem(EnumItem.generateId(), "No");
    newField.setType(new EnumType(Cardinality.SINGLE, Arrays.asList(yes, no)));
    formClass.getElements().add(newField);
    locator.persist(formClass);
    // verify that it appears as attribute group
    ActivityFormDTO activity = getActivity(1);
    AttributeGroupDTO group = findGroup(activity, "New Group");
    assertThat(group.isMultipleAllowed(), equalTo(false));
    assertThat(group.getAttributes(), hasSize(2));
    assertThat(group.getAttributes().get(0), hasProperty("name", Matchers.equalTo("Yes")));
    assertThat(group.getAttributes().get(1), hasProperty("name", Matchers.equalTo("No")));
    // Now update the same attribute group and a value
    newField.setLabel("Do you like ice cream?");
    yes.setLabel("Oui");
    no.setLabel("Non");
    locator.persist(formClass);
    group = findGroup(getActivity(1), "Do you like ice cream?");
    assertThat(group.isMultipleAllowed(), equalTo(false));
    assertThat(group.getAttributes(), contains(hasProperty("name", Matchers.equalTo("Oui")), hasProperty("name", Matchers.equalTo("Non"))));
    // Remove one of our new enum values
    newField.setType(new EnumType(Cardinality.SINGLE, Arrays.asList(yes)));
    locator.persist(formClass);
    group = findGroup(getActivity(1), "Do you like ice cream?");
    assertThat(group.isMultipleAllowed(), equalTo(false));
    assertThat(group.getAttributes(), contains(hasProperty("name", Matchers.equalTo("Oui"))));
}
Also used : CuidAdapter.activityFormClass(org.activityinfo.model.legacy.CuidAdapter.activityFormClass) EnumType(org.activityinfo.model.type.enumerated.EnumType) EnumItem(org.activityinfo.model.type.enumerated.EnumItem) Test(org.junit.Test)

Aggregations

EnumType (org.activityinfo.model.type.enumerated.EnumType)39 EnumItem (org.activityinfo.model.type.enumerated.EnumItem)30 FormField (org.activityinfo.model.form.FormField)14 FormClass (org.activityinfo.model.form.FormClass)11 ResourceId (org.activityinfo.model.resource.ResourceId)7 QuantityType (org.activityinfo.model.type.number.QuantityType)7 Test (org.junit.Test)7 CalculatedFieldType (org.activityinfo.model.type.expr.CalculatedFieldType)5 SubFormReferenceType (org.activityinfo.model.type.subform.SubFormReferenceType)5 EnumValue (org.activityinfo.model.type.enumerated.EnumValue)4 CuidAdapter.activityFormClass (org.activityinfo.model.legacy.CuidAdapter.activityFormClass)3 JsonValue (org.activityinfo.json.JsonValue)2 CreateResult (org.activityinfo.legacy.shared.command.result.CreateResult)2 ColumnView (org.activityinfo.model.query.ColumnView)2 Cardinality (org.activityinfo.model.type.Cardinality)2 FieldType (org.activityinfo.model.type.FieldType)2 FieldValue (org.activityinfo.model.type.FieldValue)2 NarrativeType (org.activityinfo.model.type.NarrativeType)2 ReferenceType (org.activityinfo.model.type.ReferenceType)2 TextType (org.activityinfo.model.type.primitive.TextType)2