use of org.activityinfo.model.type.enumerated.EnumValue in project activityinfo by bedatadriven.
the class MySqlUpdateTest method testSingleSiteResource.
@Test
public void testSingleSiteResource() throws IOException {
int databaseId = 1;
ResourceId formId = CuidAdapter.activityFormClass(1);
TypedRecordUpdate update = new TypedRecordUpdate();
update.setUserId(userId);
update.setFormId(formId);
update.setRecordId(cuid(SITE_DOMAIN, 1));
update.set(field(formId, PARTNER_FIELD), CuidAdapter.partnerRef(databaseId, 2));
update.set(indicatorField(1), new Quantity(900));
update.set(attributeGroupField(1), new EnumValue(attributeId(CATASTROPHE_NATURELLE_ID)));
Updater updater = updater();
updater.execute(update);
query(CuidAdapter.activityFormClass(1), "_id", "partner", "BENE", "cause");
assertThat(column("_id"), hasValues(cuid(SITE_DOMAIN, 1), cuid(SITE_DOMAIN, 2), cuid(SITE_DOMAIN, 3)));
assertThat(column("partner"), hasValues(partnerRecordId(2), partnerRecordId(1), partnerRecordId(2)));
assertThat(column("BENE"), hasValues(900, 3600, 10000));
assertThat(column("cause"), hasValues("Catastrophe Naturelle", "Deplacement", "Catastrophe Naturelle"));
}
use of org.activityinfo.model.type.enumerated.EnumValue in project activityinfo by bedatadriven.
the class MySqlUpdateTest method addNewAttributes.
@Test
public void addNewAttributes() {
KeyGenerator generator = new KeyGenerator();
int activityId = generator.generateInt();
EnumType enumType = new EnumType(Cardinality.SINGLE, new EnumItem(EnumItem.generateId(), "A"), new EnumItem(EnumItem.generateId(), "B"));
FormField selectField = new FormField(ResourceId.generateFieldId(EnumType.TYPE_CLASS)).setType(enumType).setLabel("Select");
FormClass formClass = new FormClass(CuidAdapter.activityFormClass(activityId));
formClass.setDatabaseId(1);
formClass.setLabel("New Form");
formClass.addElement(selectField);
catalog.createOrUpdateFormSchema(formClass);
System.out.println("Created activity " + activityId);
// Now change the enum items
EnumType updatedType = new EnumType(Cardinality.SINGLE, new EnumItem(EnumItem.generateId(), "C"), new EnumItem(EnumItem.generateId(), "D"));
selectField.setType(updatedType);
newRequest();
catalog.createOrUpdateFormSchema(formClass);
newRequest();
// Now try to save a new instance with the value
FieldValue valueC = new EnumValue(updatedType.getValues().get(0).getId());
FormInstance newRecord = new FormInstance(CuidAdapter.generateSiteCuid(), formClass.getId());
newRecord.set(selectField.getId(), new EnumValue(updatedType.getValues().get(0).getId()));
newRecord.set(CuidAdapter.partnerField(activityId), CuidAdapter.partnerRef(1, 1));
executeUpdate(newRecord);
// Ensure that the select field has been saved
FormRecord saved = catalog.getForm(formClass.getId()).get().get(newRecord.getId()).get();
FormInstance savedInstance = FormInstance.toFormInstance(formClass, saved);
assertThat(savedInstance.get(selectField.getId()), equalTo(valueC));
}
use of org.activityinfo.model.type.enumerated.EnumValue in project activityinfo by bedatadriven.
the class RadioGroupWidget method init.
@Override
public void init(FieldValue value) {
EnumValue enumValue = (EnumValue) value;
CheckBox checkBox = radios.get(enumValue.getValueId());
checkBox.setValue(true);
}
use of org.activityinfo.model.type.enumerated.EnumValue in project activityinfo by bedatadriven.
the class CheckBoxGroupWidget method init.
@Override
public void init(FieldValue value) {
EnumValue enumValue = (EnumValue) value;
for (ResourceId itemId : enumValue.getResourceIds()) {
CheckBox checkBox = checkBoxes.get(itemId);
checkBox.setValue(true);
}
}
use of org.activityinfo.model.type.enumerated.EnumValue in project activityinfo by bedatadriven.
the class EnumFieldImporter method persistSingleValuedEnum.
private boolean persistSingleValuedEnum(SourceRow row, FormInstance instance) {
if (validateSingleValuedEnum(row).isPersistable()) {
final Set<ResourceId> result = Sets.newHashSet();
String value = sources.get(0).getValue(row);
for (EnumItem enumItem : enumType.getValues()) {
if (enumItem.getLabel().equalsIgnoreCase(value)) {
result.add(enumItem.getId());
break;
}
}
if (!result.isEmpty()) {
instance.set(targets.get(0).getFormField().getId(), new EnumValue(result));
return true;
}
}
return false;
}
Aggregations