Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat - Fetch of input data from the persistence layer (Logistic Address + Legal Entities) #184

Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
61c75f6
fix(gate): Bridge Dummy Pagination changes, Unit test fixes according…
alexsilva-CGI May 22, 2023
c43ad8c
fix(gate): Fix removal of administrativeArea and New invalid entries …
alexsilva-CGI May 24, 2023
0328826
fix(gate): Changed Created Paginated collection for the Common PageRe…
alexsilva-CGI May 25, 2023
9d7278f
feat(gate): junction of site persistence to LogisticAddress and Legal…
alexsilva-CGI May 4, 2023
3de9b02
feat(gate): Replaced Saas provider calls in some addresses services f…
alexsilva-CGI May 17, 2023
35bc580
fix(gate): Unit test fixes according to new service logic
alexsilva-CGI May 22, 2023
1064612
feat(gate): Added Logic for site entity, added flyway migration file …
alexsilva-CGI Apr 5, 2023
d51fdb0
fix(gate): added new changes to upsert unit tests to check for persis…
alexsilva-CGI May 8, 2023
1386b62
feat(gate): Replaced SaaS provider calls in some legalEntity services…
cezaralexandremorais May 18, 2023
856a169
fix(gate): changed some Legalentity controllet input Unit tests
cezaralexandremorais May 23, 2023
04f60d5
feat(gate): Changed logic of LegalEntityGateInputResponse mapping and…
cezaralexandremorais May 25, 2023
76784d7
fix(gate): Usage of Page Response from Common, instead of created Dat…
alexsilva-CGI May 25, 2023
7ee5376
feat(gate): Fixed some unit test removing Saas logic on LegalEntityCo…
cezaralexandremorais May 29, 2023
e4d7753
fix(gate): fix unit test legal entity error, Removed unused SaaS unit…
alexsilva-CGI May 29, 2023
874a60a
fix(gate): Remove double persistence in site logic
alexsilva-CGI May 30, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
nicoprow marked this conversation as resolved.
Show resolved Hide resolved
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 ->
nicoprow marked this conversation as resolved.
Show resolved Hide resolved
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
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ class SiteService(
val sitesSaas = toSaasModels(sites)
saasClient.upsertSites(sitesSaas)

sitePersistenceService.persistSitesBP(sites)

nicoprow marked this conversation as resolved.
Show resolved Hide resolved
// create changelog entry if all goes well from saasClient
sites.forEach { site ->
changelogRepository.save(ChangelogEntry(site.externalId, LsaType.Site))
Expand Down
Loading