Skip to content

Commit b9e0fea

Browse files
committed
GH-2313 - Extract property iteration.
1 parent fd443eb commit b9e0fea

File tree

3 files changed

+33
-34
lines changed

3 files changed

+33
-34
lines changed

src/main/java/org/springframework/data/neo4j/core/Neo4jTemplate.java

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import static org.neo4j.cypherdsl.core.Cypher.asterisk;
2020
import static org.neo4j.cypherdsl.core.Cypher.parameter;
2121

22-
import java.beans.PropertyDescriptor;
2322
import java.util.ArrayList;
2423
import java.util.Collection;
2524
import java.util.Collections;
@@ -333,11 +332,9 @@ public <T, R> R saveAs(T instance, Class<R> resultType) {
333332
}
334333

335334
ProjectionInformation projectionInformation = projectionFactory.getProjectionInformation(resultType);
336-
List<PropertyDescriptor> inputProperties = projectionInformation.getInputProperties();
337-
Set<PropertyPath> pps = new HashSet<>();
338-
for (PropertyDescriptor inputProperty : inputProperties) {
339-
PropertyFilterSupport.addPropertiesFrom(resultType, resultType, projectionFactory, pps, inputProperty.getName(), neo4jMappingContext);
340-
}
335+
Collection<PropertyPath> pps = PropertyFilterSupport.addPropertiesFrom(resultType, resultType,
336+
projectionFactory, neo4jMappingContext);
337+
341338
T savedInstance = saveImpl(instance, pps);
342339
if (projectionInformation.isClosed()) {
343340
return projectionFactory.createProjection(resultType, savedInstance);
@@ -499,11 +496,11 @@ public <T, R> List<R> saveAllAs(Iterable<T> instances, Class<R> resultType) {
499496
}
500497

501498
ProjectionInformation projectionInformation = projectionFactory.getProjectionInformation(resultType);
502-
List<PropertyPath> pps = new ArrayList<>();
503-
for (PropertyDescriptor inputProperty : projectionInformation.getInputProperties()) {
504-
PropertyFilterSupport.addPropertiesFrom(commonElementType, resultType, projectionFactory, pps, inputProperty.getName(), neo4jMappingContext);
505-
}
506-
List<T> savedInstances = saveAllImpl(instances, pps);
499+
500+
Collection<PropertyPath> pps = PropertyFilterSupport.addPropertiesFrom(resultType, commonElementType,
501+
projectionFactory, neo4jMappingContext);
502+
503+
List<T> savedInstances = saveAllImpl(instances, new ArrayList<>(pps));
507504

508505
if (projectionInformation.isClosed()) {
509506
return savedInstances.stream().map(instance -> projectionFactory.createProjection(resultType, instance))
@@ -898,12 +895,9 @@ <T, R> List<R> doSave(Iterable<R> instances, Class<T> domainType) {
898895

899896
Class<?> resultType = TemplateSupport.findCommonElementType(instances);
900897

901-
ProjectionInformation projectionInformation = projectionFactory.getProjectionInformation(resultType);
902-
List<PropertyDescriptor> inputProperties = projectionInformation.getInputProperties();
903-
Set<PropertyPath> pps = new HashSet<>();
904-
for (PropertyDescriptor inputProperty : inputProperties) {
905-
PropertyFilterSupport.addPropertiesFrom(domainType, resultType, projectionFactory, pps, inputProperty.getName(), neo4jMappingContext);
906-
}
898+
Collection<PropertyPath> pps = PropertyFilterSupport.addPropertiesFrom(resultType, domainType,
899+
projectionFactory, neo4jMappingContext);
900+
907901
List<R> results = new ArrayList<>();
908902
for (R instance : instances) {
909903
EntityFromDtoInstantiatingConverter<T> converter = new EntityFromDtoInstantiatingConverter<>(domainType, neo4jMappingContext);

src/main/java/org/springframework/data/neo4j/core/PropertyFilterSupport.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,19 @@ public static List<PropertyPath> getInputProperties(ResultProcessor resultProces
5959
return returnedType.isProjecting() ? filteredProperties : Collections.emptyList();
6060
}
6161

62-
public static void addPropertiesFrom(Class<?> domainType, Class<?> returnedType, ProjectionFactory factory,
62+
public static List<PropertyPath> addPropertiesFrom(Class<?> returnType, Class<?> domainType,
63+
ProjectionFactory projectionFactory,
64+
Neo4jMappingContext neo4jMappingContext) {
65+
66+
ProjectionInformation projectionInformation = projectionFactory.getProjectionInformation(returnType);
67+
List<PropertyPath> pps = new ArrayList<>();
68+
for (PropertyDescriptor inputProperty : projectionInformation.getInputProperties()) {
69+
addPropertiesFrom(returnType, domainType, projectionFactory, pps, inputProperty.getName(), neo4jMappingContext);
70+
}
71+
return pps;
72+
}
73+
74+
private static void addPropertiesFrom(Class<?> domainType, Class<?> returnedType, ProjectionFactory factory,
6375
Collection<PropertyPath> filteredProperties, String inputProperty,
6476
Neo4jMappingContext mappingContext) {
6577

src/main/java/org/springframework/data/neo4j/core/ReactiveNeo4jTemplate.java

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,10 @@
2727
import reactor.util.function.Tuple2;
2828
import reactor.util.function.Tuples;
2929

30-
import java.beans.PropertyDescriptor;
3130
import java.util.ArrayList;
3231
import java.util.Collection;
3332
import java.util.Collections;
3433
import java.util.HashMap;
35-
import java.util.HashSet;
3634
import java.util.LinkedHashSet;
3735
import java.util.List;
3836
import java.util.Map;
@@ -317,10 +315,9 @@ public <T, R> Mono<R> saveAs(T instance, Class<R> resultType) {
317315
}
318316

319317
ProjectionInformation projectionInformation = projectionFactory.getProjectionInformation(resultType);
320-
List<PropertyPath> pps = new ArrayList<>();
321-
for (PropertyDescriptor inputProperty : projectionInformation.getInputProperties()) {
322-
PropertyFilterSupport.addPropertiesFrom(instance.getClass(), resultType, projectionFactory, pps, inputProperty.getName(), neo4jMappingContext);
323-
}
318+
Collection<PropertyPath> pps = PropertyFilterSupport.addPropertiesFrom(resultType, instance.getClass(),
319+
projectionFactory, neo4jMappingContext);
320+
324321
Mono<T> savingPublisher = saveImpl(instance, pps);
325322
if (projectionInformation.isClosed()) {
326323
return savingPublisher.map(savedInstance -> projectionFactory.createProjection(resultType, savedInstance));
@@ -344,12 +341,9 @@ <T, R> Flux<R> doSave(Iterable<R> instances, Class<T> domainType) {
344341

345342
Class<?> resultType = TemplateSupport.findCommonElementType(instances);
346343

347-
ProjectionInformation projectionInformation = projectionFactory.getProjectionInformation(resultType);
348-
List<PropertyDescriptor> inputProperties = projectionInformation.getInputProperties();
349-
Set<PropertyPath> pps = new HashSet<>();
350-
for (PropertyDescriptor inputProperty : inputProperties) {
351-
PropertyFilterSupport.addPropertiesFrom(domainType, resultType, projectionFactory, pps, inputProperty.getName(), neo4jMappingContext);
352-
}
344+
Collection<PropertyPath> pps = PropertyFilterSupport.addPropertiesFrom(resultType, domainType,
345+
projectionFactory, neo4jMappingContext);
346+
353347
return Flux.fromIterable(instances)
354348
.flatMap(instance -> {
355349
EntityFromDtoInstantiatingConverter<T> converter = new EntityFromDtoInstantiatingConverter<>(domainType, neo4jMappingContext);
@@ -448,10 +442,9 @@ public <T, R> Flux<R> saveAllAs(Iterable<T> instances, Class<R> resultType) {
448442
}
449443

450444
ProjectionInformation projectionInformation = projectionFactory.getProjectionInformation(resultType);
451-
List<PropertyPath> pps = new ArrayList<>();
452-
for (PropertyDescriptor inputProperty : projectionInformation.getInputProperties()) {
453-
PropertyFilterSupport.addPropertiesFrom(commonElementType, resultType, projectionFactory, pps, inputProperty.getName(), neo4jMappingContext);
454-
}
445+
List<PropertyPath> pps = PropertyFilterSupport.addPropertiesFrom(resultType, commonElementType,
446+
projectionFactory, neo4jMappingContext);
447+
455448
Flux<T> savedInstances = saveAllImpl(instances, pps);
456449
if (projectionInformation.isClosed()) {
457450
return savedInstances.map(instance -> projectionFactory.createProjection(resultType, instance));

0 commit comments

Comments
 (0)