diff --git a/pom.xml b/pom.xml index 4879ff7713c18f3eb84f26dfa9b9917e2fc2697c..eaf5fad2c0789beec326becf7603acd4e5a15468 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 com.gitee.digital-engine spring-domain - 2.5.8 + 2.6.1 pom diff --git a/spring-boot-starter-domain/pom.xml b/spring-boot-starter-domain/pom.xml index 2c12f8fe1989536c20cd40da4481059c638a867e..3530237517afdbf4ec8688a4e0031332645a72f1 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.5.8 + 2.6.1 spring-boot-starter-domain diff --git a/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/api/EntityCriterionBuilder.java b/spring-boot-starter-domain/src/main/java/com/gitee/spring/boot/starter/domain/api/EntityCriterionBuilder.java similarity index 34% rename from spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/api/EntityCriterionBuilder.java rename to spring-boot-starter-domain/src/main/java/com/gitee/spring/boot/starter/domain/api/EntityCriterionBuilder.java index c5d6980fd78636c13f1939fdd2c2ba70a69e8896..ab2e6917b5d19ea22be624aaf5652b120af7ff74 100644 --- a/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/api/EntityCriterionBuilder.java +++ b/spring-boot-starter-domain/src/main/java/com/gitee/spring/boot/starter/domain/api/EntityCriterionBuilder.java @@ -1,10 +1,9 @@ -package com.gitee.spring.domain.coating.api; +package com.gitee.spring.boot.starter.domain.api; import com.gitee.spring.domain.core.api.EntityCriterion; -import com.gitee.spring.domain.core.api.EntityMapper; public interface EntityCriterionBuilder { - EntityCriterion newCriterion(EntityMapper entityMapper, String fieldName, Object fieldValue); + EntityCriterion newCriterion(String fieldName, Object fieldValue); } diff --git a/spring-boot-starter-domain/src/main/java/com/gitee/spring/boot/starter/domain/builder/EQEntityCriterionBuilder.java b/spring-boot-starter-domain/src/main/java/com/gitee/spring/boot/starter/domain/builder/EQEntityCriterionBuilder.java new file mode 100644 index 0000000000000000000000000000000000000000..c25e486f1eeea9e1b655839c600126f5feb183b0 --- /dev/null +++ b/spring-boot-starter-domain/src/main/java/com/gitee/spring/boot/starter/domain/builder/EQEntityCriterionBuilder.java @@ -0,0 +1,31 @@ +package com.gitee.spring.boot.starter.domain.builder; + +import cn.hutool.core.util.StrUtil; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.gitee.spring.boot.starter.domain.api.EntityCriterionBuilder; +import com.gitee.spring.domain.core.api.EntityCriterion; +import com.gitee.spring.domain.core.constants.Operator; +import com.gitee.spring.domain.core.entity.AbstractEntityCriterion; +import com.gitee.spring.domain.core.entity.EntityExample; + +import java.util.Collection; + +public class EQEntityCriterionBuilder implements EntityCriterionBuilder { + + @Override + public EntityCriterion newCriterion(String fieldName, Object fieldValue) { + return new AbstractEntityCriterion(fieldName, Operator.EQ, fieldValue) { + @Override + public void appendTo(EntityExample entityExample) { + QueryWrapper queryWrapper = (QueryWrapper) entityExample.getExample(); + String fieldName = StrUtil.toUnderlineCase(this.fieldName); + if (fieldValue instanceof Collection) { + queryWrapper.in(fieldName, (Collection) fieldValue); + } else { + queryWrapper.eq(fieldName, fieldValue); + } + } + }; + } + +} diff --git a/spring-boot-starter-domain/src/main/java/com/gitee/spring/boot/starter/domain/builder/GEEntityCriterionBuilder.java b/spring-boot-starter-domain/src/main/java/com/gitee/spring/boot/starter/domain/builder/GEEntityCriterionBuilder.java new file mode 100644 index 0000000000000000000000000000000000000000..1e276183277961f8d6397d1a63f29901705bb3e9 --- /dev/null +++ b/spring-boot-starter-domain/src/main/java/com/gitee/spring/boot/starter/domain/builder/GEEntityCriterionBuilder.java @@ -0,0 +1,25 @@ +package com.gitee.spring.boot.starter.domain.builder; + +import cn.hutool.core.util.StrUtil; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.gitee.spring.boot.starter.domain.api.EntityCriterionBuilder; +import com.gitee.spring.domain.core.api.EntityCriterion; +import com.gitee.spring.domain.core.constants.Operator; +import com.gitee.spring.domain.core.entity.AbstractEntityCriterion; +import com.gitee.spring.domain.core.entity.EntityExample; + +public class GEEntityCriterionBuilder implements EntityCriterionBuilder { + + @Override + public EntityCriterion newCriterion(String fieldName, Object fieldValue) { + return new AbstractEntityCriterion(fieldName, Operator.GE, fieldValue) { + @Override + public void appendTo(EntityExample entityExample) { + QueryWrapper queryWrapper = (QueryWrapper) entityExample.getExample(); + String fieldName = StrUtil.toUnderlineCase(this.fieldName); + queryWrapper.ge(fieldName, fieldValue); + } + }; + } + +} diff --git a/spring-boot-starter-domain/src/main/java/com/gitee/spring/boot/starter/domain/builder/GTEntityCriterionBuilder.java b/spring-boot-starter-domain/src/main/java/com/gitee/spring/boot/starter/domain/builder/GTEntityCriterionBuilder.java new file mode 100644 index 0000000000000000000000000000000000000000..9dd568c99b0550cd36c9b3cdfbea1483a0a7c352 --- /dev/null +++ b/spring-boot-starter-domain/src/main/java/com/gitee/spring/boot/starter/domain/builder/GTEntityCriterionBuilder.java @@ -0,0 +1,25 @@ +package com.gitee.spring.boot.starter.domain.builder; + +import cn.hutool.core.util.StrUtil; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.gitee.spring.boot.starter.domain.api.EntityCriterionBuilder; +import com.gitee.spring.domain.core.api.EntityCriterion; +import com.gitee.spring.domain.core.constants.Operator; +import com.gitee.spring.domain.core.entity.AbstractEntityCriterion; +import com.gitee.spring.domain.core.entity.EntityExample; + +public class GTEntityCriterionBuilder implements EntityCriterionBuilder { + + @Override + public EntityCriterion newCriterion(String fieldName, Object fieldValue) { + return new AbstractEntityCriterion(fieldName, Operator.GT, fieldValue) { + @Override + public void appendTo(EntityExample entityExample) { + QueryWrapper queryWrapper = (QueryWrapper) entityExample.getExample(); + String fieldName = StrUtil.toUnderlineCase(this.fieldName); + queryWrapper.gt(fieldName, fieldValue); + } + }; + } + +} diff --git a/spring-boot-starter-domain/src/main/java/com/gitee/spring/boot/starter/domain/builder/LEEntityCriterionBuilder.java b/spring-boot-starter-domain/src/main/java/com/gitee/spring/boot/starter/domain/builder/LEEntityCriterionBuilder.java new file mode 100644 index 0000000000000000000000000000000000000000..39edf59dcb3629626811f6500f6c2c1fc3c06ba2 --- /dev/null +++ b/spring-boot-starter-domain/src/main/java/com/gitee/spring/boot/starter/domain/builder/LEEntityCriterionBuilder.java @@ -0,0 +1,25 @@ +package com.gitee.spring.boot.starter.domain.builder; + +import cn.hutool.core.util.StrUtil; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.gitee.spring.boot.starter.domain.api.EntityCriterionBuilder; +import com.gitee.spring.domain.core.api.EntityCriterion; +import com.gitee.spring.domain.core.constants.Operator; +import com.gitee.spring.domain.core.entity.AbstractEntityCriterion; +import com.gitee.spring.domain.core.entity.EntityExample; + +public class LEEntityCriterionBuilder implements EntityCriterionBuilder { + + @Override + public EntityCriterion newCriterion(String fieldName, Object fieldValue) { + return new AbstractEntityCriterion(fieldName, Operator.LE, fieldValue) { + @Override + public void appendTo(EntityExample entityExample) { + QueryWrapper queryWrapper = (QueryWrapper) entityExample.getExample(); + String fieldName = StrUtil.toUnderlineCase(this.fieldName); + queryWrapper.le(fieldName, fieldValue); + } + }; + } + +} diff --git a/spring-boot-starter-domain/src/main/java/com/gitee/spring/boot/starter/domain/builder/LTEntityCriterionBuilder.java b/spring-boot-starter-domain/src/main/java/com/gitee/spring/boot/starter/domain/builder/LTEntityCriterionBuilder.java new file mode 100644 index 0000000000000000000000000000000000000000..eade336c8275da6e5fabde819c7eb6db8871e426 --- /dev/null +++ b/spring-boot-starter-domain/src/main/java/com/gitee/spring/boot/starter/domain/builder/LTEntityCriterionBuilder.java @@ -0,0 +1,25 @@ +package com.gitee.spring.boot.starter.domain.builder; + +import cn.hutool.core.util.StrUtil; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.gitee.spring.boot.starter.domain.api.EntityCriterionBuilder; +import com.gitee.spring.domain.core.api.EntityCriterion; +import com.gitee.spring.domain.core.constants.Operator; +import com.gitee.spring.domain.core.entity.AbstractEntityCriterion; +import com.gitee.spring.domain.core.entity.EntityExample; + +public class LTEntityCriterionBuilder implements EntityCriterionBuilder { + + @Override + public EntityCriterion newCriterion(String fieldName, Object fieldValue) { + return new AbstractEntityCriterion(fieldName, Operator.LT, fieldValue) { + @Override + public void appendTo(EntityExample entityExample) { + QueryWrapper queryWrapper = (QueryWrapper) entityExample.getExample(); + String fieldName = StrUtil.toUnderlineCase(this.fieldName); + queryWrapper.lt(fieldName, fieldValue); + } + }; + } + +} diff --git a/spring-boot-starter-domain/src/main/java/com/gitee/spring/boot/starter/domain/repository/MybatisPlusEntityMapper.java b/spring-boot-starter-domain/src/main/java/com/gitee/spring/boot/starter/domain/repository/MybatisPlusEntityMapper.java index cc38eb01afc8395300d30fa1cd49c842e0eb2f91..46212caa62355f7ace95e5bdcde20b1bd4da588f 100644 --- a/spring-boot-starter-domain/src/main/java/com/gitee/spring/boot/starter/domain/repository/MybatisPlusEntityMapper.java +++ b/spring-boot-starter-domain/src/main/java/com/gitee/spring/boot/starter/domain/repository/MybatisPlusEntityMapper.java @@ -1,29 +1,42 @@ package com.gitee.spring.boot.starter.domain.repository; -import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.gitee.spring.boot.starter.domain.api.EntityCriterionBuilder; +import com.gitee.spring.boot.starter.domain.builder.*; import com.gitee.spring.domain.core.api.EntityCriterion; import com.gitee.spring.domain.core.api.EntityMapper; -import com.gitee.spring.domain.core.entity.AbstractEntityCriterion; +import com.gitee.spring.domain.core.constants.Operator; 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 java.util.Collection; import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; public class MybatisPlusEntityMapper implements EntityMapper { + public static Map operatorEntityCriterionBuilderMap = new ConcurrentHashMap<>(); + + static { + operatorEntityCriterionBuilderMap.put(Operator.EQ, new EQEntityCriterionBuilder()); + operatorEntityCriterionBuilderMap.put(Operator.GT, new GTEntityCriterionBuilder()); + operatorEntityCriterionBuilderMap.put(Operator.GE, new GEEntityCriterionBuilder()); + operatorEntityCriterionBuilderMap.put(Operator.LT, new LTEntityCriterionBuilder()); + operatorEntityCriterionBuilderMap.put(Operator.LE, new LEEntityCriterionBuilder()); + } + @Override public Object newPage(Integer pageNum, Integer pageSize) { return new Page<>(pageNum, pageSize); } @Override - public List getDataFromPage(Object dataPage) { - return ((IPage) dataPage).getRecords(); + @SuppressWarnings("unchecked") + public List getDataFromPage(Object dataPage) { + return ((IPage) dataPage).getRecords(); } @Override @@ -35,7 +48,7 @@ public class MybatisPlusEntityMapper implements EntityMapper { } @Override - public EntityExample newExample(EntityDefinition entityDefinition, BoundedContext boundedContext) { + public EntityExample newExample(BoundedContext boundedContext, EntityDefinition entityDefinition) { EntityExample entityExample = new EntityExample(new QueryWrapper<>()) { @Override public EntityExample selectColumns() { @@ -66,67 +79,9 @@ public class MybatisPlusEntityMapper implements EntityMapper { } @Override - public EntityCriterion newEqualCriterion(String fieldName, Object fieldValue) { - return new AbstractEntityCriterion(fieldName, fieldValue) { - @Override - public void appendTo(EntityExample entityExample) { - QueryWrapper queryWrapper = (QueryWrapper) entityExample.getExample(); - String fieldName = StrUtil.toUnderlineCase(this.fieldName); - if (fieldValue instanceof Collection) { - queryWrapper.in(fieldName, (Collection) fieldValue); - } else { - queryWrapper.eq(fieldName, fieldValue); - } - } - }; - } - - @Override - public EntityCriterion newGreaterThanCriterion(String fieldName, Object fieldValue) { - return new AbstractEntityCriterion(fieldName, fieldValue) { - @Override - public void appendTo(EntityExample entityExample) { - QueryWrapper queryWrapper = (QueryWrapper) entityExample.getExample(); - String fieldName = StrUtil.toUnderlineCase(this.fieldName); - queryWrapper.gt(fieldName, fieldValue); - } - }; - } - - @Override - public EntityCriterion newGreaterThanOrEqualCriterion(String fieldName, Object fieldValue) { - return new AbstractEntityCriterion(fieldName, fieldValue) { - @Override - public void appendTo(EntityExample entityExample) { - QueryWrapper queryWrapper = (QueryWrapper) entityExample.getExample(); - String fieldName = StrUtil.toUnderlineCase(this.fieldName); - queryWrapper.ge(fieldName, fieldValue); - } - }; - } - - @Override - public EntityCriterion newLessThanCriterion(String fieldName, Object fieldValue) { - return new AbstractEntityCriterion(fieldName, fieldValue) { - @Override - public void appendTo(EntityExample entityExample) { - QueryWrapper queryWrapper = (QueryWrapper) entityExample.getExample(); - String fieldName = StrUtil.toUnderlineCase(this.fieldName); - queryWrapper.lt(fieldName, fieldValue); - } - }; - } - - @Override - public EntityCriterion newLessThanOrEqualCriterion(String fieldName, Object fieldValue) { - return new AbstractEntityCriterion(fieldName, fieldValue) { - @Override - public void appendTo(EntityExample entityExample) { - QueryWrapper queryWrapper = (QueryWrapper) entityExample.getExample(); - String fieldName = StrUtil.toUnderlineCase(this.fieldName); - queryWrapper.le(fieldName, fieldValue); - } - }; + public EntityCriterion newCriterion(String fieldName, String operator, Object fieldValue) { + EntityCriterionBuilder entityCriterionBuilder = operatorEntityCriterionBuilderMap.get(operator); + return entityCriterionBuilder.newCriterion(fieldName, fieldValue); } } diff --git a/spring-domain-coating/pom.xml b/spring-domain-coating/pom.xml index 38035c3f6832e1b84125de209b065745b1c1df9f..8524391eb4c5e77bc45348a8d7953bc2b1589faf 100644 --- a/spring-domain-coating/pom.xml +++ b/spring-domain-coating/pom.xml @@ -6,7 +6,7 @@ com.gitee.digital-engine spring-domain - 2.5.8 + 2.6.1 spring-domain-coating diff --git a/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/api/CoatingAssembler.java b/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/api/CoatingAssembler.java index d0878152b7237b54d3f50592a710ea47239a640d..5dc35ac229e6e711c7a562e6110c8ef92857217a 100644 --- a/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/api/CoatingAssembler.java +++ b/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/api/CoatingAssembler.java @@ -2,8 +2,8 @@ package com.gitee.spring.domain.coating.api; public interface CoatingAssembler { - void assemble(Object coating, Object entity); + void assemble(Object coatingObject, Object entity); - void disassemble(Object coating, Object entity); + void disassemble(Object coatingObject, Object entity); } diff --git a/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/builder/EqualEntityCriterionBuilder.java b/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/builder/EqualEntityCriterionBuilder.java deleted file mode 100644 index df9d307e4898ac8ae6a44cd1521936cea0c780a6..0000000000000000000000000000000000000000 --- a/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/builder/EqualEntityCriterionBuilder.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.gitee.spring.domain.coating.builder; - -import com.gitee.spring.domain.coating.api.EntityCriterionBuilder; -import com.gitee.spring.domain.core.api.EntityCriterion; -import com.gitee.spring.domain.core.api.EntityMapper; - -public class EqualEntityCriterionBuilder implements EntityCriterionBuilder { - - @Override - public EntityCriterion newCriterion(EntityMapper entityMapper, String fieldName, Object fieldValue) { - return entityMapper.newEqualCriterion(fieldName, fieldValue); - } - -} diff --git a/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/builder/GreaterThanEntityCriterionBuilder.java b/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/builder/GreaterThanEntityCriterionBuilder.java deleted file mode 100644 index be3866b43d849356c3997be88755a7724aeed105..0000000000000000000000000000000000000000 --- a/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/builder/GreaterThanEntityCriterionBuilder.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.gitee.spring.domain.coating.builder; - -import com.gitee.spring.domain.coating.api.EntityCriterionBuilder; -import com.gitee.spring.domain.core.api.EntityCriterion; -import com.gitee.spring.domain.core.api.EntityMapper; - -public class GreaterThanEntityCriterionBuilder implements EntityCriterionBuilder { - - @Override - public EntityCriterion newCriterion(EntityMapper entityMapper, String fieldName, Object fieldValue) { - return entityMapper.newGreaterThanCriterion(fieldName, fieldValue); - } - -} diff --git a/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/builder/GreaterThanOrEqualEntityCriterionBuilder.java b/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/builder/GreaterThanOrEqualEntityCriterionBuilder.java deleted file mode 100644 index 0c0f1204294e4479cb25f6b097df060ea8bea044..0000000000000000000000000000000000000000 --- a/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/builder/GreaterThanOrEqualEntityCriterionBuilder.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.gitee.spring.domain.coating.builder; - -import com.gitee.spring.domain.coating.api.EntityCriterionBuilder; -import com.gitee.spring.domain.core.api.EntityCriterion; -import com.gitee.spring.domain.core.api.EntityMapper; - -public class GreaterThanOrEqualEntityCriterionBuilder implements EntityCriterionBuilder { - - @Override - public EntityCriterion newCriterion(EntityMapper entityMapper, String fieldName, Object fieldValue) { - return entityMapper.newGreaterThanOrEqualCriterion(fieldName, fieldValue); - } - -} diff --git a/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/builder/LessThanEntityCriterionBuilder.java b/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/builder/LessThanEntityCriterionBuilder.java deleted file mode 100644 index e94d01befa30cd531faf1ea4b54ff55e211093ab..0000000000000000000000000000000000000000 --- a/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/builder/LessThanEntityCriterionBuilder.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.gitee.spring.domain.coating.builder; - -import com.gitee.spring.domain.coating.api.EntityCriterionBuilder; -import com.gitee.spring.domain.core.api.EntityCriterion; -import com.gitee.spring.domain.core.api.EntityMapper; - -public class LessThanEntityCriterionBuilder implements EntityCriterionBuilder { - - @Override - public EntityCriterion newCriterion(EntityMapper entityMapper, String fieldName, Object fieldValue) { - return entityMapper.newLessThanCriterion(fieldName, fieldValue); - } - -} diff --git a/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/builder/LessThanOrEqualEntityCriterionBuilder.java b/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/builder/LessThanOrEqualEntityCriterionBuilder.java deleted file mode 100644 index 164c388e5423b8f92bed493b68122a19b750a662..0000000000000000000000000000000000000000 --- a/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/builder/LessThanOrEqualEntityCriterionBuilder.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.gitee.spring.domain.coating.builder; - -import com.gitee.spring.domain.coating.api.EntityCriterionBuilder; -import com.gitee.spring.domain.core.api.EntityCriterion; -import com.gitee.spring.domain.core.api.EntityMapper; - -public class LessThanOrEqualEntityCriterionBuilder implements EntityCriterionBuilder { - - @Override - public EntityCriterion newCriterion(EntityMapper entityMapper, String fieldName, Object fieldValue) { - return entityMapper.newLessThanOrEqualCriterion(fieldName, fieldValue); - } - -} diff --git a/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/config/DomainCoatingConfiguration.java b/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/config/DomainCoatingConfiguration.java deleted file mode 100644 index ada4aeb1ad82e152b77841f1103ec3415d071de9..0000000000000000000000000000000000000000 --- a/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/config/DomainCoatingConfiguration.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.gitee.spring.domain.coating.config; - -import com.gitee.spring.domain.coating.builder.*; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.core.annotation.Order; - -@Order(-100) -@Configuration -public class DomainCoatingConfiguration { - - @Bean - public EqualEntityCriterionBuilder equalCriterionBuilder() { - return new EqualEntityCriterionBuilder(); - } - - @Bean - public GreaterThanEntityCriterionBuilder greaterThanCriterionBuilder() { - return new GreaterThanEntityCriterionBuilder(); - } - - @Bean - public GreaterThanOrEqualEntityCriterionBuilder greaterThanOrEqualCriterionBuilder() { - return new GreaterThanOrEqualEntityCriterionBuilder(); - } - - @Bean - public LessThanEntityCriterionBuilder lessThanCriterionBuilder() { - return new LessThanEntityCriterionBuilder(); - } - - @Bean - public LessThanOrEqualEntityCriterionBuilder lessThanOrEqualCriterionBuilder() { - return new LessThanOrEqualEntityCriterionBuilder(); - } - -} diff --git a/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/entity/ChainCriterion.java b/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/entity/ChainCriterion.java index 66b131f2eeb4c89b732c8bce0da21bd2be7e6457..467869d225f12ebcd79577380cc9a020343c52ad 100644 --- a/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/entity/ChainCriterion.java +++ b/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/entity/ChainCriterion.java @@ -1,15 +1,12 @@ package com.gitee.spring.domain.coating.entity; import com.gitee.spring.domain.core.entity.EntityExample; -import com.gitee.spring.domain.core.repository.ConfiguredRepository; import lombok.AllArgsConstructor; import lombok.Data; @Data @AllArgsConstructor public class ChainCriterion { - private String definitionAccessPath; - private ConfiguredRepository definitionRepository; - private ConfiguredRepository queryRepository; + private RepositoryLocation repositoryLocation; private EntityExample entityExample; } diff --git a/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/entity/PropertyDefinition.java b/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/entity/PropertyDefinition.java index 7870f36aa7a6cdb60f7d98f6bf60d17f4ff8f3c8..3f2f95293325ecd3780d45c36b396ac52d1f1fee 100644 --- a/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/entity/PropertyDefinition.java +++ b/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/entity/PropertyDefinition.java @@ -1,6 +1,5 @@ package com.gitee.spring.domain.coating.entity; -import com.gitee.spring.domain.coating.api.EntityCriterionBuilder; import com.gitee.spring.domain.core.entity.EntityPropertyChain; import lombok.AllArgsConstructor; import lombok.Data; @@ -21,6 +20,5 @@ public class PropertyDefinition { private String aliasAttribute; private String operatorAttribute; private boolean boundLocation; - private EntityCriterionBuilder entityCriterionBuilder; private EntityPropertyChain entityPropertyChain; } diff --git a/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/entity/RepositoryDefinition.java b/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/entity/RepositoryDefinition.java new file mode 100644 index 0000000000000000000000000000000000000000..ee03cdb88d74bc088a211ece89308ff3cb044f3e --- /dev/null +++ b/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/entity/RepositoryDefinition.java @@ -0,0 +1,15 @@ +package com.gitee.spring.domain.coating.entity; + +import com.gitee.spring.domain.core.repository.ConfiguredRepository; +import lombok.AllArgsConstructor; +import lombok.Data; + +@Data +@AllArgsConstructor +public class RepositoryDefinition { + private String prefixAccessPath; + private String absoluteAccessPath; + private boolean aggregateRoot; + private ConfiguredRepository definitionRepository; + private ConfiguredRepository configuredRepository; +} diff --git a/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/entity/RepositoryGroup.java b/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/entity/RepositoryGroup.java new file mode 100644 index 0000000000000000000000000000000000000000..49420967b5cc887104eef4893f2df84133794c62 --- /dev/null +++ b/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/entity/RepositoryGroup.java @@ -0,0 +1,15 @@ +package com.gitee.spring.domain.coating.entity; + +import com.gitee.spring.domain.coating.repository.AbstractAwareRepository; +import lombok.AllArgsConstructor; +import lombok.Data; + +import java.util.List; + +@Data +@AllArgsConstructor +public class RepositoryGroup { + private String accessPath; + private AbstractAwareRepository abstractAwareRepository; + private List repositoryDefinitions; +} diff --git a/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/entity/RepositoryLocation.java b/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/entity/RepositoryLocation.java index 189ab97676783a24312d711801edbf71214dbd24..5708bc80f4033ef320fb77085df21987003c3dcf 100644 --- a/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/entity/RepositoryLocation.java +++ b/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/entity/RepositoryLocation.java @@ -1,7 +1,5 @@ package com.gitee.spring.domain.coating.entity; -import com.gitee.spring.domain.core.repository.AbstractDelegateRepository; -import com.gitee.spring.domain.core.repository.ConfiguredRepository; import lombok.AllArgsConstructor; import lombok.Data; @@ -10,13 +8,6 @@ import java.util.List; @Data @AllArgsConstructor public class RepositoryLocation { - private List multiAccessPath; - private String parentAccessPath; - private String prefixAccessPath; - private String absoluteAccessPath; - private boolean forwardParent; - private ConfiguredRepository parentConfiguredRepository; - private AbstractDelegateRepository abstractDelegateRepository; - private ConfiguredRepository belongConfiguredRepository; + private RepositoryDefinition repositoryDefinition; private List collectedPropertyDefinitions; } diff --git a/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/impl/DefaultCoatingAssembler.java b/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/impl/DefaultCoatingAssembler.java index ad447ac6f22f68b09f3f4dd6fe08c62e6a397727..d13808dbb9d3782854ee257a3a96d2580bfc644d 100644 --- a/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/impl/DefaultCoatingAssembler.java +++ b/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/impl/DefaultCoatingAssembler.java @@ -17,23 +17,22 @@ public class DefaultCoatingAssembler implements CoatingAssembler { private CoatingDefinition coatingDefinition; private List availablePropertyDefinitions; - private List repositoryLocations; private List reversedRepositoryLocations; @Override - public void assemble(Object coating, Object entity) { + public void assemble(Object coatingObject, Object entity) { for (PropertyDefinition propertyDefinition : availablePropertyDefinitions) { EntityPropertyChain entityPropertyChain = propertyDefinition.getEntityPropertyChain(); Object targetValue = entityPropertyChain.getValue(entity); - ReflectUtil.setFieldValue(coating, propertyDefinition.getDeclaredField(), targetValue); + ReflectUtil.setFieldValue(coatingObject, propertyDefinition.getDeclaredField(), targetValue); } } @Override - public void disassemble(Object coating, Object entity) { + public void disassemble(Object coatingObject, Object entity) { for (PropertyDefinition propertyDefinition : availablePropertyDefinitions) { EntityPropertyChain entityPropertyChain = propertyDefinition.getEntityPropertyChain(); - Object fieldValue = ReflectUtil.getFieldValue(coating, propertyDefinition.getDeclaredField()); + Object fieldValue = ReflectUtil.getFieldValue(coatingObject, propertyDefinition.getDeclaredField()); entityPropertyChain.setValue(entity, fieldValue); } } diff --git a/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/repository/AbstractAwareRepository.java b/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/repository/AbstractAwareRepository.java new file mode 100644 index 0000000000000000000000000000000000000000..4f5915e150c5819143b945913f3a2358b0e91ba6 --- /dev/null +++ b/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/repository/AbstractAwareRepository.java @@ -0,0 +1,73 @@ +package com.gitee.spring.domain.coating.repository; + +import cn.hutool.core.util.StrUtil; +import com.gitee.spring.domain.core.entity.EntityDefinition; +import com.gitee.spring.domain.coating.entity.RepositoryGroup; +import com.gitee.spring.domain.coating.entity.RepositoryDefinition; +import com.gitee.spring.domain.core.repository.AbstractRepository; +import com.gitee.spring.domain.core.repository.ConfiguredRepository; +import com.gitee.spring.domain.event.repository.AbstractEventRepository; +import lombok.Data; +import lombok.EqualsAndHashCode; +import org.apache.commons.lang3.StringUtils; + +import java.util.*; + +@Data +@EqualsAndHashCode(callSuper = false) +public abstract class AbstractAwareRepository extends AbstractEventRepository { + + protected Map repositoryGroupMap = new LinkedHashMap<>(); + protected List repositoryGroups = new ArrayList<>(); + protected List reversedRepositoryGroups = new ArrayList<>(); + + @Override + public void afterPropertiesSet() throws Exception { + super.afterPropertiesSet(); + resolveRepositoryGroups(new ArrayList<>(), this); + repositoryGroups.addAll(repositoryGroupMap.values()); + reversedRepositoryGroups.addAll(repositoryGroupMap.values()); + Collections.reverse(reversedRepositoryGroups); + } + + protected void resolveRepositoryGroups(List multiAccessPath, AbstractAwareRepository abstractAwareRepository) { + String prefixAccessPath = StrUtil.join("", multiAccessPath); + + String groupAccessPath = StringUtils.isNotBlank(prefixAccessPath) ? prefixAccessPath : "/"; + RepositoryGroup repositoryGroup = new RepositoryGroup(groupAccessPath, abstractAwareRepository, new ArrayList<>()); + repositoryGroupMap.put(groupAccessPath, repositoryGroup); + List repositoryDefinitions = repositoryGroup.getRepositoryDefinitions(); + + for (ConfiguredRepository configuredRepository : abstractAwareRepository.getSubRepositories()) { + EntityDefinition entityDefinition = configuredRepository.getEntityDefinition(); + String absoluteAccessPath = prefixAccessPath + entityDefinition.getAccessPath(); + + AbstractRepository abstractRepository = configuredRepository.getRepository(); + if (abstractRepository instanceof AbstractAwareRepository) { + AbstractAwareRepository repository = (AbstractAwareRepository) abstractRepository; + + RepositoryDefinition repositoryDefinition = new RepositoryDefinition( + prefixAccessPath, + absoluteAccessPath, + true, + configuredRepository, + repository.getRootRepository()); + repositoryDefinitions.add(repositoryDefinition); + + multiAccessPath = new ArrayList<>(multiAccessPath); + multiAccessPath.add(entityDefinition.getAccessPath()); + resolveRepositoryGroups(multiAccessPath, repository); + + } else { + RepositoryDefinition repositoryDefinition = new RepositoryDefinition( + prefixAccessPath, + absoluteAccessPath, + false, + configuredRepository, + configuredRepository); + repositoryDefinitions.add(repositoryDefinition); + } + } + } + +} 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 e4c3752b2267fadd52f37084bb72681eaf40ffca..ca131c52033ed102c1f514e1cd0c81a7ca8eeedc 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 @@ -3,13 +3,14 @@ package com.gitee.spring.domain.coating.repository; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.lang.Assert; import cn.hutool.core.util.ReflectUtil; -import com.gitee.spring.domain.coating.api.EntityCriterionBuilder; import com.gitee.spring.domain.coating.entity.ChainCriterion; import com.gitee.spring.domain.coating.entity.PropertyDefinition; +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.EntityCriterion; import com.gitee.spring.domain.core.api.EntityMapper; +import com.gitee.spring.domain.core.constants.Operator; import com.gitee.spring.domain.core.entity.*; import com.gitee.spring.domain.core.repository.ConfiguredRepository; import lombok.extern.slf4j.Slf4j; @@ -19,19 +20,19 @@ import java.util.*; @Slf4j public abstract class AbstractChainRepository extends AbstractCoatingRepository { - public EntityExample buildExample(BoundedContext boundedContext, Object coating) { - DefaultCoatingAssembler defaultCoatingAssembler = (DefaultCoatingAssembler) classCoatingAssemblerMap.get(coating.getClass()); + public EntityExample buildExample(BoundedContext boundedContext, Object coatingObject) { + DefaultCoatingAssembler defaultCoatingAssembler = (DefaultCoatingAssembler) classCoatingAssemblerMap.get(coatingObject.getClass()); Assert.notNull(defaultCoatingAssembler, "No coating assembler exists!"); Map criterionMap = new LinkedHashMap<>(); for (RepositoryLocation repositoryLocation : defaultCoatingAssembler.getReversedRepositoryLocations()) { - String absoluteAccessPath = repositoryLocation.getAbsoluteAccessPath(); - if (!criterionMap.containsKey(absoluteAccessPath)) { - ChainCriterion chainCriterion = buildCriterion(boundedContext, repositoryLocation); - criterionMap.put(absoluteAccessPath, chainCriterion); - } - ChainCriterion chainCriterion = criterionMap.get(absoluteAccessPath); - addToExampleOfCriterion(repositoryLocation, coating, chainCriterion); + ChainCriterion chainCriterion = buildCriterion(boundedContext, repositoryLocation); + addToExampleOfCriterion(chainCriterion, coatingObject); + + RepositoryDefinition repositoryDefinition = repositoryLocation.getRepositoryDefinition(); + String absoluteAccessPath = repositoryDefinition.getAbsoluteAccessPath(); + absoluteAccessPath = repositoryDefinition.isAggregateRoot() ? absoluteAccessPath + "/" : absoluteAccessPath; + criterionMap.put(absoluteAccessPath, chainCriterion); } executeChainQuery(boundedContext, criterionMap); @@ -42,31 +43,26 @@ public abstract class AbstractChainRepository extends AbstractCoatingRepo } protected ChainCriterion buildCriterion(BoundedContext boundedContext, RepositoryLocation repositoryLocation) { - String definitionAccessPath = repositoryLocation.getPrefixAccessPath(); - ConfiguredRepository definitionRepository = repositoryLocation.getBelongConfiguredRepository(); - - if (repositoryLocation.isForwardParent()) { - definitionAccessPath = repositoryLocation.getParentAccessPath(); - definitionRepository = repositoryLocation.getParentConfiguredRepository(); - } - - ConfiguredRepository queryRepository = repositoryLocation.getBelongConfiguredRepository(); - EntityDefinition entityDefinition = queryRepository.getEntityDefinition(); - EntityMapper entityMapper = queryRepository.getEntityMapper(); - EntityExample entityExample = entityMapper.newExample(entityDefinition, boundedContext); - - return new ChainCriterion(definitionAccessPath, definitionRepository, queryRepository, entityExample); + RepositoryDefinition repositoryDefinition = repositoryLocation.getRepositoryDefinition(); + ConfiguredRepository configuredRepository = repositoryDefinition.getConfiguredRepository(); + EntityDefinition entityDefinition = configuredRepository.getEntityDefinition(); + EntityMapper entityMapper = configuredRepository.getEntityMapper(); + EntityExample entityExample = entityMapper.newExample(boundedContext, entityDefinition); + return new ChainCriterion(repositoryLocation, entityExample); } - protected void addToExampleOfCriterion(RepositoryLocation repositoryLocation, Object coating, ChainCriterion chainCriterion) { + protected void addToExampleOfCriterion(ChainCriterion chainCriterion, Object coatingObject) { + RepositoryLocation repositoryLocation = chainCriterion.getRepositoryLocation(); EntityExample entityExample = chainCriterion.getEntityExample(); for (PropertyDefinition propertyDefinition : repositoryLocation.getCollectedPropertyDefinitions()) { - Object fieldValue = ReflectUtil.getFieldValue(coating, propertyDefinition.getDeclaredField()); + String aliasAttribute = propertyDefinition.getAliasAttribute(); + String operatorAttribute = propertyDefinition.getOperatorAttribute(); + Object fieldValue = ReflectUtil.getFieldValue(coatingObject, propertyDefinition.getDeclaredField()); if (fieldValue != null) { - EntityCriterionBuilder entityCriterionBuilder = propertyDefinition.getEntityCriterionBuilder(); - ConfiguredRepository queryRepository = chainCriterion.getQueryRepository(); - EntityMapper entityMapper = queryRepository.getEntityMapper(); - EntityCriterion entityCriterion = entityCriterionBuilder.newCriterion(entityMapper, propertyDefinition.getAliasAttribute(), fieldValue); + RepositoryDefinition repositoryDefinition = repositoryLocation.getRepositoryDefinition(); + ConfiguredRepository configuredRepository = repositoryDefinition.getConfiguredRepository(); + EntityMapper entityMapper = configuredRepository.getEntityMapper(); + EntityCriterion entityCriterion = entityMapper.newCriterion(aliasAttribute, operatorAttribute, fieldValue); entityExample.addCriterion(entityCriterion); } } @@ -74,33 +70,37 @@ public abstract class AbstractChainRepository extends AbstractCoatingRepo protected void executeChainQuery(BoundedContext boundedContext, Map criterionMap) { criterionMap.forEach((accessPath, chainCriterion) -> { - if ("/".equals(accessPath)) return; - - String definitionAccessPath = chainCriterion.getDefinitionAccessPath(); - ConfiguredRepository definitionRepository = chainCriterion.getDefinitionRepository(); - ConfiguredRepository queryRepository = chainCriterion.getQueryRepository(); + RepositoryLocation repositoryLocation = chainCriterion.getRepositoryLocation(); EntityExample entityExample = chainCriterion.getEntityExample(); + RepositoryDefinition repositoryDefinition = repositoryLocation.getRepositoryDefinition(); + String prefixAccessPath = repositoryDefinition.getPrefixAccessPath(); + ConfiguredRepository definitionRepository = repositoryDefinition.getDefinitionRepository(); + ConfiguredRepository configuredRepository = repositoryDefinition.getConfiguredRepository(); + EntityDefinition entityDefinition = definitionRepository.getEntityDefinition(); - EntityMapper entityMapper = queryRepository.getEntityMapper(); + EntityMapper entityMapper = configuredRepository.getEntityMapper(); + + for (BindingDefinition bindingDefinition : entityDefinition.getBoundBindingDefinitions()) { + String absoluteAccessPath = prefixAccessPath + bindingDefinition.getBelongAccessPath(); + ChainCriterion targetChainCriterion = criterionMap.get(absoluteAccessPath); + if (targetChainCriterion != null) { + EntityExample targetEntityExample = targetChainCriterion.getEntityExample(); + if (targetEntityExample.isEmptyQuery()) { + entityExample.setEmptyQuery(true); + break; + } + } + } - for (BindingDefinition bindingDefinition : entityDefinition.getBindingDefinitions()) { - if (bindingDefinition.isFromContext()) { + if (!entityExample.isEmptyQuery()) { + for (BindingDefinition bindingDefinition : entityDefinition.getContextBindingDefinitions()) { Object boundValue = boundedContext.get(bindingDefinition.getBindAttribute()); if (boundValue != null) { - EntityCriterion entityCriterion = entityMapper.newEqualCriterion(bindingDefinition.getFieldAliasAttribute(), boundValue); + String aliasAttribute = bindingDefinition.getAliasAttribute(); + EntityCriterion entityCriterion = entityMapper.newCriterion(aliasAttribute, Operator.EQ, boundValue); entityExample.addCriterion(entityCriterion); } - } else { - String absoluteAccessPath = definitionAccessPath + bindingDefinition.getBelongAccessPath(); - ChainCriterion targetChainCriterion = criterionMap.get(absoluteAccessPath); - if (targetChainCriterion != null) { - EntityExample targetEntityExample = targetChainCriterion.getEntityExample(); - if (targetEntityExample.isEmptyQuery()) { - entityExample.setEmptyQuery(true); - break; - } - } } } @@ -110,37 +110,36 @@ public abstract class AbstractChainRepository extends AbstractCoatingRepo List entities = Collections.emptyList(); if (!entityExample.isEmptyQuery() && entityExample.isDirtyQuery()) { - entityExample.setColumns(entityDefinition.getBindingColumns()); - entities = queryRepository.selectByExample(boundedContext, entityExample.buildExample()); + entities = configuredRepository.selectByExample(boundedContext, entityExample); log.debug("The data queried is: {}", entities); } - for (BindingDefinition bindingDefinition : entityDefinition.getBindingDefinitions()) { - if (!bindingDefinition.isFromContext()) { - String absoluteAccessPath = definitionAccessPath + bindingDefinition.getBelongAccessPath(); - ChainCriterion targetChainCriterion = criterionMap.get(absoluteAccessPath); - if (targetChainCriterion != null) { - EntityExample targetEntityExample = targetChainCriterion.getEntityExample(); - if (entities.isEmpty()) { - targetEntityExample.setEmptyQuery(true); - continue; - } - - List fieldValues = collectFieldValues(entities, bindingDefinition.getFieldAttribute()); - if (fieldValues.isEmpty()) { - targetEntityExample.setEmptyQuery(true); - continue; - } - - String boundFieldName = bindingDefinition.getBoundFieldName(); - Object fieldValue = fieldValues.size() == 1 ? fieldValues.get(0) : fieldValues; - - ConfiguredRepository targetQueryRepository = targetChainCriterion.getQueryRepository(); - EntityMapper targetEntityMapper = targetQueryRepository.getEntityMapper(); - - EntityCriterion entityCriterion = targetEntityMapper.newEqualCriterion(boundFieldName, fieldValue); - targetEntityExample.addCriterion(entityCriterion); + for (BindingDefinition bindingDefinition : entityDefinition.getBoundBindingDefinitions()) { + String absoluteAccessPath = prefixAccessPath + bindingDefinition.getBelongAccessPath(); + ChainCriterion targetChainCriterion = criterionMap.get(absoluteAccessPath); + if (targetChainCriterion != null) { + EntityExample targetEntityExample = targetChainCriterion.getEntityExample(); + if (entities.isEmpty()) { + targetEntityExample.setEmptyQuery(true); + continue; + } + + List fieldValues = collectFieldValues(entities, bindingDefinition.getFieldAttribute()); + if (fieldValues.isEmpty()) { + targetEntityExample.setEmptyQuery(true); + continue; } + + String boundFieldName = bindingDefinition.getBoundFieldName(); + Object fieldValue = fieldValues.size() == 1 ? fieldValues.get(0) : fieldValues; + + RepositoryLocation targetRepositoryLocation = targetChainCriterion.getRepositoryLocation(); + RepositoryDefinition targetRepositoryDefinition = targetRepositoryLocation.getRepositoryDefinition(); + ConfiguredRepository targetConfiguredRepository = targetRepositoryDefinition.getConfiguredRepository(); + EntityMapper targetEntityMapper = targetConfiguredRepository.getEntityMapper(); + + EntityCriterion entityCriterion = targetEntityMapper.newCriterion(boundFieldName, Operator.EQ, fieldValue); + targetEntityExample.addCriterion(entityCriterion); } } }); diff --git a/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/repository/AbstractCoatingRepository.java b/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/repository/AbstractCoatingRepository.java index 57fd30e06d0770d69a931796e91e64b5a41ad67a..179ebd474213c80bb51d6b3529c29f6d08ed5f25 100644 --- a/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/repository/AbstractCoatingRepository.java +++ b/spring-domain-coating/src/main/java/com/gitee/spring/domain/coating/repository/AbstractCoatingRepository.java @@ -7,21 +7,18 @@ import com.gitee.spring.domain.coating.annotation.CoatingScan; import com.gitee.spring.domain.coating.annotation.IgnoreProperty; import com.gitee.spring.domain.coating.annotation.Property; import com.gitee.spring.domain.coating.api.CoatingAssembler; -import com.gitee.spring.domain.coating.api.EntityCriterionBuilder; import com.gitee.spring.domain.coating.api.CustomAssembler; -import com.gitee.spring.domain.coating.builder.*; import com.gitee.spring.domain.coating.entity.CoatingDefinition; import com.gitee.spring.domain.coating.impl.DefaultCoatingAssembler; import com.gitee.spring.domain.coating.entity.PropertyDefinition; import com.gitee.spring.domain.coating.utils.ResourceUtils; -import com.gitee.spring.domain.core.api.Constants; +import com.gitee.spring.domain.core.constants.Attribute; import com.gitee.spring.domain.core.entity.EntityDefinition; import com.gitee.spring.domain.core.entity.EntityPropertyChain; import com.gitee.spring.domain.coating.entity.RepositoryLocation; -import com.gitee.spring.domain.core.repository.AbstractDelegateRepository; +import com.gitee.spring.domain.coating.entity.RepositoryDefinition; +import com.gitee.spring.domain.coating.entity.RepositoryGroup; import com.gitee.spring.domain.core.repository.ConfiguredRepository; -import com.gitee.spring.domain.core.utils.PathUtils; -import com.gitee.spring.domain.event.repository.AbstractEventRepository; import lombok.Data; import lombok.EqualsAndHashCode; import org.apache.commons.lang3.StringUtils; @@ -36,7 +33,7 @@ import java.util.concurrent.ConcurrentHashMap; @Data @EqualsAndHashCode(callSuper = false) -public abstract class AbstractCoatingRepository extends AbstractEventRepository { +public abstract class AbstractCoatingRepository extends AbstractAwareRepository { protected Map, CoatingAssembler> classCoatingAssemblerMap = new ConcurrentHashMap<>(); protected Map nameCoatingAssemblerMap = new ConcurrentHashMap<>(); @@ -81,24 +78,21 @@ public abstract class AbstractCoatingRepository extends AbstractEventRepo boolean isBoundLocation = false; if (attributes != null) { - locationAttribute = attributes.getString(Constants.LOCATION_ATTRIBUTE); - aliasAttribute = attributes.getString(Constants.ALIAS_ATTRIBUTE); - operatorAttribute = attributes.getString(Constants.OPERATOR_ATTRIBUTE); + locationAttribute = attributes.getString(Attribute.LOCATION_ATTRIBUTE); + aliasAttribute = attributes.getString(Attribute.ALIAS_ATTRIBUTE); + operatorAttribute = attributes.getString(Attribute.OPERATOR_ATTRIBUTE); isBoundLocation = locationAttribute.startsWith("/"); } if (StringUtils.isBlank(aliasAttribute)) { aliasAttribute = fieldName; } - EntityCriterionBuilder entityCriterionBuilder = getEntityCriterionBuilder(operatorAttribute); - Assert.notNull(entityCriterionBuilder, "The builder of criterion cannot be null!"); - EntityPropertyChain entityPropertyChain = fieldEntityPropertyChainMap.get(fieldName); PropertyDefinition propertyDefinition = new PropertyDefinition( declaredField, fieldClass, isCollection, genericFieldClass, fieldName, attributes, locationAttribute, aliasAttribute, operatorAttribute, - isBoundLocation, entityCriterionBuilder, entityPropertyChain); + isBoundLocation, entityPropertyChain); allPropertyDefinitionMap.put(fieldName, propertyDefinition); @@ -114,19 +108,14 @@ public abstract class AbstractCoatingRepository extends AbstractEventRepo } }); - Map repositoryLocationMap = new LinkedHashMap<>(); - collectRepositoryLocationMap(repositoryLocationMap, new ArrayList<>(), null, this, + List reversedRepositoryLocations = collectReversedRepositoryLocations( locationPropertyDefinitionsMap, fieldPropertyDefinitionMap); - - List repositoryLocations = new ArrayList<>(repositoryLocationMap.values()); - checkFieldNames(coatingClass, allPropertyDefinitionMap.keySet(), repositoryLocations); - List reversedRepositoryLocations = new ArrayList<>(repositoryLocations); - Collections.reverse(reversedRepositoryLocations); + checkFieldNames(coatingClass, allPropertyDefinitionMap.keySet(), reversedRepositoryLocations); AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(coatingClass, Coating.class); String name = null; if (attributes != null) { - name = attributes.getString(Constants.NAME_ATTRIBUTE); + name = attributes.getString(Attribute.NAME_ATTRIBUTE); } if (StringUtils.isBlank(name)) { name = StrUtil.lowerFirst(coatingClass.getSimpleName()); @@ -134,7 +123,7 @@ public abstract class AbstractCoatingRepository extends AbstractEventRepo CoatingDefinition coatingDefinition = new CoatingDefinition(entityClass, coatingClass, attributes, name, allPropertyDefinitionMap); CoatingAssembler coatingAssembler = new DefaultCoatingAssembler( - coatingDefinition, availablePropertyDefinitions, repositoryLocations, reversedRepositoryLocations); + coatingDefinition, availablePropertyDefinitions, reversedRepositoryLocations); classCoatingAssemblerMap.put(coatingClass, coatingAssembler); Assert.isTrue(!nameCoatingAssemblerMap.containsKey(name), "The same coating name cannot exist!"); @@ -143,107 +132,35 @@ public abstract class AbstractCoatingRepository extends AbstractEventRepo } } - protected EntityCriterionBuilder getEntityCriterionBuilder(String operatorAttribute) { - if ("=".equals(operatorAttribute)) { - return applicationContext.getBean(EqualEntityCriterionBuilder.class); - - } else if (">".equals(operatorAttribute)) { - return applicationContext.getBean(GreaterThanEntityCriterionBuilder.class); - - } else if (">=".equals(operatorAttribute)) { - return applicationContext.getBean(GreaterThanOrEqualEntityCriterionBuilder.class); - - } else if ("<".equals(operatorAttribute)) { - return applicationContext.getBean(LessThanEntityCriterionBuilder.class); - - } else if ("<=".equals(operatorAttribute)) { - return applicationContext.getBean(LessThanOrEqualEntityCriterionBuilder.class); - } - return null; - } - - protected void collectRepositoryLocationMap(Map repositoryLocationMap, - List multiAccessPath, - ConfiguredRepository parentConfiguredRepository, - AbstractDelegateRepository abstractDelegateRepository, - Map> locationPropertyDefinitionsMap, - Map fieldPropertyDefinitionMap) { - - String parentAccessPath = multiAccessPath.size() > 1 ? StrUtil.join("", multiAccessPath.subList(0, multiAccessPath.size() - 1)) : ""; - String prefixAccessPath = StrUtil.join("", multiAccessPath); - - collectRepositoryLocationByLocations( - repositoryLocationMap, multiAccessPath, - parentConfiguredRepository, abstractDelegateRepository, locationPropertyDefinitionsMap, - parentAccessPath, prefixAccessPath, rootRepository); - - for (EntityPropertyChain entityPropertyChain : entityPropertyChainMap.values()) { - String accessPath = entityPropertyChain.getAccessPath(); - ConfiguredRepository configuredRepository = configuredRepositoryMap.get(accessPath); - if (configuredRepository != null) { - collectRepositoryLocationByLocations( - repositoryLocationMap, multiAccessPath, - parentConfiguredRepository, abstractDelegateRepository, locationPropertyDefinitionsMap, - parentAccessPath, prefixAccessPath, configuredRepository); - } - - String fieldName = entityPropertyChain.getFieldName(); - if (fieldPropertyDefinitionMap.containsKey(fieldName) || entityPropertyChain.isBoundProperty()) { - String belongAccessPath = PathUtils.getBelongPath(configuredRepositoryMap.keySet(), accessPath); - ConfiguredRepository belongConfiguredRepository = configuredRepositoryMap.get(belongAccessPath); - if (belongConfiguredRepository != null) { - EntityDefinition entityDefinition = belongConfiguredRepository.getEntityDefinition(); - String absoluteAccessPath = prefixAccessPath + entityDefinition.getAccessPath(); - boolean forwardParent = entityDefinition.isRoot() && parentConfiguredRepository != null; - - if (!repositoryLocationMap.containsKey(absoluteAccessPath)) { - RepositoryLocation repositoryLocation = new RepositoryLocation( - multiAccessPath, parentAccessPath, prefixAccessPath, absoluteAccessPath, - forwardParent, parentConfiguredRepository, abstractDelegateRepository, belongConfiguredRepository, - new ArrayList<>()); - repositoryLocationMap.put(absoluteAccessPath, repositoryLocation); - } + protected List collectReversedRepositoryLocations(Map> locationPropertyDefinitionsMap, + Map fieldPropertyDefinitionMap) { + List reversedRepositoryLocations = new ArrayList<>(); + for (RepositoryGroup repositoryGroup : reversedRepositoryGroups) { + for (RepositoryDefinition repositoryDefinition : repositoryGroup.getRepositoryDefinitions()) { + String absoluteAccessPath = repositoryDefinition.getAbsoluteAccessPath(); + ConfiguredRepository definitionRepository = repositoryDefinition.getDefinitionRepository(); + EntityDefinition entityDefinition = definitionRepository.getEntityDefinition(); + + List propertyDefinitions = new ArrayList<>(); + List locationPropertyDefinitions = locationPropertyDefinitionsMap.get(absoluteAccessPath); + if (locationPropertyDefinitions != null) { + propertyDefinitions.addAll(locationPropertyDefinitions); + } + for (String fieldName : entityDefinition.getFieldNames()) { PropertyDefinition propertyDefinition = fieldPropertyDefinitionMap.get(fieldName); if (propertyDefinition != null) { - RepositoryLocation repositoryLocation = repositoryLocationMap.get(absoluteAccessPath); - List propertyDefinitions = repositoryLocation.getCollectedPropertyDefinitions(); propertyDefinitions.add(propertyDefinition); } } - } - } - for (ConfiguredRepository configuredRepository : delegateConfiguredRepositories) { - multiAccessPath = new ArrayList<>(multiAccessPath); - EntityDefinition entityDefinition = configuredRepository.getEntityDefinition(); - multiAccessPath.add(entityDefinition.getAccessPath()); - AbstractDelegateRepository delegateRepository = (AbstractDelegateRepository) configuredRepository.getRepository(); - collectRepositoryLocationMap(repositoryLocationMap, multiAccessPath, configuredRepository, delegateRepository, - locationPropertyDefinitionsMap, fieldPropertyDefinitionMap); - } - } - - protected void collectRepositoryLocationByLocations(Map repositoryLocationMap, - List multiAccessPath, - ConfiguredRepository parentConfiguredRepository, - AbstractDelegateRepository abstractDelegateRepository, - Map> locationPropertyDefinitionsMap, - String parentAccessPath, - String prefixAccessPath, - ConfiguredRepository configuredRepository) { - EntityDefinition entityDefinition = configuredRepository.getEntityDefinition(); - String absoluteAccessPath = prefixAccessPath + entityDefinition.getAccessPath(); - boolean forwardParent = entityDefinition.isRoot() && parentConfiguredRepository != null; - - if (locationPropertyDefinitionsMap.containsKey(absoluteAccessPath)) { - List propertyDefinitions = locationPropertyDefinitionsMap.get(absoluteAccessPath); - RepositoryLocation repositoryLocation = new RepositoryLocation( - multiAccessPath, parentAccessPath, prefixAccessPath, absoluteAccessPath, - forwardParent, parentConfiguredRepository, abstractDelegateRepository, configuredRepository, - propertyDefinitions); - repositoryLocationMap.put(absoluteAccessPath, repositoryLocation); + if (!propertyDefinitions.isEmpty() || entityDefinition.isBoundEntity()) { + RepositoryLocation repositoryLocation = new RepositoryLocation(repositoryDefinition, propertyDefinitions); + reversedRepositoryLocations.add(repositoryLocation); + } + } } + return reversedRepositoryLocations; } protected void checkFieldNames(Class coatingClass, Set fieldNames, List repositoryLocations) { diff --git a/spring-domain-coating/src/main/resources/META-INF/spring.factories b/spring-domain-coating/src/main/resources/META-INF/spring.factories deleted file mode 100644 index 706563c417615094f2fc816eb5b7fe78a2a4ed66..0000000000000000000000000000000000000000 --- a/spring-domain-coating/src/main/resources/META-INF/spring.factories +++ /dev/null @@ -1 +0,0 @@ -org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.gitee.spring.domain.coating.config.DomainCoatingConfiguration \ No newline at end of file diff --git a/spring-domain-core/pom.xml b/spring-domain-core/pom.xml index 062e7442270d9cfbb2e9c11672077a16466700dc..d6139a9ff979f7cae3af71916d20767273e4f378 100644 --- a/spring-domain-core/pom.xml +++ b/spring-domain-core/pom.xml @@ -6,7 +6,7 @@ com.gitee.digital-engine spring-domain - 2.5.8 + 2.6.1 spring-domain-core diff --git a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/annotation/Binding.java b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/annotation/Binding.java index f1530a9ecc26cd18bfc1f7323b8d28a300fd52c2..fa1a3d2154904a8c6e0203f13468f289066c6827 100644 --- a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/annotation/Binding.java +++ b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/annotation/Binding.java @@ -11,10 +11,8 @@ public @interface Binding { String field(); - String fieldAlias() default ""; + String alias() default ""; String bind(); - String bindAlias() default ""; - } diff --git a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/api/EntityAssembler.java b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/api/EntityAssembler.java index 4a3f5f0f88b1749315a85cff6555d498e0ab648a..2f657f1ff07049d93f44f576858e8af0c928f450 100644 --- a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/api/EntityAssembler.java +++ b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/api/EntityAssembler.java @@ -5,8 +5,8 @@ import com.gitee.spring.domain.core.entity.EntityDefinition; public interface EntityAssembler { - Object assemble(EntityDefinition entityDefinition, BoundedContext boundedContext, Object persistentObject); + Object assemble(BoundedContext boundedContext, EntityDefinition entityDefinition, Object persistentObject); - Object disassemble(EntityDefinition entityDefinition, BoundedContext boundedContext, Object entity); + Object disassemble(BoundedContext boundedContext, EntityDefinition entityDefinition, Object entity); } diff --git a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/api/EntityCriterion.java b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/api/EntityCriterion.java index 2d7df86b322bd3e1c620887a61f05c1d7f9506f6..271ede65ec5787568f1d5d905c49c406e02504cb 100644 --- a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/api/EntityCriterion.java +++ b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/api/EntityCriterion.java @@ -3,7 +3,13 @@ package com.gitee.spring.domain.core.api; import com.gitee.spring.domain.core.entity.EntityExample; public interface EntityCriterion { - + + String getFieldName(); + + String getOperator(); + + Object getFieldValue(); + void appendTo(EntityExample entityExample); } diff --git a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/api/EntityIndex.java b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/api/EntityIndex.java new file mode 100644 index 0000000000000000000000000000000000000000..f7c2299f8fc68b360e74ddc1c2d345b8f5419c1e --- /dev/null +++ b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/api/EntityIndex.java @@ -0,0 +1,11 @@ +package com.gitee.spring.domain.core.api; + +import com.gitee.spring.domain.core.repository.ConfiguredRepository; + +import java.util.List; + +public interface EntityIndex { + + List selectList(Object rootEntity, ConfiguredRepository configuredRepository); + +} diff --git a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/api/EntityMapper.java b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/api/EntityMapper.java index 3194535a2cc17b3b7875a1b206b4ed292db2562c..477c722767c10cb737b5a2cbbbdab2822e72094e 100644 --- a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/api/EntityMapper.java +++ b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/api/EntityMapper.java @@ -10,20 +10,12 @@ public interface EntityMapper { Object newPage(Integer pageNum, Integer pageSize); - List getDataFromPage(Object dataPage); + List getDataFromPage(Object dataPage); Object newPageOfEntities(Object dataPage, List entities); - EntityExample newExample(EntityDefinition entityDefinition, BoundedContext boundedContext); + EntityExample newExample(BoundedContext boundedContext, EntityDefinition entityDefinition); - EntityCriterion newEqualCriterion(String fieldName, Object fieldValue); - - EntityCriterion newGreaterThanCriterion(String fieldName, Object fieldValue); - - EntityCriterion newGreaterThanOrEqualCriterion(String fieldName, Object fieldValue); - - EntityCriterion newLessThanCriterion(String fieldName, Object fieldValue); - - EntityCriterion newLessThanOrEqualCriterion(String fieldName, Object fieldValue); + EntityCriterion newCriterion(String fieldName, String operator, Object fieldValue); } diff --git a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/api/Constants.java b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/constants/Attribute.java similarity index 80% rename from spring-domain-core/src/main/java/com/gitee/spring/domain/core/api/Constants.java rename to spring-domain-core/src/main/java/com/gitee/spring/domain/core/constants/Attribute.java index 69bc5a9bbfbe63850d64bf1ab0f5dd16592ad318..b70f3379e24a413da50aa88027c5e05f456ff562 100644 --- a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/api/Constants.java +++ b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/constants/Attribute.java @@ -1,6 +1,6 @@ -package com.gitee.spring.domain.core.api; +package com.gitee.spring.domain.core.constants; -public interface Constants { +public interface Attribute { String NAME_ATTRIBUTE = "name"; String SCENE_ATTRIBUTE = "scene"; @@ -14,11 +14,9 @@ public interface Constants { String REPOSITORY_ATTRIBUTE = "repository"; String FIELD_ATTRIBUTE = "field"; - String FIELD_ALIAS_ATTRIBUTE = "fieldAlias"; + String ALIAS_ATTRIBUTE = "alias"; String BIND_ATTRIBUTE = "bind"; - String BIND_ALIAS_ATTRIBUTE = "bindAlias"; String LOCATION_ATTRIBUTE = "location"; - String ALIAS_ATTRIBUTE = "alias"; String OPERATOR_ATTRIBUTE = "operator"; } diff --git a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/constants/Operator.java b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/constants/Operator.java new file mode 100644 index 0000000000000000000000000000000000000000..590e43681c9dc13e5111933ae8d47d0847bd3165 --- /dev/null +++ b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/constants/Operator.java @@ -0,0 +1,9 @@ +package com.gitee.spring.domain.core.constants; + +public interface Operator { + String EQ = "="; + String GT = ">"; + String GE = ">="; + String LT = "<"; + String LE = "<="; +} diff --git a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/entity/AbstractEntityCriterion.java b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/entity/AbstractEntityCriterion.java index 6c7c201be833804644ce1ba5b164320c62912139..7eac6bda3e3d2c92460e9c39fb1ad8b13e19a792 100644 --- a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/entity/AbstractEntityCriterion.java +++ b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/entity/AbstractEntityCriterion.java @@ -8,5 +8,6 @@ import lombok.Data; @AllArgsConstructor public abstract class AbstractEntityCriterion implements EntityCriterion { protected String fieldName; + protected String Operator; protected Object fieldValue; } 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 e01d5a90abb16850495c49eadbcae5c3091a2455..01c83fd61250c89a63f1ef41105d2c49ab5badf6 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 @@ -10,13 +10,13 @@ import org.springframework.core.annotation.AnnotationAttributes; public class BindingDefinition { private AnnotationAttributes attributes; private String fieldAttribute; - private String fieldAliasAttribute; + private String aliasAttribute; private String bindAttribute; - private String bindAliasAttribute; private boolean fromContext; private boolean boundId; private String belongAccessPath; private ConfiguredRepository belongConfiguredRepository; private String boundFieldName; private EntityPropertyChain boundEntityPropertyChain; + private EntityPropertyChain fieldEntityPropertyChain; } diff --git a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/entity/BoundedContext.java b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/entity/BoundedContext.java index 2793533446de1640f70f8e631c94295605891619..f9b9b3dfe66b28e45b5992cbe9d4deebbf1862e2 100644 --- a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/entity/BoundedContext.java +++ b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/entity/BoundedContext.java @@ -1,6 +1,15 @@ package com.gitee.spring.domain.core.entity; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; + import java.util.LinkedHashMap; +@Data +@NoArgsConstructor +@AllArgsConstructor +@EqualsAndHashCode(callSuper = false) public class BoundedContext extends LinkedHashMap { } 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 9c87570bae2c67a6f193695b051374b63588e19f..5b313a967d2de7c3b27889547fa807e140d9fe39 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 @@ -6,19 +6,22 @@ import org.springframework.core.annotation.AnnotationAttributes; import java.lang.reflect.AnnotatedElement; import java.util.List; +import java.util.Map; +import java.util.Set; @Data @AllArgsConstructor public class EntityDefinition { private boolean root; private String accessPath; + private String uniqueKey; private AnnotatedElement annotatedElement; private Class entityClass; private boolean collection; private Class genericEntityClass; private String fieldName; private AnnotationAttributes attributes; - private String[] sceneAttribute; + private Set sceneAttribute; private Object mapper; private Class pojoClass; private boolean sameType; @@ -27,10 +30,14 @@ public class EntityDefinition { private boolean mapAsExample; private String orderByAsc; private String orderByDesc; - private String orderBy; + private String[] orderBy; private String sort; private int orderAttribute; - private List bindingDefinitions; + private List allBindingDefinitions; + private List boundBindingDefinitions; + private List contextBindingDefinitions; private BindingDefinition boundIdBindingDefinition; - private List bindingColumns; + private boolean boundEntity; + private Set fieldNames; + private Map entityPropertyChainMap; } diff --git a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/entity/EntityExample.java b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/entity/EntityExample.java index ea11c6999176967e4943ad571a2c8fafcd6d160b..9be0e2c6d36416f8044fea7b307b18af679f9e99 100644 --- a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/entity/EntityExample.java +++ b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/entity/EntityExample.java @@ -8,11 +8,12 @@ import java.util.List; @Data public class EntityExample { + protected boolean emptyQuery = false; protected Object example; protected List columns; protected List entityCriteria = new ArrayList<>(); - protected String orderBy; + protected String[] orderBy; protected String sort; public EntityExample(Object example) { diff --git a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/entity/EntityPropertyChain.java b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/entity/EntityPropertyChain.java index 89559f1702e188db4b00172696378eb9d4dbb6b4..bb8d82779def14580f19689d079fc608f6be13af 100644 --- a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/entity/EntityPropertyChain.java +++ b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/entity/EntityPropertyChain.java @@ -12,14 +12,29 @@ import java.lang.reflect.Field; @AllArgsConstructor @ToString(exclude = "lastEntityPropertyChain") public class EntityPropertyChain implements EntityProperty { + private EntityPropertyChain lastEntityPropertyChain; private Class lastEntityClass; - private Field declaredField; private String accessPath; + private Field declaredField; private Class entityClass; + private boolean collection; + private Class genericEntityClass; private String fieldName; private EntityProperty entityProperty; - private boolean boundProperty; + + public EntityPropertyChain(EntityPropertyChain lastEntityPropertyChain, + EntityPropertyChain entityPropertyChain) { + this.lastEntityPropertyChain = lastEntityPropertyChain; + this.lastEntityClass = entityPropertyChain.getLastEntityClass(); + this.accessPath = entityPropertyChain.getAccessPath(); + this.declaredField = entityPropertyChain.getDeclaredField(); + this.entityClass = entityPropertyChain.getEntityClass(); + this.collection = entityPropertyChain.isCollection(); + this.genericEntityClass = entityPropertyChain.getGenericEntityClass(); + this.fieldName = entityPropertyChain.getFieldName(); + this.entityProperty = entityPropertyChain.getEntityProperty(); + } public void initialize() { if (entityProperty == null) { diff --git a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/impl/DefaultEntityAssembler.java b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/impl/DefaultEntityAssembler.java index 675f30e3acdb2a5a476eb55413aaeeff00932459..d977c9afa4b490ca7f025ce3e7eef8f2f904b867 100644 --- a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/impl/DefaultEntityAssembler.java +++ b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/impl/DefaultEntityAssembler.java @@ -8,7 +8,7 @@ import com.gitee.spring.domain.core.entity.EntityDefinition; public class DefaultEntityAssembler implements EntityAssembler { @Override - public Object assemble(EntityDefinition entityDefinition, BoundedContext boundedContext, Object persistentObject) { + public Object assemble(BoundedContext boundedContext, EntityDefinition entityDefinition, Object persistentObject) { if (entityDefinition.isSameType()) { return persistentObject; } else { @@ -17,7 +17,7 @@ public class DefaultEntityAssembler implements EntityAssembler { } @Override - public Object disassemble(EntityDefinition entityDefinition, BoundedContext boundedContext, Object entity) { + public Object disassemble(BoundedContext boundedContext, EntityDefinition entityDefinition, Object entity) { if (entityDefinition.isSameType()) { return entity; } else { diff --git a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/impl/DefaultEntityIndex.java b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/impl/DefaultEntityIndex.java new file mode 100644 index 0000000000000000000000000000000000000000..cb6bdc1dc129f9273b271e34f3ec9764a087792a --- /dev/null +++ b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/impl/DefaultEntityIndex.java @@ -0,0 +1,82 @@ +package com.gitee.spring.domain.core.impl; + +import com.gitee.spring.domain.core.api.EntityIndex; +import com.gitee.spring.domain.core.entity.BindingDefinition; +import com.gitee.spring.domain.core.entity.EntityDefinition; +import com.gitee.spring.domain.core.entity.EntityPropertyChain; +import com.gitee.spring.domain.core.repository.ConfiguredRepository; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +public class DefaultEntityIndex implements EntityIndex { + + private final Map entitiesMap = new ConcurrentHashMap<>(); + + @SuppressWarnings("unchecked") + public DefaultEntityIndex(ConfiguredRepository configuredRepository, List entities) { + EntityDefinition entityDefinition = configuredRepository.getEntityDefinition(); + List bindingDefinitions = entityDefinition.getBoundBindingDefinitions(); + for (Object entity : entities) { + StringBuilder builder = new StringBuilder(); + for (BindingDefinition bindingDefinition : bindingDefinitions) { + String aliasAttribute = bindingDefinition.getAliasAttribute(); + EntityPropertyChain entityPropertyChain = bindingDefinition.getFieldEntityPropertyChain(); + Object boundValue = entityPropertyChain.getValue(entity); + builder.append(aliasAttribute).append(": ").append(boundValue).append(", "); + } + if (builder.length() > 0) { + builder.delete(builder.length() - 2, builder.length()); + } + String foreignKey = builder.toString(); + Object existEntity = entitiesMap.get(foreignKey); + if (existEntity == null) { + entitiesMap.put(foreignKey, entity); + } else { + if (existEntity instanceof Collection) { + ((Collection) existEntity).add(entity); + } else { + List list = new ArrayList<>(); + list.add(existEntity); + list.add(entity); + entitiesMap.put(foreignKey, list); + } + } + } + } + + @Override + @SuppressWarnings("unchecked") + public List selectList(Object rootEntity, ConfiguredRepository configuredRepository) { + String foreignKey = buildForeignKey(rootEntity, configuredRepository); + Object existEntity = entitiesMap.get(foreignKey); + if (existEntity != null) { + if (existEntity instanceof Collection) { + return (List) existEntity; + } else { + return Collections.singletonList(existEntity); + } + } + return Collections.emptyList(); + } + + public String buildForeignKey(Object rootEntity, ConfiguredRepository configuredRepository) { + StringBuilder builder = new StringBuilder(); + EntityDefinition entityDefinition = configuredRepository.getEntityDefinition(); + for (BindingDefinition bindingDefinition : entityDefinition.getBoundBindingDefinitions()) { + String aliasAttribute = bindingDefinition.getAliasAttribute(); + EntityPropertyChain boundEntityPropertyChain = bindingDefinition.getBoundEntityPropertyChain(); + Object boundValue = boundEntityPropertyChain.getValue(rootEntity); + builder.append(aliasAttribute).append(": ").append(boundValue).append(", "); + } + if (builder.length() > 0) { + builder.delete(builder.length() - 2, builder.length()); + } + return builder.toString(); + } + +} diff --git a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/mapper/MapEntityMapper.java b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/mapper/MapEntityMapper.java index 685fd2e4727247cc80b4654d5b64d9c7f3584b68..51a3005af8ca186739d62012f3dfd632a1835de7 100644 --- a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/mapper/MapEntityMapper.java +++ b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/mapper/MapEntityMapper.java @@ -19,13 +19,13 @@ public class MapEntityMapper extends ProxyEntityMapper { } @Override - public EntityExample newExample(EntityDefinition entityDefinition, BoundedContext boundedContext) { + public EntityExample newExample(BoundedContext boundedContext, EntityDefinition entityDefinition) { return new EntityExample(new LinkedHashMap<>()); } @Override - public EntityCriterion newEqualCriterion(String fieldName, Object fieldValue) { - return new AbstractEntityCriterion(fieldName, fieldValue) { + public EntityCriterion newCriterion(String fieldName, String operator, Object fieldValue) { + return new AbstractEntityCriterion(fieldName, operator, fieldValue) { @Override @SuppressWarnings("unchecked") public void appendTo(EntityExample entityExample) { @@ -43,24 +43,4 @@ public class MapEntityMapper extends ProxyEntityMapper { }; } - @Override - public EntityCriterion newGreaterThanCriterion(String fieldName, Object fieldValue) { - return newEqualCriterion(fieldName + "::GreaterThan", fieldValue); - } - - @Override - public EntityCriterion newGreaterThanOrEqualCriterion(String fieldName, Object fieldValue) { - return newEqualCriterion(fieldName + "::GreaterThanOrEqual", fieldValue); - } - - @Override - public EntityCriterion newLessThanCriterion(String fieldName, Object fieldValue) { - return newEqualCriterion(fieldName + "::LessThan", fieldValue); - } - - @Override - public EntityCriterion newLessThanOrEqualCriterion(String fieldName, Object fieldValue) { - return newEqualCriterion(fieldName + "::LessThanOrEqual", fieldValue); - } - } diff --git a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/mapper/NoBuiltEntityMapper.java b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/mapper/NoBuiltEntityMapper.java deleted file mode 100644 index 64cd49da5baf8590ab5bd0671d632f42c187728a..0000000000000000000000000000000000000000 --- a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/mapper/NoBuiltEntityMapper.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.gitee.spring.domain.core.mapper; - -import com.gitee.spring.domain.core.api.EntityMapper; -import com.gitee.spring.domain.core.entity.BoundedContext; -import com.gitee.spring.domain.core.entity.EntityDefinition; -import com.gitee.spring.domain.core.entity.EntityExample; - -public class NoBuiltEntityMapper extends ProxyEntityMapper { - - public NoBuiltEntityMapper(EntityMapper entityMapper) { - super(entityMapper); - } - - @Override - public EntityExample newExample(EntityDefinition entityDefinition, BoundedContext boundedContext) { - EntityExample entityExample = super.newExample(entityDefinition, boundedContext); - return new EntityExample(entityExample) { - @Override - public Object buildExample() { - return example; - } - }; - } - -} diff --git a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/mapper/ProxyEntityMapper.java b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/mapper/ProxyEntityMapper.java index e14c9f70f616691d3ca1fadd1ffd54aaa45bc2ba..ef3348ed1e4339641a4713fb931333e268871b20 100644 --- a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/mapper/ProxyEntityMapper.java +++ b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/mapper/ProxyEntityMapper.java @@ -22,7 +22,7 @@ public class ProxyEntityMapper implements EntityMapper { } @Override - public List getDataFromPage(Object dataPage) { + public List getDataFromPage(Object dataPage) { return entityMapper.getDataFromPage(dataPage); } @@ -32,33 +32,13 @@ public class ProxyEntityMapper implements EntityMapper { } @Override - public EntityExample newExample(EntityDefinition entityDefinition, BoundedContext boundedContext) { - return entityMapper.newExample(entityDefinition, boundedContext); + public EntityExample newExample(BoundedContext boundedContext, EntityDefinition entityDefinition) { + return entityMapper.newExample(boundedContext, entityDefinition); } @Override - public EntityCriterion newEqualCriterion(String fieldName, Object fieldValue) { - return entityMapper.newEqualCriterion(fieldName, fieldValue); - } - - @Override - public EntityCriterion newGreaterThanCriterion(String fieldName, Object fieldValue) { - return entityMapper.newGreaterThanCriterion(fieldName, fieldValue); - } - - @Override - public EntityCriterion newGreaterThanOrEqualCriterion(String fieldName, Object fieldValue) { - return entityMapper.newGreaterThanOrEqualCriterion(fieldName, fieldValue); - } - - @Override - public EntityCriterion newLessThanCriterion(String fieldName, Object fieldValue) { - return entityMapper.newLessThanCriterion(fieldName, fieldValue); - } - - @Override - public EntityCriterion newLessThanOrEqualCriterion(String fieldName, Object fieldValue) { - return entityMapper.newLessThanOrEqualCriterion(fieldName, fieldValue); + public EntityCriterion newCriterion(String fieldName, String operator, Object fieldValue) { + return entityMapper.newCriterion(fieldName, operator, fieldValue); } } diff --git a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/repository/AbstractBatchRepository.java b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/repository/AbstractBatchRepository.java new file mode 100644 index 0000000000000000000000000000000000000000..812af6ba7d5e70404b597d8f86fb63b7f4110b6f --- /dev/null +++ b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/repository/AbstractBatchRepository.java @@ -0,0 +1,116 @@ +package com.gitee.spring.domain.core.repository; + +import com.gitee.spring.domain.core.api.EntityCriterion; +import com.gitee.spring.domain.core.api.EntityIndex; +import com.gitee.spring.domain.core.api.EntityMapper; +import com.gitee.spring.domain.core.api.EntityProperty; +import com.gitee.spring.domain.core.constants.Operator; +import com.gitee.spring.domain.core.entity.*; +import com.gitee.spring.domain.core.impl.DefaultEntityIndex; +import lombok.extern.slf4j.Slf4j; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +@Slf4j +public abstract class AbstractBatchRepository extends AbstractGenericRepository { + + @Override + protected void handleRootEntities(BoundedContext boundedContext, List rootEntities) { + if (rootEntities.size() == 1) { + super.handleRootEntity(boundedContext, rootEntities.get(0)); + + } else if (rootEntities.size() > 1) { + if (delegateRepositoryMap.size() == 1) { + executeQuery(boundedContext, rootEntities, this); + } else { + Map, List> repositoryEntitiesMap = adaptiveRepositoryEntities(rootEntities); + repositoryEntitiesMap.forEach((abstractDelegateRepository, eachRootEntities) -> + executeQuery(boundedContext, eachRootEntities, abstractDelegateRepository)); + } + } + } + + protected Map, List> adaptiveRepositoryEntities(List rootEntities) { + Map, List> repositoryEntitiesMap = new LinkedHashMap<>(); + for (Object rootEntity : rootEntities) { + AbstractDelegateRepository abstractDelegateRepository = adaptiveRepository(rootEntity); + List eachRootEntities = repositoryEntitiesMap.computeIfAbsent(abstractDelegateRepository, key -> new ArrayList<>()); + eachRootEntities.add(rootEntity); + } + return repositoryEntitiesMap; + } + + protected void executeQuery(BoundedContext boundedContext, List rootEntities, AbstractDelegateRepository abstractDelegateRepository) { + for (ConfiguredRepository configuredRepository : abstractDelegateRepository.getSubRepositories()) { + if (isMatchScenes(boundedContext, configuredRepository)) { + EntityExample entityExample = newExampleByRootEntities(boundedContext, rootEntities, configuredRepository); + if (!entityExample.isEmptyQuery() && entityExample.isDirtyQuery()) { + List entities = configuredRepository.selectByExample(boundedContext, entityExample); + log.debug("The data queried is: {}", entities); + EntityIndex entityIndex = buildEntityIndex(configuredRepository, entities); + assembleRootEntities(rootEntities, configuredRepository, entityIndex); + } + } + } + } + + protected EntityExample newExampleByRootEntities(BoundedContext boundedContext, List rootEntities, ConfiguredRepository configuredRepository) { + EntityDefinition entityDefinition = configuredRepository.getEntityDefinition(); + EntityMapper entityMapper = configuredRepository.getEntityMapper(); + EntityExample entityExample = entityMapper.newExample(boundedContext, entityDefinition); + for (BindingDefinition bindingDefinition : entityDefinition.getBoundBindingDefinitions()) { + EntityPropertyChain boundEntityPropertyChain = bindingDefinition.getBoundEntityPropertyChain(); + List fieldValues = new ArrayList<>(); + for (Object rootEntity : rootEntities) { + Object fieldValue = boundEntityPropertyChain.getValue(rootEntity); + if (fieldValue != null) { + fieldValues.add(fieldValue); + } + } + if (!fieldValues.isEmpty()) { + String aliasAttribute = bindingDefinition.getAliasAttribute(); + EntityCriterion entityCriterion = entityMapper.newCriterion(aliasAttribute, Operator.EQ, fieldValues); + entityExample.addCriterion(entityCriterion); + } else { + entityExample.setEmptyQuery(true); + break; + } + } + if (!entityExample.isEmptyQuery() && entityExample.isDirtyQuery()) { + for (BindingDefinition bindingDefinition : entityDefinition.getContextBindingDefinitions()) { + String bindAttribute = bindingDefinition.getBindAttribute(); + Object boundValue = boundedContext.get(bindAttribute); + if (boundValue != null) { + String aliasAttribute = bindingDefinition.getAliasAttribute(); + EntityCriterion entityCriterion = entityMapper.newCriterion(aliasAttribute, Operator.EQ, boundValue); + entityExample.addCriterion(entityCriterion); + } + } + } + return entityExample; + } + + protected EntityIndex buildEntityIndex(ConfiguredRepository configuredRepository, List entities) { + return new DefaultEntityIndex(configuredRepository, entities); + } + + protected void assembleRootEntities(List rootEntities, ConfiguredRepository configuredRepository, EntityIndex entityIndex) { + for (Object rootEntity : rootEntities) { + EntityPropertyChain entityPropertyChain = configuredRepository.getEntityPropertyChain(); + EntityPropertyChain lastEntityPropertyChain = entityPropertyChain.getLastEntityPropertyChain(); + Object lastEntity = lastEntityPropertyChain == null ? rootEntity : lastEntityPropertyChain.getValue(rootEntity); + if (lastEntity != null) { + List entities = entityIndex.selectList(rootEntity, configuredRepository); + Object entity = convertManyToOneEntity(configuredRepository, entities); + if (entity != null) { + EntityProperty entityProperty = entityPropertyChain.getEntityProperty(); + entityProperty.setValue(lastEntity, entity); + } + } + } + } + +} 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 88aa23be5ecd5a11f88c4008c007ff6c398e30e8..284904ecb0c73be07f8030d76269b68548ae5b4e 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 @@ -8,11 +8,10 @@ import com.gitee.spring.domain.core.annotation.Repository; import com.gitee.spring.domain.core.api.EntityAssembler; import com.gitee.spring.domain.core.api.EntityMapper; import com.gitee.spring.domain.core.entity.BindingDefinition; -import com.gitee.spring.domain.core.api.Constants; +import com.gitee.spring.domain.core.constants.Attribute; import com.gitee.spring.domain.core.entity.EntityDefinition; import com.gitee.spring.domain.core.entity.EntityPropertyChain; import com.gitee.spring.domain.core.mapper.MapEntityMapper; -import com.gitee.spring.domain.core.mapper.NoBuiltEntityMapper; import com.gitee.spring.domain.core.utils.PathUtils; import com.gitee.spring.domain.core.utils.ReflectUtils; import lombok.Data; @@ -29,26 +28,28 @@ import org.springframework.util.ReflectionUtils; import java.lang.reflect.*; import java.util.*; +import java.util.stream.Collectors; @Data @EqualsAndHashCode(callSuper = false) public abstract class AbstractContextRepository extends AbstractRepository implements ApplicationContextAware, InitializingBean { - protected ApplicationContext applicationContext; - protected Class entityClass; protected Constructor entityCtor; protected AnnotationAttributes attributes; protected String name; - protected Map entityPropertyChainMap = new LinkedHashMap<>(); - protected Map configuredRepositoryMap = new LinkedHashMap<>(); + protected Map allEntityPropertyChainMap = new LinkedHashMap<>(); + protected Map fieldEntityPropertyChainMap = new LinkedHashMap<>(); + protected Map allConfiguredRepositoryMap = new LinkedHashMap<>(); protected ConfiguredRepository rootRepository; protected List subRepositories = new ArrayList<>(); protected List orderedRepositories = new ArrayList<>(); + protected ApplicationContext applicationContext; + @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; @@ -64,98 +65,131 @@ public abstract class AbstractContextRepository extends AbstractRepositor attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(this.getClass(), Repository.class); if (attributes != null) { - name = attributes.getString(Constants.NAME_ATTRIBUTE); + name = attributes.getString(Attribute.NAME_ATTRIBUTE); } if (StringUtils.isBlank(name)) { name = StrUtil.lowerFirst(entityClass.getSimpleName()); } - resolveRootRepository(entityClass); - Assert.notNull(rootRepository, "The root repository does not exist!"); List> superClasses = ReflectUtils.getAllSuperClasses(entityClass, Object.class); superClasses.add(entityClass); - superClasses.forEach(clazz -> resolveSubRepositories("/", clazz)); - - orderedRepositories.sort(Comparator.comparingInt(configuredRepository -> configuredRepository.getEntityDefinition().getOrderAttribute())); - } - - protected void resolveRootRepository(Class entityClass) { - AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(entityClass, Entity.class); - if (attributes != null) { - Set bindingAnnotations = AnnotatedElementUtils.getMergedRepeatableAnnotations(entityClass, Binding.class); + superClasses.forEach(clazz -> resolveAllEntityPropertyChainMap("", clazz)); - ConfiguredRepository configuredRepository = newConfiguredRepository( - "/", entityClass, null, - entityClass, entityClass, null, - attributes, bindingAnnotations); + Map annotatedElementMap = new LinkedHashMap<>(); + annotatedElementMap.put("/", entityClass); + allEntityPropertyChainMap.values().forEach(entityPropertyChain -> + annotatedElementMap.put(entityPropertyChain.getAccessPath(), entityPropertyChain.getDeclaredField())); + annotatedElementMap.forEach(this::resolveConfiguredRepository); - configuredRepositoryMap.put("/", configuredRepository); - rootRepository = configuredRepository; - orderedRepositories.add(configuredRepository); - } + orderedRepositories.sort(Comparator.comparingInt( + configuredRepository -> configuredRepository.getEntityDefinition().getOrderAttribute())); } - protected void resolveSubRepositories(String accessPath, Class entityClass) { + protected void resolveAllEntityPropertyChainMap(String accessPath, Class entityClass) { ReflectionUtils.doWithLocalFields(entityClass, declaredField -> { - String fieldName = declaredField.getName(); - String fieldAccessPath = "/".equals(accessPath) ? accessPath + fieldName : accessPath + "/" + fieldName; - Class fieldEntityClass = declaredField.getType(); + boolean isCollection = false; Class fieldGenericEntityClass = fieldEntityClass; + String fieldName = declaredField.getName(); + if (Collection.class.isAssignableFrom(fieldEntityClass)) { + isCollection = true; ParameterizedType parameterizedType = (ParameterizedType) declaredField.getGenericType(); Type actualTypeArgument = parameterizedType.getActualTypeArguments()[0]; fieldGenericEntityClass = (Class) actualTypeArgument; } - EntityPropertyChain entityPropertyChain = newEntityPropertyChain( - entityClass, declaredField, fieldAccessPath, fieldEntityClass, fieldName); - entityPropertyChainMap.put(fieldAccessPath, entityPropertyChain); + EntityPropertyChain lastEntityPropertyChain = allEntityPropertyChainMap.get(accessPath); + String fieldAccessPath = accessPath + "/" + fieldName; + + EntityPropertyChain entityPropertyChain = new EntityPropertyChain( + lastEntityPropertyChain, + entityClass, + fieldAccessPath, + declaredField, + fieldEntityClass, + isCollection, + fieldGenericEntityClass, + fieldName, + null); + allEntityPropertyChainMap.put(fieldAccessPath, entityPropertyChain); + fieldEntityPropertyChainMap.putIfAbsent(fieldName, entityPropertyChain); - AnnotationAttributes fieldAttributes = AnnotatedElementUtils.getMergedAnnotationAttributes(declaredField, Entity.class); - if (fieldAttributes != null) { - entityPropertyChain.initialize(); - Set fieldBindingAnnotations = AnnotatedElementUtils.getMergedRepeatableAnnotations(declaredField, Binding.class); + if (!filterEntityClass(fieldEntityClass)) { + resolveAllEntityPropertyChainMap(fieldAccessPath, fieldEntityClass); + } + }); + } + + protected boolean filterEntityClass(Class entityClass) { + String className = entityClass.getName(); + return className.startsWith("java.lang.") || className.startsWith("java.util."); + } + protected void resolveConfiguredRepository(String accessPath, AnnotatedElement annotatedElement) { + AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(annotatedElement, Entity.class); + Set bindingAnnotations = AnnotatedElementUtils.getMergedRepeatableAnnotations(annotatedElement, Binding.class); + if (attributes != null) { + if ("/".equals(accessPath)) { ConfiguredRepository configuredRepository = newConfiguredRepository( - fieldAccessPath, declaredField, entityPropertyChain, - fieldEntityClass, fieldGenericEntityClass, fieldName, - fieldAttributes, fieldBindingAnnotations); + true, + "/", + annotatedElement, + null, + entityClass, + false, + entityClass, + null, + attributes, + bindingAnnotations); + rootRepository = configuredRepository; + allConfiguredRepositoryMap.put(accessPath, configuredRepository); + orderedRepositories.add(configuredRepository); + + } else { + EntityPropertyChain entityPropertyChain = allEntityPropertyChainMap.get(accessPath); + entityPropertyChain.initialize(); + + Class entityClass = entityPropertyChain.getEntityClass(); + boolean isCollection = entityPropertyChain.isCollection(); + Class genericEntityClass = entityPropertyChain.getGenericEntityClass(); + String fieldName = entityPropertyChain.getFieldName(); - configuredRepositoryMap.put(fieldAccessPath, configuredRepository); + ConfiguredRepository configuredRepository = newConfiguredRepository( + false, + accessPath, + annotatedElement, + entityPropertyChain, + entityClass, + isCollection, + genericEntityClass, + fieldName, + attributes, + bindingAnnotations); subRepositories.add(configuredRepository); + allConfiguredRepositoryMap.put(accessPath, configuredRepository); orderedRepositories.add(configuredRepository); } - - if (!filterEntityClass(fieldEntityClass)) { - resolveSubRepositories(fieldAccessPath, fieldEntityClass); - } - }); - } - - protected EntityPropertyChain newEntityPropertyChain(Class lastEntityClass, Field declaredField, - String accessPath, Class entityClass, String fieldName) { - String lastAccessPath = PathUtils.getLastAccessPath(accessPath); - EntityPropertyChain lastEntityPropertyChain = entityPropertyChainMap.get(lastAccessPath); - return new EntityPropertyChain(lastEntityPropertyChain, lastEntityClass, declaredField, - accessPath, entityClass, fieldName, null, false); + } } @SuppressWarnings("unchecked") - protected ConfiguredRepository newConfiguredRepository(String accessPath, + protected ConfiguredRepository newConfiguredRepository(boolean isRoot, + String accessPath, AnnotatedElement annotatedElement, EntityPropertyChain entityPropertyChain, Class entityClass, + boolean isCollection, Class genericEntityClass, String fieldName, AnnotationAttributes attributes, Set bindingAnnotations) { - boolean isRoot = entityPropertyChain == null; - boolean isCollection = Collection.class.isAssignableFrom(entityClass); + String uniqueKey = name + ":" + accessPath; - String[] sceneAttribute = attributes.getStringArray(Constants.SCENE_ATTRIBUTE); + String[] sceneAttributeStrs = attributes.getStringArray(Attribute.SCENE_ATTRIBUTE); + Set sceneAttribute = new LinkedHashSet<>(Arrays.asList(sceneAttributeStrs)); - Class mapperClass = attributes.getClass(Constants.MAPPER_ATTRIBUTE); + Class mapperClass = attributes.getClass(Attribute.MAPPER_ATTRIBUTE); Object mapper = null; Class pojoClass = null; if (mapperClass != Object.class) { @@ -173,43 +207,45 @@ public abstract class AbstractContextRepository extends AbstractRepositor boolean sameType = genericEntityClass == pojoClass; Class mappedClass = pojoClass != null ? pojoClass : genericEntityClass; - boolean useEntityExample = attributes.getBoolean(Constants.USE_ENTITY_EXAMPLE_ATTRIBUTE); - boolean mapAsExample = attributes.getBoolean(Constants.MAP_AS_EXAMPLE_ATTRIBUTE); + boolean useEntityExample = attributes.getBoolean(Attribute.USE_ENTITY_EXAMPLE_ATTRIBUTE); + boolean mapAsExample = attributes.getBoolean(Attribute.MAP_AS_EXAMPLE_ATTRIBUTE); - String orderByAsc = attributes.getString(Constants.ORDER_BY_ASC_ATTRIBUTE); - String orderByDesc = attributes.getString(Constants.ORDER_BY_DESC_ATTRIBUTE); - String orderBy = null; + String orderByAsc = attributes.getString(Attribute.ORDER_BY_ASC_ATTRIBUTE); + String orderByDesc = attributes.getString(Attribute.ORDER_BY_DESC_ATTRIBUTE); + String[] orderBy = null; String sort = null; if (StringUtils.isNotBlank(orderByAsc)) { - orderBy = orderByAsc; + orderBy = StrUtil.splitTrim(orderByAsc, ",").toArray(new String[0]); sort = "asc"; } if (StringUtils.isNotBlank(orderByDesc)) { - orderBy = orderByAsc; + orderBy = StrUtil.splitTrim(orderByDesc, ",").toArray(new String[0]); sort = "desc"; } - int orderAttribute = attributes.getNumber(Constants.ORDER_ATTRIBUTE).intValue(); + int orderAttribute = attributes.getNumber(Attribute.ORDER_ATTRIBUTE).intValue(); - Class assemblerClass = attributes.getClass(Constants.ASSEMBLER_ATTRIBUTE); + Class assemblerClass = attributes.getClass(Attribute.ASSEMBLER_ATTRIBUTE); EntityAssembler entityAssembler = (EntityAssembler) applicationContext.getBean(assemblerClass); - Class repositoryClass = attributes.getClass(Constants.REPOSITORY_ATTRIBUTE); + Class repositoryClass = attributes.getClass(Attribute.REPOSITORY_ATTRIBUTE); Object repository = repositoryClass != DefaultRepository.class ? applicationContext.getBean(repositoryClass) : null; List bindingDefinitions = new ArrayList<>(); + List boundBindingDefinitions = new ArrayList<>(); + List contextBindingDefinitions = new ArrayList<>(); BindingDefinition boundIdBindingDefinition = null; - List bindingColumns = new ArrayList<>(); + for (Binding bindingAnnotation : bindingAnnotations) { AnnotationAttributes bindingAttributes = AnnotationUtils.getAnnotationAttributes( bindingAnnotation, false, false); - String fieldAttribute = bindingAttributes.getString(Constants.FIELD_ATTRIBUTE); - String fieldAliasAttribute = bindingAttributes.getString(Constants.FIELD_ALIAS_ATTRIBUTE); - String bindAttribute = bindingAttributes.getString(Constants.BIND_ATTRIBUTE); - String bindAliasAttribute = bindingAttributes.getString(Constants.BIND_ALIAS_ATTRIBUTE); - if (StringUtils.isBlank(fieldAliasAttribute)) { - fieldAliasAttribute = fieldAttribute; + String fieldAttribute = bindingAttributes.getString(Attribute.FIELD_ATTRIBUTE); + String aliasAttribute = bindingAttributes.getString(Attribute.ALIAS_ATTRIBUTE); + String bindAttribute = bindingAttributes.getString(Attribute.BIND_ATTRIBUTE); + + if (StringUtils.isBlank(aliasAttribute)) { + aliasAttribute = fieldAttribute; } if (bindAttribute.startsWith(".")) { @@ -226,30 +262,30 @@ public abstract class AbstractContextRepository extends AbstractRepositor EntityPropertyChain boundEntityPropertyChain = null; if (!isFromContext) { - belongAccessPath = PathUtils.getBelongPath(configuredRepositoryMap.keySet(), bindAttribute); - belongConfiguredRepository = configuredRepositoryMap.get(belongAccessPath); - Assert.notNull(belongConfiguredRepository, "No belong repository found!"); + belongAccessPath = PathUtils.getBelongPath(allConfiguredRepositoryMap.keySet(), bindAttribute); + belongConfiguredRepository = allConfiguredRepositoryMap.get(belongAccessPath); + Assert.notNull(belongConfiguredRepository, "The belong repository cannot be null!"); boundFieldName = PathUtils.getFieldName(bindAttribute); - boundEntityPropertyChain = entityPropertyChainMap.get(bindAttribute); - Assert.notNull(boundEntityPropertyChain, "Bound path not available!"); - + boundEntityPropertyChain = allEntityPropertyChainMap.get(bindAttribute); + Assert.notNull(boundEntityPropertyChain, "The bound entity property cannot be null!"); boundEntityPropertyChain.initialize(); - boundEntityPropertyChain.setBoundProperty(true); - bindingColumns.add(StrUtil.toUnderlineCase(fieldAliasAttribute)); - } - - if (StringUtils.isBlank(bindAliasAttribute) && boundFieldName != null) { - bindAliasAttribute = boundFieldName; + EntityDefinition entityDefinition = belongConfiguredRepository.getEntityDefinition(); + entityDefinition.setBoundEntity(true); } BindingDefinition bindingDefinition = new BindingDefinition( - bindingAttributes, - fieldAttribute, fieldAliasAttribute, bindAttribute, bindAliasAttribute, - isFromContext, isBoundId, - belongAccessPath, belongConfiguredRepository, boundFieldName, boundEntityPropertyChain); + bindingAttributes, fieldAttribute, aliasAttribute, bindAttribute, + isFromContext, isBoundId, belongAccessPath, belongConfiguredRepository, + boundFieldName, boundEntityPropertyChain, null); + bindingDefinitions.add(bindingDefinition); + if (!isFromContext) { + boundBindingDefinitions.add(bindingDefinition); + } else { + contextBindingDefinitions.add(bindingDefinition); + } if (isBoundId) { boundIdBindingDefinition = bindingDefinition; @@ -261,19 +297,17 @@ public abstract class AbstractContextRepository extends AbstractRepositor } EntityDefinition entityDefinition = new EntityDefinition( - isRoot, accessPath, annotatedElement, + isRoot, accessPath, uniqueKey, annotatedElement, entityClass, isCollection, genericEntityClass, fieldName, attributes, sceneAttribute, mapper, pojoClass, sameType, mappedClass, - useEntityExample, mapAsExample, orderByAsc, orderByDesc, orderBy, sort, - orderAttribute, bindingDefinitions, boundIdBindingDefinition, bindingColumns); + useEntityExample, mapAsExample, orderByAsc, orderByDesc, orderBy, sort, orderAttribute, + bindingDefinitions, boundBindingDefinitions, contextBindingDefinitions, boundIdBindingDefinition, + false, new LinkedHashSet<>(), new LinkedHashMap<>()); EntityMapper entityMapper = newEntityMapper(entityDefinition); if (mapAsExample) { entityMapper = new MapEntityMapper(entityMapper); } - if (useEntityExample) { - entityMapper = new NoBuiltEntityMapper(entityMapper); - } if (repository == null) { Assert.isTrue(mapper != Object.class, "The mapper cannot be object class!"); @@ -281,17 +315,41 @@ public abstract class AbstractContextRepository extends AbstractRepositor } ConfiguredRepository configuredRepository = new ConfiguredRepository( - entityPropertyChain, entityDefinition, entityMapper, entityAssembler, (AbstractRepository) repository); + this, entityPropertyChain, entityDefinition, entityMapper, entityAssembler, + (AbstractRepository) repository); - return processConfiguredRepository(configuredRepository); + return postProcessRepository(configuredRepository); } - protected boolean filterEntityClass(Class entityClass) { - String className = entityClass.getName(); - return className.startsWith("java.lang.") || className.startsWith("java.util."); - } + protected ConfiguredRepository postProcessRepository(ConfiguredRepository configuredRepository) { + EntityDefinition entityDefinition = configuredRepository.getEntityDefinition(); + Set fieldNames = entityDefinition.getFieldNames(); + Map entityPropertyChainMap = entityDefinition.getEntityPropertyChainMap(); + + String prefixAccessPath = entityDefinition.getAccessPath() + "/"; + List accessPaths = allEntityPropertyChainMap.keySet().stream().filter(key -> + key.startsWith(prefixAccessPath)).collect(Collectors.toList()); + + for (String accessPath : accessPaths) { + String lastAccessPath = PathUtils.getLastAccessPath(accessPath); + EntityPropertyChain lastEntityPropertyChain = entityPropertyChainMap.get(lastAccessPath); + EntityPropertyChain entityPropertyChain = allEntityPropertyChainMap.get(accessPath); + + fieldNames.add(entityPropertyChain.getFieldName()); + + EntityPropertyChain relativeEntityPropertyChain = new EntityPropertyChain( + lastEntityPropertyChain, entityPropertyChain); + entityPropertyChainMap.put(accessPath, relativeEntityPropertyChain); + } + + for (BindingDefinition bindingDefinition : entityDefinition.getAllBindingDefinitions()) { + String accessPath = prefixAccessPath + bindingDefinition.getFieldAttribute(); + EntityPropertyChain entityPropertyChain = entityPropertyChainMap.get(accessPath); + Assert.notNull(entityPropertyChain, "The field entity property cannot be null!"); + entityPropertyChain.initialize(); + bindingDefinition.setFieldEntityPropertyChain(entityPropertyChain); + } - protected ConfiguredRepository processConfiguredRepository(ConfiguredRepository configuredRepository) { return configuredRepository; } diff --git a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/repository/AbstractDelegateRepository.java b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/repository/AbstractDelegateRepository.java index 324b135d1ecca6b4c1a9a73b7724a739b1c4e475..f635c32a361e209c25ceab8a9773d20b9e4c4a3c 100644 --- a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/repository/AbstractDelegateRepository.java +++ b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/repository/AbstractDelegateRepository.java @@ -1,33 +1,44 @@ package com.gitee.spring.domain.core.repository; -import com.gitee.spring.domain.core.entity.EntityPropertyChain; +import lombok.Data; +import lombok.EqualsAndHashCode; +import org.springframework.util.ReflectionUtils; -import java.lang.reflect.Field; import java.util.*; +@Data +@EqualsAndHashCode(callSuper = false) public abstract class AbstractDelegateRepository extends AbstractContextRepository { - protected Map fieldEntityPropertyChainMap = new LinkedHashMap<>(); - protected List delegateConfiguredRepositories = new ArrayList<>(); + protected Map, AbstractDelegateRepository> delegateRepositoryMap = new LinkedHashMap<>(); @Override - protected EntityPropertyChain newEntityPropertyChain(Class lastEntityClass, Field declaredField, - String accessPath, Class entityClass, String fieldName) { - EntityPropertyChain entityPropertyChain = super.newEntityPropertyChain(lastEntityClass, declaredField, accessPath, entityClass, fieldName); - fieldEntityPropertyChainMap.putIfAbsent(fieldName, entityPropertyChain); - return entityPropertyChain; + public void afterPropertiesSet() throws Exception { + super.afterPropertiesSet(); + resolveDelegateRepositoryMap(); } - @Override - protected ConfiguredRepository processConfiguredRepository(ConfiguredRepository configuredRepository) { - if (configuredRepository.getRepository() instanceof AbstractDelegateRepository) { - delegateConfiguredRepositories.add(configuredRepository); - } - return super.processConfiguredRepository(configuredRepository); + protected void resolveDelegateRepositoryMap() { + delegateRepositoryMap.put(entityClass, this); + ReflectionUtils.doWithLocalFields(this.getClass(), declaredField -> { + Class fieldClass = declaredField.getType(); + if (AbstractDelegateRepository.class.isAssignableFrom(fieldClass)) { + Object bean = applicationContext.getBean(fieldClass); + AbstractDelegateRepository repository = (AbstractDelegateRepository) bean; + Class fieldEntityClass = repository.getEntityClass(); + if (entityClass.isAssignableFrom(fieldEntityClass)) { + delegateRepositoryMap.put(fieldEntityClass, repository); + } + } + }); + } + + protected AbstractDelegateRepository adaptiveRepository(Class entityClass) { + return delegateRepositoryMap.get(entityClass); } protected AbstractDelegateRepository adaptiveRepository(Object rootEntity) { - return this; + return adaptiveRepository(rootEntity.getClass()); } } 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 e83e932e7bd5be46e20bf56b84b9d05b7348d5c6..1a6ecb3b14ce1bd6fc285db447c56c44e72ff713 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 @@ -5,10 +5,12 @@ import cn.hutool.core.lang.Assert; import com.gitee.spring.domain.core.api.EntityCriterion; import com.gitee.spring.domain.core.api.EntityMapper; import com.gitee.spring.domain.core.api.EntityProperty; +import com.gitee.spring.domain.core.constants.Operator; import com.gitee.spring.domain.core.entity.*; import java.util.Collection; import java.util.List; +import java.util.Set; public abstract class AbstractGenericRepository extends AbstractDelegateRepository { @@ -25,12 +27,12 @@ public abstract class AbstractGenericRepository extends AbstractDelegateR AbstractDelegateRepository abstractDelegateRepository = adaptiveRepository(rootEntity); for (ConfiguredRepository configuredRepository : abstractDelegateRepository.getSubRepositories()) { EntityPropertyChain entityPropertyChain = configuredRepository.getEntityPropertyChain(); - EntityProperty lastEntityProperty = entityPropertyChain.getLastEntityPropertyChain(); - Object lastEntity = lastEntityProperty == null ? rootEntity : lastEntityProperty.getValue(rootEntity); - if (lastEntity != null && isMatchScenes(configuredRepository, boundedContext)) { - EntityExample entityExample = newExampleByContext(configuredRepository, boundedContext, rootEntity); + EntityPropertyChain lastEntityPropertyChain = entityPropertyChain.getLastEntityPropertyChain(); + Object lastEntity = lastEntityPropertyChain == null ? rootEntity : lastEntityPropertyChain.getValue(rootEntity); + if (lastEntity != null && isMatchScenes(boundedContext, configuredRepository)) { + EntityExample entityExample = newExampleByContext(boundedContext, rootEntity, configuredRepository); if (entityExample.isDirtyQuery()) { - List entities = configuredRepository.selectByExample(boundedContext, entityExample.buildExample()); + List entities = configuredRepository.selectByExample(boundedContext, entityExample); Object entity = convertManyToOneEntity(configuredRepository, entities); if (entity != null) { EntityProperty entityProperty = entityPropertyChain.getEntityProperty(); @@ -41,10 +43,16 @@ public abstract class AbstractGenericRepository extends AbstractDelegateR } } - protected boolean isMatchScenes(ConfiguredRepository configuredRepository, BoundedContext boundedContext) { + protected boolean isMatchScenes(BoundedContext boundedContext, ConfiguredRepository configuredRepository) { EntityDefinition entityDefinition = configuredRepository.getEntityDefinition(); - String[] sceneAttribute = entityDefinition.getSceneAttribute(); - if (sceneAttribute.length == 0) return true; + Set sceneAttribute = entityDefinition.getSceneAttribute(); + return isMatchScenes(boundedContext, sceneAttribute); + } + + protected boolean isMatchScenes(BoundedContext boundedContext, Set sceneAttribute) { + if (sceneAttribute.isEmpty()) { + return true; + } for (String scene : sceneAttribute) { if (boundedContext.containsKey(scene)) { return true; @@ -53,22 +61,22 @@ public abstract class AbstractGenericRepository extends AbstractDelegateR return false; } - protected EntityExample newExampleByContext(ConfiguredRepository configuredRepository, BoundedContext boundedContext, Object rootEntity) { + protected EntityExample newExampleByContext(BoundedContext boundedContext, Object rootEntity, ConfiguredRepository configuredRepository) { EntityDefinition entityDefinition = configuredRepository.getEntityDefinition(); EntityMapper entityMapper = configuredRepository.getEntityMapper(); - EntityExample entityExample = entityMapper.newExample(entityDefinition, boundedContext); - for (BindingDefinition bindingDefinition : entityDefinition.getBindingDefinitions()) { - Object boundValue = getBoundValue(bindingDefinition, boundedContext, rootEntity); + EntityExample entityExample = entityMapper.newExample(boundedContext, entityDefinition); + for (BindingDefinition bindingDefinition : entityDefinition.getAllBindingDefinitions()) { + Object boundValue = getBoundValue(boundedContext, rootEntity, bindingDefinition); if (boundValue != null) { - String fieldAliasAttribute = bindingDefinition.getFieldAliasAttribute(); - EntityCriterion entityCriterion = entityMapper.newEqualCriterion(fieldAliasAttribute, boundValue); + String aliasAttribute = bindingDefinition.getAliasAttribute(); + EntityCriterion entityCriterion = entityMapper.newCriterion(aliasAttribute, Operator.EQ, boundValue); entityExample.addCriterion(entityCriterion); } } return entityExample; } - protected Object getBoundValue(BindingDefinition bindingDefinition, BoundedContext boundedContext, Object rootEntity) { + protected Object getBoundValue(BoundedContext boundedContext, Object rootEntity, BindingDefinition bindingDefinition) { Object boundValue; if (bindingDefinition.isFromContext()) { String bindAttribute = bindingDefinition.getBindAttribute(); @@ -93,20 +101,24 @@ public abstract class AbstractGenericRepository extends AbstractDelegateR @Override @SuppressWarnings("unchecked") public List selectByExample(BoundedContext boundedContext, Object example) { - List entities = rootRepository.selectByExample(boundedContext, example); - entities.forEach(entity -> handleRootEntity(boundedContext, entity)); - return (List) entities; + List rootEntities = rootRepository.selectByExample(boundedContext, example); + handleRootEntities(boundedContext, rootEntities); + return (List) rootEntities; } @Override public T selectPageByExample(BoundedContext boundedContext, Object example, Object page) { T dataPage = rootRepository.selectPageByExample(boundedContext, example, page); EntityMapper entityMapper = rootRepository.getEntityMapper(); - List entities = entityMapper.getDataFromPage(dataPage); - entities.forEach(entity -> handleRootEntity(boundedContext, entity)); + List rootEntities = entityMapper.getDataFromPage(dataPage); + handleRootEntities(boundedContext, rootEntities); return dataPage; } + protected void handleRootEntities(BoundedContext boundedContext, List rootEntities) { + rootEntities.forEach(rootEntity -> handleRootEntity(boundedContext, rootEntity)); + } + @Override public int insert(BoundedContext boundedContext, E entity) { Assert.notNull(entity, "The entity cannot be null!"); @@ -115,7 +127,7 @@ public abstract class AbstractGenericRepository extends AbstractDelegateR for (ConfiguredRepository configuredRepository : abstractDelegateRepository.getOrderedRepositories()) { EntityPropertyChain entityPropertyChain = configuredRepository.getEntityPropertyChain(); Object targetEntity = entityPropertyChain == null ? entity : entityPropertyChain.getValue(entity); - if (targetEntity != null && isMatchScenes(configuredRepository, boundedContext)) { + if (targetEntity != null && isMatchScenes(boundedContext, configuredRepository)) { if (targetEntity instanceof Collection) { for (Object eachEntity : (Collection) targetEntity) { setBoundValueByContext(configuredRepository, boundedContext, entity, eachEntity); @@ -134,9 +146,9 @@ public abstract class AbstractGenericRepository extends AbstractDelegateR protected void setBoundValueByContext(ConfiguredRepository configuredRepository, BoundedContext boundedContext, Object rootEntity, Object entity) { EntityDefinition entityDefinition = configuredRepository.getEntityDefinition(); - for (BindingDefinition bindingDefinition : entityDefinition.getBindingDefinitions()) { + for (BindingDefinition bindingDefinition : entityDefinition.getAllBindingDefinitions()) { if (!bindingDefinition.isBoundId()) { - Object boundValue = getBoundValue(bindingDefinition, boundedContext, rootEntity); + Object boundValue = getBoundValue(boundedContext, rootEntity, bindingDefinition); if (boundValue != null) { String fieldAttribute = bindingDefinition.getFieldAttribute(); BeanUtil.setFieldValue(entity, fieldAttribute, boundValue); @@ -162,7 +174,7 @@ public abstract class AbstractGenericRepository extends AbstractDelegateR for (ConfiguredRepository configuredRepository : abstractDelegateRepository.getOrderedRepositories()) { EntityPropertyChain entityPropertyChain = configuredRepository.getEntityPropertyChain(); Object targetEntity = entityPropertyChain == null ? entity : entityPropertyChain.getValue(entity); - if (targetEntity != null && isMatchScenes(configuredRepository, boundedContext)) { + if (targetEntity != null && isMatchScenes(boundedContext, configuredRepository)) { if (targetEntity instanceof Collection) { for (Object eachEntity : (Collection) targetEntity) { totalCount += configuredRepository.update(boundedContext, eachEntity); @@ -188,7 +200,7 @@ public abstract class AbstractGenericRepository extends AbstractDelegateR for (ConfiguredRepository configuredRepository : abstractDelegateRepository.getOrderedRepositories()) { EntityPropertyChain entityPropertyChain = configuredRepository.getEntityPropertyChain(); Object targetEntity = entityPropertyChain == null ? entity : entityPropertyChain.getValue(entity); - if (targetEntity != null && isMatchScenes(configuredRepository, boundedContext)) { + if (targetEntity != null && isMatchScenes(boundedContext, configuredRepository)) { if (targetEntity instanceof Collection) { for (Object eachEntity : (Collection) targetEntity) { totalCount += configuredRepository.delete(boundedContext, eachEntity); 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 49e76b696dec09df6d54f2a094ab234f50817360..58a6171d2968dc6ea4dc5999ad8c6e04f1b8654d 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 @@ -2,11 +2,15 @@ package com.gitee.spring.domain.core.repository; import com.gitee.spring.domain.core.api.EntityAssembler; import com.gitee.spring.domain.core.api.EntityMapper; +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 lombok.Getter; import lombok.Setter; +import java.util.List; + @Getter @Setter public class ConfiguredRepository extends ProxyRepository { @@ -36,4 +40,31 @@ public class ConfiguredRepository extends ProxyRepository { this.entityAssembler = configuredRepository.getEntityAssembler(); } + private Object processExample(Object example) { + if (!entityDefinition.isUseEntityExample() && example instanceof EntityExample) { + return ((EntityExample) example).buildExample(); + } + return example; + } + + @Override + public List selectByExample(BoundedContext boundedContext, Object example) { + return super.selectByExample(boundedContext, processExample(example)); + } + + @Override + public T selectPageByExample(BoundedContext boundedContext, Object example, Object page) { + return super.selectPageByExample(boundedContext, processExample(example), page); + } + + @Override + public int updateByExample(Object entity, Object example) { + return super.updateByExample(entity, processExample(example)); + } + + @Override + public int deleteByExample(Object example) { + return super.deleteByExample(processExample(example)); + } + } diff --git a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/repository/DefaultRepository.java b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/repository/DefaultRepository.java index 6791b6031194d95f580470dff39e1cc51e69e54f..b5029028af413bfc4cc98b8bb93fe4aa3d59ac10 100644 --- a/spring-domain-core/src/main/java/com/gitee/spring/domain/core/repository/DefaultRepository.java +++ b/spring-domain-core/src/main/java/com/gitee/spring/domain/core/repository/DefaultRepository.java @@ -5,6 +5,7 @@ import cn.hutool.core.lang.Assert; import com.gitee.spring.domain.core.api.EntityAssembler; import com.gitee.spring.domain.core.api.EntityCriterion; import com.gitee.spring.domain.core.api.EntityMapper; +import com.gitee.spring.domain.core.constants.Operator; import com.gitee.spring.domain.core.entity.BoundedContext; import com.gitee.spring.domain.core.entity.EntityDefinition; import com.gitee.spring.domain.core.entity.EntityExample; @@ -34,7 +35,7 @@ public class DefaultRepository extends ProxyRepository { public Object selectByPrimaryKey(BoundedContext boundedContext, Object primaryKey) { Object persistentObject = super.selectByPrimaryKey(boundedContext, primaryKey); if (persistentObject != null) { - return entityAssembler.assemble(entityDefinition, boundedContext, persistentObject); + return entityAssembler.assemble(boundedContext, entityDefinition, persistentObject); } return null; } @@ -51,7 +52,7 @@ public class DefaultRepository extends ProxyRepository { protected List newEntities(BoundedContext boundedContext, List persistentObjects) { List entities = new ArrayList<>(); for (Object persistentObject : persistentObjects) { - Object entity = entityAssembler.assemble(entityDefinition, boundedContext, persistentObject); + Object entity = entityAssembler.assemble(boundedContext, entityDefinition, persistentObject); entities.add(entity); } return entities; @@ -73,7 +74,7 @@ public class DefaultRepository extends ProxyRepository { public int insert(BoundedContext boundedContext, Object entity) { Object primaryKey = BeanUtil.getFieldValue(entity, "id"); if (primaryKey == null) { - Object persistentObject = entityAssembler.disassemble(entityDefinition, boundedContext, entity); + Object persistentObject = entityAssembler.disassemble(boundedContext, entityDefinition, entity); if (persistentObject != null) { int count = super.insert(boundedContext, persistentObject); copyPrimaryKey(entity, persistentObject); @@ -92,10 +93,10 @@ public class DefaultRepository extends ProxyRepository { public int update(BoundedContext boundedContext, Object entity) { Object primaryKey = BeanUtil.getFieldValue(entity, "id"); if (primaryKey != null) { - Object persistentObject = entityAssembler.disassemble(entityDefinition, boundedContext, entity); + Object persistentObject = entityAssembler.disassemble(boundedContext, entityDefinition, entity); if (persistentObject != null) { - EntityExample entityExample = entityMapper.newExample(entityDefinition, boundedContext); - EntityCriterion entityCriterion = entityMapper.newEqualCriterion("id", primaryKey); + EntityExample entityExample = entityMapper.newExample(boundedContext, entityDefinition); + EntityCriterion entityCriterion = entityMapper.newCriterion("id", Operator.EQ, primaryKey); entityExample.addCriterion(entityCriterion); return super.updateByExample(persistentObject, entityExample.buildExample()); } @@ -106,7 +107,7 @@ public class DefaultRepository extends ProxyRepository { @Override public int updateByExample(Object entity, Object example) { Assert.isTrue(!(entity instanceof Collection), "The entity cannot be a collection!"); - Object persistentObject = entityAssembler.disassemble(entityDefinition, new BoundedContext(), entity); + Object persistentObject = entityAssembler.disassemble(new BoundedContext(), entityDefinition, entity); if (persistentObject != null) { return super.updateByExample(persistentObject, example); } diff --git a/spring-domain-event/pom.xml b/spring-domain-event/pom.xml index 909bc0c757ee0294e2616cd9a36d996c5b7daca2..2153143b17ace5a699608221e5cc92c0e8773bca 100644 --- a/spring-domain-event/pom.xml +++ b/spring-domain-event/pom.xml @@ -6,7 +6,7 @@ com.gitee.digital-engine spring-domain - 2.5.8 + 2.6.1 spring-domain-event diff --git a/spring-domain-event/src/main/java/com/gitee/spring/domain/event/repository/AbstractEventRepository.java b/spring-domain-event/src/main/java/com/gitee/spring/domain/event/repository/AbstractEventRepository.java index f2a7f5f3ab31da586d0beef15b90d7042925eff4..5cf40096543ae17224d7e3ffce7e1d963c95113b 100644 --- a/spring-domain-event/src/main/java/com/gitee/spring/domain/event/repository/AbstractEventRepository.java +++ b/spring-domain-event/src/main/java/com/gitee/spring/domain/event/repository/AbstractEventRepository.java @@ -1,11 +1,11 @@ package com.gitee.spring.domain.event.repository; -import com.gitee.spring.domain.core.repository.AbstractGenericRepository; +import com.gitee.spring.domain.core.repository.AbstractBatchRepository; import com.gitee.spring.domain.core.repository.ConfiguredRepository; import com.gitee.spring.domain.event.annotation.EnableEvent; import org.springframework.core.annotation.AnnotationUtils; -public abstract class AbstractEventRepository extends AbstractGenericRepository { +public abstract class AbstractEventRepository extends AbstractBatchRepository { protected boolean enableEvent; @@ -17,8 +17,8 @@ public abstract class AbstractEventRepository extends AbstractGenericRepo } @Override - protected ConfiguredRepository processConfiguredRepository(ConfiguredRepository configuredRepository) { - return enableEvent ? new EventRepository(configuredRepository, applicationContext) : super.processConfiguredRepository(configuredRepository); + protected ConfiguredRepository postProcessRepository(ConfiguredRepository configuredRepository) { + return enableEvent ? new EventRepository(configuredRepository, applicationContext) : super.postProcessRepository(configuredRepository); } } diff --git a/spring-domain-injection/pom.xml b/spring-domain-injection/pom.xml index 19ebd610dc323b234d859ce608c5286b60dc9cd0..80339ac248ddc34fd4cb1027728fd59627484e67 100644 --- a/spring-domain-injection/pom.xml +++ b/spring-domain-injection/pom.xml @@ -6,7 +6,7 @@ com.gitee.digital-engine spring-domain - 2.5.8 + 2.6.1 spring-domain-injection diff --git a/spring-domain-web/pom.xml b/spring-domain-web/pom.xml index 830daf4e17f275033976cade2e576a2cbcc74c2e..2f3baa9f1d5a126cbf64c7d6e40e26d9de187835 100644 --- a/spring-domain-web/pom.xml +++ b/spring-domain-web/pom.xml @@ -6,7 +6,7 @@ com.gitee.digital-engine spring-domain - 2.5.8 + 2.6.1 spring-domain-web