Skip to content

Commit

Permalink
fix(PnX-SI/gn_mobile_occtax#133): fetch all taxa from new API
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrimault committed Dec 5, 2023
1 parent 039553d commit 5496c1b
Show file tree
Hide file tree
Showing 47 changed files with 795 additions and 316 deletions.
2 changes: 2 additions & 0 deletions commons/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ The authority of this content provider is `<application package>.provider`.
| **\<Base URI\>**/taxonomy/\* | String | Fetch taxonomy matching a given kingdom |
| **\<Base URI\>**/taxonomy/\*/\* | String, String | Fetch taxonomy matching a given kingdom and group |
| **\<Base URI\>**/taxa | n/a | Fetch all taxa |
| **\<Base URI\>**/taxa/list/# | Number | Fetch all taxa matching a given list ID |
| **\<Base URI\>**/taxa/area/# | Number | Fetch all taxa matching a given area ID |
| **\<Base URI\>**/taxa/list/#/area/# | Number, Number | Fetch all taxa matching a given list ID and area ID |
| **\<Base URI\>**/taxa/# | Number | Fetch a taxon by ID |
| **\<Base URI\>**/taxa/#/area/# | Number, Number | Fetch a taxon by ID matching a given area ID |
| **\<Base URI\>**/nomenclature_types | n/a | Fetch all nomenclature types |
Expand Down
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 {
id 'org.jetbrains.kotlin.android'
}

version = "1.3.4"
version = "1.4.0"

android {
namespace 'fr.geonature.commons'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import fr.geonature.commons.data.dao.NomenclatureTaxonomyDao
import fr.geonature.commons.data.dao.NomenclatureTypeDao
import fr.geonature.commons.data.dao.TaxonAreaDao
import fr.geonature.commons.data.dao.TaxonDao
import fr.geonature.commons.data.dao.TaxonListDao
import fr.geonature.commons.data.dao.TaxonomyDao
import fr.geonature.commons.data.entity.AdditionalField
import fr.geonature.commons.data.entity.AdditionalFieldDataset
Expand All @@ -29,6 +30,7 @@ import fr.geonature.commons.data.entity.NomenclatureTaxonomy
import fr.geonature.commons.data.entity.NomenclatureType
import fr.geonature.commons.data.entity.Taxon
import fr.geonature.commons.data.entity.TaxonArea
import fr.geonature.commons.data.entity.TaxonList
import fr.geonature.commons.data.entity.Taxonomy

/**
Expand All @@ -43,6 +45,7 @@ import fr.geonature.commons.data.entity.Taxonomy
Taxonomy::class,
Taxon::class,
TaxonArea::class,
TaxonList::class,
NomenclatureType::class,
Nomenclature::class,
NomenclatureTaxonomy::class,
Expand All @@ -53,7 +56,7 @@ import fr.geonature.commons.data.entity.Taxonomy
CodeObject::class,
FieldValue::class,
],
version = 20,
version = 21,
exportSchema = false
)
abstract class LocalDatabase : RoomDatabase() {
Expand Down Expand Up @@ -83,6 +86,11 @@ abstract class LocalDatabase : RoomDatabase() {
*/
abstract fun taxonAreaDao(): TaxonAreaDao

/**
* @return The DAO for the [TaxonList.TABLE_NAME] table.
*/
abstract fun taxonListDao(): TaxonListDao

/**
* @return The DAO for the [NomenclatureType.TABLE_NAME] table.
*/
Expand Down
125 changes: 82 additions & 43 deletions commons/src/main/java/fr/geonature/commons/data/MainContentProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import fr.geonature.commons.data.entity.Taxon
import fr.geonature.commons.data.entity.Taxonomy
import fr.geonature.mountpoint.model.MountPoint
import fr.geonature.mountpoint.util.FileUtils
import org.tinylog.Logger
import java.io.File
import java.io.FileNotFoundException

Expand Down Expand Up @@ -122,10 +123,20 @@ class MainContentProvider : ContentProvider() {
Taxon.TABLE_NAME,
TAXA
)
addURI(
authority,
"${Taxon.TABLE_NAME}/list/#",
TAXA_LIST_ID
)
addURI(
authority,
"${Taxon.TABLE_NAME}/area/#",
TAXA_AREA
TAXA_AREA_ID
)
addURI(
authority,
"${Taxon.TABLE_NAME}/list/#/area/#",
TAXA_LIST_AREA_ID
)
addURI(
authority,
Expand Down Expand Up @@ -184,7 +195,7 @@ class MainContentProvider : ContentProvider() {
DATASET_ID -> "$VND_TYPE_ITEM_PREFIX/$authority.${Dataset.TABLE_NAME}"
INPUT_OBSERVERS, INPUT_OBSERVERS_IDS -> "$VND_TYPE_DIR_PREFIX/$authority.${InputObserver.TABLE_NAME}"
INPUT_OBSERVER_ID -> "$VND_TYPE_ITEM_PREFIX/$authority.${InputObserver.TABLE_NAME}"
TAXA, TAXA_AREA -> "$VND_TYPE_DIR_PREFIX/$authority.${Taxon.TABLE_NAME}"
TAXA, TAXA_LIST_ID, TAXA_AREA_ID, TAXA_LIST_AREA_ID -> "$VND_TYPE_DIR_PREFIX/$authority.${Taxon.TABLE_NAME}"
TAXON_ID, TAXON_AREA_ID -> "$VND_TYPE_ITEM_PREFIX/$authority.${Taxon.TABLE_NAME}"
TAXONOMY, TAXONOMY_KINGDOM -> "$VND_TYPE_DIR_PREFIX/$authority.${Taxonomy.TABLE_NAME}"
TAXONOMY_KINGDOM_GROUP -> "$VND_TYPE_ITEM_PREFIX/$authority.${Taxonomy.TABLE_NAME}"
Expand All @@ -210,61 +221,67 @@ class MainContentProvider : ContentProvider() {
appContext,
uri
)

DATASET, DATASET_ACTIVE -> datasetQuery(
appContext,
uri
)

DATASET_ID -> datasetByIdQuery(
appContext,
uri
)

INPUT_OBSERVERS -> inputObserversQuery(
appContext,
selection,
selectionArgs
)

INPUT_OBSERVERS_IDS -> inputObserversByIdsQuery(
appContext,
uri
)

INPUT_OBSERVER_ID -> inputObserverByIdQuery(
appContext,
uri
)

TAXONOMY, TAXONOMY_KINGDOM, TAXONOMY_KINGDOM_GROUP -> taxonomyQuery(
appContext,
uri
)
TAXA -> taxaQuery(

TAXA, TAXA_LIST_ID, TAXA_AREA_ID -> taxaQuery(
appContext,
uri,
selection,
selectionArgs,
sortOrder
)

TAXON_ID -> taxonByIdQuery(
appContext,
uri
)
TAXA_AREA -> taxaWithAreaQuery(
appContext,
uri,
selection,
selectionArgs,
sortOrder
)

TAXON_AREA_ID -> taxonWithAreaByIdQuery(
appContext,
uri
)

NOMENCLATURE_TYPES -> nomenclatureTypesQuery(appContext)
NOMENCLATURE_TYPES_DEFAULT -> defaultNomenclaturesByModule(
appContext,
uri
)

NOMENCLATURE_ITEMS_TAXONOMY_KINGDOM, NOMENCLATURE_ITEMS_TAXONOMY_KINGDOM_GROUP -> nomenclaturesWithTaxonomyQuery(
appContext,
uri
)

else -> throw IllegalArgumentException("Unknown URI (query): $uri")
}
}
Expand Down Expand Up @@ -301,6 +318,7 @@ class MainContentProvider : ContentProvider() {
ParcelFileDescriptor.MODE_READ_ONLY
)
}

INPUT_ID -> {
val packageId = uri.pathSegments
.drop(uri.pathSegments.indexOf("inputs") + 1)
Expand Down Expand Up @@ -328,6 +346,7 @@ class MainContentProvider : ContentProvider() {
ParcelFileDescriptor.MODE_READ_ONLY
)
}

else -> throw IllegalArgumentException("Unknown URI (openFile): $uri")
}
}
Expand All @@ -350,6 +369,7 @@ class MainContentProvider : ContentProvider() {
values
)
}

else -> throw IllegalArgumentException("Unknown URI (insert): $uri")
}
}
Expand Down Expand Up @@ -484,6 +504,7 @@ class MainContentProvider : ContentProvider() {
lastPathSegments[0],
lastPathSegments[1]
)

else -> return@also
}
}
Expand All @@ -492,12 +513,57 @@ class MainContentProvider : ContentProvider() {

private fun taxaQuery(
context: Context,
uri: Uri,
selection: String?,
selectionArgs: Array<String>?,
sortOrder: String?
): Cursor {
return getTaxonDao(context)
.QB()
val uriRegex = "/${Taxon.TABLE_NAME}(/list/\\d+)?(/area/\\d+)?".toRegex()

val mathResult = uri.path
?.takeIf { uriRegex.matches(it) }
?.let { uriRegex.find(it) }
?: run {
Logger.warn { "invalid taxa URI: '$uri', fetch all taxa..." }

return getTaxonDao(context)
.QB()
.whereSelection(
selection,
arrayOf(
*selectionArgs
?: emptyArray()
)
)
.also {
if (sortOrder.isNullOrEmpty()) {
return@also
}

(it as TaxonDao.QB).orderBy(sortOrder)
}
.cursor()
}

val qb = getTaxonDao(context).QB()

mathResult.groupValues
.drop(1)
.filterNot { it.isBlank() }
.forEach {
val id = it
.substringAfterLast("/")
.toLongOrNull()

with(it) {
when {
startsWith("/list") -> qb.withListId(id)
startsWith("/area") -> qb.withArea(id)
}
}
}

return qb
.whereSelection(
selection,
arrayOf(
Expand Down Expand Up @@ -525,35 +591,6 @@ class MainContentProvider : ContentProvider() {
.cursor()
}

private fun taxaWithAreaQuery(
context: Context,
uri: Uri,
selection: String?,
selectionArgs: Array<String>?,
sortOrder: String?
): Cursor {
val filterOnArea = uri.lastPathSegment?.toLongOrNull()

return getTaxonDao(context)
.QB()
.withArea(filterOnArea)
.whereSelection(
selection,
arrayOf(
*selectionArgs
?: emptyArray()
)
)
.also {
if (sortOrder.isNullOrEmpty()) {
return@also
}

(it as TaxonDao.QB).orderBy(sortOrder)
}
.cursor()
}

private fun taxonWithAreaByIdQuery(
context: Context,
uri: Uri
Expand Down Expand Up @@ -701,8 +738,10 @@ class MainContentProvider : ContentProvider() {
const val TAXONOMY_KINGDOM_GROUP = 32
const val TAXA = 40
const val TAXON_ID = 41
const val TAXA_AREA = 42
const val TAXON_AREA_ID = 43
const val TAXA_LIST_ID = 42
const val TAXA_AREA_ID = 43
const val TAXA_LIST_AREA_ID = 44
const val TAXON_AREA_ID = 45
const val NOMENCLATURE_TYPES = 50
const val NOMENCLATURE_TYPES_DEFAULT = 51
const val NOMENCLATURE_ITEMS_TAXONOMY_KINGDOM = 52
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.geonature.commons.data.dao

import androidx.room.Dao
import androidx.room.Query
import fr.geonature.commons.data.entity.Dataset
import fr.geonature.commons.data.helper.EntityHelper.column
import fr.geonature.commons.data.helper.SQLiteSelectQueryBuilder.OrderingTerm.ASC
Expand All @@ -13,6 +14,14 @@ import fr.geonature.commons.data.helper.SQLiteSelectQueryBuilder.OrderingTerm.AS
@Dao
abstract class DatasetDao : BaseDao<Dataset>() {

@Query(
"""SELECT d.*
FROM ${Dataset.TABLE_NAME} d
WHERE d.${Dataset.COLUMN_ID} = :datasetId
"""
)
abstract suspend fun findById(datasetId: Long): Dataset?

/**
* Internal query builder for [Dataset].
*/
Expand Down
33 changes: 33 additions & 0 deletions commons/src/main/java/fr/geonature/commons/data/dao/TaxonDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import androidx.room.Query
import fr.geonature.commons.data.entity.AbstractTaxon
import fr.geonature.commons.data.entity.Taxon
import fr.geonature.commons.data.entity.TaxonArea
import fr.geonature.commons.data.entity.TaxonList
import fr.geonature.commons.data.helper.EntityHelper.column
import fr.geonature.commons.data.helper.SQLiteSelectQueryBuilder

/**
* Data access object for [Taxon].
Expand Down Expand Up @@ -57,6 +59,37 @@ abstract class TaxonDao : BaseDao<Taxon>() {
selectQueryBuilder.columns(*Taxon.defaultProjection())
}

fun withListId(listId: Long?): QB {
if (listId == null) return this

selectQueryBuilder
.columns(*TaxonList.defaultProjection())
.join(
SQLiteSelectQueryBuilder.JoinOperator.DEFAULT,
TaxonList.TABLE_NAME,
"${
column(
TaxonList.COLUMN_TAXON_ID,
TaxonList.TABLE_NAME
).second
} = ${
column(
AbstractTaxon.COLUMN_ID,
entityTableName
).second
} AND ${
column(
TaxonList.COLUMN_TAXA_LIST_ID,
TaxonList.TABLE_NAME
).second
} = ?",
TaxonList.TABLE_NAME,
listId
)

return this
}

fun withArea(id: Long?): QB {
if (id == null) return this

Expand Down
Loading

0 comments on commit 5496c1b

Please sign in to comment.