From 0ea9fad7a0ac71b5a0a505a98e36551e8c6b8686 Mon Sep 17 00:00:00 2001 From: dankito Date: Tue, 8 Sep 2020 14:23:56 +0200 Subject: [PATCH] Implemented restoring data directly when creating fints4kBankingClient. This also fixes that restored BankData has data that mapped BankData didn't have (like allowedJobs, pinInfo, ...) --- .../net/dankito/banking/ui/IBankingClient.kt | 2 -- .../banking/ui/presenter/BankingPresenter.kt | 7 ------ .../dankito/banking/fints4kBankingClient.kt | 25 +++++++++++-------- .../dankito/banking/hbci4jBankingClient.kt | 5 ---- 4 files changed, 15 insertions(+), 24 deletions(-) diff --git a/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/IBankingClient.kt b/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/IBankingClient.kt index 2fb1f078..5a79cfa5 100644 --- a/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/IBankingClient.kt +++ b/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/IBankingClient.kt @@ -24,6 +24,4 @@ interface IBankingClient { fun transferMoneyAsync(data: TransferMoneyData, callback: (BankingClientResponse) -> Unit) - fun restoreData() - } \ No newline at end of file diff --git a/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/presenter/BankingPresenter.kt b/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/presenter/BankingPresenter.kt index 76710273..779c5cee 100644 --- a/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/presenter/BankingPresenter.kt +++ b/ui/BankingUiCommon/src/commonMain/kotlin/net/dankito/banking/ui/presenter/BankingPresenter.kt @@ -120,13 +120,6 @@ open class BankingPresenter( deserializedAccounts.forEach { customer -> val newClient = bankingClientCreator.createClient(customer, dataFolder, asyncRunner, callback) - try { - newClient.restoreData() - } catch (e: Exception) { - log.error(e) { "Could not deserialize customer data of $customer" } - // TODO: show error message to user - } - addClientForAccount(customer, newClient) } diff --git a/ui/fints4kBankingClient/src/commonMain/kotlin/net/dankito/banking/fints4kBankingClient.kt b/ui/fints4kBankingClient/src/commonMain/kotlin/net/dankito/banking/fints4kBankingClient.kt index 9f6a3ed5..da739efc 100644 --- a/ui/fints4kBankingClient/src/commonMain/kotlin/net/dankito/banking/fints4kBankingClient.kt +++ b/ui/fints4kBankingClient/src/commonMain/kotlin/net/dankito/banking/fints4kBankingClient.kt @@ -47,7 +47,7 @@ open class fints4kBankingClient( protected var didTryToGetAccountDataFromBank = false - protected val bank = BankData(customer.bankCode, customer.customerId, customer.password, customer.finTsServerAddress, customer.bic, customer.bankName) + protected val bank = restoreDataOrMapFromUiModel(customer) protected open val client = FinTsClientForCustomer(bank, createFinTsClientCallback(callback), webClient, base64Service) @@ -152,19 +152,24 @@ open class fints4kBankingClient( } - override fun restoreData() { - val deserializedBank = serializer.deserializeObject(getFints4kClientDataFile(), BankData::class) + protected open fun restoreDataOrMapFromUiModel(customer: Customer): BankData { + return restoreData(customer) ?: + BankData(customer.bankCode, customer.customerId, customer.password, customer.finTsServerAddress, customer.bic, customer.bankName) + } - deserializedBank?.let { - mapper.updateCustomer(bank, deserializedBank) - - mapper.mapCustomer(customer, bank) // TODO: necessary? + protected open fun restoreData(customer: Customer): BankData? { + try { + return serializer.deserializeObject(getFints4kClientDataFile(customer.bankCode, customer.customerId), BankData::class) + } catch (e: Exception) { + log.warn(e) { "Could not deserialize bank data of $customer (which is ok if bank is just about to be added)" } } + + return null } protected open fun saveData() { try { - val clientDataFile = getFints4kClientDataFile() + val clientDataFile = getFints4kClientDataFile(bank.bankCode, bank.customerId) serializer.serializeObject(bank, clientDataFile) } catch (e: Exception) { @@ -172,12 +177,12 @@ open class fints4kBankingClient( } } - protected open fun getFints4kClientDataFile(): File { + protected open fun getFints4kClientDataFile(bankCode: String, customerId: String): File { val folder = File(dataFolder, "fints4k-client") folder.mkdirs() - return File(folder, "${bank.bankCode}_${bank.customerId}_$fints4kClientDataFilename") + return File(folder, "${bankCode}_${customerId}_$fints4kClientDataFilename") } diff --git a/ui/hbci4jBankingClient/src/main/kotlin/net/dankito/banking/hbci4jBankingClient.kt b/ui/hbci4jBankingClient/src/main/kotlin/net/dankito/banking/hbci4jBankingClient.kt index 352ed8ba..a7a0f417 100644 --- a/ui/hbci4jBankingClient/src/main/kotlin/net/dankito/banking/hbci4jBankingClient.kt +++ b/ui/hbci4jBankingClient/src/main/kotlin/net/dankito/banking/hbci4jBankingClient.kt @@ -271,11 +271,6 @@ open class hbci4jBankingClient( } - override fun restoreData() { - // nothing to do for hbci4j - } - - protected open fun connect(): ConnectResult { return connect(credentials, HBCIVersion.HBCI_300) }