Search in sources :

Example 1 with Schema

use of org.hisp.dhis.dto.schemas.Schema in project dhis2-core by dhis2.

the class DataGenerator method generateObjectMatchingSchema.

public static JsonObject generateObjectMatchingSchema(List<SchemaProperty> schemaProperties) {
    JsonObject objectBody = new JsonObject();
    for (SchemaProperty prop : schemaProperties) {
        JsonElement element;
        if (prop.getPropertyType() == PropertyType.REFERENCE) {
            List<SchemaProperty> referenceProperties = new SchemasActions().getRequiredProperties(prop.getName());
            JsonObject referenceObject = generateObjectMatchingSchema(referenceProperties);
            String uid = new RestApiActions(prop.getRelativeApiEndpoint()).post(referenceObject).extractUid();
            referenceObject.addProperty("id", uid);
            element = referenceObject;
        } else if (prop.getPropertyType() == PropertyType.IDENTIFIER) {
            if (!StringUtils.containsAny(prop.getName(), "id", "uid", "code")) {
                Schema schema = new SchemasActions().getSchema(prop.getName());
                JsonObject referenceObject = generateObjectMatchingSchema(schema.getRequiredProperties());
                String uid = new RestApiActions(schema.getPlural()).post(referenceObject).extractUid();
                element = new JsonPrimitive(uid);
            } else {
                element = new JsonPrimitive(new IdGenerator().generateUniqueId());
            }
        } else {
            element = generateRandomValueMatchingSchema(prop);
        }
        objectBody.add(prop.getName(), element);
    }
    return objectBody;
}
Also used : RestApiActions(org.hisp.dhis.actions.RestApiActions) SchemaProperty(org.hisp.dhis.dto.schemas.SchemaProperty) JsonPrimitive(com.google.gson.JsonPrimitive) JsonElement(com.google.gson.JsonElement) SchemasActions(org.hisp.dhis.actions.SchemasActions) Schema(org.hisp.dhis.dto.schemas.Schema) JsonObject(com.google.gson.JsonObject) IdGenerator(org.hisp.dhis.actions.IdGenerator)

Aggregations

JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 IdGenerator (org.hisp.dhis.actions.IdGenerator)1 RestApiActions (org.hisp.dhis.actions.RestApiActions)1 SchemasActions (org.hisp.dhis.actions.SchemasActions)1 Schema (org.hisp.dhis.dto.schemas.Schema)1 SchemaProperty (org.hisp.dhis.dto.schemas.SchemaProperty)1