Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Cleaning Dummy): Duplication Check #1040

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ For changes to the BPDM Helm charts please consult the [changelog](charts/bpdm/C
- BPDM Pool & Gate: Reduce standard batch size for golden record task processing ([#1032](https://github.com/eclipse-tractusx/bpdm/pull/1032))

- BPDM Orchestrator: Fix possible out-of-memory exception during the execution of large volumes of tasks ([#1029](https://github.com/eclipse-tractusx/bpdm/pull/1029))
- BPDM Cleaning Service Dummy: Improve duplication check to better distinguish between incoming business partners

## [6.1.0] - [2024-07-15]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Service
import java.time.Instant
import java.time.temporal.ChronoUnit
import java.util.*

@Service
class CleaningServiceDummy(
Expand Down Expand Up @@ -101,16 +102,19 @@ class CleaningServiceDummy(
?: businessPartner.additionalAddress
?: PostalAddress.empty

val legalAddressBpnReference = addressToClean.bpnReference
.toRequestIfNotBpn(businessPartner.legalEntityReference(), businessPartner.siteReference(), businessPartner.legalAddressReference())

return with(businessPartner.legalEntity){
copy(
bpnReference = bpnReference.toRequestIfNotBpn(businessPartner.createLegalEntityReferenceValue()),
bpnReference = bpnReference.toRequestIfNotBpn(businessPartner.legalEntityReference()),
legalName = legalName ?: businessPartner.uncategorized.nameParts.joinToString(""),
identifiers = identifiers.takeIf { it.isNotEmpty() } ?: businessPartner.uncategorized.identifiers,
states = states.takeIf { it.isNotEmpty() } ?: businessPartner.uncategorized.states,
confidenceCriteria = dummyConfidenceCriteria.copy(sharedByOwner = sharedByOwner),
hasChanged = businessPartner.type == GoldenRecordType.LegalEntity,
isCatenaXMemberData = sharedByOwner,
legalAddress = cleanAddress(addressToClean, businessPartner.createLegalAddressReferenceValue(), true, sharedByOwner),
legalAddress = cleanAddress(addressToClean, legalAddressBpnReference, true, sharedByOwner),
)
}
}
Expand All @@ -127,12 +131,15 @@ class CleaningServiceDummy(
?: PostalAddress.empty
}

val siteMainBpnReference = addressToClean?.bpnReference
?.toRequestIfNotBpn(businessPartner.legalAddressReference(), businessPartner.siteReference(), businessPartner.siteMainAddressReference())

with(site){
copy(
bpnReference = bpnReference.toRequestIfNotBpn(businessPartner.createSiteReferenceValue()),
bpnReference = bpnReference.toRequestIfNotBpn(businessPartner.legalEntityReference(), businessPartner.siteReference()),
confidenceCriteria = dummyConfidenceCriteria.copy(sharedByOwner = sharedByOwner),
hasChanged = businessPartner.type == GoldenRecordType.Site,
siteMainAddress = addressToClean?.let { cleanAddress(addressToClean, businessPartner.createSiteMainAddressReferenceValue(), true, sharedByOwner) }
siteMainAddress = addressToClean?.let { cleanAddress(addressToClean, siteMainBpnReference!!, true, sharedByOwner) }
)
}
}
Expand All @@ -142,46 +149,48 @@ class CleaningServiceDummy(
return businessPartner.additionalAddress?.let {
cleanAddress(
it,
businessPartner.createAdditionalAddressReferenceValue(),
it.bpnReference.toRequestIfNotBpn(businessPartner.legalEntityReference(), businessPartner.siteReference(), businessPartner.addressReference()),
businessPartner.type == GoldenRecordType.Address,
sharedByOwner
)
}
}


private fun cleanAddress(addressToClean: PostalAddress, requestId: String, hasChanged: Boolean, sharedByOwner: Boolean): PostalAddress {
private fun cleanAddress(addressToClean: PostalAddress, bpnReference: BpnReference, hasChanged: Boolean, sharedByOwner: Boolean): PostalAddress {
return addressToClean.copy(
bpnReference = addressToClean.bpnReference.toRequestIfNotBpn(requestId),
bpnReference = bpnReference,
hasChanged = hasChanged,
confidenceCriteria = dummyConfidenceCriteria.copy(sharedByOwner = sharedByOwner)
)
}

private fun BusinessPartner.createLegalEntityReferenceValue() =
"LEGAL_ENTITY" + (legalEntity.legalName ?: nameParts.joinToString(" "))
private fun BusinessPartner.legalEntityReference() =
"LEGAL_ENTITY${legalEntity.legalName ?: namePartsName()}".toUUID()

private fun BusinessPartner.siteReference() = "S_${site?.siteName ?: namePartsName()}".toUUID()

private fun BusinessPartner.createSiteReferenceValue() =
"SITE" + createLegalEntityReferenceValue() + (site?.siteName ?: "")
private fun BusinessPartner.addressReference() = "A_${additionalAddress?.addressName ?: namePartsName()}".toUUID()

private fun BusinessPartner.createSiteMainAddressReferenceValue() =
"SITE_MAIN_ADDRESS" + createSiteReferenceValue()
private fun BusinessPartner.namePartsName() = uncategorized.nameParts.joinToString(" ")

private fun BusinessPartner.createLegalAddressReferenceValue() =
"LEGAL_ADDRESS" + createLegalEntityReferenceValue()
private fun BusinessPartner.legalAddressReference() = "LEGAL_ADDRESS".toUUID()

private fun BusinessPartner.siteMainAddressReference() = "SITE_MAIN_ADDRESS".toUUID()

private fun BusinessPartner.createAdditionalAddressReferenceValue() =
"ADDITIONAL_ADDRESS" + (additionalAddress?.addressName ?: "") + createSiteReferenceValue()

private fun generateBpnRequestIdentifier(fromString: String) =
BpnReference(fromString.toUUID().toString(), null, BpnReferenceType.BpnRequestIdentifier)
private fun generateBpnRequestIdentifier(legalEntityReference: UUID, siteReference: UUID? = null, addressReference: UUID? = null): BpnReference{
val siteReferenceString = siteReference?.let { "${siteReference}_" } ?: ""
val addressReferenceString = addressReference?.let { "${addressReference}_" } ?: ""
return BpnReference( addressReferenceString + siteReferenceString + legalEntityReference.toString(), null, BpnReferenceType.BpnRequestIdentifier)
}



private fun BpnReference.toRequestIfNotBpn(requestId: String) =
private fun BpnReference.toRequestIfNotBpn(legalEntityReference: UUID, siteReference: UUID? = null, additionalAddressReference: UUID? = null) =
if(referenceType == BpnReferenceType.Bpn)
this
else
generateBpnRequestIdentifier(requestId)
generateBpnRequestIdentifier(legalEntityReference, siteReference, additionalAddressReference)

}