Search in sources :

Example 61 with FormClass

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();
}
Also used : FormClass(org.activityinfo.model.form.FormClass)

Example 62 with FormClass

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);
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) HashMap(java.util.HashMap) FormClass(org.activityinfo.model.form.FormClass) JsonValue(org.activityinfo.json.JsonValue)

Example 63 with FormClass

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);
    }
}
Also used : SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) FormClassProvider(org.activityinfo.model.formTree.FormClassProvider) CalculatedFieldType(org.activityinfo.model.type.expr.CalculatedFieldType) QuantityType(org.activityinfo.model.type.number.QuantityType) ResourceId(org.activityinfo.model.resource.ResourceId) FormClass(org.activityinfo.model.form.FormClass) EnumType(org.activityinfo.model.type.enumerated.EnumType) FileOutputStream(java.io.FileOutputStream) EnumItem(org.activityinfo.model.type.enumerated.EnumItem) Test(org.junit.Test)

Example 64 with FormClass

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");
}
Also used : SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) FormClass(org.activityinfo.model.form.FormClass) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow)

Example 65 with FormClass

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));
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) QuantityType(org.activityinfo.model.type.number.QuantityType) FormClass(org.activityinfo.model.form.FormClass) FormField(org.activityinfo.model.form.FormField) Test(org.junit.Test)

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