Skip to content

Commit

Permalink
Remove Logger and Dates usages
Browse files Browse the repository at this point in the history
  • Loading branch information
enricocolasante committed Dec 14, 2023
1 parent 29c7887 commit 32a4e75
Show file tree
Hide file tree
Showing 30 changed files with 826 additions and 832 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ kotlin {
val commonMain by getting {
dependencies {
implementation("org.hisp.dhis.lib.expression:expression-parser:1.1.0-SNAPSHOT")
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.4.1")
}
}
val commonTest by getting {
Expand Down
5 changes: 5 additions & 0 deletions src/commonMain/kotlin/org/hisp/dhis/rules/Logger.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.hisp.dhis.rules

data class Logger(val severe: (String) -> Unit, val fine: (String) -> Unit)

expect fun createLogger(className: String): Logger
5 changes: 5 additions & 0 deletions src/jsMain/kotlin/org/hisp/dhis/rules/Logger.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.hisp.dhis.rules

actual fun createLogger(className: String): Logger {
return Logger({message -> println(message)}, {message: String -> println(message)})
}
6 changes: 6 additions & 0 deletions src/jvmMain/kotlin/org/hisp/dhis/rules/Logger.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.hisp.dhis.rules

actual fun createLogger(className: String): Logger{
val javaLogger = java.util.logging.Logger.getLogger(className)
return Logger({message -> javaLogger.severe(message)}, {message: String -> javaLogger.fine(message)})
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import org.hisp.dhis.rules.models.*
import org.hisp.dhis.rules.models.RuleEvaluationResult.Companion.errorRule
import org.hisp.dhis.rules.models.RuleEvaluationResult.Companion.evaluatedResult
import org.hisp.dhis.rules.models.RuleEvaluationResult.Companion.notEvaluatedResult
import java.util.logging.Level
import java.util.logging.Logger

class RuleConditionEvaluator {
fun getEvaluatedAndErrorRuleEffects(
Expand Down Expand Up @@ -134,7 +132,7 @@ class RuleConditionEvaluator {
" with condition (" + rule.condition() + ")" +
" raised an unexpected exception: " + e.message
}
log.log(Level.SEVERE, errorMessage)
log.severe(errorMessage)
ruleEvaluationResults.add(errorRule(rule, errorMessage))
}

Expand Down Expand Up @@ -205,6 +203,6 @@ class RuleConditionEvaluator {
}

companion object {
private val log = Logger.getLogger(RuleConditionEvaluator::class.java.name)
private val log = createLogger(RuleConditionEvaluator::class.java.name)
}
}
1 change: 0 additions & 1 deletion src/jvmMain/kotlin/org/hisp/dhis/rules/RuleEngine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import org.hisp.dhis.lib.expression.spi.IllegalExpressionException
import org.hisp.dhis.lib.expression.spi.ParseException
import org.hisp.dhis.lib.expression.spi.ValueType
import org.hisp.dhis.rules.models.*
import java.util.Date

data class RuleEngine(
val executionContext: RuleEngineContext,
Expand Down
10 changes: 1 addition & 9 deletions src/jvmMain/kotlin/org/hisp/dhis/rules/RuleVariableValue.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,12 @@ package org.hisp.dhis.rules
import org.hisp.dhis.lib.expression.spi.ValueType
import org.hisp.dhis.lib.expression.spi.VariableValue
import org.hisp.dhis.rules.models.RuleValueType
import java.text.SimpleDateFormat
import java.util.Date

fun getFormattedDate(date: Date): String {
val format = SimpleDateFormat()
format.applyPattern("yyyy-MM-dd")
return format.format(date)
}

data class RuleVariableValue(
val type: RuleValueType,
val value: String? = null,
val candidates: List<String> = listOf(),
val eventDate: String? = getFormattedDate(Date())
val eventDate: String? = null
) {
fun toVariableValue(): VariableValue {
return VariableValue(valueType(), value, candidates, eventDate)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package org.hisp.dhis.rules

import kotlinx.datetime.LocalDate
import org.hisp.dhis.rules.models.*
import org.hisp.dhis.rules.utils.RuleEngineUtils
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
import kotlin.collections.ArrayList
import kotlin.collections.HashMap

class RuleVariableValueMapBuilder private constructor() {
val dateFormat: SimpleDateFormat

private val allConstantValues: MutableMap<String, String>

private val ruleVariables: MutableList<RuleVariable>
Expand All @@ -21,8 +17,6 @@ class RuleVariableValueMapBuilder private constructor() {
private var triggerEnvironment: TriggerEnvironment? = null

init {
this.dateFormat = SimpleDateFormat(DATE_PATTERN, Locale.US)

// collections used for construction of resulting variable value map
ruleVariables = ArrayList()
ruleEvents = ArrayList()
Expand Down Expand Up @@ -64,7 +58,7 @@ class RuleVariableValueMapBuilder private constructor() {
fun ruleEvents(ruleEvents: List<RuleEvent>): RuleVariableValueMapBuilder {
check(!isEventInList(ruleEvents, ruleEvent)) {
String.format(
Locale.US, "ruleEvent %s is already set " +
"ruleEvent %s is already set " +
"as a target, but also present in the context: events list", ruleEvent!!.event()
)
}
Expand Down Expand Up @@ -181,55 +175,55 @@ class RuleVariableValueMapBuilder private constructor() {

private fun buildEnvironmentVariables(): Map<String, RuleVariableValue> {
val valueMap: MutableMap<String, RuleVariableValue> = HashMap()
val currentDate = dateFormat.format(Date())
val currentDate = LocalDate.Companion.currentDate()
valueMap[RuleEngineUtils.ENV_VAR_CURRENT_DATE] = RuleVariableValue(
RuleValueType.TEXT,
currentDate,
listOf(currentDate),
currentDate
currentDate.toString(),
listOf(currentDate.toString()),
currentDate.toString()
)
if (triggerEnvironment != null) {
val environment = triggerEnvironment!!.clientName
valueMap[RuleEngineUtils.ENV_VAR_ENVIRONMENT] = RuleVariableValue(
RuleValueType.TEXT,
environment,
listOf(environment),
currentDate
currentDate.toString()
)
}
if (!ruleEvents.isEmpty()) {
valueMap[RuleEngineUtils.ENV_VAR_EVENT_COUNT] = RuleVariableValue(
RuleValueType.NUMERIC, ruleEvents.size.toString(),
listOf(ruleEvents.size.toString()), currentDate
listOf(ruleEvents.size.toString()), currentDate.toString()
)
}
if (ruleEnrollment != null) {
valueMap[RuleEngineUtils.ENV_VAR_ENROLLMENT_ID] = RuleVariableValue(
RuleValueType.TEXT, ruleEnrollment!!.enrollment,
listOf(ruleEnrollment!!.enrollment), currentDate
listOf(ruleEnrollment!!.enrollment), currentDate.toString()
)
valueMap[RuleEngineUtils.ENV_VAR_ENROLLMENT_COUNT] = RuleVariableValue(
RuleValueType.NUMERIC, "1",
listOf("1"), currentDate
listOf("1"), currentDate.toString()
)
valueMap[RuleEngineUtils.ENV_VAR_TEI_COUNT] = RuleVariableValue(
RuleValueType.NUMERIC, "1",
listOf("1"), currentDate
listOf("1"), currentDate.toString()
)
val enrollmentDate = dateFormat.format(ruleEnrollment!!.enrollmentDate)
val enrollmentDate = ruleEnrollment!!.enrollmentDate
valueMap[RuleEngineUtils.ENV_VAR_ENROLLMENT_DATE] = RuleVariableValue(
RuleValueType.TEXT, enrollmentDate,
listOf(enrollmentDate), currentDate
RuleValueType.TEXT, enrollmentDate.toString(),
listOf(enrollmentDate.toString()), currentDate.toString()
)
val incidentDate = dateFormat.format(ruleEnrollment!!.incidentDate)
val incidentDate = ruleEnrollment!!.incidentDate
valueMap[RuleEngineUtils.ENV_VAR_INCIDENT_DATE] = RuleVariableValue(
RuleValueType.TEXT, incidentDate,
listOf(incidentDate), currentDate
RuleValueType.TEXT, incidentDate.toString(),
listOf(incidentDate.toString()), currentDate.toString()
)
val status = ruleEnrollment!!.status.toString()
valueMap[RuleEngineUtils.ENV_VAR_ENROLLMENT_STATUS] = RuleVariableValue(
RuleValueType.TEXT, status,
listOf(status), currentDate
listOf(status), currentDate.toString()
)
val organisationUnit = ruleEnrollment!!.organisationUnit
valueMap[RuleEngineUtils.ENV_VAR_OU] = RuleVariableValue(RuleValueType.TEXT, organisationUnit)
Expand All @@ -240,23 +234,23 @@ class RuleVariableValueMapBuilder private constructor() {
RuleVariableValue(RuleValueType.TEXT, organisationUnitCode)
}
if (ruleEvent != null) {
val eventDate = dateFormat.format(ruleEvent!!.eventDate())
val eventDate = ruleEvent!!.eventDate()
valueMap[RuleEngineUtils.ENV_VAR_EVENT_DATE] = RuleVariableValue(
RuleValueType.TEXT, eventDate,
listOf(eventDate), currentDate
RuleValueType.TEXT, eventDate.toString(),
listOf(eventDate.toString()), currentDate.toString()
)
if (ruleEvent!!.dueDate() != null) {
val dueDate = dateFormat.format(ruleEvent!!.dueDate())
val dueDate = ruleEvent!!.dueDate()
valueMap[RuleEngineUtils.ENV_VAR_DUE_DATE] = RuleVariableValue(
RuleValueType.TEXT, dueDate,
listOf(dueDate), currentDate
RuleValueType.TEXT, dueDate.toString(),
listOf(dueDate.toString()), currentDate.toString()
)
}
if (ruleEvent!!.completedDate() != null) {
val completedDate = dateFormat.format(ruleEvent!!.completedDate())
val completedDate = ruleEvent!!.completedDate()
valueMap[RuleEngineUtils.ENV_VAR_COMPLETED_DATE] = RuleVariableValue(
RuleValueType.TEXT, completedDate,
listOf(completedDate), currentDate
RuleValueType.TEXT, completedDate.toString(),
listOf(completedDate.toString()), currentDate.toString()
)
}

Expand All @@ -267,15 +261,15 @@ class RuleVariableValueMapBuilder private constructor() {
}
valueMap[RuleEngineUtils.ENV_VAR_EVENT_COUNT] = RuleVariableValue(
RuleValueType.NUMERIC, eventCount,
listOf(eventCount), currentDate
listOf(eventCount), currentDate.toString()
)
valueMap[RuleEngineUtils.ENV_VAR_EVENT_ID] = RuleVariableValue(
RuleValueType.TEXT, ruleEvent!!.event(),
listOf(ruleEvent!!.event()), currentDate
listOf(ruleEvent!!.event()), currentDate.toString()
)
val status = ruleEvent!!.status().toString()
valueMap[RuleEngineUtils.ENV_VAR_EVENT_STATUS] = RuleVariableValue(
RuleValueType.TEXT, status, listOf(status), currentDate
RuleValueType.TEXT, status, listOf(status), currentDate.toString()
)
val organisationUnit = ruleEvent!!.organisationUnit()
valueMap[RuleEngineUtils.ENV_VAR_OU] = RuleVariableValue(RuleValueType.TEXT, organisationUnit)
Expand Down Expand Up @@ -310,7 +304,6 @@ class RuleVariableValueMapBuilder private constructor() {
}

companion object {
private const val DATE_PATTERN = "yyyy-MM-dd"
fun target(ruleEnrollment: RuleEnrollment): RuleVariableValueMapBuilder {
return RuleVariableValueMapBuilder(ruleEnrollment)
}
Expand Down
22 changes: 13 additions & 9 deletions src/jvmMain/kotlin/org/hisp/dhis/rules/Utils.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package org.hisp.dhis.rules

import kotlinx.datetime.Clock
import kotlinx.datetime.LocalDate
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toLocalDateTime
import org.hisp.dhis.rules.models.RuleDataValue
import org.hisp.dhis.rules.models.RuleEvent
import java.text.SimpleDateFormat
import java.util.Locale
import java.util.Date
import java.util.regex.Pattern
import kotlin.collections.ArrayList

val dateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.US)
const val VARIABLE_PATTERN = "[#]\\{([\\w -_.]+)\\}"
val VARIABLE_PATTERN_COMPILED = Pattern.compile(VARIABLE_PATTERN)
fun values(ruleDataValues: List<RuleDataValue>): List<String> {
Expand All @@ -23,23 +23,23 @@ val dateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.US)
ruleDataValues: List<RuleDataValue>,
ruleEvent: RuleEvent
): String {
val dates: MutableList<Date> = ArrayList()
val dates: MutableList<LocalDate> = ArrayList()
for (date in ruleDataValues) {
val d = date.eventDate
if (d.before(ruleEvent.eventDate)) {
if (d < ruleEvent.eventDate) {
dates.add(d)
}
}
return dateFormat.format(dates.max())
return dates.max().toString()
}

fun getLastUpdateDate(ruleDataValues: List<RuleDataValue>): String {
val dates: MutableList<Date> = ArrayList()
val dates: MutableList<LocalDate> = ArrayList()
for (date in ruleDataValues) {
val d = date.eventDate
dates.add(d)
}
return dateFormat.format(dates.max())
return dates.max().toString()
}

fun unwrapVariableName(variable: String): String {
Expand All @@ -51,3 +51,7 @@ val dateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.US)
}
throw IllegalArgumentException("Malformed variable: $variable")
}

fun LocalDate.Companion.currentDate(): LocalDate {
return Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault()).date
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.hisp.dhis.rules.models

import java.util.Date
import kotlinx.datetime.LocalDate

data class RuleDataValue(
val eventDate: Date,
val eventDate: LocalDate,
val programStage: String,
val dataElement: String,
val value: String
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.hisp.dhis.rules.models

import java.util.Date
import kotlinx.datetime.LocalDate

data class RuleEnrollment(
val enrollment: String,
val programName: String,
val incidentDate: Date,
val enrollmentDate: Date,
val incidentDate: LocalDate,
val enrollmentDate: LocalDate,
val status: Status,
val organisationUnit: String,
val organisationUnitCode: String,
Expand Down
18 changes: 7 additions & 11 deletions src/jvmMain/kotlin/org/hisp/dhis/rules/models/RuleEvent.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package org.hisp.dhis.rules.models

import java.util.Date
import kotlinx.datetime.LocalDate

data class RuleEvent(
val event: String,
val programStage: String,
val programStageName: String,
val status: Status,
val eventDate: Date,
val dueDate: Date?,
val completedDate: Date?,
val eventDate: LocalDate,
val dueDate: LocalDate?,
val completedDate: LocalDate?,
val organisationUnit: String,
val organisationUnitCode: String?,
val dataValues: List<RuleDataValue>
Expand Down Expand Up @@ -39,15 +39,15 @@ data class RuleEvent(
return status
}

fun eventDate(): Date {
fun eventDate(): LocalDate {
return eventDate
}

fun dueDate(): Date? {
fun dueDate(): LocalDate? {
return dueDate
}

fun completedDate(): Date? {
fun completedDate(): LocalDate? {
return completedDate
}

Expand All @@ -62,8 +62,4 @@ data class RuleEvent(
fun dataValues(): List<RuleDataValue> {
return dataValues
}

companion object {
val EVENT_DATE_COMPARATOR: Comparator<RuleEvent> = Comparator.comparing{e: RuleEvent-> e.eventDate()}.reversed()
}
}
Loading

0 comments on commit 32a4e75

Please sign in to comment.