Search in sources :

Example 16 with ReferenceType

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;
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) FormField(org.activityinfo.model.form.FormField) ReferenceType(org.activityinfo.model.type.ReferenceType)

Example 17 with ReferenceType

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;
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) FormField(org.activityinfo.model.form.FormField) ReferenceType(org.activityinfo.model.type.ReferenceType)

Example 18 with ReferenceType

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));
}
Also used : LookupKeySet(org.activityinfo.model.formTree.LookupKeySet) FormTree(org.activityinfo.model.formTree.FormTree) FormField(org.activityinfo.model.form.FormField) ReferenceType(org.activityinfo.model.type.ReferenceType) Test(org.junit.Test)

Example 19 with ReferenceType

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"))));
}
Also used : FormTree(org.activityinfo.model.formTree.FormTree) Optional(com.google.common.base.Optional) RecordRef(org.activityinfo.model.type.RecordRef) FormField(org.activityinfo.model.form.FormField) ReferenceType(org.activityinfo.model.type.ReferenceType) Test(org.junit.Test)

Example 20 with ReferenceType

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"));
}
Also used : ColumnView(org.activityinfo.model.query.ColumnView) StringArrayColumnView(org.activityinfo.model.query.StringArrayColumnView) Connection(org.activityinfo.observable.Connection) ColumnSet(org.activityinfo.model.query.ColumnSet) ReferenceType(org.activityinfo.model.type.ReferenceType) FormTreeBuilder(org.activityinfo.model.formTree.FormTreeBuilder) FormTree(org.activityinfo.model.formTree.FormTree) FormClass(org.activityinfo.model.form.FormClass) FormSource(org.activityinfo.store.query.shared.FormSource) FormField(org.activityinfo.model.form.FormField) StringArrayColumnView(org.activityinfo.model.query.StringArrayColumnView) FormMetadataProviderStub(org.activityinfo.model.formTree.FormMetadataProviderStub) Test(org.junit.Test)

Aggregations

ReferenceType (org.activityinfo.model.type.ReferenceType)21 FormField (org.activityinfo.model.form.FormField)16 ResourceId (org.activityinfo.model.resource.ResourceId)10 FormClass (org.activityinfo.model.form.FormClass)6 FormTree (org.activityinfo.model.formTree.FormTree)6 Test (org.junit.Test)6 SubFormReferenceType (org.activityinfo.model.type.subform.SubFormReferenceType)4 FormTreeBuilder (org.activityinfo.model.formTree.FormTreeBuilder)2 RecordRef (org.activityinfo.model.type.RecordRef)2 EnumType (org.activityinfo.model.type.enumerated.EnumType)2 CalculatedFieldType (org.activityinfo.model.type.expr.CalculatedFieldType)2 QuantityType (org.activityinfo.model.type.number.QuantityType)2 Optional (com.google.common.base.Optional)1 ValueUpdater (com.google.gwt.cell.client.ValueUpdater)1 LinkedList (java.util.LinkedList)1 FormProfile (org.activityinfo.geoadmin.merge2.view.profile.FormProfile)1 FormSection (org.activityinfo.model.form.FormSection)1 FormClassProvider (org.activityinfo.model.formTree.FormClassProvider)1 FormMetadataProviderStub (org.activityinfo.model.formTree.FormMetadataProviderStub)1 LookupKeySet (org.activityinfo.model.formTree.LookupKeySet)1