Apache camel split the same field data - apache-camel

I faced this problem "No position 1 defined for the field: 6805000004289776.2020.12, line: 1 must be specified"
My source data looks like this "6805000004289776.2020.12";"2020-12-31"
The entire first column is the same value but "bindy" tries to split it into multiple values
How can I tell him not to do this?
#Entity
#CsvRecord(separator = ";", quote = "\"")
#Table(name = "payment_request_details")
open class PaymentRequestDetail {
#Id
#GeneratedValue
#Column(name = "id", nullable = false)
open var id: UUID? = null
#Column(name = "payment_request_id", nullable = false, insertable = false, updatable = false)
open var paymentRequestId: UUID? = null
#DataField(pos = 1)
#Column(name = "account", nullable = false, length = 64)
open var account: String? = null

Related

No value passed for parameter string

I am doing an application in Kotlin, where questions are added to the DB. I am getting an error like: no value passed for parameter string
if (cursor.moveToFirst()) {
do {
val question = Question() //this line gives the error
question.setID(cursor.getString(0).toInt())
question.setQuestion(cursor.getString(1))
question.setOption1(cursor.getString(2))
question.setOption2(cursor.getString(3))
question.setOption3(cursor.getString(4))
question.setOption4(cursor.getString(5))
question.setAnswer(cursor.getString(6))
// Adding contact to list
questionList.add(question)
} while (cursor.moveToNext())
}
Question Class
class Question(
toInt: Int,
string: String,
string1: String,
string2: String,
string3: String,
string4: String,
string5: String
) {
var _id = 0
var _question: String? = null
var _option1: String? = null
var _option2: String? = null
var _option3: String? = null
var _option4: String? = null
var _answer: String? = null
fun Question() {}
fun Question(
id: Int,
question: String?,
option1: String?,
option2: String?,
option3: String?,
option4: String?,
_answer: String?
) {
_id = id
_question = question
_option1 = option1
_option2 = option2
_option3 = option3
_option4 = option4
this._answer = _answer
}
fun Question(
question: String?,
option1: String?,
option2: String?,
option3: String?,
option4: String?,
_answer: String?
) {
_question = question
_option1 = option1
_option2 = option2
_option3 = option3
_option4 = option4
this._answer = _answer
}
fun getID(): Int {
return _id
}
fun setID(id: Int) {
_id = id
}
fun getQuestion(): String? {
return _question
}
fun setQuestion(question: String?) {
_question = question
}
fun getOption1(): String? {
return _option1
}
fun setOption1(option1: String?) {
_option1 = option1
}
fun getOption2(): String? {
return _option2
}
fun setOption2(option2: String?) {
_option2 = option2
}
fun getOption3(): String? {
return _option3
}
fun setOption3(option3: String?) {
_option3 = option3
}
fun getOption4(): String? {
return _option4
}
fun setOption4(option4: String?) {
_option4 = option4
}
fun getAnswer(): String? {
return _answer
}
fun setAnswer(answer: String?) {
_answer = answer
}
}
This line is giving me the error
val question = Question()
First, it seems you are trying to create some extra constructors with the syntax
fun Question() {
but this is not the syntax for a constructor. It is a regular member function named Question and can only be called on an already existing instance of a Question. The correct syntax would be:
constructor() {
However, since you have a primary constructor, this would be a secondary constructor and would have to call the primary constructor with the arguments it requires, which is going to be super complicated for your case.
Your Question class is extremely overdesigned. In Kotlin:
There is no reason to have setter functions since we have properties instead of fields. Any setter function is redundant to the capabilities of a property, so there is no reason to pollute your code with them.
You can put properties directly in the primary constructor. Then you don't have to manually assign constructor parameters to property values.
You can give parameters default values so you don't have to create special constructors that represent empty conditions.
With the above features of Kotlin, you can rewrite your entire class like this, with no loss of functionality or flexibility:
data class Question (
var id = 0,
var question: String? = null,
var option1: String? = null,
var option2: String? = null,
var option3: String? = null,
var option4: String? = null,
var answer: String? = null
)
At your use site, you can call this empty constructor and then set the properties on it like you were trying to do. Properties in Kotlin should be set with assignment syntax (=), not setter functions. You can also use the apply scope function to avoid having to write question. over and over:
if (cursor.moveToFirst()) {
do {
val question = Question().apply {
id = cursor.getString(0).toInt()
question = cursor.getString(1)
option1 = cursor.getString(2)
option2 = cursor.getString(3)
option3 = cursor.getString(4)
option4 = cursor.getString(5)
answer = cursor.getString(6)
}
// Adding contact to list
questionList.add(question)
} while (cursor.moveToNext())
}
However, it looks like you have no reason to make any of these parameters nullable. You should avoid nullable parameters whenever possible, because they are harder to work with. If a Question in your app always has values for all these properties, they should not be nullable, and you don't need to provide default values. I would define your class like this:
data class Question (
val id: Int,
val question: String,
val option1: String,
val option2: String,
val option3: String,
val option4: String,
val answer: String
)
Then at the usage site, you can call this constructor with all the arguments directly:
if (cursor.moveToFirst()) {
do {
val question = Question(
id = cursor.getString(0).toInt(),
question = cursor.getString(1),
option1 = cursor.getString(2),
option2 = cursor.getString(3),
option3 = cursor.getString(4),
option4 = cursor.getString(5),
answer = cursor.getString(6)
)
// Adding contact to list
questionList.add(question)
} while (cursor.moveToNext())
}
By the way, this if/do/while pattern for traversing the cursor is unnecessarily complicated. A cursor starts at a position before the first item, so you don't need to move it to first or use do/while. It would be equivalent to use:
while (cursor.moveToNext()) {
val question = Question(
id = cursor.getString(0).toInt(),
question = cursor.getString(1),
option1 = cursor.getString(2),
option2 = cursor.getString(3),
option3 = cursor.getString(4),
option4 = cursor.getString(5),
answer = cursor.getString(6)
)
// Adding contact to list
questionList.add(question)
}
You have to initialize Question class constructor parameters value to default or null.
For example,
class Question(
toInt: Int = 0,
string: String = "",
string1: String = "",
string2: String = "",
string3: String = "",
string4: String = "",
string5: String = ""
) {
//Your code here
}
OR
class Question(
toInt: Int? = null,
string: String? = null,
string1: String? = null,
string2: String? = null,
string3: String? = null,
string4: String? = null,
string5: String? = null
) {
//Your code here
}

How to pair values in kotlin array?

I have a class with 2 property
class SelectedAmount : Serializable {
lateinit var amount: List<Int> //the values here is coming from backend in array list. eg [50,100,150,200]
var isSelect: Boolean = false
}
I want to pair each amount with a boolean value. eg [{50, true}, {100, false}, {150, false}, {200, false}]
In view activity i did
private var amountList: MutableList<AmountSelected> = ArrayList<AmountSelected>()
val amountInterval = data.reloadDenoms // BE value {50,100,150,200}
if (amountInterval != null) {
for (items in amountInterval) {
var amountSelected:SelectedAmount = SelectedAmount()
amountSelected.amount = amountInterval
amountSelected.isSelect = false // tring to set boolean false for every amountInterval value
amountList.add(amountSelected)
}
when i tring to print the value of amountList .. i get out put as
[{50,100,150,200}, false]
my expected output is
[{50, true}, {100, false}, {150, false}, {200, false}]
can anyone help me on this? I am a newbie here learning array btw
No need of List of integers in SelectedAmount
class SelectedAmount : Serializable {
lateinit var amount: int //the values here is coming from backend in array list. eg [50,100,150,200]
var isSelect: Boolean = false
}
And
// *** Note the SelectedAmount instead of AmountSelected
private var amountList: MutableList<SelectedAmount> = ArrayList<SelectedAmount>()
val amountInterval = data.reloadDenoms // BE value {50,100,150,200}
if (amountInterval != null) {
for (items in amountInterval) {
var amountSelected:SelectedAmount = SelectedAmount()
amountSelected.amount = items // **Add only the integer** You were adding the list earlier
amountSelected.isSelect = true // **You can set boolean for one or more items if needed**
amountList.add(amountSelected)
}
The easier approach I got
listOf(50,100, 150, 200).map {
it to (it % 100 != 0)
}.let {
print(it)
}

How to insert an Image URL for Images stored in Postgres into a Spring Boot Model

I have built an app with a Spring Boot Gradle Kotlin Backend, that can store and upload Multipart files to a Postgres database. The data is stored in a ByteArray. So far everything is working fine.
Now I want to add an Image URL to make it available in my JSON Object to later grab it and display it on my client side like the ones on Unsplash.
imageURL
"imageURL": "https://xyz.jpg"
File model
#Entity
#Table(name = "file")
data class File(
#Id
val id: UUID = UUID.randomUUID(),
val createdAt: LocalDateTime = LocalDateTime.now(),
var updatedAt: LocalDateTime = LocalDateTime.now(),
var likes: Int = 0,
var likedByUser: Boolean = false,
var description: String = "",
val downloadLink: URI = URI("https://unsplasy-backend.herokuapp.com/files/download/$id"),
var name: String = "",
var type: String = "",
#Basic(fetch = FetchType.LAZY)
private val data: ByteArray = byteArrayOf(),
val imageURL: TODO = "TODO"
)
Store
fun store(file: MultipartFile): File {
val fileName = file.originalFilename.toString()
val fileToSave = File(
name = fileName,
type = file.contentType.toString(),
data = file.bytes
)
return fileRepository.save(fileToSave)
}
After reading the suggestion above I came up with this and it works.
Controller Update
#GetMapping("/files/image/{id}")
fun displayImage(#PathVariable id: UUID, response: HttpServletResponse) {
val file = fileService.getFile(id)
val data = fileService.getData(file)
response.contentType = file.type
response.outputStream.write(data)
response.outputStream.close()
}
File Model update
val imageURL: URL = URL("https://unsplasy-backend.herokuapp.com/files/image/$id"),

why? error: Cannot figure out how to save this field into database

I stuck with a fool issue
I have response from server
data class AddressResponse(
#SerializedName("message") val responseMessage: String,
#SerializedName("success") val success: Int,
#SerializedName("status") val status: Int,
#SerializedName("data") val data: Data
) {
data class Data(
#SerializedName("id") val addressId: Long,
#SerializedName("status") val status: Int?,
#TypeConverters( CommunicationPropertiesConverter::class )
#SerializedName("communication_properties") val communicationProperties: AddAddressRequest.CommunicationProperties?,
#SerializedName("more_information") val moreInformation: String?
) {
data class CommunicationProperties(
#SerializedName("par1") var par1: Boolean,
#SerializedName("par2") val par2: Boolean,
#SerializedName("par3") val par3: Boolean,
#SerializedName("par4") val par4: Boolean
)
}
}
I use room and I have entity class
#Entity(tableName = "db_address")
data class DbAddress(
#PrimaryKey #ColumnInfo(name = COLUMN_ID) val id: Long,
#ColumnInfo(name = COLUMN_STATUS) val status: Int?,
#TypeConverters(CommunicationPropertiesConverter::class)
#ColumnInfo(name = COLUMN_COMM_PROPS) val communicationProperties: AddressResponse.Data.CommunicationProperties,
#ColumnInfo(name = COLUMN_MORE_INFO) val moreInfo: String?
)
I have a convertor a set annotations #TypeConverters & #TypeConverter
class CommunicationPropertiesConverter {
private val gson = Gson()
#TypeConverter
fun toCommunicationProperties(mString: String?): AddressResponse.Data.CommunicationProperties {
if (mString.isNullOrEmpty())
return AddressResponse.Data.CommunicationProperties(par1 = false, par2 = false, par3 = false, par4 = false)
val listType = object : TypeToken<AddressResponse.Data.CommunicationProperties>(){}.type
return gson.fromJson(mString, listType)
}
#TypeConverter
fun fromCommunicationProperties(properties: AddressResponse.Data.CommunicationProperties?): String {
if (properties == null)
return "0000"
return gson.toJson(properties)
}
}
but I still get an error
error: Cannot figure out how to save this field into database. You can
consider adding a type converter for it
private final ... AddressResponse.Data.CommunicationProperties communicationProperties = null
but I added converter to and from "primitive" type
why? what did I forget?
I had a similar issue and what worked for me was annotating that field (that has the custom type) with #Embedded. In my case, I didn't even need type converters anymore.

Can Dapper handle dynamic column header

I have a stored procedure that always returns a list of strings. However, based on the parameters passed to the stored procedure, the column heading for the list of strings will change. Is Dapper able to handle this? If so, how should it be handled?
conn.Open();
var p = new DynamicParameters();
p.Add("Search_Function_CD", searchFunction, DbType.String, ParameterDirection.Input);
p.Add("Search_Value", searchValue, DbType.String, direction: ParameterDirection.Input);
p.Add("o_resultset", dbType: DbType.Object, direction: ParameterDirection.Output);
var Results = (IDictionary<string, object>)conn.Query(sql: CommonConstants.ProcedureConstants.PKG_GET_SEARCH_RESULTS, param: p, commandType: CommandType.StoredProcedure);
foreach (object o in Results)
{
string element = Results.Values.ElementAt(1) as string;
searchResults.Add(element);
}
conn.Close();
return searchResults;
You can get the value by a dynamic column name or index:
var row = (IDictionary<string, object>)con.Query("select * from Products").First();
string name = row["ProductName"]; // column name 'ProductName'
// or:
string name = row.Values.ElementAt(1) as string; // column index 1

Resources