Search in sources :

Example 1 with FieldInput

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);
}
Also used : PeriodValue(org.activityinfo.model.type.time.PeriodValue) FieldInput(org.activityinfo.ui.client.input.model.FieldInput) LocalDate(org.activityinfo.model.type.time.LocalDate) Date(java.util.Date)

Example 2 with FieldInput

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)));
            }
        }
    });
}
Also used : Set(java.util.Set) ReferenceValue(org.activityinfo.model.type.ReferenceValue) RecordRef(org.activityinfo.model.type.RecordRef) FieldInput(org.activityinfo.ui.client.input.model.FieldInput)

Example 3 with FieldInput

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()));
}
Also used : RecordUpdate(org.activityinfo.model.resource.RecordUpdate) RecordRef(org.activityinfo.model.type.RecordRef) FormInputModel(org.activityinfo.ui.client.input.model.FormInputModel) FieldInput(org.activityinfo.ui.client.input.model.FieldInput) RecordTransaction(org.activityinfo.model.resource.RecordTransaction) Test(org.junit.Test)

Example 4 with FieldInput

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));
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) RecordRef(org.activityinfo.model.type.RecordRef) Quantity(org.activityinfo.model.type.number.Quantity) FormInputModel(org.activityinfo.ui.client.input.model.FormInputModel) FieldInput(org.activityinfo.ui.client.input.model.FieldInput) Test(org.junit.Test)

Example 5 with FieldInput

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));
}
Also used : EnumValue(org.activityinfo.model.type.enumerated.EnumValue) RecordRef(org.activityinfo.model.type.RecordRef) Quantity(org.activityinfo.model.type.number.Quantity) FormInputModel(org.activityinfo.ui.client.input.model.FormInputModel) FieldInput(org.activityinfo.ui.client.input.model.FieldInput) LocalDate(org.activityinfo.model.type.time.LocalDate) Test(org.junit.Test)

Aggregations

FieldInput (org.activityinfo.ui.client.input.model.FieldInput)12 RecordRef (org.activityinfo.model.type.RecordRef)6 FormInputModel (org.activityinfo.ui.client.input.model.FormInputModel)5 Test (org.junit.Test)5 LocalDate (org.activityinfo.model.type.time.LocalDate)4 FormTree (org.activityinfo.model.formTree.FormTree)2 RecordUpdate (org.activityinfo.model.resource.RecordUpdate)2 EnumValue (org.activityinfo.model.type.enumerated.EnumValue)2 Quantity (org.activityinfo.model.type.number.Quantity)2 Date (java.util.Date)1 Set (java.util.Set)1 FormInstance (org.activityinfo.model.form.FormInstance)1 RecordTransaction (org.activityinfo.model.resource.RecordTransaction)1 ResourceId (org.activityinfo.model.resource.ResourceId)1 ReferenceValue (org.activityinfo.model.type.ReferenceValue)1 GeoPoint (org.activityinfo.model.type.geo.GeoPoint)1 Month (org.activityinfo.model.type.time.Month)1 PeriodValue (org.activityinfo.model.type.time.PeriodValue)1