List of usage examples for org.springframework.data.mapping PersistentEntity getPropertyAccessor
<B> PersistentPropertyAccessor<B> getPropertyAccessor(B bean);
From source file:org.springframework.data.rest.webmvc.json.Jackson2DatatypeHelperIntegrationTests.java
/** * @see DATAREST-500/* w ww .j a v a2s .c o m*/ */ @Test public void configuresHIbernate4ModuleToLoadLazyLoadingProxies() throws Exception { PersistentEntity<?, ?> entity = entities.getPersistentEntity(Order.class); PersistentProperty<?> property = entity.getPersistentProperty("creator"); PersistentPropertyAccessor accessor = entity.getPropertyAccessor(orders.findOne(this.order.getId())); assertThat(objectMapper.writeValueAsString(accessor.getProperty(property)), is(not("null"))); }
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 . j a v a2 s .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));
}
From source file:org.springframework.data.rest.webmvc.json.DomainObjectReader.java
/** * Merges the given {@link ObjectNode} onto the given object. * /*from ww w. jav a2 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); }