Skip to content

Commit

Permalink
Merge branch 'release/0.3.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrimault committed Jul 19, 2020
2 parents 788acdd + faaa3b4 commit 4c51ed8
Show file tree
Hide file tree
Showing 22 changed files with 498 additions and 262 deletions.
2 changes: 1 addition & 1 deletion commons/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

version = "0.6.9"
version = "0.7.2"

android {
compileSdkVersion 29
Expand Down
119 changes: 106 additions & 13 deletions commons/src/main/java/fr/geonature/commons/data/AbstractTaxon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.provider.BaseColumns
import androidx.room.ColumnInfo
import androidx.room.Embedded
import fr.geonature.commons.data.helper.EntityHelper.column
import fr.geonature.commons.data.helper.SQLiteSelectQueryBuilder

/**
* Base taxon.
Expand All @@ -29,38 +30,47 @@ abstract class AbstractTaxon : Parcelable {
@Embedded
val taxonomy: Taxonomy

/**
* The common name of the taxon.
*/
@ColumnInfo(name = COLUMN_NAME_COMMON)
var commonName: String?

/**
* The description of the taxon.
*/
@ColumnInfo(name = COLUMN_DESCRIPTION)
var description: String?

/**
* Whether the taxon is part of the heritage.
* The rank description of the taxon.
*/
@ColumnInfo(name = COLUMN_HERITAGE)
var heritage: Boolean = false
@ColumnInfo(name = COLUMN_RANK)
var rank: String?

constructor(
id: Long,
name: String,
taxonomy: Taxonomy,
commonName: String? = null,
description: String? = null,
heritage: Boolean = false
rank: String? = null
) {
this.id = id
this.name = name
this.taxonomy = taxonomy
this.commonName = commonName
this.description = description
this.heritage = heritage
this.rank = rank
}

constructor(source: Parcel) : this(
source.readLong(),
source.readString()!!,
source.readParcelable(Taxonomy::class.java.classLoader)!!,
source.readString(),
source.readByte() == 1.toByte()
source.readString(),
source.readString()
)

override fun equals(other: Any?): Boolean {
Expand All @@ -70,8 +80,9 @@ abstract class AbstractTaxon : Parcelable {
if (id != other.id) return false
if (name != other.name) return false
if (taxonomy != other.taxonomy) return false
if (commonName != other.commonName) return false
if (description != other.description) return false
if (heritage != other.heritage) return false
if (rank != other.rank) return false

return true
}
Expand All @@ -80,8 +91,9 @@ abstract class AbstractTaxon : Parcelable {
var result = id.hashCode()
result = 31 * result + name.hashCode()
result = 31 * result + taxonomy.hashCode()
result = 31 * result + (commonName?.hashCode() ?: 0)
result = 31 * result + (description?.hashCode() ?: 0)
result = 31 * result + heritage.hashCode()
result = 31 * result + (rank?.hashCode() ?: 0)

return result
}
Expand All @@ -101,8 +113,9 @@ abstract class AbstractTaxon : Parcelable {
taxonomy,
flags
)
it.writeString(commonName)
it.writeString(description)
it.writeByte((if (heritage) 1 else 0).toByte()) // as boolean value
it.writeString(rank)
}
}

Expand All @@ -114,8 +127,9 @@ abstract class AbstractTaxon : Parcelable {
const val COLUMN_ID = BaseColumns._ID

const val COLUMN_NAME = "name"
const val COLUMN_NAME_COMMON = "name_common"
const val COLUMN_DESCRIPTION = "description"
const val COLUMN_HERITAGE = "heritage"
const val COLUMN_RANK = "rank"

/**
* Gets the default projection.
Expand All @@ -131,12 +145,16 @@ abstract class AbstractTaxon : Parcelable {
tableAlias
),
*Taxonomy.defaultProjection(tableAlias),
column(
COLUMN_NAME_COMMON,
tableAlias
),
column(
COLUMN_DESCRIPTION,
tableAlias
),
column(
COLUMN_HERITAGE,
COLUMN_RANK,
tableAlias
)
)
Expand All @@ -163,11 +181,11 @@ abstract class AbstractTaxon : Parcelable {
internal val wheres = mutableListOf<Pair<String, Array<*>?>>()

/**
* Filter by name.
* Filter by name or description.
*
* @return this
*/
fun byNameOrDescription(queryString: String?): Filter {
fun byNameOrDescriptionOrRank(queryString: String?): Filter {
if (queryString.isNullOrBlank()) {
return this
}
Expand All @@ -177,11 +195,19 @@ abstract class AbstractTaxon : Parcelable {
"(${getColumnAlias(
COLUMN_NAME,
tableAlias
)} LIKE ? OR ${getColumnAlias(
COLUMN_NAME_COMMON,
tableAlias
)} LIKE ? OR ${getColumnAlias(
COLUMN_DESCRIPTION,
tableAlias
)} LIKE ? OR ${getColumnAlias(
COLUMN_RANK,
tableAlias
)} LIKE ?)",
arrayOf(
"%$queryString%",
"%$queryString%",
"%$queryString%",
"%$queryString%"
)
Expand Down Expand Up @@ -261,4 +287,71 @@ abstract class AbstractTaxon : Parcelable {
)
}
}

/**
* Order by query builder.
*/
open class OrderBy(internal val tableAlias: String) {
private val orderBy = mutableSetOf<Pair<String, SQLiteSelectQueryBuilder.OrderingTerm>>()

/**
* Adds an ORDER BY statement.
*
* @param columnName The selected column name on which to apply order clause.
* @param orderingTerm The ordering sort order (default: `ASC`).
*
* @return this
*/
fun by(
columnName: String,
orderingTerm: SQLiteSelectQueryBuilder.OrderingTerm = SQLiteSelectQueryBuilder.OrderingTerm.ASC
): OrderBy {
this.orderBy.add(
Pair(
getColumnAlias(
columnName,
tableAlias
),
orderingTerm
)
)

return this
}

/**
* Adds an ORDER BY statement on 'name_common' column and on 'name' column as default if 'name_common' column is null.
*
* @param orderingTerm The ordering sort order (default: `ASC`).
*
* @return this
*/
fun byCommonName(orderingTerm: SQLiteSelectQueryBuilder.OrderingTerm = SQLiteSelectQueryBuilder.OrderingTerm.ASC): OrderBy {
this.orderBy.add(
Pair(
"COALESCE(${getColumnAlias(
COLUMN_NAME_COMMON,
tableAlias
)}, ${getColumnAlias(
COLUMN_NAME,
tableAlias
)})",
orderingTerm
)
)

return this
}

/**
* Builds the ORDER BY clause.
*/
fun build(): String? {
if (this.orderBy.isEmpty()) {
return null
}

return this.orderBy.joinToString(", ") { pair -> "${pair.first} ${pair.second.name}" }
}
}
}
28 changes: 19 additions & 9 deletions commons/src/main/java/fr/geonature/commons/data/Taxon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ class Taxon : AbstractTaxon {
id: Long,
name: String,
taxonomy: Taxonomy,
commonName: String? = null,
description: String? = null,
heritage: Boolean = false
rank: String? = null
) : super(
id,
name,
taxonomy,
commonName,
description,
heritage
rank
)

private constructor(source: Parcel) : super(source)
Expand Down Expand Up @@ -103,19 +105,22 @@ class Taxon : AbstractTaxon {
)
),
taxonomy,
cursor.get(
getColumnAlias(
COLUMN_NAME_COMMON,
tableAlias
)
),
cursor.get(
getColumnAlias(
COLUMN_DESCRIPTION,
tableAlias
)
),
requireNotNull(
cursor.get(
getColumnAlias(
COLUMN_HERITAGE,
tableAlias
),
false
cursor.get(
getColumnAlias(
COLUMN_RANK,
tableAlias
)
)
)
Expand Down Expand Up @@ -148,4 +153,9 @@ class Taxon : AbstractTaxon {
* Filter query builder.
*/
class Filter : AbstractTaxon.Filter(TABLE_NAME)

/**
* Order by query builder.
*/
class OrderBy: AbstractTaxon.OrderBy(TABLE_NAME)
}
14 changes: 11 additions & 3 deletions commons/src/main/java/fr/geonature/commons/data/TaxonWithArea.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ class TaxonWithArea : AbstractTaxon {
id: Long,
name: String,
taxonomy: Taxonomy,
commonName: String? = null,
description: String? = null,
heritage: Boolean = false,
rank: String? = null,
taxonArea: TaxonArea?
) : super(
id,
name,
taxonomy,
commonName,
description,
heritage
rank
) {
this.taxonArea = taxonArea
}
Expand All @@ -34,8 +36,9 @@ class TaxonWithArea : AbstractTaxon {
taxon.id,
taxon.name,
taxon.taxonomy,
taxon.commonName,
taxon.description,
taxon.heritage
taxon.rank
)

private constructor(source: Parcel) : super(source) {
Expand Down Expand Up @@ -152,4 +155,9 @@ class TaxonWithArea : AbstractTaxon {
return this
}
}

/**
* Order by query builder.
*/
class OrderBy: AbstractTaxon.OrderBy(Taxon.TABLE_NAME)
}
10 changes: 9 additions & 1 deletion commons/src/main/java/fr/geonature/commons/data/dao/BaseDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,21 @@ abstract class BaseDao<T> {
}

/**
* Insert an array of objects in database.
* Insert an array of objects in database (Replace strategy on conflict).
*
* @param entity the objects to be inserted.
*/
@Insert(onConflict = OnConflictStrategy.REPLACE)
abstract fun insert(vararg entity: T)

/**
* Insert an array of objects in database (Ignore strategy on conflict).
*
* @param entity the objects to be inserted.
*/
@Insert(onConflict = OnConflictStrategy.IGNORE)
abstract fun insertOrIgnore(vararg entity: T)

/**
* Select entities from given raw query.
*
Expand Down
Loading

0 comments on commit 4c51ed8

Please sign in to comment.