Example usage for org.springframework.data.rest.webmvc.json MappedProperties getPersistentProperty

List of usage examples for org.springframework.data.rest.webmvc.json MappedProperties getPersistentProperty

Introduction

In this page you can find the example usage for org.springframework.data.rest.webmvc.json MappedProperties getPersistentProperty.

Prototype

public PersistentProperty<?> getPersistentProperty(String fieldName) 

Source Link

Usage

From source file:org.springframework.data.rest.webmvc.json.DomainObjectReader.java

/**
 * Merges the given {@link ObjectNode} onto the given object.
 * //from  w  w  w  .j  a va2 s. c o m
 * @param root must not be {@literal null}.
 * @param target must not be {@literal null}.
 * @param mapper must not be {@literal null}.
 * @return
 * @throws Exception
 */
private <T> T doMerge(ObjectNode root, T target, ObjectMapper mapper) throws Exception {

    Assert.notNull(root, "Root ObjectNode must not be null!");
    Assert.notNull(target, "Target object instance must not be null!");
    Assert.notNull(mapper, "ObjectMapper must not be null!");

    PersistentEntity<?, ?> entity = entities.getPersistentEntity(target.getClass());

    if (entity == null) {
        return mapper.readerForUpdating(target).readValue(root);
    }

    MappedProperties mappedProperties = getJacksonProperties(entity, mapper);

    for (Iterator<Entry<String, JsonNode>> i = root.fields(); i.hasNext();) {

        Entry<String, JsonNode> entry = i.next();
        JsonNode child = entry.getValue();

        if (child.isArray()) {
            continue;
        }

        String fieldName = entry.getKey();

        if (!mappedProperties.hasPersistentPropertyForField(fieldName)) {
            i.remove();
            continue;
        }

        if (child.isObject()) {

            PersistentProperty<?> property = mappedProperties.getPersistentProperty(fieldName);

            if (associationLinks.isLinkableAssociation(property)) {
                continue;
            }

            PersistentPropertyAccessor accessor = entity.getPropertyAccessor(target);
            Object nested = accessor.getProperty(property);

            ObjectNode objectNode = (ObjectNode) child;

            if (property.isMap()) {

                // Keep empty Map to wipe it as expected
                if (!objectNode.fieldNames().hasNext()) {
                    continue;
                }

                doMergeNestedMap((Map<String, Object>) nested, objectNode, mapper);

                // Remove potentially emptied Map as values have been handled recursively
                if (!objectNode.fieldNames().hasNext()) {
                    i.remove();
                }

                continue;
            }

            if (nested != null && property.isEntity()) {
                doMerge(objectNode, nested, mapper);
            }
        }
    }

    return mapper.readerForUpdating(target).readValue(root);
}