Example usage for org.springframework.data.mapping PersistentEntity getPersistentProperty

List of usage examples for org.springframework.data.mapping PersistentEntity getPersistentProperty

Introduction

In this page you can find the example usage for org.springframework.data.mapping PersistentEntity getPersistentProperty.

Prototype

@Nullable
default P getPersistentProperty(Class<? extends Annotation> annotationType) 

Source Link

Document

Returns the first property equipped with an Annotation of the given type.

Usage

From source file:org.lightadmin.core.web.RepositoryFilePropertyController.java

@RequestMapping(value = BASE_MAPPING + "/binary", method = GET)
public ResponseEntity<?> filePropertyValueOfEntity(RootResourceInformation repoRequest,
        @BackendId Serializable id, @PathVariable String property) throws Exception {
    PersistentEntity<?, ?> persistentEntity = repoRequest.getPersistentEntity();
    RepositoryInvoker invoker = repoRequest.getInvoker();

    Object domainObj = invoker.invokeFindOne(id);

    if (null == domainObj) {
        throw new ResourceNotFoundException();
    }/*  ww w  .  ja v  a  2s. com*/

    PersistentProperty<?> prop = persistentEntity.getPersistentProperty(property);
    if (null == prop) {
        throw new ResourceNotFoundException();
    }

    if (isOfFileType(prop)) {
        return toResponseEntity(OK, new HttpHeaders(),
                new Resource<FilePropertyValue>(evaluateFilePropertyValue(domainObj, prop)));
    }

    return toEmptyResponse(METHOD_NOT_ALLOWED);
}

From source file:org.lightadmin.core.web.RepositoryFilePropertyController.java

@RequestMapping(value = BASE_MAPPING + "/file", method = DELETE)
public ResponseEntity<?> deleteFileOfPropertyOfEntity(RootResourceInformation repoRequest,
        @BackendId Serializable id, @PathVariable String property) throws Exception {
    PersistentEntity<?, ?> persistentEntity = repoRequest.getPersistentEntity();
    RepositoryInvoker invoker = repoRequest.getInvoker();

    Object domainObj = invoker.invokeFindOne(id);

    if (null == domainObj) {
        throw new ResourceNotFoundException();
    }/*from w ww  .ja  va 2  s .  c  o m*/

    PersistentProperty<?> prop = persistentEntity.getPersistentProperty(property);
    if (null == prop) {
        throw new ResourceNotFoundException();
    }

    if (!isOfFileType(prop)) {
        return toEmptyResponse(METHOD_NOT_ALLOWED);
    }

    fileResourceStorage().delete(domainObj, prop);

    invoker.invokeSave(domainObj);

    return toEmptyResponse(OK);
}

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  w ww.  j  av  a 2  s .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();
}