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 f2b9fa1968..ff0f8f0769 100644 --- a/app/src/main/java/org/dhis2/usescases/searchTrackEntity/SearchRepositoryImpl.java +++ b/app/src/main/java/org/dhis2/usescases/searchTrackEntity/SearchRepositoryImpl.java @@ -287,9 +287,10 @@ public Observable> 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(); } diff --git a/app/src/main/java/org/dhis2/usescases/searchTrackEntity/SearchTEIViewModel.kt b/app/src/main/java/org/dhis2/usescases/searchTrackEntity/SearchTEIViewModel.kt index 100f23e43c..ff326ec108 100644 --- a/app/src/main/java/org/dhis2/usescases/searchTrackEntity/SearchTEIViewModel.kt +++ b/app/src/main/java/org/dhis2/usescases/searchTrackEntity/SearchTEIViewModel.kt @@ -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 @@ -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 } } } diff --git a/app/src/test/java/org/dhis2/usescases/searchTrackEntity/SearchTEIViewModelTest.kt b/app/src/test/java/org/dhis2/usescases/searchTrackEntity/SearchTEIViewModelTest.kt index 40c711e3bc..6a28db5deb 100644 --- a/app/src/test/java/org/dhis2/usescases/searchTrackEntity/SearchTEIViewModelTest.kt +++ b/app/src/test/java/org/dhis2/usescases/searchTrackEntity/SearchTEIViewModelTest.kt @@ -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 { + return listOf( + FieldUiModelImpl( + uid = "uid1", + layoutId = 3, + label = "Date", + value = "04", + autocompleteList = emptyList(), + optionSetConfiguration = null, + valueType = ValueType.DATE, + ), + ) + } + private fun getFieldUIModels(): List { return listOf( FieldUiModelImpl(