com.github.rexsheng.springboot.faster.system.mybatis.MybatisFlexAutoFillFieldModule Maven / Gradle / Ivy
The newest version!
package com.github.rexsheng.springboot.faster.system.mybatis;
import com.github.rexsheng.springboot.faster.system.modular.SpringModule;
import com.github.rexsheng.springboot.faster.system.utils.AuthenticationUtil;
import com.github.rexsheng.springboot.faster.util.DateUtil;
import com.mybatisflex.annotation.InsertListener;
import com.mybatisflex.annotation.UpdateListener;
import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.spring.boot.MyBatisFlexCustomizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.env.Environment;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.core.type.filter.TypeFilter;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
@SpringModule
@ConditionalOnClass(MyBatisFlexCustomizer.class)
public class MybatisFlexAutoFillFieldModule implements MyBatisFlexCustomizer, EnvironmentAware {
private static final Logger logger= LoggerFactory.getLogger(EntityFieldAutoFillRegisterBean.class);
private Environment environment;
private EntityFieldAutoFillRegisterBean entityAutoFillRegister;
@Autowired(required = false)
public void setEntityAutoFillRegister(EntityFieldAutoFillRegisterBean entityAutoFillRegister) {
this.entityAutoFillRegister = entityAutoFillRegister;
}
@Override
public void customize(FlexGlobalConfig flexGlobalConfig) {
if(entityAutoFillRegister!=null){
Optional.ofNullable(entityAutoFillRegister.getConfigurerList()).ifPresent(list->list.forEach(configurer->{
Set> classes = new HashSet<>();
if(configurer.getEntityClass()!=null){
classes.add(configurer.getEntityClass());
}
else{
ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
TypeFilter serviceFilter=new AnnotationTypeFilter(configurer.getAnnotationClass());
scanner.addIncludeFilter(serviceFilter);
scanner.setEnvironment(environment);
scanner.findCandidateComponents(configurer.getPackageName())
.forEach(beanDefinition -> {
Class> clazz = null;
try {
clazz = Class.forName(beanDefinition.getBeanClassName());
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
if(configurer.getClassFilter()==null || configurer.getClassFilter().test(clazz)){
classes.add(clazz);
}
});
}
for (Class> clazz : classes) {
ObjectOperationListener userListener = new ObjectOperationListener(clazz,configurer);
flexGlobalConfig.registerInsertListener(userListener,clazz);
flexGlobalConfig.registerUpdateListener(userListener,clazz);
if(logger.isDebugEnabled()){
logger.debug("字段自动填充: {}", userListener);
}
}
}));
}
}
@Override
public void setEnvironment(Environment environment) {
this.environment=environment;
}
public static class ObjectOperationListener implements InsertListener, UpdateListener {
private Class> entityClass;
private boolean onInsertFillCreateTime;
private boolean onInsertFillCreateUser;
private boolean onInsertFillUpdateTime;
private boolean onInsertFillUpdateUser;
private boolean overrideCreateTimeIfNotNull;
private boolean overrideCreateUserIfNotNull;
private boolean overrideUpdateTimeIfNotNull;
private boolean overrideUpdateUserIfNotNull;
public ObjectOperationListener(EntityFieldAutoFillRegisterBean.EntityAutoFillConfigurer config) {
this(config.getEntityClass(), config);
}
public ObjectOperationListener(Class> entityClass, EntityFieldAutoFillRegisterBean.EntityAutoFillConfigurer config) {
this.entityClass = entityClass;
this.onInsertFillCreateTime=config.getAutoCreateTime()==null?true:config.getAutoCreateTime();
this.onInsertFillCreateUser=config.getAutoCreateUser()==null?true:config.getAutoCreateUser();
this.onInsertFillUpdateTime=config.getAutoUpdateTime()==null?true:config.getAutoUpdateTime();
this.onInsertFillUpdateUser=config.getAutoUpdateUser()==null?true:config.getAutoUpdateUser();
this.overrideCreateTimeIfNotNull=config.getOverrideCreateTimeIfNotNull()==null?true:config.getOverrideCreateTimeIfNotNull();
this.overrideCreateUserIfNotNull=config.getOverrideCreateUserIfNotNull()==null?true:config.getOverrideCreateUserIfNotNull();
this.overrideUpdateTimeIfNotNull=config.getOverrideUpdateTimeIfNotNull()==null?false:config.getOverrideUpdateTimeIfNotNull();
this.overrideUpdateUserIfNotNull=config.getOverrideUpdateUserIfNull()==null?false:config.getOverrideUpdateUserIfNull();
}
@Override
public void onInsert(Object entity) {
if(entityClass.isInstance(entity)){
if(onInsertFillCreateTime){
Field createTimeField= ReflectionUtils.findField(entityClass,"createTime", LocalDateTime.class);
if(createTimeField!=null){
if(overrideCreateTimeIfNotNull || !hasValue(createTimeField,entity)){
Method createTimeSetterMethod=ReflectionUtils.findMethod(entityClass,"setCreateTime", LocalDateTime.class);
if(createTimeSetterMethod!=null){
ReflectionUtils.invokeMethod(createTimeSetterMethod, entity, DateUtil.currentDateTime());
}
}
}
}
if(onInsertFillCreateUser) {
Field createUserField = ReflectionUtils.findField(entityClass, "createUser", Long.class);
if (createUserField != null) {
if(overrideCreateUserIfNotNull || !hasValue(createUserField,entity)){
Method createUserSetterMethod = ReflectionUtils.findMethod(entityClass, "setCreateUser", Long.class);
if (createUserSetterMethod != null) {
ReflectionUtils.invokeMethod(createUserSetterMethod, entity, AuthenticationUtil.currentUserId());
}
}
}
}
}
}
@Override
public void onUpdate(Object entity) {
if(entityClass.isInstance(entity)){
if(onInsertFillUpdateTime) {
Field updateTimeField = ReflectionUtils.findField(entityClass, "updateTime", LocalDateTime.class);
if (updateTimeField != null) {
if(overrideUpdateTimeIfNotNull || !hasValue(updateTimeField,entity)){
Method updateTimeSetterMethod = ReflectionUtils.findMethod(entityClass, "setUpdateTime", LocalDateTime.class);
if (updateTimeSetterMethod != null) {
ReflectionUtils.invokeMethod(updateTimeSetterMethod, entity, DateUtil.currentDateTime());
}
}
}
}
if(onInsertFillUpdateUser) {
Field updateUserField = ReflectionUtils.findField(entityClass, "updateUser", Long.class);
if (updateUserField != null) {
if(overrideUpdateUserIfNotNull || !hasValue(updateUserField,entity)) {
Method updateUserSetterMethod = ReflectionUtils.findMethod(entityClass, "setUpdateUser", Long.class);
if (updateUserSetterMethod != null) {
ReflectionUtils.invokeMethod(updateUserSetterMethod, entity, AuthenticationUtil.currentUserId());
}
}
}
}
}
}
private boolean hasValue(Field field,Object entity){
ReflectionUtils.makeAccessible(field);
return ReflectionUtils.getField(field,entity)!=null;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("{");
sb.append("entityClass=").append(entityClass);
sb.append(", onInsertFillCreateTime=").append(onInsertFillCreateTime);
sb.append(", onInsertFillCreateUser=").append(onInsertFillCreateUser);
sb.append(", onInsertFillUpdateTime=").append(onInsertFillUpdateTime);
sb.append(", onInsertFillUpdateUser=").append(onInsertFillUpdateUser);
sb.append(", overrideCreateTimeIfNotNull=").append(overrideCreateTimeIfNotNull);
sb.append(", overrideCreateUserIfNotNull=").append(overrideCreateUserIfNotNull);
sb.append(", overrideUpdateTimeIfNotNull=").append(overrideUpdateTimeIfNotNull);
sb.append(", overrideUpdateUserIfNotNull=").append(overrideUpdateUserIfNotNull);
sb.append('}');
return sb.toString();
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy