Skip to content

Commit

Permalink
refactor RuleVariable to kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
enricocolasante committed Dec 7, 2023
1 parent 88c3712 commit f18c7f8
Show file tree
Hide file tree
Showing 37 changed files with 828 additions and 1,001 deletions.
45 changes: 0 additions & 45 deletions src/main/java/org/hisp/dhis/rules/models/RuleVariable.java

This file was deleted.

25 changes: 25 additions & 0 deletions src/main/java/org/hisp/dhis/rules/models/RuleVariable.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.hisp.dhis.rules.models

import org.hisp.dhis.rules.Option
import org.hisp.dhis.rules.RuleVariableValue
import org.hisp.dhis.rules.RuleVariableValueMapBuilder
import java.util.function.Predicate
import javax.annotation.Nonnull

interface RuleVariable {
fun options(): List<Option>
fun createValues(
builder: RuleVariableValueMapBuilder,
allEventValues: Map<String, List<RuleDataValue>>,
currentEnrollmentValues: Map<String, RuleAttributeValue>,
currentEventValues: Map<String, RuleDataValue>
): Map<String, RuleVariableValue>

fun getOptionName(value: String?): String? {
// if no option found then existing value in the context will be used
return options()
.filter{ (_, code): Option -> value == code }
.map(Option::name)
.getOrElse(0) {_ -> value}
}
}

This file was deleted.

43 changes: 43 additions & 0 deletions src/main/java/org/hisp/dhis/rules/models/RuleVariableAttribute.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.hisp.dhis.rules.models

import org.hisp.dhis.rules.Option
import org.hisp.dhis.rules.RuleVariableValue
import org.hisp.dhis.rules.RuleVariableValueMapBuilder
import org.hisp.dhis.rules.dateFormat
import java.util.*
import javax.annotation.Nonnull

data class RuleVariableAttribute(
val name: String,
val useCodeForOptionSet: Boolean,
val options: List<Option>,
val trackedEntityAttribute: String,
val trackedEntityAttributeType: RuleValueType
) : RuleVariable {
override fun options(): List<Option> {
return options
}

override fun createValues(
builder: RuleVariableValueMapBuilder,
allEventValues: Map<String, List<RuleDataValue>>,
currentEnrollmentValues: Map<String, RuleAttributeValue>,
currentEventValues: Map<String, RuleDataValue>
): Map<String, RuleVariableValue> {
val valueMap: MutableMap<String, RuleVariableValue> = HashMap()
val currentDate = dateFormat.format(Date())
val variableValue: RuleVariableValue
variableValue = if (currentEnrollmentValues.containsKey(trackedEntityAttribute)) {
val value = currentEnrollmentValues[trackedEntityAttribute]
val optionValue = if (useCodeForOptionSet) value!!.value else getOptionName(value!!.value)!!
RuleVariableValue.create(
optionValue, trackedEntityAttributeType,
java.util.List.of(optionValue), currentDate
)
} else {
RuleVariableValue.create(trackedEntityAttributeType)
}
valueMap[name] = variableValue
return valueMap
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.hisp.dhis.rules.models

import org.hisp.dhis.rules.*
import javax.annotation.Nonnull

/*
* Copyright (c) 2004-2018, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of the HISP project nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ /**
* @author Zubair Asghar.
*/
class RuleVariableCalculatedValue(
val name: String,
val useCodeForOptionSet: Boolean,
val options: List<Option>,
val calculatedValueVariable: String,
val calculatedValueType: RuleValueType
) : RuleVariable {
override fun options(): List<Option> {
return options
}

override fun createValues(
builder: RuleVariableValueMapBuilder,
allEventValues: Map<String, List<RuleDataValue>>,
currentEnrollmentValues: Map<String, RuleAttributeValue>,
currentEventValues: Map<String, RuleDataValue>
): Map<String, RuleVariableValue> {
val valueMap: MutableMap<String, RuleVariableValue> = HashMap()
valueMap[name] = RuleVariableValue.create(calculatedValueType)
return valueMap
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.hisp.dhis.rules.models

import org.hisp.dhis.rules.*
import javax.annotation.Nonnull

class RuleVariableCurrentEvent(
val name: String,
val useCodeForOptionSet: Boolean,
val options: List<Option>,
val dataElement: String,
val dataElementType: RuleValueType
) : RuleVariableDataElement {
override fun dataElement(): String {
return dataElement
}

override fun dataElementType(): RuleValueType {
return dataElementType
}

override fun options(): List<Option> {
return options
}

override fun createValues(
builder: RuleVariableValueMapBuilder,
allEventValues: Map<String, List<RuleDataValue>>,
currentEnrollmentValues: Map<String, RuleAttributeValue>,
currentEventValues: Map<String, RuleDataValue>
): Map<String, RuleVariableValue> {
val valueMap: MutableMap<String, RuleVariableValue> = HashMap()
val variableValue: RuleVariableValue
variableValue = if (currentEventValues.containsKey(dataElement)) {
val value = currentEventValues[dataElement]
val optionValue = if (useCodeForOptionSet) value!!.value else getOptionName(value!!.value)!!
RuleVariableValue.create(
optionValue, dataElementType,
java.util.List.of(optionValue), getLastUpdateDate(java.util.List.of(value))
)
} else {
RuleVariableValue.create(dataElementType)
}
valueMap[name] = variableValue
return valueMap
}
}
Loading

0 comments on commit f18c7f8

Please sign in to comment.