@ -15,15 +15,14 @@
* /
* /
package org.springframework.data.jdbc.domain.support ;
package org.springframework.data.jdbc.domain.support ;
import org.springframework.beans.factory.ObjectFactory ;
import lombok.RequiredArgsConstructor ;
import org.springframework.context.ApplicationListener ;
import org.springframework.context.ApplicationListener ;
import org.springframework.data.auditing.AuditingHandler ;
import org.springframework.data.auditing.AuditingHandler ;
import org.springframework.data.jdbc.mapping.event.BeforeSaveEvent ;
import org.springframework.data.jdbc.mapping.event.BeforeSaveEvent ;
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext ;
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext ;
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntityInformation ;
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntityInformation ;
import org.springframework.data.jdbc.repository.config.EnableJdbcAuditing ;
import org.springframework.data.jdbc.repository.config.EnableJdbcAuditing ;
import org.springframework.lang.Nullable ;
import org.springframework.util.Assert ;
/ * *
/ * *
* Spring JDBC event listener to capture auditing information on persisting and updating entities .
* Spring JDBC event listener to capture auditing information on persisting and updating entities .
@ -31,36 +30,15 @@ import org.springframework.util.Assert;
* An instance of this class gets registered when you apply { @link EnableJdbcAuditing } to your Spring config .
* An instance of this class gets registered when you apply { @link EnableJdbcAuditing } to your Spring config .
*
*
* @author Kazuki Shimizu
* @author Kazuki Shimizu
* @author Jens Schauder
* @see EnableJdbcAuditing
* @see EnableJdbcAuditing
* @since 1 . 0
* @since 1 . 0
* /
* /
@RequiredArgsConstructor
public class JdbcAuditingEventListener implements ApplicationListener < BeforeSaveEvent > {
public class JdbcAuditingEventListener implements ApplicationListener < BeforeSaveEvent > {
@Nullable private AuditingHandler handler ;
private final AuditingHandler handler ;
private JdbcMappingContext context ;
private final JdbcMappingContext context ;
/ * *
* Configures the { @link AuditingHandler } to be used to set the current auditor on the domain types touched .
*
* @param auditingHandler must not be { @literal null } .
* /
public void setAuditingHandler ( ObjectFactory < AuditingHandler > auditingHandler ) {
Assert . notNull ( auditingHandler , "AuditingHandler must not be null!" ) ;
this . handler = auditingHandler . getObject ( ) ;
}
/ * *
* Configures a { @link JdbcMappingContext } that use for judging whether new object or not .
* @param context must not be { @literal null }
* /
public void setJdbcMappingContext ( JdbcMappingContext context ) {
Assert . notNull ( context , "JdbcMappingContext must not be null!" ) ;
this . context = context ;
}
/ * *
/ * *
* { @inheritDoc }
* { @inheritDoc }
@ -70,19 +48,22 @@ public class JdbcAuditingEventListener implements ApplicationListener<BeforeSave
@Override
@Override
public void onApplicationEvent ( BeforeSaveEvent event ) {
public void onApplicationEvent ( BeforeSaveEvent event ) {
if ( handler ! = null ) {
Object entity = event . getEntity ( ) ;
@SuppressWarnings ( "unchecked" )
Class < Object > entityType = event . getChange ( ) . getEntityType ( ) ;
JdbcPersistentEntityInformation < Object , ? > entityInformation = context
. getRequiredPersistentEntityInformation ( entityType ) ;
invokeHandler ( entity , entityInformation ) ;
}
private < T > void invokeHandler ( T entity , JdbcPersistentEntityInformation < T , ? > entityInformation ) {
event . getOptionalEntity ( ) . ifPresent ( entity - > {
if ( entityInformation . isNew ( entity ) ) {
@SuppressWarnings ( "unchecked" )
handler . markCreated ( entity ) ;
Class < Object > entityType = event . getChange ( ) . getEntityType ( ) ;
} else {
JdbcPersistentEntityInformation < Object , ? > entityInformation =
handler . markModified ( entity ) ;
context . getRequiredPersistentEntityInformation ( entityType ) ;
if ( entityInformation . isNew ( entity ) ) {
handler . markCreated ( entity ) ;
} else {
handler . markModified ( entity ) ;
}
} ) ;
}
}
}
}
}
}