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

View File

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

View File

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

View File

@ -57,8 +57,8 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
private short replicas;
private String refreshInterval;
private String indexStoreType;
private Optional<String> parentType = Optional.empty();
private Optional<ElasticsearchPersistentProperty> parentIdProperty = Optional.empty();
private String parentType;
private ElasticsearchPersistentProperty parentIdProperty;
private String settingPath;
private boolean createIndexAndMapping;
@ -131,12 +131,12 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
}
@Override
public Optional<String> getParentType() {
public String getParentType() {
return parentType;
}
@Override
public Optional<ElasticsearchPersistentProperty> getParentIdProperty() {
public ElasticsearchPersistentProperty getParentIdProperty() {
return parentIdProperty;
}
@ -154,15 +154,15 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
public void addPersistentProperty(ElasticsearchPersistentProperty property) {
super.addPersistentProperty(property);
Optional<Parent> annotation = property.findAnnotation(Parent.class);
Parent annotation = property.findAnnotation(Parent.class);
annotation.ifPresent(parent -> {
Assert.isTrue(!this.parentIdProperty.isPresent(), "Only one field can hold a @Parent annotation");
Assert.isTrue(!this.parentType.isPresent(), "Only one field can hold a @Parent annotation");
if (annotation != null) {
Assert.isNull(this.parentIdProperty, "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");
this.parentIdProperty = Optional.of(property);
this.parentType = Optional.of(parent.type());
});
this.parentIdProperty = property;
this.parentType = annotation.type();
}
if (property.isVersionProperty()) {
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) {
return entityInformation.getId(entity).orElse(null);
return entityInformation.getId(entity);
}
private List<String> stringIdsRepresentation(Iterable<ID> ids) {

View File

@ -15,8 +15,6 @@
*/
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.ElasticsearchPersistentProperty;
import org.springframework.data.repository.core.support.PersistentEntityInformation;
@ -59,13 +57,7 @@ public class MappingElasticsearchEntityInformation<T, ID> extends PersistentEnti
@Override
public String getIdAttribute() {
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();
return entityMetadata.getRequiredIdProperty().getFieldName();
}
@Override
@ -81,13 +73,9 @@ public class MappingElasticsearchEntityInformation<T, ID> extends PersistentEnti
@Override
public Long getVersion(T entity) {
Optional<ElasticsearchPersistentProperty> versionProperty = entityMetadata.getVersionProperty();
ElasticsearchPersistentProperty versionProperty = entityMetadata.getVersionProperty();
try {
return (Long) versionProperty //
.flatMap(property -> entityMetadata.getPropertyAccessor(entity).getProperty(property)) //
.orElse(null);
return versionProperty != null ? (Long) entityMetadata.getPropertyAccessor(entity).getProperty(versionProperty) : null;
} catch (Exception e) {
throw new IllegalStateException("failed to load version field", e);
}
@ -96,12 +84,9 @@ public class MappingElasticsearchEntityInformation<T, ID> extends PersistentEnti
@Override
public String getParentId(T entity) {
Optional<ElasticsearchPersistentProperty> parentProperty = entityMetadata.getParentIdProperty();
ElasticsearchPersistentProperty parentProperty = entityMetadata.getParentIdProperty();
try {
return (String) parentProperty //
.flatMap(property -> entityMetadata.getPropertyAccessor(entity).getProperty(property)) //
.orElse(null);
return parentProperty != null ? (String) entityMetadata.getPropertyAccessor(entity).getProperty(parentProperty) : null;
} catch (Exception 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.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.SimpleTypeHolder;
import org.springframework.data.util.ClassTypeInformation;