Example usage for org.springframework.data.mapping PersistentProperty isEntity

List of usage examples for org.springframework.data.mapping PersistentProperty isEntity

Introduction

In this page you can find the example usage for org.springframework.data.mapping PersistentProperty isEntity.

Prototype

boolean isEntity();

Source Link

Document

Returns whether the type of the PersistentProperty is actually to be regarded as PersistentEntity in turn.

Usage

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

private Collection<AbstractJsonSchemaProperty<?>> getNestedPropertiesFor(PersistentProperty<?> property,
        Definitions descriptors) {//from   www  .ja  va 2s.  co m

    if (!property.isEntity()) {
        return Collections.emptyList();
    }

    return getPropertiesFor(property.getActualType(),
            associations.getMappings().getMetadataFor(property.getActualType()), descriptors);
}

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

/**
 * Merges the given {@link ObjectNode} onto the given object.
 * // w  w  w.  j  av  a 2s. c  om
 * @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);
}

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

private List<AbstractJsonSchemaProperty<?>> getPropertiesFor(Class<?> type, final ResourceMetadata metadata,
        final Definitions definitions) {

    final PersistentEntity<?, ?> entity = entities.getPersistentEntity(type);
    final JacksonMetadata jackson = new JacksonMetadata(objectMapper, type);

    if (entity == null) {
        return Collections.<AbstractJsonSchemaProperty<?>>emptyList();
    }/*from ww w  .  ja  va  2s  .  c  o  m*/

    JsonSchemaPropertyRegistrar registrar = new JsonSchemaPropertyRegistrar(jackson);

    for (BeanPropertyDefinition definition : jackson) {

        PersistentProperty<?> persistentProperty = entity.getPersistentProperty(definition.getInternalName());

        // First pass, early drops to avoid unnecessary calculation
        if (persistentProperty != null) {

            if (persistentProperty.isIdProperty() && !configuration.isIdExposedFor(type)) {
                continue;
            }

            if (persistentProperty.isVersionProperty()) {
                continue;
            }

            if (!definition.couldSerialize()) {
                continue;
            }
        }

        AnnotatedMember primaryMember = definition.getPrimaryMember();

        if (primaryMember == null) {
            continue;
        }

        TypeInformation<?> propertyType = persistentProperty == null
                ? ClassTypeInformation.from(primaryMember.getRawType())
                : persistentProperty.getTypeInformation();
        TypeInformation<?> actualPropertyType = propertyType.getActualType();
        Class<?> rawPropertyType = propertyType.getType();

        JsonSchemaFormat format = configuration.getMetadataConfiguration().getSchemaFormatFor(rawPropertyType);
        ResourceDescription description = persistentProperty == null
                ? jackson.getFallbackDescription(metadata, definition)
                : getDescriptionFor(persistentProperty, metadata);
        JsonSchemaProperty property = getSchemaProperty(definition, propertyType, description);

        boolean isSyntheticProperty = persistentProperty == null;
        boolean isNotWritable = !isSyntheticProperty && !persistentProperty.isWritable();
        boolean isJacksonReadOnly = !isSyntheticProperty && jackson.isReadOnly(persistentProperty);

        if (isSyntheticProperty || isNotWritable || isJacksonReadOnly) {
            property = property.withReadOnly();
        }

        if (format != null) {

            // Types with explicitly registered format -> value object with format
            registrar.register(property.withFormat(format), actualPropertyType);
            continue;
        }

        Pattern pattern = configuration.getMetadataConfiguration().getPatternFor(rawPropertyType);

        if (pattern != null) {
            registrar.register(property.withPattern(pattern), actualPropertyType);
            continue;
        }

        if (jackson.isValueType()) {
            registrar.register(property.with(STRING_TYPE_INFORMATION), actualPropertyType);
            continue;
        }

        if (persistentProperty == null) {
            registrar.register(property, actualPropertyType);
            continue;
        }

        if (configuration.isLookupType(persistentProperty.getActualType())) {
            registrar.register(property.with(propertyType), actualPropertyType);
        } else if (associations.isLinkableAssociation(persistentProperty)) {
            registrar.register(property.asAssociation(), null);
        } else {

            if (persistentProperty.isEntity()) {

                if (!definitions.hasDefinitionFor(propertyType)) {
                    definitions.addDefinition(propertyType,
                            new Item(propertyType, getNestedPropertiesFor(persistentProperty, definitions)));
                }

                registrar.register(property.with(propertyType, Definitions.getReference(propertyType)),
                        actualPropertyType);

            } else {

                registrar.register(property.with(propertyType), actualPropertyType);
            }
        }
    }

    return registrar.getProperties();
}