Added SegmentNumberGenerator so that segment numbers don't have to be hard coded

This commit is contained in:
dankl 2019-10-04 14:29:46 +02:00 committed by dankito
parent b164737a47
commit a6a7930c29
3 changed files with 50 additions and 5 deletions

View File

@ -2,7 +2,9 @@ package net.dankito.fints.messages
import net.dankito.fints.messages.datenelemente.implementierte.*
import net.dankito.fints.messages.nachrichten.Nachricht
import net.dankito.fints.messages.segmente.ISegmentNumberGenerator
import net.dankito.fints.messages.segmente.Segment
import net.dankito.fints.messages.segmente.SegmentNumberGenerator
import net.dankito.fints.messages.segmente.implementierte.IdentifikationsSegment
import net.dankito.fints.messages.segmente.implementierte.Nachrichtenabschluss
import net.dankito.fints.messages.segmente.implementierte.Nachrichtenkopf
@ -13,7 +15,7 @@ import net.dankito.fints.messages.segmente.implementierte.Verarbeitungsvorbereit
* Takes the Segments of they payload, may signs and encrypts them, calculates message size,
* adds the message header and closing, and formats the whole message to string.
*/
open class MessageBuilder {
open class MessageBuilder(protected val generator: ISegmentNumberGenerator = SegmentNumberGenerator()) {
companion object {
const val MessageHeaderLength = 30
@ -36,6 +38,7 @@ open class MessageBuilder {
productName: String,
productVersion: String
): String {
return createDialogInitMessage(bankCountryCode, bankCode, KundenID.Anonymous, KundensystemID.Anonymous,
BPDVersion.VersionNotReceivedYet, UPDVersion.VersionNotReceivedYet, Dialogsprache.Default, productName, productVersion)
}
@ -51,9 +54,10 @@ open class MessageBuilder {
productName: String,
productVersion: String
): String {
return createMessage(listOf(
IdentifikationsSegment(2, bankCountryCode, bankCode, customerId, customerSystemId),
Verarbeitungsvorbereitung(3, bpdVersion, updVersion, language, productName, productVersion)
IdentifikationsSegment(generator.resetSegmentNumber(1), bankCountryCode, bankCode, customerId, customerSystemId),
Verarbeitungsvorbereitung(generator.getNextSegmentNumber(), bpdVersion, updVersion, language, productName, productVersion)
))
}
@ -65,9 +69,9 @@ open class MessageBuilder {
val messageSize = payload.length + MessageHeaderLength + MessageClosingLength + AddedSeparatorsLength
val messageNumber = Nachrichtennummer.FirstMessageNumber
val header = Nachrichtenkopf(1, messageSize, "0", messageNumber)
val header = Nachrichtenkopf(ISegmentNumberGenerator.FirstSegmentNumber, messageSize, "0", messageNumber)
val closing = Nachrichtenabschluss(4, messageNumber)
val closing = Nachrichtenabschluss(generator.getNextSegmentNumber(), messageNumber)
return listOf(header.format(), payload, closing.format())
.joinToString(Nachricht.SegmentSeparator, postfix = Nachricht.SegmentSeparator)

View File

@ -0,0 +1,15 @@
package net.dankito.fints.messages.segmente
interface ISegmentNumberGenerator {
companion object {
const val FirstSegmentNumber = 1
}
fun resetSegmentNumber(countNumberToSkipForHeader: Int): Int
fun getNextSegmentNumber(): Int
}

View File

@ -0,0 +1,26 @@
package net.dankito.fints.messages.segmente
import net.dankito.fints.messages.segmente.ISegmentNumberGenerator.Companion.FirstSegmentNumber
open class SegmentNumberGenerator : ISegmentNumberGenerator {
protected var currentSegmentNumber = 0
override fun resetSegmentNumber(countNumbersToSkipForHeaders: Int): Int {
currentSegmentNumber = FirstSegmentNumber + countNumbersToSkipForHeaders
return currentSegmentNumber
}
override fun getNextSegmentNumber(): Int {
return ++currentSegmentNumber
}
override fun toString(): String {
return "Current segment number = $currentSegmentNumber"
}
}