Search in sources :

Example 6 with EnumType

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

the class SchemaImporterV3 method processRows.

@Override
public boolean processRows() {
    formMap.clear();
    subFormMap.clear();
    enumMap.clear();
    refMap.clear();
    fatalError = false;
    for (SourceRow row : source.getRows()) {
        try {
            FormClass parentFormClass = getFormClass(row);
            FormClass formClass = getSubFormClass(parentFormClass, row);
            String type = formFieldType.get(row);
            if (isEnum(type)) {
                addChoice(formClass, row);
            } else {
                FieldType fieldType = parseFieldType(row);
                FormField newField = addField(formClass, fieldType.getTypeClass(), row);
                newField.setType(fieldType);
                if (newField.getType() instanceof ReferenceType) {
                    refMap.put(formClass, newField);
                }
            }
        } catch (UnableToParseRowException e) {
            warnings.add(SafeHtmlUtils.fromString(e.getMessage()));
            fatalError = true;
        }
    }
    for (EnumBuilder enumBuilder : enumMap.values()) {
        enumBuilder.formField.setType(new EnumType(enumBuilder.cardinality, enumBuilder.items));
    }
    if (validationCallback != null) {
        validateReferences();
    }
    return !fatalError;
}
Also used : FormClass(org.activityinfo.model.form.FormClass) EnumType(org.activityinfo.model.type.enumerated.EnumType) SourceRow(org.activityinfo.ui.client.component.importDialog.model.source.SourceRow) FormField(org.activityinfo.model.form.FormField) SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) CalculatedFieldType(org.activityinfo.model.type.expr.CalculatedFieldType)

Example 7 with EnumType

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

the class CurlExamplesGenerator method getFormSchema.

public static Example getFormSchema() {
    FormClass exampleForm = new FormClass(ResourceId.generateId()).setDatabaseId(54).setLabel("NFI Distribution").setDescription("Form for collecting results of NFI distributions in North Kivu");
    exampleForm.addField(ResourceId.generateId()).setLabel("Date of Distribution").setType(LocalDateType.INSTANCE).setRequired(true);
    exampleForm.addField(CuidAdapter.partnerField(33)).setLabel("Partner").setType(ReferenceType.single(CuidAdapter.partnerFormId(54))).setRequired(true).setDescription("The implementing partner who conducted the distribution");
    exampleForm.addField(ResourceId.generateId()).setLabel("Donor").setType(new EnumType(Cardinality.SINGLE, new EnumItem(ResourceId.generateId(), "USAID"), new EnumItem(ResourceId.generateId(), "DFID"), new EnumItem(ResourceId.generateId(), "ECHO"))).setRequired(true);
    exampleForm.addField(ResourceId.generateId()).setLabel("Number of households receiving a kit").setType(new QuantityType("households")).setRequired(true);
    return new Example("curl https://www.activityinfo.org/form/" + exampleForm.getId() + "/schema", exampleForm.toJson());
}
Also used : QuantityType(org.activityinfo.model.type.number.QuantityType) FormClass(org.activityinfo.model.form.FormClass) EnumType(org.activityinfo.model.type.enumerated.EnumType) EnumItem(org.activityinfo.model.type.enumerated.EnumItem)

Example 8 with EnumType

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

the class AttributeGroupDTO method asFormField.

@Override
public FormField asFormField() {
    Cardinality cardinality = isMultipleAllowed() ? Cardinality.MULTIPLE : Cardinality.SINGLE;
    List<EnumItem> values = Lists.newArrayList();
    for (AttributeDTO attribute : getAttributes()) {
        values.add(new EnumItem(CuidAdapter.attributeId(attribute.getId()), attribute.getName()));
    }
    return new FormField(CuidAdapter.attributeGroupField(getId())).setLabel(getName()).setType(new EnumType(cardinality, values)).setRequired(isMandatory());
}
Also used : Cardinality(org.activityinfo.model.type.Cardinality) EnumType(org.activityinfo.model.type.enumerated.EnumType) EnumItem(org.activityinfo.model.type.enumerated.EnumItem) FormField(org.activityinfo.model.form.FormField)

Example 9 with EnumType

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

the class AttributeDimBinding method findAttributeOrder.

private Map<String, Integer> findAttributeOrder(Optional<FormField> field) {
    Map<String, Integer> map = new HashMap<>();
    if (field.isPresent() && field.get().getType() instanceof EnumType) {
        EnumType type = (EnumType) field.get().getType();
        List<EnumItem> items = type.getValues();
        for (int i = 0; i < items.size(); i++) {
            map.put(items.get(i).getLabel(), i);
        }
    }
    return map;
}
Also used : EnumType(org.activityinfo.model.type.enumerated.EnumType) EnumItem(org.activityinfo.model.type.enumerated.EnumItem)

Example 10 with EnumType

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

the class ActivityTest method orderIndicatorsActivities.

@Test
public void orderIndicatorsActivities() {
    SchemaDTO schema = execute(new GetSchema());
    UserDatabaseDTO db = schema.getDatabaseById(1);
    LocationTypeDTO locType = schema.getCountryById(1).getLocationTypes().get(0);
    ActivityFormDTO act = new ActivityFormDTO();
    act.setName("Household Survey");
    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));
    // create three new fields with an order that mixes "attributes" and "indicators"
    FormField newField = new FormField(ResourceId.generateFieldId(QuantityType.TYPE_CLASS));
    newField.setLabel("How old are you?");
    newField.setType(new QuantityType().setUnits("years"));
    formClass.addElement(newField);
    FormField newGenderField = new FormField(ResourceId.generateFieldId(EnumType.TYPE_CLASS));
    newGenderField.setLabel("Gender");
    EnumItem male = new EnumItem(EnumItem.generateId(), "Male");
    EnumItem female = new EnumItem(EnumItem.generateId(), "Female");
    newGenderField.setType(new EnumType(Cardinality.SINGLE, Arrays.asList(male, female)));
    formClass.addElement(newGenderField);
    FormField newTextField = new FormField(ResourceId.generateFieldId(TextType.TYPE_CLASS));
    newTextField.setLabel("What is your name?");
    newTextField.setType(TextType.SIMPLE);
    formClass.addElement(newTextField);
    assertResolves(locator.persist(formClass));
    TFormClass reform = new TFormClass(assertResolves(locator.getFormClass(formClass.getId())));
    System.out.println(Joiner.on("\n").join(reform.getFormClass().getFields()));
    int a = reform.indexOfField("How old are you?");
    int b = reform.indexOfField("Gender");
    int c = reform.indexOfField("What is your name?");
    assertTrue(a < b && b < c);
}
Also used : CreateResult(org.activityinfo.legacy.shared.command.result.CreateResult) ResourceId(org.activityinfo.model.resource.ResourceId) QuantityType(org.activityinfo.model.type.number.QuantityType) 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