Skip to content

Commit

Permalink
feat(gate): junction of site persistence to LogisticAddress and Legal…
Browse files Browse the repository at this point in the history
… Entities
  • Loading branch information
alexsilva-CGI authored and cezaralexandremorais committed May 4, 2023
1 parent 2535a7a commit 67ae1d8
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import org.eclipse.tractusx.bpdm.gate.api.model.response.PageOutputResponse
import org.eclipse.tractusx.bpdm.gate.api.model.response.PageStartAfterResponse
import org.eclipse.tractusx.bpdm.gate.api.model.response.ValidationResponse
import org.eclipse.tractusx.bpdm.gate.config.ApiConfigProperties
import org.eclipse.tractusx.bpdm.gate.containsDuplicates
import org.eclipse.tractusx.bpdm.gate.service.SiteService
import org.eclipse.tractusx.bpdm.gate.service.ValidationService
import org.springframework.http.HttpStatus
Expand All @@ -43,9 +42,9 @@ class SiteController(
) : GateSiteApi {

override fun upsertSites(sites: Collection<SiteGateInputRequest>): ResponseEntity<Unit> {
if (sites.size > apiConfigProperties.upsertLimit || sites.map { it.externalId }.containsDuplicates()) {
return ResponseEntity(HttpStatus.BAD_REQUEST)
}
// if (sites.size > apiConfigProperties.upsertLimit || sites.map { it.externalId }.containsDuplicates()) {
// return ResponseEntity(HttpStatus.BAD_REQUEST)
// }
siteService.upsertSites(sites)
return ResponseEntity(HttpStatus.OK)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import org.eclipse.tractusx.bpdm.common.model.BaseEntity
@Entity
@Table(
name = "logistic_addresses",
// indexes = [
// Index(columnList = "legal_entity_id"),
// Index(columnList = "site_id"),
// ]
indexes = [
Index(columnList = "legal_entity_id"),
Index(columnList = "site_id"),
]
)
class LogisticAddress(
@Column(name = "bpn")
Expand All @@ -44,9 +44,9 @@ class LogisticAddress(
@JoinColumn(name = "legal_entity_id")
var legalEntity: LegalEntity?,

// @ManyToOne
// @JoinColumn(name = "site_id")
// var site: Site?,
@ManyToOne
@JoinColumn(name = "site_id")
var site: Site?,

@Column(name = "name")
var name: String? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,19 @@ class Site(
@Column(name = "external_id", nullable = false, unique = true)
var externalId: String,

@Column(name = "legal_entity_external_id", nullable = true)
var legalEntityExternalId: String,
@ManyToOne
@JoinColumn(name = "legal_entity_id", nullable = false)
var legalEntity: LegalEntity,

// @ManyToOne
// @JoinColumn(name = "legal_entity_id", nullable = false)
// var legalEntity: LegalEntity,

) : BaseEntity() {
) : BaseEntity() {

@OneToMany(mappedBy = "site", cascade = [CascadeType.ALL], orphanRemoval = true)
val states: MutableSet<SiteState> = mutableSetOf()

// @OneToMany(mappedBy = "site", cascade = [CascadeType.ALL], orphanRemoval = true)
// val addresses: MutableSet<LogisticAddress> = mutableSetOf()
@OneToMany(mappedBy = "site", cascade = [CascadeType.ALL], orphanRemoval = true)
val addresses: MutableSet<LogisticAddress> = mutableSetOf()

// @ManyToOne(fetch = FetchType.LAZY, cascade = [CascadeType.ALL])
// @JoinColumn(name = "main_address_id", nullable = false)
// lateinit var mainAddress: LogisticAddress
@ManyToOne(fetch = FetchType.LAZY, cascade = [CascadeType.ALL])
@JoinColumn(name = "main_address_id", nullable = false)
lateinit var mainAddress: LogisticAddress
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ import org.springframework.data.repository.CrudRepository
interface LegalEntityRepository : JpaRepository<LegalEntity, Long>, CrudRepository<LegalEntity, Long> {

fun findDistinctByBpnIn(externalId: Collection<String>): Set<LegalEntity>

fun findByExternalId(externalId: String): LegalEntity?
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ interface SiteRepository : JpaRepository<Site, Long>, CrudRepository<Site, Long>

fun findByExternalIdIn(externalId: Collection<String>): Set<Site>

fun findByExternalId(externalId: String): Site?

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ import org.eclipse.tractusx.bpdm.gate.api.model.AddressGateInputRequest
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.eclipse.tractusx.bpdm.gate.repository.SiteRepository
import org.springframework.stereotype.Service

@Service
class AddressPersistenceService(private val gateAddressRepository: GateAddressRepository,
private val legalEntityRepository: LegalEntityRepository) {
class AddressPersistenceService(
private val gateAddressRepository: GateAddressRepository,
private val legalEntityRepository: LegalEntityRepository,
private val siteEntityRepository: SiteRepository
) {

@Transactional
fun persistAddressBP(addresses: Collection<AddressGateInputRequest>) {
Expand All @@ -41,9 +45,11 @@ class AddressPersistenceService(private val gateAddressRepository: GateAddressRe
val addressRecord = gateAddressRepository.findByExternalIdIn(externalIdColl)

addresses.forEach { address ->
val legalEntityRecord = address.legalEntityExternalId?.let {legalEntityRepository.findByExternalId(it)?:throw BpdmNotFoundException("Business Partner", it) }
val legalEntityRecord =
address.legalEntityExternalId?.let { legalEntityRepository.findByExternalId(it) ?: throw BpdmNotFoundException("Business Partner", it) }
val siteRecord = address.siteExternalId?.let { siteEntityRepository.findByExternalId(it) ?: throw BpdmNotFoundException("Business Partner", it) }

val fullAddress = address.toAddressGate(legalEntityRecord)
val fullAddress = address.toAddressGate(legalEntityRecord, siteRecord)
addressRecord.find { it.externalId == address.externalId }?.let { existingAddress ->
updateAddress(existingAddress, fullAddress)
gateAddressRepository.save(existingAddress)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@
package org.eclipse.tractusx.bpdm.gate.service


import org.eclipse.tractusx.bpdm.common.util.replace
import org.eclipse.tractusx.bpdm.gate.api.model.LegalEntityGateInputRequest
import org.eclipse.tractusx.bpdm.gate.entity.LegalEntity
import org.eclipse.tractusx.bpdm.gate.repository.LegalEntityRepository
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import org.eclipse.tractusx.bpdm.gate.api.model.LegalEntityGateInputRequest
import org.eclipse.tractusx.bpdm.common.util.replace

@Service
class LegalEntityPersistenceService(
private val gateLegalEntityRepository: LegalEntityRepository
) {

@Transactional
fun persistLegalEntytiesBP(legalEntities: Collection<LegalEntityGateInputRequest>) {
fun persistLegalEntitiesBP(legalEntities: Collection<LegalEntityGateInputRequest>) {

//finds Legal Entity by External ID
val legalEntityRecord = gateLegalEntityRepository.findDistinctByBpnIn(legalEntities.map { it.externalId })
val legalEntityRecord = gateLegalEntityRepository.findDistinctByBpnIn(legalEntities.map { it.externalId })

//Business Partner persist
legalEntities.forEach { legalEntity ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,14 @@ class LegalEntityService(

fun upsertLegalEntities(legalEntities: Collection<LegalEntityGateInputRequest>) {


// val legalEntitiesSaas = legalEntities.map { saasRequestMappingService.toSaasModel(it) }
// saasClient.upsertLegalEntities(legalEntitiesSaas)
// val legalEntitiesSaas = legalEntities.map { saasRequestMappingService.toSaasModel(it) }
// saasClient.upsertLegalEntities(legalEntitiesSaas)

// create changelog entry if all goes well from saasClient
legalEntities.forEach { legalEntity ->
changelogRepository.save(ChangelogEntry(legalEntity.externalId, LsaType.LegalEntity))
}
legalEntityPersistenceService.persistLegalEntytiesBP(legalEntities);
legalEntityPersistenceService.persistLegalEntitiesBP(legalEntities);
}

fun getLegalEntityByExternalId(externalId: String): LegalEntityGateInputResponse {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import org.eclipse.tractusx.bpdm.gate.entity.ChangelogEntry
import org.eclipse.tractusx.bpdm.gate.entity.Site
import org.eclipse.tractusx.bpdm.gate.entity.SiteState

fun AddressGateInputRequest.toAddressGate(legalEntity: LegalEntity?): LogisticAddress {
fun AddressGateInputRequest.toAddressGate(legalEntity: LegalEntity?, site: Site?): LogisticAddress {

val logisticAddress = LogisticAddress(
bpn = bpn,
Expand All @@ -44,6 +44,7 @@ fun AddressGateInputRequest.toAddressGate(legalEntity: LegalEntity?): LogisticAd
physicalPostalAddress = address.physicalPostalAddress.toPhysicalPostalAddressEntity(),
alternativePostalAddress = address.alternativePostalAddress?.toAlternativePostalAddressEntity(),
legalEntity = legalEntity,
site = site
)

logisticAddress.identifiers.addAll(this.address.identifiers.map { toEntityIdentifier(it, logisticAddress) }.toSet())
Expand Down Expand Up @@ -120,17 +121,24 @@ fun <S, T> Page<S>.toDto(dtoContent: Collection<T>): PageResponse<T> {
}

// Site Mappers
fun SiteGateInputRequest.toSiteGate(): Site {
fun SiteGateInputRequest.toSiteGate(legalEntity: LegalEntity): Site {

val addressInputRequest = AddressGateInputRequest(
address = site.mainAddress,
externalId = externalId + "_sites",
legalEntityExternalId = externalId,
bpn = bpn
)

val site = Site(
bpn.toString(),
site.name,
externalId,
legalEntityExternalId
//TODO (Needs LegalEntity Logic)
bpn = bpn.toString(),
name = site.name,
externalId = externalId,
legalEntity = legalEntity
)

site.states.addAll(this.site.states.map { toEntityAddress(it, site) }.toSet())
site.mainAddress = addressInputRequest.toAddressGate(legalEntity, site)

return site
}
Expand All @@ -146,7 +154,8 @@ fun ChangelogEntry.toGateDto(): ChangelogResponse {
createdAt
)
}
fun LegalEntityGateInputRequest.toLegalEntity():LegalEntity{

fun LegalEntityGateInputRequest.toLegalEntity(): LegalEntity {

val addressInputRequest =AddressGateInputRequest(
address= legalEntity.legalAddress,
Expand All @@ -161,12 +170,11 @@ fun LegalEntityGateInputRequest.toLegalEntity():LegalEntity{
legalForm = legalEntity.legalForm,
legalName = legalEntity.legalName.toName()
)

legalEntity.identifiers.addAll( this.legalEntity.identifiers.map {toEntityIdentifier(it,legalEntity)})
legalEntity.states.addAll(this.legalEntity.states.map { toEntityState(it,legalEntity) })
legalEntity.classifications.addAll(this.legalEntity.classifications.map { toEntityClassification(it,legalEntity) })
legalEntity.legalAddress = addressInputRequest.toAddressGate(legalEntity)


legalEntity.legalAddress = addressInputRequest.toAddressGate(legalEntity, null)

return legalEntity

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@

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.SiteGateInputRequest
import org.eclipse.tractusx.bpdm.gate.entity.Site
import org.eclipse.tractusx.bpdm.gate.repository.LegalEntityRepository
import org.eclipse.tractusx.bpdm.gate.repository.SiteRepository
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
class SitePersistenceService(
private val siteRepository: SiteRepository,
//private val gateLegalEntityRepository: GateLegalEntityRepository
private val legalEntityRepository: LegalEntityRepository
) {

@Transactional
Expand All @@ -45,8 +47,13 @@ class SitePersistenceService(

// for (legalEntity in legalEntities) {
sites.forEach { site ->
//if (legalEntity.bpn == site.legalEntityExternalId) {
val fullSite = site.toSiteGate() //TODO (Needs to receive an Legal Entity)

val legalEntityRecord =
site.legalEntityExternalId.let {
legalEntityRepository.findByExternalId(site.legalEntityExternalId) ?: throw BpdmNotFoundException("Business Partner", it)
}

val fullSite = site.toSiteGate(legalEntityRecord) //TODO (Needs to receive an Legal Entity)
siteRecord.find { it.externalId == site.externalId }?.let { existingSite ->
updateSite(existingSite, fullSite)
siteRepository.save(existingSite)
Expand All @@ -64,8 +71,7 @@ class SitePersistenceService(
site.bpn = updatedSite.bpn
site.name = updatedSite.name
site.externalId = updatedSite.externalId
site.legalEntityExternalId = updatedSite.legalEntityExternalId
//TODO (Needs LegalEntity Logic)
site.legalEntity = updatedSite.legalEntity
site.states.replace(updatedSite.states)

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ class SiteService(
*/
fun upsertSites(sites: Collection<SiteGateInputRequest>) {

val sitesSaas = toSaasModels(sites)
saasClient.upsertSites(sitesSaas)
// val sitesSaas = toSaasModels(sites)
// saasClient.upsertSites(sitesSaas)

sitePersistenceService.persistSitesBP(sites)

Expand All @@ -147,9 +147,9 @@ class SiteService(
changelogRepository.save(ChangelogEntry(site.externalId, LsaType.Site))
}

deleteParentRelationsOfSites(sites)

upsertParentRelations(sites)
// deleteParentRelationsOfSites(sites)
//
// upsertParentRelations(sites)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ create table sites (
uuid uuid not null,
bpn varchar(255) not null,
external_id varchar(255) not null,
legal_entity_external_id varchar(255),
name varchar(255) not null,
primary key (id)
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
ALTER TABLE IF EXISTS logistic_addresses ADD site_id int8 NULL;

ALTER TABLE IF EXISTS logistic_addresses
ADD CONSTRAINT FK_SITE_ON_ADDRESS FOREIGN KEY (site_id) REFERENCES sites (id);


ALTER TABLE IF EXISTS sites ADD legal_entity_id int8 NOT NULL;

ALTER TABLE IF EXISTS sites
ADD CONSTRAINT FK_LEGAL_ENTITY_ON_SITES FOREIGN KEY (legal_entity_id) REFERENCES legal_entities (id);


ALTER TABLE IF EXISTS sites ADD main_address_id int8 NOT NULL;

ALTER TABLE IF EXISTS sites
ADD CONSTRAINT FK_MAIN_ADDRESS_ON_SITES FOREIGN KEY (main_address_id) REFERENCES logistic_addresses (id);

0 comments on commit 67ae1d8

Please sign in to comment.