Skip to content

Commit

Permalink
feat(pool): eclipse-tractusx#432 Upsert Business Partners from Cleani…
Browse files Browse the repository at this point in the history
…ng Result

- create initial poll logic
- refactor legal entity creation validation so that it could be used by orchestrator DTOs
  • Loading branch information
rainer-exxcellent committed Oct 19, 2023
1 parent ac45d67 commit 2e66f7a
Show file tree
Hide file tree
Showing 14 changed files with 407 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import org.eclipse.tractusx.bpdm.common.dto.openapidescription.AddressIdentifier
data class AddressIdentifierDto(

@get:Schema(description = AddressIdentifierDescription.value)
val value: String,
override val value: String,

@get:Schema(description = AddressIdentifierDescription.type)
val type: String,
)
override val type: String,
) : IBaseAddressIdentifierDto
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ import java.time.LocalDateTime
data class AddressStateDto(

@get:Schema(description = AddressStateDescription.description)
val description: String?,
override val description: String?,

@get:Schema(description = AddressStateDescription.validFrom)
val validFrom: LocalDateTime?,
override val validFrom: LocalDateTime?,

@get:Schema(description = AddressStateDescription.validTo)
val validTo: LocalDateTime?,
override val validTo: LocalDateTime?,

@get:Schema(description = AddressStateDescription.type)
val type: BusinessStateType
)
override val type: BusinessStateType
): IBaseAddressStateDto
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ import org.eclipse.tractusx.bpdm.common.model.ClassificationType
data class ClassificationDto(

@get:Schema(description = ClassificationDescription.type)
val type: ClassificationType,
override val type: ClassificationType,

@get:Schema(description = ClassificationDescription.code)
val code: String?,
override val code: String?,

@get:Schema(description = ClassificationDescription.value)
val value: String?
)
override val value: String?
) : IBaseClassificationDto
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,4 @@ interface IBaseLegalEntityDto {
@get:ArraySchema(arraySchema = Schema(description = LegalEntityDescription.classifications, required = false))
val classifications: Collection<IBaseClassificationDto>

// TODO OpenAPI description for complex field does not work!!
@get:Schema(description = LegalEntityDescription.legalAddress)
val legalAddress: IBaseLogisticAddressDto?
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ import org.eclipse.tractusx.bpdm.common.dto.openapidescription.LegalEntityDescri
@Schema(description = LegalEntityDescription.header)
data class LegalEntityDto(
@get:ArraySchema(arraySchema = Schema(description = LegalEntityDescription.identifiers, required = false))
val identifiers: Collection<LegalEntityIdentifierDto> = emptyList(),
override val identifiers: Collection<LegalEntityIdentifierDto> = emptyList(),

@get:Schema(description = LegalEntityDescription.legalShortName)
val legalShortName: String?,
override val legalShortName: String?,

@get:Schema(description = LegalEntityDescription.legalForm)
val legalForm: String? = null,
override val legalForm: String? = null,

@get:ArraySchema(arraySchema = Schema(description = LegalEntityDescription.states))
val states: Collection<LegalEntityStateDto> = emptyList(),
override val states: Collection<LegalEntityStateDto> = emptyList(),

@get:ArraySchema(arraySchema = Schema(description = LegalEntityDescription.classifications, required = false))
val classifications: Collection<ClassificationDto> = emptyList(),
)
override val classifications: Collection<ClassificationDto> = emptyList(),
) : IBaseLegalEntityDto
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,10 @@ package org.eclipse.tractusx.bpdm.common.dto
import io.swagger.v3.oas.annotations.media.Schema
import org.eclipse.tractusx.bpdm.common.dto.openapidescription.LegalEntityIdentifierDescription

@Schema(description = LegalEntityIdentifierDescription.header)
data class LegalEntityIdentifierDto(

@get:Schema(description = LegalEntityIdentifierDescription.value)
val value: String,

@get:Schema(description = LegalEntityIdentifierDescription.type)
val type: String,
data class LegalEntityIdentifierDto(

@get:Schema(description = LegalEntityIdentifierDescription.issuingBody)
val issuingBody: String?
)
override val value: String,
override val type: String,
override val issuingBody: String?
) : IBaseLegalEntityIdentifierDto
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ import java.time.LocalDateTime
data class LegalEntityStateDto(

@get:Schema(description = LegalEntityStateDescription.description)
val description: String?,
override val description: String?,

@get:Schema(description = LegalEntityStateDescription.validFrom)
val validFrom: LocalDateTime?,
override val validFrom: LocalDateTime?,

@get:Schema(description = LegalEntityStateDescription.validTo)
val validTo: LocalDateTime?,
override val validTo: LocalDateTime?,

@get:Schema(description = LegalEntityStateDescription.type)
val type: BusinessStateType
)
override val type: BusinessStateType
) : IBaseLegalEntityStateDto
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,12 @@ data class LogisticAddressDto(
val name: String? = null,

@get:ArraySchema(arraySchema = Schema(description = LogisticAddressDescription.states))
val states: Collection<AddressStateDto> = emptyList(),
override val states: Collection<AddressStateDto> = emptyList(),

@get:ArraySchema(arraySchema = Schema(description = LogisticAddressDescription.identifiers))
val identifiers: Collection<AddressIdentifierDto> = emptyList(),
override val identifiers: Collection<AddressIdentifierDto> = emptyList(),

// TODO OpenAPI description for complex field does not work!!
@get:Schema(description = LogisticAddressDescription.physicalPostalAddress)
val physicalPostalAddress: PhysicalPostalAddressDto,
override val physicalPostalAddress: PhysicalPostalAddressDto,

// TODO OpenAPI description for complex field does not work!!
@get:Schema(description = LogisticAddressDescription.alternativePostalAddress)
val alternativePostalAddress: AlternativePostalAddressDto? = null
)
override val alternativePostalAddress: AlternativePostalAddressDto? = null
) : IBaseLogisticAddressDto
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ data class LegalEntityDto(

override val classifications: Collection<BusinessPartnerClassificationDto> = emptyList(),

override val legalAddress: LogisticAddressDto? = null
val legalAddress: LogisticAddressDto? = null

) : IBaseLegalEntityDto
4 changes: 4 additions & 0 deletions bpdm-pool/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
<groupId>${project.groupId}</groupId>
<artifactId>bpdm-pool-api</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>bpdm-orchestrator-api</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,12 @@ class BusinessPartnerBuildService(
fun createLegalEntities(requests: Collection<LegalEntityPartnerCreateRequest>): LegalEntityPartnerCreateResponseWrapper {
logger.info { "Create ${requests.size} new legal entities" }

val errorsByRequest = requestValidationService.validateLegalEntityCreates(requests)
val errors = errorsByRequest.flatMap { it.value }
val validRequests = requests.filterNot { errorsByRequest.containsKey(it) }

val errorsByRequest = requestValidationService.validateLegalEntityCreatesPool(requests.associateWith { it.legalEntity })
val errorsByRequestAddress = requestValidationService.validateLegalEntityCreatesAddressesPool(requests.associateWith { it.legalAddress })

val errors = errorsByRequest.flatMap { it.value } + errorsByRequestAddress.flatMap { it.value }
val validRequests = requests.filterNot { errorsByRequest.containsKey(it) || errorsByRequestAddress.containsKey(it) }

val legalEntityMetadataMap = metadataService.getMetadata(requests.map { it.legalEntity }).toMapping()
val addressMetadataMap = metadataService.getMetadata(requests.map { it.legalAddress }).toMapping()
Expand Down Expand Up @@ -410,8 +413,8 @@ class BusinessPartnerBuildService(
bpn = bpn,
legalEntity = null,
site = null,
physicalPostalAddress = createPhysicalAddress(dto.physicalPostalAddress, metadataMap),
alternativePostalAddress = dto.alternativePostalAddress?.let { createAlternativeAddress(it, metadataMap) },
physicalPostalAddress = createPhysicalAddress(dto.physicalPostalAddress, metadataMap.regions),
alternativePostalAddress = dto.alternativePostalAddress?.let { createAlternativeAddress(it, metadataMap.regions) },
name = dto.name
)

Expand All @@ -422,24 +425,24 @@ class BusinessPartnerBuildService(

private fun updateLogisticAddress(address: LogisticAddress, dto: LogisticAddressDto, metadataMap: AddressMetadataMapping) {
address.name = dto.name
address.physicalPostalAddress = createPhysicalAddress(dto.physicalPostalAddress, metadataMap)
address.alternativePostalAddress = dto.alternativePostalAddress?.let { createAlternativeAddress(it, metadataMap) }
address.physicalPostalAddress = createPhysicalAddress(dto.physicalPostalAddress, metadataMap.regions)
address.alternativePostalAddress = dto.alternativePostalAddress?.let { createAlternativeAddress(it, metadataMap.regions) }

address.identifiers.apply {
clear()
addAll(dto.identifiers.map { toEntity(it, metadataMap, address) })
addAll(dto.identifiers.map { toEntity(it, metadataMap.idTypes, address) })
}
address.states.apply {
clear()
addAll(dto.states.map { toEntity(it, address) })
}
}

private fun createPhysicalAddress(physicalAddress: PhysicalPostalAddressDto, metadataMap: AddressMetadataMapping): PhysicalPostalAddress {
private fun createPhysicalAddress(physicalAddress: PhysicalPostalAddressDto, regions: Map<String, Region>): PhysicalPostalAddress {
return PhysicalPostalAddress(
geographicCoordinates = physicalAddress.geographicCoordinates?.let { toEntity(it) },
country = physicalAddress.country,
administrativeAreaLevel1 = metadataMap.regions[physicalAddress.administrativeAreaLevel1],
administrativeAreaLevel1 = regions[physicalAddress.administrativeAreaLevel1],
administrativeAreaLevel2 = physicalAddress.administrativeAreaLevel2,
administrativeAreaLevel3 = physicalAddress.administrativeAreaLevel3,
postCode = physicalAddress.postalCode,
Expand All @@ -454,11 +457,11 @@ class BusinessPartnerBuildService(
)
}

private fun createAlternativeAddress(alternativeAddress: AlternativePostalAddressDto, metadataMap: AddressMetadataMapping): AlternativePostalAddress {
private fun createAlternativeAddress(alternativeAddress: AlternativePostalAddressDto, regions: Map<String, Region>): AlternativePostalAddress {
return AlternativePostalAddress(
geographicCoordinates = alternativeAddress.geographicCoordinates?.let { toEntity(it) },
country = alternativeAddress.country,
administrativeAreaLevel1 = metadataMap.regions[alternativeAddress.administrativeAreaLevel1],
administrativeAreaLevel1 = regions[alternativeAddress.administrativeAreaLevel1],
postCode = alternativeAddress.postalCode,
city = alternativeAddress.city,
deliveryServiceType = alternativeAddress.deliveryServiceType,
Expand Down Expand Up @@ -530,12 +533,12 @@ class BusinessPartnerBuildService(

private fun toEntity(
dto: AddressIdentifierDto,
metadataMap: AddressMetadataMapping,
idTypes: Map<String, IdentifierType>,
partner: LogisticAddress
): AddressIdentifier {
return AddressIdentifier(
value = dto.value,
type = metadataMap.idTypes[dto.type]!!,
type = idTypes[dto.type]!!,
address = partner
)
}
Expand All @@ -561,13 +564,14 @@ class BusinessPartnerBuildService(
)


private data class LegalEntityMetadataMapping(
data class LegalEntityMetadataMapping(
val idTypes: Map<String, IdentifierType>,
val legalForms: Map<String, LegalForm>
)

private data class AddressMetadataMapping(
data class AddressMetadataMapping(
val idTypes: Map<String, IdentifierType>,
val regions: Map<String, Region>
)

}
Loading

0 comments on commit 2e66f7a

Please sign in to comment.