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);
}
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;
}
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++;
}
}
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);
}
}
}
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);
}
Aggregations