Browse Source

Polishing.

Refactoring and code aesthetics.

See #1249
Original pull request #1250
pull/1231/head
Jens Schauder 4 years ago
parent
commit
ac583991ac
No known key found for this signature in database
GPG Key ID: 45CC872F17423DBF
  1. 21
      spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/RelationalEntityDeleteWriter.java
  2. 7
      spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/WritingContext.java
  3. 28
      spring-data-relational/src/test/java/org/springframework/data/relational/core/conversion/RelationalEntityDeleteWriterUnitTests.java
  4. 3
      spring-data-relational/src/test/java/org/springframework/data/relational/core/conversion/RelationalEntityInsertWriterUnitTests.java

21
spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/RelationalEntityDeleteWriter.java

@ -18,12 +18,12 @@ package org.springframework.data.relational.core.conversion; @@ -18,12 +18,12 @@ package org.springframework.data.relational.core.conversion;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import org.springframework.data.convert.EntityWriter;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.PersistentPropertyPath;
import org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@ -74,10 +74,7 @@ public class RelationalEntityDeleteWriter implements EntityWriter<Object, Mutabl @@ -74,10 +74,7 @@ public class RelationalEntityDeleteWriter implements EntityWriter<Object, Mutabl
List<DbAction<?>> deleteReferencedActions = new ArrayList<>();
context.findPersistentPropertyPaths(entityType, PersistentProperty::isEntity) //
.filter(p -> !p.getRequiredLeafProperty().isEmbedded() //
&& PersistentPropertyPathExtension.isWritable(p)) //
.forEach(p -> deleteReferencedActions.add(new DbAction.DeleteAll<>(p)));
forAllTableRepresentingPaths(entityType, p -> deleteReferencedActions.add(new DbAction.DeleteAll<>(p)));
Collections.reverse(deleteReferencedActions);
@ -118,14 +115,18 @@ public class RelationalEntityDeleteWriter implements EntityWriter<Object, Mutabl @@ -118,14 +115,18 @@ public class RelationalEntityDeleteWriter implements EntityWriter<Object, Mutabl
List<DbAction<?>> actions = new ArrayList<>();
context.findPersistentPropertyPaths(aggregateChange.getEntityType(), p -> p.isEntity()) //
.filter(p -> !p.getRequiredLeafProperty().isEmbedded() //
&& PersistentPropertyPathExtension.isWritable(p)) //
.forEach(p -> actions.add(new DbAction.Delete<>(id, p)));
forAllTableRepresentingPaths(aggregateChange.getEntityType(), p -> actions.add(new DbAction.Delete<>(id, p)));
Collections.reverse(actions);
return actions;
}
private void forAllTableRepresentingPaths(Class<?> entityType,
Consumer<PersistentPropertyPath<RelationalPersistentProperty>> pathConsumer) {
context.findPersistentPropertyPaths(entityType, property -> property.isEntity() && !property.isEmbedded()) //
.filter(PersistentPropertyPathExtension::isWritable) //
.forEach(pathConsumer);
}
}

7
spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/WritingContext.java

@ -25,7 +25,7 @@ import java.util.Map; @@ -25,7 +25,7 @@ import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.data.mapping.PersistentPropertyPath;
import org.springframework.data.mapping.PersistentPropertyPaths;
import org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
@ -47,7 +47,7 @@ class WritingContext<T> { @@ -47,7 +47,7 @@ class WritingContext<T> {
private final RelationalMappingContext context;
private final T root;
private final Class<T> entityType;
private final PersistentPropertyPaths<?, RelationalPersistentProperty> paths;
private final List<PersistentPropertyPath<RelationalPersistentProperty>> paths;
private final Map<PathNode, DbAction<?>> previousActions = new HashMap<>();
private final Map<PersistentPropertyPath<RelationalPersistentProperty>, List<PathNode>> nodesCache = new HashMap<>();
private final IdValueSource rootIdValueSource;
@ -63,7 +63,8 @@ class WritingContext<T> { @@ -63,7 +63,8 @@ class WritingContext<T> {
this.aggregateChange = aggregateChange;
this.rootIdValueSource = IdValueSource.forInstance(root,
context.getRequiredPersistentEntity(aggregateChange.getEntityType()));
this.paths = context.findPersistentPropertyPaths(entityType, (p) -> p.isEntity() && !p.isEmbedded() && p.isWritable());
this.paths = context.findPersistentPropertyPaths(entityType, (p) -> p.isEntity() && !p.isEmbedded()) //
.filter(PersistentPropertyPathExtension::isWritable).toList();
}
/**

28
spring-data-relational/src/test/java/org/springframework/data/relational/core/conversion/RelationalEntityDeleteWriterUnitTests.java

@ -15,13 +15,14 @@ @@ -15,13 +15,14 @@
*/
package org.springframework.data.relational.core.conversion;
import static org.assertj.core.api.Assertions.*;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.assertj.core.api.Assertions;
import org.assertj.core.groups.Tuple;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@ -57,7 +58,7 @@ public class RelationalEntityDeleteWriterUnitTests { @@ -57,7 +58,7 @@ public class RelationalEntityDeleteWriterUnitTests {
converter.write(entity.id, aggregateChange);
Assertions.assertThat(extractActions(aggregateChange))
assertThat(extractActions(aggregateChange))
.extracting(DbAction::getClass, DbAction::getEntityType, DbActionTestSupport::extractPath) //
.containsExactly( //
Tuple.tuple(AcquireLockRoot.class, SomeEntity.class, ""), //
@ -76,7 +77,7 @@ public class RelationalEntityDeleteWriterUnitTests { @@ -76,7 +77,7 @@ public class RelationalEntityDeleteWriterUnitTests {
converter.write(entity.id, aggregateChange);
Assertions.assertThat(extractActions(aggregateChange))
assertThat(extractActions(aggregateChange))
.extracting(DbAction::getClass, DbAction::getEntityType, DbActionTestSupport::extractPath) //
.containsExactly(Tuple.tuple(DeleteRoot.class, SingleEntity.class, ""));
}
@ -88,7 +89,7 @@ public class RelationalEntityDeleteWriterUnitTests { @@ -88,7 +89,7 @@ public class RelationalEntityDeleteWriterUnitTests {
converter.write(null, aggregateChange);
Assertions.assertThat(extractActions(aggregateChange))
assertThat(extractActions(aggregateChange))
.extracting(DbAction::getClass, DbAction::getEntityType, DbActionTestSupport::extractPath) //
.containsExactly( //
Tuple.tuple(AcquireLockAllRoot.class, SomeEntity.class, ""), //
@ -105,7 +106,7 @@ public class RelationalEntityDeleteWriterUnitTests { @@ -105,7 +106,7 @@ public class RelationalEntityDeleteWriterUnitTests {
converter.write(null, aggregateChange);
Assertions.assertThat(extractActions(aggregateChange))
assertThat(extractActions(aggregateChange))
.extracting(DbAction::getClass, DbAction::getEntityType, DbActionTestSupport::extractPath) //
.containsExactly(Tuple.tuple(DeleteAllRoot.class, SingleEntity.class, ""));
}
@ -115,11 +116,12 @@ public class RelationalEntityDeleteWriterUnitTests { @@ -115,11 +116,12 @@ public class RelationalEntityDeleteWriterUnitTests {
WithReadOnlyReference entity = new WithReadOnlyReference(23L);
MutableAggregateChange<WithReadOnlyReference> aggregateChange = MutableAggregateChange.forDelete(WithReadOnlyReference.class);
MutableAggregateChange<WithReadOnlyReference> aggregateChange = MutableAggregateChange
.forDelete(WithReadOnlyReference.class);
converter.write(entity.id, aggregateChange);
Assertions.assertThat(extractActions(aggregateChange))
assertThat(extractActions(aggregateChange))
.extracting(DbAction::getClass, DbAction::getEntityType, DbActionTestSupport::extractPath) //
.containsExactly( //
Tuple.tuple(DeleteRoot.class, WithReadOnlyReference.class, "") //
@ -131,18 +133,18 @@ public class RelationalEntityDeleteWriterUnitTests { @@ -131,18 +133,18 @@ public class RelationalEntityDeleteWriterUnitTests {
WithReadOnlyReference entity = new WithReadOnlyReference(23L);
MutableAggregateChange<WithReadOnlyReference> aggregateChange = MutableAggregateChange.forDelete(WithReadOnlyReference.class);
MutableAggregateChange<WithReadOnlyReference> aggregateChange = MutableAggregateChange
.forDelete(WithReadOnlyReference.class);
converter.write(null, aggregateChange);
Assertions.assertThat(extractActions(aggregateChange))
assertThat(extractActions(aggregateChange))
.extracting(DbAction::getClass, DbAction::getEntityType, DbActionTestSupport::extractPath) //
.containsExactly( //
Tuple.tuple(DeleteAllRoot.class, WithReadOnlyReference.class, "") //
);
}
private List<DbAction<?>> extractActions(MutableAggregateChange<?> aggregateChange) {
List<DbAction<?>> actions = new ArrayList<>();
@ -179,8 +181,8 @@ public class RelationalEntityDeleteWriterUnitTests { @@ -179,8 +181,8 @@ public class RelationalEntityDeleteWriterUnitTests {
@RequiredArgsConstructor
private static class WithReadOnlyReference {
@Id final Long id;
@ReadOnlyProperty
OtherEntity other;
@ReadOnlyProperty OtherEntity other;
}
}

3
spring-data-relational/src/test/java/org/springframework/data/relational/core/conversion/RelationalEntityInsertWriterUnitTests.java

@ -26,7 +26,6 @@ import org.junit.jupiter.api.Test; @@ -26,7 +26,6 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.ReadOnlyProperty;
import org.springframework.data.relational.core.conversion.DbAction.InsertRoot;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
@ -73,10 +72,8 @@ public class RelationalEntityInsertWriterUnitTests { @@ -73,10 +72,8 @@ public class RelationalEntityInsertWriterUnitTests {
.containsExactly( //
tuple(InsertRoot.class, SingleReferenceEntity.class, "", SingleReferenceEntity.class, false) //
);
}
private List<DbAction<?>> extractActions(MutableAggregateChange<?> aggregateChange) {
List<DbAction<?>> actions = new ArrayList<>();

Loading…
Cancel
Save