From 2d4b5a5b198fc3818e09857a6bd8e5ebbb0a1494 Mon Sep 17 00:00:00 2001 From: alexsilva Date: Mon, 5 Jun 2023 09:01:10 +0100 Subject: [PATCH] feat(gate): Changed Search Output logic on site/legalEntity --- .../bpdm/gate/api/GateLegalEntityApi.kt | 8 +- .../tractusx/bpdm/gate/api/GateSiteApi.kt | 8 +- .../model/LegalEntityGateOutputResponse.kt | 43 +++++++++++ .../gate/api/model/SiteGateOutputResponse.kt | 48 ++++++++++++ .../gate/controller/LegalEntityController.kt | 10 +-- .../bpdm/gate/controller/SiteController.kt | 8 +- .../bpdm/gate/service/AddressService.kt | 46 ------------ .../bpdm/gate/service/LegalEntityService.kt | 75 ++++--------------- .../bpdm/gate/service/ResponseMappings.kt | 30 +++++++- .../tractusx/bpdm/gate/service/SiteService.kt | 61 ++++----------- .../LegalEntityControllerOutputIT.kt | 7 +- .../gate/controller/SiteControllerOutputIT.kt | 6 +- 12 files changed, 167 insertions(+), 183 deletions(-) create mode 100644 bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/LegalEntityGateOutputResponse.kt create mode 100644 bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/SiteGateOutputResponse.kt diff --git a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/GateLegalEntityApi.kt b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/GateLegalEntityApi.kt index 310e56aff..64594e62d 100644 --- a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/GateLegalEntityApi.kt +++ b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/GateLegalEntityApi.kt @@ -29,9 +29,7 @@ import org.eclipse.tractusx.bpdm.common.dto.request.PaginationRequest import org.eclipse.tractusx.bpdm.common.dto.response.PageResponse import org.eclipse.tractusx.bpdm.gate.api.model.LegalEntityGateInputRequest import org.eclipse.tractusx.bpdm.gate.api.model.LegalEntityGateInputResponse -import org.eclipse.tractusx.bpdm.gate.api.model.LegalEntityGateOutput -import org.eclipse.tractusx.bpdm.gate.api.model.request.PaginationStartAfterRequest -import org.eclipse.tractusx.bpdm.gate.api.model.response.PageOutputResponse +import org.eclipse.tractusx.bpdm.gate.api.model.LegalEntityGateOutputResponse import org.eclipse.tractusx.bpdm.gate.api.model.response.ValidationResponse import org.springdoc.core.annotations.ParameterObject import org.springframework.http.MediaType @@ -121,9 +119,9 @@ interface GateLegalEntityApi { @PostMapping("/output/legal-entities/search") @PostExchange("/output/legal-entities/search") fun getLegalEntitiesOutput( - @ParameterObject @Valid paginationRequest: PaginationStartAfterRequest, + @ParameterObject @Valid paginationRequest: PaginationRequest, @RequestBody(required = false) externalIds: Collection? - ): PageOutputResponse + ): PageResponse @Operation( summary = "Validate a legal entity", diff --git a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/GateSiteApi.kt b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/GateSiteApi.kt index 4557793ce..0e6cd64cf 100644 --- a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/GateSiteApi.kt +++ b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/GateSiteApi.kt @@ -29,9 +29,7 @@ import org.eclipse.tractusx.bpdm.common.dto.request.PaginationRequest import org.eclipse.tractusx.bpdm.common.dto.response.PageResponse import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateInputRequest import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateInputResponse -import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateOutput -import org.eclipse.tractusx.bpdm.gate.api.model.request.PaginationStartAfterRequest -import org.eclipse.tractusx.bpdm.gate.api.model.response.PageOutputResponse +import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateOutputResponse import org.eclipse.tractusx.bpdm.gate.api.model.response.ValidationResponse import org.springdoc.core.annotations.ParameterObject import org.springframework.http.MediaType @@ -121,9 +119,9 @@ interface GateSiteApi { @PostMapping("/output/sites/search") @PostExchange("/output/sites/search") fun getSitesOutput( - @ParameterObject @Valid paginationRequest: PaginationStartAfterRequest, + @ParameterObject @Valid paginationRequest: PaginationRequest, @RequestBody(required = false) externalIds: Collection? - ): PageOutputResponse + ): PageResponse @Operation( summary = "Validate a site", diff --git a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/LegalEntityGateOutputResponse.kt b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/LegalEntityGateOutputResponse.kt new file mode 100644 index 000000000..912284111 --- /dev/null +++ b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/LegalEntityGateOutputResponse.kt @@ -0,0 +1,43 @@ +/******************************************************************************* + * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ******************************************************************************/ + +package org.eclipse.tractusx.bpdm.gate.api.model + +import com.fasterxml.jackson.annotation.JsonUnwrapped +import com.fasterxml.jackson.databind.annotation.JsonDeserialize +import io.swagger.v3.oas.annotations.media.Schema +import org.eclipse.tractusx.bpdm.common.dto.LegalEntityDto +import org.eclipse.tractusx.bpdm.common.service.DataClassUnwrappedJsonDeserializer +import java.time.LocalDateTime + +@JsonDeserialize(using = DataClassUnwrappedJsonDeserializer::class) +@Schema(name = "LegalEntityGateOutputResponse", description = "Legal entity with external id") +data class LegalEntityGateOutputResponse( + @field:JsonUnwrapped + val legalEntity: LegalEntityDto, + + @Schema(description = "ID the record has in the external system where the record originates from", required = true) + val externalId: String, + + @Schema(description = "Business Partner Number") + val bpn: String?, + + @Schema(description = "Time the sharing process was started according to SaaS") + val processStartedAt: LocalDateTime? = null, +) \ No newline at end of file diff --git a/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/SiteGateOutputResponse.kt b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/SiteGateOutputResponse.kt new file mode 100644 index 000000000..b27ae86d5 --- /dev/null +++ b/bpdm-gate-api/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/api/model/SiteGateOutputResponse.kt @@ -0,0 +1,48 @@ +/******************************************************************************* + * Copyright (c) 2021,2023 Contributors to the Eclipse Foundation + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ******************************************************************************/ + +package org.eclipse.tractusx.bpdm.gate.api.model + +import com.fasterxml.jackson.annotation.JsonUnwrapped +import com.fasterxml.jackson.databind.annotation.JsonDeserialize +import io.swagger.v3.oas.annotations.media.Schema +import org.eclipse.tractusx.bpdm.common.dto.SiteDto +import org.eclipse.tractusx.bpdm.common.service.DataClassUnwrappedJsonDeserializer +import java.time.LocalDateTime + +@JsonDeserialize(using = DataClassUnwrappedJsonDeserializer::class) +@Schema( + name = "SiteGateOutputResponse", description = "Site with legal entity reference" +) +data class SiteGateOutputResponse( + @field:JsonUnwrapped + val site: SiteGateDto, + + @Schema(description = "ID the record has in the external system where the record originates from") + val externalId: String, + + @Schema(description = "External id of the related legal entity") + val legalEntityExternalId: String, + + @Schema(description = "Business Partner Number") + val bpn: String?, + + @Schema(description = "Time the sharing process was started according to SaaS") + val processStartedAt: LocalDateTime? = null, +) \ No newline at end of file diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/LegalEntityController.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/LegalEntityController.kt index ac81902b6..9afc58592 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/LegalEntityController.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/LegalEntityController.kt @@ -24,9 +24,7 @@ import org.eclipse.tractusx.bpdm.common.dto.response.PageResponse import org.eclipse.tractusx.bpdm.gate.api.GateLegalEntityApi import org.eclipse.tractusx.bpdm.gate.api.model.LegalEntityGateInputRequest import org.eclipse.tractusx.bpdm.gate.api.model.LegalEntityGateInputResponse -import org.eclipse.tractusx.bpdm.gate.api.model.LegalEntityGateOutput -import org.eclipse.tractusx.bpdm.gate.api.model.request.PaginationStartAfterRequest -import org.eclipse.tractusx.bpdm.gate.api.model.response.PageOutputResponse +import org.eclipse.tractusx.bpdm.gate.api.model.LegalEntityGateOutputResponse import org.eclipse.tractusx.bpdm.gate.api.model.response.ValidationResponse import org.eclipse.tractusx.bpdm.gate.config.ApiConfigProperties import org.eclipse.tractusx.bpdm.gate.containsDuplicates @@ -67,10 +65,10 @@ class LegalEntityController( } override fun getLegalEntitiesOutput( - paginationRequest: PaginationStartAfterRequest, + paginationRequest: PaginationRequest, externalIds: Collection? - ): PageOutputResponse { - return legalEntityService.getLegalEntitiesOutput(externalIds, paginationRequest.limit, paginationRequest.startAfter) + ): PageResponse { + return legalEntityService.getLegalEntitiesOutput(externalIds = externalIds, page = paginationRequest.page, size = paginationRequest.size) } override fun validateLegalEntity(legalEntityInput: LegalEntityGateInputRequest): ValidationResponse { diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteController.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteController.kt index 3716cd65c..280501471 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteController.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteController.kt @@ -24,9 +24,7 @@ import org.eclipse.tractusx.bpdm.common.dto.response.PageResponse import org.eclipse.tractusx.bpdm.gate.api.GateSiteApi import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateInputRequest import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateInputResponse -import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateOutput -import org.eclipse.tractusx.bpdm.gate.api.model.request.PaginationStartAfterRequest -import org.eclipse.tractusx.bpdm.gate.api.model.response.PageOutputResponse +import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateOutputResponse import org.eclipse.tractusx.bpdm.gate.api.model.response.ValidationResponse import org.eclipse.tractusx.bpdm.gate.config.ApiConfigProperties import org.eclipse.tractusx.bpdm.gate.containsDuplicates @@ -66,8 +64,8 @@ class SiteController( return siteService.getSites(page = paginationRequest.page, size = paginationRequest.size) } - override fun getSitesOutput(paginationRequest: PaginationStartAfterRequest, externalIds: Collection?): PageOutputResponse { - return siteService.getSitesOutput(externalIds, paginationRequest.limit, paginationRequest.startAfter) + override fun getSitesOutput(paginationRequest: PaginationRequest, externalIds: Collection?): PageResponse { + return siteService.getSitesOutput(externalIds = externalIds, page = paginationRequest.page, size = paginationRequest.size) } override fun validateSite(siteInput: SiteGateInputRequest): ValidationResponse { diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/AddressService.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/AddressService.kt index e49672651..d37cd32e3 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/AddressService.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/AddressService.kt @@ -26,8 +26,6 @@ import org.eclipse.tractusx.bpdm.common.exception.BpdmNotFoundException import org.eclipse.tractusx.bpdm.common.model.OutputInputEnum import org.eclipse.tractusx.bpdm.gate.api.model.AddressGateInputRequest import org.eclipse.tractusx.bpdm.gate.api.model.AddressGateInputResponse -import org.eclipse.tractusx.bpdm.gate.api.model.AddressGateOutput -import org.eclipse.tractusx.bpdm.gate.api.model.response.LogisticAddressGateResponse import org.eclipse.tractusx.bpdm.gate.api.model.AddressGateOutputResponse import org.eclipse.tractusx.bpdm.gate.api.model.response.LsaType import org.eclipse.tractusx.bpdm.gate.config.BpnConfigProperties @@ -111,50 +109,6 @@ class AddressService( } } -// val partnerResponse = saasClient.getAddresses(limit = limit, startAfter = startAfter, externalIds = externalIds) -// val partners = partnerResponse.values -// -// val partnersWithExternalId = outputSaasMappingService.mapWithExternalId(partners) -// val augmentedPartnerResponse = saasClient.getAugmentedAddresses(externalIds = partnersWithExternalId.map { it.externalId }) -// val partnersWithLocalBpn = outputSaasMappingService.mapWithLocalBpn(partnersWithExternalId, augmentedPartnerResponse.values) -// -// //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.bpna } -// -// if (bpnSet.size > addressesByBpnMap.size) { -// logger.warn { "Requested ${bpnSet.size} addresses from pool, but only ${addressesByBpnMap.size} were found." } -// } -// -// val partnersWithPoolBpn = partnersWithLocalBpn.filter { addressesByBpnMap[it.bpn] != null } -// val bpnByExternalIdMap = partnersWithPoolBpn.map { Pair(it.partner.externalId!!, it.bpn) }.toMap() -// -// //Evaluate the sharing status of the legal entities -// val sharingStatus = outputSaasMappingService.evaluateSharingStatus(partners, partnersWithLocalBpn, partnersWithPoolBpn) -// -// val validAddresses = sharingStatus.validExternalIds.map { externalId -> -// val bpn = bpnByExternalIdMap[externalId]!! -// val address = addressesByBpnMap[bpn]!! -// toAddressOutput(externalId, address) -// } -// -// return PageOutputResponse( -// total = partnerResponse.total, -// nextStartAfter = partnerResponse.nextStartAfter, -// content = validAddresses, -// invalidEntries = partners.size - sharingStatus.validExternalIds.size, // difference between all entries from SaaS and valid content -// pending = sharingStatus.pendingExternalIds, -// errors = sharingStatus.errors, -// ) -// } - - fun toAddressOutput(externalId: String, address: LogisticAddressGateResponse): AddressGateOutput { - return AddressGateOutput( - address = address, - externalId = externalId - ) - } - /** * Upsert addresses by: * diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/LegalEntityService.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/LegalEntityService.kt index 62ca4b554..41a15bfa2 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/LegalEntityService.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/LegalEntityService.kt @@ -20,17 +20,13 @@ package org.eclipse.tractusx.bpdm.gate.service import mu.KotlinLogging -import org.eclipse.tractusx.bpdm.common.dto.response.LegalEntityResponse import org.eclipse.tractusx.bpdm.common.dto.response.PageResponse -import org.eclipse.tractusx.bpdm.common.dto.response.PoolLegalEntityResponse import org.eclipse.tractusx.bpdm.common.exception.BpdmNotFoundException import org.eclipse.tractusx.bpdm.common.model.OutputInputEnum import org.eclipse.tractusx.bpdm.gate.api.model.LegalEntityGateInputRequest import org.eclipse.tractusx.bpdm.gate.api.model.LegalEntityGateInputResponse -import org.eclipse.tractusx.bpdm.gate.api.model.LegalEntityGateOutput -import org.eclipse.tractusx.bpdm.gate.api.model.response.LogisticAddressGateResponse +import org.eclipse.tractusx.bpdm.gate.api.model.LegalEntityGateOutputResponse import org.eclipse.tractusx.bpdm.gate.api.model.response.LsaType -import org.eclipse.tractusx.bpdm.gate.api.model.response.PageOutputResponse import org.eclipse.tractusx.bpdm.gate.entity.ChangelogEntry import org.eclipse.tractusx.bpdm.gate.entity.LegalEntity import org.eclipse.tractusx.bpdm.gate.repository.ChangelogRepository @@ -92,69 +88,26 @@ class LegalEntityService( * Get legal entities by first fetching legal entities from "augmented business partners" in SaaS. Augmented business partners from SaaS should contain a BPN, * which is then used to fetch the data for the legal entities from the bpdm pool. */ - fun getLegalEntitiesOutput(externalIds: Collection?, limit: Int, startAfter: String?): PageOutputResponse { - val partnerResponse = saasClient.getLegalEntities(limit = limit, startAfter = startAfter, externalIds = externalIds) - val partners = partnerResponse.values + fun getLegalEntitiesOutput(externalIds: Collection?, page: Int, size: Int): PageResponse { - val partnersWithExternalId = outputSaasMappingService.mapWithExternalId(partners) - val augmentedPartnerResponse = saasClient.getAugmentedLegalEntities(externalIds = partnersWithExternalId.map { it.externalId }) - val partnersWithLocalBpn = outputSaasMappingService.mapWithLocalBpn(partnersWithExternalId, augmentedPartnerResponse.values) + val legalEntityPage = legalEntityRepository.findByExternalIdInAndDataType(externalIds, OutputInputEnum.Output, PageRequest.of(page, size)) - //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.legalEntity.bpnl } - val legalAddressesByBpnMap = poolClient.searchLegalAddresses(bpnSet).associateBy { it.bpnLegalEntity } - - if (bpnSet.size > legalEntitiesByBpnMap.size) { - logger.warn { "Requested ${bpnSet.size} legal entities from pool, but only ${legalEntitiesByBpnMap.size} were found." } - } - if (bpnSet.size > legalAddressesByBpnMap.size) { - logger.warn { "Requested ${bpnSet.size} legal addresses from pool, but only ${legalAddressesByBpnMap.size} were found." } - } - - //Filter only legal entities which can be found with their legal address in the Pool under the given local BPN - val partnersWithPoolBpn = partnersWithLocalBpn.filter { legalEntitiesByBpnMap[it.bpn] != null && legalAddressesByBpnMap[it.bpn] != null } - val bpnByExternalIdMap = partnersWithPoolBpn.associate { Pair(it.externalId, it.bpn) } + return PageResponse( + page = page, + totalElements = legalEntityPage.totalElements, + totalPages = legalEntityPage.totalPages, + contentSize = legalEntityPage.content.size, + content = toValidOutputLegalEntities(legalEntityPage), + ) - //Evaluate the sharing status of the legal entities - val sharingStatus = outputSaasMappingService.evaluateSharingStatus(partners, partnersWithLocalBpn, partnersWithPoolBpn) + } - val validLegalEntities = sharingStatus.validExternalIds.map { externalId -> - val bpn = bpnByExternalIdMap[externalId]!! - val legalEntity = legalEntitiesByBpnMap[bpn]!! - val legalAddress = legalAddressesByBpnMap[bpn]!! - toLegalEntityOutput(externalId, legalEntity, legalAddress) + private fun toValidOutputLegalEntities(legalEntityPage: Page): List { + return legalEntityPage.content.map { legalEntity -> + legalEntity.toLegalEntityGateOutputResponse(legalEntity) } - - return PageOutputResponse( - total = partnerResponse.total, - nextStartAfter = partnerResponse.nextStartAfter, - content = validLegalEntities, - invalidEntries = partners.size - sharingStatus.validExternalIds.size, // difference between all entries from SaaS and valid content - pending = sharingStatus.pendingExternalIds, - errors = sharingStatus.errors, - ) } - fun toLegalEntityOutput(externalId: String, legalEntityPool: PoolLegalEntityResponse, legalAddress: LogisticAddressGateResponse): LegalEntityGateOutput = - LegalEntityGateOutput( - legalEntity = LegalEntityResponse( - bpnl = legalEntityPool.legalEntity.bpnl, - identifiers = legalEntityPool.legalEntity.identifiers, - legalShortName = legalEntityPool.legalEntity.legalShortName, - legalForm = legalEntityPool.legalEntity.legalForm, - states = legalEntityPool.legalEntity.states, - classifications = legalEntityPool.legalEntity.classifications, - relations = legalEntityPool.legalEntity.relations, - currentness = legalEntityPool.legalEntity.currentness, - createdAt = legalEntityPool.legalEntity.createdAt, - updatedAt = legalEntityPool.legalEntity.updatedAt, - ), - legalAddress = legalAddress, - legalNameParts = arrayOf(legalEntityPool.legalName), - externalId = externalId - ) - private fun toValidLegalEntities(legalEntityPage: Page): List { return legalEntityPage.content.map { legalEntity -> legalEntity.LegalEntityGateInputResponse(legalEntity) diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/ResponseMappings.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/ResponseMappings.kt index 56d066e96..b14954ef4 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/ResponseMappings.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/ResponseMappings.kt @@ -347,7 +347,7 @@ fun mapToDtoSitesStates(states: MutableSet): Collection return states.map { SiteStateDto(it.description, it.validFrom, it.validTo, it.type) } } -//LegalEntity mapping to LegalEntityGateInputResponse +//Site mapping to SiteGateInputResponse fun Site.toSiteGateInputResponse(sitePage: Site): SiteGateInputResponse { return SiteGateInputResponse( @@ -360,7 +360,7 @@ fun Site.toSiteGateInputResponse(sitePage: Site): SiteGateInputResponse { } -//Logistic Address mapping to AddressGateInputResponse +//Logistic Address mapping to AddressGateOutputResponse fun LogisticAddress.toAddressGateOutputResponse(logisticAddressPage: LogisticAddress): AddressGateOutputResponse { val addressGateOutputResponse = AddressGateOutputResponse( @@ -373,3 +373,29 @@ fun LogisticAddress.toAddressGateOutputResponse(logisticAddressPage: LogisticAdd return addressGateOutputResponse } + +//Site mapping to SiteGateOutputResponse +fun Site.toSiteGateOutputResponse(sitePage: Site): SiteGateOutputResponse { + + return SiteGateOutputResponse( + site = sitePage.toSiteDto(), + externalId = externalId, + legalEntityExternalId = legalEntity.externalId, + bpn = bpn, + processStartedAt = null //TODO Remove this? + ) + +} + +//LegalEntity mapping to LegalEntityGateOutputResponse +fun LegalEntity.toLegalEntityGateOutputResponse(legalEntity: LegalEntity): LegalEntityGateOutputResponse { + + return LegalEntityGateOutputResponse( + legalEntity = legalEntity.toLegalEntityDto(), + externalId = legalEntity.externalId, + bpn = legalEntity.bpn, + processStartedAt = null //TODO Remove this? + ) +} + + diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SiteService.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SiteService.kt index e3906c225..297f44d13 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SiteService.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SiteService.kt @@ -21,16 +21,13 @@ package org.eclipse.tractusx.bpdm.gate.service import mu.KotlinLogging import org.eclipse.tractusx.bpdm.common.dto.response.PageResponse -import org.eclipse.tractusx.bpdm.common.dto.response.SiteResponse import org.eclipse.tractusx.bpdm.common.dto.saas.BusinessPartnerSaas import org.eclipse.tractusx.bpdm.common.exception.BpdmNotFoundException import org.eclipse.tractusx.bpdm.common.model.OutputInputEnum import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateInputRequest import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateInputResponse -import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateOutput -import org.eclipse.tractusx.bpdm.gate.api.model.response.LogisticAddressGateResponse +import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateOutputResponse import org.eclipse.tractusx.bpdm.gate.api.model.response.LsaType -import org.eclipse.tractusx.bpdm.gate.api.model.response.PageOutputResponse import org.eclipse.tractusx.bpdm.gate.config.BpnConfigProperties import org.eclipse.tractusx.bpdm.gate.entity.ChangelogEntry import org.eclipse.tractusx.bpdm.gate.entity.Site @@ -87,56 +84,26 @@ class SiteService( * Get sites by first fetching sites from "augmented business partners" in SaaS. Augmented business partners from SaaS should contain a BPN, * which is then used to fetch the data for the sites from the bpdm pool. */ - fun getSitesOutput(externalIds: Collection?, limit: Int, startAfter: String?): PageOutputResponse { - val partnerResponse = saasClient.getSites(limit = limit, startAfter = startAfter, externalIds = externalIds) - val partners = partnerResponse.values + fun getSitesOutput(externalIds: Collection?, page: Int, size: Int): PageResponse { - val partnersWithExternalId = outputSaasMappingService.mapWithExternalId(partners) - val augmentedPartnerResponse = saasClient.getAugmentedSites(externalIds = partnersWithExternalId.map { it.externalId }) - val partnersWithLocalBpn = outputSaasMappingService.mapWithLocalBpn(partnersWithExternalId, augmentedPartnerResponse.values) + val sitePage = siteRepository.findByExternalIdInAndDataType(externalIds, OutputInputEnum.Output, PageRequest.of(page, size)) - val bpnSet = partnersWithLocalBpn.map { it.bpn }.toSet() - val sitesByBpnMap = poolClient.searchSites(bpnSet).associateBy { it.bpns } - val mainAddressesByBpnMap = poolClient.searchMainAddresses(bpnSet).associateBy { it.bpnSite } - - if (bpnSet.size > sitesByBpnMap.size) { - logger.warn { "Requested ${bpnSet.size} sites from pool, but only ${sitesByBpnMap.size} were found." } - } - if (bpnSet.size > mainAddressesByBpnMap.size) { - logger.warn { "Requested ${bpnSet.size} main addresses of sites from pool, but only ${mainAddressesByBpnMap.size} were found." } - } - - //Filter only sites which can be found with their main address in the Pool under the given local BPN - val partnersWithPoolBpn = partnersWithLocalBpn.filter { sitesByBpnMap[it.bpn] != null && mainAddressesByBpnMap[it.bpn] != null } - val bpnByExternalIdMap = partnersWithPoolBpn.map { Pair(it.partner.externalId!!, it.bpn) }.toMap() + return PageResponse( + page = page, + totalElements = sitePage.totalElements, + totalPages = sitePage.totalPages, + contentSize = sitePage.content.size, + content = toValidOutputSites(sitePage), + ) - //Evaluate the sharing status of the legal entities - val sharingStatus = outputSaasMappingService.evaluateSharingStatus(partners, partnersWithLocalBpn, partnersWithPoolBpn) + } - val validSites = sharingStatus.validExternalIds.map { externalId -> - val bpn = bpnByExternalIdMap[externalId]!! - val site = sitesByBpnMap[bpn]!! - val mainAddress = mainAddressesByBpnMap[bpn]!! - toSiteOutput(externalId, site, mainAddress) + private fun toValidOutputSites(sitePage: Page): List { + return sitePage.content.map { sites -> + sites.toSiteGateOutputResponse(sites) } - - return PageOutputResponse( - total = partnerResponse.total, - nextStartAfter = partnerResponse.nextStartAfter, - content = validSites, - invalidEntries = partners.size - sharingStatus.validExternalIds.size, // difference between all entries from SaaS and valid content - pending = sharingStatus.pendingExternalIds, - errors = sharingStatus.errors, - ) } - fun toSiteOutput(externalId: String, site: SiteResponse, mainAddress: LogisticAddressGateResponse) = - SiteGateOutput( - site = site, - mainAddress = mainAddress, - externalId = externalId - ) - /** * Upsert sites by: * diff --git a/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/controller/LegalEntityControllerOutputIT.kt b/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/controller/LegalEntityControllerOutputIT.kt index 031d6bd8c..2aed005eb 100644 --- a/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/controller/LegalEntityControllerOutputIT.kt +++ b/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/controller/LegalEntityControllerOutputIT.kt @@ -23,11 +23,12 @@ import com.fasterxml.jackson.databind.ObjectMapper import com.github.tomakehurst.wiremock.client.WireMock.* import com.github.tomakehurst.wiremock.core.WireMockConfiguration import com.github.tomakehurst.wiremock.junit5.WireMockExtension +import org.assertj.core.api.Assertions.assertThat +import org.eclipse.tractusx.bpdm.common.dto.request.PaginationRequest import org.eclipse.tractusx.bpdm.common.dto.saas.AugmentedBusinessPartnerResponseSaas import org.eclipse.tractusx.bpdm.common.dto.saas.PagedResponseSaas import org.eclipse.tractusx.bpdm.gate.api.client.GateClient import org.eclipse.tractusx.bpdm.gate.api.exception.BusinessPartnerSharingError -import org.eclipse.tractusx.bpdm.gate.api.model.request.PaginationStartAfterRequest import org.eclipse.tractusx.bpdm.gate.api.model.response.ErrorInfo import org.eclipse.tractusx.bpdm.gate.api.model.response.PageOutputResponse import org.eclipse.tractusx.bpdm.gate.util.* @@ -176,7 +177,7 @@ internal class LegalEntityControllerOutputIT @Autowired constructor( ) ) - val paginationValue = PaginationStartAfterRequest(startAfter, limit) + val paginationValue = PaginationRequest(0, 10) val pageResponse = gateClient.legalEntities().getLegalEntitiesOutput(paginationValue, emptyList()) val expectedResponse = PageOutputResponse( total = total, @@ -281,7 +282,7 @@ internal class LegalEntityControllerOutputIT @Autowired constructor( ) ) - val paginationValue = PaginationStartAfterRequest(startAfter, limit) + val paginationValue = PaginationRequest(0, 10) val pageResponse = gateClient.legalEntities().getLegalEntitiesOutput(paginationValue, listOf(CommonValues.externalId1, CommonValues.externalId2)) val expectedResponse = PageOutputResponse( total = total, diff --git a/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteControllerOutputIT.kt b/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteControllerOutputIT.kt index ee30bc563..d057f3621 100644 --- a/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteControllerOutputIT.kt +++ b/bpdm-gate/src/test/kotlin/org/eclipse/tractusx/bpdm/gate/controller/SiteControllerOutputIT.kt @@ -24,12 +24,12 @@ import com.github.tomakehurst.wiremock.client.WireMock.* import com.github.tomakehurst.wiremock.core.WireMockConfiguration import com.github.tomakehurst.wiremock.junit5.WireMockExtension import org.assertj.core.api.Assertions.assertThat +import org.eclipse.tractusx.bpdm.common.dto.request.PaginationRequest import org.eclipse.tractusx.bpdm.common.dto.response.PageResponse import org.eclipse.tractusx.bpdm.common.dto.saas.AugmentedBusinessPartnerResponseSaas import org.eclipse.tractusx.bpdm.common.dto.saas.PagedResponseSaas import org.eclipse.tractusx.bpdm.gate.api.client.GateClient import org.eclipse.tractusx.bpdm.gate.api.exception.BusinessPartnerSharingError -import org.eclipse.tractusx.bpdm.gate.api.model.request.PaginationStartAfterRequest import org.eclipse.tractusx.bpdm.gate.api.model.response.ErrorInfo import org.eclipse.tractusx.bpdm.gate.api.model.response.PageOutputResponse import org.eclipse.tractusx.bpdm.gate.util.* @@ -182,7 +182,7 @@ internal class SiteControllerOutputIT @Autowired constructor( ) ) - val paginationValue = PaginationStartAfterRequest(startAfter, limit) + val paginationValue = PaginationRequest(0, 10) val pageResponseValue = gateClient.sites().getSitesOutput(paginationValue, emptyList()) assertThat(pageResponseValue).isEqualTo( @@ -295,7 +295,7 @@ internal class SiteControllerOutputIT @Autowired constructor( ) ) - val paginationValue = PaginationStartAfterRequest(startAfter, limit) + val paginationValue = PaginationRequest(0, 10) val pageResponseValue = gateClient.sites().getSitesOutput(paginationValue, listOf(CommonValues.externalIdSite1, CommonValues.externalIdSite2)) assertThat(pageResponseValue).isEqualTo(