Skip to content

Commit

Permalink
feat(pool): eclipse-tractusx#432 Upsert Business Partners from Cleani…
Browse files Browse the repository at this point in the history
…ng Result

- upsert business partner
  • Loading branch information
rainer-exxcellent committed Oct 19, 2023
1 parent 8166ef5 commit 2192710
Show file tree
Hide file tree
Showing 11 changed files with 926 additions and 295 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,11 @@ import java.time.LocalDateTime
@Schema(description = SiteStateDescription.header)
data class SiteStateDto(

@get:Schema(description = SiteStateDescription.description)
val description: String?,
override val description: String?,

@get:Schema(description = SiteStateDescription.validFrom)
val validFrom: LocalDateTime?,
override val validFrom: LocalDateTime?,

@get:Schema(description = SiteStateDescription.validTo)
val validTo: LocalDateTime?,
override val validTo: LocalDateTime?,

@get:Schema(description = SiteStateDescription.type)
val type: BusinessStateType
)
override val type: BusinessStateType
) : IBaseSiteStateDto
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class OpenSearchSyncStarterService(
* @return true if index mapping changed, false otherwise
*/
private fun updateOnInit(indexDefinition: IndexDefinition): Boolean {
val indexAlreadyExists = openSearchClient.indices().exists { it.index(indexDefinition.indexName) }.value()
/* val indexAlreadyExists = openSearchClient.indices().exists { it.index(indexDefinition.indexName) }.value()
return if (!indexAlreadyExists) {
true
Expand All @@ -137,7 +137,8 @@ class OpenSearchSyncStarterService(
deleteIndexIfExists(tempIndexName)
requiredMappingMetadata != existingMappingMetadata
}
}*/
return true
}

private fun getIndexMappings(indexName: String): MappingMetadata {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*******************************************************************************
* 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.pool.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 CleaningServiceClientsConfig {

// Pool-Client without authentication
@Bean
@ConditionalOnProperty(
value = ["bpdm.pool.security-enabled"],
havingValue = "false",
matchIfMissing = true
)
fun poolClientNoAuth(configProperties: CleaningServiceConfigProperties): OrchestrationApiClient {
val url = configProperties.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,30 @@
/*******************************************************************************
* 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.pool.config

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


@ConfigurationProperties(prefix = "bpdm.pool")
data class CleaningServiceConfigProperties(
val baseUrl: String = "http://localhost:8080/",
val securityEnabled: Boolean = false,
val oauth2ClientRegistration: String?
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*******************************************************************************
* 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.pool.exception


class BpdmValidationException(message: String) : RuntimeException(message)
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ class BusinessPartnerBuildService(


val errorsByRequest = requestValidationService.validateLegalEntityCreates(requests.associateWith { it.legalEntity }) { theRequest -> theRequest.index }
val errorsByRequestAddress = requestValidationService.validateLegalEntityCreatesAddresses(requests.associateWith { it.legalAddress }) { theRequest -> theRequest.index }
val errorsByRequestAddress =
requestValidationService.validateLegalEntityCreatesAddresses(requests.associateWith { it.legalAddress }) { theRequest -> theRequest.index }

val errors = errorsByRequest.flatMap { it.value } + errorsByRequestAddress.flatMap { it.value }
val validRequests = requests.filterNot { errorsByRequest.containsKey(it) || errorsByRequestAddress.containsKey(it) }
Expand Down Expand Up @@ -350,7 +351,7 @@ class BusinessPartnerBuildService(
legalEntity = partner,
)

site.states.addAll(request.states.map { toEntity(it, site) })
site.states.addAll(request.states.map { toSiteState(it, site) })

return site
}
Expand All @@ -376,16 +377,16 @@ class BusinessPartnerBuildService(
partner.states.clear()
partner.classifications.clear()

partner.states.addAll(request.states.map { toEntity(it, partner) })
partner.identifiers.addAll(request.identifiers.map { toEntity(it, metadataMap, partner) })
partner.classifications.addAll(request.classifications.map { toEntity(it, partner) }.toSet())
partner.states.addAll(request.states.map { toLegalEntityState(it, partner) })
partner.identifiers.addAll(request.identifiers.map { toLegalEntityIdentifier(it, metadataMap.idTypes, partner) })
partner.classifications.addAll(request.classifications.map { toLegalEntityClassification(it, partner) }.toSet())
}

private fun updateSite(site: Site, request: SiteDto) {
site.name = request.name

site.states.clear()
site.states.addAll(request.states.map { toEntity(it, site) })
site.states.addAll(request.states.map { toSiteState(it, site) })
}

private fun createLogisticAddress(
Expand Down Expand Up @@ -430,11 +431,11 @@ class BusinessPartnerBuildService(

address.identifiers.apply {
clear()
addAll(dto.identifiers.map { toEntity(it, metadataMap.idTypes, address) })
addAll(dto.identifiers.map { toAddressIdentifier(it, metadataMap.idTypes, address) })
}
address.states.apply {
clear()
addAll(dto.states.map { toEntity(it, address) })
addAll(dto.states.map { toAddressState(it, address) })
}
}

Expand All @@ -457,7 +458,7 @@ class BusinessPartnerBuildService(
)
}

private fun createAlternativeAddress(alternativeAddress: AlternativePostalAddressDto, regions: Map<String, Region>): AlternativePostalAddress {
private fun createAlternativeAddress(alternativeAddress: AlternativePostalAddressDto, regions: Map<String, Region>): AlternativePostalAddress {
return AlternativePostalAddress(
geographicCoordinates = alternativeAddress.geographicCoordinates?.let { toEntity(it) },
country = alternativeAddress.country,
Expand All @@ -470,86 +471,10 @@ class BusinessPartnerBuildService(
)
}

private fun createStreet(dto: StreetDto): Street {
return Street(
name = dto.name,
houseNumber = dto.houseNumber,
milestone = dto.milestone,
direction = dto.direction
)
}

private fun toEntity(dto: LegalEntityStateDto, legalEntity: LegalEntity): LegalEntityState {
return LegalEntityState(
description = dto.description,
validFrom = dto.validFrom,
validTo = dto.validTo,
type = dto.type,
legalEntity = legalEntity
)
}

private fun toEntity(dto: SiteStateDto, site: Site): SiteState {
return SiteState(
description = dto.description,
validFrom = dto.validFrom,
validTo = dto.validTo,
type = dto.type,
site = site
)
}

private fun toEntity(dto: AddressStateDto, address: LogisticAddress): AddressState {
return AddressState(
description = dto.description,
validFrom = dto.validFrom,
validTo = dto.validTo,
type = dto.type,
address = address
)
}

private fun toEntity(dto: ClassificationDto, partner: LegalEntity): LegalEntityClassification {
return LegalEntityClassification(
value = dto.value,
code = dto.code,
type = dto.type,
legalEntity = partner
)
}

private fun toEntity(
dto: LegalEntityIdentifierDto,
metadataMap: LegalEntityMetadataMapping,
partner: LegalEntity
): LegalEntityIdentifier {
return LegalEntityIdentifier(
value = dto.value,
type = metadataMap.idTypes[dto.type]!!,
issuingBody = dto.issuingBody,
legalEntity = partner
)
}

private fun toEntity(
dto: AddressIdentifierDto,
idTypes: Map<String, IdentifierType>,
partner: LogisticAddress
): AddressIdentifier {
return AddressIdentifier(
value = dto.value,
type = idTypes[dto.type]!!,
address = partner
)
}

private fun toEntity(dto: GeoCoordinateDto): GeographicCoordinate {
fun toEntity(dto: GeoCoordinateDto): GeographicCoordinate {
return GeographicCoordinate(dto.latitude, dto.longitude, dto.altitude)
}

private fun createCurrentnessTimestamp(): Instant {
return Instant.now().truncatedTo(ChronoUnit.MICROS)
}

private fun LegalEntityMetadataDto.toMapping() =
LegalEntityMetadataMapping(
Expand All @@ -574,4 +499,85 @@ class BusinessPartnerBuildService(
val regions: Map<String, Region>
)

companion object {

fun createCurrentnessTimestamp(): Instant {
return Instant.now().truncatedTo(ChronoUnit.MICROS)
}

fun createStreet(dto: IBaseStreetDto): Street {
return Street(
name = dto.name,
houseNumber = dto.houseNumber,
milestone = dto.milestone,
direction = dto.direction
)
}

fun toLegalEntityState(dto: IBaseLegalEntityStateDto, legalEntity: LegalEntity): LegalEntityState {
return LegalEntityState(
description = dto.description,
validFrom = dto.validFrom,
validTo = dto.validTo,
type = dto.type,
legalEntity = legalEntity
)
}

fun toSiteState(dto: IBaseSiteStateDto, site: Site): SiteState {
return SiteState(
description = dto.description,
validFrom = dto.validFrom,
validTo = dto.validTo,
type = dto.type,
site = site
)
}

fun toAddressState(dto: IBaseAddressStateDto, address: LogisticAddress): AddressState {
return AddressState(
description = dto.description,
validFrom = dto.validFrom,
validTo = dto.validTo,
type = dto.type,
address = address
)
}

fun toLegalEntityClassification(dto: IBaseClassificationDto, partner: LegalEntity): LegalEntityClassification {
return LegalEntityClassification(
value = dto.value,
code = dto.code,
type = dto.type,
legalEntity = partner
)
}

fun toLegalEntityIdentifier(
dto: IBaseLegalEntityIdentifierDto,
idTypes: Map<String, IdentifierType>,
partner: LegalEntity
): LegalEntityIdentifier {
return LegalEntityIdentifier(
value = dto.value,
type = idTypes[dto.type]!!,
issuingBody = dto.issuingBody,
legalEntity = partner
)
}

fun toAddressIdentifier(
dto: IBaseAddressIdentifierDto,
idTypes: Map<String, IdentifierType>,
partner: LogisticAddress
): AddressIdentifier {
return AddressIdentifier(
value = dto.value,
type = idTypes[dto.type]!!,
address = partner
)
}

}

}
Loading

0 comments on commit 2192710

Please sign in to comment.