From 8c1e08f7db4afaaad5ded9469448cbb9ae073325 Mon Sep 17 00:00:00 2001 From: alexsilva Date: Wed, 10 May 2023 10:38:40 +0100 Subject: [PATCH] fix(gate): Reverted PersistenceServices update logic --- .../gate/service/AddressPersistenceService.kt | 31 +++++++--------- .../service/LegalEntityPersistenceService.kt | 35 ++++++++----------- .../gate/service/SitePersistenceService.kt | 16 ++++----- .../tractusx/bpdm/gate/service/SiteService.kt | 4 +-- 4 files changed, 35 insertions(+), 51 deletions(-) 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 48e3b1540..563e3e4f0 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 @@ -22,9 +22,9 @@ package org.eclipse.tractusx.bpdm.gate.service import jakarta.transaction.Transactional import org.eclipse.tractusx.bpdm.common.util.replace import org.eclipse.tractusx.bpdm.gate.api.model.AddressGateInputRequest -import org.eclipse.tractusx.bpdm.gate.entity.AddressIdentifier -import org.eclipse.tractusx.bpdm.gate.entity.AddressState +import org.eclipse.tractusx.bpdm.gate.entity.LegalEntity import org.eclipse.tractusx.bpdm.gate.entity.LogisticAddress +import org.eclipse.tractusx.bpdm.gate.entity.Site import org.eclipse.tractusx.bpdm.gate.repository.GateAddressRepository import org.eclipse.tractusx.bpdm.gate.repository.LegalEntityRepository import org.eclipse.tractusx.bpdm.gate.repository.SiteRepository @@ -56,7 +56,7 @@ class AddressPersistenceService( val fullAddress = address.toAddressGate(legalEntityRecord, siteRecord) addressRecord.find { it.externalId == address.externalId }?.let { existingAddress -> - updateAddress(existingAddress, fullAddress) + updateAddress(existingAddress, address, legalEntityRecord, siteRecord) gateAddressRepository.save(existingAddress) } ?: run { gateAddressRepository.save(fullAddress) @@ -64,27 +64,20 @@ class AddressPersistenceService( } } - private fun updateAddress(address: LogisticAddress, changeAddress: LogisticAddress) { + private fun updateAddress(address: LogisticAddress, changeAddress: AddressGateInputRequest, legalEntityRecord: LegalEntity?, siteRecord: Site?) { - address.name = changeAddress.name + address.name = changeAddress.address.name address.bpn = changeAddress.bpn address.externalId = changeAddress.externalId - address.legalEntity = changeAddress.legalEntity - address.siteExternalId = changeAddress.siteExternalId - address.physicalPostalAddress = changeAddress.physicalPostalAddress - address.alternativePostalAddress = changeAddress.alternativePostalAddress + address.legalEntity = legalEntityRecord + address.site = siteRecord + address.siteExternalId = changeAddress.siteExternalId.toString() + address.physicalPostalAddress = changeAddress.address.physicalPostalAddress.toPhysicalPostalAddressEntity() + address.alternativePostalAddress = changeAddress.address.alternativePostalAddress?.toAlternativePostalAddressEntity() - address.identifiers.replace(changeAddress.identifiers.map { toEntityIdentifier(it, address) }) - address.states.replace(changeAddress.states.map { toEntityAddress(it, address) }) + address.identifiers.replace(changeAddress.address.identifiers.map { toEntityIdentifier(it, address) }) + address.states.replace(changeAddress.address.states.map { toEntityAddress(it, address) }) } - fun toEntityAddress(dto: AddressState, address: LogisticAddress): AddressState { - return AddressState(dto.description, dto.validFrom, dto.validTo, dto.type, address) - } - - fun toEntityIdentifier(dto: AddressIdentifier, address: LogisticAddress): AddressIdentifier { - return AddressIdentifier(dto.value, dto.type, address) - } - } \ No newline at end of file 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 23dae4f86..ba42f9a2b 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 @@ -23,7 +23,10 @@ package org.eclipse.tractusx.bpdm.gate.service import org.eclipse.tractusx.bpdm.common.exception.BpdmNotFoundException import org.eclipse.tractusx.bpdm.common.util.replace import org.eclipse.tractusx.bpdm.gate.api.model.LegalEntityGateInputRequest -import org.eclipse.tractusx.bpdm.gate.entity.* +import org.eclipse.tractusx.bpdm.gate.entity.AddressIdentifier +import org.eclipse.tractusx.bpdm.gate.entity.AddressState +import org.eclipse.tractusx.bpdm.gate.entity.LegalEntity +import org.eclipse.tractusx.bpdm.gate.entity.LogisticAddress import org.eclipse.tractusx.bpdm.gate.repository.GateAddressRepository import org.eclipse.tractusx.bpdm.gate.repository.LegalEntityRepository import org.springframework.stereotype.Service @@ -51,7 +54,7 @@ class LegalEntityPersistenceService( updateAddress(logisticAddressRecord, fullLegalEntity.legalAddress) - updateLegalEntity(existingLegalEntity, fullLegalEntity, logisticAddressRecord) + updateLegalEntity(existingLegalEntity, legalEntity, logisticAddressRecord) gateLegalEntityRepository.save(existingLegalEntity) } ?: run { @@ -62,32 +65,24 @@ class LegalEntityPersistenceService( } } - private fun updateLegalEntity(legalEntity: LegalEntity, legalEntityRequest: LegalEntity, logisticAddressRecord: LogisticAddress): LegalEntity { + private fun updateLegalEntity( + legalEntity: LegalEntity, + legalEntityRequest: LegalEntityGateInputRequest, + logisticAddressRecord: LogisticAddress + ): LegalEntity { legalEntity.bpn = legalEntityRequest.bpn legalEntity.externalId = legalEntityRequest.externalId - legalEntity.legalForm = legalEntityRequest.legalForm - legalEntity.legalName = legalEntityRequest.legalName - legalEntity.identifiers.replace(legalEntityRequest.identifiers.map { toEntityIdentifier(it, legalEntity) }) - legalEntity.states.replace(legalEntityRequest.states.map { toEntityState(it, legalEntity) }) - legalEntity.classifications.replace(legalEntityRequest.classifications.map { toEntityClassification(it, legalEntity) }) + legalEntity.legalForm = legalEntityRequest.legalEntity.legalForm + legalEntity.legalName = legalEntityRequest.legalEntity.legalName.toName() + legalEntity.identifiers.replace(legalEntityRequest.legalEntity.identifiers.map { toEntityIdentifier(it, legalEntity) }) + legalEntity.states.replace(legalEntityRequest.legalEntity.states.map { toEntityState(it, legalEntity) }) + legalEntity.classifications.replace(legalEntityRequest.legalEntity.classifications.map { toEntityClassification(it, legalEntity) }) legalEntity.legalAddress = logisticAddressRecord legalEntity.legalAddress.legalEntity = legalEntity return legalEntity } - fun toEntityIdentifier(dto: LegalEntityIdentifier, legalEntity: LegalEntity): LegalEntityIdentifier { - return LegalEntityIdentifier(dto.value, dto.type, dto.issuingBody, legalEntity) - } - - fun toEntityClassification(dto: Classification, legalEntity: LegalEntity): Classification { - return Classification(dto.value, dto.code, dto.type, legalEntity) - } - - fun toEntityState(dto: LegalEntityState, legalEntity: LegalEntity): LegalEntityState { - return LegalEntityState(dto.officialDenotation, dto.validFrom, dto.validTo, dto.type, legalEntity) - } - private fun updateAddress(address: LogisticAddress, changeAddress: LogisticAddress) { address.name = changeAddress.name 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 1ed07573c..0f2ef85cb 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 @@ -60,7 +60,8 @@ class SitePersistenceService( updateAddress(logisticAddressRecord, fullSite.mainAddress) - updateSite(existingSite, fullSite) + updateSite(existingSite, site, legalEntityRecord) + siteRepository.save(existingSite) } ?: run { siteRepository.save(fullSite) @@ -68,19 +69,14 @@ class SitePersistenceService( } } - - private fun updateSite(site: Site, updatedSite: Site) { + private fun updateSite(site: Site, updatedSite: SiteGateInputRequest, legalEntityRecord: LegalEntity) { site.bpn = updatedSite.bpn - site.name = updatedSite.name + site.name = updatedSite.site.name site.externalId = updatedSite.externalId - site.legalEntity = updatedSite.legalEntity - site.states.replace(updatedSite.states.map { toEntityAddress(it, site) }) - - } + site.legalEntity = legalEntityRecord + site.states.replace(updatedSite.site.states.map { toEntityAddress(it, site) }) - fun toEntityAddress(dto: SiteState, site: Site): SiteState { - return SiteState(dto.description, dto.validFrom, dto.validTo, dto.type, site) } private fun updateAddress(address: LogisticAddress, changeAddress: LogisticAddress) { diff --git a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SiteService.kt b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SiteService.kt index 9c5e7d03c..d9969a7e9 100644 --- a/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SiteService.kt +++ b/bpdm-gate/src/main/kotlin/org/eclipse/tractusx/bpdm/gate/service/SiteService.kt @@ -140,13 +140,13 @@ class SiteService( val sitesSaas = toSaasModels(sites) saasClient.upsertSites(sitesSaas) - sitePersistenceService.persistSitesBP(sites) - // create changelog entry if all goes well from saasClient sites.forEach { site -> changelogRepository.save(ChangelogEntry(site.externalId, LsaType.Site)) } + sitePersistenceService.persistSitesBP(sites) + deleteParentRelationsOfSites(sites) upsertParentRelations(sites)