Skip to content

Commit

Permalink
feat(PnX-SI/gn_mobile_core#41): filter automatically taxa list accord…
Browse files Browse the repository at this point in the history
…ing to the current selected dataset
  • Loading branch information
sgrimault committed Mar 2, 2024
1 parent 4bb7dbc commit 3ca4603
Show file tree
Hide file tree
Showing 16 changed files with 179 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ObservationRecordRemoteDataSourceImpl(private val occtaxAPIClient: IOcctax
geometry = observationRecord.geometry
).apply {
comment.comment = observationRecord.comment.comment
dataset.datasetId = observationRecord.dataset.datasetId
dataset.dataset = observationRecord.dataset.dataset
dates.start = observationRecord.dates.start
dates.end = observationRecord.dates.end
observationRecord.observers.getAllObserverIds()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fr.geonature.occtax.features.record.domain

import android.os.Parcelable
import fr.geonature.commons.data.entity.AbstractTaxon
import fr.geonature.commons.data.entity.Dataset
import kotlinx.parcelize.IgnoredOnParcel
import kotlinx.parcelize.Parcelize
import org.locationtech.jts.geom.Geometry
Expand Down Expand Up @@ -128,19 +129,36 @@ class DatasetRecord(private val properties: SortedMap<String, PropertyValue>) {
/**
* The current selected dataset of this observation record.
*/
var datasetId: Long?
get() = properties[DATASET_ID_KEY].takeIf { it is PropertyValue.Number }
?.let { it as PropertyValue.Number }?.value?.toLong()
var dataset: PropertyValue.Dataset?
get() = properties[DATASET_ID_KEY].takeIf { it is PropertyValue.Dataset }
?.let { it as PropertyValue.Dataset }
set(value) {
PropertyValue.Number(
PropertyValue.Dataset(
DATASET_ID_KEY,
value
value?.datasetId,
value?.taxaListId
)
.also {
properties[it.code] = it
if (it.isEmpty()) properties.remove(DATASET_ID_KEY)
else properties[it.code] = it
}
}

fun setDataset(dataset: Dataset?) {
this.dataset = PropertyValue.Dataset(
DATASET_ID_KEY,
dataset?.id,
dataset?.taxaListId
)
}

fun setDatasetId(datasetId: Long?) {
this.dataset = PropertyValue.Dataset(
DATASET_ID_KEY,
datasetId
)
}

companion object {
const val DATASET_ID_KEY = "id_dataset"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ sealed class PropertyValue : Parcelable {
is StringArray -> value.isEmpty()
is Number -> value == null
is NumberArray -> value.isEmpty()
is Dataset -> datasetId == null
is Nomenclature -> value == null
is AdditionalField -> value.all { it.value.isEmpty() }
is Taxa -> value.all { taxon -> taxon.properties.all { it.value.isEmpty() } }
Expand All @@ -37,6 +38,7 @@ sealed class PropertyValue : Parcelable {
is StringArray -> code
is Number -> code
is NumberArray -> code
is Dataset -> code
is Nomenclature -> code
is AdditionalField -> code
is Taxa -> code
Expand Down Expand Up @@ -112,6 +114,13 @@ sealed class PropertyValue : Parcelable {
}
}

/**
* As dataset.
*/
@Parcelize
data class Dataset(val code: String, val datasetId: Long?, val taxaListId: Long? = null) :
PropertyValue()

/**
* As nomenclature value.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package fr.geonature.occtax.features.record.io

import android.util.JsonReader
import android.util.JsonToken
import fr.geonature.commons.util.nextLongOrNull
import fr.geonature.commons.util.nextStringOrNull
import fr.geonature.commons.util.toDate
import fr.geonature.maps.jts.geojson.io.GeoJsonReader
import fr.geonature.occtax.features.record.domain.DatasetRecord
import fr.geonature.occtax.features.record.domain.ObservationRecord
import fr.geonature.occtax.features.record.domain.ObserversRecord
import fr.geonature.occtax.features.record.domain.PropertyValue
Expand Down Expand Up @@ -66,6 +68,7 @@ class ObservationRecordJsonReader {
internalId = it
)
}

"status" -> observationRecord = observationRecord.copy(
status = runCatching {
reader
Expand All @@ -74,14 +77,17 @@ class ObservationRecordJsonReader {
}.getOrNull()
?: ObservationRecord.Status.DRAFT
)

"geometry" -> observationRecord = readGeometry(
reader,
observationRecord
)

"properties" -> observationRecord = readProperties(
reader,
observationRecord
)

else -> reader.skipValue()
}
}
Expand Down Expand Up @@ -110,9 +116,11 @@ class ObservationRecordJsonReader {
reader.nextNull()
observationRecord
}

JsonToken.BEGIN_OBJECT -> {
observationRecord.copy(geometry = GeoJsonReader().readGeometry(reader))
}

else -> {
reader.skipValue()
observationRecord
Expand All @@ -132,18 +140,26 @@ class ObservationRecordJsonReader {
when (val keyName = reader.nextName()) {
"internal_id" -> updatedObservationRecord =
updatedObservationRecord.copy(internalId = reader.nextLong())

"dataset" -> reader.skipValue()
// ignore legacy "default" property
"default" -> reader.skipValue()
"digitiser" -> reader.skipValue()

DatasetRecord.DATASET_ID_KEY -> {
updatedObservationRecord.dataset.setDatasetId(reader.nextLongOrNull())
}

ObserversRecord.OBSERVERS_KEY -> readObservers(
reader,
observationRecord
)

TaxaRecord.TAXA_KEY -> readTaxa(
reader,
observationRecord
)

else -> {
if (keyName.startsWith("id_nomenclature")) {
readNomenclatureValue(
Expand All @@ -165,11 +181,13 @@ class ObservationRecordJsonReader {
.also {
updatedObservationRecord.properties[it.first] = it.second
}

JsonToken.NUMBER -> updatedObservationRecord.properties[keyName] =
PropertyValue.Number(
keyName,
reader.nextLong()
)

else -> reader.skipValue()
}
}
Expand Down Expand Up @@ -246,6 +264,7 @@ class ObservationRecordJsonReader {
JsonToken.NUMBER -> {
observationRecord.observers.addObserverId(reader.nextLong())
}

JsonToken.BEGIN_OBJECT -> {
reader.beginObject()
while (reader.hasNext()) {
Expand All @@ -256,11 +275,13 @@ class ObservationRecordJsonReader {
}
reader.endObject()
}

else -> reader.skipValue()
}
}
reader.endArray()
}

else -> reader.skipValue()
}
}
Expand All @@ -283,11 +304,13 @@ class ObservationRecordJsonReader {
observationRecord
)
}

else -> reader.skipValue()
}
}
reader.endArray()
}

else -> reader.skipValue()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,21 +158,28 @@ class ObservationRecordJsonWriter {
when (val propertyValue = it.value) {
is PropertyValue.Text -> writer.name(propertyValue.code)
.value(propertyValue.value)

is PropertyValue.Number -> writer.name(propertyValue.code)
.value(propertyValue.value)

is PropertyValue.NumberArray -> {
writer.name(propertyValue.code)
.beginArray()
propertyValue.value.forEach { value -> writer.value(value) }
writer.endArray()
}

is PropertyValue.Dataset -> writer.name(propertyValue.code)
.value(propertyValue.datasetId)

is PropertyValue.Nomenclature -> {
// GeoNature default properties mapping
when (propertyValue.code) {
"TYP_GRP" -> writer.name("id_nomenclature_grp_typ")
.value(propertyValue.value)
}
}

is PropertyValue.Taxa -> {
writer.name(propertyValue.code)
.beginArray()
Expand All @@ -185,6 +192,7 @@ class ObservationRecordJsonWriter {
}
writer.endArray()
}

else -> {}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class SetDefaultNomenclatureValuesUseCase @Inject constructor(
val editableFieldsInformation =
nomenclatureRepository.getEditableFields(EditableField.Type.INFORMATION)
.getOrElse { emptyList() } + if (params.withAdditionalFields) additionalFieldRepository.getAllAdditionalFields(
observationRecord.dataset.datasetId,
observationRecord.dataset.dataset?.datasetId,
EditableField.Type.INFORMATION
)
.getOrElse { emptyList() } else emptyList()
Expand All @@ -68,7 +68,7 @@ class SetDefaultNomenclatureValuesUseCase @Inject constructor(
val editableFieldsCounting =
nomenclatureRepository.getEditableFields(EditableField.Type.COUNTING)
.getOrElse { emptyList() } + if (params.withAdditionalFields) additionalFieldRepository.getAllAdditionalFields(
observationRecord.dataset.datasetId,
observationRecord.dataset.dataset?.datasetId,
EditableField.Type.COUNTING
)
.getOrElse { emptyList() } else emptyList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,10 @@ class InputPagerFragmentActivity : AbstractPagerFragmentActivity(),

override fun startEditTaxon() {
pageFragmentViewModel.add(
R.string.pager_fragment_taxa_title to TaxaFragment.newInstance(appSettings.areaObservationDuration),
R.string.pager_fragment_taxa_title to TaxaFragment.newInstance(
appSettings.areaObservationDuration,
appSettings.dataSyncSettings.taxrefListId.toLong()
),
R.string.pager_fragment_information_title to InformationFragment.newInstance(
saveDefaultValues = appSettings.nomenclatureSettings?.saveDefaultValues ?: false,
withAdditionalFields = appSettings.nomenclatureSettings?.withAdditionalFields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ class CountingFragment : AbstractInputFragment() {

private fun launchEditCountingMetadataActivity(countingRecord: CountingRecord? = null) {
val context = context ?: return
val datasetId = observationRecord?.dataset?.datasetId
val datasetId = observationRecord?.dataset?.dataset?.datasetId
val taxonRecord = observationRecord?.taxa?.selectedTaxonRecord ?: return

editCountingResultLauncher.launch(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ class InformationFragment : AbstractInputFragment() {

override fun refreshView() {
nomenclatureViewModel.getEditableFields(
observationRecord?.dataset?.datasetId,
observationRecord?.dataset?.dataset?.datasetId,
arguments?.getBoolean(
ARG_WITH_ADDITIONAL_FIELDS,
false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class ObserversAndDateInputFragment : AbstractInputFragment() {
null,
null
)

LOADER_DATASET_ID -> CursorLoader(
requireContext(),
buildUri(
Expand All @@ -117,6 +118,7 @@ class ObserversAndDateInputFragment : AbstractInputFragment() {
null,
null
)

else -> throw IllegalArgumentException()
}
}
Expand Down Expand Up @@ -163,17 +165,23 @@ class ObserversAndDateInputFragment : AbstractInputFragment() {

updateSelectedObservers(inputObserversLoaded)
}

LOADER_DATASET_ID -> {
if (data.count == 0) {
selectedDataset = null
observationRecord?.dataset?.datasetId = null
observationRecord?.dataset?.setDatasetId(null)
listener.validateCurrentPage()
}

if (data.moveToFirst()) {
selectedDataset = Dataset.fromCursor(data)
}

selectedDataset?.also {
Logger.info { "selected dataset: ${it.id}, taxa list ID: ${it.taxaListId}" }
}

observationRecord?.dataset?.setDataset(selectedDataset)
updateSelectedDatasetActionView(selectedDataset)
}
}
Expand Down Expand Up @@ -315,7 +323,7 @@ class ObserversAndDateInputFragment : AbstractInputFragment() {
override fun validate(): Boolean {
return observationRecord?.observers?.getAllObserverIds()
?.isNotEmpty() ?: false &&
this.observationRecord?.dataset?.datasetId != null &&
this.observationRecord?.dataset?.dataset?.datasetId != null &&
this.observationRecord?.properties?.filterValues { it is PropertyValue.Nomenclature }
?.isNotEmpty() == true &&
inputDateView?.hasErrors() == false
Expand Down Expand Up @@ -353,7 +361,7 @@ class ObserversAndDateInputFragment : AbstractInputFragment() {
}
}

val selectedDatasetId = observationRecord?.dataset?.datasetId
val selectedDatasetId = observationRecord?.dataset?.dataset?.datasetId

if (selectedDatasetId != null) {
LoaderManager.getInstance(this)
Expand Down Expand Up @@ -413,11 +421,13 @@ class ObserversAndDateInputFragment : AbstractInputFragment() {
}

private fun updateSelectedDataset(selectedDataset: Dataset?) {
selectedDataset?.also {
Logger.info { "selected dataset: ${it.id}, taxa list ID: ${it.taxaListId}" }
}

this.selectedDataset = selectedDataset

observationRecord?.dataset?.also {
it.datasetId = selectedDataset?.id
}
observationRecord?.dataset?.setDataset(selectedDataset)

listener.validateCurrentPage()

Expand Down Expand Up @@ -453,10 +463,10 @@ class ObserversAndDateInputFragment : AbstractInputFragment() {

private fun setDefaultDatasetFromSettings() {
observationRecord?.dataset?.run {
if (datasetId == null) {
if (dataset?.datasetId == null) {
val context = context ?: return
getDefaultDatasetId(context).also { defaultDatasetId ->
if (defaultDatasetId != null) this.datasetId = defaultDatasetId
if (defaultDatasetId != null) setDatasetId(defaultDatasetId)
}
}
}
Expand Down
Loading

0 comments on commit 3ca4603

Please sign in to comment.