Search in sources :

Example 1 with RecordTree

use of org.activityinfo.model.formTree.RecordTree in project activityinfo by bedatadriven.

the class LookupKeySetTest method labelTest.

@Test
public void labelTest() {
    NfiForm nfiForm = setup.getNfiForm();
    VillageForm villageForm = setup.getVillageForm();
    AdminLevelForm territoryForm = villageForm.getParentForm();
    AdminLevelForm provinceForm = territoryForm.getParentForm().get();
    Observable<Maybe<RecordTree>> nfiRecordTree = setup.getFormStore().getRecordTree(nfiForm.getRecordRef(0));
    Connection<Maybe<RecordTree>> nfiRecordTreeView = setup.connect(nfiRecordTree);
    RecordTree tree = nfiRecordTreeView.assertLoaded().get();
    LookupKeySet lookupKeySet = new LookupKeySet(tree.getFormTree(), tree.getFormTree().getRootField(nfiForm.getVillageField().getId()).getField());
    assertThat(lookupKeySet.getLookupKeys().size(), equalTo(3));
    ReferenceValue referenceValue = (ReferenceValue) tree.getRoot().get(nfiForm.getVillageField().getId());
    RecordRef villageRef = referenceValue.getOnlyReference();
    RecordTree villageTree = tree.subTree(villageRef);
    referenceValue = (ReferenceValue) villageTree.getRoot().get(villageForm.getAdminFieldId());
    RecordRef territoryRef = referenceValue.getOnlyReference();
    RecordTree territoryTree = villageTree.subTree(territoryRef);
    referenceValue = (ReferenceValue) territoryTree.getRoot().get(territoryForm.getParentFieldId());
    RecordRef provinceRef = referenceValue.getOnlyReference();
    Maybe<String> villageLabel = lookupKeySet.label(tree, villageRef);
    Maybe<String> territoryLabel = lookupKeySet.label(tree, territoryRef);
    Maybe<String> provinceLabel = lookupKeySet.label(tree, provinceRef);
    assertThat(villageLabel, equalTo(Maybe.of("Village 660")));
    assertThat(territoryLabel, equalTo(Maybe.of("Territory 85")));
    assertThat(provinceLabel, equalTo(Maybe.of("Province 11")));
}
Also used : RecordTree(org.activityinfo.model.formTree.RecordTree) Maybe(org.activityinfo.promise.Maybe) LookupKeySet(org.activityinfo.model.formTree.LookupKeySet) ReferenceValue(org.activityinfo.model.type.ReferenceValue) RecordRef(org.activityinfo.model.type.RecordRef) Test(org.junit.Test)

Example 2 with RecordTree

use of org.activityinfo.model.formTree.RecordTree in project activityinfo by bedatadriven.

the class RecordTreeLoaderTest method references.

@Test
public void references() {
    BioDataForm bioDataForm = setup.getBioDataForm();
    Observable<Maybe<RecordTree>> recordTree = setup.getFormStore().getRecordTree(bioDataForm.getRecordRef(0));
    Connection<Maybe<RecordTree>> recordTreeView = setup.connect(recordTree);
    RecordTree tree = recordTreeView.assertLoaded().get();
    LookupKeySet lookupKeySet = new LookupKeySet(tree.getFormTree(), tree.getFormTree().getRootField(BioDataForm.PROTECTION_CODE_FIELD_ID).getField());
    assertThat(lookupKeySet.getLookupKeys(), hasSize(1));
    ReferenceValue referenceValue = (ReferenceValue) tree.getRoot().get(BioDataForm.PROTECTION_CODE_FIELD_ID);
    RecordRef ref = referenceValue.getOnlyReference();
    assertThat(lookupKeySet.label(tree, ref), equalTo(Maybe.of("00667")));
}
Also used : RecordTree(org.activityinfo.model.formTree.RecordTree) Maybe(org.activityinfo.promise.Maybe) LookupKeySet(org.activityinfo.model.formTree.LookupKeySet) ReferenceValue(org.activityinfo.model.type.ReferenceValue) RecordRef(org.activityinfo.model.type.RecordRef) BioDataForm(org.activityinfo.store.testing.BioDataForm) Test(org.junit.Test)

Example 3 with RecordTree

use of org.activityinfo.model.formTree.RecordTree 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 4 with RecordTree

use of org.activityinfo.model.formTree.RecordTree 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 5 with RecordTree

use of org.activityinfo.model.formTree.RecordTree in project activityinfo by bedatadriven.

the class RecordTreeLoaderTest method simpleSurvey.

@Test
public void simpleSurvey() {
    Survey survey = setup.getSurveyForm();
    Observable<Maybe<RecordTree>> recordTree = setup.getFormStore().getRecordTree(survey.getRecordRef(0));
    Connection<Maybe<RecordTree>> recordTreeView = setup.connect(recordTree);
    RecordTree tree = recordTreeView.assertLoaded().get();
    assertThat(tree.getRoot().get(survey.getNameFieldId()), equalTo(TextValue.valueOf("Melanie")));
}
Also used : RecordTree(org.activityinfo.model.formTree.RecordTree) Survey(org.activityinfo.store.testing.Survey) Maybe(org.activityinfo.promise.Maybe) Test(org.junit.Test)

Aggregations

RecordTree (org.activityinfo.model.formTree.RecordTree)5 RecordRef (org.activityinfo.model.type.RecordRef)4 Maybe (org.activityinfo.promise.Maybe)4 Test (org.junit.Test)3 LookupKeySet (org.activityinfo.model.formTree.LookupKeySet)2 ReferenceValue (org.activityinfo.model.type.ReferenceValue)2 FormInputModel (org.activityinfo.ui.client.input.model.FormInputModel)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 Level (java.util.logging.Level)1 Logger (java.util.logging.Logger)1 RecordLockSet (org.activityinfo.model.database.RecordLockSet)1 FormEvalContext (org.activityinfo.model.form.FormEvalContext)1 FormField (org.activityinfo.model.form.FormField)1 FormInstance (org.activityinfo.model.form.FormInstance)1 FormTree (org.activityinfo.model.formTree.FormTree)1 FormulaNode (org.activityinfo.model.formula.FormulaNode)1