From b18a9a04f4d44d7089a8b98f00eeb110ea223664 Mon Sep 17 00:00:00 2001 From: dankito Date: Thu, 14 May 2020 22:36:30 +0200 Subject: [PATCH] Implemented that if an integer is enter only for bank code is search, otherwise only for bank name and city --- .../net/dankito/fints/banks/BankFinderBase.kt | 16 ++++++++++++++++ .../dankito/fints/banks/InMemoryBankFinder.kt | 18 ++++++------------ .../dankito/fints/banks/LuceneBankFinder.kt | 7 +------ 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/fints4k/src/main/kotlin/net/dankito/fints/banks/BankFinderBase.kt b/fints4k/src/main/kotlin/net/dankito/fints/banks/BankFinderBase.kt index 2b6d7b22..60f35a03 100644 --- a/fints4k/src/main/kotlin/net/dankito/fints/banks/BankFinderBase.kt +++ b/fints4k/src/main/kotlin/net/dankito/fints/banks/BankFinderBase.kt @@ -14,6 +14,22 @@ abstract class BankFinderBase : IBankFinder { } + protected abstract fun findBankByNameOrCityForNonEmptyQuery(query: String): List + + + override fun findBankByNameBankCodeOrCity(query: String?): List { + if (query.isNullOrBlank()) { + return getBankList() + } + + if (query.toIntOrNull() != null) { // if query is an integer, then it can only be an bank code, but not a bank name or city + return findBankByBankCode(query) + } + + return findBankByNameOrCityForNonEmptyQuery(query) + } + + protected open fun loadBankListFile(): List { try { val bankListString = readBankListFile() diff --git a/fints4k/src/main/kotlin/net/dankito/fints/banks/InMemoryBankFinder.kt b/fints4k/src/main/kotlin/net/dankito/fints/banks/InMemoryBankFinder.kt index 64a12865..5e53f481 100644 --- a/fints4k/src/main/kotlin/net/dankito/fints/banks/InMemoryBankFinder.kt +++ b/fints4k/src/main/kotlin/net/dankito/fints/banks/InMemoryBankFinder.kt @@ -21,22 +21,17 @@ open class InMemoryBankFinder() : BankFinderBase(), IBankFinder { return getBankList().filter { it.bankCode.startsWith(query) } } - override fun findBankByNameBankCodeOrCity(query: String?): List { - if (query.isNullOrEmpty()) { - return getBankList() - } - - val queryLowerCase = query.toLowerCase() - val queryPartsLowerCase = queryLowerCase.split(" ") + override fun findBankByNameOrCityForNonEmptyQuery(query: String): List { + val queryPartsLowerCase = query.toLowerCase().split(" ") return getBankList().filter { bankInfo -> - checkIfAllQueryPartsMatchBank(queryPartsLowerCase, bankInfo) + checkIfAllQueryPartsMatchBankNameOrCity(queryPartsLowerCase, bankInfo) } } - protected open fun checkIfAllQueryPartsMatchBank(queryPartsLowerCase: List, bankInfo: BankInfo): Boolean { + protected open fun checkIfAllQueryPartsMatchBankNameOrCity(queryPartsLowerCase: List, bankInfo: BankInfo): Boolean { for (queryPartLowerCase in queryPartsLowerCase) { - if (checkIfQueryMatchesBank(bankInfo, queryPartLowerCase) == false) { + if (checkIfQueryMatchesBankNameOrCity(bankInfo, queryPartLowerCase) == false) { return false } } @@ -44,9 +39,8 @@ open class InMemoryBankFinder() : BankFinderBase(), IBankFinder { return true } - protected open fun checkIfQueryMatchesBank(bankInfo: BankInfo, queryLowerCase: String): Boolean { + protected open fun checkIfQueryMatchesBankNameOrCity(bankInfo: BankInfo, queryLowerCase: String): Boolean { return bankInfo.name.toLowerCase().contains(queryLowerCase) - || bankInfo.bankCode.startsWith(queryLowerCase) || bankInfo.city.toLowerCase().contains(queryLowerCase) } diff --git a/fints4k/src/main/kotlin/net/dankito/fints/banks/LuceneBankFinder.kt b/fints4k/src/main/kotlin/net/dankito/fints/banks/LuceneBankFinder.kt index e88d53cd..60df3aa6 100644 --- a/fints4k/src/main/kotlin/net/dankito/fints/banks/LuceneBankFinder.kt +++ b/fints4k/src/main/kotlin/net/dankito/fints/banks/LuceneBankFinder.kt @@ -90,19 +90,14 @@ open class LuceneBankFinder(indexFolder: File) : BankFinderBase(), IBankFinder { return getBanksFromQuery(luceneQuery) } - override fun findBankByNameBankCodeOrCity(query: String?): List { + override fun findBankByNameOrCityForNonEmptyQuery(query: String): List { bankFinderWhileUpdatingIndex?.let { return it.findBankByNameBankCodeOrCity(query) } - if (query.isNullOrBlank()) { - return getBankList() - } - val luceneQuery = queries.createQueriesForSingleTerms(query.toLowerCase()) { singleTerm -> listOf( queries.fulltextQuery(BankInfoNameFieldName, singleTerm), - queries.startsWith(BankInfoBankCodeFieldName, singleTerm), queries.contains(BankInfoCityIndexedFieldName, singleTerm) ) }