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

fix(Golden Record): Update on Legal and Site Main Address #714

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -31,14 +31,12 @@ import org.eclipse.tractusx.bpdm.gate.api.model.SharingStateType
import org.eclipse.tractusx.bpdm.gate.api.model.request.BusinessPartnerInputRequest
import org.eclipse.tractusx.bpdm.gate.api.model.response.BusinessPartnerInputDto
import org.eclipse.tractusx.bpdm.gate.api.model.response.BusinessPartnerOutputDto
import org.eclipse.tractusx.bpdm.gate.config.PoolConfigProperties
import org.eclipse.tractusx.bpdm.gate.entity.ChangelogEntry
import org.eclipse.tractusx.bpdm.gate.entity.generic.*
import org.eclipse.tractusx.bpdm.gate.exception.BpdmMissingStageException
import org.eclipse.tractusx.bpdm.gate.repository.ChangelogRepository
import org.eclipse.tractusx.bpdm.gate.repository.SharingStateRepository
import org.eclipse.tractusx.bpdm.gate.repository.generic.BusinessPartnerRepository
import org.eclipse.tractusx.bpdm.pool.api.client.PoolApiClient
import org.eclipse.tractusx.orchestrator.api.client.OrchestrationApiClient
import org.eclipse.tractusx.orchestrator.api.model.BusinessPartnerGenericDto
import org.eclipse.tractusx.orchestrator.api.model.TaskCreateRequest
Expand All @@ -56,11 +54,7 @@ class BusinessPartnerService(
private val sharingStateService: SharingStateService,
private val changelogRepository: ChangelogRepository,
private val orchestrationApiClient: OrchestrationApiClient,
private val orchestratorMappings: OrchestratorMappings,
private val sharingStateRepository: SharingStateRepository,
private val poolClient: PoolApiClient,
private val syncRecordService: SyncRecordService,
private val poolConfigProperties: PoolConfigProperties
private val sharingStateRepository: SharingStateRepository
) {
private val logger = KotlinLogging.logger { }

Expand Down Expand Up @@ -103,23 +97,23 @@ class BusinessPartnerService(
val externalIds = entityCandidates.map { it.externalId }
assertInputStageExists(externalIds)

val validatedEntities = filterUpdateCandidates(entityCandidates, StageType.Output)
val changedBusinessPartners = filterUpdateCandidates(entityCandidates, StageType.Output)

val resolutionResults = resolveCandidatesForStage(validatedEntities, StageType.Output)
val resolutionResults = resolveCandidatesForStage(changedBusinessPartners, StageType.Output)

saveChangelog(resolutionResults)

val partners = resolutionResults.map { it.businessPartner }
val changedPartners = resolutionResults.map { it.businessPartner }

val successRequests = partners.map {
val successRequests = entityCandidates.map {
SharingStateService.SuccessRequest(
SharingStateService.SharingStateIdentifierDto(it.externalId, BusinessPartnerType.GENERIC),
it.bpnA!!
)
}
sharingStateService.setSuccess(successRequests)

return businessPartnerRepository.saveAll(partners)
return businessPartnerRepository.saveAll(changedPartners)
}

private fun getBusinessPartners(pageRequest: PageRequest, externalIds: Collection<String>?, stage: StageType): Page<BusinessPartner> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ package org.eclipse.tractusx.bpdm.pool.controller
import org.eclipse.tractusx.bpdm.common.dto.PageDto
import org.eclipse.tractusx.bpdm.common.dto.PaginationRequest
import org.eclipse.tractusx.bpdm.pool.api.PoolSiteApi
import org.eclipse.tractusx.bpdm.pool.api.model.request.*
import org.eclipse.tractusx.bpdm.pool.api.model.request.SiteBpnSearchRequest
import org.eclipse.tractusx.bpdm.pool.api.model.request.SitePartnerCreateRequest
import org.eclipse.tractusx.bpdm.pool.api.model.request.SitePartnerUpdateRequest
import org.eclipse.tractusx.bpdm.pool.api.model.response.*
import org.eclipse.tractusx.bpdm.pool.service.AddressService
import org.eclipse.tractusx.bpdm.pool.service.BusinessPartnerBuildService
Expand Down Expand Up @@ -65,7 +67,7 @@ class SiteController(
override fun createSite(
requests: Collection<SitePartnerCreateRequest>
): SitePartnerCreateResponseWrapper {
return businessPartnerBuildService.createSites(requests)
return businessPartnerBuildService.createSitesWithMainAddress(requests)
}

@PreAuthorize("hasAuthority(@poolSecurityConfigProperties.getChangePoolPartnerDataAsRole())")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ import org.eclipse.tractusx.bpdm.common.dto.*
import org.eclipse.tractusx.bpdm.common.exception.BpdmNotFoundException
import org.eclipse.tractusx.bpdm.common.util.replace
import org.eclipse.tractusx.bpdm.pool.api.model.ChangelogType
import org.eclipse.tractusx.bpdm.pool.api.model.ConfidenceCriteriaDto
import org.eclipse.tractusx.bpdm.pool.api.model.LogisticAddressDto
import org.eclipse.tractusx.bpdm.pool.api.model.SiteStateDto
import org.eclipse.tractusx.bpdm.pool.api.model.request.*
import org.eclipse.tractusx.bpdm.pool.api.model.response.*
import org.eclipse.tractusx.bpdm.pool.dto.AddressMetadataDto
Expand Down Expand Up @@ -52,7 +54,8 @@ class BusinessPartnerBuildService(
private val changelogService: PartnerChangelogService,
private val siteRepository: SiteRepository,
private val logisticAddressRepository: LogisticAddressRepository,
private val requestValidationService: RequestValidationService
private val requestValidationService: RequestValidationService,
private val businessPartnerEquivalenceService: BusinessPartnerEquivalenceService
) {

private val logger = KotlinLogging.logger { }
Expand All @@ -64,7 +67,6 @@ class BusinessPartnerBuildService(
fun createLegalEntities(requests: Collection<LegalEntityPartnerCreateRequest>): LegalEntityPartnerCreateResponseWrapper {
logger.info { "Create ${requests.size} new legal entities" }


val errorsByRequest = requestValidationService.validateLegalEntitiesToCreateFromController(requests)
val errors = errorsByRequest.flatMap { it.value }
val validRequests = requests.filterNot { errorsByRequest.containsKey(it) }
Expand Down Expand Up @@ -100,8 +102,36 @@ class BusinessPartnerBuildService(
return LegalEntityPartnerCreateResponseWrapper(legalEntityResponse, errors)
}

fun createSitesWithLegalAddressAsMain(requests: Collection<SiteCreateRequestWithLegalAddressAsMain>): SitePartnerCreateResponseWrapper {
logger.info { "Create ${requests.size} new sites with legal address as site main address" }

val legalEntities = legalEntityRepository.findDistinctByBpnIn(requests.map { it.bpnLParent })
val legalEntitiesByBpn = legalEntities.associateBy { it.bpn }

val bpnSs = bpnIssuingService.issueSiteBpns(requests.size)

val createdSites = requests.zip(bpnSs).map { (siteRequest, bpnS) ->
val legalEntityParent =
legalEntitiesByBpn[siteRequest.bpnLParent] ?: throw BpdmValidationException("Parent ${siteRequest.bpnLParent} not found for site to create")
createSite(siteRequest, bpnS, legalEntityParent)
.apply { mainAddress = legalEntityParent.legalAddress }
.apply { mainAddress.site = this }
}

changelogService.createChangelogEntries(createdSites.map {
ChangelogEntryCreateRequest(it.bpn, ChangelogType.CREATE, BusinessPartnerType.SITE)
})

siteRepository.saveAll(createdSites)

val siteResponse = createdSites.mapIndexed { index, site -> site.toUpsertDto(index.toString()) }

return SitePartnerCreateResponseWrapper(siteResponse, emptyList())

}

@Transactional
fun createSites(requests: Collection<SitePartnerCreateRequest>): SitePartnerCreateResponseWrapper {
fun createSitesWithMainAddress(requests: Collection<SitePartnerCreateRequest>): SitePartnerCreateResponseWrapper {
logger.info { "Create ${requests.size} new sites" }

val errorsByRequest = requestValidationService.validateSitesToCreateFromController(requests)
Expand Down Expand Up @@ -188,23 +218,37 @@ class BusinessPartnerBuildService(
val bpnsToFetch = validRequests.map { it.bpnl }
val legalEntities = legalEntityRepository.findDistinctByBpnIn(bpnsToFetch)
businessPartnerFetchService.fetchDependenciesWithLegalAddress(legalEntities)

changelogService.createChangelogEntries(legalEntities.map {
ChangelogEntryCreateRequest(it.bpn, ChangelogType.UPDATE, BusinessPartnerType.LEGAL_ENTITY)
})
changelogService.createChangelogEntries(legalEntities.map {
ChangelogEntryCreateRequest(it.legalAddress.bpn, ChangelogType.UPDATE, BusinessPartnerType.ADDRESS)
})

val requestsByBpn = validRequests.associateBy { it.bpnl }
val updatedLegalEntities = legalEntities.map { legalEntity ->
val request = requestsByBpn[legalEntity.bpn]!!
updateLegalEntity(legalEntity, request.legalEntity, request.legalEntity.legalName, legalEntityMetadataMap)
updateLogisticAddress(legalEntity.legalAddress, request.legalAddress, addressMetadataMap)
legalEntityRepository.save(legalEntity)

val legalEntityRequestPairs = legalEntities.map { legalEntity -> Pair(legalEntity, requestsByBpn[legalEntity.bpn]!!) }
legalEntityRequestPairs.forEach { (legalEntity, request) ->
if (!businessPartnerEquivalenceService.isEquivalent(request)) {
updateLegalEntity(legalEntity, request.legalEntity, request.legalEntity.legalName, legalEntityMetadataMap)
updateLogisticAddress(legalEntity.legalAddress, request.legalAddress, addressMetadataMap)
legalEntityRepository.save(legalEntity)

changelogService.createChangelogEntries(
listOf(
ChangelogEntryCreateRequest(
legalEntity.bpn,
ChangelogType.UPDATE,
BusinessPartnerType.LEGAL_ENTITY
)
)
)
changelogService.createChangelogEntries(
listOf(
ChangelogEntryCreateRequest(
legalEntity.legalAddress.bpn,
ChangelogType.UPDATE,
BusinessPartnerType.ADDRESS
)
)
)
}
}

val legalEntityResponses = updatedLegalEntities.map { it.toUpsertDto(it.bpn) }
val legalEntityResponses = legalEntityRequestPairs.map { (legalEntity, request) -> legalEntity.toUpsertDto(request.bpnl) }

return LegalEntityPartnerUpdateResponseWrapper(legalEntityResponses, errors)
}
Expand All @@ -221,23 +265,29 @@ class BusinessPartnerBuildService(

val bpnsToFetch = validRequests.map { it.bpns }
val sites = siteRepository.findDistinctByBpnIn(bpnsToFetch)

changelogService.createChangelogEntries(sites.map {
ChangelogEntryCreateRequest(it.bpn, ChangelogType.UPDATE, BusinessPartnerType.SITE)
})
changelogService.createChangelogEntries(sites.map {
ChangelogEntryCreateRequest(it.mainAddress.bpn, ChangelogType.UPDATE, BusinessPartnerType.ADDRESS)
})

val requestByBpnMap = validRequests.associateBy { it.bpns }
val updatedSites = sites.map {
val request = requestByBpnMap[it.bpn]!!
updateSite(it, request.site)
updateLogisticAddress(it.mainAddress, request.site.mainAddress, addressMetadataMap)
siteRepository.save(it)

val siteRequestPairs = sites.map { site -> Pair(site, requestByBpnMap[site.bpn]!!) }
siteRequestPairs.forEach { (site, request) ->
if (!businessPartnerEquivalenceService.isEquivalent(request)) {
updateSite(site, request.site)
updateLogisticAddress(site.mainAddress, request.site.mainAddress, addressMetadataMap)
siteRepository.save(site)

changelogService.createChangelogEntries(listOf(ChangelogEntryCreateRequest(site.bpn, ChangelogType.UPDATE, BusinessPartnerType.SITE)))
changelogService.createChangelogEntries(
listOf(
ChangelogEntryCreateRequest(
site.mainAddress.bpn,
ChangelogType.UPDATE,
BusinessPartnerType.ADDRESS
)
)
)
}
}

val siteResponses = updatedSites.map { it.toUpsertDto(it.bpn) }
val siteResponses = siteRequestPairs.map { (site, request) -> site.toUpsertDto(request.bpns) }

return SitePartnerUpdateResponseWrapper(siteResponses, errors)
}
Expand All @@ -252,16 +302,17 @@ class BusinessPartnerBuildService(
val addresses = logisticAddressRepository.findDistinctByBpnIn(validRequests.map { it.bpna })
val metadataMap = metadataService.getMetadata(validRequests.map { it.address }).toMapping()

changelogService.createChangelogEntries(addresses.map {
ChangelogEntryCreateRequest(it.bpn, ChangelogType.UPDATE, BusinessPartnerType.ADDRESS)
})
val addressRequestPairs = addresses.sortedBy { it.bpn }.zip(requests.sortedBy { it.bpna })
addressRequestPairs.forEach { (address, request) ->
if (!businessPartnerEquivalenceService.isEquivalent(request)) {
updateLogisticAddress(address, request.address, metadataMap)
logisticAddressRepository.save(address)

val requestsByBpn = validRequests.associateBy { it.bpna }
val updatedAddresses = addresses.map { address ->
updateLogisticAddress(address, requestsByBpn[address.bpn]!!.address, metadataMap)
logisticAddressRepository.save(address)
changelogService.createChangelogEntries(listOf(ChangelogEntryCreateRequest(address.bpn, ChangelogType.UPDATE, BusinessPartnerType.ADDRESS)))
}
}
val addressResponses = updatedAddresses.map { it.toDto() }

val addressResponses = addresses.map { it.toDto() }

return AddressPartnerUpdateResponseWrapper(addressResponses, errors)
}
Expand Down Expand Up @@ -393,6 +444,13 @@ class BusinessPartnerBuildService(
val regions: Map<String, Region>
)

data class SiteCreateRequestWithLegalAddressAsMain(
override val name: String,
override val states: Collection<SiteStateDto>,
override val confidenceCriteria: ConfidenceCriteriaDto,
val bpnLParent: String
) : IBaseSiteDto

companion object {

fun createCurrentnessTimestamp(): Instant {
Expand Down Expand Up @@ -526,6 +584,7 @@ class BusinessPartnerBuildService(
throw BpdmValidationException(TaskStepBuildService.CleaningError.LEGAL_NAME_IS_NULL.message)
}


legalEntity.currentness = createCurrentnessTimestamp()

legalEntity.legalName = Name(value = legalName, shortName = legalEntityDto.legalShortName)
Expand Down Expand Up @@ -606,6 +665,4 @@ class BusinessPartnerBuildService(
)
}



}
Loading
Loading