DATAES-369 - Adapt to API changes in mapping subsystem.

This commit is contained in:
Mark Paluch 2017-06-30 11:41:40 +02:00
parent 4219973b84
commit b5bab6b85e
7 changed files with 37 additions and 60 deletions

View File

@ -22,7 +22,6 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Optional;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.get.GetResponse;
@ -176,15 +175,12 @@ public class DefaultResultMapper extends AbstractResultMapper {
if (mappingContext != null && clazz.isAnnotationPresent(Document.class)) { if (mappingContext != null && clazz.isAnnotationPresent(Document.class)) {
ElasticsearchPersistentEntity<?> persistentEntity = mappingContext.getRequiredPersistentEntity(clazz); ElasticsearchPersistentEntity<?> persistentEntity = mappingContext.getRequiredPersistentEntity(clazz);
Optional<ElasticsearchPersistentProperty> idProperty = persistentEntity.getIdProperty(); ElasticsearchPersistentProperty idProperty = persistentEntity.getIdProperty();
// Only deal with String because ES generated Ids are strings ! // Only deal with String because ES generated Ids are strings !
if (idProperty != null && idProperty.getType().isAssignableFrom(String.class)) {
idProperty.ifPresent(property -> { persistentEntity.getPropertyAccessor(result).setProperty(idProperty, id);
if (property.getType().isAssignableFrom(String.class)) { }
persistentEntity.getPropertyAccessor(result).setProperty(property, Optional.ofNullable(id));
}
});
} }
} }

View File

@ -26,7 +26,6 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Optional;
import org.elasticsearch.action.ListenableActionFuture; import org.elasticsearch.action.ListenableActionFuture;
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest; import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest;
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest; import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest;
@ -187,12 +186,10 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
XContentBuilder xContentBuilder = null; XContentBuilder xContentBuilder = null;
try { try {
Optional<ElasticsearchPersistentProperty> idProperty = persistentEntity.getIdProperty(); ElasticsearchPersistentProperty property = persistentEntity.getRequiredIdProperty();
ElasticsearchPersistentProperty property = idProperty.orElseThrow(() -> new IllegalArgumentException(String.format("No Id property for %s found", clazz.getSimpleName())));
xContentBuilder = buildMapping(clazz, persistentEntity.getIndexType(), xContentBuilder = buildMapping(clazz, persistentEntity.getIndexType(),
property.getFieldName(), persistentEntity.getParentType().orElse(null)); property.getFieldName(), persistentEntity.getParentType());
} catch (Exception e) { } catch (Exception e) {
throw new ElasticsearchException("Failed to build mapping for " + clazz.getSimpleName(), e); throw new ElasticsearchException("Failed to build mapping for " + clazz.getSimpleName(), e);
} }
@ -1102,24 +1099,25 @@ public class ElasticsearchTemplate implements ElasticsearchOperations, Applicati
private String getPersistentEntityId(Object entity) { private String getPersistentEntityId(Object entity) {
ElasticsearchPersistentEntity<?> persistentEntity = getPersistentEntityFor(entity.getClass()); ElasticsearchPersistentEntity<?> persistentEntity = getPersistentEntityFor(entity.getClass());
Optional<Object> identifier = persistentEntity.getIdentifierAccessor(entity).getIdentifier(); Object identifier = persistentEntity.getIdentifierAccessor(entity).getIdentifier();
return identifier.map(String::valueOf).orElse(null); if (identifier != null){
return identifier.toString();
}
return null;
} }
private void setPersistentEntityId(Object entity, String id) { private void setPersistentEntityId(Object entity, String id) {
ElasticsearchPersistentEntity<?> persistentEntity = getPersistentEntityFor(entity.getClass()); ElasticsearchPersistentEntity<?> persistentEntity = getPersistentEntityFor(entity.getClass());
Optional<ElasticsearchPersistentProperty> idProperty = persistentEntity.getIdProperty(); ElasticsearchPersistentProperty idProperty = persistentEntity.getIdProperty();
// Only deal with text because ES generated Ids are strings ! // Only deal with text because ES generated Ids are strings !
idProperty.ifPresent(property -> { if (idProperty != null && idProperty.getType().isAssignableFrom(String.class)) {
persistentEntity.getPropertyAccessor(entity).setProperty(idProperty, id);
if (property.getType().isAssignableFrom(String.class)) { }
persistentEntity.getPropertyAccessor(entity).setProperty(property, Optional.ofNullable(id));
}
});
} }
private void setPersistentEntityIndexAndType(Query query, Class clazz) { private void setPersistentEntityIndexAndType(Query query, Class clazz) {

View File

@ -15,8 +15,6 @@
*/ */
package org.springframework.data.elasticsearch.core.mapping; package org.springframework.data.elasticsearch.core.mapping;
import java.util.Optional;
import org.springframework.data.mapping.PersistentEntity; import org.springframework.data.mapping.PersistentEntity;
/** /**
@ -42,11 +40,11 @@ public interface ElasticsearchPersistentEntity<T> extends PersistentEntity<T, El
String getIndexStoreType(); String getIndexStoreType();
Optional<ElasticsearchPersistentProperty> getVersionProperty(); ElasticsearchPersistentProperty getVersionProperty();
Optional<String> getParentType(); String getParentType();
Optional<ElasticsearchPersistentProperty> getParentIdProperty(); ElasticsearchPersistentProperty getParentIdProperty();
String settingPath(); String settingPath();

View File

@ -57,8 +57,8 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
private short replicas; private short replicas;
private String refreshInterval; private String refreshInterval;
private String indexStoreType; private String indexStoreType;
private Optional<String> parentType = Optional.empty(); private String parentType;
private Optional<ElasticsearchPersistentProperty> parentIdProperty = Optional.empty(); private ElasticsearchPersistentProperty parentIdProperty;
private String settingPath; private String settingPath;
private boolean createIndexAndMapping; private boolean createIndexAndMapping;
@ -131,12 +131,12 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
} }
@Override @Override
public Optional<String> getParentType() { public String getParentType() {
return parentType; return parentType;
} }
@Override @Override
public Optional<ElasticsearchPersistentProperty> getParentIdProperty() { public ElasticsearchPersistentProperty getParentIdProperty() {
return parentIdProperty; return parentIdProperty;
} }
@ -154,15 +154,15 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
public void addPersistentProperty(ElasticsearchPersistentProperty property) { public void addPersistentProperty(ElasticsearchPersistentProperty property) {
super.addPersistentProperty(property); super.addPersistentProperty(property);
Optional<Parent> annotation = property.findAnnotation(Parent.class); Parent annotation = property.findAnnotation(Parent.class);
annotation.ifPresent(parent -> { if (annotation != null) {
Assert.isTrue(!this.parentIdProperty.isPresent(), "Only one field can hold a @Parent annotation"); Assert.isNull(this.parentIdProperty, "Only one field can hold a @Parent annotation");
Assert.isTrue(!this.parentType.isPresent(), "Only one field can hold a @Parent annotation"); Assert.isNull(this.parentType, "Only one field can hold a @Parent annotation");
Assert.isTrue(property.getType() == String.class, "Parent ID property should be String"); Assert.isTrue(property.getType() == String.class, "Parent ID property should be String");
this.parentIdProperty = Optional.of(property); this.parentIdProperty = property;
this.parentType = Optional.of(parent.type()); this.parentType = annotation.type();
}); }
if (property.isVersionProperty()) { if (property.isVersionProperty()) {
Assert.isTrue(property.getType() == Long.class, "Version property should be Long"); Assert.isTrue(property.getType() == Long.class, "Version property should be Long");

View File

@ -316,7 +316,7 @@ public abstract class AbstractElasticsearchRepository<T, ID extends Serializable
} }
protected ID extractIdFromBean(T entity) { protected ID extractIdFromBean(T entity) {
return entityInformation.getId(entity).orElse(null); return entityInformation.getId(entity);
} }
private List<String> stringIdsRepresentation(Iterable<ID> ids) { private List<String> stringIdsRepresentation(Iterable<ID> ids) {

View File

@ -15,8 +15,6 @@
*/ */
package org.springframework.data.elasticsearch.repository.support; package org.springframework.data.elasticsearch.repository.support;
import java.util.Optional;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity; import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty; import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
import org.springframework.data.repository.core.support.PersistentEntityInformation; import org.springframework.data.repository.core.support.PersistentEntityInformation;
@ -59,13 +57,7 @@ public class MappingElasticsearchEntityInformation<T, ID> extends PersistentEnti
@Override @Override
public String getIdAttribute() { public String getIdAttribute() {
return entityMetadata.getRequiredIdProperty().getFieldName();
ElasticsearchPersistentProperty property = entityMetadata.getIdProperty()
.orElseThrow(() -> new IllegalArgumentException(String.format(
"Unable to identify 'id' property in class %s. Make sure the 'id' property is annotated with @Id or named as 'id' or 'documentId'",
entityMetadata.getType().getSimpleName())));
return property.getFieldName();
} }
@Override @Override
@ -81,13 +73,9 @@ public class MappingElasticsearchEntityInformation<T, ID> extends PersistentEnti
@Override @Override
public Long getVersion(T entity) { public Long getVersion(T entity) {
Optional<ElasticsearchPersistentProperty> versionProperty = entityMetadata.getVersionProperty(); ElasticsearchPersistentProperty versionProperty = entityMetadata.getVersionProperty();
try { try {
return versionProperty != null ? (Long) entityMetadata.getPropertyAccessor(entity).getProperty(versionProperty) : null;
return (Long) versionProperty //
.flatMap(property -> entityMetadata.getPropertyAccessor(entity).getProperty(property)) //
.orElse(null);
} catch (Exception e) { } catch (Exception e) {
throw new IllegalStateException("failed to load version field", e); throw new IllegalStateException("failed to load version field", e);
} }
@ -96,12 +84,9 @@ public class MappingElasticsearchEntityInformation<T, ID> extends PersistentEnti
@Override @Override
public String getParentId(T entity) { public String getParentId(T entity) {
Optional<ElasticsearchPersistentProperty> parentProperty = entityMetadata.getParentIdProperty(); ElasticsearchPersistentProperty parentProperty = entityMetadata.getParentIdProperty();
try { try {
return parentProperty != null ? (String) entityMetadata.getPropertyAccessor(entity).getProperty(parentProperty) : null;
return (String) parentProperty //
.flatMap(property -> entityMetadata.getPropertyAccessor(entity).getProperty(property)) //
.orElse(null);
} catch (Exception e) { } catch (Exception e) {
throw new IllegalStateException("failed to load parent ID: " + e, e); throw new IllegalStateException("failed to load parent ID: " + e, e);
} }

View File

@ -19,7 +19,7 @@ import java.beans.IntrospectionException;
import org.junit.Test; import org.junit.Test;
import org.springframework.data.annotation.Version; import org.springframework.data.annotation.Version;
import org.springframework.data.mapping.model.MappingException; import org.springframework.data.mapping.MappingException;
import org.springframework.data.mapping.model.Property; import org.springframework.data.mapping.model.Property;
import org.springframework.data.mapping.model.SimpleTypeHolder; import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.data.util.ClassTypeInformation; import org.springframework.data.util.ClassTypeInformation;