Search in sources :

Example 31 with FormField

use of org.activityinfo.model.form.FormField in project activityinfo by bedatadriven.

the class CloneDatabaseTest method assertFormClass.

private void assertFormClass(FormClass sourceFormClass, FormClass targetFormClass) {
    assertNotEquals(sourceFormClass.getId(), targetFormClass.getId());
    assertEquals(sourceFormClass.getLabel(), targetFormClass.getLabel());
    assertEquals(sourceFormClass.getDescription(), targetFormClass.getDescription());
    // fields
    for (FormField sourceField : sourceFormClass.getFields()) {
        FormField targetField = (FormField) elementByName(targetFormClass.getElements(), sourceField.getLabel());
        assertNotEquals(sourceField.getId(), targetField.getId());
        assertEquals(sourceField.getDescription(), targetField.getDescription());
        assertEquals(sourceField.getCode(), targetField.getCode());
        assertEquals(sourceField.getRelevanceConditionExpression(), targetField.getRelevanceConditionExpression());
        assertEquals(sourceField.getType().getTypeClass(), targetField.getType().getTypeClass());
        // todo
        if (sourceField.getType() instanceof ReferenceType) {
        // need something more sophisticated to check equality of ReferenceType
        } else if (sourceField.getType() instanceof EnumType) {
        // need something more sophisticated to check equality of ReferenceType
        }
    }
    // sections
    for (FormSection sourceSection : sourceFormClass.getSections()) {
        FormSection targetSection = (FormSection) elementByName(targetFormClass.getElements(), sourceSection.getLabel());
        assertNotEquals(sourceSection.getId(), targetSection.getId());
        assertEquals(sourceSection.getLabel(), targetSection.getLabel());
    }
}
Also used : FormSection(org.activityinfo.model.form.FormSection) EnumType(org.activityinfo.model.type.enumerated.EnumType) FormField(org.activityinfo.model.form.FormField) ReferenceType(org.activityinfo.model.type.ReferenceType)

Example 32 with FormField

use of org.activityinfo.model.form.FormField in project activityinfo by bedatadriven.

the class FormModelTest method doNotPersistFormClassWithStaleSubformReference.

@Ignore
@Test
public void doNotPersistFormClassWithStaleSubformReference() {
    setupForms();
    FormClass formClass = new FormClass(ResourceId.generateId());
    formClass.setDatabaseId(DATABASE_ID);
    FormClass subform = new FormClass(ResourceId.generateId());
    subform.setDatabaseId(DATABASE_ID);
    FormField subformOwnerField = formClass.addField(CuidAdapter.generateIndicatorId());
    subformOwnerField.setType(new SubFormReferenceType(subform.getId()));
    locator.persist(formClass).then(new AsyncCallback<Void>() {

        @Override
        public void onFailure(Throwable caught) {
        // expected result
        }

        @Override
        public void onSuccess(Void result) {
            throw new RuntimeException("FormClass is persisted with stale (non-existent) SubFormClass reference.");
        }
    });
}
Also used : SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) FormClass(org.activityinfo.model.form.FormClass) FormField(org.activityinfo.model.form.FormField) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 33 with FormField

use of org.activityinfo.model.form.FormField in project activityinfo by bedatadriven.

the class FormModelTest method setupForms.

/**
 * Sets up test fixtures. Must be called by each test to ensure that it runs AFTER
 * the dbunit setup
 */
public void setupForms() {
    ResourceId masterFormId = CuidAdapter.activityFormClass(3);
    masterFormClass = new FormClass(masterFormId);
    masterFormClass.setDatabaseId(CuidAdapter.databaseId(1));
    FormField partnerField = new FormField(CuidAdapter.partnerField(3));
    partnerField.setLabel("Partner");
    partnerField.setType(ReferenceType.single(CuidAdapter.partnerFormId(1)));
    masterFormClass.addElement(partnerField);
    FormField labelField = masterFormClass.addField(CuidAdapter.generateIndicatorId());
    labelField.setLabel("label1");
    labelField.setType(TextType.SIMPLE);
    subFormClass = new FormClass(ResourceId.generateId());
    subFormClass.setDatabaseId(masterFormClass.getDatabaseId());
    subFormClass.setParentFormId(masterFormId);
    subFormClass.setSubFormKind(SubFormKind.MONTHLY);
    subFormChildField = subFormClass.addField();
    subFormChildField.setType(TextType.SIMPLE);
    subFormField = masterFormClass.addField(CuidAdapter.generateIndicatorId());
    subFormField.setType(new SubFormReferenceType(subFormClass.getId()));
    assertResolves(locator.persist(subFormClass));
    assertResolves(locator.persist(masterFormClass));
}
Also used : SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) ResourceId(org.activityinfo.model.resource.ResourceId) FormClass(org.activityinfo.model.form.FormClass) FormField(org.activityinfo.model.form.FormField)

Example 34 with FormField

use of org.activityinfo.model.form.FormField in project activityinfo by bedatadriven.

the class HierarchyTest method buildViewModelTest.

@Test
public void buildViewModelTest() {
    FormClass campForm = assertResolves(locator.getFormClass(CAMP_CLASS));
    FormField adminField = campForm.getField(CuidAdapter.field(CAMP_CLASS, CuidAdapter.ADMIN_FIELD));
    Set<RecordRef> fieldValue = Collections.singleton(new RecordRef(CAMP_DISTRICT_CLASS, entity(325703)));
    Hierarchy tree = assertResolves(Hierarchy.get(locator, (ReferenceType) adminField.getType()));
    prettyPrintTree(tree);
    assertThat(tree.getRoots(), hasSize(1));
    createWidgets(tree);
    Presenter presenter = new Presenter(locator, tree, widgets, new ValueUpdater() {

        @Override
        public void update(Object value) {
            System.out.println("VALUE = " + value);
        }
    });
    assertResolves(presenter.setInitialSelection(fieldValue));
    assertThat(presenter.getSelectionLabel(CAMP_DISTRICT_CLASS), equalTo("District 5"));
    // now try to get options for the root level
    List<Choice> choices = assertResolves(widgets.get(REGION).choices.get());
    System.out.println(choices);
    assertThat(choices, hasSize(3));
    // if we change the root item, then all descendants should be cleared
    widgets.get(REGION).setSelection("South");
    prettyPrintWidgets(tree);
    assertThat(widgets.get(CAMP_DISTRICT_CLASS).selection, isEmptyOrNullString());
    assertThat(widgets.get(GOVERNORATE_ID).choices, Matchers.notNullValue());
    List<Choice> governorateChoices = assertResolves(widgets.get(GOVERNORATE_ID).choices.get());
    System.out.println(governorateChoices);
    assertThat(governorateChoices, hasSize(4));
}
Also used : ValueUpdater(com.google.gwt.cell.client.ValueUpdater) FormClass(org.activityinfo.model.form.FormClass) RecordRef(org.activityinfo.model.type.RecordRef) FormField(org.activityinfo.model.form.FormField) ReferenceType(org.activityinfo.model.type.ReferenceType) Test(org.junit.Test)

Example 35 with FormField

use of org.activityinfo.model.form.FormField in project activityinfo by bedatadriven.

the class FixSubForm method maybeFixForm.

private boolean maybeFixForm(final PrintWriter logger, MySqlQueryExecutor executor, ResourceId parentFormId) {
    logger.println("Fixing " + parentFormId + "...");
    final MySqlStorageProvider catalog = new MySqlStorageProvider(executor);
    FormStorage parentForm = catalog.getForm(parentFormId).get();
    final FormClass formClass = parentForm.getFormClass();
    logger.println("Loaded activity " + parentForm.getFormClass().getLabel());
    final List<FormField> updated = new ArrayList<>();
    ObjectifyService.run(new VoidWork() {

        @Override
        public void vrun() {
            Hrd.ofy().transact(new VoidWork() {

                @Override
                public void vrun() {
                    for (FormField formField : formClass.getFields()) {
                        if (formField.getType() instanceof SubFormReferenceType) {
                            if (maybeFixForm(logger, formClass, formField)) {
                                updated.add(formField);
                            }
                        }
                    }
                }
            });
        }
    });
    logger.println("TX COMPLETED!");
    if (!updated.isEmpty()) {
        logger.println("Updating parent form schema...");
        catalog.createOrUpdateFormSchema(formClass);
        return true;
    } else {
        return false;
    }
}
Also used : SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) FormStorage(org.activityinfo.store.spi.FormStorage) VoidWork(com.googlecode.objectify.VoidWork) FormClass(org.activityinfo.model.form.FormClass) ArrayList(java.util.ArrayList) MySqlStorageProvider(org.activityinfo.store.mysql.MySqlStorageProvider) FormField(org.activityinfo.model.form.FormField)

Aggregations

FormField (org.activityinfo.model.form.FormField)119 FormClass (org.activityinfo.model.form.FormClass)48 ResourceId (org.activityinfo.model.resource.ResourceId)32 Test (org.junit.Test)30 QuantityType (org.activityinfo.model.type.number.QuantityType)18 SubFormReferenceType (org.activityinfo.model.type.subform.SubFormReferenceType)17 ReferenceType (org.activityinfo.model.type.ReferenceType)16 EnumType (org.activityinfo.model.type.enumerated.EnumType)14 FieldValue (org.activityinfo.model.type.FieldValue)12 EnumItem (org.activityinfo.model.type.enumerated.EnumItem)11 JsonValue (org.activityinfo.json.JsonValue)9 FormTree (org.activityinfo.model.formTree.FormTree)9 FormInstance (org.activityinfo.model.form.FormInstance)8 CalculatedFieldType (org.activityinfo.model.type.expr.CalculatedFieldType)8 ColumnSet (org.activityinfo.model.query.ColumnSet)6 QueryModel (org.activityinfo.model.query.QueryModel)6 KeyGenerator (org.activityinfo.model.legacy.KeyGenerator)5 Quantity (org.activityinfo.model.type.number.Quantity)5 TypedRecordUpdate (org.activityinfo.store.spi.TypedRecordUpdate)5 ArrayList (java.util.ArrayList)4