use of org.activityinfo.model.resource.ResourceId in project activityinfo by bedatadriven.
the class ResourceLocatorAdaptorTest method siteDeletion.
@Test
public void siteDeletion() {
QueryModel query = new QueryModel(NFI_DIST_FORM_CLASS);
query.selectResourceId().as("id");
query.selectField(CuidAdapter.field(VILLAGE_CLASS, CuidAdapter.NAME_FIELD));
ColumnSet columnSet = assertResolves(locator.queryTable(query));
assertThat(columnSet.getNumRows(), equalTo(3));
final ResourceId firstRecordId = ResourceId.valueOf(columnSet.getColumnView("id").getString(0));
assertResolves(locator.remove(NFI_DIST_FORM_CLASS, firstRecordId));
columnSet = assertResolves(locator.queryTable(query));
// size is reduced
assertThat(columnSet.getNumRows(), equalTo(2));
assertThat(columnSet.getColumnView("id"), not(hasValue(firstRecordId)));
}
use of org.activityinfo.model.resource.ResourceId in project activityinfo by bedatadriven.
the class ResourceLocatorAdaptorTest method deleteLocation.
@Test
public void deleteLocation() {
ResourceId instanceToDelete = CuidAdapter.locationInstanceId(1);
assertResolves(locator.remove(CuidAdapter.locationFormClass(1), instanceToDelete));
QueryModel queryModel = new QueryModel(CuidAdapter.locationFormClass(1));
queryModel.selectResourceId().as("id");
ColumnSet columnSet = assertResolves(locator.queryTable(queryModel));
ColumnView idColumn = columnSet.getColumnView("id");
for (int i = 0; i < idColumn.numRows(); i++) {
if (idColumn.getString(i).equals(instanceToDelete.asString())) {
throw new AssertionError();
}
}
}
use of org.activityinfo.model.resource.ResourceId in project activityinfo by bedatadriven.
the class FormModelTest method setupForms.
/**
* Sets up test fixtures. Must be called by each test to ensure that it runs AFTER
* the dbunit setup
*/
public void setupForms() {
ResourceId masterFormId = CuidAdapter.activityFormClass(3);
masterFormClass = new FormClass(masterFormId);
masterFormClass.setDatabaseId(CuidAdapter.databaseId(1));
FormField partnerField = new FormField(CuidAdapter.partnerField(3));
partnerField.setLabel("Partner");
partnerField.setType(ReferenceType.single(CuidAdapter.partnerFormId(1)));
masterFormClass.addElement(partnerField);
FormField labelField = masterFormClass.addField(CuidAdapter.generateIndicatorId());
labelField.setLabel("label1");
labelField.setType(TextType.SIMPLE);
subFormClass = new FormClass(ResourceId.generateId());
subFormClass.setDatabaseId(masterFormClass.getDatabaseId());
subFormClass.setParentFormId(masterFormId);
subFormClass.setSubFormKind(SubFormKind.MONTHLY);
subFormChildField = subFormClass.addField();
subFormChildField.setType(TextType.SIMPLE);
subFormField = masterFormClass.addField(CuidAdapter.generateIndicatorId());
subFormField.setType(new SubFormReferenceType(subFormClass.getId()));
assertResolves(locator.persist(subFormClass));
assertResolves(locator.persist(masterFormClass));
}
use of org.activityinfo.model.resource.ResourceId in project activityinfo by bedatadriven.
the class MySqlRecordHistoryBuilder method queryLabels.
private List<String> queryLabels(ReferenceType type, ReferenceValue value) {
Map<ResourceId, String> labelMap = new HashMap<>();
for (ResourceId formId : type.getRange()) {
Optional<FormStorage> form = catalog.getForm(formId);
if (form.isPresent()) {
Optional<ResourceId> labelFieldId = findLabelField(form.get().getFormClass());
for (RecordRef ref : value.getReferences()) {
Optional<FormRecord> record = form.get().get(ref.getRecordId());
if (record.isPresent()) {
JsonValue labelValue = Json.createNull();
if (labelFieldId.isPresent()) {
labelValue = record.get().getFields().get(labelFieldId.get().asString());
}
if (labelValue.isJsonNull()) {
labelValue = record.get().getFields().get(CuidAdapter.field(formId, CuidAdapter.NAME_FIELD).asString());
}
if (labelValue.isJsonPrimitive()) {
labelMap.put(ref.getRecordId(), labelValue.asString());
}
}
}
}
}
List<String> list = new ArrayList<>();
for (RecordRef ref : value.getReferences()) {
String label = labelMap.get(ref.getRecordId());
if (label == null) {
list.add(ref.toQualifiedString());
} else {
list.add(label);
}
}
return list;
}
use of org.activityinfo.model.resource.ResourceId 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;
}
Aggregations