Search in sources :

Example 1 with Quantity

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));
}
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 2 with Quantity

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));
}
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)

Example 3 with Quantity

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));
}
Also used : FormulaSyntaxException(org.activityinfo.model.formula.diagnostic.FormulaSyntaxException) Quantity(org.activityinfo.model.type.number.Quantity) LocalDate(org.activityinfo.model.type.time.LocalDate)

Example 4 with Quantity

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());
}
Also used : Quantity(org.activityinfo.model.type.number.Quantity) LocalDate(org.activityinfo.model.type.time.LocalDate)

Example 5 with Quantity

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));
}
Also used : FormulaSyntaxException(org.activityinfo.model.formula.diagnostic.FormulaSyntaxException) Quantity(org.activityinfo.model.type.number.Quantity) FieldValue(org.activityinfo.model.type.FieldValue) LocalDate(org.activityinfo.model.type.time.LocalDate)

Aggregations

Quantity (org.activityinfo.model.type.number.Quantity)23 Test (org.junit.Test)13 FieldValue (org.activityinfo.model.type.FieldValue)9 LocalDate (org.activityinfo.model.type.time.LocalDate)8 FormClass (org.activityinfo.model.form.FormClass)6 ResourceId (org.activityinfo.model.resource.ResourceId)6 QuantityType (org.activityinfo.model.type.number.QuantityType)6 FormField (org.activityinfo.model.form.FormField)5 TypedRecordUpdate (org.activityinfo.store.spi.TypedRecordUpdate)5 QueryModel (org.activityinfo.model.query.QueryModel)4 GeoPoint (org.activityinfo.model.type.geo.GeoPoint)4 ColumnSet (org.activityinfo.model.query.ColumnSet)3 EnumValue (org.activityinfo.model.type.enumerated.EnumValue)3 ColumnSetBuilder (org.activityinfo.store.query.server.ColumnSetBuilder)3 NullFormSupervisor (org.activityinfo.store.query.shared.NullFormSupervisor)3 JsonValue (org.activityinfo.json.JsonValue)2 FormulaSyntaxException (org.activityinfo.model.formula.diagnostic.FormulaSyntaxException)2 KeyGenerator (org.activityinfo.model.legacy.KeyGenerator)2 RecordRef (org.activityinfo.model.type.RecordRef)2 CalculatedFieldType (org.activityinfo.model.type.expr.CalculatedFieldType)2