Skip to content

Commit

Permalink
Merge pull request #1103 from PhenoApps/brapi_min_max_fix
Browse files Browse the repository at this point in the history
replace deprecated min and max methods in mapTraits, add debug logging
  • Loading branch information
trife authored Dec 11, 2024
2 parents f653c1d + 34026c3 commit 4eea855
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1355,15 +1355,14 @@ private Pair<List<TraitObject>, Integer> mapTraits(List<BrAPIObservationVariable
if (var.getScale() != null) {
if (var.getScale().getValidValues() != null) {

if (var.getScale().getValidValues().getMin() != null) {
trait.setMinimum(var.getScale().getValidValues().getMin().toString());
if (var.getScale().getValidValues().getMinimumValue() != null) {
trait.setMinimum(var.getScale().getValidValues().getMinimumValue());
} else {
// Fieldbook requires empty string for min and maxes.
trait.setMinimum("");
}

if (var.getScale().getValidValues().getMax() != null) {
trait.setMaximum(var.getScale().getValidValues().getMax().toString());
if (var.getScale().getValidValues().getMaximumValue() != null) {
trait.setMaximum(var.getScale().getValidValues().getMaximumValue());
} else {
trait.setMaximum("");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.annotation.SuppressLint
import android.content.ContentValues
import android.database.Cursor
import android.database.MatrixCursor
import android.util.Log
import com.fieldbook.tracker.database.*
import com.fieldbook.tracker.database.Migrator.ObservationVariable
import com.fieldbook.tracker.database.Migrator.ObservationVariableAttribute
Expand Down Expand Up @@ -319,37 +320,57 @@ class ObservationVariableDao {
//TODO missing obs. vars. for min/max/categories
fun insertTraits(t: TraitObject) = withDatabase { db ->

if (getTraitByName(t.name) != null) -1
else {
if (getTraitByName(t.name) != null) {
Log.d("ObservationVariableDao", "Trait ${t.name} already exists, skipping insertion.")
-1
} else {
val contentValues = ContentValues().apply {
put("external_db_id", t.externalDbId)
put("trait_data_source", t.traitDataSource)
put("observation_variable_name", t.name)
put("observation_variable_details", t.details)
put("observation_variable_field_book_format", t.format)
put("default_value", t.defaultValue)
put("visible", t.visible.toString())
put("position", t.realPosition)
put("additional_info", t.additionalInfo)
}

val varRowId = db.insert(
ObservationVariable.tableName, null,
ContentValues().apply {
// put(PK, t.id)
put("external_db_id", t.externalDbId)
put("trait_data_source", t.traitDataSource)
put("observation_variable_name", t.name)
put("observation_variable_details", t.details)
put("observation_variable_field_book_format", t.format)
put("default_value", t.defaultValue)
put("visible", t.visible.toString())
put("position", t.realPosition)
put("additional_info", t.additionalInfo)
})

ObservationVariableValueDao.insert(
// Log trait values being inserted
Log.d("ObservationVariableDao", "Saving trait ${t.name} with values:")
contentValues.keySet().forEach { key ->
contentValues.get(key)?.let { value ->
Log.d("ObservationVariableDao", "$key: $value")
}
}

// Log additional values: min, max, categories
Log.d("ObservationVariableDao", "And additional attributes:")
Log.d("ObservationVariableDao", "minimum: ${t.minimum.orEmpty()}")
Log.d("ObservationVariableDao", "maximum: ${t.maximum.orEmpty()}")
Log.d("ObservationVariableDao", "categories: ${t.categories.orEmpty()}")
Log.d("ObservationVariableDao", "closeKeyboardOnOpen: ${t.closeKeyboardOnOpen ?: "false"}")

val varRowId = db.insert(ObservationVariable.tableName, null, contentValues)

if (varRowId != -1L) {
ObservationVariableValueDao.insert(
t.minimum.orEmpty(),
t.maximum.orEmpty(),
t.categories.orEmpty(),
(t.closeKeyboardOnOpen ?: "false").toString(),
varRowId.toString())
varRowId.toString()
)
Log.d("ObservationVariableDao", "Trait ${t.name} inserted successfully with row ID: $varRowId")
} else {
Log.e("ObservationVariableDao", "Failed to insert trait ${t.name}")
}

varRowId

}

} ?: -1


fun deleteTrait(id: String) = withDatabase { db ->
db.delete(ObservationVariable.tableName,
"${ObservationVariable.PK} = ?", arrayOf(id))
Expand Down

0 comments on commit 4eea855

Please sign in to comment.