Search in sources :

Example 21 with FormField

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

the class FormTreeBuilder method queryTree.

public FormTree queryTree(ResourceId rootFormId) {
    FormTree tree = new FormTree(rootFormId);
    FormMetadata root = metadataProvider.getFormMetadata(rootFormId);
    if (root.isDeleted()) {
        tree.setRootState(FormTree.State.DELETED);
        return tree;
    }
    if (!root.isVisible()) {
        tree.setRootState(FormTree.State.FORBIDDEN);
        return tree;
    }
    FormClass rootSchema = root.getSchema();
    List<ResourceId> stack = Lists.newArrayList(rootFormId);
    Optional<FormField> parentField = rootSchema.getParentField();
    if (parentField.isPresent()) {
        if (!stack.contains(parentField.get().getId())) {
            tree.addFormMetadata(root);
            FormTree.Node node = tree.addRootField(root, parentField.get());
            fetchChildren(stack, tree, node, rootSchema.getParentFormId().asSet());
        }
    }
    // Add fields defined by this FormClass
    for (FormField field : rootSchema.getFields()) {
        tree.addFormMetadata(root);
        FormTree.Node node = tree.addRootField(root, field);
        if (node.isReference()) {
            fetchChildren(stack, tree, node, node.getRange());
        } else if (field.getType() instanceof RecordFieldType) {
            addChildren(stack, tree, node, embeddedForm(node));
        }
    }
    return tree;
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) FormClass(org.activityinfo.model.form.FormClass) FormField(org.activityinfo.model.form.FormField) RecordFieldType(org.activityinfo.model.type.RecordFieldType) FormMetadata(org.activityinfo.model.form.FormMetadata)

Example 22 with FormField

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

the class FormSymbolTable method tryResolveSymbol.

public Optional<FormField> tryResolveSymbol(String name) {
    FormField field = idMap.get(name);
    if (field != null) {
        return Optional.of(field);
    }
    Collection<FormField> matching = codeMap.get(name.toLowerCase());
    if (matching.isEmpty()) {
        // as last resort, try matching against label
        matching = labelMap.get(name.toLowerCase());
    }
    if (matching.size() > 1) {
        throw new AmbiguousSymbolException(name);
    } else if (matching.isEmpty()) {
        return Optional.absent();
    } else {
        return Optional.of(Iterables.getOnlyElement(matching));
    }
}
Also used : AmbiguousSymbolException(org.activityinfo.model.formula.diagnostic.AmbiguousSymbolException) FormField(org.activityinfo.model.form.FormField)

Example 23 with FormField

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

the class FormEvalContextTest method circularRefs.

@Test
public void circularRefs() {
    FormField a = new FormField(ResourceId.generateId());
    a.setCode("A");
    a.setType(new CalculatedFieldType("B+1"));
    a.setLabel("A");
    FormField b = new FormField(ResourceId.generateId());
    b.setCode("B");
    b.setType(new CalculatedFieldType("A/50"));
    b.setLabel("B");
    FormClass formClass = new FormClass(ResourceId.generateId());
    formClass.addElement(a);
    formClass.addElement(b);
    FormEvalContext context = new FormEvalContext(formClass);
    context.setInstance(new FormInstance(ResourceId.generateSubmissionId(formClass), formClass.getId()));
    assertThat(context.getFieldValue(a.getId()), instanceOf(ErrorValue.class));
}
Also used : CalculatedFieldType(org.activityinfo.model.type.expr.CalculatedFieldType) FormClass(org.activityinfo.model.form.FormClass) ErrorValue(org.activityinfo.model.type.ErrorValue) FormInstance(org.activityinfo.model.form.FormInstance) FormField(org.activityinfo.model.form.FormField) FormEvalContext(org.activityinfo.model.form.FormEvalContext) Test(org.junit.Test)

Example 24 with FormField

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

the class CalculatedFieldTypeTest method emptySerialization.

@Test
public void emptySerialization() {
    FormField field = new FormField(ResourceId.generateId());
    field.setType(new CalculatedFieldType());
    JsonValue record = field.toJsonObject();
    System.out.println(record.toJson());
    FormField read = FormField.fromJson(record);
    assertThat(read.getType(), instanceOf(CalculatedFieldType.class));
    CalculatedFieldType readType = (CalculatedFieldType) read.getType();
    assertThat(readType.getExpression(), nullValue());
}
Also used : JsonValue(org.activityinfo.json.JsonValue) FormField(org.activityinfo.model.form.FormField) Test(org.junit.Test)

Example 25 with FormField

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

the class CalculatedFieldTypeTest method serialization.

@Test
public void serialization() {
    FormField field = new FormField(ResourceId.generateId());
    field.setType(new CalculatedFieldType("A+B"));
    JsonValue record = field.toJsonObject();
    System.out.println(record.toJson());
    FormField read = FormField.fromJson(record);
    assertThat(read.getType(), instanceOf(CalculatedFieldType.class));
    CalculatedFieldType readType = (CalculatedFieldType) read.getType();
    assertThat(readType.getExpression(), equalTo("A+B"));
}
Also used : JsonValue(org.activityinfo.json.JsonValue) FormField(org.activityinfo.model.form.FormField) Test(org.junit.Test)

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