Skip to content

Commit 0cada7f

Browse files
committed
DATAES-328 - Integrate Data Commons Java 8 upgrade branch.
1 parent 64bad23 commit 0cada7f

25 files changed

+447
-372
lines changed

src/main/java/org/springframework/data/elasticsearch/core/DefaultResultMapper.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2016 the original author or authors.
2+
* Copyright 2014-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,18 +15,15 @@
1515
*/
1616
package org.springframework.data.elasticsearch.core;
1717

18-
1918
import java.io.ByteArrayOutputStream;
2019
import java.io.IOException;
2120
import java.nio.charset.Charset;
2221
import java.util.ArrayList;
2322
import java.util.Collection;
2423
import java.util.LinkedList;
2524
import java.util.List;
25+
import java.util.Optional;
2626

27-
import com.fasterxml.jackson.core.JsonEncoding;
28-
import com.fasterxml.jackson.core.JsonFactory;
29-
import com.fasterxml.jackson.core.JsonGenerator;
3027
import org.apache.commons.lang.StringUtils;
3128
import org.elasticsearch.action.get.GetResponse;
3229
import org.elasticsearch.action.get.MultiGetItemResponse;
@@ -42,14 +39,18 @@
4239
import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl;
4340
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
4441
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
45-
import org.springframework.data.mapping.PersistentProperty;
4642
import org.springframework.data.mapping.context.MappingContext;
4743

44+
import com.fasterxml.jackson.core.JsonEncoding;
45+
import com.fasterxml.jackson.core.JsonFactory;
46+
import com.fasterxml.jackson.core.JsonGenerator;
47+
4848
/**
4949
* @author Artur Konczak
5050
* @author Petar Tahchiev
5151
* @author Young Gu
5252
* @author Oliver Gierke
53+
* @author Mark Paluch
5354
*/
5455
public class DefaultResultMapper extends AbstractResultMapper {
5556

@@ -119,7 +120,6 @@ private <T> void populateScriptFields(T result, SearchHit hit) {
119120
}
120121
}
121122

122-
123123
private <T> T mapEntity(Collection<SearchHitField> values, Class<T> clazz) {
124124
return mapEntity(buildJSONFromFields(values), clazz);
125125
}
@@ -175,13 +175,17 @@ private <T> void setPersistentEntityId(T result, String id, Class<T> clazz) {
175175

176176
if (mappingContext != null && clazz.isAnnotationPresent(Document.class)) {
177177

178-
ElasticsearchPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(clazz);
179-
PersistentProperty<?> idProperty = persistentEntity.getIdProperty();
180-
178+
ElasticsearchPersistentEntity<?> persistentEntity = mappingContext.getRequiredPersistentEntity(clazz);
179+
Optional<ElasticsearchPersistentProperty> idProperty = persistentEntity.getIdProperty();
180+
181181
// Only deal with String because ES generated Ids are strings !
182-
if (idProperty != null && idProperty.getType().isAssignableFrom(String.class)) {
183-
persistentEntity.getPropertyAccessor(result).setProperty(idProperty, id);
184-
}
182+
183+
idProperty.ifPresent(property -> {
184+
if (property.getType().isAssignableFrom(String.class)) {
185+
persistentEntity.getPropertyAccessor(result).setProperty(property, Optional.ofNullable(id));
186+
}
187+
});
188+
185189
}
186190
}
187191
}

src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java

Lines changed: 116 additions & 84 deletions
Large diffs are not rendered by default.

src/main/java/org/springframework/data/elasticsearch/core/mapping/ElasticsearchPersistentEntity.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2014 the original author or authors.
2+
* Copyright 2013-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,15 +15,17 @@
1515
*/
1616
package org.springframework.data.elasticsearch.core.mapping;
1717

18+
import java.util.Optional;
19+
1820
import org.springframework.data.mapping.PersistentEntity;
1921

2022
/**
2123
* ElasticsearchPersistentEntity
2224
*
2325
* @author Rizwan Idrees
2426
* @author Mohsin Husen
27+
* @author Mark Paluch
2528
*/
26-
2729
public interface ElasticsearchPersistentEntity<T> extends PersistentEntity<T, ElasticsearchPersistentProperty> {
2830

2931
String getIndexName();
@@ -40,11 +42,11 @@ public interface ElasticsearchPersistentEntity<T> extends PersistentEntity<T, El
4042

4143
String getIndexStoreType();
4244

43-
ElasticsearchPersistentProperty getVersionProperty();
45+
Optional<ElasticsearchPersistentProperty> getVersionProperty();
4446

45-
String getParentType();
47+
Optional<String> getParentType();
4648

47-
ElasticsearchPersistentProperty getParentIdProperty();
49+
Optional<ElasticsearchPersistentProperty> getParentIdProperty();
4850

4951
String settingPath();
5052

src/main/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchMappingContext.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,13 +15,11 @@
1515
*/
1616
package org.springframework.data.elasticsearch.core.mapping;
1717

18-
import java.beans.PropertyDescriptor;
19-
import java.lang.reflect.Field;
20-
2118
import org.springframework.beans.BeansException;
2219
import org.springframework.context.ApplicationContext;
2320
import org.springframework.context.ApplicationContextAware;
2421
import org.springframework.data.mapping.context.AbstractMappingContext;
22+
import org.springframework.data.mapping.model.Property;
2523
import org.springframework.data.mapping.model.SimpleTypeHolder;
2624
import org.springframework.data.util.TypeInformation;
2725

@@ -30,27 +28,27 @@
3028
*
3129
* @author Rizwan Idrees
3230
* @author Mohsin Husen
31+
* @author Mark Paluch
3332
*/
34-
3533
public class SimpleElasticsearchMappingContext extends
3634
AbstractMappingContext<SimpleElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> implements ApplicationContextAware {
3735

3836
private ApplicationContext context;
3937

4038
@Override
4139
protected <T> SimpleElasticsearchPersistentEntity<?> createPersistentEntity(TypeInformation<T> typeInformation) {
42-
final SimpleElasticsearchPersistentEntity<T> persistentEntity =
43-
new SimpleElasticsearchPersistentEntity<T>(typeInformation);
40+
final SimpleElasticsearchPersistentEntity<T> persistentEntity = new SimpleElasticsearchPersistentEntity<T>(
41+
typeInformation);
4442
if (context != null) {
4543
persistentEntity.setApplicationContext(context);
4644
}
4745
return persistentEntity;
4846
}
4947

5048
@Override
51-
protected ElasticsearchPersistentProperty createPersistentProperty(Field field, PropertyDescriptor descriptor,
52-
SimpleElasticsearchPersistentEntity<?> owner, SimpleTypeHolder simpleTypeHolder) {
53-
return new SimpleElasticsearchPersistentProperty(field, descriptor, owner, simpleTypeHolder);
49+
protected ElasticsearchPersistentProperty createPersistentProperty(Property property,
50+
SimpleElasticsearchPersistentEntity<?> owner, SimpleTypeHolder simpleTypeHolder) {
51+
return new SimpleElasticsearchPersistentProperty(property, owner, simpleTypeHolder);
5452
}
5553

5654
@Override

src/main/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentEntity.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2014 the original author or authors.
2+
* Copyright 2013-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818
import static org.springframework.util.StringUtils.*;
1919

2020
import java.util.Locale;
21+
import java.util.Optional;
2122

2223
import org.springframework.beans.BeansException;
2324
import org.springframework.context.ApplicationContext;
@@ -41,6 +42,7 @@
4142
* @param <T>
4243
* @author Rizwan Idrees
4344
* @author Mohsin Husen
45+
* @author Mark Paluch
4446
*/
4547
public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntity<T, ElasticsearchPersistentProperty>
4648
implements ElasticsearchPersistentEntity<T>, ApplicationContextAware {
@@ -55,8 +57,8 @@ public class SimpleElasticsearchPersistentEntity<T> extends BasicPersistentEntit
5557
private short replicas;
5658
private String refreshInterval;
5759
private String indexStoreType;
58-
private String parentType;
59-
private ElasticsearchPersistentProperty parentIdProperty;
60+
private Optional<String> parentType = Optional.empty();
61+
private Optional<ElasticsearchPersistentProperty> parentIdProperty = Optional.empty();
6062
private String settingPath;
6163
private boolean createIndexAndMapping;
6264

@@ -129,12 +131,12 @@ public String getRefreshInterval() {
129131
}
130132

131133
@Override
132-
public String getParentType() {
134+
public Optional<String> getParentType() {
133135
return parentType;
134136
}
135137

136138
@Override
137-
public ElasticsearchPersistentProperty getParentIdProperty() {
139+
public Optional<ElasticsearchPersistentProperty> getParentIdProperty() {
138140
return parentIdProperty;
139141
}
140142

@@ -152,16 +154,15 @@ public boolean isCreateIndexAndMapping() {
152154
public void addPersistentProperty(ElasticsearchPersistentProperty property) {
153155
super.addPersistentProperty(property);
154156

155-
if (property.getField() != null) {
156-
Parent parent = property.getField().getAnnotation(Parent.class);
157-
if (parent != null) {
158-
Assert.isNull(this.parentIdProperty, "Only one field can hold a @Parent annotation");
159-
Assert.isNull(this.parentType, "Only one field can hold a @Parent annotation");
160-
Assert.isTrue(property.getType() == String.class, "Parent ID property should be String");
161-
this.parentIdProperty = property;
162-
this.parentType = parent.type();
163-
}
164-
}
157+
Optional<Parent> annotation = property.findAnnotation(Parent.class);
158+
159+
annotation.ifPresent(parent -> {
160+
Assert.isTrue(!this.parentIdProperty.isPresent(), "Only one field can hold a @Parent annotation");
161+
Assert.isTrue(!this.parentType.isPresent(), "Only one field can hold a @Parent annotation");
162+
Assert.isTrue(property.getType() == String.class, "Parent ID property should be String");
163+
this.parentIdProperty = Optional.of(property);
164+
this.parentType = Optional.of(parent.type());
165+
});
165166

166167
if (property.isVersionProperty()) {
167168
Assert.isTrue(property.getType() == Long.class, "Version property should be Long");

src/main/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentProperty.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,21 +15,21 @@
1515
*/
1616
package org.springframework.data.elasticsearch.core.mapping;
1717

18-
import java.beans.PropertyDescriptor;
19-
import java.lang.reflect.Field;
2018
import java.util.HashSet;
2119
import java.util.Set;
2220

2321
import org.springframework.data.mapping.Association;
2422
import org.springframework.data.mapping.PersistentEntity;
2523
import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty;
24+
import org.springframework.data.mapping.model.Property;
2625
import org.springframework.data.mapping.model.SimpleTypeHolder;
2726

2827
/**
2928
* Elasticsearch specific {@link org.springframework.data.mapping.PersistentProperty} implementation processing
3029
*
3130
* @author Rizwan Idrees
3231
* @author Mohsin Husen
32+
* @author Mark Paluch
3333
*/
3434
public class SimpleElasticsearchPersistentProperty extends
3535
AnnotationBasedPersistentProperty<ElasticsearchPersistentProperty> implements ElasticsearchPersistentProperty {
@@ -43,19 +43,19 @@ public class SimpleElasticsearchPersistentProperty extends
4343
SUPPORTED_ID_PROPERTY_NAMES.add("documentId");
4444
}
4545

46-
public SimpleElasticsearchPersistentProperty(Field field, PropertyDescriptor propertyDescriptor,
47-
PersistentEntity<?, ElasticsearchPersistentProperty> owner, SimpleTypeHolder simpleTypeHolder) {
48-
super(field, propertyDescriptor, owner, simpleTypeHolder);
46+
public SimpleElasticsearchPersistentProperty(Property property,
47+
PersistentEntity<?, ElasticsearchPersistentProperty> owner, SimpleTypeHolder simpleTypeHolder) {
48+
super(property, owner, simpleTypeHolder);
4949
}
5050

5151
@Override
5252
public String getFieldName() {
53-
return field.getName();
53+
return getProperty().getName();
5454
}
5555

5656
@Override
5757
public boolean isIdProperty() {
58-
return super.isIdProperty() || (field != null ? SUPPORTED_ID_PROPERTY_NAMES.contains(getFieldName()) : false);
58+
return super.isIdProperty() || SUPPORTED_ID_PROPERTY_NAMES.contains(getFieldName());
5959
}
6060

6161
@Override

src/main/java/org/springframework/data/elasticsearch/core/query/CriteriaQuery.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,24 +23,26 @@
2323
*
2424
* @author Rizwan Idrees
2525
* @author Mohsin Husen
26+
* @author Mark Paluch
2627
*/
2728
public class CriteriaQuery extends AbstractQuery {
2829

2930
private Criteria criteria;
3031

31-
private CriteriaQuery() {
32-
}
32+
private CriteriaQuery() {}
3333

3434
public CriteriaQuery(Criteria criteria) {
35-
this(criteria, null);
35+
this(criteria, Pageable.NONE);
3636
}
3737

3838
public CriteriaQuery(Criteria criteria, Pageable pageable) {
39+
40+
Assert.notNull(criteria, "Criteria must not be null!");
41+
Assert.notNull(pageable, "Pageable must not be null!");
42+
3943
this.criteria = criteria;
4044
this.pageable = pageable;
41-
if (pageable != null) {
42-
this.addSort(pageable.getSort());
43-
}
45+
this.addSort(pageable.getSort());
4446
}
4547

4648
public static final Query fromQuery(CriteriaQuery source) {

0 commit comments

Comments
 (0)