Skip to content

Commit

Permalink
Merge pull request #540 from catenax-ng/feat/resolve_finished_cleanin…
Browse files Browse the repository at this point in the history
…g_task

Feat (Gate) - Resolve Finished Cleaning Task
  • Loading branch information
nicoprow authored Oct 19, 2023
2 parents 8248532 + c6edfe5 commit 1c7a84c
Show file tree
Hide file tree
Showing 10 changed files with 581 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ package org.eclipse.tractusx.bpdm.gate
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.context.properties.ConfigurationPropertiesScan
import org.springframework.boot.runApplication
import org.springframework.scheduling.annotation.EnableScheduling

@EnableScheduling
@SpringBootApplication
@ConfigurationPropertiesScan
class Application
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.eclipse.tractusx.bpdm.gate.repository

import org.eclipse.tractusx.bpdm.common.dto.BusinessPartnerType
import org.eclipse.tractusx.bpdm.gate.api.model.SharingStateType
import org.eclipse.tractusx.bpdm.gate.entity.SharingState
import org.springframework.data.jpa.domain.Specification
import org.springframework.data.jpa.repository.JpaSpecificationExecutor
Expand Down Expand Up @@ -52,4 +53,6 @@ interface SharingStateRepository : PagingAndSortingRepository<SharingState, Long

fun findByExternalIdAndBusinessPartnerType(externalId: String, businessPartnerType: BusinessPartnerType): SharingState?

fun findBySharingStateType(sharingStateType: SharingStateType): Set<SharingState>

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import org.eclipse.tractusx.bpdm.common.model.StageType
import org.eclipse.tractusx.bpdm.common.service.toPageDto
import org.eclipse.tractusx.bpdm.common.util.copyAndSync
import org.eclipse.tractusx.bpdm.common.util.replace
import org.eclipse.tractusx.bpdm.gate.api.exception.BusinessPartnerSharingError
import org.eclipse.tractusx.bpdm.gate.api.model.ChangelogType
import org.eclipse.tractusx.bpdm.gate.api.model.SharingStateType
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.BusinessPartnerInputDto
Expand All @@ -35,11 +37,13 @@ import org.eclipse.tractusx.bpdm.gate.entity.ChangelogEntry
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.SharingStateRepository
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.scheduling.annotation.Scheduled
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

Expand All @@ -50,7 +54,8 @@ class BusinessPartnerService(
private val sharingStateService: SharingStateService,
private val changelogRepository: ChangelogRepository,
private val orchestrationApiClient: OrchestrationApiClient,
private val orchestratorMappings: OrchestratorMappings
private val orchestratorMappings: OrchestratorMappings,
private val sharingStateRepository: SharingStateRepository
) {

@Transactional
Expand Down Expand Up @@ -91,7 +96,7 @@ class BusinessPartnerService(
val taskCreateResponse = createGoldenRecordTasks(orchestratorBusinessPartnersDto)

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

return businessPartnerRepository.saveAll(partners)
Expand Down Expand Up @@ -230,16 +235,59 @@ class BusinessPartnerService(
)
}

private fun updateSharingState(entity: BusinessPartner, stateDto: TaskClientStateDto) {
private fun updateSharingState(externalId: String, stateDto: TaskClientStateDto) {

val errorMessage = if (stateDto.processingState.errors.isNotEmpty()) stateDto.processingState.errors.joinToString(" // ") { it.description } else null
val errorCode = if (stateDto.processingState.errors.isNotEmpty()) BusinessPartnerSharingError.SharingProcessError else null

sharingStateService.upsertSharingState(
SharingStateDto(
BusinessPartnerType.ADDRESS,
entity.externalId,
externalId,
sharingStateType = orchestratorMappings.toSharingStateType(stateDto.processingState.resultState),
sharingErrorCode = errorCode,
sharingErrorMessage = errorMessage,
taskId = stateDto.taskId
)
)
}

@Scheduled(cron = "\${cleaningService.pollingCron:-}", zone = "UTC")
fun finishCleaningTask() {

var validBusinessPartner: List<BusinessPartner> = emptyList()

val sharingStates = sharingStateRepository.findBySharingStateType(SharingStateType.Pending)
val nonNullTaskIds = sharingStates.mapNotNull { it.taskId }

val taskStates = orchestrationApiClient.goldenRecordTasks.searchTaskStates(TaskStateRequest(nonNullTaskIds))

val sharingStateMap = sharingStates.associateBy { it.taskId }

//Task verification and map
taskStates.tasks.forEach { task ->

val relatedSharingState = sharingStateMap[task.taskId]

// Check if relatedSharingState exists
if (relatedSharingState != null) {
if (task.processingState.resultState == ResultState.Success) {
val businessPartner = orchestratorMappings.toBusinessPartner(task.businessPartnerResult!!, relatedSharingState.externalId)
validBusinessPartner = validBusinessPartner.plus(businessPartner)

// Set Sharing State to Success
updateSharingState(businessPartner.externalId, task)
} else if (task.processingState.resultState == ResultState.Error) {
// Set related Sharing State Type as Error
updateSharingState(relatedSharingState.externalId, task)
}
}
}

//If it is cleaned, upsert Output
if (validBusinessPartner.isNotEmpty()) {
upsertBusinessPartnersOutput(validBusinessPartner)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ 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.common.model.StageType
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
Expand Down Expand Up @@ -127,4 +128,82 @@ class OrchestratorMappings(
ResultState.Success -> SharingStateType.Success
ResultState.Error -> SharingStateType.Error
}

//Mapping BusinessPartnerGenericDto from to BusinessPartner
fun toBusinessPartner(entity: BusinessPartnerGenericDto, externalId: String) = BusinessPartner(
externalId = externalId,
nameParts = entity.nameParts.toMutableList(),
shortName = entity.shortName,
identifiers = entity.identifiers.map { toIdentifier(it) }.toSortedSet(),
legalForm = entity.legalForm,
states = entity.states.map { toState(it) }.toSortedSet(),
classifications = entity.classifications.map { toClassification(it) }.toSortedSet(),
roles = entity.roles.toSortedSet(),
postalAddress = toPostalAddress(entity.postalAddress),
bpnL = entity.bpnL,
bpnS = entity.bpnS,
bpnA = entity.bpnA,
stage = StageType.Output
)

private fun toIdentifier(dto: BusinessPartnerIdentifierDto) =
Identifier(type = dto.type, value = dto.value, issuingBody = dto.issuingBody)

private fun toState(dto: BusinessPartnerStateDto) =
State(type = dto.type, validFrom = dto.validFrom, validTo = dto.validTo, description = dto.description)

private fun toClassification(dto: ClassificationDto) =
Classification(type = dto.type, code = dto.code, value = dto.value)

private fun toPostalAddress(entity: PostalAddressDto) =
PostalAddress(
addressType = entity.addressType,
physicalPostalAddress = entity.physicalPostalAddress?.let(::toPhysicalPostalAddress),
alternativePostalAddress = entity.alternativePostalAddress?.let(this::toAlternativePostalAddress)
)

private fun toPhysicalPostalAddress(dto: PhysicalPostalAddressDto) =
PhysicalPostalAddress(
geographicCoordinates = dto.geographicCoordinates?.let(::toGeographicCoordinate),
country = dto.country,
administrativeAreaLevel1 = dto.administrativeAreaLevel1,
administrativeAreaLevel2 = dto.administrativeAreaLevel2,
administrativeAreaLevel3 = dto.administrativeAreaLevel3,
postalCode = dto.postalCode,
city = dto.city,
district = dto.district,
street = dto.street?.let(::toStreet),
companyPostalCode = dto.companyPostalCode,
industrialZone = dto.industrialZone,
building = dto.building,
floor = dto.floor,
door = dto.door
)

private fun toAlternativePostalAddress(dto: AlternativePostalAddressDto) =
AlternativePostalAddress(
geographicCoordinates = dto.geographicCoordinates?.let(::toGeographicCoordinate),
country = dto.country,
administrativeAreaLevel1 = dto.administrativeAreaLevel1,
postalCode = dto.postalCode,
city = dto.city,
deliveryServiceType = dto.deliveryServiceType,
deliveryServiceQualifier = dto.deliveryServiceQualifier,
deliveryServiceNumber = dto.deliveryServiceNumber
)

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

private fun toGeographicCoordinate(dto: GeoCoordinateDto) =
GeographicCoordinate(latitude = dto.latitude, longitude = dto.longitude, altitude = dto.altitude)
}
2 changes: 2 additions & 0 deletions bpdm-gate/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ spring.jpa.properties.hibernate.order_inserts=true
#Flyway configuration
spring.flyway.enabled=true
spring.flyway.schemas=bpdmgate
#Cleaning Task Job Configurations
goldenRecordTask.pollingCron=-



Loading

0 comments on commit 1c7a84c

Please sign in to comment.