From 899ceb48906d5406e9186feb4d729f733355eb10 Mon Sep 17 00:00:00 2001 From: Xavier Molloy Date: Wed, 26 Jun 2024 16:24:37 +0200 Subject: [PATCH] ci: [ANDROAPP-5753] Format value depending on value type, migrate ValueUtils to kotlin, fix sonar smells in DashboardRepositoryImpl --- .../SearchRepositoryImpl.java | 10 +- .../teiDashboard/DashboardRepositoryImpl.kt | 232 ++++++++++-------- .../main/java/org/dhis2/utils/ValueUtils.kt | 143 ++++++++--- .../dhis2/commons/data/SearchTeiModel.java | 3 - 4 files changed, 242 insertions(+), 146 deletions(-) diff --git a/app/src/main/java/org/dhis2/usescases/searchTrackEntity/SearchRepositoryImpl.java b/app/src/main/java/org/dhis2/usescases/searchTrackEntity/SearchRepositoryImpl.java index 070bb93b60..98a36d230a 100644 --- a/app/src/main/java/org/dhis2/usescases/searchTrackEntity/SearchRepositoryImpl.java +++ b/app/src/main/java/org/dhis2/usescases/searchTrackEntity/SearchRepositoryImpl.java @@ -368,7 +368,7 @@ private void setAttributeValue(SearchTeiModel searchTei, TrackedEntitySearchItem String value = attribute.getValue(); String transformedValue; if (value != null) { - transformedValue = ValueUtils.transformValue(d2, value, attribute.getValueType(), attribute.getOptionSet()); + transformedValue = ValueUtils.Companion.transformValue(d2, value, attribute.getValueType(), attribute.getOptionSet()); } else { transformedValue = sortingValueSetter.getUnknownLabel(); } @@ -409,8 +409,8 @@ private void setOverdueEvents(@NonNull SearchTeiModel tei, Program selectedProgr if (count > 0) { tei.setHasOverdue(true); - Date scheduleDate = scheduleList.size() > 0 ? scheduleList.get(0).dueDate() : null; - Date overdueDate = overdueList.size() > 0 ? overdueList.get(0).dueDate() : null; + Date scheduleDate = !scheduleList.isEmpty() ? scheduleList.get(0).dueDate() : null; + Date overdueDate = !overdueList.isEmpty() ? overdueList.get(0).dueDate() : null; Date dateToShow = null; if (scheduleDate != null && overdueDate != null) { if (scheduleDate.before(overdueDate)) { @@ -530,8 +530,8 @@ private List getTETypeAttributeUids(String teTypeUid) { } private int getTeiDefaultRes(TrackedEntityInstance tei) { - TrackedEntityType teiType = d2.trackedEntityModule().trackedEntityTypes().uid(tei.trackedEntityType()).blockingGet(); - return resources.getObjectStyleDrawableResource(teiType.style().icon(), R.drawable.photo_temp_gray); + TrackedEntityType teiTypeValues = d2.trackedEntityModule().trackedEntityTypes().uid(tei.trackedEntityType()).blockingGet(); + return resources.getObjectStyleDrawableResource(teiTypeValues.style().icon(), R.drawable.photo_temp_gray); } private List getTrackedEntityAttributesForRelationship(TrackedEntityInstance tei, Program selectedProgram) { diff --git a/app/src/main/java/org/dhis2/usescases/teiDashboard/DashboardRepositoryImpl.kt b/app/src/main/java/org/dhis2/usescases/teiDashboard/DashboardRepositoryImpl.kt index a123aa9781..d81101aa2d 100644 --- a/app/src/main/java/org/dhis2/usescases/teiDashboard/DashboardRepositoryImpl.kt +++ b/app/src/main/java/org/dhis2/usescases/teiDashboard/DashboardRepositoryImpl.kt @@ -97,53 +97,42 @@ class DashboardRepositoryImpl( teiUid, ) .map> { attributesValues: List -> - val formattedValues: MutableList = - java.util.ArrayList() - for (attributeValue in attributesValues) { - if (attributeValue.value() != null) { - val attribute = - d2.trackedEntityModule().trackedEntityAttributes() - .uid(attributeValue.trackedEntityAttribute()).blockingGet() - if (attribute!!.valueType() != ValueType.IMAGE) { - formattedValues.add( - ValueUtils.transform( - d2, - attributeValue, - attribute!!.valueType(), - if (attribute!!.optionSet() != null) { - attribute!!.optionSet()!! - .uid() - } else { - null - }, - ), - ) - } - } else { - formattedValues.add( - TrackedEntityAttributeValue.builder() - .trackedEntityAttribute(attributeValue.trackedEntityAttribute()) - .trackedEntityInstance(teiUid) - .value("") - .build(), - ) - } - } + val formattedValues = formatProgramAttributeValues(attributesValues) formattedValues }.toObservable() } else { val teType = d2.trackedEntityModule().trackedEntityInstances().uid(teiUid).blockingGet()!! .trackedEntityType() - val attributeValues: MutableList = java.util.ArrayList() - for (attributeValue in teiAttributesProvider.getValuesFromTrackedEntityTypeAttributes( - teType, - teiUid, - )) { - val attribute = d2.trackedEntityModule().trackedEntityAttributes() - .uid(attributeValue.trackedEntityAttribute()).blockingGet() - if (attribute!!.valueType() != ValueType.IMAGE && attributeValue.value() != null) { - attributeValues.add( + + val attributeValues = mapTeiTypeAttributeValues( + teiAttributesProvider.getValuesFromTrackedEntityTypeAttributes( + teType, + teiUid, + ), + ) + if (attributeValues.isEmpty()) { + formatProgramAttributeValuesByTrackedEntity( + attributeValues, + teiAttributesProvider.getValuesFromProgramTrackedEntityAttributes( + teType, + teiUid, + ), + ) + } + Observable.just(attributeValues) + } + } + + private fun formatProgramAttributeValues(list: List): MutableList { + val formattedValues: MutableList = mutableListOf() + for (attributeValue in list) { + if (attributeValue.value() != null) { + val attribute = + d2.trackedEntityModule().trackedEntityAttributes() + .uid(attributeValue.trackedEntityAttribute()).blockingGet() + if (attribute!!.valueType() != ValueType.IMAGE) { + formattedValues.add( ValueUtils.transform( d2, attributeValue, @@ -157,31 +146,103 @@ class DashboardRepositoryImpl( ), ) } + } else { + formattedValues.add( + TrackedEntityAttributeValue.builder() + .trackedEntityAttribute(attributeValue.trackedEntityAttribute()) + .trackedEntityInstance(teiUid) + .value("") + .build(), + ) } - if (attributeValues.isEmpty()) { - for (attributeValue in teiAttributesProvider.getValuesFromProgramTrackedEntityAttributes( - teType, - teiUid, - )) { - val attribute = d2.trackedEntityModule().trackedEntityAttributes() - .uid(attributeValue.trackedEntityAttribute()).blockingGet() - attributeValues.add( - ValueUtils.transform( - d2, - attributeValue, - attribute!!.valueType(), - if (attribute.optionSet() != null) { - attribute.optionSet()!! - .uid() - } else { - null - }, + } + return formattedValues + } + + private fun formatProgramAttributeValuesByTrackedEntity( + formattedList: MutableList, + list: List, + ): MutableList { + for (attributeValue in list) { + val attribute = d2.trackedEntityModule().trackedEntityAttributes() + .uid(attributeValue.trackedEntityAttribute()).blockingGet() + formattedList.add( + ValueUtils.transform( + d2, + attributeValue, + attribute!!.valueType(), + if (attribute.optionSet() != null) { + attribute.optionSet()!! + .uid() + } else { + null + }, + ), + ) + } + return formattedList + } + + private fun mapTeiTypeAttributeValues(list: List): MutableList { + val attributeValues: MutableList = mutableListOf() + for (attributeValue in list) { + val attribute = d2.trackedEntityModule().trackedEntityAttributes() + .uid(attributeValue.trackedEntityAttribute()).blockingGet() + if (attribute!!.valueType() != ValueType.IMAGE && attributeValue.value() != null) { + attributeValues.add( + ValueUtils.transform( + d2, + attributeValue, + attribute.valueType(), + if (attribute.optionSet() != null) { + attribute.optionSet()!! + .uid() + } else { + null + }, + ), + ) + } + } + + return attributeValues + } + + private fun mapRelationShipTypes(list: List, teType: String): MutableList> { + val relTypeList: MutableList> = + java.util.ArrayList() + for (relationshipType in list) { + if (relationshipType.fromConstraint() != null && relationshipType.fromConstraint()!! + .trackedEntityType() != null && relationshipType.fromConstraint()!! + .trackedEntityType()!!.uid() == teType + ) { + if (relationshipType.toConstraint() != null && relationshipType.toConstraint()!! + .trackedEntityType() != null + ) { + relTypeList.add( + Pair.create( + relationshipType, + relationshipType.toConstraint()!! + .trackedEntityType()!!.uid(), ), ) } + } else if (relationshipType.bidirectional()!! && relationshipType.toConstraint() != null && relationshipType.toConstraint()!! + .trackedEntityType() != null && relationshipType.toConstraint()!! + .trackedEntityType()!! + .uid() == teType && relationshipType.fromConstraint() != null && + relationshipType.fromConstraint()!!.trackedEntityType() != null + ) { + relTypeList.add( + Pair.create( + relationshipType, + relationshipType.fromConstraint()!! + .trackedEntityType()!!.uid(), + ), + ) } - Observable.just(attributeValues) } + return relTypeList } override fun setFollowUp(enrollmentUid: String?): Boolean { @@ -228,8 +289,8 @@ class DashboardRepositoryImpl( return d2.systemInfoModule().systemInfo().get().toObservable() .map(Function { obj: SystemInfo -> obj.version() }) .flatMap { version: String? -> - if (version == "2.29") { - return@flatMap d2.relationshipModule().relationshipTypes() + return@flatMap if (version == "2.29") { + d2.relationshipModule().relationshipTypes() .get().toObservable() .flatMapIterable { list: List? -> list } .map> { relationshipType: RelationshipType? -> @@ -239,45 +300,10 @@ class DashboardRepositoryImpl( ) }.toList().toObservable() } else { - return@flatMap d2.relationshipModule() + d2.relationshipModule() .relationshipTypes().withConstraints().get() .map>> { relationshipTypes: List -> - val relTypeList: MutableList> = - java.util.ArrayList() - for (relationshipType in relationshipTypes) { - if (relationshipType.fromConstraint() != null && relationshipType.fromConstraint()!! - .trackedEntityType() != null && relationshipType.fromConstraint()!! - .trackedEntityType()!!.uid() == teType - ) { - if (relationshipType.toConstraint() != null && relationshipType.toConstraint()!! - .trackedEntityType() != null - ) { - relTypeList.add( - Pair.create( - relationshipType, - relationshipType.toConstraint()!! - .trackedEntityType()!!.uid(), - ), - ) - } - } else if (relationshipType.bidirectional()!! && relationshipType.toConstraint() != null && relationshipType.toConstraint()!! - .trackedEntityType() != null && relationshipType.toConstraint()!! - .trackedEntityType()!! - .uid() == teType - ) { - if (relationshipType.fromConstraint() != null && relationshipType.fromConstraint()!! - .trackedEntityType() != null - ) { - relTypeList.add( - Pair.create( - relationshipType, - relationshipType.fromConstraint()!! - .trackedEntityType()!!.uid(), - ), - ) - } - } - } + val relTypeList = mapRelationShipTypes(relationshipTypes, teType) relTypeList.toList() }.toObservable() } @@ -412,7 +438,7 @@ class DashboardRepositoryImpl( .trackedEntityInstances() .uid(teiUid) .blockingGet() - ?.state() == State.TO_POST + ?.aggregatedSyncState() == State.TO_POST val hasAuthority = d2.userModule() .authorities() .byName().eq("F_TEI_CASCADE_DELETE") @@ -433,7 +459,7 @@ class DashboardRepositoryImpl( val local = d2.enrollmentModule() .enrollments() .uid(enrollmentUid) - .blockingGet()!!.state() == State.TO_POST + .blockingGet()!!.aggregatedSyncState() == State.TO_POST val hasAuthority = d2.userModule() .authorities() .byName().eq("F_ENROLLMENT_CASCADE_DELETE") @@ -451,9 +477,9 @@ class DashboardRepositoryImpl( enrollmentObjectRepository.blockingGet()!!.status()!!, ) enrollmentObjectRepository.blockingDelete() - !d2.enrollmentModule().enrollments().byTrackedEntityInstance().eq(teiUid) + d2.enrollmentModule().enrollments().byTrackedEntityInstance().eq(teiUid) .byDeleted().isFalse - .byStatus().eq(EnrollmentStatus.ACTIVE).blockingGet().isEmpty() + .byStatus().eq(EnrollmentStatus.ACTIVE).blockingGet().isNotEmpty() } } diff --git a/app/src/main/java/org/dhis2/utils/ValueUtils.kt b/app/src/main/java/org/dhis2/utils/ValueUtils.kt index 5952d55ef5..d1dc57db2e 100644 --- a/app/src/main/java/org/dhis2/utils/ValueUtils.kt +++ b/app/src/main/java/org/dhis2/utils/ValueUtils.kt @@ -1,52 +1,125 @@ -package org.dhis2.utils; +package org.dhis2.utils -import org.hisp.dhis.android.core.D2; -import org.hisp.dhis.android.core.common.ValueType; -import org.hisp.dhis.android.core.option.Option; -import org.hisp.dhis.android.core.trackedentity.TrackedEntityAttributeValue; - -import java.util.Objects; +import org.dhis2.commons.date.DateUtils +import org.hisp.dhis.android.core.D2 +import org.hisp.dhis.android.core.common.ValueType +import org.hisp.dhis.android.core.trackedentity.TrackedEntityAttributeValue +import java.text.ParseException /** * QUADRAM. Created by ppajuelo on 25/09/2018. */ - -public class ValueUtils { - - private ValueUtils() { - throw new IllegalStateException("Utility class"); +class ValueUtils private constructor() { + init { + throw IllegalStateException("Utility class") } - public static TrackedEntityAttributeValue transform(D2 d2, TrackedEntityAttributeValue attributeValue, ValueType valueType, String optionSetUid) { - String transformedValue = transformValue(d2, attributeValue.value(), valueType, optionSetUid); + companion object { + fun transform( + d2: D2, + attributeValue: TrackedEntityAttributeValue, + valueType: ValueType?, + optionSetUid: String?, + ): TrackedEntityAttributeValue { + val transformedValue = + transformValue(d2, attributeValue.value(), valueType, optionSetUid) - if (!Objects.equals(transformedValue, attributeValue.value())) { - return attributeValue.toBuilder() + return if (transformedValue != attributeValue.value()) { + attributeValue.toBuilder() .value(transformedValue) - .build(); - } else { - return attributeValue; + .build() + } else { + attributeValue + } } - } - public static String transformValue(D2 d2, String value, ValueType valueType, String optionSetUid) { - String teAttrValue = value; - if (valueType.equals(ValueType.ORGANISATION_UNIT)) { - if (!d2.organisationUnitModule().organisationUnits().byUid().eq(value).blockingIsEmpty()) { - String orgUnitName = d2.organisationUnitModule().organisationUnits() - .byUid().eq(value) - .one().blockingGet().displayName(); - teAttrValue = orgUnitName; + fun transformValue( + d2: D2, + value: String?, + valueType: ValueType?, + optionSetUid: String?, + ): String? { + var teAttrValue = value + when (valueType) { + ValueType.ORGANISATION_UNIT -> { + if (!d2.organisationUnitModule().organisationUnits().byUid().eq(value) + .blockingIsEmpty() + ) { + val orgUnitName = d2.organisationUnitModule().organisationUnits() + .byUid().eq(value) + .one().blockingGet()!!.displayName()!! + teAttrValue = orgUnitName + } + } + ValueType.DATE, ValueType.AGE -> { + teAttrValue = formatDate(teAttrValue) + } + ValueType.DATETIME -> { + teAttrValue = formatDateTime(teAttrValue) + } + + ValueType.PERCENTAGE -> { + teAttrValue?.let { + if (it.isNotEmpty()) { + teAttrValue = "$it %" + } + } + } + else -> { + teAttrValue = transformOptionSet(optionSetUid, d2, value) + } } - } else if (optionSetUid != null) { - String optionCode = value; - if (optionCode != null) { - Option option = d2.optionModule().options().byOptionSetUid().eq(optionSetUid).byCode().eq(optionCode).one().blockingGet(); - if (option != null && (Objects.equals(option.code(), optionCode) || Objects.equals(option.name(), optionCode))) { - teAttrValue = option.displayName(); + return teAttrValue + } + + private fun transformOptionSet(optionSetUid: String?, d2: D2, value: String?): String? { + var teAttrValue = value + if (optionSetUid != null) { + val optionCode = value + if (optionCode != null) { + val option = + d2.optionModule().options().byOptionSetUid().eq(optionSetUid).byCode() + .eq(optionCode).one().blockingGet() + if (option != null && (option.code() == optionCode || option.name() == optionCode)) { + teAttrValue = option.displayName() + } + } + } + return teAttrValue + } + + private fun formatDate(value: String?): String { + return if (value?.isNotEmpty() == true) { + var formattedDate = "" + val date = try { + DateUtils.oldUiDateFormat().parse(value) + } catch (e: ParseException) { + null + } + date?.let { + formattedDate = DateUtils.uiDateFormat().format(date) + } + formattedDate + } else { + "" + } + } + + private fun formatDateTime(value: String?): String { + return if (value?.isNotEmpty() == true) { + var formattedDate = "" + val date = try { + DateUtils.databaseDateFormatNoSeconds().parse(value) + } catch (e: ParseException) { + null + } + date?.let { + formattedDate = DateUtils.uiDateTimeFormat().format(date) } + formattedDate + } else { + "" } } - return teAttrValue; } } diff --git a/commons/src/main/java/org/dhis2/commons/data/SearchTeiModel.java b/commons/src/main/java/org/dhis2/commons/data/SearchTeiModel.java index d3c9597a70..ba8d33d93c 100644 --- a/commons/src/main/java/org/dhis2/commons/data/SearchTeiModel.java +++ b/commons/src/main/java/org/dhis2/commons/data/SearchTeiModel.java @@ -1,7 +1,5 @@ package org.dhis2.commons.data; -import androidx.compose.ui.graphics.Color; - import org.dhis2.commons.data.tuples.Trio; import org.dhis2.ui.MetadataIconData; import org.hisp.dhis.android.core.enrollment.Enrollment; @@ -9,7 +7,6 @@ import org.hisp.dhis.android.core.program.Program; import org.hisp.dhis.android.core.trackedentity.TrackedEntityAttributeValue; import org.hisp.dhis.android.core.trackedentity.TrackedEntityInstance; -import org.hisp.dhis.mobile.ui.designsystem.component.internal.ImageCardData; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable;