Search in sources :

Example 1 with FormClass

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

the class NewDimensionDialog method onForestUpdated.

private void onForestUpdated(Observable<FormForest> forest) {
    store.clear();
    if (forest.isLoaded()) {
        // The Form Name can be a dimension
        store.add(new FormNode());
        // Add root fields...
        for (FormTree.Node node : forest.get().getRootNodes()) {
            if (isEligible(node.getType())) {
                store.add(new RootFieldNode(node.getDefiningFormClass().getId(), node.getField()));
            }
        }
        // Add reference forms with eligible fields
        for (FormClass formClass : forest.get().getReferencedForms()) {
            List<DimensionNode> children = new ArrayList<>();
            for (FormField field : formClass.getFields()) {
                if (isEligible(field.getType())) {
                    children.add(new ReferencedNode(formClass, field));
                }
            }
            if (!children.isEmpty()) {
                ReferenceFormNode refNode = new ReferenceFormNode(formClass, children.get(0));
                store.add(refNode);
                store.add(refNode, children);
            }
        }
    }
}
Also used : FormTree(org.activityinfo.model.formTree.FormTree) FormClass(org.activityinfo.model.form.FormClass) ArrayList(java.util.ArrayList) FormField(org.activityinfo.model.form.FormField)

Example 2 with FormClass

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

the class FormulaValidatorTest method invalidFormSchema.

@Test
public void invalidFormSchema() {
    FormClass formClass = new FormClass(ResourceId.valueOf("XYZ"));
    formClass.addField(ResourceId.valueOf("F1")).setCode("A").setLabel("Field A1").setType(TextType.SIMPLE);
    formClass.addField(ResourceId.valueOf("F2")).setCode("A").setLabel("Field A2").setType(TextType.SIMPLE);
    TestBatchFormClassProvider formProvider = new TestBatchFormClassProvider();
    formProvider.add(formClass);
    FormTreeBuilder formTreeBuilder = new FormTreeBuilder(formProvider);
    FormTree formTree = formTreeBuilder.queryTree(formClass.getId());
    FormulaValidator validator = new FormulaValidator(formTree);
    assertFalse(validator.validate(FormulaParser.parse("A")));
    assertThat(validator.getErrors(), hasSize(1));
}
Also used : FormulaValidator(org.activityinfo.analysis.FormulaValidator) FormTree(org.activityinfo.model.formTree.FormTree) FormClass(org.activityinfo.model.form.FormClass) TestBatchFormClassProvider(org.activityinfo.model.formTree.TestBatchFormClassProvider) FormTreeBuilder(org.activityinfo.model.formTree.FormTreeBuilder) Test(org.junit.Test)

Example 3 with FormClass

use of org.activityinfo.model.form.FormClass 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"))));
}
Also used : FormMetadataProvider(org.activityinfo.model.formTree.FormMetadataProvider) RecordRef(org.activityinfo.model.type.RecordRef) FormInputModel(org.activityinfo.ui.client.input.model.FormInputModel) FormTreeBuilder(org.activityinfo.model.formTree.FormTreeBuilder) FormMetadata(org.activityinfo.model.form.FormMetadata) SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) FormTree(org.activityinfo.model.formTree.FormTree) ResourceId(org.activityinfo.model.resource.ResourceId) FormClass(org.activityinfo.model.form.FormClass) TestingFormStore(org.activityinfo.ui.client.store.TestingFormStore) FormStore(org.activityinfo.ui.client.store.FormStore) Test(org.junit.Test)

Example 4 with FormClass

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

the class SubFormPane method addOrUpdateTab.

private void addOrUpdateTab(FormTree formTree, FormTree.Node node) {
    TabItemConfig tabItemConfig = tabs.get(node.getFieldId());
    if (tabItemConfig == null) {
        SubFormReferenceType subFormType = (SubFormReferenceType) node.getType();
        FormClass subForm = formTree.getFormClass(subFormType.getClassId());
        TabItemConfig config = new TabItemConfig(subForm.getLabel());
        tabs.put(node.getFieldId(), config);
        add(new SubFormGrid(viewModel, subForm.getId()), config);
    }
}
Also used : SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) FormClass(org.activityinfo.model.form.FormClass) TabItemConfig(com.sencha.gxt.widget.core.client.TabItemConfig)

Example 5 with FormClass

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

the class RelevanceHandler method applyRelevanceLogic.

private void applyRelevanceLogic(final FormField field) {
    if (field.hasRelevanceConditionExpression()) {
        try {
            FormulaLexer lexer = new FormulaLexer(field.getRelevanceConditionExpression());
            FormulaParser parser = new FormulaParser(lexer);
            FormulaNode expr = parser.parse();
            FieldContainer fieldContainer = simpleFormPanel.getWidgetCreator().get(field.getId());
            if (fieldContainer != null) {
                FormModel model = simpleFormPanel.getModel();
                Optional<FormInstance> instance = model.getWorkingInstance(field.getId(), simpleFormPanel.getSelectedKey(field));
                FormClass formClass = model.getClassByField(field.getId());
                boolean relevant;
                if (instance.isPresent()) {
                    relevant = expr.evaluateAsBoolean(new FormEvalContext(formClass, instance.get()));
                } else {
                    relevant = expr.evaluateAsBoolean(new FormEvalContext(formClass, new FormInstance(ResourceId.generateSubmissionId(formClass), formClass.getId())));
                }
                fieldContainer.getFieldWidget().setReadOnly(!relevant);
                if (!relevant) {
                    if (resettingValues) {
                        // we are in resetting state -> handle nested relevance
                        fieldContainer.getFieldWidget().clearValue();
                        fieldContainer.getFieldWidget().fireValueChanged();
                    }
                    fieldsWithAppliedRelevance.add(fieldContainer);
                } else {
                    fieldsWithAppliedRelevance.remove(fieldContainer);
                }
            } else {
                Log.error("Can't find container for fieldId: " + field.getId() + ", fieldName: " + field.getLabel() + ", expression: " + field.getRelevanceConditionExpression());
            }
        } catch (Exception e) {
            Log.error("Error: Unable to apply relevance logic. FieldId: " + field.getId() + ", fieldName: " + field.getLabel() + ", expression: " + field.getRelevanceConditionExpression(), e);
        }
    }
}
Also used : FormulaNode(org.activityinfo.model.formula.FormulaNode) FormulaLexer(org.activityinfo.model.formula.FormulaLexer) FormClass(org.activityinfo.model.form.FormClass) FormulaParser(org.activityinfo.model.formula.FormulaParser) FormInstance(org.activityinfo.model.form.FormInstance) FormEvalContext(org.activityinfo.model.form.FormEvalContext)

Aggregations

FormClass (org.activityinfo.model.form.FormClass)109 FormField (org.activityinfo.model.form.FormField)49 ResourceId (org.activityinfo.model.resource.ResourceId)41 Test (org.junit.Test)38 QuantityType (org.activityinfo.model.type.number.QuantityType)20 SubFormReferenceType (org.activityinfo.model.type.subform.SubFormReferenceType)19 FormInstance (org.activityinfo.model.form.FormInstance)14 EnumType (org.activityinfo.model.type.enumerated.EnumType)12 JsonValue (org.activityinfo.json.JsonValue)11 FormTree (org.activityinfo.model.formTree.FormTree)10 EnumItem (org.activityinfo.model.type.enumerated.EnumItem)10 ColumnSet (org.activityinfo.model.query.ColumnSet)8 QueryModel (org.activityinfo.model.query.QueryModel)8 CalculatedFieldType (org.activityinfo.model.type.expr.CalculatedFieldType)8 FieldValue (org.activityinfo.model.type.FieldValue)7 Quantity (org.activityinfo.model.type.number.Quantity)7 FormTreeBuilder (org.activityinfo.model.formTree.FormTreeBuilder)6 KeyGenerator (org.activityinfo.model.legacy.KeyGenerator)6 ColumnSetBuilder (org.activityinfo.store.query.server.ColumnSetBuilder)6 ColumnView (org.activityinfo.model.query.ColumnView)5