Skip to content

Commit

Permalink
refactor(DTO): Unify postal address for normal/verbose model
Browse files Browse the repository at this point in the history
  • Loading branch information
martinfkaeser committed Dec 6, 2023
1 parent 8634d5e commit 5dc9687
Show file tree
Hide file tree
Showing 18 changed files with 91 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,10 @@ private fun poolToGatePhysicalAddress(address: PhysicalPostalAddressVerboseDto):
}
return Gate_PhysicalPostalAddressDto(
geographicCoordinates = address.geographicCoordinates,
country = address.country.technicalKey,
country = address.country,
postalCode = address.postalCode,
city = address.city,
administrativeAreaLevel1 = address.administrativeAreaLevel1?.regionCode,
administrativeAreaLevel1 = address.administrativeAreaLevel1,
administrativeAreaLevel2 = address.administrativeAreaLevel2,
administrativeAreaLevel3 = address.administrativeAreaLevel3,
district = address.district,
Expand All @@ -274,10 +274,10 @@ private fun poolToGatePhysicalAddress(address: PhysicalPostalAddressVerboseDto):
private fun poolToGateAlternativeAddress(address: AlternativePostalAddressVerboseDto): org.eclipse.tractusx.bpdm.gate.api.model.AlternativePostalAddressDto {
return org.eclipse.tractusx.bpdm.gate.api.model.AlternativePostalAddressDto(
geographicCoordinates = address.geographicCoordinates,
country = address.country.technicalKey,
country = address.country,
postalCode = address.postalCode,
city = address.city,
administrativeAreaLevel1 = address.administrativeAreaLevel1?.regionCode,
administrativeAreaLevel1 = address.administrativeAreaLevel1,
deliveryServiceNumber = address.deliveryServiceNumber,
deliveryServiceType = address.deliveryServiceType,
deliveryServiceQualifier = address.deliveryServiceQualifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ interface IBaseAlternativePostalAddressDto {
val geographicCoordinates: GeoCoordinateDto?

@get:Schema(description = PostalAddressDescription.country)
val country: Any?
val country: CountryCode?

@get:Schema(description = PostalAddressDescription.administrativeAreaLevel1)
val administrativeAreaLevel1: Any?
val administrativeAreaLevel1: String?

@get:Schema(description = PostalAddressDescription.postalCode)
val postalCode: String?
Expand All @@ -52,8 +52,4 @@ interface IBaseAlternativePostalAddressDto {

@get:Schema(description = PostalAddressDescription.deliveryServiceNumber)
val deliveryServiceNumber: String?

fun adminLevel1Key(): String?

fun countryCode(): CountryCode?
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ interface IBasePhysicalPostalAddressDto {
val geographicCoordinates: GeoCoordinateDto?

@get:Schema(description = PostalAddressDescription.country)
val country: Any?
val country: CountryCode?

@get:Schema(description = PostalAddressDescription.administrativeAreaLevel1)
val administrativeAreaLevel1: Any?
val administrativeAreaLevel1: String?

@get:Schema(description = PostalAddressDescription.administrativeAreaLevel2)
val administrativeAreaLevel2: String?
Expand Down Expand Up @@ -69,8 +69,4 @@ interface IBasePhysicalPostalAddressDto {

@get:Schema(description = PostalAddressDescription.door)
val door: String?

fun adminLevel1Key(): String?

fun countryCode(): CountryCode?
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.eclipse.tractusx.bpdm.common.service

import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.annotation.JsonUnwrapped
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.*
Expand Down Expand Up @@ -58,15 +59,17 @@ private class DataClassUnwrappedJsonDeserializerForType(destinationJavaType: Jav
this.primaryConstructor = destinationClass.primaryConstructor
?: throw IllegalStateException("Primary constructor required for '$destinationClass'")

// Annotation @field:JsonUnwrapped is stored on the Java field, not the constructor parameter.
// Annotations @field:JsonUnwrapped and @field:JsonProperty are stored on the Java field, not the constructor parameter.
val propertiesByName = destinationClass.memberProperties.associateBy { it.name }

this.constructorParameters = primaryConstructor.parameters.map { param ->
val name = param.name
?: throw IllegalStateException("Some primary constructor parameter of '$destinationClass' doesn't have a name")
val type = param.type
val jsonUnwrapped = propertiesByName[name]?.javaField?.getAnnotation(JsonUnwrapped::class.java) != null
ConstructorParameter(name, type, jsonUnwrapped)
val altName = propertiesByName[name]?.javaField?.getAnnotation(JsonProperty::class.java)?.value
val finalName = if (altName.isNullOrEmpty()) name else altName
ConstructorParameter(finalName, type, jsonUnwrapped)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,4 @@ data class AlternativePostalAddressDto(
override val deliveryServiceQualifier: String? = null,
override val deliveryServiceNumber: String? = null

) : IBaseAlternativePostalAddressDto {
override fun adminLevel1Key(): String? {
return administrativeAreaLevel1
}

override fun countryCode(): CountryCode? {
return country
}
}
) : IBaseAlternativePostalAddressDto
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,4 @@ data class PhysicalPostalAddressDto(
override val floor: String? = null,
override val door: String? = null

) : IBasePhysicalPostalAddressDto {
override fun adminLevel1Key(): String? {
return administrativeAreaLevel1
}

override fun countryCode(): CountryCode? {
return country
}
}
) : IBasePhysicalPostalAddressDto
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import org.eclipse.tractusx.bpdm.common.dto.IBaseAlternativePostalAddressDto
import org.eclipse.tractusx.bpdm.common.model.DeliveryServiceType

data class AlternativePostalAddressDto(

override val geographicCoordinates: GeoCoordinateDto? = null,
override val country: CountryCode? = null,
override val administrativeAreaLevel1: String? = null,
Expand All @@ -34,11 +35,4 @@ data class AlternativePostalAddressDto(
override val deliveryServiceQualifier: String? = null,
override val deliveryServiceNumber: String? = null

) : IBaseAlternativePostalAddressDto {
override fun adminLevel1Key(): String? {
return administrativeAreaLevel1
}
override fun countryCode(): CountryCode? {
return country
}
}
) : IBaseAlternativePostalAddressDto
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import org.eclipse.tractusx.bpdm.common.dto.GeoCoordinateDto
import org.eclipse.tractusx.bpdm.common.dto.IBasePhysicalPostalAddressDto

data class PhysicalPostalAddressDto(

override val geographicCoordinates: GeoCoordinateDto? = null,
override val country: CountryCode? = null,
override val administrativeAreaLevel1: String? = null,
Expand All @@ -39,12 +40,4 @@ data class PhysicalPostalAddressDto(
override val floor: String? = null,
override val door: String? = null

) : IBasePhysicalPostalAddressDto {
override fun adminLevel1Key(): String? {
return administrativeAreaLevel1
}

override fun countryCode(): CountryCode? {
return country
}
}
) : IBasePhysicalPostalAddressDto
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,4 @@ data class AlternativePostalAddressDto(
override val deliveryServiceQualifier: String?,
override val deliveryServiceNumber: String

) : IBaseAlternativePostalAddressDto {
override fun adminLevel1Key(): String? {
return administrativeAreaLevel1
}

override fun countryCode(): CountryCode {
return country
}
}
) : IBaseAlternativePostalAddressDto
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package org.eclipse.tractusx.bpdm.pool.api.model

import com.fasterxml.jackson.annotation.JsonIgnore
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.neovisionaries.i18n.CountryCode
import io.swagger.v3.oas.annotations.media.Schema
Expand All @@ -34,20 +36,28 @@ import org.eclipse.tractusx.bpdm.common.service.DataClassUnwrappedJsonDeserializ
data class AlternativePostalAddressVerboseDto(

override val geographicCoordinates: GeoCoordinateDto?,
override val country: TypeKeyNameVerboseDto<CountryCode>,
override val administrativeAreaLevel1: RegionDto?,

@field:JsonProperty("country")
@get:Schema(description = PostalAddressDescription.country)
val countryVerbose: TypeKeyNameVerboseDto<CountryCode>,

@field:JsonProperty("administrativeAreaLevel1")
@get:Schema(description = PostalAddressDescription.administrativeAreaLevel1)
val administrativeAreaLevel1Verbose: RegionDto?,

override val postalCode: String?,
override val city: String,
override val deliveryServiceType: DeliveryServiceType,
override val deliveryServiceQualifier: String?,
override val deliveryServiceNumber: String

) : IBaseAlternativePostalAddressDto {
override fun adminLevel1Key(): String? {
return administrativeAreaLevel1?.regionCode
}
override fun countryCode(): CountryCode {
return country.technicalKey
}
}

@get:JsonIgnore
override val country: CountryCode
get() = countryVerbose.technicalKey

@get:JsonIgnore
override val administrativeAreaLevel1: String?
get() = administrativeAreaLevel1Verbose?.regionCode
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,4 @@ data class PhysicalPostalAddressDto(
override val floor: String?,
override val door: String?

) : IBasePhysicalPostalAddressDto {
override fun adminLevel1Key(): String? {
return administrativeAreaLevel1
}

override fun countryCode(): CountryCode {
return country
}
}
) : IBasePhysicalPostalAddressDto
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package org.eclipse.tractusx.bpdm.pool.api.model

import com.fasterxml.jackson.annotation.JsonIgnore
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.neovisionaries.i18n.CountryCode
import io.swagger.v3.oas.annotations.media.Schema
Expand All @@ -33,8 +35,15 @@ import org.eclipse.tractusx.bpdm.common.service.DataClassUnwrappedJsonDeserializ
data class PhysicalPostalAddressVerboseDto(

override val geographicCoordinates: GeoCoordinateDto?,
override val country: TypeKeyNameVerboseDto<CountryCode>,
override val administrativeAreaLevel1: RegionDto?,

@field:JsonProperty("country")
@get:Schema(description = PostalAddressDescription.country)
val countryVerbose: TypeKeyNameVerboseDto<CountryCode>,

@field:JsonProperty("administrativeAreaLevel1")
@get:Schema(description = PostalAddressDescription.administrativeAreaLevel1)
val administrativeAreaLevel1Verbose: RegionDto?,

override val administrativeAreaLevel2: String?,
override val administrativeAreaLevel3: String?,
override val postalCode: String?,
Expand All @@ -48,11 +57,12 @@ data class PhysicalPostalAddressVerboseDto(
override val door: String?

) : IBasePhysicalPostalAddressDto {
override fun adminLevel1Key(): String? {
return administrativeAreaLevel1?.regionCode
}

override fun countryCode(): CountryCode {
return country.technicalKey
}
@get:JsonIgnore
override val country: CountryCode
get() = countryVerbose.technicalKey

@get:JsonIgnore
override val administrativeAreaLevel1: String?
get() = administrativeAreaLevel1Verbose?.regionCode
}
Original file line number Diff line number Diff line change
Expand Up @@ -538,13 +538,13 @@ class BusinessPartnerBuildService(

fun createPhysicalAddress(physicalAddress: IBasePhysicalPostalAddressDto, regions: Map<String, Region>): PhysicalPostalAddress {

if (physicalAddress.countryCode() == null || physicalAddress.city == null) {
if (physicalAddress.country == null || physicalAddress.city == null) {
throw BpdmValidationException(TaskStepBuildService.CleaningError.COUNTRY_CITY_IS_NULL.message)
}

return PhysicalPostalAddress(
geographicCoordinates = physicalAddress.geographicCoordinates?.let { GeographicCoordinate(it.latitude, it.longitude, it.altitude) },
country = physicalAddress.countryCode()!!,
country = physicalAddress.country!!,
administrativeAreaLevel1 = regions[physicalAddress.administrativeAreaLevel1],
administrativeAreaLevel2 = physicalAddress.administrativeAreaLevel2,
administrativeAreaLevel3 = physicalAddress.administrativeAreaLevel3,
Expand All @@ -569,7 +569,7 @@ class BusinessPartnerBuildService(

fun createAlternativeAddress(alternativeAddress: IBaseAlternativePostalAddressDto, regions: Map<String, Region>): AlternativePostalAddress {

if (alternativeAddress.countryCode() == null || alternativeAddress.city == null ||
if (alternativeAddress.country == null || alternativeAddress.city == null ||
alternativeAddress.deliveryServiceType == null || alternativeAddress.deliveryServiceNumber == null
) {

Expand All @@ -578,7 +578,7 @@ class BusinessPartnerBuildService(

return AlternativePostalAddress(
geographicCoordinates = alternativeAddress.geographicCoordinates?.let { GeographicCoordinate(it.latitude, it.longitude, it.altitude) },
country = alternativeAddress.countryCode()!!,
country = alternativeAddress.country!!,
administrativeAreaLevel1 = regions[alternativeAddress.administrativeAreaLevel1],
postCode = alternativeAddress.postalCode,
city = alternativeAddress.city!!,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ class MetadataService(
val idTypeKeys = requests.flatMap { it.identifiers }.map { it.type }.toSet()
val idTypes = identifierTypeRepository.findByBusinessPartnerTypeAndTechnicalKeyIn(IdentifierBusinessPartnerType.ADDRESS, idTypeKeys)

val regionKeys = requests.mapNotNull { it.physicalPostalAddress?.adminLevel1Key() }
.plus(requests.mapNotNull { it.alternativePostalAddress?.adminLevel1Key() })
val regionKeys = requests.mapNotNull { it.physicalPostalAddress?.administrativeAreaLevel1 }
.plus(requests.mapNotNull { it.alternativePostalAddress?.administrativeAreaLevel1 })
.toSet()
val regions = regionRepository.findByRegionCodeIn(regionKeys)

Expand All @@ -165,8 +165,8 @@ class MetadataService(

fun getRegions(requests: Collection<IBaseLogisticAddressDto>): Set<Region> {

val regionKeys = requests.mapNotNull { it.physicalPostalAddress?.adminLevel1Key() }
.plus(requests.mapNotNull { it.alternativePostalAddress?.adminLevel1Key() })
val regionKeys = requests.mapNotNull { it.physicalPostalAddress?.administrativeAreaLevel1 }
.plus(requests.mapNotNull { it.alternativePostalAddress?.administrativeAreaLevel1 })
.toSet()
val regions = regionRepository.findByRegionCodeIn(regionKeys)
return regions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ fun LogisticAddress.toMainAddressResponse(): MainAddressVerboseDto {
fun PhysicalPostalAddress.toDto(): PhysicalPostalAddressVerboseDto {
return PhysicalPostalAddressVerboseDto(
geographicCoordinates = geographicCoordinates?.toDto(),
country = country.toDto(),
countryVerbose = country.toDto(),
postalCode = postCode,
city = city,
administrativeAreaLevel1 = administrativeAreaLevel1?.let { RegionDto(it.countryCode, it.regionCode, it.regionName) },
administrativeAreaLevel1Verbose = administrativeAreaLevel1?.let { RegionDto(it.countryCode, it.regionCode, it.regionName) },
administrativeAreaLevel2 = administrativeAreaLevel2,
administrativeAreaLevel3 = administrativeAreaLevel3,
district = districtLevel1,
Expand All @@ -164,10 +164,10 @@ fun PhysicalPostalAddress.toDto(): PhysicalPostalAddressVerboseDto {
fun AlternativePostalAddress.toDto(): AlternativePostalAddressVerboseDto {
return AlternativePostalAddressVerboseDto(
geographicCoordinates = geographicCoordinates?.toDto(),
country = country.toDto(),
countryVerbose = country.toDto(),
postalCode = postCode,
city = city,
administrativeAreaLevel1 = administrativeAreaLevel1?.let { RegionDto(it.countryCode, it.regionCode, it.regionName) },
administrativeAreaLevel1Verbose = administrativeAreaLevel1?.let { RegionDto(it.countryCode, it.regionCode, it.regionName) },
deliveryServiceType = deliveryServiceType,
deliveryServiceNumber = deliveryServiceNumber,
deliveryServiceQualifier = deliveryServiceQualifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,17 +550,17 @@ class TaskStepFetchAndReserveServiceTest @Autowired constructor(
val verbosePhysicalAddress = verboseAddress.physicalPostalAddress
val physicalAddress = address?.physicalPostalAddress
assertThat(verbosePhysicalAddress).usingRecursiveComparison()
.ignoringFields("country", "administrativeAreaLevel1")
.ignoringFields("countryVerbose", "administrativeAreaLevel1Verbose")
.isEqualTo(physicalAddress)
assertThat(verbosePhysicalAddress.country.technicalKey.name).isEqualTo(physicalAddress?.country?.name)
assertThat(verbosePhysicalAddress.administrativeAreaLevel1?.regionCode).isEqualTo(physicalAddress?.administrativeAreaLevel1)
assertThat(verbosePhysicalAddress.country.name).isEqualTo(physicalAddress?.country?.name)
assertThat(verbosePhysicalAddress.administrativeAreaLevel1).isEqualTo(physicalAddress?.administrativeAreaLevel1)
val verboseAlternAddress = verboseAddress.alternativePostalAddress
val alternAddress = address?.alternativePostalAddress
assertThat(verboseAlternAddress).usingRecursiveComparison()
.ignoringFields("country", "administrativeAreaLevel1")
.ignoringFields("countryVerbose", "administrativeAreaLevel1Verbose")
.isEqualTo(alternAddress)
assertThat(verboseAlternAddress?.country?.technicalKey?.name).isEqualTo(alternAddress?.country?.name)
assertThat(verboseAlternAddress?.administrativeAreaLevel1?.regionCode).isEqualTo(alternAddress?.administrativeAreaLevel1)
assertThat(verboseAlternAddress?.country?.name).isEqualTo(alternAddress?.country?.name)
assertThat(verboseAlternAddress?.administrativeAreaLevel1).isEqualTo(alternAddress?.administrativeAreaLevel1)
}

fun compareAddressStates(statesVerbose: Collection<AddressStateVerboseDto>, states: Collection<AddressStateDto>?) {
Expand Down
Loading

0 comments on commit 5dc9687

Please sign in to comment.