Search in sources :

Example 66 with JsonValue

use of org.activityinfo.json.JsonValue 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 67 with JsonValue

use of org.activityinfo.json.JsonValue in project activityinfo by bedatadriven.

the class JsonFormTreeBuilder method toJson.

public static JsonValue toJson(FormTree tree) {
    ResourceId rootFormClassId = tree.getRootFormId();
    JsonValue forms = createObject();
    collectForms(forms, tree.getRootFields());
    JsonValue object = createObject();
    object.put("root", rootFormClassId.asString());
    object.put("forms", forms);
    return object;
}
Also used : ResourceId(org.activityinfo.model.resource.ResourceId) JsonValue(org.activityinfo.json.JsonValue)

Example 68 with JsonValue

use of org.activityinfo.json.JsonValue 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 69 with JsonValue

use of org.activityinfo.json.JsonValue 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 70 with JsonValue

use of org.activityinfo.json.JsonValue in project activityinfo by bedatadriven.

the class FormInstance method toFormInstance.

public static FormInstance toFormInstance(FormClass formClass, FormRecord record) {
    FormInstance instance = new FormInstance(ResourceId.valueOf(record.getRecordId()), formClass.getId());
    if (record.getParentRecordId() != null) {
        instance.setParentRecordId(ResourceId.valueOf(record.getParentRecordId()));
    }
    JsonValue fieldsObject = record.getFields();
    for (FormField field : formClass.getFields()) {
        if (fieldsObject.hasKey(field.getName())) {
            JsonValue fieldValue = fieldsObject.get(field.getName());
            if (!fieldValue.isJsonNull()) {
                instance.set(field.getId(), field.getType().parseJsonValue(fieldValue));
            }
        }
    }
    return instance;
}
Also used : JsonValue(org.activityinfo.json.JsonValue)

Aggregations

JsonValue (org.activityinfo.json.JsonValue)117 Test (org.junit.Test)24 ResourceId (org.activityinfo.model.resource.ResourceId)19 FormClass (org.activityinfo.model.form.FormClass)13 FormField (org.activityinfo.model.form.FormField)9 FieldValue (org.activityinfo.model.type.FieldValue)8 HashMap (java.util.HashMap)6 Map (java.util.Map)6 QuantityType (org.activityinfo.model.type.number.QuantityType)5 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Annotation (java.lang.annotation.Annotation)3 ArrayList (java.util.ArrayList)3 FormTreeBuilder (org.activityinfo.model.formTree.FormTreeBuilder)3 JaxRsJsonReader (org.activityinfo.server.endpoint.rest.JaxRsJsonReader)3 TypedRecordUpdate (org.activityinfo.store.spi.TypedRecordUpdate)3 EmbeddedEntity (com.google.appengine.api.datastore.EmbeddedEntity)2 URL (java.net.URL)2 JsonException (org.activityinfo.json.JsonException)2 JsonMappingException (org.activityinfo.json.JsonMappingException)2