Skip to content

Commit

Permalink
Merge pull request #184 from catenax-ng/feat/persistence_query_input_…
Browse files Browse the repository at this point in the history
…legalentities

Feat - Fetch of input data from the persistence layer (Logistic Address + Legal Entities)
  • Loading branch information
nicoprow authored May 30, 2023
2 parents a1ac682 + 874a60a commit 3864433
Show file tree
Hide file tree
Showing 10 changed files with 251 additions and 360 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,11 @@ class GateQueryService(
do {
val pageResponse = gateClient.legalEntities().getLegalEntitiesByExternalIds(
externalIds = externalIds,
paginationRequest = PaginationStartAfterRequest(pageStartAfter, bridgeConfigProperties.queryPageSize)
paginationRequest = PaginationRequest(0, bridgeConfigProperties.queryPageSize)
)
pageStartAfter = pageResponse.nextStartAfter
//pageStartAfter = pageResponse.nextStartAfter
validContent.addAll(pageResponse.content)
invalidEntries += pageResponse.invalidEntries
invalidEntries += 0 //pageResponse.invalidEntries //TODO Needs to be changed according to the removal of SaaS
} while (pageStartAfter != null)

logger.info { "Gate returned ${validContent.size} valid legal entities, $invalidEntries were invalid" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ import io.swagger.v3.oas.annotations.media.Content
import io.swagger.v3.oas.annotations.responses.ApiResponse
import io.swagger.v3.oas.annotations.responses.ApiResponses
import jakarta.validation.Valid
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.response.PageStartAfterResponse
import org.eclipse.tractusx.bpdm.gate.api.model.response.ValidationResponse
import org.springdoc.core.annotations.ParameterObject
import org.springframework.http.MediaType
Expand Down Expand Up @@ -89,9 +90,9 @@ interface GateLegalEntityApi {
@PostMapping("/input/legal-entities/search")
@PostExchange("/input/legal-entities/search")
fun getLegalEntitiesByExternalIds(
@ParameterObject @Valid paginationRequest: PaginationStartAfterRequest,
@ParameterObject @Valid paginationRequest: PaginationRequest,
@RequestBody externalIds: Collection<String>
): PageStartAfterResponse<LegalEntityGateInputResponse>
): PageResponse<LegalEntityGateInputResponse>

@Operation(
summary = "Get page of legal entities",
Expand All @@ -105,7 +106,7 @@ interface GateLegalEntityApi {
)
@GetMapping("/input/legal-entities")
@GetExchange("/input/legal-entities")
fun getLegalEntities(@ParameterObject @Valid paginationRequest: PaginationStartAfterRequest): PageStartAfterResponse<LegalEntityGateInputResponse>
fun getLegalEntities(@ParameterObject @Valid paginationRequest: PaginationRequest): PageResponse<LegalEntityGateInputResponse>

@Operation(
summary = "Get page of legal entities",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*******************************************************************************
* 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.response

import io.swagger.v3.oas.annotations.media.Schema

@Schema(description = "Paginated collection of Logistic Addresses")
data class PageLogisticAddressResponse<T>(
@Schema(description = "Total number of all results in all pages")
val totalElements: Long,
@Schema(description = "Total number pages")
val totalPages: Int,
@Schema(description = "Current page number")
val page: Int,
@Schema(description = "Number of results in the page")
val contentSize: Int,
@Schema(description = "Collection of results in the page")
val content: Collection<T>,
)

Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@

package org.eclipse.tractusx.bpdm.gate.controller

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.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.response.PageStartAfterResponse
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
Expand Down Expand Up @@ -55,14 +56,14 @@ class LegalEntityController(
}

override fun getLegalEntitiesByExternalIds(
paginationRequest: PaginationStartAfterRequest,
paginationRequest: PaginationRequest,
externalIds: Collection<String>
): PageStartAfterResponse<LegalEntityGateInputResponse> {
return legalEntityService.getLegalEntities(limit = paginationRequest.limit, startAfter = paginationRequest.startAfter, externalIds = externalIds)
): PageResponse<LegalEntityGateInputResponse> {
return legalEntityService.getLegalEntities(page = paginationRequest.page, size = paginationRequest.size, externalIds = externalIds)
}

override fun getLegalEntities(paginationRequest: PaginationStartAfterRequest): PageStartAfterResponse<LegalEntityGateInputResponse> {
return legalEntityService.getLegalEntities(paginationRequest.limit, paginationRequest.startAfter)
override fun getLegalEntities(paginationRequest: PaginationRequest): PageResponse<LegalEntityGateInputResponse> {
return legalEntityService.getLegalEntities(page = paginationRequest.page, size = paginationRequest.size)
}

override fun getLegalEntitiesOutput(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@
package org.eclipse.tractusx.bpdm.gate.repository

import org.eclipse.tractusx.bpdm.gate.entity.LegalEntity
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.repository.CrudRepository

interface LegalEntityRepository : JpaRepository<LegalEntity, Long>, CrudRepository<LegalEntity, Long> {

fun findDistinctByExternalIdIn(externalId: Collection<String>): Set<LegalEntity>

fun findByExternalId(externalId: String): LegalEntity?

fun findByExternalIdIn(externalId: Collection<String>?, pageable: Pageable): Page<LegalEntity>
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,64 +22,62 @@ 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.LogisticAddressResponse
import org.eclipse.tractusx.bpdm.common.dto.saas.BusinessPartnerSaas
import org.eclipse.tractusx.bpdm.common.dto.saas.FetchResponse
import org.eclipse.tractusx.bpdm.common.exception.BpdmMappingException
import org.eclipse.tractusx.bpdm.common.dto.response.PageResponse
import org.eclipse.tractusx.bpdm.common.exception.BpdmNotFoundException
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.LsaType
import org.eclipse.tractusx.bpdm.gate.api.model.response.PageOutputResponse
import org.eclipse.tractusx.bpdm.gate.api.model.response.PageStartAfterResponse
import org.eclipse.tractusx.bpdm.gate.entity.ChangelogEntry
import org.eclipse.tractusx.bpdm.gate.entity.LegalEntity
import org.eclipse.tractusx.bpdm.gate.repository.ChangelogRepository
import org.eclipse.tractusx.bpdm.gate.repository.LegalEntityRepository
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageRequest
import org.springframework.stereotype.Service

@Service
class LegalEntityService(
private val saasRequestMappingService: SaasRequestMappingService,
private val inputSaasMappingService: InputSaasMappingService,
private val outputSaasMappingService: OutputSaasMappingService,
private val saasClient: SaasClient,
private val poolClient: PoolClient,
private val changelogRepository: ChangelogRepository,
private val legalEntityPersistenceService: LegalEntityPersistenceService
private val legalEntityPersistenceService: LegalEntityPersistenceService,
private val legalEntityRepository: LegalEntityRepository
) {

private val logger = KotlinLogging.logger { }

fun upsertLegalEntities(legalEntities: Collection<LegalEntityGateInputRequest>) {

val legalEntitiesSaas = legalEntities.map { saasRequestMappingService.toSaasModel(it) }
saasClient.upsertLegalEntities(legalEntitiesSaas)

// create changelog entry if all goes well from saasClient
legalEntities.forEach { legalEntity ->
changelogRepository.save(ChangelogEntry(legalEntity.externalId, LsaType.LegalEntity))
}
legalEntityPersistenceService.persistLegalEntitiesBP(legalEntities);
legalEntityPersistenceService.persistLegalEntitiesBP(legalEntities)
}

fun getLegalEntityByExternalId(externalId: String): LegalEntityGateInputResponse {
val fetchResponse = saasClient.getBusinessPartner(externalId)

when (fetchResponse.status) {
FetchResponse.Status.OK -> return inputSaasMappingService.toInputLegalEntity(fetchResponse.businessPartner!!)
FetchResponse.Status.NOT_FOUND -> throw BpdmNotFoundException("Legal Entity", externalId)
}
val legalEntity = legalEntityRepository.findByExternalId(externalId) ?: throw BpdmNotFoundException("LegalEntity", externalId)
return toValidSingleLegalEntity(legalEntity)
}

fun getLegalEntities(limit: Int, startAfter: String?, externalIds: Collection<String>? = null): PageStartAfterResponse<LegalEntityGateInputResponse> {
val partnerCollection = saasClient.getLegalEntities(limit, startAfter, externalIds)
fun getLegalEntities(page: Int, size: Int, externalIds: Collection<String>? = null): PageResponse<LegalEntityGateInputResponse> {

val validEntries = toValidLegalEntities(partnerCollection.values)
val legalEntitiesPage = if (externalIds != null) {
legalEntityRepository.findByExternalIdIn(externalIds, PageRequest.of(page, size))
} else {
legalEntityRepository.findAll(PageRequest.of(page, size))
}

return PageStartAfterResponse(
total = partnerCollection.total,
nextStartAfter = partnerCollection.nextStartAfter,
content = validEntries,
invalidEntries = partnerCollection.values.size - validEntries.size
return PageResponse(
page = page,
totalElements = legalEntitiesPage.totalElements,
totalPages = legalEntitiesPage.totalPages,
contentSize = legalEntitiesPage.content.size,
content = toValidLegalEntities(legalEntitiesPage)
)
}

Expand Down Expand Up @@ -138,21 +136,19 @@ class LegalEntityService(
externalId = externalId
)

private fun toValidLegalEntities(partners: Collection<BusinessPartnerSaas>): Collection<LegalEntityGateInputResponse> {
return partners.mapNotNull {
val logMessageStart =
"SaaS business partner for legal entity with ID ${it.id ?: "Unknown"}"

try {
if (it.addresses.size > 1) {
logger.warn { "$logMessageStart has multiple legal addresses." }
}

inputSaasMappingService.toInputLegalEntity(it)
} catch (e: BpdmMappingException) {
logger.warn { "$logMessageStart will be ignored: ${e.message}" }
null
}
private fun toValidLegalEntities(legalEntityPage: Page<LegalEntity>): List<LegalEntityGateInputResponse> {
return legalEntityPage.content.map { legalEntity ->
legalEntity.LegalEntityGateInputResponse(legalEntity)
}
}

}

private fun toValidSingleLegalEntity(legalEntity: LegalEntity): LegalEntityGateInputResponse {

return LegalEntityGateInputResponse(
legalEntity = legalEntity.toLegalEntityDto(),
bpn = legalEntity.bpn,
externalId = legalEntity.externalId
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import org.eclipse.tractusx.bpdm.common.dto.response.PageResponse
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.LegalEntityGateInputRequest
import org.eclipse.tractusx.bpdm.gate.api.model.LegalEntityGateInputResponse
import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateInputRequest
import org.eclipse.tractusx.bpdm.gate.api.model.response.ChangelogResponse
import org.eclipse.tractusx.bpdm.gate.entity.*
Expand Down Expand Up @@ -160,9 +161,9 @@ fun LegalEntityGateInputRequest.toLegalEntity(): LegalEntity {
legalName = legalEntity.legalName.toName()
)

legalEntity.identifiers.addAll(this.legalEntity.identifiers.map { toEntityIdentifier(it, legalEntity) })
legalEntity.states.addAll(this.legalEntity.states.map { toEntityState(it, legalEntity) })
legalEntity.classifications.addAll(this.legalEntity.classifications.map { toEntityClassification(it, legalEntity) })
legalEntity.identifiers.addAll( this.legalEntity.identifiers.map {toEntityIdentifier(it,legalEntity)})
legalEntity.states.addAll(this.legalEntity.states.map { toEntityState(it,legalEntity) })
legalEntity.classifications.addAll(this.legalEntity.classifications.map { toEntityClassification(it,legalEntity) })

legalEntity.legalAddress = addressInputRequest.toAddressGate(legalEntity, null)

Expand All @@ -177,6 +178,7 @@ fun toEntityIdentifier(dto: LegalEntityIdentifierDto, legalEntity: LegalEntity):
fun toEntityState(dto: LegalEntityStateDto, legalEntity: LegalEntity): LegalEntityState {
return LegalEntityState(dto.officialDenotation, dto.validFrom, dto.validTo, dto.type, legalEntity)
}

fun toEntityClassification(dto: ClassificationDto, legalEntity: LegalEntity): Classification {
return Classification(dto.value, dto.code, dto.type, legalEntity)
}
Expand Down Expand Up @@ -296,4 +298,41 @@ private fun Street.toStreetDto(): StreetDto {
milestone = milestone,
direction = direction
)
}

fun LegalEntity.toLegalEntityDto(): LegalEntityDto {
return LegalEntityDto(
legalName = legalName.toNameDto(),
legalForm = legalForm,
legalAddress = legalAddress.toLogisticAddressDto(),
states = mapToLegalEntityStateDto(states),
classifications = mapToLegalEntityClassificationsDto(classifications),
identifiers = mapToLegalEntityIdentifierDto(identifiers)
)

}

fun mapToLegalEntityStateDto(states: MutableSet<LegalEntityState>): Collection<LegalEntityStateDto> {
return states.map { LegalEntityStateDto(it.officialDenotation, it.validFrom, it.validTo, it.type) }
}

fun mapToLegalEntityIdentifierDto(identifier: MutableSet<LegalEntityIdentifier>): Collection<LegalEntityIdentifierDto> {
return identifier.map { LegalEntityIdentifierDto(it.value, it.type.toString(), it.issuingBody) }
}

fun mapToLegalEntityClassificationsDto(classification: MutableSet<Classification>): Collection<ClassificationDto> {
return classification.map { ClassificationDto(it.value, it.code, it.type) }
}

fun Name.toNameDto(): NameDto {
return NameDto(value, shortName)
}

fun LegalEntity.LegalEntityGateInputResponse(legalEntity: LegalEntity): LegalEntityGateInputResponse {
return LegalEntityGateInputResponse(
legalEntity = legalEntity.toLegalEntityDto(),
externalId = legalEntity.externalId,
bpn = legalEntity.bpn,
processStartedAt = null
)
}
Loading

0 comments on commit 3864433

Please sign in to comment.