Search in sources :

Example 1 with FormClassProvider

use of org.activityinfo.model.formTree.FormClassProvider in project activityinfo by bedatadriven.

the class ActivityInfoClientAsyncImpl method getFormTree.

@Override
public Promise<FormTree> getFormTree(final ResourceId formId) {
    return get(formUrl(formId) + "/tree", jsonElement -> {
        JsonValue root = jsonElement;
        JsonValue forms = root.get("forms");
        final Map<ResourceId, FormClass> formMap = new HashMap<>();
        for (Map.Entry<String, JsonValue> entry : forms.entrySet()) {
            FormClass formClass = FormClass.fromJson(entry.getValue());
            formMap.put(formClass.getId(), formClass);
        }
        FormTreeBuilder builder = new FormTreeBuilder(new FormClassProvider() {

            @Override
            public FormClass getFormClass(ResourceId formId1) {
                FormClass formClass = formMap.get(formId1);
                assert formClass != null;
                return formClass;
            }
        });
        return builder.queryTree(formId);
    });
}
Also used : FormClassProvider(org.activityinfo.model.formTree.FormClassProvider) ResourceId(org.activityinfo.model.resource.ResourceId) HashMap(java.util.HashMap) JsonValue(org.activityinfo.json.JsonValue) HashMap(java.util.HashMap) Map(java.util.Map) FormTreeBuilder(org.activityinfo.model.formTree.FormTreeBuilder)

Example 2 with FormClassProvider

use of org.activityinfo.model.formTree.FormClassProvider in project activityinfo by bedatadriven.

the class NodeMatcherTest method tree.

private FormTree tree() {
    if (rootFormClass == null) {
        throw new IllegalStateException("Root FormClass is unset");
    }
    FormClassProvider provider = new FormClassProvider() {

        @Override
        public FormClass getFormClass(ResourceId formId) {
            if (rootFormClass.getId().equals(formId)) {
                return rootFormClass;
            }
            FormClass formClass = formClasses.get(formId);
            if (formClass == null) {
                throw new IllegalArgumentException(formId.toString());
            }
            return formClass;
        }
    };
    FormTreeBuilder builder = new FormTreeBuilder(provider);
    return builder.queryTree(rootFormClass.getId());
}
Also used : FormClassProvider(org.activityinfo.model.formTree.FormClassProvider) ResourceId(org.activityinfo.model.resource.ResourceId) FormClass(org.activityinfo.model.form.FormClass) FormTreeBuilder(org.activityinfo.model.formTree.FormTreeBuilder)

Example 3 with FormClassProvider

use of org.activityinfo.model.formTree.FormClassProvider in project activityinfo by bedatadriven.

the class BatchingFormTreeBuilder method queryTrees.

public Map<ResourceId, FormTree> queryTrees(Collection<ResourceId> rootFormIds) {
    // Fetch Required FormClasses in batches
    Set<ResourceId> toFetch = Sets.newHashSet();
    toFetch.addAll(rootFormIds);
    while (!toFetch.isEmpty()) {
        // First round: fetch root form classes
        List<FormClass> fetched = fetchFormClasses(toFetch);
        toFetch.clear();
        // Find newly referenced forms
        for (FormClass formClass : fetched) {
            if (formClass.isSubForm()) {
                if (!formCache.containsKey(formClass.getParentFormId().get())) {
                    toFetch.add(formClass.getParentFormId().get());
                }
            }
            for (FormField formField : formClass.getFields()) {
                if (formField.getType() instanceof ReferenceType) {
                    ReferenceType refType = (ReferenceType) formField.getType();
                    for (ResourceId refFormId : refType.getRange()) {
                        if (!formCache.containsKey(refFormId)) {
                            toFetch.add(refFormId);
                        }
                    }
                } else if (formField.getType() instanceof SubFormReferenceType) {
                    SubFormReferenceType subFormType = (SubFormReferenceType) formField.getType();
                    if (!formCache.containsKey(subFormType.getClassId())) {
                        toFetch.add(subFormType.getClassId());
                    }
                }
            }
        }
    }
    // Now assemble trees
    Map<ResourceId, FormTree> treeMap = new HashMap<>();
    FormTreeBuilder builder = new FormTreeBuilder(new FormClassProvider() {

        @Override
        public FormClass getFormClass(ResourceId formId) {
            return formCache.get(formId);
        }
    });
    for (ResourceId rootFormId : rootFormIds) {
        treeMap.put(rootFormId, builder.queryTree(rootFormId));
    }
    return treeMap;
}
Also used : SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) ReferenceType(org.activityinfo.model.type.ReferenceType) FormTreeBuilder(org.activityinfo.model.formTree.FormTreeBuilder) SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) FormClassProvider(org.activityinfo.model.formTree.FormClassProvider) FormTree(org.activityinfo.model.formTree.FormTree) ResourceId(org.activityinfo.model.resource.ResourceId) FormClass(org.activityinfo.model.form.FormClass) FormField(org.activityinfo.model.form.FormField)

Example 4 with FormClassProvider

use of org.activityinfo.model.formTree.FormClassProvider 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)

Aggregations

FormClassProvider (org.activityinfo.model.formTree.FormClassProvider)4 ResourceId (org.activityinfo.model.resource.ResourceId)4 FormClass (org.activityinfo.model.form.FormClass)3 FormTreeBuilder (org.activityinfo.model.formTree.FormTreeBuilder)3 SubFormReferenceType (org.activityinfo.model.type.subform.SubFormReferenceType)2 FileOutputStream (java.io.FileOutputStream)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 JsonValue (org.activityinfo.json.JsonValue)1 FormField (org.activityinfo.model.form.FormField)1 FormTree (org.activityinfo.model.formTree.FormTree)1 ReferenceType (org.activityinfo.model.type.ReferenceType)1 EnumItem (org.activityinfo.model.type.enumerated.EnumItem)1 EnumType (org.activityinfo.model.type.enumerated.EnumType)1 CalculatedFieldType (org.activityinfo.model.type.expr.CalculatedFieldType)1 QuantityType (org.activityinfo.model.type.number.QuantityType)1 Test (org.junit.Test)1