Search in sources :

Example 91 with ResourceId

use of org.activityinfo.model.resource.ResourceId in project activityinfo by bedatadriven.

the class FormField method toJsonObject.

@Override
public JsonValue toJsonObject() {
    JsonValue object = createObject();
    object.put("id", id.asString());
    object.put("code", code);
    object.put("label", label);
    object.put("description", description);
    object.put("relevanceCondition", relevanceConditionExpression);
    object.put("visible", visible);
    object.put("required", required);
    object.put("type", type.getTypeClass().getId());
    if (key) {
        object.put("key", true);
    }
    if (!superProperties.isEmpty()) {
        JsonValue superPropertiesArray = Json.createArray();
        for (ResourceId superProperty : superProperties) {
            superPropertiesArray.add(Json.createFromNullable(superProperty.asString()));
        }
        object.put("superProperties", superPropertiesArray);
    }
    if (type instanceof ParametrizedFieldType) {
        object.put("typeParameters", ((ParametrizedFieldType) type).getParametersAsJson());
    }
    return object;
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) JsonValue(org.activityinfo.json.JsonValue)

Example 92 with ResourceId

use of org.activityinfo.model.resource.ResourceId in project activityinfo by bedatadriven.

the class FormInstance method toJsonObject.

public JsonValue toJsonObject() {
    JsonValue fields = createObject();
    for (Map.Entry<ResourceId, FieldValue> entry : fieldMap.entrySet()) {
        if (entry.getValue() != null) {
            fields.put(entry.getKey().asString(), entry.getValue().toJson());
        }
    }
    JsonValue object = createObject();
    object.put("formId", getFormId().asString());
    object.put("recordId", getId().asString());
    object.put("fieldValues", fields);
    return object;
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) JsonValue(org.activityinfo.json.JsonValue) FieldValue(org.activityinfo.model.type.FieldValue) BooleanFieldValue(org.activityinfo.model.type.primitive.BooleanFieldValue) HashMap(java.util.HashMap) Map(java.util.Map)

Example 93 with ResourceId

use of org.activityinfo.model.resource.ResourceId 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 94 with ResourceId

use of org.activityinfo.model.resource.ResourceId in project activityinfo by bedatadriven.

the class FormClass method fromJson.

public static FormClass fromJson(JsonValue object) {
    // Deal with previous encoding
    ResourceId id;
    if (object.hasKey("@id")) {
        id = ResourceId.valueOf(object.get("@id").asString());
    } else {
        id = ResourceId.valueOf(object.get("id").asString());
    }
    FormClass formClass = new FormClass(id);
    if (object.hasKey("schemaVersion")) {
        formClass.setSchemaVersion(object.get("schemaVersion").asLong());
    }
    if (object.hasKey("databaseId")) {
        formClass.setDatabaseId(ResourceId.valueOf(object.get("databaseId").asString()));
    }
    if (object.hasKey("_class_label")) {
        formClass.setLabel(JsonParsing.toNullableString(object.get("_class_label")));
    } else {
        formClass.setLabel(JsonParsing.toNullableString(object.get("label")));
    }
    if (object.hasKey("subFormKind")) {
        formClass.setSubFormKind(SubFormKind.valueOf(object.get("subFormKind").asString().toUpperCase()));
        formClass.setParentFormId(ResourceId.valueOf(object.get("parentFormId").asString()));
    }
    if (object.hasKey("elements")) {
        JsonValue elements = object.get("elements");
        if (elements.isJsonArray()) {
            JsonValue elementsArray = elements;
            formClass.elements.addAll(fromJsonArray(elementsArray));
        }
    }
    return formClass;
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) JsonValue(org.activityinfo.json.JsonValue)

Example 95 with ResourceId

use of org.activityinfo.model.resource.ResourceId 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

ResourceId (org.activityinfo.model.resource.ResourceId)198 Test (org.junit.Test)42 FormClass (org.activityinfo.model.form.FormClass)41 FormField (org.activityinfo.model.form.FormField)34 RecordRef (org.activityinfo.model.type.RecordRef)30 JsonValue (org.activityinfo.json.JsonValue)21 FormTree (org.activityinfo.model.formTree.FormTree)18 ReferenceValue (org.activityinfo.model.type.ReferenceValue)17 FieldValue (org.activityinfo.model.type.FieldValue)16 FormInstance (org.activityinfo.model.form.FormInstance)15 HashMap (java.util.HashMap)14 QuantityType (org.activityinfo.model.type.number.QuantityType)13 ColumnSet (org.activityinfo.model.query.ColumnSet)12 QueryModel (org.activityinfo.model.query.QueryModel)12 EnumValue (org.activityinfo.model.type.enumerated.EnumValue)11 SubFormReferenceType (org.activityinfo.model.type.subform.SubFormReferenceType)11 ReferenceType (org.activityinfo.model.type.ReferenceType)10 EnumItem (org.activityinfo.model.type.enumerated.EnumItem)10 FormStorage (org.activityinfo.store.spi.FormStorage)10 ArrayList (java.util.ArrayList)9