use of org.activityinfo.model.type.FieldValue in project activityinfo by bedatadriven.
the class ResourceLocatorAdaptorTest method persistSiteWithCalculatedIndicators.
@Test
@OnDataSet("/dbunit/sites-calculated-indicators.db.xml")
public void persistSiteWithCalculatedIndicators() {
int siteId = new KeyGenerator().generateInt();
FormInstance instance = new FormInstance(CuidAdapter.cuid(SITE_DOMAIN, siteId), NFI_DIST_FORM_CLASS);
instance.set(indicatorField(1), 1);
instance.set(indicatorField(2), 2);
instance.set(locationField(NFI_DIST_ID), locationRef(VILLAGE_CLASS, 1));
instance.set(partnerField(NFI_DIST_ID), partnerRef(PEAR_DATABASE_ID, 1));
instance.set(projectField(NFI_DIST_ID), projectRef(PEAR_DATABASE_ID, 1));
instance.set(field(NFI_DIST_FORM_CLASS, START_DATE_FIELD), new LocalDate(2014, 1, 1));
instance.set(field(NFI_DIST_FORM_CLASS, END_DATE_FIELD), new LocalDate(2014, 1, 1));
instance.set(field(NFI_DIST_FORM_CLASS, COMMENT_FIELD), NarrativeValue.valueOf("My comment"));
assertResolves(locator.persist(instance));
FormInstance firstRead = assertResolves(locator.getFormInstance(NFI_DIST_FORM_CLASS, instance.getId()));
assertThat(firstRead.get(indicatorField(1)), equalTo((FieldValue) new Quantity(1)));
assertThat(firstRead.get(indicatorField(2)), equalTo((FieldValue) new Quantity(2)));
// set indicators to null
instance.set(indicatorField(1).asString(), null);
instance.set(indicatorField(2).asString(), null);
// persist it
assertResolves(locator.persist(instance));
// read from server
FormInstance secondRead = assertResolves(locator.getFormInstance(NFI_DIST_FORM_CLASS, instance.getId()));
// BENE
assertThat(secondRead.get(indicatorField(1)), nullValue());
// BACHE
assertThat(secondRead.get(indicatorField(2)), nullValue());
// // Both BENE+BACHE and BENE/BACHE should be missing, because both
// // BENE and BACHE are missing
// assertEquals(null, secondRead.getValue(path(indicatorField(11))));
// assertEquals(null, secondRead.getValue(path(indicatorField(12))));
}
use of org.activityinfo.model.type.FieldValue in project activityinfo by bedatadriven.
the class UpdaterTest method serialNumber.
@Test
public void serialNumber() throws JsonMappingException {
FormClass formClass = new FormClass(ResourceId.valueOf("FORM1"));
formClass.addField(ResourceId.valueOf("FIELD0")).setType(TextType.SIMPLE).setLabel("Province Code").setCode("PROVINCE").setRequired(true);
FormField serialNumberField = formClass.addField(ResourceId.valueOf("FIELD1")).setType(new SerialNumberType("PROVINCE", 5)).setRequired(true).setLabel("File Number").setCode("SN");
JsonValue fields = Json.createObject();
fields.put("PROVINCE", "KUNDUZ");
JsonValue change = createObject();
change.put("recordId", "A");
change.put("formId", "FORM1");
change.put("fields", fields);
TypedRecordUpdate update = Updater.parseChange(formClass, change, userId);
FormInstance effectiveRecord = updater.computeEffectiveRecord(formClass, Optional.<FormRecord>absent(), update);
updater.generateSerialNumber(formClass, serialNumberField, effectiveRecord, update);
FieldValue serialValue = update.getChangedFieldValues().get(serialNumberField.getId());
assertThat(serialValue, equalTo((FieldValue) new SerialNumber("KUNDUZ", 1)));
}
use of org.activityinfo.model.type.FieldValue in project activityinfo by bedatadriven.
the class UpdaterTest method validQuantity.
@Test
public void validQuantity() throws JsonMappingException {
ResourceId fieldId = ResourceId.valueOf("Q1");
FormClass formClass = new FormClass(ResourceId.valueOf("XYZ123"));
formClass.addElement(new FormField(fieldId).setType(new QuantityType("meters")));
JsonValue fields = createObject();
fields.put("Q1", 41.3);
JsonValue change = createObject();
change.put("recordId", "A");
change.put("formId", "XYZ123");
change.put("fields", fields);
TypedRecordUpdate update = Updater.parseChange(formClass, change, userId);
assertThat(update.getChangedFieldValues().get(fieldId), equalTo((FieldValue) new Quantity(41.3)));
}
use of org.activityinfo.model.type.FieldValue in project activityinfo by bedatadriven.
the class UpdaterTest method parsedQuantity.
@Test
public void parsedQuantity() throws JsonMappingException {
ResourceId fieldId = ResourceId.valueOf("Q1");
FormClass formClass = new FormClass(ResourceId.valueOf("XYZ123"));
formClass.addElement(new FormField(fieldId).setType(new QuantityType("meters")));
JsonValue fields = Json.createObject();
fields.put("Q1", "41.3");
JsonValue change = createObject();
change.put("recordId", "A");
change.put("formId", "XYZ123");
change.put("fields", fields);
TypedRecordUpdate update = Updater.parseChange(formClass, change, userId);
assertThat(update.getChangedFieldValues().get(fieldId), equalTo((FieldValue) new Quantity(41.3)));
}
use of org.activityinfo.model.type.FieldValue in project activityinfo by bedatadriven.
the class Updater method createOrUpdate.
private void createOrUpdate(ResourceId formId, ResourceId recordId, JsonValue jsonObject, boolean create) {
Optional<FormStorage> collection = catalog.getForm(formId);
if (!collection.isPresent()) {
throw new InvalidUpdateException("No such formId: " + formId);
}
TypedRecordUpdate update = new TypedRecordUpdate();
update.setUserId(userId);
update.setFormId(formId);
update.setRecordId(recordId);
if (jsonObject.hasKey("deleted") && !jsonObject.get("deleted").isJsonNull()) {
update.setDeleted(jsonObject.get("deleted").asBoolean());
}
if (jsonObject.hasKey("parentRecordId") && !jsonObject.get("parentRecordId").isJsonNull()) {
update.setParentId(ResourceId.valueOf(jsonObject.get("parentRecordId").asString()));
}
FormClass formClass = collection.get().getFormClass();
JsonValue fieldValues = jsonObject.get("fieldValues");
for (FormField formField : formClass.getFields()) {
if (formField.getType().isUpdatable()) {
if (fieldValues.hasKey(formField.getName())) {
JsonValue updatedValueElement = fieldValues.get(formField.getName());
FieldValue updatedValue;
if (updatedValueElement.isJsonNull()) {
updatedValue = null;
} else {
try {
updatedValue = formField.getType().parseJsonValue(updatedValueElement);
} catch (Exception e) {
e.printStackTrace();
throw new InvalidUpdateException("Could not parse updated value for field " + formField.getId() + ": " + e.getMessage());
}
}
update.getChangedFieldValues().put(formField.getId(), updatedValue);
} else if (create) {
update.getChangedFieldValues().put(formField.getId(), null);
}
}
}
executeUpdate(collection.get(), update);
}
Aggregations