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