use of org.activityinfo.ui.client.input.model.FieldInput in project activityinfo by bedatadriven.
the class AbstractWeekWidget method onDatePickerSelected.
private void onDatePickerSelected(SelectEvent event) {
FieldInput input = input();
if (input.getState() == FieldInput.State.VALID) {
PeriodValue value = (PeriodValue) input.getValue();
dateMenu.getDatePicker().setValue(value.asInterval().getStartDate().atMidnightInMyTimezone());
} else {
dateMenu.getDatePicker().setValue(new Date());
}
dateMenu.show(pickButton);
}
use of org.activityinfo.ui.client.input.model.FieldInput 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.ui.client.input.model.FieldInput 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()));
}
use of org.activityinfo.ui.client.input.model.FieldInput in project activityinfo by bedatadriven.
the class FormInputViewModelTest method monthlySubForms.
@Test
public void monthlySubForms() {
ClinicForm clinicForm = setup.getCatalog().getClinicForm();
ResourceId consultCountFieldId = clinicForm.getSubForm().getConsultsField().getId();
FormInputViewModelBuilder builder = builderFor(clinicForm);
ResourceId parentRecordId = ResourceId.generateId();
FormInputModel inputModel = new FormInputModel(new RecordRef(clinicForm.getFormId(), parentRecordId));
FormInputViewModel viewModel = builder.build(inputModel);
// Get the sub form view model.
// The active ref should be set to 2017-10 by default.
ResourceId subFormFieldId = clinicForm.getSubFormField().getId();
SubFormViewModel subForm = viewModel.getSubForm(subFormFieldId);
assertThat(subForm, notNullValue());
RecordRef octoberId = new RecordRef(clinicForm.getSubForm().getFormId(), ResourceId.generatedPeriodSubmissionId(parentRecordId, "2017-10"));
RecordRef novemberId = new RecordRef(clinicForm.getSubForm().getFormId(), ResourceId.generatedPeriodSubmissionId(parentRecordId, "2017-11"));
assertThat(subForm.getActiveRecordRef(), equalTo(octoberId));
// Update a field in the active form
inputModel = inputModel.updateSubForm(subForm.update(consultCountFieldId, new FieldInput(new Quantity(33))));
viewModel = builder.build(inputModel);
FormInputViewModel subFormViewModel = viewModel.getSubForm(subFormFieldId).getActiveSubViewModel();
assertThat(subFormViewModel.getField(consultCountFieldId), equalTo(new Quantity(33)));
assertThat(subFormViewModel.isValid(), equalTo(true));
// Now change the active record to another month
inputModel = inputModel.updateActiveSubRecord(subFormFieldId, novemberId);
viewModel = builder.build(inputModel);
subForm = viewModel.getSubForm(subFormFieldId);
// At this point, the subform is not valid because required fields are not filled in,
// but it is also not dirty because we have not changed anything
assertThat(subForm.getActiveSubViewModel().isValid(), equalTo(false));
assertThat(subForm.getActiveSubViewModel().isDirty(), equalTo(false));
assertThat(subForm.getActiveSubViewModel().isPlaceholder(), equalTo(true));
// Now fill in the required field
inputModel = inputModel.updateSubForm(subForm.update(consultCountFieldId, new FieldInput(new Quantity(44))));
viewModel = builder.build(inputModel);
assertThat(viewModel.getSubForm(subFormFieldId).getActiveRecordRef(), equalTo(novemberId));
assertThat(viewModel.getSubForm(subFormFieldId).isValid(), equalTo(true));
}
use of org.activityinfo.ui.client.input.model.FieldInput in project activityinfo by bedatadriven.
the class FormInputViewModelTest method testPersistence.
@Test
public void testPersistence() {
Survey survey = setup.getCatalog().getSurvey();
FormInputViewModelBuilder builder = builderFor(survey);
// Start with no input
FormInputModel inputModel = new FormInputModel(new RecordRef(survey.getFormId(), ResourceId.generateId())).update(survey.getGenderFieldId(), new FieldInput(new EnumValue(survey.getMaleId()))).update(survey.getNameFieldId(), new FieldInput(TextValue.valueOf("BOB"))).update(survey.getDobFieldId(), new FieldInput(new LocalDate(1982, 1, 16))).update(survey.getAgeFieldId(), new FieldInput(new Quantity(35)));
// Verify that it's valid
FormInputViewModel viewModel = builder.build(inputModel);
assertThat(viewModel.isValid(), equalTo(true));
// Now build the update transaction and save!
Promise<Void> completed = setup.getFormStore().updateRecords(viewModel.buildTransaction());
assertThat(completed.getState(), equalTo(Promise.State.FULFILLED));
}
Aggregations