-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implement database storage for rule configurations - Remove commented-out code in `RuleEngineApplication` to clean up the class. - Introduce `RuleConfigController` to handle HTTP requests for rule configurations. - Define JPA entities: `RuleSetDefinition`, `RuleDefinition`, `ConditionDefinition`, and `ActionDefinition` for database mapping. - Create repositories for each entity to facilitate database operations. - Implement `RuleService` to manage rule configurations using the repositories. - Add `YamlRuleConfigLoader` to load rules from YAML configuration. - Update `build.gradle.kts` to include Spring Data JPA and MySQL dependencies. - Configure Spring Boot application properties for MySQL database connection. - Add SQL scripts to create necessary tables and constraints for rule configurations. * Refactor rule configuration to be stored in the database - Adjust data classes in RuleConfig to include default values for fields. - Add SQL script to create the `rule_set_definition_entity` table and insert initial data. - Remove un-used classes * Refactor rule configuration to be stored in the database - Add SQL script to create the `rule_set_definition_entity` table and insert initial * Store RuleSetDefinition in DB - Config JPA with noArg - Implement DbRuleConfigService - Implement RuleConfigController - Remove odd classes * Refactor RuleConfiguration to use Spring ConfigurationProperties * Completed save RuleConfig to DB
- Loading branch information
1 parent
839014d
commit 9da9327
Showing
15 changed files
with
315 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package trile.rule.config | ||
|
||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
import trile.rule.model.RuleSetDefinition | ||
|
||
@RestController | ||
@RequestMapping("/api/rules") | ||
class RuleConfigController( | ||
private val ruleConfigService: RuleConfigService, | ||
private val ruleSetsByUseCase: Map<String, RuleSetDefinition> | ||
) { | ||
|
||
@GetMapping | ||
fun getAllRules(): ResponseEntity<List<RuleSetDefinition>> { | ||
return ResponseEntity.ok(ruleConfigService.getAllRuleSets()) | ||
} | ||
|
||
@GetMapping("rule-set-by-use-case") | ||
fun getRuleSetByUserCase() = ruleSetsByUseCase | ||
|
||
// @PostMapping | ||
// fun createRuleSet(@RequestBody ruleSet: RuleSetDefinition): ResponseEntity<RuleSetDefinition> { | ||
// return ResponseEntity.ok(ruleService.saveRuleSet(ruleSet)) | ||
// } | ||
// | ||
// @DeleteMapping("/{id}") | ||
// fun deleteRuleSet(@PathVariable id: Long): ResponseEntity<Void> { | ||
// ruleService.deleteRuleSet(id) | ||
// return ResponseEntity.noContent().build() | ||
// } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package trile.rule.config | ||
|
||
import trile.rule.model.RuleSetDefinition | ||
|
||
|
||
interface RuleConfigService { | ||
fun getAllRuleSets(): List<RuleSetDefinition> | ||
fun createRuleSet(ruleSet: RuleSetDefinition): RuleSetDefinition | ||
fun updateRuleSet(id: Long, ruleSet: RuleSetDefinition): RuleSetDefinition? | ||
fun deleteRuleSet(id: Long) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package trile.rule.config | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import trile.rule.model.RuleSetDefinition | ||
|
||
@Configuration | ||
data class RuleConfiguration( | ||
private val ruleProperties: RuleProperties, | ||
private val ruleConfigService: RuleConfigService | ||
) { | ||
|
||
@Bean | ||
fun ruleSetsByUseCase(): Map<String, RuleSetDefinition> { | ||
return when (ruleProperties.source) { | ||
RuleConfigurationSource.YAML -> ruleProperties.ruleSets | ||
RuleConfigurationSource.DB -> ruleConfigService.getAllRuleSets() | ||
}.associateBy { it.useCase } | ||
} | ||
} | ||
|
||
@Configuration | ||
@ConfigurationProperties(prefix = "rules") | ||
data class RuleProperties( | ||
var source: RuleConfigurationSource = RuleConfigurationSource.YAML, | ||
var ruleSets: List<RuleSetDefinition> = emptyList() | ||
) | ||
|
||
enum class RuleConfigurationSource { | ||
DB, | ||
YAML | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//package trile.rule.config | ||
// | ||
//import jakarta.transaction.Transactional | ||
//import org.springframework.boot.CommandLineRunner | ||
//import org.springframework.stereotype.Component | ||
//import trile.common.Log | ||
//import trile.rule.config.db.RuleSetDefinitionRepository | ||
//import trile.rule.model.RuleSetDefinition | ||
// | ||
//@Component | ||
//class RuleDataInitializer( | ||
// private val ruleConfigService: RuleConfigService, | ||
// private val ruleProperties: RuleProperties, | ||
// private val ruleSetDefinitionRepository: RuleSetDefinitionRepository, | ||
// private val ruleSetsByUseCase: Map<String, RuleSetDefinition>, | ||
//) : CommandLineRunner, Log { | ||
// | ||
// @Transactional | ||
// override fun run(vararg args: String?) { | ||
// l.info("Loaded rule config: [$ruleSetsByUseCase]") | ||
// | ||
// ruleSetDefinitionRepository.findAll().forEach { ruleSetDefinitionRepository.deleteById(it.id) } | ||
// ruleProperties.ruleSets.forEach { ruleConfigService.createRuleSet(it) } | ||
// } | ||
//} |
41 changes: 41 additions & 0 deletions
41
src/main/kotlin/trile/rule/config/db/DbRuleConfigService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package trile.rule.config.db | ||
|
||
import org.springframework.stereotype.Service | ||
import trile.common.Log | ||
import trile.rule.config.RuleConfigService | ||
import trile.rule.model.RuleSetDefinition | ||
|
||
@Service | ||
//@ConditionalOnProperty(prefix = "rules", name = ["source"], havingValue = "DB") | ||
class DbRuleConfigService( | ||
private val ruleSetRepository: RuleSetDefinitionRepository, | ||
) : RuleConfigService, Log { | ||
|
||
|
||
override fun getAllRuleSets(): List<RuleSetDefinition> { | ||
return ruleSetRepository.findAll().map { entity -> | ||
RuleSetDefinition(useCase = entity.useCase, name = entity.name, rules = entity.rules) | ||
} | ||
} | ||
|
||
|
||
override fun createRuleSet(ruleSet: RuleSetDefinition): RuleSetDefinition { | ||
val entity = RuleSetDefinitionEntity(useCase = ruleSet.useCase, name = ruleSet.name, rules = ruleSet.rules) | ||
val savedEntity = ruleSetRepository.save(entity) | ||
return RuleSetDefinition(useCase = ruleSet.useCase, name = savedEntity.name, rules = savedEntity.rules) | ||
} | ||
|
||
override fun updateRuleSet(id: Long, ruleSet: RuleSetDefinition): RuleSetDefinition { | ||
return ruleSetRepository.findById(id).map { existingEntity -> | ||
existingEntity.useCase = ruleSet.useCase | ||
existingEntity.name = ruleSet.name | ||
existingEntity.rules = ruleSet.rules | ||
val updatedEntity = ruleSetRepository.save(existingEntity) | ||
RuleSetDefinition(useCase = updatedEntity.useCase, name = updatedEntity.name, rules = updatedEntity.rules) | ||
}.orElseThrow() | ||
} | ||
|
||
override fun deleteRuleSet(id: Long) { | ||
ruleSetRepository.deleteById(id) | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/kotlin/trile/rule/config/db/RuleConfigEntities.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package trile.rule.config.db | ||
|
||
|
||
import jakarta.persistence.Column | ||
import jakarta.persistence.Entity | ||
import jakarta.persistence.GeneratedValue | ||
import jakarta.persistence.GenerationType | ||
import jakarta.persistence.Id | ||
import org.hibernate.annotations.JdbcTypeCode | ||
import org.hibernate.type.SqlTypes | ||
import trile.rule.model.RuleDefinition | ||
|
||
@Entity | ||
data class RuleSetDefinitionEntity( | ||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
var id: Long = 0, | ||
|
||
@Column(name = "use-case") | ||
var useCase: String, | ||
|
||
var name: String, | ||
|
||
@JdbcTypeCode(SqlTypes.JSON) | ||
@Column(name = "rules", columnDefinition = "json") | ||
var rules: List<RuleDefinition> | ||
) | ||
|
5 changes: 5 additions & 0 deletions
5
src/main/kotlin/trile/rule/config/db/RuleConfigRepositories.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package trile.rule.config.db | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository | ||
|
||
interface RuleSetDefinitionRepository : JpaRepository<RuleSetDefinitionEntity, Long> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.