Skip to content

Commit

Permalink
[ANDROAPP-6126] Date parse exception when searching by unfinished dat…
Browse files Browse the repository at this point in the history
…e string (#3604)

Signed-off-by: Pablo <[email protected]>
  • Loading branch information
Balcan authored Apr 29, 2024
1 parent 2dfbcb9 commit ea49f9a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,10 @@ public Observable<Pair<String, String>> saveToEnroll(@NonNull String teiType,
if (dataValue.contains("_os_"))
dataValue = dataValue.split("_os_")[1];

boolean isGenerated = d2.trackedEntityModule().trackedEntityAttributes().uid(key).blockingGet().generated();

if (!isGenerated) {
TrackedEntityAttribute attribute = d2.trackedEntityModule().trackedEntityAttributes().uid(key).blockingGet();
boolean isGenerated = attribute.generated();
boolean hasValidValue = attribute.valueType().getValidator().validate(dataValue).getSucceeded();
if (!isGenerated && hasValidValue) {
valueStore.overrideProgram(programUid);
valueStore.save(key, dataValue).blockingFirst();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import org.hisp.dhis.android.core.arch.helpers.Result
import org.hisp.dhis.android.core.common.ValueType
import org.hisp.dhis.android.core.maintenance.D2ErrorCode
import timber.log.Timber
import java.text.ParseException

const val TEI_TYPE_SEARCH_MAX_RESULTS = 5

Expand Down Expand Up @@ -923,20 +924,28 @@ class SearchTEIViewModel(
ValueType.DATE, ValueType.AGE -> {
item.value?.let {
if (it.isNotEmpty()) {
val date = DateUtils.oldUiDateFormat().parse(it)
date?.let {
map[item.uid] = DateUtils.uiDateFormat().format(date)
val date = try {
DateUtils.oldUiDateFormat().parse(it)
} catch (e: ParseException) {
null
}
map[item.uid] = date?.let {
DateUtils.uiDateFormat().format(date)
} ?: it
}
}
}
ValueType.DATETIME -> {
item.value?.let {
if (it.isNotEmpty()) {
val date = DateUtils.databaseDateFormatNoSeconds().parse(it)
date?.let {
map[item.uid] = DateUtils.uiDateTimeFormat().format(date)
val date = try {
DateUtils.databaseDateFormatNoSeconds().parse(it)
} catch (e: ParseException) {
null
}
map[item.uid] = date?.let {
DateUtils.uiDateTimeFormat().format(date)
} ?: it
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,32 @@ class SearchTEIViewModelTest {
assertTrue(expectedMap == formattedMap)
}

@Test
fun `should return test`() {
viewModel.uiState = viewModel.uiState.copy(items = getMalformedDateFieldUIModels())
val expectedMap = mapOf(
"uid1" to "04",
)

val formattedMap = viewModel.getFriendlyQueryData()

assertTrue(expectedMap == formattedMap)
}

private fun getMalformedDateFieldUIModels(): List<FieldUiModel> {
return listOf(
FieldUiModelImpl(
uid = "uid1",
layoutId = 3,
label = "Date",
value = "04",
autocompleteList = emptyList(),
optionSetConfiguration = null,
valueType = ValueType.DATE,
),
)
}

private fun getFieldUIModels(): List<FieldUiModel> {
return listOf(
FieldUiModelImpl(
Expand Down

0 comments on commit ea49f9a

Please sign in to comment.