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 7c2b4c75d..fbaf4e8ce 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 @@ -35,8 +35,8 @@ class LegalEntity( @Column(name = "externalId", nullable = false, unique = true) var externalId: String, - @Embedded - var legalName: Name, + @Column(name = "name_shortname") + var shortName: String?, @Column(name = "legal_form_id", nullable = false) var legalForm: String?, @@ -59,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 5e6a1861d..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") - var name: String? = null, - @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/Name.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/entity/Name.kt deleted file mode 100644 index d53f69d93..000000000 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/entity/Name.kt +++ /dev/null @@ -1,32 +0,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.Column -import jakarta.persistence.Embeddable - -@Embeddable -class Name( - @Column(name = "name_value", nullable = false) - val value: String, - - @Column(name = "name_shortname") - val shortName: String? -) 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 10f926a07..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 @@ -29,9 +29,6 @@ class Site( @Column(name = "bpn") var bpn: String? = null, - @Column(name = "name", nullable = false) - var name: String, - @Column(name = "external_id", nullable = false, unique = true) var externalId: String, @@ -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 d775e4f66..eadb1a728 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.name = changeAddress.address.nameParts.firstOrNull() 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.name = changeAddress.address.nameParts.firstOrNull() 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 605b70e90..fa1f26479 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 @@ -26,7 +26,10 @@ import org.eclipse.tractusx.bpdm.common.util.replace import org.eclipse.tractusx.bpdm.gate.api.model.LsaType import org.eclipse.tractusx.bpdm.gate.api.model.request.LegalEntityGateInputRequest import org.eclipse.tractusx.bpdm.gate.api.model.request.LegalEntityGateOutputRequest -import org.eclipse.tractusx.bpdm.gate.entity.* +import org.eclipse.tractusx.bpdm.gate.entity.AddressState +import org.eclipse.tractusx.bpdm.gate.entity.ChangelogEntry +import org.eclipse.tractusx.bpdm.gate.entity.LegalEntity +import org.eclipse.tractusx.bpdm.gate.entity.LogisticAddress import org.eclipse.tractusx.bpdm.gate.repository.ChangelogRepository import org.eclipse.tractusx.bpdm.gate.repository.GateAddressRepository import org.eclipse.tractusx.bpdm.gate.repository.LegalEntityRepository @@ -82,23 +85,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.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.name = changeAddress.name 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.map { toNameParts(it.namePart, address, null, null) }) } @@ -144,11 +151,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.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 c8bd30c89..697f466bf 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 5adbb22e2..a360892ba 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 @@ -35,7 +35,6 @@ fun AddressGateInputRequest.toAddressGate(legalEntity: LegalEntity?, site: Site? val logisticAddress = LogisticAddress( externalId = externalId, - name = address.nameParts.firstOrNull(), physicalPostalAddress = address.physicalPostalAddress.toPhysicalPostalAddressEntity(), alternativePostalAddress = address.alternativePostalAddress?.toAlternativePostalAddressEntity(), legalEntity = legalEntity, @@ -44,6 +43,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 } @@ -53,7 +53,6 @@ fun AddressGateOutputRequest.toAddressGateOutput(legalEntity: LegalEntity?, site val logisticAddress = LogisticAddress( bpn = bpn, externalId = externalId, - name = address.nameParts.firstOrNull(), physicalPostalAddress = address.physicalPostalAddress.toPhysicalPostalAddressEntity(), alternativePostalAddress = address.alternativePostalAddress?.toAlternativePostalAddressEntity(), legalEntity = legalEntity, @@ -62,6 +61,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 } @@ -70,6 +70,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( @@ -133,13 +137,14 @@ fun SiteGateInputRequest.toSiteGate(legalEntity: LegalEntity, datatype: OutputIn ) val site = Site( - name = site.nameParts.firstOrNull() ?: "", 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 @@ -156,13 +161,13 @@ fun SiteGateOutputRequest.toSiteGate(legalEntity: LegalEntity, datatype: OutputI val site = Site( bpn = bpn, - name = site.nameParts.firstOrNull() ?: "", 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 @@ -191,12 +196,13 @@ fun LegalEntityGateInputRequest.toLegalEntity(datatype: OutputInputEnum): LegalE val legalEntity = LegalEntity( externalId = externalId, legalForm = legalEntity.legalForm, - legalName = Name(legalNameParts[0], legalEntity.legalShortName), + shortName = 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) @@ -217,12 +223,13 @@ fun LegalEntityGateOutputRequest.toLegalEntity(datatype: OutputInputEnum): Legal bpn = bpn, externalId = externalId, legalForm = legalEntity.legalForm, - legalName = Name(legalNameParts[0], legalEntity.legalShortName), + shortName = 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) @@ -263,7 +270,7 @@ fun LogisticAddress.toAddressGateInputResponse(logisticAddressPage: LogisticAddr fun LogisticAddress.toLogisticAddressDto(): LogisticAddressGateDto { val logisticAddress = LogisticAddressGateDto( - nameParts = name?.let { listOf(name!!) } ?: emptyList(), + nameParts = getNamePartValues(nameParts), states = mapToDtoStates(states), physicalPostalAddress = physicalPostalAddress.toPhysicalPostalAddress(), alternativePostalAddress = alternativePostalAddress?.toAlternativePostalAddressDto(), @@ -272,6 +279,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) } } @@ -344,9 +361,10 @@ private fun Street.toStreetDto(): StreetGateDto { } fun LegalEntity.toLegalEntityDto(): LegalEntityDto { + return LegalEntityDto( legalForm = legalForm, - legalShortName = legalName.shortName, + legalShortName = shortName, states = mapToLegalEntityStateDto(states), classifications = mapToLegalEntityClassificationsDto(classifications), ) @@ -368,7 +386,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) ) } @@ -376,7 +394,7 @@ fun LegalEntity.toLegalEntityGateInputResponse(legalEntity: LegalEntity): LegalE fun Site.toSiteDto(): SiteGateDto { return SiteGateDto( - nameParts = if (name.isEmpty()) emptyList() else listOf(name), + nameParts = getNamePartValues(nameParts), states = mapToDtoSitesStates(states) ) } @@ -428,7 +446,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 e4ec87f3d..6c513dad0 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.name = updatedSite.site.nameParts.firstOrNull() ?: "" 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.name = changeAddress.name 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.map { toNameParts(it.namePart, address, null, null) }) } 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.name = updatedSite.site.nameParts.firstOrNull() ?: "" 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 new file mode 100644 index 000000000..fd9174589 --- /dev/null +++ b/bpdm-gate/src/main/resources/db/migration/V4_0_0_4__add_name_parts_collumn.sql @@ -0,0 +1,46 @@ +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; + +INSERT INTO name_parts (id, address_id, name_part, created_at, updated_at, uuid) +SELECT nextval('bpdm_sequence'), id, name, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, uuid +FROM logistic_addresses WHERE name IS NOT NULL; + +INSERT INTO name_parts (id, site_id, name_part, created_at, updated_at, uuid) +SELECT nextval('bpdm_sequence'), id, name, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, uuid +FROM sites WHERE name IS NOT NULL; + +INSERT INTO name_parts (id, legal_entity_id, name_part, created_at, updated_at, uuid) +SELECT nextval('bpdm_sequence'), id, name_shortname, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, uuid +FROM legal_entities WHERE name_shortname IS NOT NULL; + +ALTER TABLE logistic_addresses +DROP COLUMN IF EXISTS name; + +ALTER TABLE sites +DROP COLUMN IF EXISTS name; + +ALTER TABLE legal_entities +DROP COLUMN IF EXISTS name_value; \ No newline at end of file