Skip to content

Commit

Permalink
Merge pull request #56 from hatunet/dev
Browse files Browse the repository at this point in the history
closed #55 : support @createdby and @LastModifiedBy to audit entity a…
  • Loading branch information
Jarvis Song committed Dec 23, 2016
2 parents a470629 + d89de56 commit 3c24082
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@
*/
String transactionManagerRef() default "transactionManager";

String auditorAwareRef() default "auditorAware";

/**
* Configures whether nested repository-interfaces (e.g. defined as inner classes) should be discovered by the
* repositories infrastructure.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
public class MybatisRepositoryConfigExtension extends RepositoryConfigurationExtensionSupport {
private static final String DEFAULT_TRANSACTION_MANAGER_BEAN_NAME = TxUtils.DEFAULT_TRANSACTION_MANAGER;
private static final String DEFAULT_SQL_SESSION_FACTORY_BEAN_NAME = "sqlSessionFactory";
private static final String DEFAULT_AUDITOR_AWARE_BEAN_NAME = "auditorAware";
private static final String ENABLE_DEFAULT_TRANSACTIONS_ATTRIBUTE = "enableDefaultTransactions";
private static final String SQL_SESSION_TEMPLATE_BEAN_NAME_SUFFIX = "_Template";
private static final String DIALECT_BEAN_NAME_SUFFIX = "_Dialect";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,8 @@ public interface MybatisEntityInformation<T, ID extends Serializable>

void setLastModifiedDate(T entity);

void setCreatedBy(T entity);

void setLastModifiedBy(T entity);

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.springframework.data.mybatis.repository.support;

import org.springframework.data.domain.AuditorAware;
import org.springframework.data.domain.Persistable;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mybatis.annotations.Entity;
Expand All @@ -38,6 +39,7 @@ public abstract class MybatisEntityInformationSupport<T, ID extends Serializable


protected final PersistentEntity<T, ?> persistentEntity;
protected final AuditorAware<?> auditorAware;

/**
* Creates a new {@link AbstractEntityInformation} from the given domain class.
Expand All @@ -46,9 +48,11 @@ public abstract class MybatisEntityInformationSupport<T, ID extends Serializable
*/
protected MybatisEntityInformationSupport(
PersistentEntity<T, ?> persistentEntity,
AuditorAware<?> auditorAware,
Class<T> domainClass) {
super(domainClass);
this.persistentEntity = persistentEntity;
this.auditorAware = auditorAware;
}

@Override
Expand All @@ -63,14 +67,16 @@ public String getEntityName() {
}


public static <T, ID extends Serializable> MybatisEntityInformation<T, ID> getEntityInformation(MybatisMappingContext mappingContext, Class<T> domainClass) {
public static <T, ID extends Serializable> MybatisEntityInformation<T, ID> getEntityInformation(MybatisMappingContext mappingContext,
AuditorAware<?> auditorAware,
Class<T> domainClass) {
Assert.notNull(domainClass);
PersistentEntity<T, ?> persistentEntity = (PersistentEntity<T, ?>) mappingContext.getPersistentEntity(domainClass);
if (Persistable.class.isAssignableFrom(domainClass)) {
return new MybatisPersistableEntityInformation(persistentEntity, domainClass);
return new MybatisPersistableEntityInformation(persistentEntity, auditorAware, domainClass);
}

return new MybatisMetamodelEntityInformation<T, ID>(persistentEntity, domainClass);
return new MybatisMetamodelEntityInformation<T, ID>(persistentEntity, auditorAware, domainClass);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
package org.springframework.data.mybatis.repository.support;

import org.joda.time.DateTime;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.PersistentPropertyAccessor;
Expand All @@ -39,18 +42,23 @@ public class MybatisMetamodelEntityInformation<T, ID extends Serializable> exten

private PersistentProperty<?> createdDateProperty;
private PersistentProperty<?> lastModifiedDateProperty;
private PersistentProperty<?> createdByProperty;
private PersistentProperty<?> lastModifiedByProperty;


/**
* Creates a new {@link AbstractEntityInformation} from the given domain class.
*
* @param persistentEntity
* @param domainClass must not be {@literal null}.
*/
protected MybatisMetamodelEntityInformation(PersistentEntity<T, ?> persistentEntity, Class<T> domainClass) {
super(persistentEntity, domainClass);
protected MybatisMetamodelEntityInformation(PersistentEntity<T, ?> persistentEntity, AuditorAware<?> auditorAware, Class<T> domainClass) {
super(persistentEntity, auditorAware, domainClass);

createdDateProperty = persistentEntity.getPersistentProperty(CreatedDate.class);
lastModifiedDateProperty = persistentEntity.getPersistentProperty(LastModifiedDate.class);
createdByProperty = persistentEntity.getPersistentProperty(CreatedBy.class);
lastModifiedByProperty = persistentEntity.getPersistentProperty(LastModifiedBy.class);
}

@Override
Expand Down Expand Up @@ -114,6 +122,26 @@ public void setLastModifiedDate(T entity) {

}

private void setCurrentAuditor(PersistentProperty<?> property, T entity) {
persistentEntity.getPropertyAccessor(entity).setProperty(property, auditorAware.getCurrentAuditor());
}

@Override
public void setCreatedBy(T entity) {
if (null == createdByProperty || null == auditorAware) {
return;
}
setCurrentAuditor(createdByProperty, entity);
}

@Override
public void setLastModifiedBy(T entity) {
if (null == lastModifiedByProperty || null == auditorAware) {
return;
}
setCurrentAuditor(lastModifiedByProperty, entity);
}

@Override
public int increaseVersion(T entity) {
PersistentProperty<?> versionProperty = persistentEntity.getVersionProperty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.springframework.data.mybatis.repository.support;

import org.springframework.data.domain.AuditorAware;
import org.springframework.data.domain.Persistable;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.repository.core.support.AbstractEntityInformation;
Expand All @@ -37,8 +38,8 @@ public class MybatisPersistableEntityInformation<T extends Persistable<ID>, ID e
* @param persistentEntity
* @param domainClass must not be {@literal null}.
*/
protected MybatisPersistableEntityInformation(PersistentEntity<T, ?> persistentEntity, Class<T> domainClass) {
super(persistentEntity, domainClass);
protected MybatisPersistableEntityInformation(PersistentEntity<T, ?> persistentEntity, AuditorAware<?> auditorAware, Class<T> domainClass) {
super(persistentEntity, auditorAware, domainClass);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.springframework.data.mybatis.repository.support;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.mybatis.mapping.MybatisMappingContext;
import org.springframework.data.mybatis.repository.dialect.Dialect;
import org.springframework.data.mybatis.repository.query.MybatisQueryLookupStrategy;
Expand All @@ -45,23 +46,25 @@ public class MybatisRepositoryFactory extends RepositoryFactorySupport {
private final SqlSessionTemplate sessionTemplate;
private final Dialect dialect;
private final MybatisMappingContext mappingContext;
private final AuditorAware<?> auditorAware;

public MybatisRepositoryFactory(final MybatisMappingContext mappingContext,
final SqlSessionTemplate sessionTemplate,
final Dialect dialect) {
final Dialect dialect,
AuditorAware<?> auditorAware) {
Assert.notNull(sessionTemplate);
Assert.notNull(dialect);
this.mappingContext = mappingContext;
this.sessionTemplate = sessionTemplate;
this.dialect = dialect;

this.auditorAware = auditorAware;
}

@Override
public <T, ID extends Serializable> MybatisEntityInformation<T, ID> getEntityInformation(Class<T> domainClass) {

return (MybatisEntityInformation<T, ID>)
MybatisEntityInformationSupport.getEntityInformation(mappingContext, domainClass);
MybatisEntityInformationSupport.getEntityInformation(mappingContext, auditorAware, domainClass);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package org.springframework.data.mybatis.repository.support;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.mybatis.mapping.MybatisMappingContext;
import org.springframework.data.mybatis.repository.dialect.Dialect;
import org.springframework.data.repository.Repository;
Expand All @@ -40,6 +42,9 @@ public class MybatisRepositoryFactoryBean<T extends Repository<S, ID>, S, ID ext
private Dialect dialect;
private MybatisMappingContext mappingContext;

@Autowired(required = false)
private AuditorAware<?> auditorAware;

@Override
public void afterPropertiesSet() {
Assert.notNull(sqlSessionTemplate, "SqlSessionTemplate must not be null.");
Expand All @@ -54,7 +59,7 @@ public void setMappingContext(MybatisMappingContext mappingContext) {

@Override
protected RepositoryFactorySupport doCreateRepositoryFactory() {
return new MybatisRepositoryFactory(mappingContext, sqlSessionTemplate, dialect);
return new MybatisRepositoryFactory(mappingContext, sqlSessionTemplate, dialect, auditorAware);
}

public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
Expand All @@ -65,4 +70,7 @@ public void setDialect(Dialect dialect) {
this.dialect = dialect;
}

public void setAuditorAware(AuditorAware<?> auditorAware) {
this.auditorAware = auditorAware;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public <S extends T> S save(S entity) {
// insert

entityInformation.setCreatedDate(entity);
entityInformation.setCreatedBy(entity);

if (entityInformation.hasVersion()) {
entityInformation.setVersion(entity, 0);
Expand All @@ -81,6 +82,7 @@ public <S extends T> S save(S entity) {
// update

entityInformation.setLastModifiedDate(entity);
entityInformation.setLastModifiedBy(entity);

int row = update(STATEMENT_UPDATE, entity);
if (row == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.mybatis.repository.config.EnableMybatisRepositories;
import org.springframework.data.mybatis.support.SqlSessionFactoryBean;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
Expand Down Expand Up @@ -57,4 +58,14 @@ public PlatformTransactionManager transactionManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}

@Bean
public AuditorAware<Long> auditorAware() {
return new AuditorAware<Long>() {
@Override
public Long getCurrentAuditor() {
return 1001L;
}
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@

package org.springframework.data.mybatis.domain.sample;

import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.annotation.Version;
import org.springframework.data.annotation.*;
import org.springframework.data.mybatis.annotations.Column;
import org.springframework.data.mybatis.annotations.Entity;
import org.springframework.data.mybatis.annotations.Id;
Expand Down Expand Up @@ -50,9 +48,11 @@ public class Department {
protected Date lastModifiedDate;

@Column(name = "creator")
@CreatedBy
protected Long createdBy;

@Column(name = "MODIFIER")
@LastModifiedBy
protected Long lastModifiedBy;

public Department() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void setUp() throws Exception {
}

@Test
public void testCreateDate() {
public void testCreatedDate() {
Department develop = new Department("develop");
assertNull(develop.getCreatedDate());
develop = departmentRepository.save(develop);
Expand All @@ -70,5 +70,21 @@ public void testLastModifiedDate() {
assertNotNull(research.getLastModifiedDate());
}

@Test
public void testCreatedBy() {
Department develop = new Department("develop");
assertNull(develop.getCreatedBy());
develop = departmentRepository.save(develop);
assertNotNull(develop.getCreatedBy());
}

@Test
public void testLastModifiedBy() {
assertNull(research.getLastModifiedBy());
research.setName("research1");
departmentRepository.save(research);
assertNotNull(research.getLastModifiedBy());
}


}
4 changes: 1 addition & 3 deletions src/test/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="org.springframework.data.mybatis" level="DEBUG" additivity="false">
<appender-ref ref="STDOUT"/>
</logger>

<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>
Expand Down

0 comments on commit 3c24082

Please sign in to comment.