Skip to content

Commit 1f2dc16

Browse files
schaudergregturn
authored andcommitted
DATAJDBC-120 - Changed events to support manipulation of DbChange.
This change enables the manipulation of the DbChange instance before it gets interpreted and turned into SQL statements. Only for Aggregate Roots events get fired, since these are the abstraction the repositories work on. Insert and Update events got removed, since this distinction doesn't exist on the Aggregate Root level. It only exists on the level of entities and/or tables which is represented by DbActions. Improved some tests to properly check all the events triggered.
1 parent fe5ae93 commit 1f2dc16

25 files changed

+362
-237
lines changed

src/main/java/org/springframework/data/jdbc/core/DefaultJdbcInterpreter.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.springframework.data.jdbc.core.conversion.DbAction.Insert;
2626
import org.springframework.data.jdbc.core.conversion.DbAction.Update;
2727
import org.springframework.data.jdbc.core.conversion.Interpreter;
28-
import org.springframework.data.jdbc.mapping.event.Identifier;
2928
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext;
3029
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity;
3130

@@ -74,7 +73,7 @@ public <T> void interpret(Update<T> update) {
7473
public <T> void interpret(Delete<T> delete) {
7574

7675
if (delete.getPropertyPath() == null) {
77-
template.doDelete(Identifier.of(delete.getRootId()), Optional.ofNullable(delete.getEntity()),
76+
template.doDelete(delete.getRootId(), Optional.ofNullable(delete.getEntity()),
7877
delete.getEntityType());
7978
} else {
8079
template.doDelete(delete.getRootId(), delete.getPropertyPath());

src/main/java/org/springframework/data/jdbc/core/EntityRowMapper.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,11 @@ public <T> T getParameterValue(Parameter<T, JdbcPersistentProperty> parameter) {
128128
return null;
129129
}
130130

131+
String column = prefix + name;
131132
try {
132-
return conversionService.convert(resultSet.getObject(prefix + name), parameter.getType().getType());
133+
return conversionService.convert(resultSet.getObject(column), parameter.getType().getType());
133134
} catch (SQLException o_O) {
134-
throw new MappingException(String.format("Couldn't read column %s from ResultSet.", name), o_O);
135+
throw new MappingException(String.format("Couldn't read column %s from ResultSet.", column), o_O);
135136
}
136137
}
137138
}

src/main/java/org/springframework/data/jdbc/core/EventPublishingEntityRowMapper.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public T mapRow(ResultSet resultSet, int i) throws SQLException {
5050

5151
T instance = delegate.mapRow(resultSet, i);
5252

53-
publisher.publishEvent(new AfterCreation(Identifier.of(entityInformation.getRequiredId(instance)), instance));
53+
publisher.publishEvent(new AfterCreation(Identifier.of(entityInformation.getRequiredId(instance)), instance, null));
5454

5555
return instance;
5656
}

src/main/java/org/springframework/data/jdbc/core/JdbcEntityTemplate.java

+37-29
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,9 @@
3636
import org.springframework.data.jdbc.core.conversion.JdbcEntityDeleteWriter;
3737
import org.springframework.data.jdbc.core.conversion.JdbcEntityWriter;
3838
import org.springframework.data.jdbc.mapping.event.AfterDelete;
39-
import org.springframework.data.jdbc.mapping.event.AfterInsert;
40-
import org.springframework.data.jdbc.mapping.event.AfterUpdate;
39+
import org.springframework.data.jdbc.mapping.event.AfterSave;
4140
import org.springframework.data.jdbc.mapping.event.BeforeDelete;
42-
import org.springframework.data.jdbc.mapping.event.BeforeInsert;
43-
import org.springframework.data.jdbc.mapping.event.BeforeUpdate;
41+
import org.springframework.data.jdbc.mapping.event.BeforeSave;
4442
import org.springframework.data.jdbc.mapping.event.Identifier;
4543
import org.springframework.data.jdbc.mapping.event.Identifier.Specified;
4644
import org.springframework.data.jdbc.mapping.model.BasicJdbcPersistentEntityInformation;
@@ -102,14 +100,29 @@ private static GenericConversionService getDefaultConversionService() {
102100

103101
@Override
104102
public <T> void save(T instance, Class<T> domainType) {
105-
createChange(instance).executeWith(interpreter);
103+
104+
JdbcPersistentEntityInformation<T, ?> entityInformation = context.getRequiredPersistentEntityInformation(domainType);
105+
106+
AggregateChange change = createChange(instance);
107+
108+
publisher.publishEvent(new BeforeSave( //
109+
Identifier.ofNullable(entityInformation.getId(instance)), //
110+
instance, //
111+
change //
112+
));
113+
114+
change.executeWith(interpreter);
115+
116+
publisher.publishEvent(new AfterSave( //
117+
Identifier.of(entityInformation.getId(instance)), //
118+
instance, //
119+
change //
120+
));
106121
}
107122

108123
@Override
109124
public <T> void insert(T instance, Class<T> domainType, Map<String, Object> additionalParameters) {
110125

111-
publisher.publishEvent(new BeforeInsert(instance));
112-
113126
KeyHolder holder = new GeneratedKeyHolder();
114127
JdbcPersistentEntity<T> persistentEntity = getRequiredPersistentEntity(domainType);
115128
JdbcPersistentEntityInformation<T, ?> entityInformation = context
@@ -132,23 +145,17 @@ public <T> void insert(T instance, Class<T> domainType, Map<String, Object> addi
132145
throw new IllegalStateException(String.format(ENTITY_NEW_AFTER_INSERT, persistentEntity));
133146
}
134147

135-
publisher.publishEvent(new AfterInsert(Identifier.of(entityInformation.getRequiredId(instance)), instance));
136-
137148
}
138149

139150
@Override
140151
public <S> void update(S instance, Class<S> domainType) {
141152

142153
JdbcPersistentEntity<S> persistentEntity = getRequiredPersistentEntity(domainType);
143-
JdbcPersistentEntityInformation<S, ?> entityInformation = context
144-
.getRequiredPersistentEntityInformation(domainType);
145154

146-
Specified specifiedId = Identifier.of(entityInformation.getRequiredId(instance));
147-
publisher.publishEvent(new BeforeUpdate(specifiedId, instance));
148155
operations.update(sql(domainType).getUpdate(), getPropertyMap(instance, persistentEntity));
149-
publisher.publishEvent(new AfterUpdate(specifiedId, instance));
150156
}
151157

158+
@SuppressWarnings("ConstantConditions")
152159
@Override
153160
public long count(Class<?> domainType) {
154161
return operations.getJdbcOperations().queryForObject(sql(domainType).getCount(), Long.class);
@@ -211,9 +218,21 @@ public void deleteAll(Class<?> domainType) {
211218
change.executeWith(interpreter);
212219
}
213220

214-
void doDelete(Object rootId, PropertyPath propertyPath) {
221+
private void deleteTree(Object id, Object entity, Class<?> domainType) {
215222

216-
JdbcPersistentEntity<?> entityToDelete = context.getRequiredPersistentEntity(propertyPath.getTypeInformation());
223+
AggregateChange change = createDeletingChange(id, entity, domainType);
224+
225+
Specified specifiedId = Identifier.of(id);
226+
Optional<Object> optionalEntity = Optional.ofNullable(entity);
227+
publisher.publishEvent(new BeforeDelete(specifiedId, optionalEntity, change));
228+
229+
change.executeWith(interpreter);
230+
231+
publisher.publishEvent(new AfterDelete(specifiedId, optionalEntity, change));
232+
233+
}
234+
235+
void doDelete(Object rootId, PropertyPath propertyPath) {
217236

218237
JdbcPersistentEntity<?> rootEntity = context.getRequiredPersistentEntity(propertyPath.getOwningType());
219238

@@ -228,23 +247,12 @@ void doDelete(Object rootId, PropertyPath propertyPath) {
228247

229248
}
230249

231-
void doDelete(Specified specifiedId, Optional<Object> optionalEntity, Class<?> domainType) {
232-
233-
publisher.publishEvent(new BeforeDelete(specifiedId, optionalEntity));
250+
void doDelete(Object id, Optional<Object> optionalEntity, Class<?> domainType) {
234251

235252
String deleteByIdSql = sql(domainType).getDeleteById();
236-
MapSqlParameterSource parameter = createIdParameterSource(specifiedId.getValue(), domainType);
253+
MapSqlParameterSource parameter = createIdParameterSource(id, domainType);
237254

238255
operations.update(deleteByIdSql, parameter);
239-
240-
publisher.publishEvent(new AfterDelete(specifiedId, optionalEntity));
241-
}
242-
243-
private void deleteTree(Object id, Object entity, Class<?> domainType) {
244-
245-
AggregateChange change = createDeletingChange(id, entity, domainType);
246-
247-
change.executeWith(interpreter);
248256
}
249257

250258
private <T> AggregateChange createChange(T instance) {

src/main/java/org/springframework/data/jdbc/mapping/event/AfterCreation.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.jdbc.mapping.event;
1717

18+
import org.springframework.data.jdbc.core.conversion.AggregateChange;
1819
import org.springframework.data.jdbc.mapping.event.Identifier.Specified;
1920

2021
/**
@@ -31,8 +32,9 @@ public class AfterCreation extends JdbcEventWithIdAndEntity {
3132
/**
3233
* @param id of the entity
3334
* @param entity the newly instantiated entity.
35+
* @param change
3436
*/
35-
public AfterCreation(Specified id, Object entity) {
36-
super(id, entity);
37+
public AfterCreation(Specified id, Object entity, AggregateChange change) {
38+
super(id, entity, change);
3739
}
3840
}

src/main/java/org/springframework/data/jdbc/mapping/event/AfterDelete.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.util.Optional;
1919

20+
import org.springframework.data.jdbc.core.conversion.AggregateChange;
2021
import org.springframework.data.jdbc.mapping.event.Identifier.Specified;
2122

2223
/**
@@ -33,8 +34,9 @@ public class AfterDelete extends JdbcEventWithId {
3334
/**
3435
* @param id of the entity.
3536
* @param instance the deleted entity if it is available.
37+
* @param change the {@link AggregateChange} encoding the planned actions to be performed on the database.
3638
*/
37-
public AfterDelete(Specified id, Optional<Object> instance) {
38-
super(id, instance);
39+
public AfterDelete(Specified id, Optional<Object> instance, AggregateChange change) {
40+
super(id, instance, change);
3941
}
4042
}

src/main/java/org/springframework/data/jdbc/mapping/event/AfterInsert.java

-37
This file was deleted.

src/main/java/org/springframework/data/jdbc/mapping/event/AfterSave.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.jdbc.mapping.event;
1717

18+
import org.springframework.data.jdbc.core.conversion.AggregateChange;
1819
import org.springframework.data.jdbc.mapping.event.Identifier.Specified;
1920

2021
/**
@@ -30,8 +31,9 @@ public class AfterSave extends JdbcEventWithIdAndEntity {
3031
/**
3132
* @param id identifier of
3233
* @param instance the newly saved entity.
34+
* @param change the {@link AggregateChange} encoding the planned actions to be performed on the database.
3335
*/
34-
AfterSave(Specified id, Object instance) {
35-
super(id, instance);
36+
public AfterSave(Specified id, Object instance, AggregateChange change) {
37+
super(id, instance, change);
3638
}
3739
}

src/main/java/org/springframework/data/jdbc/mapping/event/AfterUpdate.java

-37
This file was deleted.

src/main/java/org/springframework/data/jdbc/mapping/event/BeforeDelete.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.util.Optional;
1919

20+
import org.springframework.data.jdbc.core.conversion.AggregateChange;
2021
import org.springframework.data.jdbc.mapping.event.Identifier.Specified;
2122

2223
/**
@@ -32,8 +33,9 @@ public class BeforeDelete extends JdbcEventWithId {
3233
/**
3334
* @param id the id of the entity
3435
* @param entity the entity about to get deleted. Might be empty.
36+
* @param change the {@link AggregateChange} encoding the planned actions to be performed on the database.
3537
*/
36-
public <T> BeforeDelete(Specified id, Optional<Object> entity) {
37-
super(id, entity);
38+
public <T> BeforeDelete(Specified id, Optional<Object> entity, AggregateChange change) {
39+
super(id, entity, change);
3840
}
3941
}

src/main/java/org/springframework/data/jdbc/mapping/event/BeforeInsert.java

-36
This file was deleted.

src/main/java/org/springframework/data/jdbc/mapping/event/BeforeSave.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.springframework.data.jdbc.mapping.event;
1717

18+
import org.springframework.data.jdbc.core.conversion.AggregateChange;
19+
1820
/**
1921
* Subclasses of this get published before an entity gets saved to the database.
2022
*
@@ -28,8 +30,9 @@ public class BeforeSave extends JdbcEventWithEntity {
2830
/**
2931
* @param id of the entity to be saved.
3032
* @param instance the entity about to get saved.
33+
* @param change
3134
*/
32-
BeforeSave(Identifier id, Object instance) {
33-
super(id, instance);
35+
public BeforeSave(Identifier id, Object instance, AggregateChange change) {
36+
super(id, instance, change);
3437
}
3538
}

0 commit comments

Comments
 (0)