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