Skip to content

Commit

Permalink
Merge pull request #286 from catenax-ng/fix/persistence_changelog
Browse files Browse the repository at this point in the history
Fix - Legal/Main Address Changelogs on Update/Create
  • Loading branch information
nicoprow authored Jun 22, 2023
2 parents c279eab + ff19123 commit 85a5398
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import org.eclipse.tractusx.bpdm.common.model.OutputInputEnum
import org.eclipse.tractusx.bpdm.common.util.replace
import org.eclipse.tractusx.bpdm.gate.api.model.LegalEntityGateInputRequest
import org.eclipse.tractusx.bpdm.gate.api.model.LegalEntityGateOutputRequest
import org.eclipse.tractusx.bpdm.gate.api.model.LsaType
import org.eclipse.tractusx.bpdm.gate.entity.*
import org.eclipse.tractusx.bpdm.gate.repository.ChangelogRepository
import org.eclipse.tractusx.bpdm.gate.repository.GateAddressRepository
import org.eclipse.tractusx.bpdm.gate.repository.LegalEntityRepository
import org.springframework.http.HttpStatus
Expand All @@ -36,7 +38,8 @@ import org.springframework.web.server.ResponseStatusException
@Service
class LegalEntityPersistenceService(
private val gateLegalEntityRepository: LegalEntityRepository,
private val gateAddressRepository: GateAddressRepository
private val gateAddressRepository: GateAddressRepository,
private val changelogRepository: ChangelogRepository
) {

@Transactional
Expand All @@ -58,12 +61,20 @@ class LegalEntityPersistenceService(
updateLegalEntity(existingLegalEntity, legalEntity, logisticAddressRecord)

gateLegalEntityRepository.save(existingLegalEntity)
saveChangelog(legalEntity)
} ?: run {
gateLegalEntityRepository.save(fullLegalEntity)
saveChangelog(legalEntity)
}
}
}

//Creates Changelog For both Legal Entity and Logistic Address when they are created or updated
private fun saveChangelog(legalEntity: LegalEntityGateInputRequest) {
changelogRepository.save(ChangelogEntry(getMainAddressForLegalEntityExternalId(legalEntity.externalId), LsaType.ADDRESS))
changelogRepository.save(ChangelogEntry(legalEntity.externalId, LsaType.LEGAL_ENTITY))
}

private fun updateLegalEntity(
legalEntity: LegalEntity,
legalEntityRequest: LegalEntityGateInputRequest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ import mu.KotlinLogging
import org.eclipse.tractusx.bpdm.common.dto.response.PageResponse
import org.eclipse.tractusx.bpdm.common.exception.BpdmNotFoundException
import org.eclipse.tractusx.bpdm.common.model.OutputInputEnum
import org.eclipse.tractusx.bpdm.gate.api.model.*
import org.eclipse.tractusx.bpdm.gate.entity.ChangelogEntry
import org.eclipse.tractusx.bpdm.gate.api.model.LegalEntityGateInputRequest
import org.eclipse.tractusx.bpdm.gate.api.model.LegalEntityGateInputResponse
import org.eclipse.tractusx.bpdm.gate.api.model.LegalEntityGateOutputRequest
import org.eclipse.tractusx.bpdm.gate.api.model.LegalEntityGateOutputResponse
import org.eclipse.tractusx.bpdm.gate.entity.LegalEntity
import org.eclipse.tractusx.bpdm.gate.repository.ChangelogRepository
import org.eclipse.tractusx.bpdm.gate.repository.LegalEntityRepository
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageRequest
import org.springframework.stereotype.Service

@Service
class LegalEntityService(
private val changelogRepository: ChangelogRepository,
private val legalEntityPersistenceService: LegalEntityPersistenceService,
private val legalEntityRepository: LegalEntityRepository
) {
Expand All @@ -46,9 +46,6 @@ class LegalEntityService(
**/
fun upsertLegalEntities(legalEntities: Collection<LegalEntityGateInputRequest>) {

legalEntities.forEach { legalEntity ->
changelogRepository.save(ChangelogEntry(legalEntity.externalId, LsaType.LEGAL_ENTITY))
}
legalEntityPersistenceService.persistLegalEntitiesBP(legalEntities, OutputInputEnum.Input)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ package org.eclipse.tractusx.bpdm.gate.service
import org.eclipse.tractusx.bpdm.common.exception.BpdmNotFoundException
import org.eclipse.tractusx.bpdm.common.model.OutputInputEnum
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.SiteGateInputRequest
import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateOutputRequest
import org.eclipse.tractusx.bpdm.gate.entity.*
import org.eclipse.tractusx.bpdm.gate.repository.ChangelogRepository
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 All @@ -37,7 +39,8 @@ import org.springframework.web.server.ResponseStatusException
class SitePersistenceService(
private val siteRepository: SiteRepository,
private val legalEntityRepository: LegalEntityRepository,
private val addressRepository: GateAddressRepository
private val addressRepository: GateAddressRepository,
private val changelogRepository: ChangelogRepository
) {

@Transactional
Expand All @@ -59,12 +62,20 @@ class SitePersistenceService(
updateAddress(logisticAddressRecord, fullSite.mainAddress)
updateSite(existingSite, site, legalEntityRecord)
siteRepository.save(existingSite)
saveChangelog(site)
} ?: run {
siteRepository.save(fullSite)
saveChangelog(site)
}
}
}

//Creates Changelog For both Site and Logistic Address when they are created or updated
private fun saveChangelog(site: SiteGateInputRequest) {
changelogRepository.save(ChangelogEntry(getMainAddressForSiteExternalId(site.externalId), LsaType.ADDRESS))
changelogRepository.save(ChangelogEntry(site.externalId, LsaType.SITE))
}

private fun getAddressRecord(externalId: String, datatype: OutputInputEnum): LogisticAddress {
return addressRepository.findByExternalIdAndDataType(externalId, datatype)
?: throw BpdmNotFoundException("Business Partner", "Error")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,14 @@ import mu.KotlinLogging
import org.eclipse.tractusx.bpdm.common.dto.response.PageResponse
import org.eclipse.tractusx.bpdm.common.dto.saas.BusinessPartnerSaas
import org.eclipse.tractusx.bpdm.common.exception.BpdmNotFoundException
import org.eclipse.tractusx.bpdm.gate.api.model.LsaType
import org.eclipse.tractusx.bpdm.common.model.OutputInputEnum
import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateInputRequest
import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateInputResponse
import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateOutputRequest
import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateOutputResponse
import org.eclipse.tractusx.bpdm.gate.config.BpnConfigProperties
import org.eclipse.tractusx.bpdm.gate.entity.ChangelogEntry
import org.eclipse.tractusx.bpdm.gate.entity.Site
import org.eclipse.tractusx.bpdm.gate.exception.SaasNonexistentParentException
import org.eclipse.tractusx.bpdm.gate.repository.ChangelogRepository
import org.eclipse.tractusx.bpdm.gate.repository.SiteRepository
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageRequest
Expand All @@ -44,7 +41,6 @@ class SiteService(
private val saasRequestMappingService: SaasRequestMappingService,
private val saasClient: SaasClient,
private val bpnConfigProperties: BpnConfigProperties,
private val changelogRepository: ChangelogRepository,
private val sitePersistenceService: SitePersistenceService,
private val siteRepository: SiteRepository
) {
Expand Down Expand Up @@ -111,11 +107,8 @@ class SiteService(
**/
fun upsertSites(sites: Collection<SiteGateInputRequest>) {

sites.forEach { site ->
changelogRepository.save(ChangelogEntry(site.externalId, LsaType.SITE))
}

sitePersistenceService.persistSitesBP(sites, OutputInputEnum.Input)

}

/**
Expand Down

0 comments on commit 85a5398

Please sign in to comment.