Skip to content

Commit

Permalink
feat(api): #172 Rename BPN fields
Browse files Browse the repository at this point in the history
  • Loading branch information
rainer-exxcellent committed May 25, 2023
1 parent 006abf4 commit 60afcd0
Show file tree
Hide file tree
Showing 19 changed files with 151 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class GateUpdateService(
) {
for (entity in responseWrapper.entities) {
val externalId = entity.index
buildSuccessSharingStateDto(LsaType.LegalEntity, externalId, entity.legalEntity.bpn, true)
buildSuccessSharingStateDto(LsaType.LegalEntity, externalId, entity.legalEntity.bpnl, true)
?.let { gateClient.sharingState().upsertSharingState(it) }
}
for (errorInfo in responseWrapper.errors) {
Expand All @@ -57,7 +57,7 @@ class GateUpdateService(
externalIdByBpn: Map<String, String>
) {
for (entity in responseWrapper.entities) {
val bpn = entity.legalEntity.bpn
val bpn = entity.legalEntity.bpnl
val externalId = externalIdByBpn[bpn]
buildSuccessSharingStateDto(LsaType.LegalEntity, externalId, bpn, false)
?.let { gateClient.sharingState().upsertSharingState(it) }
Expand All @@ -76,7 +76,7 @@ class GateUpdateService(
) {
for (entity in responseWrapper.entities) {
val externalId = entity.index
buildSuccessSharingStateDto(LsaType.Site, externalId, entity.site.bpn, true)
buildSuccessSharingStateDto(LsaType.Site, externalId, entity.site.bpns, true)
?.let { gateClient.sharingState().upsertSharingState(it) }
}
for (errorInfo in responseWrapper.errors) {
Expand All @@ -92,7 +92,7 @@ class GateUpdateService(
externalIdByBpn: Map<String, String>
) {
for (entity in responseWrapper.entities) {
val bpn = entity.site.bpn
val bpn = entity.site.bpns
val externalId = externalIdByBpn[bpn]
buildSuccessSharingStateDto(LsaType.Site, externalId, bpn, false)
?.let { gateClient.sharingState().upsertSharingState(it) }
Expand All @@ -111,7 +111,7 @@ class GateUpdateService(
) {
for (entity in responseWrapper.entities) {
val externalId = entity.index
buildSuccessSharingStateDto(LsaType.Address, externalId, entity.address.bpn, true)
buildSuccessSharingStateDto(LsaType.Address, externalId, entity.address.bpna, true)
?.let { gateClient.sharingState().upsertSharingState(it) }
}
for (errorInfo in responseWrapper.errors) {
Expand All @@ -127,7 +127,7 @@ class GateUpdateService(
externalIdByBpn: Map<String, String>
) {
for (entity in responseWrapper.entities) {
val bpn = entity.bpn
val bpn = entity.bpna
val externalId = externalIdByBpn[bpn]
buildSuccessSharingStateDto(LsaType.Address, externalId, bpn, false)
?.let { gateClient.sharingState().upsertSharingState(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import java.time.Instant
@Schema(name = "LegalEntityResponse", description = "Legal entity record")
data class LegalEntityResponse(
@get:Schema(description = "Business Partner Number of this legal entity")
val bpn: String,
val bpnl: String,

@ArraySchema(arraySchema = Schema(description = "All identifiers of the business partner, including BPN information"))
val identifiers: Collection<LegalEntityIdentifierResponse> = emptyList(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import java.time.Instant
data class LogisticAddressResponse(

@get:Schema(description = "Business Partner Number of this address")
val bpn: String,
val bpna: String,

@get:Schema(
description = "Name of the logistic address of the business partner. This is not according to official\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import java.time.Instant
@Schema(name = "SiteResponse", description = "Site of a legal entity")
data class SiteResponse(
@get:Schema(description = "Business Partner Number, main identifier value for sites")
val bpn: String,
val bpns: String,

@get:Schema(description = "Site name")
val name: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class AddressService(

//Search entries in the pool with BPNs found in the local mirror
val bpnSet = partnersWithLocalBpn.map { it.bpn }.toSet()
val addressesByBpnMap = poolClient.searchAddresses(bpnSet).associateBy { it.bpn }
val addressesByBpnMap = poolClient.searchAddresses(bpnSet).associateBy { it.bpna }

if (bpnSet.size > addressesByBpnMap.size) {
logger.warn { "Requested ${bpnSet.size} addresses from pool, but only ${addressesByBpnMap.size} were found." }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class LegalEntityService(

//Search entries in the pool with BPNs found in the local mirror
val bpnSet = partnersWithLocalBpn.map { it.bpn }.toSet()
val legalEntitiesByBpnMap = poolClient.searchLegalEntities(bpnSet).associateBy { it.bpn }
val legalEntitiesByBpnMap = poolClient.searchLegalEntities(bpnSet).associateBy { it.bpnl }
val legalAddressesByBpnMap = poolClient.searchLegalAddresses(bpnSet).associateBy { it.bpnLegalEntity }

if (bpnSet.size > legalEntitiesByBpnMap.size) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class SiteService(
val partnersWithLocalBpn = outputSaasMappingService.mapWithLocalBpn(partnersWithExternalId, augmentedPartnerResponse.values)

val bpnSet = partnersWithLocalBpn.map { it.bpn }.toSet()
val sitesByBpnMap = poolClient.searchSites(bpnSet).associateBy { it.bpn }
val sitesByBpnMap = poolClient.searchSites(bpnSet).associateBy { it.bpns }
val mainAddressesByBpnMap = poolClient.searchMainAddresses(bpnSet).associateBy { it.bpnSite }

if (bpnSet.size > sitesByBpnMap.size) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ object ResponseValues {
)

val logisticAddress1 = LogisticAddressResponse(
bpn = CommonValues.bpnAddress1,
bpna = CommonValues.bpnAddress1,
physicalPostalAddress = address1,
bpnLegalEntity = CommonValues.bpn1,
bpnSite = "BPNS0000000001XY",
Expand All @@ -212,7 +212,7 @@ object ResponseValues {
)

val logisticAddress2 = LogisticAddressResponse(
bpn = CommonValues.bpnAddress2,
bpna = CommonValues.bpnAddress2,
physicalPostalAddress = address2,
bpnLegalEntity = CommonValues.bpn2,
bpnSite = "BPNS0000000002XY",
Expand All @@ -222,7 +222,7 @@ object ResponseValues {


val legalEntityResponse1 = LegalEntityResponse(
bpn = CommonValues.bpn1,
bpnl = CommonValues.bpn1,
identifiers = listOf(identifier1, identifier2),
legalName = name1,
legalForm = legalForm1,
Expand All @@ -234,7 +234,7 @@ object ResponseValues {
)

val legalEntityResponse2 = LegalEntityResponse(
bpn = CommonValues.bpn2,
bpnl = CommonValues.bpn2,
identifiers = listOf(identifier3, identifier4),
legalName = name3,
legalForm = legalForm2,
Expand Down Expand Up @@ -272,15 +272,15 @@ object ResponseValues {
)

val siteResponse1 = SiteResponse(
bpn = CommonValues.bpnSite1,
bpns = CommonValues.bpnSite1,
name = CommonValues.nameSite1,
states = listOf(),
bpnLegalEntity = CommonValues.bpn1,
createdAt = CommonValues.now,
updatedAt = CommonValues.now
)
val siteResponse2 = SiteResponse(
bpn = CommonValues.bpnSite2,
bpns = CommonValues.bpnSite2,
name = CommonValues.nameSite2,
states = listOf(),
bpnLegalEntity = CommonValues.bpn2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import mu.KotlinLogging
import org.eclipse.tractusx.bpdm.common.dto.IdentifierLsaType
import org.eclipse.tractusx.bpdm.common.dto.response.LogisticAddressResponse
import org.eclipse.tractusx.bpdm.common.dto.saas.BusinessPartnerSaas
import org.eclipse.tractusx.bpdm.common.dto.saas.ThoroughfareSaas
import org.eclipse.tractusx.bpdm.common.exception.BpdmMappingException
import org.eclipse.tractusx.bpdm.common.service.SaasMappings
import org.eclipse.tractusx.bpdm.pool.api.model.response.AddressPartnerCreateResponse
Expand Down Expand Up @@ -88,7 +87,7 @@ class PartnerImportPageService(
)
}

private fun addNewMetadata(partners: Collection<BusinessPartnerSaas>){
private fun addNewMetadata(partners: Collection<BusinessPartnerSaas>) {
val (legalEntitiesSaas, _, addressesSaas) = partitionIntoLSA(partners) { it.extractLsaType() }

addNewMetadataIdentifierTypes(legalEntitiesSaas, IdentifierLsaType.LEGAL_ENTITY)
Expand Down Expand Up @@ -134,9 +133,9 @@ class PartnerImportPageService(
val createdSites = if (sites.isNotEmpty()) businessPartnerBuildService.createSites(sites).entities else emptyList()
val createdAddresses = if (addresses.isNotEmpty()) businessPartnerBuildService.createAddresses(addresses).entities else emptyList()

val legalEntityImportEntries = createdLegalEntities.mapNotNull { if (it.index != null) ImportEntry(it.index!!, it.legalEntity.bpn) else null }
val siteImportEntries = createdSites.mapNotNull { if (it.index != null) ImportEntry(it.index!!, it.site.bpn) else null }
val addressImportEntries = createdAddresses.mapNotNull { if (it.index != null) ImportEntry(it.index!!, it.address.bpn) else null }
val legalEntityImportEntries = createdLegalEntities.mapNotNull { if (it.index != null) ImportEntry(it.index!!, it.legalEntity.bpnl) else null }
val siteImportEntries = createdSites.mapNotNull { if (it.index != null) ImportEntry(it.index!!, it.site.bpns) else null }
val addressImportEntries = createdAddresses.mapNotNull { if (it.index != null) ImportEntry(it.index!!, it.address.bpna) else null }

importEntryRepository.saveAll(legalEntityImportEntries + siteImportEntries + addressImportEntries)

Expand Down Expand Up @@ -179,16 +178,16 @@ class PartnerImportPageService(
}
}

private fun determineParentBPNs(parents: Collection<BusinessPartnerSaas>): Collection<BusinessPartnerWithBpn>{
private fun determineParentBPNs(parents: Collection<BusinessPartnerSaas>): Collection<BusinessPartnerWithBpn> {
val parentByImportId = parents.associateBy { it.externalId!! }

val (parentsWithoutBpn, parentsWithBpn) = partitionHasNoBpnAndValidBpn(parents)

//create missing parents in the Pool
val (newLegalEntities, newSites, _) = createPartners(parentsWithoutBpn)

val createdParents = newLegalEntities.map { Pair(parentByImportId[it.index], it.legalEntity.bpn) }
.plus(newSites.map { Pair(parentByImportId[it.index], it.site.bpn) })
val createdParents = newLegalEntities.map { Pair(parentByImportId[it.index], it.legalEntity.bpnl) }
.plus(newSites.map { Pair(parentByImportId[it.index], it.site.bpns) })
.filter { (parent, _) -> parent != null }
.map { BusinessPartnerWithBpn(it.first!!, it.second) }

Expand All @@ -211,7 +210,7 @@ class PartnerImportPageService(

try {
SaasMappings.convertSaasAdressesToLogisticAddressDto(partner.addresses, partner.externalId)
} catch(e: BpdmMappingException) {
} catch (e: BpdmMappingException) {
logger.warn { "SaaS Partner with id ${partner.id} is invalid. Message: ${e.localizedMessage}" }
return false
}
Expand Down Expand Up @@ -268,21 +267,21 @@ class PartnerImportPageService(
* If we encounter valid BPNs with no import entry we create an import entry for it (as self correction logic)
* Optionally, depending on the configuration we either ignore records with invalid BPNs or we treat them as having no BPN
*/
private fun partitionHasNoBpnAndValidBpn(partners: Collection<BusinessPartnerSaas>): Pair<Collection<BusinessPartnerSaas>, Collection<BusinessPartnerWithBpn>>{
private fun partitionHasNoBpnAndValidBpn(partners: Collection<BusinessPartnerSaas>): Pair<Collection<BusinessPartnerSaas>, Collection<BusinessPartnerWithBpn>> {
//search BPN in import entry based on CX-Pool identifier
val (withEntry, withoutEntry) = partitionHasImportEntry(partners)
//if no entry has been found look for BPN in identifiers of records
val (hasBpnIdentifier, hasNoBpnIdentifier) = partitionContainsBpnIdentifier(withoutEntry)
//if BPN is identifiers but no import record exists, check whether the BPN is known to the BPDM Pool
val (bpnFound, bpnMissing) = partitionBpnFound(hasBpnIdentifier)

val consequence = if(adapterProperties.treatInvalidBpnAsNew) "Record will be treated as having no BPN." else "Record will be ignored."
val consequence = if (adapterProperties.treatInvalidBpnAsNew) "Record will be treated as having no BPN." else "Record will be ignored."
bpnMissing.forEach {
logger.warn { "Business partner with Id ${it.partner.externalId} contains BPN ${it.bpn} but such BPN can't be found in the Pool." + consequence }
}

val hasValidBpn = withEntry + bpnFound
val hasNoBpn = if(adapterProperties.treatInvalidBpnAsNew) hasNoBpnIdentifier + bpnMissing.map { it.partner } else hasNoBpnIdentifier
val hasNoBpn = if (adapterProperties.treatInvalidBpnAsNew) hasNoBpnIdentifier + bpnMissing.map { it.partner } else hasNoBpnIdentifier

//Create missing import entries for records which have known BPNs
importEntryRepository.saveAll(bpnFound.map { ImportEntry(it.partner.externalId!!, it.bpn) })
Expand Down Expand Up @@ -330,14 +329,13 @@ class PartnerImportPageService(
return Pair(bpnExists.values, bpnMissing.values)
}

private fun partitionLSA(bpns: Collection<String>): Triple<Collection<String>, Collection<String>, Collection<String>>{
private fun partitionLSA(bpns: Collection<String>): Triple<Collection<String>, Collection<String>, Collection<String>> {
val bpnLs = mutableListOf<String>()
val bpnSs = mutableListOf<String>()
val bpnAs = mutableListOf<String>()

bpns.map { it.uppercase()}.forEach {
when(it.take(4))
{
bpns.map { it.uppercase() }.forEach {
when (it.take(4)) {
"BPNL" -> bpnLs.add(it)
"BPNS" -> bpnSs.add(it)
"BPNA" -> bpnAs.add(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class BusinessPartnerBuildService(
val addressResponses = createAddressesForSite(siteRequests, errors, metadataMap)
.plus(createAddressesForLegalEntity(legalEntityRequests, errors, metadataMap))

changelogService.createChangelogEntries(addressResponses.map { ChangelogEntryDto(it.address.bpn, ChangelogType.CREATE, ChangelogSubject.ADDRESS) })
changelogService.createChangelogEntries(addressResponses.map { ChangelogEntryDto(it.address.bpna, ChangelogType.CREATE, ChangelogSubject.ADDRESS) })

return EntitiesWithErrors(addressResponses, errors)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fun LegalEntity.toUpsertDto(entryId: String?): LegalEntityPartnerCreateResponse

fun LegalEntity.toDto(): LegalEntityResponse {
return LegalEntityResponse(
bpn = bpn,
bpnl = bpn,
identifiers = identifiers.map { it.toDto() },
legalName = legalName.toDto(),
legalForm = legalForm?.toDto(),
Expand Down Expand Up @@ -111,7 +111,7 @@ fun AddressState.toDto(): AddressStateResponse {

fun LogisticAddress.toDto(): LogisticAddressResponse {
return LogisticAddressResponse(
bpn = bpn,
bpna = bpn,
bpnLegalEntity = legalEntity?.bpn,
isLegalAddress = legalEntity?.legalAddress == this,
bpnSite = site?.bpn,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,9 +470,9 @@ class SaasControllerImportIT @Autowired constructor(

//assert actual is expected
val expectedPartners = listOf(
ResponseValues.legalEntity1.copy(bpn = bpnL2),
ResponseValues.legalEntity2.copy(bpn = bpnL3),
ResponseValues.legalEntity3.copy(bpn = bpnL1)
ResponseValues.legalEntity1.copy(bpnl = bpnL2),
ResponseValues.legalEntity2.copy(bpnl = bpnL3),
ResponseValues.legalEntity3.copy(bpnl = bpnL1)
)
val actual = getLegalEntities(bpns)
testHelpers.assertRecursively(actual).ignoringFieldsMatchingRegexes(".*currentness").isEqualTo(expectedPartners)
Expand Down Expand Up @@ -515,9 +515,9 @@ class SaasControllerImportIT @Autowired constructor(

//assert actual is expected
val expectedPartners = listOf(
ResponseValues.site1.copy(bpn = bpnS2, bpnLegalEntity = bpnL2),
ResponseValues.site2.copy(bpn = bpnS3, bpnLegalEntity = bpnL3),
ResponseValues.site3.copy(bpn = bpnS1, bpnLegalEntity = bpnL1),
ResponseValues.site1.copy(bpns = bpnS2, bpnLegalEntity = bpnL2),
ResponseValues.site2.copy(bpns = bpnS3, bpnLegalEntity = bpnL3),
ResponseValues.site3.copy(bpns = bpnS1, bpnLegalEntity = bpnL1),
)
val actual = getSites(bpns).content
testHelpers.assertRecursively(actual).isEqualTo(expectedPartners)
Expand Down Expand Up @@ -560,9 +560,9 @@ class SaasControllerImportIT @Autowired constructor(

//assert actual is expected
val expectedPartners = listOf(
ResponseValues.addressPartner1.copy(bpn = bpnA2),
ResponseValues.addressPartner2.copy(bpn = bpnA3),
ResponseValues.addressPartner3.copy(bpn = bpnA1)
ResponseValues.addressPartner1.copy(bpna = bpnA2),
ResponseValues.addressPartner2.copy(bpna = bpnA3),
ResponseValues.addressPartner3.copy(bpna = bpnA1)
)
val actual = getAddresses(bpns).content
testHelpers.assertRecursively(actual.toList().sortedBy { it.physicalPostalAddress.industrialZone })
Expand Down Expand Up @@ -595,7 +595,8 @@ class SaasControllerImportIT @Autowired constructor(
private fun getSites(bpns: Collection<String>): PageResponse<SiteResponse> =
poolClient.sites().searchSites(SiteBpnSearchRequest(sites = bpns), PaginationRequest())

private fun getAddresses(bpns: Collection<String>): PageResponse<LogisticAddressResponse> = poolClient.addresses().searchAddresses(AddressPartnerBpnSearchRequest(addresses = bpns), PaginationRequest())
private fun getAddresses(bpns: Collection<String>): PageResponse<LogisticAddressResponse> =
poolClient.addresses().searchAddresses(AddressPartnerBpnSearchRequest(addresses = bpns), PaginationRequest())

private fun importPartners(
partnersToImport: List<BusinessPartnerSaas>,
Expand Down
Loading

0 comments on commit 60afcd0

Please sign in to comment.