use of org.activityinfo.model.type.number.Quantity 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.model.type.number.Quantity 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));
}
use of org.activityinfo.model.type.number.Quantity in project activityinfo by bedatadriven.
the class YearFracFunction method apply.
@Override
public FieldValue apply(List<FieldValue> arguments) {
if (arguments.size() != 2) {
throw new FormulaSyntaxException("YEARFRAC() requires two arguments");
}
LocalDate startDate = (LocalDate) arguments.get(0);
LocalDate endDate = (LocalDate) arguments.get(1);
if (startDate == null || endDate == null) {
return null;
}
return new Quantity(compute(startDate, endDate));
}
use of org.activityinfo.model.type.number.Quantity in project activityinfo by bedatadriven.
the class AddDateFunction method apply.
@Override
public FieldValue apply(List<FieldValue> arguments) {
LocalDate date = (LocalDate) arguments.get(0);
Quantity days = (Quantity) arguments.get(1);
return date.plusDays((int) days.getValue());
}
use of org.activityinfo.model.type.number.Quantity in project activityinfo by bedatadriven.
the class DateComponentFunction method apply.
@Override
public final FieldValue apply(List<FieldValue> arguments) {
checkArity(arguments, 1);
FieldValue argument = arguments.get(0);
if (!(argument instanceof LocalDate)) {
throw new FormulaSyntaxException("Expected date argument");
}
LocalDate date = (LocalDate) argument;
return new Quantity(apply(date));
}
Aggregations