How to parcelize Google's Place class? - arraylist

in my custom object, i have 7 attributes, 5 of them are strings, and were auto genereated in the contructor fine, but the other two did not generate automatically. the last two are of class Place and ArrayList<int>:
class Spot() : Parcelable{
private var uid: String? = null
private var timeFrom: String? = null
private var timeTo: String? = null
private var rate: String? = null
private var description: String? = null
private var place: Place? = null
private var days : ArrayList<Int>? = null
constructor(parcel: Parcel) : this() {
uid = parcel.readString()
timeFrom = parcel.readString()
timeTo = parcel.readString()
rate = parcel.readString()
description = parcel.readString()
}
...
How do i parcelize them?

There are several methods to do it. You need to add the code yourself for ArrayList and Place Object in the constructor and writeToParcel methods.
constructor
constructor(parcel: Parcel) : this() {
uid = parcel.readString()
timeFrom = parcel.readString()
timeTo = parcel.readString()
rate = parcel.readString()
description = parcel.readString()
// Add the below to line of code
days = parcel.readArrayList(Int::class.java.classLoader) as ArrayList<Int>?
place = parcel.readParcelable<Place>(Place::class.java.classLoader)
}
writeToParcel
override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeString(uid)
parcel.writeString(timeFrom)
parcel.writeString(timeTo)
parcel.writeString(rate)
parcel.writeString(description)
// Add the below to line of code
parcel.writeList(days)
parcel.writeParcelable(place, flags)
}
And if I'm not wrong, Googles Place class is already Parcelable so this code should work.
Hope it helps!

Related

Testing a private method in Kotlin

I'm trying to test a private method which takes the below parameters as input :
ClassToBeTested
delete(
someId1: String,
aList: List<CustomObject>,
someId2: String,
someId3: String,
someId4: String
) : Long? {
}
TestClass
val method = ClassToBeTested.javaClass.getDeclaredMethod("delete", String::class.java,
List::class.java, String::class.java, String::class.java, String::class.java)
method.isAccessible = true
val testId = method.invoke(ClassToBeTested, someId1, aList, someId2, someId3, someId4)
I end up getting the below error :
java.lang.NoSuchMethodException:
ClassToBeTested$Companion.delete(java.lang.String, java.util.Arrays$ArrayList, java.lang.String, java.lang.String, java.lang.String)
When I tried changing the above method declaration as :
val method = ClassToBeTested.javaClass.getDeclaredMethod("delete", String::class.java,
List<CustomObject>::class.java, String::class.java, String::class.java, String::class.java)
I get the below error :
Kotlin: Only classes are allowed on the left hand side of a class literal
Is there anyway we can test the private function which takes a parameter of List of Custom Objects?
Don't use reflection for this. Simply make the method internal and call it directly from your tests. You can even add an annotation like #VisibleForTesting or #TestOnly, those are available in various libraries.
You can use Kotlin's declaredMemberFunctions property to find it based on its name alone:
data class CustomObject(val id: String)
class ClassToBeTested {
private fun delete(
someId1: String,
aList: List<CustomObject>,
someId2: String,
someId3: String,
someId4: String
): Long? {
return someId1.toLongOrNull()
}
}
class SampleTest {
#Test
fun test1() {
val instance = ClassToBeTested()
val method = instance::class.declaredMemberFunctions.first { it.name == "delete" }.apply { isAccessible = true }
val result = method.call(instance, "1", emptyList<CustomObject>(), "2", "3", "4")
assertEquals(1L, result)
}
}

Returning one of different object types from single function in kotlin

I have the following structure at present:
#Entity
#Table(name = "table_app_settings")
data class AppSetting(
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "app_setting_id")
val id: Long? = null,
#Column(name = "app_setting_name")
val name: String = "",
#Column(name = "app_setting_value")
var value: String = "",
#Column(name = "app_setting_type")
val type: AppSettingType,
)
enum class AppSettingType {
CHAR,
STRING,
BYTE,
SHORT,
INT,
LONG,
DOUBLE,
FLOAT,
BOOLEAN,
}
This is then saved to the database with the following:
override fun saveAppSetting(setting: AppSetting): DatabaseResult<AppSetting> {
log.info("Saving App Setting ${setting.name} to database.")
return try {
// Attempt to save the entity to the database. If we do not throw an exception, return success.
val savedSetting = appSettingsRepository.save(setting)
DatabaseResult(
code = ResultCode.CREATION_SUCCESS,
entity = savedSetting
)
} catch(exception: DataAccessException) {
log.error("Unable to save App Setting ${setting.name} to database. Reason: ${exception.message}")
DatabaseResult(
code = ResultCode.CREATION_FAILURE
)
}
}
Now, let's say that I wish to save a Char type to database, I figure I would use the following:
override fun saveAppSetting(name: String, value: Char): DatabaseResult<Char> {
val appSettingResult = saveAppSetting(AppSetting(
name = name,
value = value.toString(),
type = AppSettingType.CHAR,
))
return if(appSettingResult.code != ResultCode.CREATION_FAILURE) {
val entity = getAppSetting<Char>(appSettingResult.entity?.name!!).entity.toString().first()
DatabaseResult(
code = appSettingResult.code,
entity = entity
)
} else {
DatabaseResult(
code = ResultCode.CREATION_FAILURE,
)
}
}
I also figured that I would need to do the following in order to retrieve the correct object type:
override fun getAppSetting(name: String): DatabaseResult<Any?> {
log.info("Getting App Setting $name from database.")
val appSetting = appSettingsRepository.findAppSettingByName(name)
return if(appSetting != null) {
log.info("App Setting $name has ID of ${appSetting.id} within the database")
when(appSetting.type) {
AppSettingType.CHAR -> {
DatabaseResult<Char>(
code = ResultCode.FETCH_SUCCESS,
entity = appSetting.value.first(),
)
}
AppSettingType.STRING -> {
DatabaseResult<String>(
code = ResultCode.FETCH_SUCCESS,
entity = appSetting.value,
)
}
AppSettingType.BYTE -> {
DatabaseResult<Byte>(
code = ResultCode.FETCH_SUCCESS,
entity = appSetting.value.toByte(),
)
}
AppSettingType.SHORT -> {
DatabaseResult<Short>(
code = ResultCode.FETCH_SUCCESS,
entity = appSetting.value.toShort(),
)
}
AppSettingType.INT -> {
DatabaseResult<Int>(
code = ResultCode.FETCH_SUCCESS,
entity = appSetting.value.toInt(),
)
}
AppSettingType.LONG -> {
DatabaseResult<Long>(
code = ResultCode.FETCH_SUCCESS,
entity = appSetting.value.toLong(),
)
}
AppSettingType.DOUBLE -> {
DatabaseResult<Double>(
code = ResultCode.FETCH_SUCCESS,
entity = appSetting.value.toDouble(),
)
}
AppSettingType.FLOAT -> {
DatabaseResult<Float>(
code = ResultCode.FETCH_SUCCESS,
entity = appSetting.value.toFloat()
)
}
AppSettingType.BOOLEAN -> {
DatabaseResult<Boolean>(
code = ResultCode.FETCH_SUCCESS,
entity = appSetting.value.toBoolean()
)
}
}
} else {
log.error("App Setting $name does not seem to exist within the database.")
DatabaseResult(
code = ResultCode.FETCH_FAILURE
)
}
However, when I then wish to use said object, I still have to write something like the following:
val newBarcode = getAppSetting("barcode_value").entity.toString().toInt()
Assuming I've "initialised" barcode_value with a value of 177 (for example).
How can I get the function to return what I need without having to do .toString.to...()?
Yes this all possible, here is a simplified demo, firstly
import kotlin.reflect.KClass
data class AppSetting(
val id: Long? = null,
val name: String = "",
var value: String = "",
val type: AppSettingType,
)
enum class AppSettingType(val clazz: KClass<out Any>) {
CHAR(Char::class),
STRING(String::class),
INT(Int::class),
}
So I added a clazz so from the enum we know the Kotlin type
and now a function to simulate your repository fetch
fun findAppSettingByName(name: String): AppSetting? {
return when(name) {
"Char thing" -> AppSetting(value= "C", type = AppSettingType.CHAR)
"String thing" -> AppSetting(value= "Str", type = AppSettingType.STRING)
"Int thing" -> AppSetting(value= "42", type = AppSettingType.INT)
else -> throw IllegalArgumentException()
}
}
Next in the function declaration I have made it generic with T and for the purposes of the demo removed the DatabaseResult container. Then I added a clazz parameter which is the typical Java way of carrying the required class information into the function:
fun <T : Any> getAppSetting(name: String, clazz: KClass<T>): T? {
val appSetting: AppSetting? = findAppSettingByName(name)
return appSetting?.let {
require(clazz == appSetting.type.clazz) {
"appSetting.type=${appSetting.type.clazz} mismatched with requested class=${clazz}"
}
when (appSetting.type) {
AppSettingType.CHAR -> appSetting.value.first()
AppSettingType.STRING -> appSetting.value
AppSettingType.INT -> appSetting.value.toInt()
} as T
}
}
the as T is important to cast the values into the required return type - this is unchecked but the when() clause should be creating the correct types.
Now let's test it:
val c1: Char? = getAppSetting("Char thing", Char::class)
val s1: String? = getAppSetting("String thing", String::class)
val i1: Int? = getAppSetting("Int thing", Int::class)
println("c1=$c1 s1=$s1 i1=$i1")
val c2: Char? = getAppSetting("Char thing")
val s2: String? = getAppSetting("String thing")
val i2: Int? = getAppSetting("Int thing")
println("c2=$c2 s2=$s2 i2=$i2")
}
The output is
c1=C s1=Str i1=42
c2=C s2=Str i2=42
But how do c2/s2/i2 work, the final part is this function
inline fun <reified T : Any> getAppSetting(name: String) = getAppSetting(name, T::class)
This is reified generic parameters... there is no need to pass the clazz because this can be found from the data type of the receiving variable.
There are many articles about this advanced topic, e.g.
https://typealias.com/guides/getting-real-with-reified-type-parameters/
https://medium.com/kotlin-thursdays/introduction-to-kotlin-generics-reified-generic-parameters-7643f53ba513
Now, I didn't completely answer what you wanted because you wanted to receive a DatabaseResult<T> wrapper. What might be possible, is to have a function that returns DatabaseResult<T> and you can obtain the T from it as the "clazz" parameter, but I'll leave that for someone else to improve on :-) but I think that gets you pretty close.

FOREIGN KEY constraint failed (code 787 SQLITE_CONSTRAINT_FOREIGNKEY) when add data to table room

I have to tables CheckListModel and CheckListPoints, it is one to n relationship, when i try to add
data in DB CheckListModel adds correctly, but when code gose to add CheckListPoints i got this error. I have no idea why this happening
This is my DB
#Database(entities = [CheckListModel::class,CheckListPoints::class],version = 4,exportSchema = false)
abstract class CheckListDB : RoomDatabase() {
abstract fun checkListDBDao():CheckListModelDBDao
companion object {
#Volatile
private var instance: CheckListDB? = null
fun getInstance(context: Context):CheckListDB{
return instance ?: synchronized(this){
instance?: buildDatabase(context).also { instance = it }
}
}
private fun buildDatabase(context: Context): CheckListDB{
return Room.databaseBuilder(context,CheckListDB::class.java,"check_list_model").fallbackToDestructiveMigration().build()
}
}
}
entityes
#Entity(
tableName = "check_list_point",
foreignKeys = [
ForeignKey(entity = CheckListModel::class, parentColumns = ["checkListModelID"],childColumns = ["checkListColumnID"],onDelete = ForeignKey.CASCADE)
],
indices = [Index("checkListColumnID")]
)
data class CheckListPoints(
#ColumnInfo(name = "correctly")
var correctly: Boolean,
#ColumnInfo(name = "requirement")
var requirement: String,
#ColumnInfo(name = "passed")
var passed: Boolean,
#ColumnInfo(name="checkListColumnID")
val checkListColumnID: Long,
#PrimaryKey(autoGenerate = true)
val checkListPointsModelID: Long = 0L
): Serializable
#Entity(tableName = "check_list_model")
data class CheckListModel (
#ColumnInfo(name = "check_list_name")
val checkListName: String,
#ColumnInfo(name = "check_list_count")
val checkListCount: Int,
#ColumnInfo(name = "check_list_result")
val checkListResult: Int,
#ColumnInfo(name = "description")
val description: String,
#PrimaryKey(autoGenerate = true)
val checkListModelID: Long = 0L
) : Serializable
relationship
data class CheckListWithCheckListModel(
#Embedded val CheckList: CheckListModel,
#Relation(
parentColumn = "checkListModelID",
entityColumn = "checkListColumnID"
)
val checkListPoints: List<CheckListPoints>
)
this is dao
#Dao
interface CheckListModelDBDao {
#Insert
fun insertCheckList(data:CheckListModel)
#Insert
fun insertCheckListPoint(vararg data:CheckListPoints)
#Delete
fun deleteCheckList(checkList: CheckListModel)
#Transaction
#Query("SELECT * FROM check_list_model " )
fun getEverything(): Flow<List<CheckListWithCheckListModel>>
}
and this is how i add
private var doorCheckListModel = CheckListModel("Дверь",0,0,"4321")
private val doorCheckListPoint1 = CheckListPoints(false,"1",false,doorCheckListModel.checkListModelID)
private val doorCheckListPoint2 = CheckListPoints(false,"2",false,doorCheckListModel.checkListModelID)
private var doorListOfCheckListPoints = listOf<CheckListPoints>(doorCheckListPoint1,doorCheckListPoint2)
private var windowCheckListModel = CheckListModel("Окно",0,0,"4321")
private var windowCheckListPoint1 = CheckListPoints(false,"1",false,windowCheckListModel.checkListModelID)
private var windowCheckListPoint2 = CheckListPoints(false,"1",false,windowCheckListModel.checkListModelID)
private var windowListOfCheckListPoints = listOf<CheckListPoints>(windowCheckListPoint1,windowCheckListPoint2)
var checkLists = MutableLiveData<List<CheckListModel>>().apply {
value = listOf(doorCheckListModel,windowCheckListModel)
}
fun addCheckList(name: String){
viewModelScope.launch(Dispatchers.IO) {
when (name) {
"Дверь" -> insert(doorCheckListModel,doorListOfCheckListPoints)
"Окно" -> insert(windowCheckListModel,windowListOfCheckListPoints)
}
}
}
private suspend fun insert(checkList: CheckListModel, checkListPoints: List<CheckListPoints>){
database.insertCheckList(checkList)
for(checkListPoint in checkListPoints){
database.insertCheckListPoint(checkListPoint)
}
}
}
also i display data from CheckListModel in fragment. CheckListModel added to DB Correctly and display correctly, but CheckListPoints has not
When you create doorCheckListModel, its checkListModelID is initially 0. You use this 0 as checkListColumnID in doorCheckListPoint1. So when you save the CheckListModel, Room automatically generates the primary key and saves in the table. Similar is the case for primary key in CheckListPoints table. But the entries saved in CheckListPoints table still have 0 in checkListColumnID column.
This is why the foreign key constraint is failing. There is no CheckListModel with 0 as its primary key. To fix this, you will have to set the value of checkListColumnID before saving a CheckListPoints entry in the table.
If you go through Room documentation, the #Insert annotated function can optionally return the rowId for the inserted item. For integer primary keys, rowId is the same as primary key.
Try this code:
// Return the primary key here
#Insert
fun insertCheckList(data:CheckListModel): Long
private suspend fun insert(checkList: CheckListModel, checkListPoints: List<CheckListPoints>){
val id = database.insertCheckList(checkList)
for(checkListPoint in checkListPoints){
database.insertCheckListPoint(checkListPoint.copy(checkListColumnID = id))
}
}

net.corda.nodeapi.internal.persistence.CouldNotCreateDataSourceException: Could not create the DataSource

I'm trying to develop a CorDapp using the example here. I've added two modules into my project, one for contracts and the other one for flows. I've added test cases for my contract and it works fine, but test cases for flow fail on setup stage. Here's the code of my test class
#TestInstance(TestInstance.Lifecycle.PER_CLASS)
class SharedInformationFlowTests {
private lateinit var network: MockNetwork
private lateinit var a: StartedMockNode
private val proposal = LedgerUpdateProposal.testProposal()
#BeforeAll
fun setup() {
val params = MockNetworkParameters(cordappsForAllNodes = listOf(
TestCordapp.findCordapp("com.something.contract")
))
network = MockNetwork(params) //fails here
a = network.createPartyNode()
network.runNetwork()
}
#AfterAll
fun tearDown() {
network.stopNodes()
}
And here are the error messages I get:
[WARN] 13:42:52,620 [main] spi.SqlExceptionHelper. - SQL Error: 0, SQLState: null {changeSet=migration/vault-schema.changelog-v9.xml::update-vault-states::R3.Corda, databaseChangeLog=master.changelog.json}
[ERROR] 13:42:52,620 [main] spi.SqlExceptionHelper. - Connection is closed {changeSet=migration/vault-schema.changelog-v9.xml::update-vault-states::R3.Corda, databaseChangeLog=master.changelog.json}
net.corda.nodeapi.internal.persistence.CouldNotCreateDataSourceException: Could not create the
DataSource: Migration failed for change set migration/vault-schema.changelog-v9.xml::update-vault-states::R3.Corda:
Reason: net.corda.nodeapi.internal.persistence.HibernateConfigException: Could not create Hibernate configuration: Unable to open JDBC Connection for DDL execution
I think there's something wrong with the liquibase. I've tried adding changelog file to my resources/migration directory as advised here, but it doesn't seem to have any effect. Please help.
UPDATE Added schema
/**
* The family of com.sentinel.schemas for SharingInformationState.
*/
object SharingInformationSchema
/**
* An SharingInformationState schema.
*/
object SharingInformationSchemaV1 : MappedSchema(
schemaFamily = SharingInformationSchema.javaClass,
version = 1,
mappedTypes = listOf(PersistentSharingInformation::class.java)) {
override val migrationResource: String? = "sharing-information-schema-v1.changelog-master.xml"
#Entity
#Table(name = "persistent_sharing_information")
class PersistentSharingInformation(
#Column(name = "owner_id")
var dataOwnerId: Long,
#Column(name = "buyer_id")
var dataBuyerId: Long,
#Column(name = "start_date")
val startDate: String,
#Column(name = "end_date")
val endDate: String,
#Column(name = "shared_fields")
val sharedFieldsIds: String,
#Column(name = "agreement_status")
val agreementStatus: String,
#Column(name = "contract_type")
val contractType: String,
#Column(name = "linear_id")
var linearId: UUID
) : PersistentState() {
// Default constructor required by hibernate.
constructor() : this(0L, 0L,
"", "", "[]", "", "", UUID.randomUUID())
}
}
#BelongsToContract(com.package.contract.SharingInformationContract::class)
class SharingInformationState(val ourParty: Party,
val proposal: LedgerUpdateProposal,
override val linearId: UniqueIdentifier = UniqueIdentifier()) : LinearState, QueryableState {
override val participants: List<AbstractParty> = listOf(ourParty)
override fun generateMappedObject(schema: MappedSchema): PersistentState {
return when (schema) {
SharingInformationSchemaV1 -> SharingInformationSchemaV1.PersistentSharingInformation(
proposal.ownerId,
proposal.buyerId,
proposal.startDate,
proposal.endDate,
proposal.sharedFieldsIds.toString(),
proposal.agreementStatus.name,
proposal.contractType.name,
linearId.id
)
else -> throw IllegalArgumentException("Unrecognised schema $schema")
}
}
override fun supportedSchemas(): Iterable<MappedSchema> = listOf(SharingInformationSchemaV1)
}
#CordaSerializable
enum class AgreementStatus { APPROVED, REJECTED }
#CordaSerializable
enum class ContractType { CORPORATE, CONSUMER, MARKETING, BINDING }

Cannot save data model that contains List<Model> with Room ORM Kotlin

I have a problem with Room ORM working on Kotlin. My task is having ability to save and get data models RouteTemplateModel, that contains list of addresses of type AddressModel and object of class RouteModel that contains title of the specific route. Here is my code:
AddressModel.kt
#Entity(foreignKeys = arrayOf(
ForeignKey(entity = RouteModel::class,
parentColumns = arrayOf("routeId"),
childColumns = arrayOf("parentId"))))
data class AddressModel(
#PrimaryKey(autoGenerate = true)
var addressId: Long,
var parentId: Long,
var street: String,
var house: String,
var entrance: String,
var title: String){
constructor(): this(0, 0, "", "", "", "")
}
RouteModel.kt
#Entity
data class RouteModel(
#PrimaryKey(autoGenerate = true)
var routeId: Long,
var title: String) {
constructor() : this(0, "")
}
Here is my simple models, I found in documentation of Room that for creating relations between models I need to use #ForeignKey and #Relation
So with code samples in doc and tutorials I create RouteTemplateModel that contains object of RouteModel and list of AddressModels. Here is the class
RouteTemplateModel
class RouteTemplateModel{
private var id: Long = 0
#Embedded
private var routeModel: RouteModel = RouteModel()
#Relation(parentColumn = "routeId", entityColumn = "parentId")
private var addressList: List<AddressModel> = listOf()
constructor()
constructor(id: Long, routeModel: RouteModel, title: String,
addressList: List<AddressModel>){
this.id = id
this.routeModel = routeModel
this.addressList = addressList
}
fun getId(): Long{
return id
}
fun getRouteModel(): RouteModel{
return routeModel
}
fun getAddressList(): List<AddressModel>{
return addressList
}
fun setId(id: Long){
this.id = id
}
fun setRouteModel(routeModel: RouteModel){
this.routeModel = routeModel
}
fun setAddressList(addressList: List<AddressModel>){
this.addressList = addressList
}
}
So what`s a problem? I am getting such errors:
Error:The columns returned by the query does not have the fields [id]
in com.innotech.webcab3kotlin.model.RouteTemplateModel even though
they are annotated as non-null or primitive. Columns returned by the
query: [routeId,title]
And
Error:Type of the parameter must be a class annotated with #Entity or
a collection/array of it.
It is a real problem, because if my trying to fix first error and annotate in RouteTemplateModel id variable to return this column too, I need annotate class as Entity (like in second error), but when I do it I am getting an error
Error:Entities cannot have relations.
Here is AppDatabase.kt
#Database(entities = arrayOf(RouteModel::class, AddressModel::class), version = 1)
abstract class AppDatabase : RoomDatabase() {
abstract fun getRouteDao(): RouteDao
}
and RouteDao.kt
#Dao
interface RouteDao {
#Query("SELECT routeId, title FROM RouteModel")
fun getAll(): List<RouteTemplateModel>
#Insert
fun insertAll(vararg models: RouteTemplateModel)
#Delete
fun delete(model: RouteTemplateModel)
}
Thats really confusing. Please, help me)
Your "parentId" column is capable of holding long value only, make its type to "Text" then create a TypeConverter from "List" to String and vice a versa for reference please have a look at link .