Search in sources :

Example 36 with EnumType

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

the class ActivityUpdater method insertAttributeGroup.

private void insertAttributeGroup(FormField formField, int groupSortOrder) {
    SqlInsert insert;
    int groupId = CuidAdapter.getLegacyIdFromCuid(formField.getId());
    EnumType type = (EnumType) formField.getType();
    insert = SqlInsert.insertInto("attributegroup");
    insert.value("AttributeGroupId", groupId);
    insert.value("multipleAllowed", type.getCardinality() == Cardinality.MULTIPLE);
    insert.value("sortOrder", groupSortOrder);
    insert.value("mandatory", formField.isRequired());
    insert.value("name", formField.getLabel(), 255);
    insert.execute(executor);
    insert = SqlInsert.insertInto("attributegroupinactivity");
    insert.value("attributeGroupId", groupId);
    insert.value("activityId", activityId);
    insert.execute(executor);
    int attributeSortOrder = 1;
    for (EnumItem enumItem : type.getValues()) {
        insert = SqlInsert.insertInto("attribute");
        insert.value("attributeId", CuidAdapter.getLegacyIdFromCuid(enumItem.getId()));
        insert.value("name", enumItem.getLabel(), 255);
        insert.value("attributeGroupId", groupId);
        insert.value("sortOrder", attributeSortOrder);
        insert.execute(executor);
        attributeSortOrder++;
    }
}
Also used : EnumType(org.activityinfo.model.type.enumerated.EnumType) EnumItem(org.activityinfo.model.type.enumerated.EnumItem)

Example 37 with EnumType

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

the class ActivityUpdater method updateAttributeRows.

private void updateAttributeRows(ActivityField existingField, FormField formField) {
    EnumType existingType = (EnumType) existingField.getFormField().getType();
    EnumType updatedType = (EnumType) formField.getType();
    Map<ResourceId, EnumItem> existingItems = new HashMap<>();
    Set<ResourceId> presentItems = new HashSet<>();
    for (EnumItem enumItem : existingType.getValues()) {
        existingItems.put(enumItem.getId(), enumItem);
    }
    int sortOrder = 1;
    for (EnumItem updatedItem : updatedType.getValues()) {
        presentItems.add(updatedItem.getId());
        EnumItem existingItem = existingItems.get(updatedItem.getId());
        if (existingItem == null) {
            SqlInsert insert = SqlInsert.insertInto("attribute");
            insert.value("attributeId", CuidAdapter.getLegacyIdFromCuid(updatedItem.getId()));
            insert.value("attributeGroupId", existingField.getId());
            insert.value("name", updatedItem.getLabel(), 255);
            insert.value("sortOrder", sortOrder);
            insert.execute(executor);
        } else {
            SqlUpdate update = SqlUpdate.update("attribute");
            update.where("attributeId", CuidAdapter.getLegacyIdFromCuid(updatedItem.getId()));
            update.setIfChanged("name", existingItem.getLabel(), updatedItem.getLabel(), 255);
            update.setIfChanged("sortOrder", existingField.getSortOrder(), sortOrder);
            update.execute(executor);
        }
        sortOrder++;
    }
    for (EnumItem existingItem : existingType.getValues()) {
        if (!presentItems.contains(existingItem.getId())) {
            SqlUpdate delete = SqlUpdate.update("attribute");
            delete.where("attributeId", CuidAdapter.getLegacyIdFromCuid(existingItem.getId()));
            delete.set("dateDeleted", new Date());
            delete.set("deleted", 1);
            delete.execute(executor);
        }
    }
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) EnumType(org.activityinfo.model.type.enumerated.EnumType) EnumItem(org.activityinfo.model.type.enumerated.EnumItem)

Example 38 with EnumType

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

the class EnumImportStrategy method createImporter.

@Override
public FieldImporter createImporter(FormTree.Node node, Map<TargetSiteId, ColumnAccessor> mappings, ImportModel model) {
    EnumType type = (EnumType) node.getType();
    List<ColumnAccessor> sourceColumns = Lists.newArrayList();
    if (type.getCardinality() == Cardinality.SINGLE) {
        sourceColumns.add(mappings.get(VALUE));
    } else {
        for (EnumItem item : type.getValues()) {
            ColumnAccessor accessor = mappings.get(new TargetSiteId(item.getId().asString()));
            if (accessor == null) {
                accessor = new EmptyColumn(item.getLabel());
            }
            sourceColumns.add(accessor);
        }
    }
    return new EnumFieldImporter(sourceColumns, getImportSites(node), type);
}
Also used : EnumType(org.activityinfo.model.type.enumerated.EnumType) EnumItem(org.activityinfo.model.type.enumerated.EnumItem)

Example 39 with EnumType

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

the class XPathBuilderTest method createFormClass.

private static TFormClass 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 new TFormClass(formClass);
}
Also used : EnumType(org.activityinfo.model.type.enumerated.EnumType) FormClass(org.activityinfo.model.form.FormClass) TFormClass(org.activityinfo.model.form.TFormClass) TFormClass(org.activityinfo.model.form.TFormClass) EnumItem(org.activityinfo.model.type.enumerated.EnumItem) FormField(org.activityinfo.model.form.FormField)

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