use of org.activityinfo.model.type.RecordRef in project activityinfo by bedatadriven.
the class ReferenceFieldWidget method init.
@Override
public void init(FieldValue value) {
ReferenceValue referenceValue = (ReferenceValue) value;
RecordRef recordRef = referenceValue.getOnlyReference();
viewModel.setInitialSelection(referenceValue.getReferences());
}
use of org.activityinfo.model.type.RecordRef in project activityinfo by bedatadriven.
the class ReferenceFieldWidget method onSelection.
private void onSelection(SelectionEvent<String> event) {
LOGGER.info("onSelection: " + event.getSelectedItem());
viewModel.getSelectedRecords().once().then(new AsyncCallback<Set<RecordRef>>() {
@Override
public void onFailure(Throwable caught) {
}
@Override
public void onSuccess(Set<RecordRef> result) {
if (result.isEmpty()) {
fieldUpdater.update(FieldInput.EMPTY);
} else {
fieldUpdater.update(new FieldInput(new ReferenceValue(result)));
}
}
});
}
use of org.activityinfo.model.type.RecordRef in project activityinfo by bedatadriven.
the class SubFormViewModelBuilder method computeActiveSubRecord.
private RecordRef computeActiveSubRecord(RecordRef parentRecordRef, FormInputModel inputModel) {
// Has the user chosen a specific period?
Optional<RecordRef> activeSubRecord = inputModel.getActiveSubRecord(fieldId);
if (activeSubRecord.isPresent()) {
return activeSubRecord.get();
}
// Otherwise choose the active record based on the user's previous choices
// or the current date
PeriodValue activePeriod = subFormKind.getPeriodType().containingDate(memory.getLastUsedDate());
ResourceId recordId = ResourceId.periodSubRecordId(parentRecordRef, activePeriod);
return new RecordRef(subFormId, recordId);
}
use of org.activityinfo.model.type.RecordRef in project activityinfo by bedatadriven.
the class KeySelectionSet method normalize.
/**
* When we have overlapping hierarchies, we don't include all complete selections as
* the value for the field.
*
* For example, given a reference field that includes [PROVINCE, TERRITORY] in the range, we will
* have two {@link KeySelection} for each of the forms in this range. Their key trees look like this:
*
* <pre>
*
* PROVINCE TERRITORY
*
* k0: Province.Name k0: Province.Name
* ^
* |
* k1: Territory.Name
*
* </pre>
*
* Once the user has selected both the Province.Name (k0) and the Territory.Name (k1), then we have a valid
* selection for both forms.
*
* However, in this case, we don't want to store _both_ the territory _and_ the province as this not really
* the user's intent. We exclude the province selection because it is implied by the territory selection.
*/
private Set<RecordRef> normalize(List<Optional<RecordRef>> refs) {
// Build a map from referenced form id -> selection
Map<ResourceId, RecordRef> selectionMap = new HashMap<>();
for (int i = 0; i < selections.size(); i++) {
if (refs.get(i).isPresent()) {
selectionMap.put(selections.get(i).getFormId(), refs.get(i).get());
}
}
List<ResourceId> selectedFormIds = Lists.newArrayList(selectionMap.keySet());
// Now remove redundant parents
for (ResourceId formId : selectedFormIds) {
for (ResourceId ancestorFormId : lookupKeySet.getAncestorForms(formId)) {
selectionMap.remove(ancestorFormId);
}
}
return new HashSet<>(selectionMap.values());
}
use of org.activityinfo.model.type.RecordRef in project activityinfo by bedatadriven.
the class FormInputViewModelTest method testSubFormInput.
@Test
public void testSubFormInput() {
FormInputViewModelBuilder builder = builderFor(setup.getCatalog().getIncidentForm());
// Start with empty input
FormInputModel inputModel = new FormInputModel(new RecordRef(IncidentForm.FORM_ID, ResourceId.generateId()));
// Should see one (empty) sub form record
FormInputViewModel viewModel = builder.build(inputModel);
SubFormViewModel referralSubForm = viewModel.getSubForm(IncidentForm.REFERRAL_FIELD_ID);
assertThat(referralSubForm.getSubRecords(), hasSize(1));
// We can update this sub record
FormInputViewModel subRecord = referralSubForm.getSubRecords().get(0);
inputModel = inputModel.update(subRecord.getRecordRef(), ReferralSubForm.ORGANIZATION_FIELD_ID, new FieldInput(TextValue.valueOf("CRS")));
viewModel = builder.build(inputModel);
referralSubForm = viewModel.getSubForm(IncidentForm.REFERRAL_FIELD_ID);
assertThat(referralSubForm.getSubRecords(), hasSize(1));
// Now add a second record
inputModel = inputModel.addSubRecord(new RecordRef(ReferralSubForm.FORM_ID, ResourceId.generateId()));
viewModel = builder.build(inputModel);
referralSubForm = viewModel.getSubForm(IncidentForm.REFERRAL_FIELD_ID);
assertThat(referralSubForm.getSubRecords(), hasSize(2));
// Verify that the transaction is built is correctly
RecordTransaction tx = viewModel.buildTransaction();
RecordUpdate[] changes = tx.getChangeArray();
assertThat(changes.length, equalTo(3));
RecordUpdate parentChange = changes[0];
RecordUpdate subFormChange = changes[1];
assertThat(parentChange.getRecordRef(), equalTo(inputModel.getRecordRef()));
assertThat(subFormChange.getParentRecordId(), equalTo(parentChange.getRecordId().asString()));
}
Aggregations