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(gate): Gate persist new fields and fix fields retrieval #315

Merged
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 @@ -35,7 +35,7 @@ data class StreetGateDto(
val name: String? = null,

@get:Schema(description = "Describes the name suffix of the Street.")
val NameSuffix: String? = null,
val nameSuffix: String? = null,

@get:Schema(description = "Describes the additional name suffix of the Street.")
val additionalNameSuffix: String? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ class LegalEntity(
@JoinColumn(name = "legal_address_id", nullable = false)
lateinit var legalAddress: LogisticAddress

@OneToMany(mappedBy = "legalEntity", cascade = [CascadeType.ALL], orphanRemoval = true)
val roles: MutableSet<Roles> = mutableSetOf()

@OneToMany(mappedBy = "legalEntity", cascade = [CascadeType.ALL], orphanRemoval = true)
val addresses: MutableSet<LogisticAddress> = mutableSetOf()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,7 @@ class LogisticAddress(

@OneToMany(mappedBy = "address", cascade = [CascadeType.ALL], orphanRemoval = true)
val nameParts: MutableSet<NameParts> = mutableSetOf()

@OneToMany(mappedBy = "address", cascade = [CascadeType.ALL], orphanRemoval = true)
val roles: MutableSet<Roles> = mutableSetOf()
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ class PhysicalPostalAddress(
@AttributeOverride(name = "houseNumber", column = Column(name = "phy_street_number"))
@AttributeOverride(name = "milestone", column = Column(name = "phy_street_milestone"))
@AttributeOverride(name = "direction", column = Column(name = "phy_street_direction"))
@AttributeOverride(name = "namePrefix", column = Column(name = "phy_name_prefix"))
@AttributeOverride(name = "additionalNamePrefix", column = Column(name = "phy_additional_name_prefix"))
@AttributeOverride(name = "nameSuffix", column = Column(name = "phy_name_suffix"))
@AttributeOverride(name = "additionalNameSuffix", column = Column(name = "phy_additional_name_suffix"))
val street: Street? = null,

// specific for PhysicalPostalAddress
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*******************************************************************************
* 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
******************************************************************************/

/*******************************************************************************
* 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.entity

import jakarta.persistence.*
import org.eclipse.tractusx.bpdm.common.model.BaseEntity
import org.eclipse.tractusx.bpdm.gate.api.model.BusinessPartnerRole

@Entity
@Table(name = "roles")
class Roles(

@ManyToOne
@JoinColumn(name = "legal_entity_id", nullable = false)
var legalEntity: LegalEntity?,

@ManyToOne
@JoinColumn(name = "address_id", nullable = true)
var address: LogisticAddress?,

@ManyToOne
@JoinColumn(name = "site_id", nullable = true)
var site: Site?,

@Column(name = "role_name", nullable = false)
@Enumerated(EnumType.STRING)
val roleName: BusinessPartnerRole

) : BaseEntity()
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ class Site(
@OneToMany(mappedBy = "site", cascade = [CascadeType.ALL], orphanRemoval = true)
val addresses: MutableSet<LogisticAddress> = mutableSetOf()

@OneToMany(mappedBy = "site", cascade = [CascadeType.ALL], orphanRemoval = true)
val roles: MutableSet<Roles> = mutableSetOf()

@ManyToOne(fetch = FetchType.LAZY, cascade = [CascadeType.ALL])
@JoinColumn(name = "main_address_id", nullable = false)
lateinit var mainAddress: LogisticAddress
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,18 @@ class Street(
val milestone: String? = null,

@Column
val direction: String? = null
)
val direction: String? = null,

@Column
val namePrefix: String? = null,

@Column
val additionalNamePrefix: String? = null,

@Column
val nameSuffix: String? = null,

@Column
val additionalNameSuffix: String? = null,

)
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class AddressPersistenceService(

address.states.replace(changeAddress.address.states.map { toEntityAddress(it, address) })
address.nameParts.replace(changeAddress.address.nameParts.map { toNameParts(it, address, null, null) })
address.roles.replace(changeAddress.address.roles.distinct().map { toRoles(it, null, null, address) })

}

Expand Down Expand Up @@ -116,6 +117,7 @@ class AddressPersistenceService(

address.states.replace(changeAddress.address.states.map { toEntityAddress(it, address) })
address.nameParts.replace(changeAddress.address.nameParts.map { toNameParts(it, address, null, null) })
address.roles.replace(changeAddress.address.roles.distinct().map { toRoles(it, null, null, address) })

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class LegalEntityPersistenceService(
legalEntity.states.replace(legalEntityRequest.legalEntity.states.map { toEntityState(it, legalEntity) })
legalEntity.classifications.replace(legalEntityRequest.legalEntity.classifications.map { toEntityClassification(it, legalEntity) })
legalEntity.nameParts.replace(legalEntityRequest.legalNameParts.map { toNameParts(it, null, null, legalEntity) })
legalEntity.roles.replace(legalEntityRequest.roles.distinct().map { toRoles(it, legalEntity, null, null) })

legalEntity.legalAddress = logisticAddressRecord
legalEntity.legalAddress.legalEntity = legalEntity
Expand All @@ -106,6 +107,7 @@ class LegalEntityPersistenceService(

address.states.replace(changeAddress.states.map { toEntityAddress(it, address) })
address.nameParts.replace(changeAddress.nameParts.map { toNameParts(it.namePart, address, null, null) })
address.roles.replace(changeAddress.roles.distinct().map { toRoles(it.roleName, null, null, address) })

}

Expand Down Expand Up @@ -156,6 +158,7 @@ class LegalEntityPersistenceService(
legalEntity.states.replace(legalEntityRequest.legalEntity.states.map { toEntityState(it, legalEntity) })
legalEntity.classifications.replace(legalEntityRequest.legalEntity.classifications.map { toEntityClassification(it, legalEntity) })
legalEntity.nameParts.replace(legalEntityRequest.legalNameParts.map { toNameParts(it, null, null, legalEntity) })
legalEntity.roles.replace(legalEntityRequest.roles.distinct().map { toRoles(it, legalEntity, null, null) })

legalEntity.legalAddress = logisticAddressRecord
legalEntity.legalAddress.legalEntity = legalEntity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ private fun toValidSingleLegalEntity(legalEntity: LegalEntity): LegalEntityGateI
return LegalEntityGateInputResponse(
legalEntity = legalEntity.toLegalEntityDto(),
legalNameParts = getNamePartValuesToList(legalEntity.nameParts),
roles = legalEntity.roles.map { it.roleName },
legalAddress = legalEntity.legalAddress.toAddressGateInputResponse(legalEntity.legalAddress),
externalId = legalEntity.externalId
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ package org.eclipse.tractusx.bpdm.gate.service
import org.eclipse.tractusx.bpdm.common.dto.*
import org.eclipse.tractusx.bpdm.common.dto.response.PageResponse
import org.eclipse.tractusx.bpdm.common.model.OutputInputEnum
import org.eclipse.tractusx.bpdm.gate.api.model.LogisticAddressGateDto
import org.eclipse.tractusx.bpdm.gate.api.model.PhysicalPostalAddressGateDto
import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateDto
import org.eclipse.tractusx.bpdm.gate.api.model.StreetGateDto
import org.eclipse.tractusx.bpdm.gate.api.model.*
import org.eclipse.tractusx.bpdm.gate.api.model.request.*
import org.eclipse.tractusx.bpdm.gate.api.model.response.*
import org.eclipse.tractusx.bpdm.gate.entity.*
Expand All @@ -44,6 +41,7 @@ fun AddressGateInputRequest.toAddressGate(legalEntity: LegalEntity?, site: Site?

logisticAddress.states.addAll(this.address.states.map { toEntityAddress(it, logisticAddress) }.toSet())
logisticAddress.nameParts.addAll(this.address.nameParts.map { toNameParts(it, logisticAddress, null, null) }.toSet())
logisticAddress.roles.addAll(this.address.roles.distinct().map { toRoles(it, null, null, logisticAddress) }.toSet())

return logisticAddress
}
Expand All @@ -62,6 +60,7 @@ fun AddressGateOutputRequest.toAddressGateOutput(legalEntity: LegalEntity?, site

logisticAddress.states.addAll(this.address.states.map { toEntityAddress(it, logisticAddress) }.toSet())
logisticAddress.nameParts.addAll(this.address.nameParts.map { toNameParts(it, logisticAddress, null, null) }.toSet())
logisticAddress.roles.addAll(this.address.roles.distinct().map { toRoles(it, null, null, logisticAddress) }.toSet())

return logisticAddress
}
Expand All @@ -79,7 +78,7 @@ fun AlternativePostalAddressDto.toAlternativePostalAddressEntity(): AlternativeP
return AlternativePostalAddress(
geographicCoordinates = baseAddress.geographicCoordinates?.toGeographicCoordinateEntity(),
country = baseAddress.country,
administrativeAreaLevel1 = null, // TODO Add region mapping Logic
administrativeAreaLevel1 = areaPart.administrativeAreaLevel1,
postCode = baseAddress.postalCode,
city = baseAddress.city,
deliveryServiceType = deliveryServiceType,
Expand All @@ -94,7 +93,7 @@ fun PhysicalPostalAddressGateDto.toPhysicalPostalAddressEntity(): PhysicalPostal
return PhysicalPostalAddress(
geographicCoordinates = baseAddress.geographicCoordinates?.toGeographicCoordinateEntity(),
country = baseAddress.country,
administrativeAreaLevel1 = null, // TODO Add region mapping Logic
administrativeAreaLevel1 = areaPart.administrativeAreaLevel1,
administrativeAreaLevel2 = areaPart.administrativeAreaLevel2,
administrativeAreaLevel3 = areaPart.administrativeAreaLevel3,
postCode = baseAddress.postalCode,
Expand All @@ -115,11 +114,16 @@ fun GeoCoordinateDto.toGeographicCoordinateEntity(): GeographicCoordinate {
}

private fun StreetGateDto.toStreetEntity(): Street {

return Street(
name = name,
houseNumber = houseNumber,
milestone = milestone,
direction = direction
direction = direction,
namePrefix = namePrefix,
additionalNamePrefix = additionalNamePrefix,
nameSuffix = nameSuffix,
additionalNameSuffix = additionalNameSuffix
)
}

Expand All @@ -144,6 +148,7 @@ fun SiteGateInputRequest.toSiteGate(legalEntity: LegalEntity, datatype: OutputIn

site.states.addAll(this.site.states.map { toEntityAddress(it, site) }.toSet())
site.nameParts.addAll(this.site.nameParts.map { toNameParts(it, null, site, null) }.toSet())
site.roles.addAll(this.site.roles.distinct().map { toRoles(it, null, site, null) })

site.mainAddress = addressInputRequest.toAddressGate(null, site, datatype)

Expand All @@ -168,6 +173,8 @@ fun SiteGateOutputRequest.toSiteGate(legalEntity: LegalEntity, datatype: OutputI

site.states.addAll(this.site.states.map { toEntityAddress(it, site) }.toSet())
site.nameParts.addAll(this.site.nameParts.map { toNameParts(it, null, site, null) }.toSet())
site.roles.addAll(this.site.roles.distinct().map { toRoles(it, null, site, null) })

site.mainAddress = addressOutputRequest.toAddressGateOutput(null, site, datatype)

return site
Expand Down Expand Up @@ -203,6 +210,7 @@ fun LegalEntityGateInputRequest.toLegalEntity(datatype: OutputInputEnum): LegalE
legalEntity.states.addAll(this.legalEntity.states.map { toEntityState(it, legalEntity) })
legalEntity.classifications.addAll(this.legalEntity.classifications.map { toEntityClassification(it, legalEntity) })
legalEntity.nameParts.addAll(this.legalNameParts.map { toNameParts(it, null, null, legalEntity) })
legalEntity.roles.addAll(this.roles.distinct().map { toRoles(it, legalEntity, null, null) })

legalEntity.legalAddress = addressInputRequest.toAddressGate(legalEntity, null, datatype)

Expand All @@ -229,6 +237,7 @@ fun LegalEntityGateOutputRequest.toLegalEntity(datatype: OutputInputEnum): Legal

legalEntity.states.addAll(this.legalEntity.states.map { toEntityState(it, legalEntity) })
legalEntity.classifications.addAll(this.legalEntity.classifications.map { toEntityClassification(it, legalEntity) })
legalEntity.roles.addAll(this.roles.distinct().map { toRoles(it, legalEntity, null, null) })
legalEntity.nameParts.addAll(this.legalNameParts.map { toNameParts(it, null, null, legalEntity) })

legalEntity.legalAddress = addressOutputRequest.toAddressGateOutput(legalEntity, null, datatype)
Expand All @@ -237,6 +246,10 @@ fun LegalEntityGateOutputRequest.toLegalEntity(datatype: OutputInputEnum): Legal

}

fun toRoles(role: BusinessPartnerRole, legalEntity: LegalEntity?, site: Site?, address: LogisticAddress?): Roles {
return Roles(legalEntity, address, site, role)
}

fun toEntityState(dto: LegalEntityStateDto, legalEntity: LegalEntity): LegalEntityState {
return LegalEntityState(dto.officialDenotation, dto.validFrom, dto.validTo, dto.type, legalEntity)
}
Expand Down Expand Up @@ -272,6 +285,7 @@ fun LogisticAddress.toLogisticAddressDto(): LogisticAddressGateDto {
val logisticAddress = LogisticAddressGateDto(
nameParts = getNamePartValues(nameParts),
states = mapToDtoStates(states),
roles = roles.map { it.roleName },
physicalPostalAddress = physicalPostalAddress.toPhysicalPostalAddress(),
alternativePostalAddress = alternativePostalAddress?.toAlternativePostalAddressDto(),
)
Expand Down Expand Up @@ -303,7 +317,7 @@ fun AlternativePostalAddress.toAlternativePostalAddressDto(): AlternativePostalA
)

val areaDistrictAlternativDto = AreaDistrictAlternativDto(
administrativeAreaLevel1 = null // TODO Add region mapping Logic
administrativeAreaLevel1 = administrativeAreaLevel1
)

return AlternativePostalAddressDto(
Expand All @@ -326,7 +340,7 @@ fun PhysicalPostalAddress.toPhysicalPostalAddress(): PhysicalPostalAddressGateDt
)

val areaDistrictDto = AreaDistrictDto(
administrativeAreaLevel1 = null, // TODO Add region mapping Logic
administrativeAreaLevel1 = administrativeAreaLevel1,
administrativeAreaLevel2 = administrativeAreaLevel2,
administrativeAreaLevel3 = administrativeAreaLevel3,
district = districtLevel1
Expand All @@ -352,11 +366,16 @@ fun GeographicCoordinate.toGeographicCoordinateDto(): GeoCoordinateDto {
}

private fun Street.toStreetDto(): StreetGateDto {

return StreetGateDto(
name = name,
houseNumber = houseNumber,
milestone = milestone,
direction = direction
direction = direction,
namePrefix = namePrefix,
additionalNamePrefix = additionalNamePrefix,
nameSuffix = nameSuffix,
additionalNameSuffix = additionalNameSuffix
)
}

Expand Down Expand Up @@ -385,6 +404,7 @@ fun LegalEntity.toLegalEntityGateInputResponse(legalEntity: LegalEntity): LegalE
return LegalEntityGateInputResponse(
legalEntity = legalEntity.toLegalEntityDto(),
legalAddress = legalAddress.toAddressGateInputResponse(legalAddress),
roles = roles.map { it.roleName },
externalId = legalEntity.externalId,
legalNameParts = getNamePartValuesToList(nameParts)
)
Expand All @@ -394,6 +414,7 @@ fun LegalEntity.toLegalEntityGateInputResponse(legalEntity: LegalEntity): LegalE
fun Site.toSiteDto(): SiteGateDto {

return SiteGateDto(
roles = roles.map { it.roleName },
nameParts = getNamePartValues(nameParts),
states = mapToDtoSitesStates(states)
)
Expand Down Expand Up @@ -449,6 +470,7 @@ fun LegalEntity.toLegalEntityGateOutputResponse(legalEntity: LegalEntity): Legal
legalNameParts = getNamePartValues(legalEntity.nameParts),
externalId = legalEntity.externalId,
bpn = legalEntity.bpn!!,
roles = roles.map { it.roleName },
legalAddress = legalAddress.toAddressGateOutputResponse(legalAddress)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class SitePersistenceService(

site.states.replace(updatedSite.site.states.map { toEntityAddress(it, site) })
site.nameParts.replace(updatedSite.site.nameParts.map { toNameParts(it, null, site, null) })
site.roles.replace(updatedSite.site.roles.distinct().map { toRoles(it, null, site, null) })

}

Expand All @@ -105,6 +106,8 @@ class SitePersistenceService(

address.states.replace(changeAddress.states.map { toEntityAddress(it, address) })
address.nameParts.replace(changeAddress.nameParts.map { toNameParts(it.namePart, address, null, null) })
address.roles.replace(changeAddress.roles.distinct().map { toRoles(it.roleName, null, null, address) })

}

fun toEntityAddress(dto: AddressState, address: LogisticAddress): AddressState {
Expand Down Expand Up @@ -150,6 +153,7 @@ class SitePersistenceService(

site.states.replace(updatedSite.site.states.map { toEntityAddress(it, site) })
site.nameParts.replace(updatedSite.site.nameParts.map { toNameParts(it, null, site, null) })
site.roles.replace(updatedSite.site.roles.distinct().map { toRoles(it, null, site, null) })

}
}
Loading