diff --git a/pom.xml b/pom.xml index 132fac37d797e7189dfacf5738ce2cbadecabc34..e7260ab9a436f3ff85c10e15da148995477df7af 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 com.gitee.digital-engine spring-domain - 2.8.1 + 2.8.2 pom diff --git a/spring-boot-starter-domain/pom.xml b/spring-boot-starter-domain/pom.xml index 32fd6852cb8d8b1c6d6ec9bffc51fb1ea1a3d7f1..da0cf2da84274d16e0fcc8f144dd9ffa90c59e38 100644 --- a/spring-boot-starter-domain/pom.xml +++ b/spring-boot-starter-domain/pom.xml @@ -6,7 +6,7 @@ com.gitee.digital-engine spring-domain - 2.8.1 + 2.8.2 spring-boot-starter-domain diff --git a/spring-boot-starter-domain/src/main/java/com/gitee/spring/boot/starter/domain/repository/MybatisPlusRepository.java b/spring-boot-starter-domain/src/main/java/com/gitee/spring/boot/starter/domain/repository/MybatisPlusRepository.java index 161200f485d4cc61cb897ba8291f9a76bdfee555..7920c1e8c0e0f68203fe257ea768d540758eef64 100644 --- a/spring-boot-starter-domain/src/main/java/com/gitee/spring/boot/starter/domain/repository/MybatisPlusRepository.java +++ b/spring-boot-starter-domain/src/main/java/com/gitee/spring/boot/starter/domain/repository/MybatisPlusRepository.java @@ -4,6 +4,7 @@ import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.lang.Pair; import cn.hutool.core.util.ReflectUtil; import cn.hutool.core.util.StrUtil; +import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; @@ -39,7 +40,9 @@ public class MybatisPlusRepository extends AbstractRepository { if (isTableId || "id".equals(fieldName)) { continue; } - fieldColumnPairs.add(new Pair<>(fieldName, StrUtil.toUnderlineCase(fieldName))); + TableField tableField = field.getAnnotation(TableField.class); + String columnName = tableField != null ? tableField.value() : StrUtil.toUnderlineCase(fieldName); + fieldColumnPairs.add(new Pair<>(fieldName, columnName)); } } } @@ -79,17 +82,21 @@ public class MybatisPlusRepository extends AbstractRepository { public int update(BoundedContext boundedContext, Object entity) { Object primaryKey = BeanUtil.getFieldValue(entity, "id"); if (primaryKey != null) { - UpdateWrapper updateWrapper = new UpdateWrapper<>(); - updateWrapper.eq("id", primaryKey); Set fieldNames = (Set) boundedContext.get("#forceUpdate"); - for (Pair fieldColumnPair : fieldColumnPairs) { - String fieldName = fieldColumnPair.getKey(); - Object fieldValue = BeanUtil.getFieldValue(entity, fieldName); - if (fieldValue != null || (fieldNames != null && fieldNames.contains(fieldName))) { - updateWrapper.set(true, fieldColumnPair.getValue(), fieldValue); + if (fieldNames != null && !fieldNames.isEmpty()) { + UpdateWrapper updateWrapper = new UpdateWrapper<>(); + updateWrapper.eq("id", primaryKey); + for (Pair fieldColumnPair : fieldColumnPairs) { + String fieldName = fieldColumnPair.getKey(); + Object fieldValue = BeanUtil.getFieldValue(entity, fieldName); + if (fieldValue != null || fieldNames.contains(fieldName)) { + updateWrapper.set(true, fieldColumnPair.getValue(), fieldValue); + } } + return baseMapper.update(null, updateWrapper); + } else { + return baseMapper.updateById(entity); } - return baseMapper.update(null, updateWrapper); } return 0; } diff --git a/spring-domain-coating/pom.xml b/spring-domain-coating/pom.xml index 07067b3661425b5fb5677087bc733184aa30433d..3275c08bdb0885d15db08f6e3d457341df4fd131 100644 --- a/spring-domain-coating/pom.xml +++ b/spring-domain-coating/pom.xml @@ -6,7 +6,7 @@ com.gitee.digital-engine spring-domain - 2.8.1 + 2.8.2 spring-domain-coating diff --git a/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/repository/AbstractChainRepository.java b/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/repository/AbstractChainRepository.java index cbe06bbea3e02cf4b5530160e02fbc742bf2d36e..a588b707d555d0b4b13b6ea533e4f235b9550476 100644 --- a/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/repository/AbstractChainRepository.java +++ b/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/repository/AbstractChainRepository.java @@ -8,20 +8,13 @@ import com.gitee.spring.domain.coating.entity.RepositoryDefinition; import com.gitee.spring.domain.coating.entity.RepositoryLocation; import com.gitee.spring.domain.coating.impl.DefaultCoatingAssembler; import com.gitee.spring.domain.core.api.EntityBinder; +import com.gitee.spring.domain.core.api.PropertyConverter; +import com.gitee.spring.domain.core.entity.*; import com.gitee.spring.domain.core.impl.binder.PropertyEntityBinder; -import com.gitee.spring.domain.core.entity.BindingDefinition; -import com.gitee.spring.domain.core.entity.BoundedContext; -import com.gitee.spring.domain.core.entity.EntityCriterion; -import com.gitee.spring.domain.core.entity.EntityDefinition; -import com.gitee.spring.domain.core.entity.EntityExample; import com.gitee.spring.domain.core.repository.ConfiguredRepository; import lombok.extern.slf4j.Slf4j; -import java.util.ArrayList; -import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; +import java.util.*; @Slf4j public abstract class AbstractChainRepository extends AbstractCoatingRepository { @@ -134,11 +127,13 @@ public abstract class AbstractChainRepository extends AbstractCoatingRepo }); } - protected List collectFieldValues(BoundedContext boundedContext, List entities, EntityBinder entityBinder) { + protected List collectFieldValues(BoundedContext boundedContext, List entities, PropertyEntityBinder propertyEntityBinder) { + PropertyConverter propertyConverter = propertyEntityBinder.getPropertyConverter(); List fieldValues = new ArrayList<>(); for (Object entity : entities) { - Object fieldValue = entityBinder.getFieldValue(boundedContext, entity); + Object fieldValue = propertyEntityBinder.getFieldValue(boundedContext, entity); if (fieldValue != null) { + fieldValue = propertyConverter.reverseConvert(boundedContext, fieldValue); fieldValues.add(fieldValue); } } diff --git a/spring-domain-core/pom.xml b/spring-domain-core/pom.xml index df78d479ae1bedb890a5090536081f2db40cb2dd..3d92a9b81e584591b5572aea7d0be4df51f29c72 100644 --- a/spring-domain-core/pom.xml +++ b/spring-domain-core/pom.xml @@ -6,7 +6,7 @@ com.gitee.digital-engine spring-domain - 2.8.1 + 2.8.2 spring-domain-core diff --git a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/api/PropertyConverter.java b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/api/PropertyConverter.java index 6ba0bc40e9dea226658265a829eef066c9cc9a0c..5560cdfadb31f62536447383cce9c65670bda8c7 100644 --- a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/api/PropertyConverter.java +++ b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/api/PropertyConverter.java @@ -6,4 +6,6 @@ public interface PropertyConverter { Object convert(BoundedContext boundedContext, Object property); + Object reverseConvert(BoundedContext boundedContext, Object property); + } diff --git a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/entity/BindingDefinition.java b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/entity/BindingDefinition.java index 058e9984ab3f5bd03cada05c2f76ab8a9f5a228f..b06ab32be7ff18f91772a647ef4f29b545e9d1cc 100644 --- a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/entity/BindingDefinition.java +++ b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/entity/BindingDefinition.java @@ -14,4 +14,5 @@ public class BindingDefinition { private String bindExpAttribute; private String bindAliasAttribute; private String propertyAttribute; + private Class converterClass; } diff --git a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/entity/EntityDefinition.java b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/entity/EntityDefinition.java index 60964d64f93b82445d077d465904c63247b21a2c..bf1b24a4d1712224e7cebc84cc97aaa9f63ffbd4 100644 --- a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/entity/EntityDefinition.java +++ b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/entity/EntityDefinition.java @@ -5,7 +5,6 @@ import lombok.Data; import org.springframework.core.annotation.AnnotationAttributes; import java.lang.reflect.AnnotatedElement; -import java.util.Map; import java.util.Set; @Data @@ -18,6 +17,7 @@ public class EntityDefinition { private boolean collection; private Class genericEntityClass; private String fieldName; + private Set fieldNames; private AnnotationAttributes attributes; private String idAttribute; private Set sceneAttribute; @@ -34,6 +34,4 @@ public class EntityDefinition { private int orderAttribute; private String[] boundColumns; private boolean boundEntity; - private Set fieldNames; - private Map entityPropertyChainMap; } diff --git a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/impl/DefaultPropertyConverter.java b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/impl/DefaultPropertyConverter.java index f03af17c999208bcb1e7b29a9c02d95bc766605b..9b2dc6c09424c745059cb8f3f8a21520c9554e41 100644 --- a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/impl/DefaultPropertyConverter.java +++ b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/impl/DefaultPropertyConverter.java @@ -39,4 +39,9 @@ public class DefaultPropertyConverter implements PropertyConverter { } } + @Override + public Object reverseConvert(BoundedContext boundedContext, Object property) { + return property; + } + } diff --git a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/impl/EntityStateResolver.java b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/impl/EntityStateResolver.java index d1541946504f621ce1612bad434d241b1ab7f1d3..6b0c1d03f152dfe9e135e13e5dd29f767e6ac400 100644 --- a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/impl/EntityStateResolver.java +++ b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/impl/EntityStateResolver.java @@ -8,7 +8,7 @@ import com.gitee.spring.domain.core.repository.ConfiguredRepository; public class EntityStateResolver { - public int resolveEntityStateByContext(BoundedContext boundedContext, ConfiguredRepository configuredRepository) { + public int resolveContextEntityState(BoundedContext boundedContext, ConfiguredRepository configuredRepository) { EntityDefinition entityDefinition = configuredRepository.getEntityDefinition(); String idAttribute = entityDefinition.getIdAttribute(); if (idAttribute != null) { diff --git a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/impl/binder/PropertyEntityBinder.java b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/impl/binder/PropertyEntityBinder.java index 5279d9bd39b039441ec0f17ddfd60e9cad8f856e..fa20f74c30b5721116c76772d6980dae966f1b80 100644 --- a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/impl/binder/PropertyEntityBinder.java +++ b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/impl/binder/PropertyEntityBinder.java @@ -30,6 +30,11 @@ public class PropertyEntityBinder extends AbstractEntityBuilder { this.propertyConverter = propertyConverter; } + public boolean isSameType() { + return boundEntityPropertyChain.getEntityClass() == fieldEntityPropertyChain.getEntityClass() + && boundEntityPropertyChain.getGenericEntityClass() == fieldEntityPropertyChain.getGenericEntityClass(); + } + @Override public Object getBoundValue(BoundedContext boundedContext, Object rootEntity) { Object boundValue = boundEntityPropertyChain.getValue(rootEntity); diff --git a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/repository/AbstractContextRepository.java b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/repository/AbstractContextRepository.java index ec572732ffe4a21975a1d3cee64c3999c787c5ce..0a9c5bd3633976a9dd8404172c1397fc5cdcea24 100644 --- a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/repository/AbstractContextRepository.java +++ b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/repository/AbstractContextRepository.java @@ -204,12 +204,11 @@ public abstract class AbstractContextRepository extends AbstractRepositor Class assemblerClass = attributes.getClass(Attribute.ASSEMBLER_ATTRIBUTE); Class repositoryClass = attributes.getClass(Attribute.REPOSITORY_ATTRIBUTE); + Set boundColumns = new LinkedHashSet<>(); + List allEntityBinders = new ArrayList<>(); List boundEntityBinders = new ArrayList<>(); List contextEntityBinders = new ArrayList<>(); - PropertyEntityBinder boundIdEntityBinder = null; - - Set boundColumns = new LinkedHashSet<>(); for (Binding bindingAnnotation : bindingAnnotations) { AnnotationAttributes bindingAttributes = AnnotationUtils.getAnnotationAttributes( @@ -235,22 +234,22 @@ public abstract class AbstractContextRepository extends AbstractRepositor bindExpAttribute = bindAttribute; } - boolean isFromContext = !bindAttribute.startsWith("/"); - boolean isIdField = "id".equals(fieldAttribute); - boolean isBoundId = !isFromContext && isIdField; + boolean isBindProperty = bindAttribute.startsWith("/"); - if (!isFromContext && StringUtils.isBlank(bindAliasAttribute)) { - bindAliasAttribute = PathUtils.getFieldName(bindAttribute); + if (isBindProperty && StringUtils.isBlank(bindAliasAttribute)) { + bindAliasAttribute = StringUtils.isBlank(propertyAttribute) ? PathUtils.getFieldName(bindAttribute) : propertyAttribute; } - if (!isFromContext) { + if (isBindProperty) { boundColumns.add(StrUtil.toUnderlineCase(aliasAttribute)); } BindingDefinition bindingDefinition = new BindingDefinition( - bindingAttributes, fieldAttribute, aliasAttribute, bindAttribute, bindExpAttribute, bindAliasAttribute, propertyAttribute); + bindingAttributes, fieldAttribute, aliasAttribute, + bindAttribute, bindExpAttribute, bindAliasAttribute, + propertyAttribute, converterClass); - if (!isFromContext) { + if (isBindProperty) { String belongAccessPath = PathUtils.getBelongPath(allConfiguredRepositoryMap.keySet(), bindAttribute); ConfiguredRepository belongConfiguredRepository = allConfiguredRepositoryMap.get(belongAccessPath); Assert.notNull(belongConfiguredRepository, "The belong repository cannot be null!"); @@ -279,12 +278,8 @@ public abstract class AbstractContextRepository extends AbstractRepositor bindingDefinition, null, belongAccessPath, belongConfiguredRepository, boundEntityPropertyChain, propertyConverter); - allEntityBinders.add(propertyEntityBinder); boundEntityBinders.add(propertyEntityBinder); - if (isBoundId) { - boundIdEntityBinder = propertyEntityBinder; - } } else { ContextEntityBinder contextEntityBinder = new ContextEntityBinder(bindingDefinition, null); @@ -293,16 +288,12 @@ public abstract class AbstractContextRepository extends AbstractRepositor } } - if (boundIdEntityBinder != null && orderAttribute == 0) { - orderAttribute = -1; - } - EntityDefinition entityDefinition = new EntityDefinition( isAggregateRoot, accessPath, annotatedElement, - entityClass, isCollection, genericEntityClass, fieldName, + entityClass, isCollection, genericEntityClass, fieldName, ReflectUtils.getFieldNames(genericEntityClass), attributes, idAttribute, sceneAttribute, mapper, pojoClass, sameType, mappedClass, useEntityExample, mapAsExample, orderByAsc, orderByDesc, orderBy, sort, orderAttribute, - boundColumns.toArray(new String[0]), false, new LinkedHashSet<>(), new LinkedHashMap<>()); + boundColumns.toArray(new String[0]), false); EntityMapper entityMapper = newEntityMapper(entityDefinition); if (mapAsExample) { @@ -341,10 +332,9 @@ public abstract class AbstractContextRepository extends AbstractRepositor } ConfiguredRepository configuredRepository = new ConfiguredRepository( - (AbstractRepository) repository, - entityPropertyChain, entityDefinition, - allEntityBinders, boundEntityBinders, contextEntityBinders, new ArrayList<>(), boundIdEntityBinder, - entityMapper, entityAssembler); + (AbstractRepository) repository, entityPropertyChain, entityDefinition, + allEntityBinders, boundEntityBinders, contextEntityBinders, new ArrayList<>(), null, + entityMapper, entityAssembler, new LinkedHashMap<>()); return postProcessRepository(configuredRepository); } @@ -356,16 +346,11 @@ public abstract class AbstractContextRepository extends AbstractRepositor protected void postProcessAllRepositories() { Map allEntityPropertyChainMap = entityPropertiesResolver.getAllEntityPropertyChainMap(); allEntityPropertyChainMap.forEach((accessPath, entityPropertyChain) -> { - String belongAccessPath = PathUtils.getBelongPath(allConfiguredRepositoryMap.keySet(), accessPath); + String lastAccessPath = PathUtils.getLastAccessPath(accessPath); + String belongAccessPath = PathUtils.getBelongPath(allConfiguredRepositoryMap.keySet(), lastAccessPath); ConfiguredRepository belongConfiguredRepository = allConfiguredRepositoryMap.get(belongAccessPath); Assert.notNull(belongConfiguredRepository, "The belong repository cannot be null!"); - EntityDefinition entityDefinition = belongConfiguredRepository.getEntityDefinition(); - - Set fieldNames = entityDefinition.getFieldNames(); - fieldNames.add(entityPropertyChain.getFieldName()); - - Map entityPropertyChainMap = entityDefinition.getEntityPropertyChainMap(); - String lastAccessPath = PathUtils.getLastAccessPath(accessPath); + Map entityPropertyChainMap = belongConfiguredRepository.getEntityPropertyChainMap(); EntityPropertyChain lastEntityPropertyChain = entityPropertyChainMap.get(lastAccessPath); EntityPropertyChain newEntityPropertyChain = new EntityPropertyChain(lastEntityPropertyChain, entityPropertyChain); entityPropertyChainMap.put(accessPath, newEntityPropertyChain); @@ -373,15 +358,13 @@ public abstract class AbstractContextRepository extends AbstractRepositor allConfiguredRepositoryMap.forEach((accessPath, configuredRepository) -> { EntityDefinition entityDefinition = configuredRepository.getEntityDefinition(); - Set fieldNames = entityDefinition.getFieldNames(); - Map entityPropertyChainMap = entityDefinition.getEntityPropertyChainMap(); + Map entityPropertyChainMap = configuredRepository.getEntityPropertyChainMap(); String prefixAccessPath = entityDefinition.isAggregateRoot() ? "/" : entityDefinition.getAccessPath() + "/"; if (entityPropertyChainMap.isEmpty() && entityDefinition.isCollection()) { EntityPropertiesResolver entityPropertiesResolver = new EntityPropertiesResolver(); entityPropertiesResolver.resolveEntityProperties("", entityDefinition.getGenericEntityClass()); Map subAllEntityPropertyChainMap = entityPropertiesResolver.getAllEntityPropertyChainMap(); - subAllEntityPropertyChainMap.values().forEach(entityPropertyChain -> fieldNames.add(entityPropertyChain.getFieldName())); entityPropertyChainMap.putAll(subAllEntityPropertyChainMap); prefixAccessPath = "/"; } @@ -389,6 +372,7 @@ public abstract class AbstractContextRepository extends AbstractRepositor List boundValueEntityBinders = configuredRepository.getBoundValueEntityBinders(); for (EntityBinder entityBinder : configuredRepository.getAllEntityBinders()) { BindingDefinition bindingDefinition = entityBinder.getBindingDefinition(); + String fieldAttribute = bindingDefinition.getFieldAttribute(); if (entityBinder instanceof AbstractEntityBuilder) { String fieldAccessPath = prefixAccessPath + bindingDefinition.getFieldAttribute(); @@ -400,13 +384,17 @@ public abstract class AbstractContextRepository extends AbstractRepositor if (entityBinder instanceof PropertyEntityBinder) { PropertyEntityBinder propertyEntityBinder = (PropertyEntityBinder) entityBinder; - boolean isNotBoundId = propertyEntityBinder != configuredRepository.getBoundIdEntityBinder(); - Class entityClass = propertyEntityBinder.getBoundEntityPropertyChain().getEntityClass(); - Class fieldEntityClass = propertyEntityBinder.getFieldEntityPropertyChain().getEntityClass(); boolean isBlankProperty = StringUtils.isBlank(bindingDefinition.getPropertyAttribute()); - boolean isDefaultConverter = bindingDefinition.getAttributes().getClass(Attribute.CONVERTER_ATTRIBUTE) == DefaultPropertyConverter.class; - if (isNotBoundId && entityClass == fieldEntityClass && isBlankProperty && isDefaultConverter) { - boundValueEntityBinders.add(entityBinder); + boolean isDefaultConverter = bindingDefinition.getConverterClass() == DefaultPropertyConverter.class; + if (propertyEntityBinder.isSameType() && isBlankProperty && isDefaultConverter) { + if (!"id".equals(fieldAttribute)) { + boundValueEntityBinders.add(entityBinder); + } else { + if (entityDefinition.getOrderAttribute() == 0) { + entityDefinition.setOrderAttribute(-1); + } + configuredRepository.setBoundIdEntityBinder(propertyEntityBinder); + } } } else if (entityBinder instanceof ContextEntityBinder) { diff --git a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/repository/AbstractGenericRepository.java b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/repository/AbstractGenericRepository.java index 55e7d6f2408b71141ac377e93064a20ecbade506..9eb38a9c33bc2ed285dcb011e093ae2075f43f28 100644 --- a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/repository/AbstractGenericRepository.java +++ b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/repository/AbstractGenericRepository.java @@ -1,10 +1,15 @@ package com.gitee.spring.domain.core.repository; -import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.lang.Assert; -import com.gitee.spring.domain.core.api.*; +import com.gitee.spring.domain.core.api.EntityBinder; +import com.gitee.spring.domain.core.api.EntityMapper; +import com.gitee.spring.domain.core.api.EntityProperty; +import com.gitee.spring.domain.core.api.GenericRepository; import com.gitee.spring.domain.core.constants.EntityState; -import com.gitee.spring.domain.core.entity.*; +import com.gitee.spring.domain.core.entity.BoundedContext; +import com.gitee.spring.domain.core.entity.EntityDefinition; +import com.gitee.spring.domain.core.entity.EntityExample; +import com.gitee.spring.domain.core.entity.EntityPropertyChain; import com.gitee.spring.domain.core.impl.EntityStateResolver; import com.gitee.spring.domain.core.utils.StringUtils; @@ -145,92 +150,13 @@ public abstract class AbstractGenericRepository extends AbstractDelegateR return operateEntityByState(boundedContext, entity, EntityState.UPDATE); } - protected int operateEntityByState(BoundedContext boundedContext, E entity, int expectedEntityState) { - Assert.notNull(entity, "The entity cannot be null!"); - int totalCount = 0; - AbstractDelegateRepository abstractDelegateRepository = adaptiveRepository(entity); - for (ConfiguredRepository configuredRepository : abstractDelegateRepository.getOrderedRepositories()) { - EntityPropertyChain entityPropertyChain = configuredRepository.getEntityPropertyChain(); - Object targetEntity = entityPropertyChain == null ? entity : entityPropertyChain.getValue(entity); - if (targetEntity != null && isMatchScenes(boundedContext, configuredRepository)) { - int contextEntityState = entityStateResolver.resolveEntityStateByContext(boundedContext, configuredRepository); - if (targetEntity instanceof Collection) { - for (Object eachEntity : (Collection) targetEntity) { - int entityState = entityStateResolver.resolveEntityState(expectedEntityState, contextEntityState, eachEntity); - if (entityState == EntityState.INSERT_OR_UPDATE) { - getBoundValueFromContext(boundedContext, entity, configuredRepository, eachEntity); - totalCount += configuredRepository.insertOrUpdate(boundedContext, eachEntity); - - } else if (entityState == EntityState.INSERT) { - getBoundValueFromContext(boundedContext, entity, configuredRepository, eachEntity); - totalCount += configuredRepository.insert(boundedContext, eachEntity); - - } else if (entityState == EntityState.UPDATE_SELECTIVE) { - totalCount += configuredRepository.updateSelective(boundedContext, eachEntity); - - } else if (entityState == EntityState.UPDATE) { - totalCount += configuredRepository.update(boundedContext, eachEntity); - - } else if (entityState == EntityState.DELETE) { - totalCount += configuredRepository.delete(boundedContext, eachEntity); - } - } - } else { - int entityState = entityStateResolver.resolveEntityState(expectedEntityState, contextEntityState, targetEntity); - if (entityState == EntityState.INSERT_OR_UPDATE) { - getBoundValueFromContext(boundedContext, entity, configuredRepository, targetEntity); - totalCount += configuredRepository.insertOrUpdate(boundedContext, targetEntity); - setBoundIdForBoundEntity(boundedContext, entity, configuredRepository, targetEntity); - - } else if (entityState == EntityState.INSERT) { - getBoundValueFromContext(boundedContext, entity, configuredRepository, targetEntity); - totalCount += configuredRepository.insert(boundedContext, targetEntity); - setBoundIdForBoundEntity(boundedContext, entity, configuredRepository, targetEntity); - - } else if (entityState == EntityState.UPDATE_SELECTIVE) { - totalCount += configuredRepository.updateSelective(boundedContext, targetEntity); - - } else if (entityState == EntityState.UPDATE) { - totalCount += configuredRepository.update(boundedContext, targetEntity); - - } else if (entityState == EntityState.DELETE) { - totalCount += configuredRepository.delete(boundedContext, targetEntity); - } - } - } - } - return totalCount; - } - - protected void getBoundValueFromContext(BoundedContext boundedContext, Object rootEntity, ConfiguredRepository configuredRepository, Object entity) { - for (EntityBinder entityBinder : configuredRepository.getBoundValueEntityBinders()) { - Object fieldValue = entityBinder.getFieldValue(boundedContext, entity); - if (fieldValue == null) { - Object boundValue = entityBinder.getBoundValue(boundedContext, rootEntity); - if (boundValue != null) { - entityBinder.setFieldValue(boundedContext, entity, boundValue); - } - } - } - } - - protected void setBoundIdForBoundEntity(BoundedContext boundedContext, Object rootEntity, ConfiguredRepository configuredRepository, Object entity) { - EntityBinder entityBinder = configuredRepository.getBoundIdEntityBinder(); - if (entityBinder != null) { - Object primaryKey = BeanUtil.getFieldValue(entity, "id"); - if (primaryKey != null) { - entityBinder.setBoundValue(boundedContext, rootEntity, primaryKey); - } - } - } - @Override public int updateByExample(BoundedContext boundedContext, Object entity, Object example) { Assert.notNull(entity, "The entity cannot be null!"); int totalCount = 0; for (ConfiguredRepository configuredRepository : getOrderedRepositories()) { if (isMatchScenes(boundedContext, configuredRepository)) { - int contextEntityState = entityStateResolver.resolveEntityStateByContext(boundedContext, configuredRepository); + int contextEntityState = entityStateResolver.resolveContextEntityState(boundedContext, configuredRepository); if (contextEntityState == EntityState.NONE) { totalCount += configuredRepository.updateByExample(boundedContext, entity, example); } @@ -260,7 +186,7 @@ public abstract class AbstractGenericRepository extends AbstractDelegateR int totalCount = 0; for (ConfiguredRepository configuredRepository : getOrderedRepositories()) { if (isMatchScenes(boundedContext, configuredRepository)) { - int contextEntityState = entityStateResolver.resolveEntityStateByContext(boundedContext, configuredRepository); + int contextEntityState = entityStateResolver.resolveContextEntityState(boundedContext, configuredRepository); if (contextEntityState == EntityState.NONE) { totalCount += configuredRepository.deleteByExample(boundedContext, example); } @@ -269,6 +195,76 @@ public abstract class AbstractGenericRepository extends AbstractDelegateR return totalCount; } + protected int operateEntityByState(BoundedContext boundedContext, E entity, int expectedEntityState) { + Assert.notNull(entity, "The entity cannot be null!"); + int totalCount = 0; + AbstractDelegateRepository abstractDelegateRepository = adaptiveRepository(entity); + for (ConfiguredRepository configuredRepository : abstractDelegateRepository.getOrderedRepositories()) { + EntityPropertyChain entityPropertyChain = configuredRepository.getEntityPropertyChain(); + Object targetEntity = entityPropertyChain == null ? entity : entityPropertyChain.getValue(entity); + if (targetEntity != null && isMatchScenes(boundedContext, configuredRepository)) { + int contextEntityState = entityStateResolver.resolveContextEntityState(boundedContext, configuredRepository); + if (targetEntity instanceof Collection) { + for (Object eachEntity : (Collection) targetEntity) { + int entityState = entityStateResolver.resolveEntityState(expectedEntityState, contextEntityState, eachEntity); + totalCount += doOperateEntityByState(boundedContext, entity, configuredRepository, eachEntity, entityState); + } + } else { + int entityState = entityStateResolver.resolveEntityState(expectedEntityState, contextEntityState, targetEntity); + totalCount += doOperateEntityByState(boundedContext, entity, configuredRepository, targetEntity, entityState); + if (expectedEntityState == EntityState.INSERT_OR_UPDATE || expectedEntityState == EntityState.INSERT) { + setBoundIdForBoundEntity(boundedContext, entity, configuredRepository, targetEntity); + } + } + } + } + return totalCount; + } + + protected int doOperateEntityByState(BoundedContext boundedContext, Object rootEntity, ConfiguredRepository configuredRepository, Object entity, + int entityState) { + if (entityState == EntityState.INSERT_OR_UPDATE) { + getBoundValueFromContext(boundedContext, rootEntity, configuredRepository, entity); + return configuredRepository.insertOrUpdate(boundedContext, entity); + + } else if (entityState == EntityState.INSERT) { + getBoundValueFromContext(boundedContext, rootEntity, configuredRepository, entity); + return configuredRepository.insert(boundedContext, entity); + + } else if (entityState == EntityState.UPDATE_SELECTIVE) { + return configuredRepository.updateSelective(boundedContext, entity); + + } else if (entityState == EntityState.UPDATE) { + return configuredRepository.update(boundedContext, entity); + + } else if (entityState == EntityState.DELETE) { + return configuredRepository.delete(boundedContext, entity); + } + return 0; + } + + protected void getBoundValueFromContext(BoundedContext boundedContext, Object rootEntity, ConfiguredRepository configuredRepository, Object entity) { + for (EntityBinder entityBinder : configuredRepository.getBoundValueEntityBinders()) { + Object fieldValue = entityBinder.getFieldValue(boundedContext, entity); + if (fieldValue == null) { + Object boundValue = entityBinder.getBoundValue(boundedContext, rootEntity); + if (boundValue != null) { + entityBinder.setFieldValue(boundedContext, entity, boundValue); + } + } + } + } + + protected void setBoundIdForBoundEntity(BoundedContext boundedContext, Object rootEntity, ConfiguredRepository configuredRepository, Object entity) { + EntityBinder entityBinder = configuredRepository.getBoundIdEntityBinder(); + if (entityBinder != null) { + Object primaryKey = entityBinder.getFieldValue(boundedContext, entity); + if (primaryKey != null) { + entityBinder.setBoundValue(boundedContext, rootEntity, primaryKey); + } + } + } + @Override public int insertList(BoundedContext boundedContext, List entities) { return entities.stream().mapToInt(entity -> insert(boundedContext, entity)).sum(); diff --git a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/repository/ConfiguredRepository.java b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/repository/ConfiguredRepository.java index 3c93d96ec5b4f19686950e6296fa9bbdee970b60..b4d842bc55f32e4750c8e539e0c704ef798c2530 100644 --- a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/repository/ConfiguredRepository.java +++ b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/repository/ConfiguredRepository.java @@ -14,6 +14,7 @@ import lombok.Setter; import java.util.Collections; import java.util.List; +import java.util.Map; @Getter @Setter @@ -28,6 +29,7 @@ public class ConfiguredRepository extends ProxyRepository { protected PropertyEntityBinder boundIdEntityBinder; protected EntityMapper entityMapper; protected EntityAssembler entityAssembler; + protected Map entityPropertyChainMap; public ConfiguredRepository(AbstractRepository repository, EntityPropertyChain entityPropertyChain, @@ -38,7 +40,8 @@ public class ConfiguredRepository extends ProxyRepository { List boundValueEntityBinders, PropertyEntityBinder boundIdEntityBinder, EntityMapper entityMapper, - EntityAssembler entityAssembler) { + EntityAssembler entityAssembler, + Map entityPropertyChainMap) { super(repository); this.entityPropertyChain = entityPropertyChain; this.entityDefinition = entityDefinition; @@ -49,6 +52,7 @@ public class ConfiguredRepository extends ProxyRepository { this.boundIdEntityBinder = boundIdEntityBinder; this.entityMapper = entityMapper; this.entityAssembler = entityAssembler; + this.entityPropertyChainMap = entityPropertyChainMap; } public ConfiguredRepository(ConfiguredRepository configuredRepository) { @@ -62,6 +66,7 @@ public class ConfiguredRepository extends ProxyRepository { this.boundIdEntityBinder = configuredRepository.getBoundIdEntityBinder(); this.entityMapper = configuredRepository.getEntityMapper(); this.entityAssembler = configuredRepository.getEntityAssembler(); + this.entityPropertyChainMap = configuredRepository.getEntityPropertyChainMap(); } @Override diff --git a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/utils/PathUtils.java b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/utils/PathUtils.java index c57536d71bcdd5abf464fe87d8fbdb5661a2cd83..4b4443ae2d8512830af2cf1507f139cb12dc9232 100644 --- a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/utils/PathUtils.java +++ b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/utils/PathUtils.java @@ -21,11 +21,10 @@ public class PathUtils { } public static String getBelongPath(Set allAccessPath, String accessPath) { - String lastAccessPath = getLastAccessPath(accessPath); - while (!allAccessPath.contains(lastAccessPath) && !"/".equals(lastAccessPath)) { - lastAccessPath = PathUtils.getLastAccessPath(lastAccessPath); + while (!allAccessPath.contains(accessPath) && !"/".equals(accessPath)) { + accessPath = PathUtils.getLastAccessPath(accessPath); } - return lastAccessPath; + return accessPath; } } diff --git a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/utils/ReflectUtils.java b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/utils/ReflectUtils.java index 1497bb4a3e1ea222e1b61b58905284c427a25a01..66abc28d9761d05cc18cf86f5f9a5c4ebaa38230 100644 --- a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/utils/ReflectUtils.java +++ b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/utils/ReflectUtils.java @@ -1,9 +1,12 @@ package com.gitee.spring.domain.core.utils; import java.lang.reflect.Constructor; +import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Collections; +import java.util.LinkedHashSet; import java.util.List; +import java.util.Set; public class ReflectUtils { @@ -11,10 +14,6 @@ public class ReflectUtils { return org.springframework.cglib.core.ReflectUtils.getConstructor(type, parameterTypes); } - public static Object newInstance(Constructor constructor, Object[] args) { - return org.springframework.cglib.core.ReflectUtils.newInstance(constructor, args); - } - public static Object newInstance(Class type) { return org.springframework.cglib.core.ReflectUtils.newInstance(type); } @@ -32,4 +31,12 @@ public class ReflectUtils { return superClasses; } + public static Set getFieldNames(Class type) { + Set fieldNames = new LinkedHashSet<>(); + for (Field field : type.getDeclaredFields()) { + fieldNames.add(field.getName()); + } + return fieldNames; + } + } diff --git a/spring-domain-event/pom.xml b/spring-domain-event/pom.xml index b980175203f89843ec223d173203c1d6503c7cf3..5b2d9b9480377885495027adb783845d9d7f9e85 100644 --- a/spring-domain-event/pom.xml +++ b/spring-domain-event/pom.xml @@ -6,7 +6,7 @@ com.gitee.digital-engine spring-domain - 2.8.1 + 2.8.2 spring-domain-event diff --git a/spring-domain-injection/pom.xml b/spring-domain-injection/pom.xml index 08418407736faea015820ab1823a6801f97e8516..73aa5c51ebffb712af6953e1308a2f447bf663fb 100644 --- a/spring-domain-injection/pom.xml +++ b/spring-domain-injection/pom.xml @@ -6,7 +6,7 @@ com.gitee.digital-engine spring-domain - 2.8.1 + 2.8.2 spring-domain-injection diff --git a/spring-domain-web/pom.xml b/spring-domain-web/pom.xml index 019ff4796fdb87828c1ab458ffdd97a5cb5592c3..9e74ffbcc1813d300706549807bcab42593e13de 100644 --- a/spring-domain-web/pom.xml +++ b/spring-domain-web/pom.xml @@ -6,7 +6,7 @@ com.gitee.digital-engine spring-domain - 2.8.1 + 2.8.2 spring-domain-web