From 9457d490995f22c2e2462bbce1d45e47255c4a30 Mon Sep 17 00:00:00 2001 From: alexsilva Date: Tue, 27 Jun 2023 16:02:44 +0100 Subject: [PATCH] fix(gate): Added new Name Parts Table related to Legal Entity, Address and Sites with the respective mappings --- .../tractusx/bpdm/gate/entity/LegalEntity.kt | 7 +- .../bpdm/gate/entity/LogisticAddress.kt | 6 +- .../tractusx/bpdm/gate/entity/NameParts.kt | 69 +++++++++++++++++++ .../eclipse/tractusx/bpdm/gate/entity/Site.kt | 6 +- .../gate/service/AddressPersistenceService.kt | 4 +- .../service/LegalEntityPersistenceService.kt | 14 +++- .../bpdm/gate/service/LegalEntityService.kt | 2 +- .../bpdm/gate/service/ResponseMappings.kt | 37 +++++++--- .../gate/service/SitePersistenceService.kt | 9 +-- .../V4_0_0_4__add_name_parts_collumn.sql | 35 ++++++++-- 10 files changed, 153 insertions(+), 36 deletions(-) create mode 100644 bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/entity/NameParts.kt diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/entity/LegalEntity.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/entity/LegalEntity.kt index da3119a3b..55de320c6 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/entity/LegalEntity.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/entity/LegalEntity.kt @@ -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 = emptyList(), - @Column(name = "data_type") @Enumerated(EnumType.STRING) var dataType: OutputInputEnum @@ -62,4 +59,8 @@ class LegalEntity( @OneToMany(mappedBy = "legalEntity", cascade = [CascadeType.ALL], orphanRemoval = true) val addresses: MutableSet = mutableSetOf() + + @OneToMany(mappedBy = "legalEntity", cascade = [CascadeType.ALL], orphanRemoval = true) + val nameParts: MutableSet = mutableSetOf() + } diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/entity/LogisticAddress.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/entity/LogisticAddress.kt index b071e3233..1c102d84f 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/entity/LogisticAddress.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/entity/LogisticAddress.kt @@ -46,9 +46,6 @@ class LogisticAddress( @JoinColumn(name = "site_id") var site: Site?, - @Column(name = "name_parts", nullable = true) - var nameParts: Collection = emptyList(), - @Column(name = "data_type") @Enumerated(EnumType.STRING) var dataType: OutputInputEnum, @@ -63,4 +60,7 @@ class LogisticAddress( @OneToMany(mappedBy = "address", cascade = [CascadeType.ALL], orphanRemoval = true) val states: MutableSet = mutableSetOf() + + @OneToMany(mappedBy = "address", cascade = [CascadeType.ALL], orphanRemoval = true) + val nameParts: MutableSet = mutableSetOf() } \ No newline at end of file diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/entity/NameParts.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/entity/NameParts.kt new file mode 100644 index 000000000..4f7fe0a4e --- /dev/null +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/entity/NameParts.kt @@ -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() \ No newline at end of file diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/entity/Site.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/entity/Site.kt index 7ef9fd3ae..1c6a082f6 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/entity/Site.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/entity/Site.kt @@ -36,9 +36,6 @@ class Site( @Enumerated(EnumType.STRING) var dataType: OutputInputEnum, - @Column(name = "name_parts", nullable = true) - var nameParts: Collection = emptyList(), - @ManyToOne @JoinColumn(name = "legal_entity_id", nullable = false) var legalEntity: LegalEntity, @@ -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 = mutableSetOf() } \ No newline at end of file diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/AddressPersistenceService.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/AddressPersistenceService.kt index 2f640068d..87751a2cd 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/AddressPersistenceService.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/AddressPersistenceService.kt @@ -67,7 +67,6 @@ 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 @@ -75,6 +74,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) }) } @@ -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 @@ -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) }) } diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/LegalEntityPersistenceService.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/LegalEntityPersistenceService.kt index a233a8774..39e5894d9 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/LegalEntityPersistenceService.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/LegalEntityPersistenceService.kt @@ -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) } @@ -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 } diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/LegalEntityService.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/LegalEntityService.kt index 7626cd12f..462789d62 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/LegalEntityService.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/LegalEntityService.kt @@ -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 ) diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/ResponseMappings.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/ResponseMappings.kt index c5b4ae95a..4b9bfcdd7 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/ResponseMappings.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/ResponseMappings.kt @@ -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, @@ -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 } @@ -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, @@ -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 } @@ -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( @@ -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 @@ -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 @@ -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) @@ -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) @@ -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(), @@ -268,6 +275,16 @@ fun LogisticAddress.toLogisticAddressDto(): LogisticAddressGateDto { return logisticAddress } +//Collection +fun getNamePartValues(nameparts: MutableSet): Collection { + return nameparts.map { it.namePart } +} + +//List +fun getNamePartValuesToList(nameparts: MutableSet): List { + return nameparts.map { it.namePart } +} + fun mapToDtoStates(states: MutableSet): Collection { return states.map { AddressStateDto(it.description, it.validFrom, it.validTo, it.type) } } @@ -364,7 +381,7 @@ fun LegalEntity.toLegalEntityGateInputResponse(legalEntity: LegalEntity): LegalE legalEntity = legalEntity.toLegalEntityDto(), legalAddress = legalAddress.toAddressGateInputResponse(legalAddress), externalId = legalEntity.externalId, - legalNameParts = listOf(legalEntity.legalName.value) + legalNameParts = getNamePartValuesToList(nameParts) ) } @@ -372,7 +389,7 @@ fun LegalEntity.toLegalEntityGateInputResponse(legalEntity: LegalEntity): LegalE fun Site.toSiteDto(): SiteGateDto { return SiteGateDto( - nameParts = nameParts, + nameParts = getNamePartValues(nameParts), states = mapToDtoSitesStates(states) ) } @@ -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) diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SitePersistenceService.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SitePersistenceService.kt index c9bec35c4..06f772566 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SitePersistenceService.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SitePersistenceService.kt @@ -88,23 +88,23 @@ class SitePersistenceService( private fun updateSite(site: Site, updatedSite: SiteGateInputRequest, legalEntityRecord: LegalEntity) { - site.nameParts = updatedSite.site.nameParts site.externalId = updatedSite.externalId site.legalEntity = legalEntityRecord + site.states.replace(updatedSite.site.states.map { toEntityAddress(it, site) }) + site.nameParts.replace(updatedSite.site.nameParts.map { toNameParts(it, null, site, null) }) } 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) } fun toEntityAddress(dto: AddressState, address: LogisticAddress): AddressState { @@ -145,10 +145,11 @@ class SitePersistenceService( private fun updateSiteOutput(site: Site, updatedSite: SiteGateOutputRequest, legalEntityRecord: LegalEntity) { site.bpn = updatedSite.bpn - site.nameParts = updatedSite.site.nameParts site.externalId = updatedSite.externalId site.legalEntity = legalEntityRecord + site.states.replace(updatedSite.site.states.map { toEntityAddress(it, site) }) + site.nameParts.replace(updatedSite.site.nameParts.map { toNameParts(it, null, site, null) }) } } \ No newline at end of file diff --git a/bpdm-gate/src/main/resources/db/migration/V4_0_0_4__add_name_parts_collumn.sql b/bpdm-gate/src/main/resources/db/migration/V4_0_0_4__add_name_parts_collumn.sql index 5cd820834..53ed6d36c 100644 --- a/bpdm-gate/src/main/resources/db/migration/V4_0_0_4__add_name_parts_collumn.sql +++ b/bpdm-gate/src/main/resources/db/migration/V4_0_0_4__add_name_parts_collumn.sql @@ -1,10 +1,31 @@ -ALTER TABLE legal_entities -ADD COLUMN name_parts VARCHAR(255) NULL; - ALTER TABLE logistic_addresses -DROP COLUMN IF EXISTS name, -ADD COLUMN name_parts VARCHAR(255) NULL; +DROP COLUMN IF EXISTS name; ALTER TABLE sites -DROP COLUMN IF EXISTS name, -ADD COLUMN name_parts VARCHAR(255) NULL; \ No newline at end of file +DROP COLUMN IF EXISTS name; + +CREATE TABLE name_parts ( + id BIGINT NOT NULL, + created_at TIMESTAMP WITH time zone NOT NULL, + updated_at TIMESTAMP WITH time zone NOT NULL, + uuid UUID NOT NULL, + address_id BIGINT NULL, + site_id BIGINT NULL, + legal_entity_id BIGINT NULL, + name_part VARCHAR(255) NOT NULL, + PRIMARY KEY (id) +); + +CREATE INDEX address_index_name_parts ON name_parts (address_id); + +CREATE INDEX site_index_name_parts ON name_parts (site_id); + +CREATE INDEX legal_entity_name_parts ON name_parts (legal_entity_id); + +ALTER TABLE IF EXISTS name_parts +ADD CONSTRAINT uuid_name_parts_uk UNIQUE (uuid); + +ALTER TABLE IF EXISTS name_parts +ADD CONSTRAINT fk_address_name_parts FOREIGN KEY (address_id) REFERENCES logistic_addresses, +ADD CONSTRAINT fk_legal_entity_name_parts FOREIGN KEY (legal_entity_id) REFERENCES legal_entities, +ADD CONSTRAINT fk_sites_name_parts FOREIGN KEY (site_id) REFERENCES sites;