Search in sources :

Example 11 with ComplexNode

use of org.hisp.dhis.node.types.ComplexNode in project dhis2-core by dhis2.

the class UserPropertyTransformerTest method testFieldNodeServiceSerializer.

@Test
void testFieldNodeServiceSerializer() throws JsonProcessingException {
    Simple simple = new Simple(1, "Simple1");
    simple.setUser(createUser('a'));
    simple.getUser().setUuid(uuid);
    simple.getUsers().add(createUser('A'));
    simple.getUsers().add(createUser('B'));
    simple.getUsers().add(createUser('C'));
    simple.getUsers().add(createUser('D'));
    ComplexNode complexNode = nodeService.toNode(simple);
    RootNode rootNode = NodeUtils.createRootNode(complexNode);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    nodeService.serialize(rootNode, "application/json", outputStream);
    String jsonSource = outputStream.toString();
    verifyJsonSource(jsonSource);
    Simple simpleFromJson = jsonMapper.readValue(jsonSource, Simple.class);
    assertEquals(1, simpleFromJson.getId());
    assertEquals("Simple1", simpleFromJson.getName());
    assertNotNull(simple.getUser());
    assertEquals("usernamea", simple.getUser().getUsername());
    assertEquals(uuid, simple.getUser().getUuid());
    assertNotNull(simple.getUsers());
    assertEquals(4, simple.getUsers().size());
    FieldFilterParams params = new FieldFilterParams(Collections.singletonList(simple), Collections.singletonList("id,name,user[id,code],users[id,code]"));
    fieldFilterService.toComplexNode(params);
}
Also used : RootNode(org.hisp.dhis.node.types.RootNode) ComplexNode(org.hisp.dhis.node.types.ComplexNode) FieldFilterParams(org.hisp.dhis.fieldfilter.FieldFilterParams) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 12 with ComplexNode

use of org.hisp.dhis.node.types.ComplexNode in project dhis2-core by dhis2.

the class UserPropertyTransformerTest method testNodeServiceSerializer.

@Test
void testNodeServiceSerializer() throws JsonProcessingException {
    Simple simple = new Simple(1, "Simple1");
    simple.setUser(createUser('a'));
    simple.getUser().setUuid(uuid);
    ComplexNode complexNode = nodeService.toNode(simple);
    RootNode rootNode = NodeUtils.createRootNode(complexNode);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    nodeService.serialize(rootNode, "application/json", outputStream);
    String jsonSource = outputStream.toString();
    verifyJsonSource(jsonSource);
    Simple simpleFromJson = jsonMapper.readValue(jsonSource, Simple.class);
    assertEquals(1, simpleFromJson.getId());
    assertEquals("Simple1", simpleFromJson.getName());
    assertNotNull(simple.getUser());
    assertEquals("usernamea", simple.getUser().getUsername());
    assertEquals(uuid, simple.getUser().getUuid());
}
Also used : RootNode(org.hisp.dhis.node.types.RootNode) ComplexNode(org.hisp.dhis.node.types.ComplexNode) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 13 with ComplexNode

use of org.hisp.dhis.node.types.ComplexNode in project dhis2-core by dhis2.

the class DefaultFieldFilterService method buildNode.

private AbstractNode buildNode(final FieldMap fieldMap, final Object klassInstance, final String namespace) {
    final ComplexNode complexNode = new ComplexNode(decapitalize(klassInstance.getClass().getSimpleName()));
    complexNode.setNamespace(namespace);
    for (final String fieldKey : fieldMap.keySet()) {
        try {
            final String originalName = org.apache.commons.lang3.StringUtils.substringBefore(fieldKey, "~");
            final String rename = org.apache.commons.lang3.StringUtils.substringBetween(fieldKey, "(", ")");
            final Field field = klassInstance.getClass().getDeclaredField(originalName);
            // NOSONAR
            field.setAccessible(true);
            final Object value = ReflectionUtils.invokeGetterMethod(originalName, klassInstance);
            if (org.apache.commons.lang3.StringUtils.isNotBlank(rename)) {
                complexNode.addChild(new SimpleNode(rename, value));
            } else {
                complexNode.addChild(new SimpleNode(originalName, value));
            }
        } catch (NoSuchFieldException e) {
            log.warn("Error reading attribute", e);
        }
    }
    return complexNode;
}
Also used : Field(java.lang.reflect.Field) ComplexNode(org.hisp.dhis.node.types.ComplexNode) EmbeddedObject(org.hisp.dhis.common.EmbeddedObject) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) SimpleNode(org.hisp.dhis.node.types.SimpleNode)

Example 14 with ComplexNode

use of org.hisp.dhis.node.types.ComplexNode in project dhis2-core by dhis2.

the class DefaultFieldFilterService method toComplexNode.

@Override
public ComplexNode toComplexNode(FieldFilterParams params) {
    if (params.getObjects().isEmpty()) {
        return null;
    }
    Object object = params.getObjects().get(0);
    CollectionNode collectionNode = toCollectionNode(object.getClass(), params);
    if (!collectionNode.getChildren().isEmpty()) {
        return (ComplexNode) collectionNode.getChildren().get(0);
    }
    return null;
}
Also used : ComplexNode(org.hisp.dhis.node.types.ComplexNode) EmbeddedObject(org.hisp.dhis.common.EmbeddedObject) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) CollectionNode(org.hisp.dhis.node.types.CollectionNode)

Example 15 with ComplexNode

use of org.hisp.dhis.node.types.ComplexNode in project dhis2-core by dhis2.

the class DefaultFieldFilterService method getProperties.

private ComplexNode getProperties(Property currentProperty, Object object, List<String> fields) {
    if (object == null) {
        return null;
    }
    // objects
    if (isBaseIdentifiableObjectIdOnly(object, fields)) {
        return createBaseIdentifiableObjectIdNode(currentProperty, object);
    }
    ComplexNode complexNode = new ComplexNode(currentProperty.getName());
    complexNode.setNamespace(currentProperty.getNamespace());
    complexNode.setProperty(currentProperty);
    Schema schema;
    if (currentProperty.hasPropertyTransformer()) {
        schema = schemaService.getDynamicSchema(HibernateProxyUtils.getRealClass(object));
    } else if (currentProperty.isCollection()) {
        schema = schemaService.getDynamicSchema(currentProperty.getItemKlass());
    } else {
        schema = schemaService.getDynamicSchema(currentProperty.getKlass());
    }
    for (String field : fields) {
        Property property = schema.getProperty(field);
        if (property == null) {
            continue;
        }
        Object returnValue = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
        SimpleNode simpleNode = new SimpleNode(field, returnValue);
        simpleNode.setAttribute(property.isAttribute());
        simpleNode.setNamespace(property.getNamespace());
        simpleNode.setProperty(property);
        complexNode.addChild(simpleNode);
    }
    return complexNode;
}
Also used : ComplexNode(org.hisp.dhis.node.types.ComplexNode) Schema(org.hisp.dhis.schema.Schema) EmbeddedObject(org.hisp.dhis.common.EmbeddedObject) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Property(org.hisp.dhis.schema.Property) SimpleNode(org.hisp.dhis.node.types.SimpleNode)

Aggregations

ComplexNode (org.hisp.dhis.node.types.ComplexNode)33 SimpleNode (org.hisp.dhis.node.types.SimpleNode)23 CollectionNode (org.hisp.dhis.node.types.CollectionNode)21 RootNode (org.hisp.dhis.node.types.RootNode)15 Test (org.junit.jupiter.api.Test)9 ArrayList (java.util.ArrayList)7 DhisSpringTest (org.hisp.dhis.DhisSpringTest)5 EmbeddedObject (org.hisp.dhis.common.EmbeddedObject)5 Node (org.hisp.dhis.node.Node)5 CategoryOption (org.hisp.dhis.category.CategoryOption)4 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)4 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)4 MetadataVersion (org.hisp.dhis.metadata.version.MetadataVersion)4 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)4 Schema (org.hisp.dhis.schema.Schema)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 FieldFilterParams (org.hisp.dhis.fieldfilter.FieldFilterParams)3 Property (org.hisp.dhis.schema.Property)3 Collection (java.util.Collection)2 Category (org.hisp.dhis.category.Category)2