Search in sources :

Example 1 with FormInstance

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

the class RecordTreeLoaderTest method subforms.

@Test
public void subforms() {
    IncidentForm incidentForm = setup.getCatalog().getIncidentForm();
    RecordRef rootRecordRef = incidentForm.getRecordRef(0);
    Observable<Maybe<RecordTree>> recordTree = setup.getFormStore().getRecordTree(rootRecordRef);
    Connection<Maybe<RecordTree>> recordTreeView = setup.connect(recordTree);
    Iterable<FormInstance> subRecords = recordTreeView.assertLoaded().get().getSubRecords(rootRecordRef, ReferralSubForm.FORM_ID);
    assertThat(Iterables.size(subRecords), equalTo(4));
}
Also used : Maybe(org.activityinfo.promise.Maybe) RecordRef(org.activityinfo.model.type.RecordRef) FormInstance(org.activityinfo.model.form.FormInstance) IncidentForm(org.activityinfo.store.testing.IncidentForm) Test(org.junit.Test)

Example 2 with FormInstance

use of org.activityinfo.model.form.FormInstance 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)

Example 3 with FormInstance

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

the class SimpleFormPanel method onFieldUpdated.

public void onFieldUpdated(FormField field, FieldValue newValue) {
    Optional<FormInstance> workingInstance = model.getWorkingInstance(field.getId(), getSelectedKey(field));
    if (workingInstance.isPresent()) {
        if (!Objects.equals(workingInstance.get().get(field.getId()), newValue)) {
            workingInstance.get().set(field.getId(), newValue);
            // skip handler must be applied after workingInstance is updated
            relevanceHandler.onValueChange();
        }
        validateField(widgetCreator.get(field.getId()));
        model.getChangedInstances().add(workingInstance.get());
    } else {
        FormClass formClass = model.getClassByField(field.getId());
        if (formClass.isSubForm()) {
            widgetCreator.get(field.getId()).setInvalid(I18N.CONSTANTS.subFormTabNotSelected());
            widgetCreator.get(field.getId()).getFieldWidget().clearValue();
        }
    }
}
Also used : FormClass(org.activityinfo.model.form.FormClass) FormInstance(org.activityinfo.model.form.FormInstance)

Example 4 with FormInstance

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

the class FormModel method getWorkingInstance.

public Optional<FormInstance> getWorkingInstance(ResourceId formFieldId, String keyId) {
    FormClass classByField = getClassByField(formFieldId);
    if (classByField.equals(rootFormClass)) {
        return Optional.of(getWorkingRootInstance());
    }
    if (classByField.isSubForm()) {
        Optional<FormInstance> valueInstance = getSubformValueInstance(classByField, getWorkingRootInstance(), keyId);
        if (valueInstance.isPresent()) {
            return valueInstance;
        } else {
            FormInstance newInstance = new FormInstance(ResourceId.generatedPeriodSubmissionId(getWorkingRootInstance().getId(), keyId), classByField.getId());
            newInstance.setParentRecordId(getWorkingRootInstance().getId());
            SubformValueKey valueKey = new SubformValueKey(classByField, getWorkingRootInstance());
            Set<FormInstance> allInstances = subFormInstances.get(valueKey);
            if (allInstances == null) {
                allInstances = Sets.newHashSet();
                subFormInstances.put(valueKey, allInstances);
            }
            allInstances.add(newInstance);
            return Optional.of(newInstance);
        }
    }
    throw new RuntimeException("Failed to identify working instance for field: " + formFieldId + ", keyId: " + keyId);
}
Also used : FormClass(org.activityinfo.model.form.FormClass) FormInstance(org.activityinfo.model.form.FormInstance)

Example 5 with FormInstance

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

the class RepeatingSubFormPanel method newValueInstance.

private FormInstance newValueInstance() {
    FormInstance newInstance = new FormInstance(ResourceId.generateSubmissionId(subForm.getId()), subForm.getId());
    newInstance.setParentRecordId(formModel.getWorkingRootInstance().getId());
    FormModel.SubformValueKey key = key();
    Set<FormInstance> instances = formModel.getSubFormInstances().get(key);
    if (instances == null) {
        instances = Sets.newHashSet();
        formModel.getSubFormInstances().put(key, instances);
    }
    instances.add(newInstance);
    formModel.getChangedInstances().add(newInstance);
    return newInstance;
}
Also used : FormModel(org.activityinfo.ui.client.component.form.FormModel) FormInstance(org.activityinfo.model.form.FormInstance)

Aggregations

FormInstance (org.activityinfo.model.form.FormInstance)46 Test (org.junit.Test)26 ResourceId (org.activityinfo.model.resource.ResourceId)15 FormClass (org.activityinfo.model.form.FormClass)14 FieldValue (org.activityinfo.model.type.FieldValue)10 FormField (org.activityinfo.model.form.FormField)9 RecordRef (org.activityinfo.model.type.RecordRef)8 ReferenceValue (org.activityinfo.model.type.ReferenceValue)7 LocalDate (org.activityinfo.model.type.time.LocalDate)7 EnumValue (org.activityinfo.model.type.enumerated.EnumValue)6 GeoPoint (org.activityinfo.model.type.geo.GeoPoint)6 OnDataSet (org.activityinfo.server.database.OnDataSet)6 KeyGenerator (org.activityinfo.model.legacy.KeyGenerator)5 SerialNumberType (org.activityinfo.model.type.SerialNumberType)5 FormRecord (org.activityinfo.model.form.FormRecord)4 SerialNumber (org.activityinfo.model.type.SerialNumber)4 Map (java.util.Map)3 FormTree (org.activityinfo.model.formTree.FormTree)3 Maybe (org.activityinfo.promise.Maybe)3 ArrayList (java.util.ArrayList)2