Skip to content

Commit

Permalink
feat(pool): eclipse-tractusx#658 validate duplicates over all address…
Browse files Browse the repository at this point in the history
…es and all legal entities
  • Loading branch information
rainer-exxcellent committed Dec 8, 2023
1 parent 37e1637 commit 36ba486
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import org.eclipse.tractusx.bpdm.pool.api.model.response.*
import org.eclipse.tractusx.bpdm.pool.dto.LegalEntityMetadataDto
import org.eclipse.tractusx.bpdm.pool.repository.*
import org.eclipse.tractusx.orchestrator.api.model.BpnReferenceType
import org.eclipse.tractusx.orchestrator.api.model.BusinessPartnerFullDto
import org.eclipse.tractusx.orchestrator.api.model.TaskStepReservationEntryDto
import org.eclipse.tractusx.orchestrator.api.model.TaskStepResultEntryDto
import org.springframework.stereotype.Service

@Service
Expand All @@ -48,48 +48,38 @@ class RequestValidationService(
fun validateTasks( taskEntries: List<TaskStepReservationEntryDto>, requestMappings: TaskEntryBpnMapping
): Map<RequestWithKey, Collection<ErrorInfo<ErrorCode>>> {

val legalEntitiesToCreate = taskEntries
.filter {
it.businessPartner.legalEntity?.bpnLReference?.referenceType == BpnReferenceType.BpnRequestIdentifier
&& !(requestMappings.hasBpnFor(it.businessPartner.legalEntity?.bpnLReference))
}
val legalEntitiesToUpdate = taskEntries
.filter {
it.businessPartner.legalEntity?.bpnLReference?.referenceType == BpnReferenceType.Bpn
|| (it.businessPartner.legalEntity?.bpnLReference?.referenceType == BpnReferenceType.BpnRequestIdentifier
&& requestMappings.hasBpnFor(it.businessPartner.legalEntity?.bpnLReference))
}
val sitesToCreate = taskEntries
.filter {
it.businessPartner.site?.bpnSReference?.referenceType == BpnReferenceType.BpnRequestIdentifier
&& !(requestMappings.hasBpnFor(it.businessPartner.site?.bpnSReference))
}
val sitesToUpdate = taskEntries
.filter {
it.businessPartner.site?.bpnSReference?.referenceType == BpnReferenceType.Bpn
|| (it.businessPartner.site?.bpnSReference?.referenceType == BpnReferenceType.BpnRequestIdentifier
&& requestMappings.hasBpnFor(it.businessPartner.site?.bpnSReference))
}

return validateLegalEntitiesFromOrchestrator(taskEntries, requestMappings) +
validateLegalEntitiesToCreateFromOrchestrator(legalEntitiesToCreate) +
validateLegalEntitiesToUpdateFromOrchestrator(legalEntitiesToUpdate, requestMappings) +
validateSitesToCreateFromOrchestrator(sitesToCreate) +
validateSitesToUpdateFromOrchestrator(sitesToUpdate, requestMappings)
validateSitesFromOrchestrator(taskEntries, requestMappings) +
validateAddressesFromOrchestrator(taskEntries, requestMappings)
}

fun validateLegalEntitiesFromOrchestrator(taskEntries: List<TaskStepReservationEntryDto>, requestMappings: TaskEntryBpnMapping): Map<RequestWithKey, Collection<ErrorInfo<ErrorCode>>> {
private fun isSiteUpdate( businessPartner: BusinessPartnerFullDto, requestMappings: TaskEntryBpnMapping)
= (businessPartner.site?.bpnSReference?.referenceType == BpnReferenceType.Bpn
|| (businessPartner.site?.bpnSReference?.referenceType == BpnReferenceType.BpnRequestIdentifier
&& requestMappings.hasBpnFor(businessPartner.site?.bpnSReference)))

private fun isSiteCreate( businessPartner: BusinessPartnerFullDto, requestMappings: TaskEntryBpnMapping)
= (businessPartner.site?.bpnSReference?.referenceType == BpnReferenceType.BpnRequestIdentifier
&& !(requestMappings.hasBpnFor(businessPartner.site?.bpnSReference)))

private fun isLegalEntityUpdate(businessPartner: BusinessPartnerFullDto, requestMappings: TaskEntryBpnMapping )
= (businessPartner.legalEntity?.bpnLReference?.referenceType == BpnReferenceType.Bpn
|| (businessPartner.legalEntity?.bpnLReference?.referenceType == BpnReferenceType.BpnRequestIdentifier
&& requestMappings.hasBpnFor(businessPartner.legalEntity?.bpnLReference)))

private fun isLegalEntityCreate(businessPartner: BusinessPartnerFullDto, requestMappings: TaskEntryBpnMapping)
= (businessPartner.legalEntity?.bpnLReference?.referenceType == BpnReferenceType.BpnRequestIdentifier
&& !(requestMappings.hasBpnFor(businessPartner.legalEntity?.bpnLReference)))

private fun validateLegalEntitiesFromOrchestrator(taskEntries: List<TaskStepReservationEntryDto>, requestMappings: TaskEntryBpnMapping): Map<RequestWithKey, Collection<ErrorInfo<ErrorCode>>> {

val legalEntitiesToCreate = taskEntries
.filter {
it.businessPartner.legalEntity?.bpnLReference?.referenceType == BpnReferenceType.BpnRequestIdentifier
&& !(requestMappings.hasBpnFor(it.businessPartner.legalEntity?.bpnLReference))
isLegalEntityCreate(it.businessPartner, requestMappings)
}
val legalEntitiesToUpdate = taskEntries
.filter {
it.businessPartner.legalEntity?.bpnLReference?.referenceType == BpnReferenceType.Bpn
|| (it.businessPartner.legalEntity?.bpnLReference?.referenceType == BpnReferenceType.BpnRequestIdentifier
&& requestMappings.hasBpnFor(it.businessPartner.legalEntity?.bpnLReference))
isLegalEntityUpdate(it.businessPartner, requestMappings)
}

val (requestsWithLegalAddressNull, requestsWithLegalAddressNotNull) = legalEntitiesToCreate.partition { it.businessPartner.legalEntity?.legalAddress == null }
Expand All @@ -109,118 +99,130 @@ class RequestValidationService(
val leCreateErrorsByRequest = validateLegalEntitiesToCreate(createBridges)
val leUpdateErrorsByRequest = validateLegalEntitiesToUpdate(updateBridges)
val duplicateValidations = validateLegalEntityDuplicates(createBridges+updateBridges)
return mergeMapsWithCollectionInValue(leCreateErrorsByRequest, leUpdateErrorsByRequest, duplicateValidations, legalAddressEmptyErrorsByRequest)
val result = mergeMapsWithCollectionInValue(leCreateErrorsByRequest, leUpdateErrorsByRequest, duplicateValidations, legalAddressEmptyErrorsByRequest)

return result.map {
it.key to it.value.distinct()
}.toMap()
}

fun validateLegalEntityDuplicates(requestBridges: List<LegalEntityBridge>): Map<RequestWithKey, Collection<ErrorInfo<ErrorCode>>> {
private fun validateSitesFromOrchestrator(taskEntries: List<TaskStepReservationEntryDto>, requestMappings: TaskEntryBpnMapping): Map<RequestWithKey, Collection<ErrorInfo<ErrorCode>>> {

val legalEntityDtos = requestBridges.map { it.legalEntity }
val duplicatesValidator = ValidateLegalEntityIdentifiersDuplicated(legalEntityDtos, LegalEntityCreateError.LegalEntityDuplicateIdentifier)
val sitesToUpdate = taskEntries
.filter {
isSiteUpdate(it.businessPartner, requestMappings)
}

return requestBridges.associate {
it.request to duplicatesValidator.validate(it.legalEntity, it.request, it.bpnL)
}.filterValues { it.isNotEmpty() }
}
val siteErrorsByRequest = validateSitesToUpdate(sitesToUpdate.map {
SiteUpdateBridge( request = it, bpnS = requestMappings.getBpn(it.businessPartner.site?.bpnSReference!!)!! )
})

fun validateLegalEntitiesToCreateFromOrchestrator(taskReservationRequests: List<TaskStepReservationEntryDto>): Map<RequestWithKey, Collection<ErrorInfo<LegalEntityCreateError>>> {
return siteErrorsByRequest
}

val (requestsWithLegalAddressNull, requestsWithLegalAddressNotNull) = taskReservationRequests.partition { it.businessPartner.legalEntity?.legalAddress == null }
private fun validateAddressesFromOrchestrator(taskEntries: List<TaskStepReservationEntryDto>, requestMappings: TaskEntryBpnMapping): Map<RequestWithKey, Collection<ErrorInfo<ErrorCode>>> {

val legalAddressBridges = requestsWithLegalAddressNotNull
.map { AddressBridge(address = it.businessPartner.legalEntity?.legalAddress!!, request = it, bpnA = null) }
val additionalAddressBridges = requestsWithLegalAddressNotNull.filter { it.businessPartner.site == null && it.businessPartner.address != null }
.map { AddressBridge(address = it.businessPartner.address!!, request = it, bpnA = null) }
val taskWithLegalAddress = taskEntries
.filter{ it.businessPartner.legalEntity?.legalAddress != null}

val legalAddressEmptyErrorsByRequest = requestsWithLegalAddressNull.associateWith {
listOf(ErrorInfo(LegalEntityCreateError.LegalAddressIdentifierNotFound, "legal address of legal entity is Empty", it.getRequestKey()))
}
val leCreateAddresses = taskWithLegalAddress.flatMap{getAddressCreateBridgesFromLegalEntity(it, requestMappings)}
val leUpdateAddresses = taskWithLegalAddress.flatMap{getAddressUpdateBridgesFromLegalEntity(it, requestMappings)}
val siteCreateAddresses = taskWithLegalAddress.flatMap{getAddressCreateBridgesFromSite(it, requestMappings)}
val siteUpdateAddresses = taskWithLegalAddress.flatMap{getAddressUpdateBridgesFromSite(it, requestMappings)}

val addressErrorsByRequest = validateAddresses(legalAddressBridges + additionalAddressBridges, leCreateMessages)
val leErrorsByRequest = validateLegalEntitiesToCreate(requestsWithLegalAddressNotNull.map {
LegalEntityBridge(legalEntity = it.businessPartner.legalEntity!!, request = it, bpnL = null)
})
return mergeMapsWithCollectionInValue(leErrorsByRequest, addressErrorsByRequest, legalAddressEmptyErrorsByRequest)
return mergeMapsWithCollectionInValue (validateAddresses(leCreateAddresses, leCreateMessages),
validateAddresses(leUpdateAddresses, leUpdateMessages),
validateAddresses(siteCreateAddresses, siteCreateMessages),
validateAddresses(siteUpdateAddresses, siteUpdateMessages),
)
}

fun validateLegalEntitiesToUpdateFromOrchestrator(taskReservationRequests: List<TaskStepReservationEntryDto>,
requestMappings: TaskEntryBpnMapping): Map<RequestWithKey, Collection<ErrorInfo<ErrorCode>>> {
fun getAddressCreateBridgesFromLegalEntity(task: TaskStepReservationEntryDto, requestMappings: TaskEntryBpnMapping): List<AddressBridge> {

val legalAddressBridges = taskReservationRequests
.map {
AddressBridge(
address = it.businessPartner.legalEntity?.legalAddress!!,
request = it,
bpnA = requestMappings.getBpn(it.businessPartner.legalEntity?.legalAddress?.bpnAReference!!)
)
val allAddresses: MutableList<AddressBridge> = mutableListOf()
val businessPartner = task.businessPartner
if ( isLegalEntityCreate(businessPartner, requestMappings)) {
allAddresses.add(AddressBridge(address = businessPartner.legalEntity?.legalAddress!!, request = task, bpnA = null))
if ( businessPartner.site == null && businessPartner.address != null) {
allAddresses.add( AddressBridge(address = businessPartner.address!!, request = task, bpnA = null))
}
}
return allAddresses
}

val additionalAddressBridges = taskReservationRequests.filter { it.businessPartner.site == null && it.businessPartner.address != null }
.map {
AddressBridge(
address = it.businessPartner.address!!,
request = it,
bpnA = requestMappings.getBpn(it.businessPartner.address?.bpnAReference!!),
)
fun getAddressUpdateBridgesFromLegalEntity(task: TaskStepReservationEntryDto, requestMappings: TaskEntryBpnMapping): List<AddressBridge> {

val allAddresses: MutableList<AddressBridge> = mutableListOf()
val businessPartner = task.businessPartner
if ( isLegalEntityUpdate(businessPartner, requestMappings)) {
allAddresses.add(AddressBridge(
address = businessPartner.legalEntity?.legalAddress!!, request = task,
bpnA = requestMappings.getBpn(businessPartner.legalEntity?.legalAddress?.bpnAReference!!)
))
if ( businessPartner.site == null && businessPartner.address != null) {
allAddresses.add( AddressBridge(
address = businessPartner.address!!,
request = task,
bpnA = requestMappings.getBpn(businessPartner.address?.bpnAReference!!),
))
}

val leErrorsByRequest = validateLegalEntitiesToUpdate(taskReservationRequests.map {
LegalEntityBridge(
legalEntity = it.businessPartner.legalEntity!!,
request = it,
bpnL = requestMappings.getBpn(it.businessPartner.legalEntity?.bpnLReference!!)!!
)
})
val addressErrorsByRequest = validateAddresses(legalAddressBridges + additionalAddressBridges, leUpdateMessages)
return mergeMapsWithCollectionInValue(leErrorsByRequest, addressErrorsByRequest)
}
return allAddresses
}

fun validateSitesToCreateFromOrchestrator(taskReservationRequests: List<TaskStepReservationEntryDto>): Map<RequestWithKey, Collection<ErrorInfo<SiteCreateError>>> {

// parents have to be checked in the legal entity validation
val (requestsWithMainAddressNull, requestsWithMainAddressNotNull) = taskReservationRequests.partition { it.businessPartner.site?.mainAddress == null }
fun getAddressCreateBridgesFromSite(task: TaskStepReservationEntryDto, requestMappings: TaskEntryBpnMapping): List<AddressBridge> {

val siteMainAddressBridges = requestsWithMainAddressNotNull
.map {
AddressBridge(address = it.businessPartner.site?.mainAddress!!, request = it, bpnA = null)
}
val additionalAddressBridges = requestsWithMainAddressNotNull
.filter { it.businessPartner.site != null && it.businessPartner.address != null }
.map {
AddressBridge(address = it.businessPartner.address!!, request = it, bpnA = null)
val allAddresses: MutableList<AddressBridge> = mutableListOf()
val businessPartner = task.businessPartner
if ( isSiteCreate(businessPartner, requestMappings)) {
allAddresses.add(AddressBridge(address = businessPartner.site?.mainAddress!!, request = task, bpnA = null))
if ( businessPartner.site != null && businessPartner.address != null) {
allAddresses.add( AddressBridge(address = businessPartner.address!!, request = task, bpnA = null))
}
}
return allAddresses
}

val mainAddressEmptyErrorsByRequest = requestsWithMainAddressNull.associateWith {
listOf(ErrorInfo(SiteCreateError.MainAddressIdentifierNotFound, "Site main address is Empty", it.getRequestKey()))
fun getAddressUpdateBridgesFromSite(task: TaskStepReservationEntryDto, requestMappings: TaskEntryBpnMapping): List<AddressBridge> {

val allAddresses: MutableList<AddressBridge> = mutableListOf()
val businessPartner = task.businessPartner
if (isSiteUpdate(businessPartner, requestMappings)) {
allAddresses.add(AddressBridge(
address = businessPartner.site?.mainAddress!!,
request = task,
bpnA = requestMappings.getBpn(businessPartner.site?.mainAddress?.bpnAReference!!)
))
if ( businessPartner.site != null && businessPartner.address != null) {
allAddresses.add( AddressBridge(
address = businessPartner.address!!,
request = task,
bpnA = requestMappings.getBpn(businessPartner.address?.bpnAReference!!),
))
}
}
val addressErrorsByRequest = validateAddresses(siteMainAddressBridges + additionalAddressBridges, siteCreateMessages)
return mergeMapsWithCollectionInValue(addressErrorsByRequest, mainAddressEmptyErrorsByRequest)
return allAddresses
}

fun validateSitesToUpdateFromOrchestrator(taskReservationRequests: List<TaskStepReservationEntryDto>
, requestMappings: TaskEntryBpnMapping): Map<RequestWithKey, Collection<ErrorInfo<SiteUpdateError>>> {

val siteMainAddressBridges = taskReservationRequests.map {
AddressBridge(
address = it.businessPartner.site?.mainAddress!!,
request = it,
bpnA = requestMappings.getBpn(it.businessPartner.site?.mainAddress?.bpnAReference!!)
)
}
val additionalAddressBridges = taskReservationRequests.filter { it.businessPartner.site != null && it.businessPartner.address != null }
.map {
AddressBridge(
address = it.businessPartner.address!!,
request = it,
bpnA = requestMappings.getBpn(it.businessPartner.address?.bpnAReference!!),
)
}
fun validateLegalEntityDuplicates(requestBridges: List<LegalEntityBridge>): Map<RequestWithKey, Collection<ErrorInfo<ErrorCode>>> {

val siteErrorsByRequest = validateSitesToUpdate(taskReservationRequests.map {
SiteUpdateBridge( request = it, bpnS = requestMappings.getBpn(it.businessPartner.site?.bpnSReference!!)!! )
})
val addressErrorsByRequest = validateAddresses(siteMainAddressBridges + additionalAddressBridges, siteUpdateMessages)
val legalEntityDtos = requestBridges.map { it.legalEntity }
val duplicatesValidator = ValidateLegalEntityIdentifiersDuplicated(legalEntityDtos, LegalEntityCreateError.LegalEntityDuplicateIdentifier)

return mergeMapsWithCollectionInValue(siteErrorsByRequest, addressErrorsByRequest)
return requestBridges.associate {
it.request to duplicatesValidator.validate(it.legalEntity, it.request, it.bpnL)
}.filterValues { it.isNotEmpty() }
}

fun validateAddressDuplicates(requestBridges: List<AddressBridge>): Map<RequestWithKey, Collection<ErrorInfo<ErrorCode>>> {

val addressDtos = requestBridges.map { it.address }
val duplicatesValidator = ValidateAddressIdentifiersDuplicated(addressDtos, LegalEntityCreateError.LegalAddressDuplicateIdentifier)

return requestBridges.associate {
it.request to duplicatesValidator.validate(it.address, it.request, it.bpnA)
}.filterValues { it.isNotEmpty() }
}

fun validateLegalEntitiesToCreateFromController(leCreateRequests: Collection<LegalEntityPartnerCreateRequest>): Map<RequestWithKey, Collection<ErrorInfo<LegalEntityCreateError>>> {
Expand Down Expand Up @@ -316,15 +318,15 @@ class RequestValidationService(
identifiersDuplicateValidator.validate(legalAddressDto, request, bridge.bpnA)

if(validationErrors.isNotEmpty()) {
val existing = result[request];
val existing = result[request]
if (existing == null) {
result[request] = validationErrors
} else {
result[request] = existing + validationErrors
}
}
}
return result;
return result
}

fun validateSitesToCreateFromController(
Expand Down
Loading

0 comments on commit 36ba486

Please sign in to comment.