com.diboot.core.binding.RelationsBinder Maven / Gradle / Ivy
/*
* Copyright (c) 2015-2020, www.dibo.ltd ([email protected]).
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.diboot.core.binding;
import com.diboot.core.binding.binder.parallel.ParallelBindingManager;
import com.diboot.core.binding.helper.DeepRelationsBinder;
import com.diboot.core.binding.parser.BindAnnotationGroup;
import com.diboot.core.binding.parser.FieldAnnotation;
import com.diboot.core.binding.parser.ParserCache;
import com.diboot.core.service.I18nConfigService;
import com.diboot.core.util.BeanUtils;
import com.diboot.core.util.ContextHolder;
import com.diboot.core.util.V;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.context.request.RequestContextHolder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
/**
* 关联关系绑定管理器
* @author [email protected]
* @version v2.0
* @date 2019/7/18
*/
@SuppressWarnings("JavaDoc")
public class RelationsBinder {
private static final Logger log = LoggerFactory.getLogger(RelationsBinder.class);
/**
* 自动转换和绑定单个VO中的注解关联(禁止循环调用,多个对象请调用convertAndBind(voList, voClass))
* @param voClass 需要转换的VO class
* @param
* @param
* @return
*/
public static VO convertAndBind(E entity, Class voClass){
// 转换为VO列表
VO vo = BeanUtils.convert(entity, voClass);
// 自动绑定关联对象
bind(vo);
return vo;
}
/**
* 自动转换和绑定多个VO中的注解关联
* @param entityList 需要转换的VO list
* @param voClass VO class
* @param
* @param
* @return
*/
public static List convertAndBind(List entityList, Class voClass){
// 转换为VO列表
List voList = BeanUtils.convertList(entityList, voClass);
// 自动绑定关联对象
bind(voList);
return voList;
}
/**
* 自动绑定单个VO的关联对象(禁止循环调用,多个对象请调用bind(voList))
* @param vo 需要注解绑定的对象
* @return
* @throws Exception
*/
public static void bind(VO vo){
bind(Collections.singletonList(vo));
}
/**
* 自动绑定多个VO集合的关联对象
* @param voList 需要注解绑定的对象集合
* @return
* @throws Exception
*/
public static void bind(List voList){
bind(voList, true);
}
/**
* 自动绑定多个VO集合的关联对象
* @param voList 需要注解绑定的对象集合
* @param enableDeepBind
* @return
* @throws Exception
*/
public static void bind(List voList, boolean enableDeepBind){
if(V.isEmpty(voList)){
return;
}
// 获取VO类
Class> voClass = voList.get(0).getClass();
BindAnnotationGroup bindAnnotationGroup = ParserCache.getBindAnnotationGroup(voClass);
if(bindAnnotationGroup.isEmpty()){
return;
}
RequestContextHolder.setRequestAttributes(RequestContextHolder.getRequestAttributes(), true);
LocaleContextHolder.setLocaleContext(LocaleContextHolder.getLocaleContext(),true);
ParallelBindingManager parallelBindingManager = ContextHolder.getBean(ParallelBindingManager.class);
// 不可能出现的错误,但是编译器需要
assert parallelBindingManager != null;
List> binderFutures = new ArrayList<>();
// 绑定Field字段名
Map> bindFieldGroupMap = bindAnnotationGroup.getBindFieldGroupMap();
if(bindFieldGroupMap != null){
for(Map.Entry> entry : bindFieldGroupMap.entrySet()){
CompletableFuture bindFieldFuture = parallelBindingManager.doBindingField(voList, entry.getValue());
binderFutures.add(bindFieldFuture);
}
}
// 绑定数据字典
List dictAnnoList = bindAnnotationGroup.getBindDictAnnotations();
if(dictAnnoList != null){
if(bindAnnotationGroup.isRequireSequential()){
CompletableFuture.allOf(binderFutures.toArray(new CompletableFuture[0])).join();
}
for(FieldAnnotation annotation : dictAnnoList){
parallelBindingManager.doBindingDict(voList, annotation);
}
}
// 绑定Entity实体
List entityAnnoList = bindAnnotationGroup.getBindEntityAnnotations();
if(entityAnnoList != null){
for(FieldAnnotation anno : entityAnnoList){
// 绑定关联对象entity
CompletableFuture bindEntFuture = parallelBindingManager.doBindingEntity(voList, anno);
binderFutures.add(bindEntFuture);
}
}
// 绑定Entity实体List
List entitiesAnnoList = bindAnnotationGroup.getBindEntityListAnnotations();
if(entitiesAnnoList != null){
for(FieldAnnotation anno : entitiesAnnoList){
// 绑定关联对象entity
CompletableFuture bindEntFuture = parallelBindingManager.doBindingEntityList(voList, anno);
binderFutures.add(bindEntFuture);
}
}
// 绑定Entity field List
Map> bindFieldListGroupMap = bindAnnotationGroup.getBindFieldListGroupMap();
if(bindFieldListGroupMap != null){
// 解析条件并且执行绑定
for(Map.Entry> entry : bindFieldListGroupMap.entrySet()){
CompletableFuture bindFieldFuture = parallelBindingManager.doBindingFieldList(voList, entry.getValue());
binderFutures.add(bindFieldFuture);
}
}
// 绑定count子项计数
List countAnnoList = bindAnnotationGroup.getBindCountAnnotations();
if(countAnnoList != null){
for(FieldAnnotation anno : countAnnoList){
// 绑定关联对象count计数
CompletableFuture bindCountFuture = parallelBindingManager.doBindingCount(voList, anno);
binderFutures.add(bindCountFuture);
}
}
// 开启国际化
if(isEnableI18N()) {
// 绑定国际化翻译
List i18nAnnoList = bindAnnotationGroup.getBindI18nAnnotations();
if(i18nAnnoList != null){
for(FieldAnnotation anno : i18nAnnoList){
// 绑定关联对象count计数
parallelBindingManager.doBindingI18n(voList, anno);
}
}
}
// 执行绑定
CompletableFuture.allOf(binderFutures.toArray(new CompletableFuture[0])).join();
// 深度绑定
if(enableDeepBind){
List deepBindEntityAnnoList = bindAnnotationGroup.getDeepBindEntityAnnotations();
List deepBindEntitiesAnnoList = bindAnnotationGroup.getDeepBindEntityListAnnotations();
if(deepBindEntityAnnoList != null || deepBindEntitiesAnnoList != null){
if(V.notEmpty(deepBindEntityAnnoList)){
FieldAnnotation firstAnnotation = deepBindEntityAnnoList.get(0);
log.debug("执行深度绑定: {}({}) for field {}", firstAnnotation.getAnnotation().annotationType().getSimpleName(),
firstAnnotation.getFieldClass().getSimpleName(), firstAnnotation.getFieldName());
}
if(deepBindEntitiesAnnoList != null) {
FieldAnnotation firstAnnotation = deepBindEntitiesAnnoList.get(0);
log.debug("执行深度绑定: {}({}) for field {}", firstAnnotation.getAnnotation().annotationType().getSimpleName(),
firstAnnotation.getFieldClass().getSimpleName(), firstAnnotation.getFieldName());
}
DeepRelationsBinder.deepBind(voList, deepBindEntityAnnoList, deepBindEntitiesAnnoList);
}
}
}
/**
* 是否启用 i18n
* @return
*/
private static Boolean ENABLE_I18N = null;
private static boolean isEnableI18N() {
if(ENABLE_I18N == null){
ENABLE_I18N = ContextHolder.getBean(I18nConfigService.class) != null;
if(ENABLE_I18N){
log.info("启用 i8n 国际化翻译转换");
}
}
return ENABLE_I18N;
}
}