Skip to content

Commit

Permalink
fix(gate): Added new Name Parts Table related to Legal Entity, Addres…
Browse files Browse the repository at this point in the history
…s and Sites with the respective mappings
  • Loading branch information
alexsilva-CGI committed Jun 27, 2023
1 parent 45554d0 commit 0513cb7
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ class LegalEntity(
@Column(name = "legal_form_id", nullable = false)
var legalForm: String?,

@Column(name = "name_parts", nullable = true)
val nameParts: List<String> = emptyList(),

@Column(name = "data_type")
@Enumerated(EnumType.STRING)
var dataType: OutputInputEnum
Expand All @@ -62,4 +59,8 @@ class LegalEntity(

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

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ class LogisticAddress(
@JoinColumn(name = "site_id")
var site: Site?,

@Column(name = "name_parts", nullable = true)
var nameParts: Collection<String> = emptyList(),

@Column(name = "data_type")
@Enumerated(EnumType.STRING)
var dataType: OutputInputEnum,
Expand All @@ -63,4 +60,7 @@ class LogisticAddress(

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

@OneToMany(mappedBy = "address", cascade = [CascadeType.ALL], orphanRemoval = true)
val nameParts: MutableSet<NameParts> = mutableSetOf()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*******************************************************************************
* 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

@Entity
@Table(
name = "name_parts",
indexes = [
Index(columnList = "address_id"),
Index(columnList = "site_id")
]
)
class NameParts(

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

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

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

@Column(name = "name_part")
val namePart: String,

) : BaseEntity()
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ class Site(
@Enumerated(EnumType.STRING)
var dataType: OutputInputEnum,

@Column(name = "name_parts", nullable = true)
var nameParts: Collection<String> = emptyList(),

@ManyToOne
@JoinColumn(name = "legal_entity_id", nullable = false)
var legalEntity: LegalEntity,
Expand All @@ -54,4 +51,7 @@ class Site(
@ManyToOne(fetch = FetchType.LAZY, cascade = [CascadeType.ALL])
@JoinColumn(name = "main_address_id", nullable = false)
lateinit var mainAddress: LogisticAddress

@OneToMany(mappedBy = "site", cascade = [CascadeType.ALL], orphanRemoval = true)
val nameParts: MutableSet<NameParts> = mutableSetOf()
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ class AddressPersistenceService(

private fun updateAddress(address: LogisticAddress, changeAddress: AddressGateInputRequest, legalEntityRecord: LegalEntity?, siteRecord: Site?) {

address.nameParts = changeAddress.address.nameParts
address.externalId = changeAddress.externalId
address.legalEntity = legalEntityRecord
address.site = siteRecord
address.physicalPostalAddress = changeAddress.address.physicalPostalAddress.toPhysicalPostalAddressEntity()
address.alternativePostalAddress = changeAddress.address.alternativePostalAddress?.toAlternativePostalAddressEntity()

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

}

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

private fun updateAddressOutput(address: LogisticAddress, changeAddress: AddressGateOutputRequest, legalEntityRecord: LegalEntity?, siteRecord: Site?) {

address.nameParts = changeAddress.address.nameParts
address.bpn = changeAddress.bpn
address.externalId = changeAddress.externalId
address.legalEntity = legalEntityRecord
Expand All @@ -116,6 +115,7 @@ class AddressPersistenceService(
address.alternativePostalAddress = changeAddress.address.alternativePostalAddress?.toAlternativePostalAddressEntity()

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

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,27 @@ class LegalEntityPersistenceService(
): LegalEntity {
legalEntity.externalId = legalEntityRequest.externalId
legalEntity.legalForm = legalEntityRequest.legalEntity.legalForm
legalEntity.legalName = Name(value = legalEntityRequest.legalNameParts[0], shortName = legalEntityRequest.legalEntity.legalShortName)
legalEntity.legalName = Name("", shortName = legalEntityRequest.legalEntity.legalShortName)

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.legalAddress = logisticAddressRecord
legalEntity.legalAddress.legalEntity = legalEntity

return legalEntity
}

private fun updateAddress(address: LogisticAddress, changeAddress: LogisticAddress) {

address.nameParts = changeAddress.nameParts
address.externalId = changeAddress.externalId
address.legalEntity = changeAddress.legalEntity
address.physicalPostalAddress = changeAddress.physicalPostalAddress
address.alternativePostalAddress = changeAddress.alternativePostalAddress

address.states.replace(changeAddress.states.map { toEntityAddress(it, address) })
//address.nameParts.replace(changeAddress.nameParts)

}

Expand Down Expand Up @@ -144,11 +148,15 @@ class LegalEntityPersistenceService(
legalEntity.bpn = legalEntityRequest.bpn
legalEntity.externalId = legalEntityRequest.externalId
legalEntity.legalForm = legalEntityRequest.legalEntity.legalForm
legalEntity.legalName = Name(value = legalEntityRequest.legalNameParts[0], shortName = legalEntityRequest.legalEntity.legalShortName)
legalEntity.legalName = Name("", shortName = legalEntityRequest.legalEntity.legalShortName)

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.legalAddress = logisticAddressRecord
legalEntity.legalAddress.legalEntity = legalEntity

return legalEntity
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private fun toValidSingleLegalEntity(legalEntity: LegalEntity): LegalEntityGateI

return LegalEntityGateInputResponse(
legalEntity = legalEntity.toLegalEntityDto(),
legalNameParts = listOf(legalEntity.legalName.value),
legalNameParts = getNamePartValuesToList(legalEntity.nameParts),
legalAddress = legalEntity.legalAddress.toAddressGateInputResponse(legalEntity.legalAddress),
externalId = legalEntity.externalId
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ fun AddressGateInputRequest.toAddressGate(legalEntity: LegalEntity?, site: Site?

val logisticAddress = LogisticAddress(
externalId = externalId,
nameParts = address.nameParts,
physicalPostalAddress = address.physicalPostalAddress.toPhysicalPostalAddressEntity(),
alternativePostalAddress = address.alternativePostalAddress?.toAlternativePostalAddressEntity(),
legalEntity = legalEntity,
Expand All @@ -40,6 +39,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())

return logisticAddress
}
Expand All @@ -49,7 +49,6 @@ fun AddressGateOutputRequest.toAddressGateOutput(legalEntity: LegalEntity?, site
val logisticAddress = LogisticAddress(
bpn = bpn,
externalId = externalId,
nameParts = address.nameParts,
physicalPostalAddress = address.physicalPostalAddress.toPhysicalPostalAddressEntity(),
alternativePostalAddress = address.alternativePostalAddress?.toAlternativePostalAddressEntity(),
legalEntity = legalEntity,
Expand All @@ -58,6 +57,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())

return logisticAddress
}
Expand All @@ -66,6 +66,10 @@ fun toEntityAddress(dto: AddressStateDto, address: LogisticAddress): AddressStat
return AddressState(dto.description, dto.validFrom, dto.validTo, dto.type, address)
}

fun toNameParts(namePartsValue: String, address: LogisticAddress?, site: Site?, legalEntity: LegalEntity?): NameParts {
return NameParts(address, site, legalEntity, namePartsValue)
}

fun AlternativePostalAddressDto.toAlternativePostalAddressEntity(): AlternativePostalAddress {

return AlternativePostalAddress(
Expand Down Expand Up @@ -129,13 +133,14 @@ fun SiteGateInputRequest.toSiteGate(legalEntity: LegalEntity, datatype: OutputIn
)

val site = Site(
nameParts = site.nameParts,
externalId = externalId,
legalEntity = legalEntity,
dataType = datatype
)

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.mainAddress = addressInputRequest.toAddressGate(null, site, datatype)

return site
Expand All @@ -152,13 +157,13 @@ fun SiteGateOutputRequest.toSiteGate(legalEntity: LegalEntity, datatype: OutputI

val site = Site(
bpn = bpn,
nameParts = site.nameParts,
externalId = externalId,
legalEntity = legalEntity,
dataType = datatype
)

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.mainAddress = addressOutputRequest.toAddressGateOutput(null, site, datatype)

return site
Expand Down Expand Up @@ -187,12 +192,13 @@ fun LegalEntityGateInputRequest.toLegalEntity(datatype: OutputInputEnum): LegalE
val legalEntity = LegalEntity(
externalId = externalId,
legalForm = legalEntity.legalForm,
legalName = Name(legalNameParts[0], legalEntity.legalShortName),
legalName = Name("", legalEntity.legalShortName),
dataType = datatype
)

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.legalAddress = addressInputRequest.toAddressGate(legalEntity, null, datatype)

Expand All @@ -213,12 +219,13 @@ fun LegalEntityGateOutputRequest.toLegalEntity(datatype: OutputInputEnum): Legal
bpn = bpn,
externalId = externalId,
legalForm = legalEntity.legalForm,
legalName = Name(legalNameParts[0], legalEntity.legalShortName),
legalName = Name("", legalEntity.legalShortName),
dataType = datatype
)

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.legalAddress = addressOutputRequest.toAddressGateOutput(legalEntity, null, datatype)

Expand Down Expand Up @@ -259,7 +266,7 @@ fun LogisticAddress.toAddressGateInputResponse(logisticAddressPage: LogisticAddr
fun LogisticAddress.toLogisticAddressDto(): LogisticAddressGateDto {

val logisticAddress = LogisticAddressGateDto(
nameParts = nameParts,
nameParts = getNamePartValues(nameParts),
states = mapToDtoStates(states),
physicalPostalAddress = physicalPostalAddress.toPhysicalPostalAddress(),
alternativePostalAddress = alternativePostalAddress?.toAlternativePostalAddressDto(),
Expand All @@ -268,6 +275,16 @@ fun LogisticAddress.toLogisticAddressDto(): LogisticAddressGateDto {
return logisticAddress
}

//Collection
fun getNamePartValues(nameparts: MutableSet<NameParts>): Collection<String> {
return nameparts.map { it.namePart }
}

//List
fun getNamePartValuesToList(nameparts: MutableSet<NameParts>): List<String> {
return nameparts.map { it.namePart }
}

fun mapToDtoStates(states: MutableSet<AddressState>): Collection<AddressStateDto> {
return states.map { AddressStateDto(it.description, it.validFrom, it.validTo, it.type) }
}
Expand Down Expand Up @@ -364,15 +381,15 @@ fun LegalEntity.toLegalEntityGateInputResponse(legalEntity: LegalEntity): LegalE
legalEntity = legalEntity.toLegalEntityDto(),
legalAddress = legalAddress.toAddressGateInputResponse(legalAddress),
externalId = legalEntity.externalId,
legalNameParts = listOf(legalEntity.legalName.value)
legalNameParts = getNamePartValuesToList(nameParts)
)
}

//Site mapping to SiteDto
fun Site.toSiteDto(): SiteGateDto {

return SiteGateDto(
nameParts = nameParts,
nameParts = getNamePartValues(nameParts),
states = mapToDtoSitesStates(states)
)
}
Expand Down Expand Up @@ -424,7 +441,7 @@ fun LegalEntity.toLegalEntityGateOutputResponse(legalEntity: LegalEntity): Legal

return LegalEntityGateOutputResponse(
legalEntity = legalEntity.toLegalEntityDto(),
legalNameParts = listOf(legalName.value),
legalNameParts = getNamePartValues(legalEntity.nameParts),
externalId = legalEntity.externalId,
bpn = legalEntity.bpn!!,
legalAddress = legalAddress.toAddressGateOutputResponse(legalAddress)
Expand Down
Loading

0 comments on commit 0513cb7

Please sign in to comment.