Skip to content

Commit

Permalink
feat(gate) - Added Parent references for Generic related to LSA Types…
Browse files Browse the repository at this point in the history
…, Optional Changelog BusinessType and Address Type mapping for Business Partner Type. Input and Output LSA Logic adaptation for generic
  • Loading branch information
alexsilva-CGI committed Sep 25, 2023
1 parent 69f08ea commit cf04062
Show file tree
Hide file tree
Showing 17 changed files with 779 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,11 @@ interface IBaseBusinessPartnerDto {

@get:Schema(description = "BPNA")
val bpnA: String?

@get:Schema(description = "Parent ID used for association")
val parentId: String?

@get:Schema(description = "Parent Type (Legal Entity or Site)")
val parentType: BusinessPartnerType?

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
package org.eclipse.tractusx.bpdm.gate.api.model.request

import io.swagger.v3.oas.annotations.media.Schema
import org.eclipse.tractusx.bpdm.common.dto.BusinessPartnerIdentifierDto
import org.eclipse.tractusx.bpdm.common.dto.BusinessPartnerRole
import org.eclipse.tractusx.bpdm.common.dto.BusinessPartnerStateDto
import org.eclipse.tractusx.bpdm.common.dto.ClassificationDto
import org.eclipse.tractusx.bpdm.common.dto.*
import org.eclipse.tractusx.bpdm.gate.api.model.BusinessPartnerPostalAddressInputDto
import org.eclipse.tractusx.bpdm.gate.api.model.IBaseBusinessPartnerGateDto

Expand All @@ -44,6 +41,8 @@ data class BusinessPartnerInputRequest(
override val isOwner: Boolean = false,
override val bpnL: String? = null,
override val bpnS: String? = null,
override val bpnA: String? = null
override val bpnA: String? = null,
override val parentId: String? = null,
override val parentType: BusinessPartnerType? = null

) : IBaseBusinessPartnerGateDto
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*******************************************************************************
* 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.api.model.request

import io.swagger.v3.oas.annotations.media.Schema
import org.eclipse.tractusx.bpdm.common.dto.*
import org.eclipse.tractusx.bpdm.gate.api.model.BusinessPartnerPostalAddressInputDto
import org.eclipse.tractusx.bpdm.gate.api.model.IBaseBusinessPartnerGateDto


@Schema(
description = "Generic business partner with external id",
requiredProperties = ["externalId", "postalAddress"]
)
data class BusinessPartnerOutputRequest(

override val externalId: String,
override val nameParts: List<String> = emptyList(),
override val shortName: String? = null,
override val identifiers: Collection<BusinessPartnerIdentifierDto> = emptyList(),
override val legalForm: String? = null,
override val states: Collection<BusinessPartnerStateDto> = emptyList(),
override val classifications: Collection<ClassificationDto> = emptyList(),
override val roles: Collection<BusinessPartnerRole> = emptyList(),
override val postalAddress: BusinessPartnerPostalAddressInputDto,
override val isOwner: Boolean = false,
override val bpnL: String? = null,
override val bpnS: String? = null,
override val bpnA: String? = null,
override val parentId: String? = null,
override val parentType: BusinessPartnerType? = null

) : IBaseBusinessPartnerGateDto
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
package org.eclipse.tractusx.bpdm.gate.api.model.response

import io.swagger.v3.oas.annotations.media.Schema
import org.eclipse.tractusx.bpdm.common.dto.BusinessPartnerIdentifierDto
import org.eclipse.tractusx.bpdm.common.dto.BusinessPartnerRole
import org.eclipse.tractusx.bpdm.common.dto.BusinessPartnerStateDto
import org.eclipse.tractusx.bpdm.common.dto.ClassificationDto
import org.eclipse.tractusx.bpdm.common.dto.*
import org.eclipse.tractusx.bpdm.common.dto.openapidescription.CommonDescription
import org.eclipse.tractusx.bpdm.gate.api.model.BusinessPartnerPostalAddressInputDto
import org.eclipse.tractusx.bpdm.gate.api.model.IBaseBusinessPartnerGateDto
Expand All @@ -44,6 +41,8 @@ data class BusinessPartnerInputDto(
override val bpnL: String?,
override val bpnS: String?,
override val bpnA: String?,
override val parentId: String? = null,
override val parentType: BusinessPartnerType? = null,

@get:Schema(description = CommonDescription.createdAt)
val createdAt: Instant,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
package org.eclipse.tractusx.bpdm.gate.api.model.response

import io.swagger.v3.oas.annotations.media.Schema
import org.eclipse.tractusx.bpdm.common.dto.BusinessPartnerIdentifierDto
import org.eclipse.tractusx.bpdm.common.dto.BusinessPartnerRole
import org.eclipse.tractusx.bpdm.common.dto.BusinessPartnerStateDto
import org.eclipse.tractusx.bpdm.common.dto.ClassificationDto
import org.eclipse.tractusx.bpdm.common.dto.*
import org.eclipse.tractusx.bpdm.common.dto.openapidescription.CommonDescription
import org.eclipse.tractusx.bpdm.gate.api.model.BusinessPartnerPostalAddressOutputDto
import org.eclipse.tractusx.bpdm.gate.api.model.IBaseBusinessPartnerGateDto
Expand All @@ -44,6 +41,8 @@ data class BusinessPartnerOutputDto(
override val bpnL: String,
override val bpnS: String?,
override val bpnA: String,
override val parentId: String? = null,
override val parentType: BusinessPartnerType? = null,

@get:Schema(description = CommonDescription.createdAt)
val createdAt: Instant,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ data class ChangelogGateDto(
val externalId: String,

@get:Schema(description = ChangelogDescription.businessPartnerType)
val businessPartnerType: BusinessPartnerType,
val businessPartnerType: BusinessPartnerType?,

@get:Schema(description = ChangelogDescription.timestamp)
val timestamp: Instant,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ChangelogEntry(

@Enumerated(EnumType.STRING)
@Column(name = "business_partner_type", nullable = false, updatable = false)
val businessPartnerType: BusinessPartnerType,
val businessPartnerType: BusinessPartnerType?,

@Enumerated(EnumType.STRING)
@Column(name = "changelog_type", nullable = false, updatable = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package org.eclipse.tractusx.bpdm.gate.entity.generic

import jakarta.persistence.*
import org.eclipse.tractusx.bpdm.common.dto.BusinessPartnerRole
import org.eclipse.tractusx.bpdm.common.dto.BusinessPartnerType
import org.eclipse.tractusx.bpdm.common.model.BaseEntity
import org.eclipse.tractusx.bpdm.common.model.StageType
import java.util.*
Expand Down Expand Up @@ -79,6 +80,13 @@ class BusinessPartner(

@Column(name = "stage")
@Enumerated(EnumType.STRING)
var stage: StageType
var stage: StageType,

@Column(name = "parent_id")
var parentId: String?,

@Column(name = "parent_type")
@Enumerated(EnumType.STRING)
var parentType: BusinessPartnerType?

) : BaseEntity()
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ interface BusinessPartnerRepository : JpaRepository<BusinessPartner, Long>, Crud

fun findByStageAndExternalIdIn(stage: StageType, externalId: Collection<String>): Set<BusinessPartner>

fun findByStageAndExternalId(stage: StageType, externalId: String?): BusinessPartner?

fun findByStageAndExternalIdIn(stage: StageType, externalId: Collection<String>, pageable: Pageable): Page<BusinessPartner>

fun findByStage(stage: StageType, pageable: Pageable): Page<BusinessPartner>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,33 @@

package org.eclipse.tractusx.bpdm.gate.service

import org.eclipse.tractusx.bpdm.common.dto.AddressType
import org.eclipse.tractusx.bpdm.common.dto.BusinessPartnerType
import org.eclipse.tractusx.bpdm.common.dto.response.PageDto
import org.eclipse.tractusx.bpdm.common.exception.BpdmNotFoundException
import org.eclipse.tractusx.bpdm.common.model.StageType
import org.eclipse.tractusx.bpdm.gate.api.model.request.AddressGateInputRequest
import org.eclipse.tractusx.bpdm.gate.api.model.request.AddressGateOutputRequest
import org.eclipse.tractusx.bpdm.gate.api.model.request.BusinessPartnerInputRequest
import org.eclipse.tractusx.bpdm.gate.api.model.request.BusinessPartnerOutputRequest
import org.eclipse.tractusx.bpdm.gate.api.model.response.AddressGateInputDto
import org.eclipse.tractusx.bpdm.gate.api.model.response.AddressGateOutputDto
import org.eclipse.tractusx.bpdm.gate.entity.LogisticAddress
import org.eclipse.tractusx.bpdm.gate.repository.GateAddressRepository
import org.eclipse.tractusx.bpdm.gate.repository.generic.BusinessPartnerRepository
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageRequest
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Service
import org.springframework.web.server.ResponseStatusException

@Service
class AddressService(
private val addressPersistenceService: AddressPersistenceService,
private val addressRepository: GateAddressRepository,
private val sharingStateService: SharingStateService
private val sharingStateService: SharingStateService,
private val businessPartnerService: BusinessPartnerService,
private val businessPartnerRepository: BusinessPartnerRepository
) {

fun getAddresses(page: Int, size: Int, externalIds: Collection<String>? = null): PageDto<AddressGateInputDto> {
Expand Down Expand Up @@ -102,14 +111,103 @@ class AddressService(
* Upsert addresses input to the database
**/
fun upsertAddresses(addresses: Collection<AddressGateInputRequest>) {
addressPersistenceService.persistAddressBP(addresses, StageType.Input)

val mappedGBP: MutableCollection<BusinessPartnerInputRequest> = mutableListOf()

addresses.forEach { address ->

val duplicateBP = businessPartnerRepository.findByStageAndExternalId(StageType.Input, address.externalId)

if (duplicateBP != null && (duplicateBP.postalAddress.addressType != AddressType.AdditionalAddress || duplicateBP.postalAddress.addressType != AddressType.LegalAndSiteMainAddress)) {
throw ResponseStatusException(HttpStatus.BAD_REQUEST, "There is already a BP with same ID!")
}

if (address.siteExternalId != null) {

val mapBusinessPartner = address.toBusinessPartnerDto(address.siteExternalId, BusinessPartnerType.SITE)

val relatedLE = businessPartnerRepository.findByStageAndExternalId(StageType.Input, mapBusinessPartner.parentId)
if (relatedLE == null || relatedLE.postalAddress.addressType != AddressType.SiteMainAddress) {
throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Related Input Site doesn't exist")
}

mappedGBP.add(mapBusinessPartner)

} else if (address.legalEntityExternalId != null) {

val mapBusinessPartner = address.toBusinessPartnerDto(address.legalEntityExternalId, BusinessPartnerType.LEGAL_ENTITY)

val relatedLE = businessPartnerRepository.findByStageAndExternalId(StageType.Input, mapBusinessPartner.parentId)
if (relatedLE == null || relatedLE.postalAddress.addressType != AddressType.LegalAddress) {
throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Related Input Legal Entity doesn't exist")
}

mappedGBP.add(mapBusinessPartner)

}
}

businessPartnerService.upsertBusinessPartnersInput(mappedGBP)


//addressPersistenceService.persistAddressBP(addresses, StageType.Input)

}

/**
* Upsert addresses output to the database
**/
fun upsertOutputAddresses(addresses: Collection<AddressGateOutputRequest>) {
addressPersistenceService.persistOutputAddressBP(addresses, StageType.Output)

val mappedGBP: MutableCollection<BusinessPartnerOutputRequest> = mutableListOf()

addresses.forEach { address ->

val relatedAddress = businessPartnerRepository.findByStageAndExternalId(StageType.Input, address.externalId)
if (relatedAddress == null || relatedAddress.postalAddress.addressType != AddressType.AdditionalAddress) {
throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Related Input Address doesn't exist")
}

val duplicateBP = businessPartnerRepository.findByStageAndExternalId(StageType.Output, address.externalId)
if (duplicateBP != null && (duplicateBP.postalAddress.addressType != AddressType.AdditionalAddress || duplicateBP.postalAddress.addressType != AddressType.LegalAndSiteMainAddress)) {
throw ResponseStatusException(HttpStatus.BAD_REQUEST, "There is already a BP with same ID!")
}

if (address.siteExternalId != null) {

val mapBusinessPartner = address.toBusinessPartnerOutputDto(address.siteExternalId, BusinessPartnerType.SITE)

if (relatedAddress.parentType != mapBusinessPartner.parentType) {
throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Related Input Address doesn't have site relationship")
}

val relatedLE = businessPartnerRepository.findByStageAndExternalId(StageType.Input, mapBusinessPartner.parentId)
if (relatedLE == null || relatedLE.postalAddress.addressType != AddressType.SiteMainAddress) {
throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Related Input Site doesn't exist")
}

mappedGBP.add(mapBusinessPartner)

} else if (address.legalEntityExternalId != null) {

val mapBusinessPartner = address.toBusinessPartnerOutputDto(address.legalEntityExternalId, BusinessPartnerType.LEGAL_ENTITY)

if (relatedAddress.parentType != mapBusinessPartner.parentType) {
throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Related Input Address doesn't have Legal Entity relationship")
}

val relatedLE = businessPartnerRepository.findByStageAndExternalId(StageType.Input, mapBusinessPartner.parentId)
if (relatedLE == null || relatedLE.postalAddress.addressType != AddressType.LegalAddress) {
throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Related Input Legal Entity doesn't exist")
}

mappedGBP.add(mapBusinessPartner)

}
}

businessPartnerService.upsertBusinessPartnersOutput(mappedGBP)

}

}
Loading

0 comments on commit cf04062

Please sign in to comment.