Skip to content

Commit

Permalink
fix(gate): Reverted PersistenceServices update logic
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsilva-CGI committed May 23, 2023
1 parent a64014d commit 8c1e08f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -56,35 +56,28 @@ 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)
}
}
}

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)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -51,7 +54,7 @@ class LegalEntityPersistenceService(

updateAddress(logisticAddressRecord, fullLegalEntity.legalAddress)

updateLegalEntity(existingLegalEntity, fullLegalEntity, logisticAddressRecord)
updateLegalEntity(existingLegalEntity, legalEntity, logisticAddressRecord)
gateLegalEntityRepository.save(existingLegalEntity)

} ?: run {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,27 +60,23 @@ class SitePersistenceService(

updateAddress(logisticAddressRecord, fullSite.mainAddress)

updateSite(existingSite, fullSite)
updateSite(existingSite, site, legalEntityRecord)

siteRepository.save(existingSite)
} ?: run {
siteRepository.save(fullSite)
}
}
}


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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8c1e08f

Please sign in to comment.