Skip to content

Commit

Permalink
feat(gate): Request Cleaning for Own Business Partner Update,Persist …
Browse files Browse the repository at this point in the history
…cleaning task identifier and associate with business partner in the SharingStateDto and update sharing state of business partner to Pending
  • Loading branch information
cezaralexandremorais committed Oct 17, 2023
1 parent 17f68ee commit 6556aa0
Show file tree
Hide file tree
Showing 13 changed files with 442 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,8 @@ data class SharingStateDto(
val bpn: String? = null,

@get:Schema(description = "The date and time when the sharing process was started.")
val sharingProcessStarted: LocalDateTime? = null
val sharingProcessStarted: LocalDateTime? = null,

@get:Schema(description = "The orchestrator task identifier that was created")
val taskId: String? = null,
)
4 changes: 4 additions & 0 deletions bpdm-gate/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.tractusx</groupId>
<artifactId>bpdm-orchestrator-api</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ import org.springframework.boot.context.properties.ConfigurationProperties
data class BpnConfigProperties(
val agencyName: String = "Catena-X",
var name: String = "Business Partner Number",
val id: String = "CX_BPN"
val id: String = "CX_BPN",
val ownerBpnL: String? = null
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*******************************************************************************
* 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.config

import org.eclipse.tractusx.orchestrator.api.client.OrchestrationApiClient
import org.eclipse.tractusx.orchestrator.api.client.OrchestrationApiClientImpl
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.HttpHeaders
import org.springframework.http.MediaType
import org.springframework.web.reactive.function.client.WebClient


@Configuration
class OrchestratorClientConfig {

// Orchestrator-Client without authentication
@Bean
@ConditionalOnProperty(
value = ["bpdm.orchestrator.security-enabled"],
havingValue = "false",
matchIfMissing = true
)
fun orchestratorClientNoAuth(orchestratorConfigProperties: OrchestratorConfigProperties): OrchestrationApiClient {
val url = orchestratorConfigProperties.baseUrl
return OrchestrationApiClientImpl { webClientBuilder(url).build() }
}

private fun webClientBuilder(url: String) =
WebClient.builder()
.baseUrl(url)
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*******************************************************************************
* 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.config

import org.springframework.boot.context.properties.ConfigurationProperties


@ConfigurationProperties(prefix = "bpdm.orchestrator")
data class OrchestratorConfigProperties(
val baseUrl: String = "http://localhost:8085",
)
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ class SharingState(
var bpn: String? = null,

@Column(name = "sharing_process_started", nullable = true)
var sharingProcessStarted: LocalDateTime? = null
var sharingProcessStarted: LocalDateTime? = null,

@Column(name = "task_id", nullable = true)
var taskId: String? = null

) : BaseEntity()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ class BusinessPartnerMappings {
door = dto.door
)


private fun toAlternativePostalAddressDto(entity: AlternativePostalAddress) =
AlternativePostalAddressGateDto(
geographicCoordinates = entity.geographicCoordinates?.let(::toGeoCoordinateDto),
Expand All @@ -211,6 +212,7 @@ class BusinessPartnerMappings {
deliveryServiceNumber = dto.deliveryServiceNumber
)


private fun toStreetDto(entity: Street) =
StreetGateDto(
name = entity.name,
Expand All @@ -223,6 +225,7 @@ class BusinessPartnerMappings {
additionalNameSuffix = entity.additionalNameSuffix
)


private fun toStreet(dto: StreetGateDto) =
Street(
name = dto.name,
Expand Down Expand Up @@ -256,6 +259,9 @@ class BusinessPartnerMappings {
private fun toGeoCoordinateDto(entity: GeographicCoordinate) =
GeoCoordinateDto(latitude = entity.latitude, longitude = entity.longitude, altitude = entity.altitude)


private fun toGeographicCoordinate(dto: GeoCoordinateDto) =
GeographicCoordinate(latitude = dto.latitude, longitude = dto.longitude, altitude = dto.altitude)


}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import org.eclipse.tractusx.bpdm.gate.entity.generic.*
import org.eclipse.tractusx.bpdm.gate.exception.BpdmMissingStageException
import org.eclipse.tractusx.bpdm.gate.repository.ChangelogRepository
import org.eclipse.tractusx.bpdm.gate.repository.generic.BusinessPartnerRepository
import org.eclipse.tractusx.orchestrator.api.client.OrchestrationApiClient
import org.eclipse.tractusx.orchestrator.api.model.*
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageRequest
import org.springframework.stereotype.Service
Expand All @@ -46,7 +48,9 @@ class BusinessPartnerService(
private val businessPartnerRepository: BusinessPartnerRepository,
private val businessPartnerMappings: BusinessPartnerMappings,
private val sharingStateService: SharingStateService,
private val changelogRepository: ChangelogRepository
private val changelogRepository: ChangelogRepository,
private val orchestrationApiClient: OrchestrationApiClient,
private val orchestratorMappings: OrchestratorMappings
) {

@Transactional
Expand Down Expand Up @@ -79,7 +83,16 @@ class BusinessPartnerService(
saveChangelog(resolutionResults)

val partners = resolutionResults.map { it.businessPartner }
partners.forEach { entity -> initSharingState(entity) }
val orchestratorBusinessPartnersDto = resolutionResults.map { orchestratorMappings.toBusinessPartnerGenericDto(it.businessPartner) }
partners.forEach { entity ->
initSharingState(entity)
}

val taskCreateResponse = requestNewCleaning(orchestratorBusinessPartnersDto)

for (i in partners.indices) {
updateSharingState(partners[i], taskCreateResponse.createdTasks[i])
}

return businessPartnerRepository.saveAll(partners)
}
Expand Down Expand Up @@ -209,6 +222,24 @@ class BusinessPartnerService(
}


private fun requestNewCleaning(orchestratorBusinessPartnersDto: List<BusinessPartnerGenericDto>): TaskCreateResponse {
return orchestrationApiClient.goldenRecordTasks.createTasks(
TaskCreateRequest(
TaskMode.UpdateFromSharingMember, orchestratorBusinessPartnersDto
)
)
}

private fun updateSharingState(entity: BusinessPartner, stateDto: TaskClientStateDto) {
sharingStateService.upsertSharingState(
SharingStateDto(
BusinessPartnerType.ADDRESS,
entity.externalId,
sharingStateType = orchestratorMappings.toSharingStateType(stateDto.processingState.resultState),
taskId = stateDto.taskId
)
)

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*******************************************************************************
* 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.service

import mu.KotlinLogging
import org.eclipse.tractusx.bpdm.common.dto.BusinessPartnerIdentifierDto
import org.eclipse.tractusx.bpdm.common.dto.BusinessPartnerStateDto
import org.eclipse.tractusx.bpdm.common.dto.ClassificationDto
import org.eclipse.tractusx.bpdm.common.dto.GeoCoordinateDto
import org.eclipse.tractusx.bpdm.gate.api.model.SharingStateType
import org.eclipse.tractusx.bpdm.gate.config.BpnConfigProperties
import org.eclipse.tractusx.bpdm.gate.entity.AlternativePostalAddress
import org.eclipse.tractusx.bpdm.gate.entity.GeographicCoordinate
import org.eclipse.tractusx.bpdm.gate.entity.PhysicalPostalAddress
import org.eclipse.tractusx.bpdm.gate.entity.Street
import org.eclipse.tractusx.bpdm.gate.entity.generic.*
import org.eclipse.tractusx.orchestrator.api.model.*
import org.springframework.stereotype.Service

@Service
class OrchestratorMappings(
private val bpnConfigProperties: BpnConfigProperties
) {
private val logger = KotlinLogging.logger { }
fun toBusinessPartnerGenericDto(entity: BusinessPartner) = BusinessPartnerGenericDto(
nameParts = entity.nameParts,
shortName = entity.shortName,
identifiers = entity.identifiers.map { toIdentifierDto(it) },
legalForm = entity.legalForm,
states = entity.states.map { toStateDto(it) },
classifications = entity.classifications.map { toClassificationDto(it) },
roles = entity.roles,
postalAddress = toBusinessPartnerGenericPostalAddressDto(entity.postalAddress),
bpnL = entity.bpnL,
bpnS = entity.bpnS,
bpnA = entity.bpnA,
ownerBpnL = getOwnerBpnL(entity)
)

private fun toClassificationDto(entity: Classification) =
ClassificationDto(type = entity.type, code = entity.code, value = entity.value)

private fun toBusinessPartnerGenericPostalAddressDto(entity: PostalAddress) =
PostalAddressDto(
addressType = entity.addressType,
physicalPostalAddress = entity.physicalPostalAddress?.let(::toBusinessPartnerGenericPhysicalPostalAddressDto),
alternativePostalAddress = entity.alternativePostalAddress?.let(::toBusinessPartnerGenericAlternativePostalAddressDto)
)

private fun toBusinessPartnerGenericPhysicalPostalAddressDto(entity: PhysicalPostalAddress) =
PhysicalPostalAddressDto(
geographicCoordinates = entity.geographicCoordinates?.let(::toBusinessPartnerGenericGeoCoordinateDto),
country = entity.country,
administrativeAreaLevel1 = entity.administrativeAreaLevel1,
administrativeAreaLevel2 = entity.administrativeAreaLevel2,
administrativeAreaLevel3 = entity.administrativeAreaLevel3,
postalCode = entity.postalCode,
city = entity.city,
district = entity.district,
street = entity.street?.let(::toGenericBusinessPartnerStreetDto),
companyPostalCode = entity.companyPostalCode,
industrialZone = entity.industrialZone,
building = entity.building,
floor = entity.floor,
door = entity.door
)

private fun toBusinessPartnerGenericAlternativePostalAddressDto(entity: AlternativePostalAddress): AlternativePostalAddressDto =
AlternativePostalAddressDto(
geographicCoordinates = entity.geographicCoordinates?.let(::toBusinessPartnerGenericGeoCoordinateDto),
country = entity.country,
administrativeAreaLevel1 = entity.administrativeAreaLevel1,
postalCode = entity.postalCode,
city = entity.city,
deliveryServiceType = entity.deliveryServiceType,
deliveryServiceQualifier = entity.deliveryServiceQualifier,
deliveryServiceNumber = entity.deliveryServiceNumber
)

private fun toGenericBusinessPartnerStreetDto(entity: Street) =
StreetDto(
name = entity.name,
houseNumber = entity.houseNumber,
milestone = entity.milestone,
direction = entity.direction,
namePrefix = entity.namePrefix,
additionalNamePrefix = entity.additionalNamePrefix,
nameSuffix = entity.nameSuffix,
additionalNameSuffix = entity.additionalNameSuffix
)

private fun toStateDto(entity: State) =
BusinessPartnerStateDto(type = entity.type, validFrom = entity.validFrom, validTo = entity.validTo, description = entity.description)

private fun toIdentifierDto(entity: Identifier) =
BusinessPartnerIdentifierDto(type = entity.type, value = entity.value, issuingBody = entity.issuingBody)

private fun toBusinessPartnerGenericGeoCoordinateDto(entity: GeographicCoordinate) =
GeoCoordinateDto(latitude = entity.latitude, longitude = entity.longitude, altitude = entity.altitude)

private fun getOwnerBpnL(entity: BusinessPartner): String? {
return if (entity.isOwnCompanyData) bpnConfigProperties.ownerBpnL else {
logger.warn { "Owner BPNL property is not configured" }
null
}
}

fun toSharingStateType(resultState: ResultState) = when (resultState) {
ResultState.Pending -> SharingStateType.Pending
ResultState.Success -> SharingStateType.Success
ResultState.Error -> SharingStateType.Error
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ class SharingStateService(private val stateRepository: SharingStateRepository) {
sharingErrorCode = dto.sharingErrorCode,
sharingErrorMessage = dto.sharingErrorMessage,
bpn = dto.bpn,
sharingProcessStarted = dto.sharingProcessStarted
sharingProcessStarted = dto.sharingProcessStarted,
taskId = dto.taskId
)
)
}
Expand All @@ -65,10 +66,10 @@ class SharingStateService(private val stateRepository: SharingStateRepository) {
entity.sharingErrorCode = dto.sharingErrorCode
entity.sharingErrorMessage = dto.sharingErrorMessage
entity.bpn = dto.bpn
entity.taskId = dto.taskId
if (dto.sharingProcessStarted != null) {
entity.sharingProcessStarted = dto.sharingProcessStarted
}

this.stateRepository.save(entity)
}

Expand All @@ -90,7 +91,8 @@ class SharingStateService(private val stateRepository: SharingStateRepository) {
sharingErrorCode = it.sharingErrorCode,
sharingErrorMessage = it.sharingErrorMessage,
bpn = it.bpn,
sharingProcessStarted = it.sharingProcessStarted
sharingProcessStarted = it.sharingProcessStarted,
taskId = it.taskId
)
})

Expand Down
Loading

0 comments on commit 6556aa0

Please sign in to comment.