From 23944c8f7a7122dab8c473ee8c98cd4637dc2851 Mon Sep 17 00:00:00 2001 From: "felix.ma" <1374147274@qq.com> Date: Thu, 27 Aug 2020 21:44:09 +0800 Subject: [PATCH] =?UTF-8?q?uuid=E9=95=BF=E5=BA=A632=E4=BD=8D=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0h2=E6=95=B0=E6=8D=AE=E5=BA=93=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/application-dev.yaml | 28 ++++++++++++ .../src/main/resources/application-test.yaml | 34 ++++++++++++++ .../src/main/resources/application.yaml | 29 +----------- dao/pom.xml | 5 ++- .../entity/ComponentDescriptorEntity.java | 6 +++ dao/src/main/resources/h2/data.sql | 1 + dao/src/main/resources/h2/schema.sql | 44 +++++++++++++++++++ dao/src/main/resources/sql/schema-entity.sql | 16 +++---- .../AnnotationComponentDiscoveryService.java | 43 ++++++++++++------ 9 files changed, 156 insertions(+), 50 deletions(-) create mode 100644 application/src/main/resources/application-dev.yaml create mode 100644 application/src/main/resources/application-test.yaml create mode 100644 dao/src/main/resources/h2/data.sql create mode 100644 dao/src/main/resources/h2/schema.sql diff --git a/application/src/main/resources/application-dev.yaml b/application/src/main/resources/application-dev.yaml new file mode 100644 index 0000000..559b49a --- /dev/null +++ b/application/src/main/resources/application-dev.yaml @@ -0,0 +1,28 @@ +logging: + level: + felix: debug +server: + port: 8081 + +spring: + datasource: + driver-class-name: org.postgresql.Driver + url: jdbc:postgresql://felix.docker:5432/rule-engine + password: postgres + username: postgres + +mybatis-plus: + mapper-locations: classpath*:mapper/*/*Mapper.xml + global-config: + refresh-mapper: true + db-config: + table-underline: true + id-type: uuid + banner: false + type-handlers-package: felix.rule.data + type-enums-package: felix.rule.data + type-aliases-package: felix.rule.data + configuration: + map-underscore-to-camel-case: true + cache-enabled: false + log-impl: org.apache.ibatis.logging.stdout.StdOutImpl diff --git a/application/src/main/resources/application-test.yaml b/application/src/main/resources/application-test.yaml new file mode 100644 index 0000000..dd085c9 --- /dev/null +++ b/application/src/main/resources/application-test.yaml @@ -0,0 +1,34 @@ +logging: + level: + felix: debug +server: + port: 8081 + +spring: + datasource: + url: jdbc:h2:mem:rule-engine + driver-class-name: org.h2.Driver + username: sa + password: + schema: classpath:h2/schema.sql + data: classpath:h2/data.sql + h2: + console: + path: /h2-console + enabled: true + +mybatis-plus: + mapper-locations: classpath*:mapper/*/*Mapper.xml + global-config: + refresh-mapper: true + db-config: + table-underline: true + id-type: uuid + banner: false + type-handlers-package: felix.rule.data + type-enums-package: felix.rule.data + type-aliases-package: felix.rule.data + configuration: + map-underscore-to-camel-case: true + cache-enabled: false + log-impl: org.apache.ibatis.logging.stdout.StdOutImpl diff --git a/application/src/main/resources/application.yaml b/application/src/main/resources/application.yaml index 559b49a..03c30d3 100644 --- a/application/src/main/resources/application.yaml +++ b/application/src/main/resources/application.yaml @@ -1,28 +1,3 @@ -logging: - level: - felix: debug -server: - port: 8081 - spring: - datasource: - driver-class-name: org.postgresql.Driver - url: jdbc:postgresql://felix.docker:5432/rule-engine - password: postgres - username: postgres - -mybatis-plus: - mapper-locations: classpath*:mapper/*/*Mapper.xml - global-config: - refresh-mapper: true - db-config: - table-underline: true - id-type: uuid - banner: false - type-handlers-package: felix.rule.data - type-enums-package: felix.rule.data - type-aliases-package: felix.rule.data - configuration: - map-underscore-to-camel-case: true - cache-enabled: false - log-impl: org.apache.ibatis.logging.stdout.StdOutImpl + profiles: + active: test diff --git a/dao/pom.xml b/dao/pom.xml index e78dbc4..cc2abcc 100644 --- a/dao/pom.xml +++ b/dao/pom.xml @@ -20,7 +20,10 @@ org.postgresql postgresql - + + com.h2database + h2 + felix-rule engine-api diff --git a/dao/src/main/java/felix/rule/data/entity/ComponentDescriptorEntity.java b/dao/src/main/java/felix/rule/data/entity/ComponentDescriptorEntity.java index bb3e2cc..67c5180 100644 --- a/dao/src/main/java/felix/rule/data/entity/ComponentDescriptorEntity.java +++ b/dao/src/main/java/felix/rule/data/entity/ComponentDescriptorEntity.java @@ -6,11 +6,13 @@ import felix.rule.data.plugin.ComponentDescriptor; import felix.rule.data.plugin.ComponentType; import lombok.Data; import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; import org.springframework.beans.BeanUtils; @EqualsAndHashCode(callSuper = true) @TableName("component_descriptor") @Data +@NoArgsConstructor public class ComponentDescriptorEntity extends BaseEntity { private ComponentType type; @@ -21,6 +23,10 @@ public class ComponentDescriptorEntity extends BaseEntity { private JsonNode configurationDescriptor; + public ComponentDescriptorEntity(ComponentDescriptor componentDescriptor) { + BeanUtils.copyProperties(componentDescriptor, this); + } + @Override public ComponentDescriptor toData() { ComponentDescriptor componentDescriptor = new ComponentDescriptor(); diff --git a/dao/src/main/resources/h2/data.sql b/dao/src/main/resources/h2/data.sql new file mode 100644 index 0000000..ab290eb --- /dev/null +++ b/dao/src/main/resources/h2/data.sql @@ -0,0 +1 @@ +select 1; diff --git a/dao/src/main/resources/h2/schema.sql b/dao/src/main/resources/h2/schema.sql new file mode 100644 index 0000000..340bd3c --- /dev/null +++ b/dao/src/main/resources/h2/schema.sql @@ -0,0 +1,44 @@ +CREATE TABLE relation +( + from_id varchar(32), + to_id varchar(32), + relation_type varchar(255), + type varchar(255), + group_type varchar(255), + additional_info varchar(2000), + CONSTRAINT relation_pkey PRIMARY KEY (from_id, to_id, group_type, relation_type) +) +; + +CREATE TABLE component_descriptor +( + id varchar(32) primary key, + type varchar(255), + name varchar(255), + clazz varchar unique, + configuration_descriptor varchar(2000) +) +; + +CREATE TABLE rule_chain +( + id varchar(32) primary key, + rule_id varchar(32), + name varchar(255), + additional_info varchar(2000), + configuration varchar(2000), + first_rule_node_id varchar(32), + debug_mode bool +) +; +CREATE TABLE rule_node +( + id varchar(32) primary key, + rule_chain_id varchar(32), + name varchar(255), + type varchar(255), + debug_mode bool, + additional_info varchar(2000), + configuration varchar(2000) +) +; diff --git a/dao/src/main/resources/sql/schema-entity.sql b/dao/src/main/resources/sql/schema-entity.sql index b2b8f7b..bb088b3 100644 --- a/dao/src/main/resources/sql/schema-entity.sql +++ b/dao/src/main/resources/sql/schema-entity.sql @@ -1,7 +1,7 @@ CREATE TABLE relation ( - from_id varchar(31), - to_id varchar(31), + from_id varchar(32), + to_id varchar(32), relation_type varchar(255), type varchar(255), group_type varchar(255), @@ -12,7 +12,7 @@ CREATE TABLE relation CREATE TABLE component_descriptor ( - id varchar(31) primary key, + id varchar(32) primary key, type varchar(255), name varchar(255), clazz varchar unique, @@ -22,19 +22,19 @@ CREATE TABLE component_descriptor CREATE TABLE rule_chain ( - id varchar(31) primary key, - rule_id varchar(31), + id varchar(32) primary key, + rule_id varchar(32), name varchar(255), additional_info jsonb, configuration jsonb, - first_rule_node_id varchar(31), + first_rule_node_id varchar(32), debug_mode bool ) ; CREATE TABLE rule_node ( - id varchar(31) primary key, - rule_chain_id varchar(31), + id varchar(32) primary key, + rule_chain_id varchar(32), name varchar(255), type varchar(255), debug_mode bool, diff --git a/service/src/main/java/felix/rule/service/impl/AnnotationComponentDiscoveryService.java b/service/src/main/java/felix/rule/service/impl/AnnotationComponentDiscoveryService.java index 6838ecc..ecbae86 100644 --- a/service/src/main/java/felix/rule/service/impl/AnnotationComponentDiscoveryService.java +++ b/service/src/main/java/felix/rule/service/impl/AnnotationComponentDiscoveryService.java @@ -1,11 +1,14 @@ package felix.rule.service.impl; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import felix.rule.api.NodeConfiguration; import felix.rule.api.NodeDefinition; import felix.rule.api.RuleNode; +import felix.rule.data.entity.ComponentDescriptorEntity; +import felix.rule.data.mapper.ComponentDescriptorMapper; import felix.rule.data.plugin.ComponentDescriptor; import felix.rule.data.plugin.ComponentType; import felix.rule.service.ComponentDiscoveryService; @@ -34,6 +37,9 @@ public class AnnotationComponentDiscoveryService implements ComponentDiscoverySe @Autowired private Environment environment; + @Autowired + private ComponentDescriptorMapper componentDescriptorMapper; + private Map components = new HashMap<>(); private Map> componentsMap = new HashMap<>(); @@ -131,20 +137,29 @@ public class AnnotationComponentDiscoveryService implements ComponentDiscoverySe log.error("Can't initialize component {}, due to {}", def.getBeanClassName(), e.getMessage(), e); throw new RuntimeException(e); } - // TODO 设置id -// ComponentDescriptor persistedComponent = componentDescriptorService.findByClazz(TenantId.SYS_TENANT_ID, clazzName); -// if (persistedComponent == null) { -// log.info("Persisting new component: {}", scannedComponent); -// scannedComponent = componentDescriptorService.saveComponent(TenantId.SYS_TENANT_ID, scannedComponent); -// } else if (scannedComponent.equals(persistedComponent)) { -// log.info("Component is already persisted: {}", persistedComponent); -// scannedComponent = persistedComponent; -// } else { -// log.info("Component {} will be updated to {}", persistedComponent, scannedComponent); -// componentDescriptorService.deleteByClazz(TenantId.SYS_TENANT_ID, persistedComponent.getClazz()); -// scannedComponent.setId(persistedComponent.getId()); -// scannedComponent = componentDescriptorService.saveComponent(TenantId.SYS_TENANT_ID, scannedComponent); -// } + ComponentDescriptor persistedComponent = null; + ComponentDescriptorEntity componentDescriptorEntity = componentDescriptorMapper.selectOne(Wrappers.lambdaQuery() + .eq(ComponentDescriptorEntity::getClazz, clazzName)); + if (componentDescriptorEntity != null) { + persistedComponent = componentDescriptorEntity.toData(); + } + if (persistedComponent == null) { + log.info("Persisting new component: {}", scannedComponent); + componentDescriptorEntity = new ComponentDescriptorEntity(scannedComponent); + componentDescriptorMapper.insert(componentDescriptorEntity); + } else if (scannedComponent.equals(persistedComponent)) { + log.info("Component is already persisted: {}", persistedComponent); + scannedComponent = persistedComponent; + } else { + log.info("Component {} will be updated to {}", persistedComponent, scannedComponent); + componentDescriptorMapper.delete(Wrappers.lambdaUpdate() + .eq(ComponentDescriptorEntity::getClazz, persistedComponent.getClazz())); + scannedComponent.setId(persistedComponent.getId()); + componentDescriptorEntity = new ComponentDescriptorEntity(scannedComponent); + componentDescriptorMapper.insert(componentDescriptorEntity); + } + // 设置id + scannedComponent.setId(componentDescriptorEntity.getId()); return scannedComponent; } -- Gitee