use of org.activityinfo.model.type.number.QuantityType in project activityinfo by bedatadriven.
the class MySqlRecordHistoryBuilder method computeDeltas.
private List<RecordDelta> computeDeltas(FormClass formClass, FormField subFormField, List<RecordVersion> versions, Map<ResourceId, String> monthlyFieldLabels) {
List<RecordDelta> deltas = new ArrayList<>();
Map<ResourceId, Map<ResourceId, FieldValue>> currentStateMap = new HashMap<>();
for (RecordVersion version : versions) {
RecordDelta delta = new RecordDelta();
delta.version = version;
delta.subFormField = subFormField;
Map<ResourceId, FieldValue> currentState = currentStateMap.get(version.getRecordId());
if (currentState == null) {
// Initialize our state map for this record
currentStateMap.put(version.getRecordId(), new HashMap<>(delta.version.getValues()));
} else {
// version.
for (FormField field : formClass.getFields()) {
if (version.getValues().containsKey(field.getId())) {
FieldValue oldValue = currentState.get(field.getId());
FieldValue newValue = version.getValues().get(field.getId());
if (!Objects.equals(oldValue, newValue)) {
FieldDelta fieldDelta = new FieldDelta();
fieldDelta.field = field;
fieldDelta.oldValue = oldValue;
fieldDelta.newValue = newValue;
if (!Strings.isNullOrEmpty(version.getSubformKey())) {
// subforms
fieldDelta.subFormKey = version.getSubformKey();
fieldDelta.subFormKind = version.getSubformKind();
}
delta.changes.add(fieldDelta);
}
currentState.put(field.getId(), newValue);
}
}
// special handling for old legacy history
for (Map.Entry<ResourceId, FieldValue> entry : version.getValues().entrySet()) {
ResourceId fieldId = entry.getKey();
String fieldIdAsString = fieldId.asString();
if (fieldIdAsString.startsWith("I") && fieldIdAsString.contains("M")) {
// e.g. I309566527M2016-8
int idInt = CuidAdapter.getLegacyIdFromCuid(fieldIdAsString.substring(fieldIdAsString.indexOf("I"), fieldIdAsString.indexOf("M")));
ResourceId id = CuidAdapter.indicatorField(idInt);
if (monthlyFieldLabels.containsKey(id)) {
// check to ensure field still exists
FieldValue oldValue = currentState.get(fieldId);
FieldValue newValue = version.getValues().get(fieldId);
if (!Objects.equals(oldValue, newValue)) {
String month = fieldIdAsString.substring(fieldIdAsString.indexOf("M") + 1);
FieldDelta fieldDelta = new FieldDelta();
fieldDelta.field = new FormField(fieldId);
fieldDelta.field.setLabel(Strings.nullToEmpty(monthlyFieldLabels.get(id)) + " (" + month + ")");
fieldDelta.field.setType(new QuantityType());
fieldDelta.oldValue = oldValue;
fieldDelta.newValue = newValue;
delta.changes.add(fieldDelta);
}
currentState.put(fieldId, newValue);
}
}
}
}
deltas.add(delta);
}
return deltas;
}
use of org.activityinfo.model.type.number.QuantityType in project activityinfo by bedatadriven.
the class HrdCatalogTest method simpleFormTest.
@Test
public void simpleFormTest() {
ResourceId collectionId = ResourceId.generateId();
ResourceId villageField = ResourceId.valueOf("FV");
ResourceId countField = ResourceId.valueOf("FC");
FormClass formClass = new FormClass(collectionId);
formClass.setParentFormId(ResourceId.valueOf("foo"));
formClass.setLabel("NFI Distributions");
formClass.addField(villageField).setLabel("Village name").setCode("VILLAGE").setType(TextType.SIMPLE);
formClass.addField(countField).setLabel("Number of Beneficiaries").setCode("BENE").setType(new QuantityType("Beneficiaries"));
HrdStorageProvider catalog = new HrdStorageProvider();
catalog.create(formClass);
Optional<FormStorage> storage = catalog.getForm(collectionId);
assertTrue(storage.isPresent());
TypedRecordUpdate village1 = new TypedRecordUpdate();
village1.setUserId(userId);
village1.setRecordId(ResourceId.generateSubmissionId(formClass));
village1.set(villageField, TextValue.valueOf("Rutshuru"));
village1.set(countField, new Quantity(1000));
TypedRecordUpdate village2 = new TypedRecordUpdate();
village2.setUserId(userId);
village2.setRecordId(ResourceId.generateSubmissionId(formClass));
village2.set(villageField, TextValue.valueOf("Beni"));
village2.set(countField, new Quantity(230));
storage.get().add(village1);
storage.get().add(village2);
QueryModel queryModel = new QueryModel(collectionId);
queryModel.selectResourceId().as("id");
queryModel.selectField("VILLAGE").as("village");
queryModel.selectField("BENE").as("family_count");
queryModel.selectExpr("BENE*5").as("individual_count");
ColumnSetBuilder builder = new ColumnSetBuilder(catalog, new NullFormSupervisor());
ColumnSet columnSet = builder.build(queryModel);
System.out.println(columnSet);
assertThat(columnSet.getNumRows(), equalTo(2));
List<RecordVersion> versions1 = ((VersionedFormStorage) storage.get()).getVersions(village1.getRecordId());
assertThat(versions1, hasSize(1));
RecordVersion version = versions1.get(0);
assertThat(version.getRecordId(), equalTo(village1.getRecordId()));
assertThat(version.getUserId(), equalTo((long) userId));
assertThat(version.getType(), equalTo(RecordChangeType.CREATED));
}
use of org.activityinfo.model.type.number.QuantityType in project activityinfo by bedatadriven.
the class HrdCatalogTest method versionRangeTest.
@Test
public void versionRangeTest() {
ResourceId collectionId = ResourceId.generateId();
ResourceId villageField = ResourceId.valueOf("FV");
ResourceId countField = ResourceId.valueOf("FC");
FormClass formClass = new FormClass(collectionId);
formClass.setParentFormId(ResourceId.valueOf("foo"));
formClass.setLabel("NFI Distributions");
formClass.addField(villageField).setLabel("Village name").setCode("VILLAGE").setType(TextType.SIMPLE);
formClass.addField(countField).setLabel("Number of Beneficiaries").setCode("BENE").setType(new QuantityType("Beneficiaries"));
HrdStorageProvider catalog = new HrdStorageProvider();
catalog.create(formClass);
VersionedFormStorage formStorage = (VersionedFormStorage) catalog.getForm(collectionId).get();
// Initially, with no records added, the form should be at version 1
// and the version range (0, 1] should be empty.
assertThat(formStorage.cacheVersion(), equalTo(1L));
FormSyncSet updatedRecords = formStorage.getVersionRange(0, 1L, resourceId -> true);
assertTrue(updatedRecords.isEmpty());
// Add a new record
TypedRecordUpdate firstUpdate = new TypedRecordUpdate();
firstUpdate.setUserId(1);
firstUpdate.setRecordId(ResourceId.generateId());
firstUpdate.set(villageField, TextValue.valueOf("Goma"));
catalog.getForm(collectionId).get().add(firstUpdate);
// Verify that the version is incremented and the version range
// (0, 2] includes our new record
assertThat(formStorage.cacheVersion(), equalTo(2L));
FormSyncSet updated = formStorage.getVersionRange(0, 2L, resourceId -> true);
assertThat(updated.getUpdatedRecordCount(), equalTo(1));
// Update the first record and add a new one
TypedRecordUpdate secondUpdate = new TypedRecordUpdate();
secondUpdate.setUserId(1);
secondUpdate.setRecordId(ResourceId.generateId());
secondUpdate.set(villageField, TextValue.valueOf("Rutshuru"));
catalog.getForm(collectionId).get().add(firstUpdate);
// Verify that the version is incremented and the version range
// (1, 2] includes our new record
assertThat(formStorage.cacheVersion(), equalTo(3L));
updated = formStorage.getVersionRange(2L, 3L, resourceId -> true);
assertThat(updated.getUpdatedRecordCount(), equalTo(1));
}
use of org.activityinfo.model.type.number.QuantityType in project activityinfo by bedatadriven.
the class UpdaterTest method missingValue.
@Test
public void missingValue() 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", Json.createNull());
JsonValue change = createObject();
change.put("recordId", "A");
change.put("formId", "XYZ123");
change.put("fields", fields);
TypedRecordUpdate update = Updater.parseChange(formClass, change, userId);
assertTrue(update.getChangedFieldValues().containsKey(fieldId));
}
use of org.activityinfo.model.type.number.QuantityType in project activityinfo by bedatadriven.
the class XFormResourceTest method invalidExpr.
@Test
@OnDataSet("/dbunit/invalid-relevance.db.xml")
public void invalidExpr() throws Exception {
setUser(USER_ID);
FormClass formClass = resourceLocator.getFormClass(CuidAdapter.activityFormClass(ACTIVITY_ID));
FormField field = new FormField(ResourceId.generateFieldId(QuantityType.TYPE_CLASS));
field.setLabel("Question");
field.setType(new QuantityType());
field.setRelevanceConditionExpression("DELETED_FIELD=1");
formClass.addElement(field);
resourceLocator.persist(formClass);
Response form = this.formResource.form(ACTIVITY_ID);
validate(form);
}
Aggregations