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

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

Introduction

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

Prototype

IdentifierAccessor getIdentifierAccessor(Object bean);

Source Link

Document

Returns the IdentifierAccessor for the given bean.

Usage

From source file:org.springframework.data.rest.webmvc.config.PersistentEntityResourceHandlerMethodArgumentResolver.java

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
        NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {

    RootResourceInformation resourceInformation = resourceInformationResolver.resolveArgument(parameter,
            mavContainer, webRequest, binderFactory);

    HttpServletRequest nativeRequest = webRequest.getNativeRequest(HttpServletRequest.class);
    ServletServerHttpRequest request = new ServletServerHttpRequest(nativeRequest);
    IncomingRequest incoming = new IncomingRequest(request);

    Class<?> domainType = resourceInformation.getDomainType();
    MediaType contentType = request.getHeaders().getContentType();

    for (HttpMessageConverter converter : messageConverters) {

        if (!converter.canRead(PersistentEntityResource.class, contentType)) {
            continue;
        }/*from  w  w  w  .  jav  a2s .c  o  m*/

        Serializable id = idResolver.resolveArgument(parameter, mavContainer, webRequest, binderFactory);
        Object objectToUpdate = getObjectToUpdate(id, resourceInformation);

        boolean forUpdate = false;
        Object entityIdentifier = null;
        PersistentEntity<?, ?> entity = resourceInformation.getPersistentEntity();

        if (objectToUpdate != null) {
            forUpdate = true;
            entityIdentifier = entity.getIdentifierAccessor(objectToUpdate).getIdentifier();
        }

        Object obj = read(resourceInformation, incoming, converter, objectToUpdate);

        if (obj == null) {
            throw new HttpMessageNotReadableException(String.format(ERROR_MESSAGE, domainType));
        }

        if (entityIdentifier != null) {
            entity.getPropertyAccessor(obj).setProperty(entity.getIdProperty(), entityIdentifier);
        }

        Builder build = PersistentEntityResource.build(obj, entity);
        return forUpdate ? build.build() : build.forCreation();
    }

    throw new HttpMessageNotReadableException(String.format(NO_CONVERTER_FOUND, domainType, contentType));
}