Skip to content

Commit

Permalink
ci: [ANDROAPP-5753] Format value depending on value type, migrate Val…
Browse files Browse the repository at this point in the history
…ueUtils to kotlin, fix sonar smells in DashboardRepositoryImpl
  • Loading branch information
xavimolloy committed Jun 26, 2024
1 parent 7a72fb8 commit 899ceb4
Show file tree
Hide file tree
Showing 4 changed files with 242 additions and 146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -530,8 +530,8 @@ private List<String> 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<TrackedEntityAttributeValue> getTrackedEntityAttributesForRelationship(TrackedEntityInstance tei, Program selectedProgram) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,53 +97,42 @@ class DashboardRepositoryImpl(
teiUid,
)
.map<List<TrackedEntityAttributeValue>> { attributesValues: List<TrackedEntityAttributeValue> ->
val formattedValues: MutableList<TrackedEntityAttributeValue> =
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<TrackedEntityAttributeValue> = 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<TrackedEntityAttributeValue>): MutableList<TrackedEntityAttributeValue> {
val formattedValues: MutableList<TrackedEntityAttributeValue> = 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,
Expand All @@ -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<TrackedEntityAttributeValue>,
list: List<TrackedEntityAttributeValue>,
): MutableList<TrackedEntityAttributeValue> {
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<TrackedEntityAttributeValue>): MutableList<TrackedEntityAttributeValue> {
val attributeValues: MutableList<TrackedEntityAttributeValue> = 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<RelationshipType>, teType: String): MutableList<Pair<RelationshipType?, String>> {
val relTypeList: MutableList<Pair<RelationshipType?, String>> =
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 {
Expand Down Expand Up @@ -228,8 +289,8 @@ class DashboardRepositoryImpl(
return d2.systemInfoModule().systemInfo().get().toObservable()
.map<String?>(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<RelationshipType?> { list: List<RelationshipType?>? -> list }
.map<Pair<RelationshipType?, String>> { relationshipType: RelationshipType? ->
Expand All @@ -239,45 +300,10 @@ class DashboardRepositoryImpl(
)
}.toList().toObservable()
} else {
return@flatMap d2.relationshipModule()
d2.relationshipModule()
.relationshipTypes().withConstraints().get()
.map<List<Pair<RelationshipType?, String>>> { relationshipTypes: List<RelationshipType> ->
val relTypeList: MutableList<Pair<RelationshipType?, String>> =
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()
}
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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()
}
}

Expand Down
Loading

0 comments on commit 899ceb4

Please sign in to comment.