diff --git a/application/src/main/resources/application-dev.yaml b/application/src/main/resources/application-dev.yaml new file mode 100644 index 0000000000000000000000000000000000000000..559b49a978c35de79f40188068c070262f7a264e --- /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 0000000000000000000000000000000000000000..dd085c9587212dc9415588629c93146333c840a9 --- /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 559b49a978c35de79f40188068c070262f7a264e..03c30d374adcae02687315ac99ad6aa3d4b49483 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 e78dbc40ed815f5c85003bebc8cc19c98544aded..cc2abcc2c964dfdfa3a80068eb8ee791a930e991 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 bb3e2cc1dd19f382b71c6bd8a8fe8843d72ec791..67c5180bfc6bdcf37a84ddbaa17b948521f21295 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 0000000000000000000000000000000000000000..ab290eb4c1f6e10ef89c4d95c1ede4471c4cad69 --- /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 0000000000000000000000000000000000000000..340bd3cb789d0aa384998639de864bc26da311d8 --- /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 b2b8f7bba9649b38d358bc4fc548cf7a473dc3f0..bb088b3332806d2133c476e8e4eafbe8c379fafe 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 6838ecc50933946830a7587e0f8fa4fce6f131a9..ecbae8628194079d7b30b4e124833555f97cca3c 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; }