use of org.activityinfo.model.type.ReferenceType in project activityinfo by bedatadriven.
the class ActivityLoader method patchDeserializedFormClass.
/**
* Apply any updates to the serialized FormClass that might be required to do changes in
* ActivityInfo.
*/
private static FormClass patchDeserializedFormClass(Activity activity, FormClass formClass) {
// Ensure that all forms have the database id
formClass.setDatabaseId(activity.getDatabaseId());
// Some partner fields have been stored to the JSON as pointing to the wrong database,
// either because there was a bug in the past or because the databaseId was manually updated
// in the activity table without a corresponding change to the formClass field.
ResourceId partnerFieldId = CuidAdapter.partnerField(activity.getId());
ReferenceType expectedPartnerType = ReferenceType.single(CuidAdapter.partnerFormId(activity.getDatabaseId()));
Optional<FormField> partnerField = formClass.getFieldIfPresent(partnerFieldId);
if (!partnerField.isPresent()) {
FormField newPartnerField = new FormField(partnerFieldId);
newPartnerField.setType(expectedPartnerType);
newPartnerField.setVisible(true);
newPartnerField.setRequired(true);
newPartnerField.setLabel("Partner");
formClass.addElement(newPartnerField);
} else {
partnerField.get().setType(expectedPartnerType);
}
// The (classic) pivot table components rely on a fixed fields with date1 and date2
ResourceId startDateId = CuidAdapter.field(formClass.getId(), CuidAdapter.START_DATE_FIELD);
ResourceId endDateId = CuidAdapter.field(formClass.getId(), CuidAdapter.END_DATE_FIELD);
for (FormField field : formClass.getFields()) {
if (field.getId().equals(startDateId) && Strings.isNullOrEmpty(field.getCode())) {
field.setCode("date1");
}
if (field.getId().equals(endDateId) && Strings.isNullOrEmpty(field.getCode())) {
field.setCode("date2");
}
}
return formClass;
}
use of org.activityinfo.model.type.ReferenceType in project activityinfo by bedatadriven.
the class ActivityUpdater method validateNewFormClass.
private FormClass validateNewFormClass(FormClass formClass) {
Map<ResourceId, FormField> fieldMap = new HashMap<>();
for (FormField field : formClass.getFields()) {
fieldMap.put(field.getId(), field);
}
ResourceId partnerFieldId = CuidAdapter.partnerField(activityId);
FormField partnerField = fieldMap.get(partnerFieldId);
if (partnerField == null) {
partnerField = new FormField(partnerFieldId);
partnerField.setLabel("Partner");
partnerField.setVisible(true);
partnerField.setRequired(true);
formClass.getElements().add(0, partnerField);
}
// Ensure partner type is correct:
partnerField.setType(new ReferenceType(Cardinality.SINGLE, CuidAdapter.partnerFormId(databaseId)));
return formClass;
}
use of org.activityinfo.model.type.ReferenceType in project activityinfo by bedatadriven.
the class LookupKeySetTest method noKeysTest.
@Test
public void noKeysTest() {
TestingStorageProvider catalog = new TestingStorageProvider();
NfiForm nfiForm = catalog.getNfiForm();
FormField field = new FormField(ResourceId.generateFieldId(ReferenceType.TYPE_CLASS));
field.setType(new ReferenceType(Cardinality.SINGLE, nfiForm.getFormId()));
FormTree formTree = catalog.getFormTree(nfiForm.getFormId());
LookupKeySet lookupKeySet = new LookupKeySet(formTree, field);
assertThat(lookupKeySet.getLookupKeys(), hasSize(1));
assertThat(lookupKeySet.getLeafKeys(), hasSize(1));
}
use of org.activityinfo.model.type.ReferenceType in project activityinfo by bedatadriven.
the class LookupViewModelTest method multipleLeafKeys.
@Test
public void multipleLeafKeys() {
IdpLocationForm locationForm = catalog.getIdpLocationForm();
FormTree formTree = catalog.getFormTree(locationForm.getFormId());
FormField referenceField = new FormField(ResourceId.generateFieldId(ReferenceType.TYPE_CLASS)).setType(new ReferenceType(Cardinality.SINGLE, locationForm.getFormId()));
LookupViewModel viewModel = new LookupViewModel(setup.getFormStore(), formTree, referenceField);
assertThat(viewModel.getLookupKeys(), hasSize(3));
LookupKeyViewModel province = viewModel.getLookupKeys().get(0);
LookupKeyViewModel type = viewModel.getLookupKeys().get(1);
LookupKeyViewModel name = viewModel.getLookupKeys().get(2);
Connection<List<String>> provinceChoices = setup.connect(province.getChoices());
Connection<List<String>> typeChoices = setup.connect(type.getChoices());
Connection<List<String>> nameChoices = setup.connect(name.getChoices());
Connection<Optional<RecordRef>> selection = setup.connect(viewModel.getSelectedRecord());
viewModel.select(province.getLookupKey(), provinceChoices.assertLoaded().get(0));
assertThat(typeChoices.assertLoaded(), hasItems(equalTo("Camp"), equalTo("Clinic"), equalTo("School")));
viewModel.select(type.getLookupKey(), "Camp");
assertThat(nameChoices.assertLoaded(), hasItems("Location 34", "Location 54", "Location 76"));
viewModel.select(name.getLookupKey(), "Location 76");
assertThat(selection.assertLoaded().get(), equalTo(new RecordRef(locationForm.getFormId(), ResourceId.valueOf("c75"))));
}
use of org.activityinfo.model.type.ReferenceType in project activityinfo by bedatadriven.
the class LookupViewModelTest method nullKeys.
@Test
public void nullKeys() {
FormClass keyForm = new FormClass(ResourceId.valueOf("FORM1"));
keyForm.addField(ResourceId.valueOf("PROVINCE")).setLabel("Province").setKey(true).setRequired(true).setType(TextType.SIMPLE);
keyForm.addField(ResourceId.valueOf("SCHOOL")).setLabel("School").setKey(true).setRequired(true).setType(TextType.SIMPLE);
FormClass form = new FormClass(ResourceId.valueOf("FORM2"));
FormField referenceField = new FormField(ResourceId.generateFieldId(ReferenceType.TYPE_CLASS));
ReferenceType referenceType = new ReferenceType(Cardinality.SINGLE, ResourceId.valueOf("FORM1"));
referenceField.setType(referenceType);
form.addField(ResourceId.valueOf("PROJECT")).setLabel("Project name").setType(referenceType);
FormTreeBuilder treeBuilder = new FormTreeBuilder(new FormMetadataProviderStub(form, keyForm));
FormTree formTree = treeBuilder.queryTree(form.getId());
Map<String, ColumnView> columnSet = new HashMap<>();
columnSet.put("id", new StringArrayColumnView(Arrays.asList("R1", "R2", "R3", "R4")));
columnSet.put("k1", new StringArrayColumnView(Arrays.asList("PZ", null, "PA", "PA")));
columnSet.put("k2", new StringArrayColumnView(Arrays.asList("S1", "S2", null, "S3")));
FormSource formSource = EasyMock.createMock(FormSource.class);
EasyMock.expect(formSource.query(EasyMock.anyObject(QueryModel.class))).andReturn(Observable.just(new ColumnSet(3, columnSet))).anyTimes();
EasyMock.replay(formSource);
LookupViewModel viewModel = new LookupViewModel(formSource, formTree, referenceField);
LookupKeyViewModel provinceKey = viewModel.getLookupKeys().get(0);
assertThat(provinceKey.getChoices().get(), contains("PA", "PZ"));
viewModel.select(provinceKey.getLookupKey(), "PA");
LookupKeyViewModel schoolKey = viewModel.getLookupKeys().get(1);
Connection<List<String>> schoolChoices = new Connection<>(schoolKey.getChoices());
assertThat(schoolChoices.assertLoaded(), contains("S3"));
}
Aggregations