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;
}
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));
}
}
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));
}
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());
}
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"));
}
Aggregations