Skip to content

Commit

Permalink
fix(PnX-SI/gn_mobile_occtax#269): keep only additional fields with va…
Browse files Browse the repository at this point in the history
…lid datasets...
  • Loading branch information
sgrimault committed Nov 14, 2024
1 parent 637f7ff commit 0ffa6c6
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 10 deletions.
2 changes: 1 addition & 1 deletion commons/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
alias(libs.plugins.kotlin.android)
}

version = "1.4.6"
version = "1.4.7"

android {
namespace 'fr.geonature.commons'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import fr.geonature.commons.features.dataset.error.DatasetException
* @author S. Grimault
*/
class DatasetLocalDataSourceImpl(private val datasetDao: DatasetDao) : IDatasetLocalDataSource {

override suspend fun getAllDatasets(): List<Dataset> {
return datasetDao.findAll()
}

override suspend fun findDatasetById(datasetId: Long): Dataset {
return datasetDao.findById(datasetId)
?: throw DatasetException.NoDatasetFoundException(datasetId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ import fr.geonature.commons.features.dataset.error.DatasetException
*/
interface IDatasetLocalDataSource {

/**
* Gets all [Dataset].
*
* @return a list of [Dataset]
*/
suspend fun getAllDatasets(): List<Dataset>

/**
* Finds [Dataset] matching given taxon ID.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ sealed class NomenclatureException(
/**
* Failure about no [NomenclatureType] found locally.
*/
object NoNomenclatureTypeFoundException : NomenclatureException()
object NoNomenclatureTypeFoundException : NomenclatureException()

/**
* Failure about no [Nomenclature] found from given mnemonic.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ class DatasetLocalDataSourceTest {
db.close()
}

@Test
fun `should find all dataset`() =
runTest {
val expectedDataset = initializeDataset()

val datasets = datasetLocalDataSource.getAllDatasets()

assertEquals(
expectedDataset,
datasets
)
}

@Test
fun `should find dataset matching given ID`() =
runTest {
Expand Down
4 changes: 2 additions & 2 deletions commons/version.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#Sat Sep 07 15:08:49 CEST 2024
VERSION_CODE=5130
#Thu Nov 14 21:36:07 CET 2024
VERSION_CODE=5187
2 changes: 1 addition & 1 deletion datasync/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
alias(libs.plugins.kotlin.android)
}

version = "0.6.2"
version = "0.6.3"

android {
namespace 'fr.geonature.datasync'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import dagger.hilt.components.SingletonComponent
import fr.geonature.commons.data.GeoNatureModuleName
import fr.geonature.commons.data.LocalDatabase
import fr.geonature.commons.data.dao.AppSyncDao
import fr.geonature.commons.features.dataset.data.IDatasetLocalDataSource
import fr.geonature.commons.features.nomenclature.data.IAdditionalFieldLocalDataSource
import fr.geonature.datasync.api.IGeoNatureAPIClient
import fr.geonature.datasync.sync.repository.ISynchronizeAdditionalFieldsRepository
Expand Down Expand Up @@ -45,12 +46,14 @@ object DataSyncModule {
fun provideSynchronizeAdditionalFieldsRepository(
@ApplicationContext appContext: Context,
@GeoNatureModuleName moduleName: String,
datasetLocalDataSource: IDatasetLocalDataSource,
additionalFieldLocalDataSource: IAdditionalFieldLocalDataSource,
geoNatureAPIClient: IGeoNatureAPIClient,
): ISynchronizeAdditionalFieldsRepository {
return SynchronizeAdditionalFieldsRepositoryImpl(
appContext,
moduleName,
datasetLocalDataSource,
additionalFieldLocalDataSource,
geoNatureAPIClient
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fr.geonature.datasync.sync.repository

import android.content.Context
import androidx.work.WorkInfo
import fr.geonature.commons.features.dataset.data.IDatasetLocalDataSource
import fr.geonature.commons.features.nomenclature.data.IAdditionalFieldLocalDataSource
import fr.geonature.datasync.R
import fr.geonature.datasync.api.IGeoNatureAPIClient
Expand All @@ -26,6 +27,7 @@ interface ISynchronizeAdditionalFieldsRepository : ISynchronizeLocalDataReposito
class SynchronizeAdditionalFieldsRepositoryImpl(
private val context: Context,
private val moduleName: String,
private val datasetLocalDataSource: IDatasetLocalDataSource,
private val additionalFieldLocalDataSource: IAdditionalFieldLocalDataSource,
private val geoNatureAPIClient: IGeoNatureAPIClient
) : ISynchronizeAdditionalFieldsRepository {
Expand Down Expand Up @@ -58,10 +60,24 @@ class SynchronizeAdditionalFieldsRepositoryImpl(
return@flow
}

Logger.info { "${additionalFields.size} additional field(s) found" }
Logger.info { "${additionalFields.size} additional field(s) found from API" }

// keep only additional fields with valid datasets
val datasetIds = datasetLocalDataSource
.getAllDatasets()
.map { it.id }
val validAdditionalFields = additionalFields.mapNotNull {
if (it.datasetIds.isEmpty() || datasetIds.isEmpty()) it
else it.datasetIds
.filter { id -> datasetIds.contains(id) }
.takeIf { ids -> ids.isNotEmpty() }
?.let { ids -> it.copy(datasetIds = ids) }
}

Logger.info { "updating ${validAdditionalFields.size} valid additional field(s)..." }

runCatching {
additionalFieldLocalDataSource.updateAdditionalFields(*additionalFields.toTypedArray())
additionalFieldLocalDataSource.updateAdditionalFields(*validAdditionalFields.toTypedArray())
}.onFailure {
emit(
DataSyncStatus(
Expand All @@ -76,7 +92,7 @@ class SynchronizeAdditionalFieldsRepositoryImpl(
state = WorkInfo.State.SUCCEEDED,
syncMessage = context.getString(
R.string.sync_data_additional_fields,
additionalFields.size
validAdditionalFields.size
)
)
)
Expand Down
4 changes: 2 additions & 2 deletions datasync/version.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#Sun Sep 01 15:08:46 CEST 2024
VERSION_CODE=1230
#Thu Nov 14 21:36:07 CET 2024
VERSION_CODE=1287

0 comments on commit 0ffa6c6

Please sign in to comment.