Search in sources :

Example 6 with FormInputModel

use of org.activityinfo.ui.client.input.model.FormInputModel in project activityinfo by bedatadriven.

the class FormInputViewModelTest method testMultipleSelectPersistence.

@Test
public void testMultipleSelectPersistence() {
    IntakeForm intakeForm = setup.getCatalog().getIntakeForm();
    FormInputViewModelBuilder builder = builderFor(intakeForm);
    FormInputModel inputModel = new FormInputModel(new RecordRef(intakeForm.getFormId(), ResourceId.generateId())).update(intakeForm.getOpenDateFieldId(), new LocalDate(2017, 1, 1)).update(intakeForm.getNationalityFieldId(), new EnumValue(intakeForm.getPalestinianId(), intakeForm.getJordanianId()));
    FormInputViewModel viewModel = builder.build(inputModel);
    assertThat(viewModel.isValid(), equalTo(true));
    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) FormInputModel(org.activityinfo.ui.client.input.model.FormInputModel) LocalDate(org.activityinfo.model.type.time.LocalDate) Test(org.junit.Test)

Example 7 with FormInputModel

use of org.activityinfo.ui.client.input.model.FormInputModel in project activityinfo by bedatadriven.

the class FormInputViewModelTest method inputMask.

@Test
public void inputMask() {
    IntakeForm intakeForm = setup.getCatalog().getIntakeForm();
    FormInputViewModelBuilder builder = builderFor(intakeForm);
    FormInputModel inputModel = new FormInputModel(new RecordRef(intakeForm.getFormId(), ResourceId.generateId()));
    // Fill in required fields
    inputModel = inputModel.update(intakeForm.getOpenDateFieldId(), new LocalDate(2017, 1, 1));
    // Does not match input mask "000"
    inputModel = inputModel.update(intakeForm.getRegNumberFieldId(), new FieldInput(TextValue.valueOf("FOOOOO")));
    FormInputViewModel viewModel = builder.build(inputModel);
    assertThat(viewModel.isValid(), equalTo(false));
    assertThat(viewModel.getValidationErrors(intakeForm.getRegNumberFieldId()), not(empty()));
}
Also used : RecordRef(org.activityinfo.model.type.RecordRef) 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 8 with FormInputModel

use of org.activityinfo.ui.client.input.model.FormInputModel in project activityinfo by bedatadriven.

the class FormInputViewModelBuilder method build.

public FormInputViewModel build(FormInputModel inputModel, Maybe<RecordTree> existingRecord, boolean placeholder) {
    // Combine the original values of the record with the newly entered data
    Map<ResourceId, FieldValue> existingValues = existingRecord.transform(r -> r.getRoot().getFieldValueMap()).or(emptyMap());
    FormInstance record = computeUpdatedRecord(existingValues, inputModel);
    // Now construct the viewModel that includes everything about
    // the current state of data entry.
    FormInputViewModel viewModel = new FormInputViewModel();
    viewModel.formTree = this.formTree;
    viewModel.inputModel = inputModel;
    viewModel.fieldValueMap = record.getFieldValueMap();
    viewModel.subFormMap = computeSubViewModels(inputModel, existingRecord);
    viewModel.existingValues = existingValues;
    viewModel.placeholder = placeholder;
    viewModel.relevant = computeRelevance(record);
    viewModel.missing = computeMissing(record, viewModel.relevant);
    viewModel.validationErrors = validateFieldValues(record);
    viewModel.dirty = computeDirty(placeholder, existingValues, record);
    viewModel.locked = checkLocks(record);
    viewModel.valid = allInputValid(inputModel) && viewModel.missing.isEmpty() && viewModel.validationErrors.isEmpty() && viewModel.subFormMap.values().stream().allMatch(SubFormViewModel::isValid);
    LOGGER.info("Valid = " + viewModel.valid);
    return viewModel;
}
Also used : java.util(java.util) FormulaParser(org.activityinfo.model.formula.FormulaParser) PeriodValue(org.activityinfo.model.type.time.PeriodValue) Multimap(com.google.common.collect.Multimap) FormInputModel(org.activityinfo.ui.client.input.model.FormInputModel) Level(java.util.logging.Level) SerialNumberType(org.activityinfo.model.type.SerialNumberType) HashMultimap(com.google.common.collect.HashMultimap) ResourceId(org.activityinfo.model.resource.ResourceId) RecordTree(org.activityinfo.model.formTree.RecordTree) FormEvalContext(org.activityinfo.model.form.FormEvalContext) RecordRef(org.activityinfo.model.type.RecordRef) FormStore(org.activityinfo.ui.client.store.FormStore) FieldValue(org.activityinfo.model.type.FieldValue) RecordLockSet(org.activityinfo.model.database.RecordLockSet) Collections.emptyMap(java.util.Collections.emptyMap) FormInstance(org.activityinfo.model.form.FormInstance) Logger(java.util.logging.Logger) FormulaNode(org.activityinfo.model.formula.FormulaNode) FieldInput(org.activityinfo.ui.client.input.model.FieldInput) FormField(org.activityinfo.model.form.FormField) TextType(org.activityinfo.model.type.primitive.TextType) Predicate(com.google.common.base.Predicate) Maybe(org.activityinfo.promise.Maybe) FormTree(org.activityinfo.model.formTree.FormTree) ResourceId(org.activityinfo.model.resource.ResourceId) FieldValue(org.activityinfo.model.type.FieldValue) FormInstance(org.activityinfo.model.form.FormInstance)

Example 9 with FormInputModel

use of org.activityinfo.ui.client.input.model.FormInputModel in project activityinfo by bedatadriven.

the class SubFormViewModelBuilder method build.

public SubFormViewModel build(FormInputModel inputModel, Maybe<RecordTree> existingParentRecord) {
    List<FormInputViewModel> subRecordViews = new ArrayList<>();
    // First do existing records
    Set<RecordRef> existingRefs = new HashSet<>();
    List<RecordTree> existingSubTrees;
    if (existingParentRecord.isVisible()) {
        existingSubTrees = existingParentRecord.get().buildSubTrees(inputModel.getRecordRef(), subTree);
    } else {
        existingSubTrees = Collections.emptyList();
    }
    Set<RecordRef> deletedRecords = new HashSet<>();
    for (RecordTree existingSubRecord : existingSubTrees) {
        RecordRef ref = existingSubRecord.getRootRef();
        if (inputModel.isDeleted(ref)) {
            deletedRecords.add(ref);
        } else {
            FormInputModel subInput = inputModel.getSubRecord(ref).orElse(new FormInputModel(ref));
            FormInputViewModel subViewModel = formBuilder.build(subInput, Maybe.of(existingSubRecord));
            existingRefs.add(ref);
            subRecordViews.add(subViewModel);
        }
    }
    // Now add sub records newly added by the user
    for (FormInputModel subInput : inputModel.getSubRecords()) {
        if (!existingRefs.contains(subInput.getRecordRef())) {
            if (subInput.getRecordRef().getFormId().equals(subFormId)) {
                FormInputViewModel subViewModel = formBuilder.build(subInput, Maybe.notFound());
                subRecordViews.add(subViewModel);
            }
        }
    }
    if (keyField.isPresent()) {
        // Keyed/Period subforms have a single active record
        RecordRef activeRecord = computeActiveSubRecord(inputModel.getRecordRef(), inputModel);
        FormInputViewModel activeRecordViewModel = find(activeRecord, subRecordViews);
        return new SubFormViewModel(fieldId, subFormKind, subRecordViews, activeRecordViewModel, deletedRecords);
    } else {
        if (subRecordViews.isEmpty()) {
            RecordRef placeholderId = placeholderRecordRef();
            if (!inputModel.isDeleted(placeholderId)) {
                subRecordViews.add(formBuilder.placeholder(placeholderId));
            }
        }
        return new SubFormViewModel(fieldId, subRecordViews, deletedRecords);
    }
}
Also used : RecordTree(org.activityinfo.model.formTree.RecordTree) RecordRef(org.activityinfo.model.type.RecordRef) FormInputModel(org.activityinfo.ui.client.input.model.FormInputModel)

Example 10 with FormInputModel

use of org.activityinfo.ui.client.input.model.FormInputModel in project activityinfo by bedatadriven.

the class FormInputViewModelTest method testSerialNumberEdit.

@Test
public void testSerialNumberEdit() {
    IntakeForm intakeForm = setup.getCatalog().getIntakeForm();
    RecordRef ref = intakeForm.getRecords().get(0).getRef();
    FormStructure structure = fetchStructure(ref);
    FormInputViewModelBuilder builder = builderFor(structure);
    FormInputModel model = new FormInputModel(ref);
    FormInputViewModel viewModel = builder.build(model, structure.getExistingRecord());
    assertThat(viewModel.getField(intakeForm.getProtectionCodeFieldId()), equalTo(new SerialNumber(1)));
}
Also used : SerialNumber(org.activityinfo.model.type.SerialNumber) RecordRef(org.activityinfo.model.type.RecordRef) FormInputModel(org.activityinfo.ui.client.input.model.FormInputModel) Test(org.junit.Test)

Aggregations

RecordRef (org.activityinfo.model.type.RecordRef)15 FormInputModel (org.activityinfo.ui.client.input.model.FormInputModel)15 Test (org.junit.Test)13 FieldInput (org.activityinfo.ui.client.input.model.FieldInput)6 ResourceId (org.activityinfo.model.resource.ResourceId)4 EnumValue (org.activityinfo.model.type.enumerated.EnumValue)4 RecordTransaction (org.activityinfo.model.resource.RecordTransaction)3 LocalDate (org.activityinfo.model.type.time.LocalDate)3 TestingFormStore (org.activityinfo.ui.client.store.TestingFormStore)3 FormTree (org.activityinfo.model.formTree.FormTree)2 FormTreeBuilder (org.activityinfo.model.formTree.FormTreeBuilder)2 RecordTree (org.activityinfo.model.formTree.RecordTree)2 RecordUpdate (org.activityinfo.model.resource.RecordUpdate)2 Quantity (org.activityinfo.model.type.number.Quantity)2 FormStore (org.activityinfo.ui.client.store.FormStore)2 Predicate (com.google.common.base.Predicate)1 HashMultimap (com.google.common.collect.HashMultimap)1 Multimap (com.google.common.collect.Multimap)1 java.util (java.util)1 Collections.emptyMap (java.util.Collections.emptyMap)1