use of org.activityinfo.model.form.FormClass in project activityinfo by bedatadriven.
the class FormTreePrettyPrinter method prettyPrintNodes.
public void prettyPrintNodes(int indent, List<FormTree.Node> nodes) {
List<FormClass> formClasses = distinctFormClasses(nodes);
pw.println(classNode(formClasses));
for (FormClass formClass : formClasses) {
if (formClasses.size() > 1) {
println(indent, classNode(Collections.singletonList(formClass)));
}
printFields(indent + 1, nodes, formClass.getId());
}
pw.flush();
}
use of org.activityinfo.model.form.FormClass in project activityinfo by bedatadriven.
the class JsonFormTreeBuilder method fromJson.
public static FormTree fromJson(JsonValue object) {
ResourceId rootFormClassId = ResourceId.valueOf(object.get("root").asString());
JsonValue forms = object.get("forms");
final Map<ResourceId, FormClass> formMap = new HashMap<>();
for (String key : forms.keys()) {
JsonValue value = forms.get(key);
FormClass formClass = FormClass.fromJson(value);
formMap.put(formClass.getId(), formClass);
}
FormClassProvider provider = new FormClassProvider() {
@Override
public FormClass getFormClass(ResourceId formId) {
FormClass formClass = formMap.get(formId);
assert formClass != null : "FormClass " + formId + " was referenced but not include in the " + "list of forms";
return formClass;
}
};
FormTreeBuilder builder = new FormTreeBuilder(provider);
return builder.queryTree(rootFormClassId);
}
use of org.activityinfo.model.form.FormClass in project activityinfo by bedatadriven.
the class XlsFormBuilderTest method test.
@Test
public void test() throws IOException {
final FormClass formClass = new FormClass(ResourceId.valueOf("F1"));
formClass.addField(ResourceId.valueOf("X1")).setLabel("What is your name?").setRequired(true).setCode("RespName").setType(TextType.SIMPLE);
formClass.addField(ResourceId.valueOf("X2")).setLabel("How old are you?").setRequired(true).setCode("Age").setType(new QuantityType("years"));
formClass.addField(ResourceId.valueOf("X3")).setLabel("Age in dog years").setRequired(true).setCode("AgeDogYears").setType(new CalculatedFieldType("Age*7"));
formClass.addField(ResourceId.valueOf("X4")).setLabel("Date of birth").setRequired(false).setCode("DOB").setType(LocalDateType.INSTANCE);
formClass.addField(ResourceId.valueOf("X5")).setLabel("What is your favorite color?").setRequired(false).setCode("Color").setType(new EnumType(Cardinality.SINGLE, Lists.newArrayList(new EnumItem(ResourceId.valueOf("Z1"), "Blue"), new EnumItem(ResourceId.valueOf("Z2"), "Red"), new EnumItem(ResourceId.valueOf("Z3"), "Green"))));
formClass.addField(ResourceId.valueOf("X6")).setLabel("Who are your brothers?").setCode("Brothers").setType(new SubFormReferenceType(ResourceId.valueOf("F2")));
formClass.addField(ResourceId.valueOf("X7")).setLabel("Likes any color?").setCode("Colorful").setType(BooleanType.INSTANCE).setRelevanceConditionExpression("containsAny(X5,Z1,Z2,Z3)");
formClass.addField(ResourceId.valueOf("X8")).setLabel("Likes the color blue?").setCode("BlueLover").setType(BooleanType.INSTANCE).setRelevanceConditionExpression("X5==Z1");
formClass.addField(ResourceId.valueOf("X9")).setLabel("Likes the color red?").setCode("RedLover").setType(BooleanType.INSTANCE).setRelevanceConditionExpression("X5==\'Z2\'");
final FormClass subFormClass = new FormClass(ResourceId.valueOf("F2"));
subFormClass.setLabel("Sub Form");
subFormClass.addField(ResourceId.valueOf("Y1")).setLabel("What is his name?").setType(TextType.SIMPLE).setCode("Name");
FormClassProvider provider = new FormClassProvider() {
@Override
public FormClass getFormClass(ResourceId formId) {
if (formId.equals(formClass.getId())) {
return formClass;
} else if (formId.equals(subFormClass.getId())) {
return subFormClass;
} else {
throw new IllegalArgumentException();
}
}
};
XlsFormBuilder writer = new XlsFormBuilder(provider);
writer.build(formClass.getId());
try (FileOutputStream fos = new FileOutputStream("test.xls")) {
writer.write(fos);
}
}
use of org.activityinfo.model.form.FormClass in project activityinfo by bedatadriven.
the class XlsFormBuilder method writeSubForm.
private void writeSubForm(FormField field) {
SubFormReferenceType subFormType = (SubFormReferenceType) field.getType();
FormClass formClass = formClassProvider.getFormClass(subFormType.getClassId());
HSSFRow beginRow = surveySheet.createRow(nextFieldRow++);
beginRow.createCell(TYPE_COLUMN).setCellValue("begin repeat");
beginRow.createCell(NAME_COLUMN).setCellValue(field.getCode());
writeFields(formClass);
HSSFRow endRow = surveySheet.createRow(nextFieldRow++);
endRow.createCell(TYPE_COLUMN).setCellValue("end repeat");
}
use of org.activityinfo.model.form.FormClass in project activityinfo by bedatadriven.
the class QuantityTypeTest method deserialization.
/**
* Ensure QuantityType is parsed correctly from JSON.
* In particular, ensure that the "aggregation" field is correctly initialised from a legacy schema with no
* aggregation field within Quantity JSON element.
*
* @throws IOException
*/
@Test
public void deserialization() throws IOException {
FormClass formClass = parseResource();
ResourceId quantityFieldId = ResourceId.valueOf("i0000006090");
FormField quantityField = formClass.getField(quantityFieldId);
// Quantity Checks
assertThat(quantityField.getLabel(), equalTo("Number of water points constructed"));
assertThat(quantityField.isRequired(), is(false));
assertThat(quantityField.isVisible(), is(true));
// QuantityType Checks
assertThat(quantityField.getType(), instanceOf(QuantityType.class));
QuantityType quantityType = (QuantityType) quantityField.getType();
assertThat(quantityType.getUnits(), equalTo("Waterpoints"));
assertThat(quantityType.getAggregation(), is(notNullValue()));
assertThat(quantityType.getAggregation(), equalTo(QuantityType.Aggregation.SUM));
assertThat(quantityType.getAggregation().ordinal(), equalTo(0));
}
Aggregations