Search in sources :

Example 36 with EnumItem

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

the class ActivityLoader method createEnumType.

private EnumType createEnumType(ResultSet rs, Map<Integer, List<EnumItem>> attributes) throws SQLException {
    Cardinality cardinality;
    if (rs.getBoolean("multipleAllowed")) {
        cardinality = Cardinality.MULTIPLE;
    } else {
        cardinality = Cardinality.SINGLE;
    }
    List<EnumItem> enumValues = attributes.get(rs.getInt("id"));
    if (enumValues == null) {
        enumValues = Lists.newArrayList();
    }
    return new EnumType(cardinality, enumValues);
}
Also used : Cardinality(org.activityinfo.model.type.Cardinality) EnumType(org.activityinfo.model.type.enumerated.EnumType) EnumItem(org.activityinfo.model.type.enumerated.EnumItem)

Example 37 with EnumItem

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

the class ActivityLoader method loadAttributes.

private Map<Integer, List<EnumItem>> loadAttributes(Set<Integer> activityIds) throws SQLException {
    Map<Integer, List<EnumItem>> attributes = Maps.newHashMap();
    String sql = "SELECT DISTINCT A.* from attribute A " + "LEFT JOIN attributegroupinactivity G on (A.attributegroupid=G.attributegroupid) " + "WHERE G.activityid IN " + idList(activityIds) + " " + "ORDER BY A.sortorder";
    try (ResultSet rs = executor.query(sql)) {
        while (rs.next()) {
            int attributeGroupId = rs.getInt("AttributeGroupId");
            List<EnumItem> values = attributes.get(attributeGroupId);
            if (values == null) {
                attributes.put(attributeGroupId, values = Lists.newArrayList());
            }
            int attributeId = rs.getInt("attributeId");
            String attributeName = rs.getString("name");
            values.add(new EnumItem(CuidAdapter.attributeId(attributeId), attributeName));
        }
    }
    return attributes;
}
Also used : ResultSet(java.sql.ResultSet) EnumItem(org.activityinfo.model.type.enumerated.EnumItem)

Example 38 with EnumItem

use of org.activityinfo.model.type.enumerated.EnumItem 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 39 with EnumItem

use of org.activityinfo.model.type.enumerated.EnumItem 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 40 with EnumItem

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

the class EnumFieldImporter method validateSingleValuedEnum.

private ValidationResult validateSingleValuedEnum(SourceRow row) {
    ColumnAccessor source = sources.get(0);
    if (source.isMissing(row)) {
        if (targets.get(0).getFormField().isRequired()) {
            return ValidationResult.error("Required value is missing");
        } else {
            return ValidationResult.MISSING;
        }
    }
    String value = source.getValue(row);
    for (EnumItem enumItem : enumType.getValues()) {
        if (enumItem.getLabel().equalsIgnoreCase(value)) {
            return ValidationResult.OK;
        }
    }
    return ValidationResult.error("Unknown value: " + value);
}
Also used : EnumItem(org.activityinfo.model.type.enumerated.EnumItem)

Aggregations

EnumItem (org.activityinfo.model.type.enumerated.EnumItem)44 EnumType (org.activityinfo.model.type.enumerated.EnumType)30 FormField (org.activityinfo.model.form.FormField)11 FormClass (org.activityinfo.model.form.FormClass)9 ResourceId (org.activityinfo.model.resource.ResourceId)9 Test (org.junit.Test)7 QuantityType (org.activityinfo.model.type.number.QuantityType)6 EnumValue (org.activityinfo.model.type.enumerated.EnumValue)5 CuidAdapter.activityFormClass (org.activityinfo.model.legacy.CuidAdapter.activityFormClass)4 CreateResult (org.activityinfo.legacy.shared.command.result.CreateResult)3 CompoundExpr (org.activityinfo.model.formula.CompoundExpr)2 SymbolNode (org.activityinfo.model.formula.SymbolNode)2 ColumnModel (org.activityinfo.model.query.ColumnModel)2 ColumnView (org.activityinfo.model.query.ColumnView)2 Cardinality (org.activityinfo.model.type.Cardinality)2 FieldValue (org.activityinfo.model.type.FieldValue)2 CalculatedFieldType (org.activityinfo.model.type.expr.CalculatedFieldType)2 SubFormReferenceType (org.activityinfo.model.type.subform.SubFormReferenceType)2 ActivityField (org.activityinfo.store.mysql.metadata.ActivityField)2 Optional (com.google.common.base.Optional)1