From a6a7930c29622328055abbfb3fc0eae9fa958209 Mon Sep 17 00:00:00 2001 From: dankl Date: Fri, 4 Oct 2019 14:29:46 +0200 Subject: [PATCH] Added SegmentNumberGenerator so that segment numbers don't have to be hard coded --- .../dankito/fints/messages/MessageBuilder.kt | 14 ++++++---- .../segmente/ISegmentNumberGenerator.kt | 15 +++++++++++ .../segmente/SegmentNumberGenerator.kt | 26 +++++++++++++++++++ 3 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 fints4javaLib/src/main/kotlin/net/dankito/fints/messages/segmente/ISegmentNumberGenerator.kt create mode 100644 fints4javaLib/src/main/kotlin/net/dankito/fints/messages/segmente/SegmentNumberGenerator.kt diff --git a/fints4javaLib/src/main/kotlin/net/dankito/fints/messages/MessageBuilder.kt b/fints4javaLib/src/main/kotlin/net/dankito/fints/messages/MessageBuilder.kt index d9066161..57670e8c 100644 --- a/fints4javaLib/src/main/kotlin/net/dankito/fints/messages/MessageBuilder.kt +++ b/fints4javaLib/src/main/kotlin/net/dankito/fints/messages/MessageBuilder.kt @@ -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) diff --git a/fints4javaLib/src/main/kotlin/net/dankito/fints/messages/segmente/ISegmentNumberGenerator.kt b/fints4javaLib/src/main/kotlin/net/dankito/fints/messages/segmente/ISegmentNumberGenerator.kt new file mode 100644 index 00000000..7a9946a0 --- /dev/null +++ b/fints4javaLib/src/main/kotlin/net/dankito/fints/messages/segmente/ISegmentNumberGenerator.kt @@ -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 + +} \ No newline at end of file diff --git a/fints4javaLib/src/main/kotlin/net/dankito/fints/messages/segmente/SegmentNumberGenerator.kt b/fints4javaLib/src/main/kotlin/net/dankito/fints/messages/segmente/SegmentNumberGenerator.kt new file mode 100644 index 00000000..b74e4eea --- /dev/null +++ b/fints4javaLib/src/main/kotlin/net/dankito/fints/messages/segmente/SegmentNumberGenerator.kt @@ -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" + } + +} \ No newline at end of file