use of org.activityinfo.model.resource.ResourceId in project activityinfo by bedatadriven.
the class FormInputViewModelTest method hiddenSubForm.
/**
* Test the ViewModel for when the user does not have access to a referenced sub form.
*/
@Test
public void hiddenSubForm() {
FormClass parentForm = new FormClass(ResourceId.valueOf("PARENT_FORM"));
parentForm.addField(ResourceId.valueOf("F1")).setLabel("What is your name?").setType(TextType.SIMPLE);
parentForm.addField(ResourceId.valueOf("F2")).setLabel("What are your secrets?").setType(new SubFormReferenceType(ResourceId.valueOf("SECRET_FORM")));
FormTreeBuilder treeBuilder = new FormTreeBuilder(new FormMetadataProvider() {
@Override
public FormMetadata getFormMetadata(ResourceId formId) {
if (formId.equals(parentForm.getId())) {
return FormMetadata.of(1, parentForm, FormPermissions.owner());
} else {
return FormMetadata.forbidden(formId);
}
}
});
FormTree formTree = treeBuilder.queryTree(parentForm.getId());
FormStore formStore = EasyMock.createMock(FormStore.class);
EasyMock.replay(formStore);
FormInputViewModelBuilder viewModelBuilder = new FormInputViewModelBuilder(formStore, formTree, new TestingActivePeriodMemory());
FormInputViewModel viewModel = viewModelBuilder.build(new FormInputModel(new RecordRef(parentForm.getId(), ResourceId.valueOf("R1"))));
}
use of org.activityinfo.model.resource.ResourceId 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.resource.ResourceId in project activityinfo by bedatadriven.
the class LookupKeySetTest method overlappingHierarchies.
/**
* For classical location types especially, a reference field's range might include multiple forms,
* such as [province, territory, health zone].
*
* This means that the user can <i>either</i> select a province, <i>or</i> a territory, <i>or</i>
* a health zone.
*
* In this case, you also have a hierarchy
* <pre>
* Province.Name
* ^ ^
* | |
* Territory.Name Health Zone.Name
* ^
* |
* Sector.Name
* </pre>
*
* But the record reference depends on which fields are completed. If only province is completed,
* then the selected record is from province. Otherwise from territory, etc.
*/
@Test
public void overlappingHierarchies() {
TestingStorageProvider catalog = setup.getCatalog();
LocaliteForm localiteForm = catalog.getLocaliteForm();
FormTree formTree = setup.getFormTree(localiteForm.getFormId());
LookupKeySet lookupKeySet = new LookupKeySet(formTree, localiteForm.getAdminField());
// The resulting key set should only include 3 keys, not 6
// because the three different forms in the range overlap
assertThat(lookupKeySet.getLookupKeys(), hasSize(3));
assertThat(lookupKeySet.getKey(0).getKeyLabel(), equalTo("Province Name"));
assertThat(lookupKeySet.getKey(1).getKeyLabel(), equalTo("Territory Name"));
assertThat(lookupKeySet.getKey(2).getKeyLabel(), equalTo("Zone de Sante Name"));
// We need the relationships between the forms
ResourceId provinceId = catalog.getProvince().getFormId();
ResourceId territoryId = catalog.getTerritory().getFormId();
assertThat(lookupKeySet.getAncestorForms(provinceId), hasSize(0));
assertThat(lookupKeySet.getAncestorForms(territoryId), contains(provinceId));
// Formulas...
for (LookupKey lookupKey : lookupKeySet.getLeafKeys()) {
System.out.println(lookupKey.getKeyLabel() + " => " + lookupKey.getKeyFormulas());
}
}
use of org.activityinfo.model.resource.ResourceId in project activityinfo by bedatadriven.
the class AsyncClientStub method getRecordVersionRange.
@Override
public Promise<FormSyncSet> getRecordVersionRange(String formId, long localVersion, long toVersion) {
if (!connected) {
return offlineResult();
}
Optional<FormStorage> form = storageProvider.getForm(ResourceId.valueOf(formId));
if (!form.isPresent()) {
return Promise.rejected(new RuntimeException("No such form"));
}
VersionedFormStorage formStorage = (VersionedFormStorage) form.get();
return Promise.resolved(formStorage.getVersionRange(localVersion, toVersion, resourceId -> true));
}
use of org.activityinfo.model.resource.ResourceId in project activityinfo by bedatadriven.
the class TableToolBar method onNewRecord.
private void onNewRecord(SelectEvent event) {
ResourceId newRecordId = ResourceId.generateSubmissionId(viewModel.getFormId());
RecordRef newRecordRef = new RecordRef(viewModel.getFormId(), newRecordId);
FormDialog dialog = new FormDialog(formStore, newRecordRef);
dialog.show();
}
Aggregations