Skip to content

Commit

Permalink
feat: align scope history with new history management
Browse files Browse the repository at this point in the history
  • Loading branch information
bobeal committed Dec 17, 2024
1 parent 0025851 commit 44d85c2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ import org.springframework.r2dbc.core.bind
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.net.URI
import java.time.ZoneOffset
import java.time.ZonedDateTime

@Service
Expand Down Expand Up @@ -90,7 +89,8 @@ class EntityService(
val attributesMetadata = ngsiLdEntity.prepareAttributes().bind()
logger.debug("Creating entity {}", ngsiLdEntity.id)

createEntityPayload(ngsiLdEntity, expandedEntity, createdAt, sub).bind()
createEntityPayload(ngsiLdEntity, expandedEntity, createdAt).bind()
scopeService.createHistory(ngsiLdEntity, createdAt, sub).bind()
entityAttributeService.createAttributes(
ngsiLdEntity,
expandedEntity,
Expand All @@ -111,8 +111,7 @@ class EntityService(
suspend fun createEntityPayload(
ngsiLdEntity: NgsiLdEntity,
expandedEntity: ExpandedEntity,
createdAt: ZonedDateTime,
sub: Sub? = null
createdAt: ZonedDateTime
): Either<APIException, Unit> = either {
val specificAccessPolicy = ngsiLdEntity.getSpecificAccessPolicy()?.bind()
databaseClient.sql(
Expand All @@ -135,9 +134,6 @@ class EntityService(
.bind("payload", Json.of(serializeObject(expandedEntity.populateCreationTimeDate(createdAt).members)))
.bind("specific_access_policy", specificAccessPolicy?.toString())
.execute()
.map {
scopeService.createHistory(ngsiLdEntity, createdAt, sub)
}
}

@Transactional
Expand Down Expand Up @@ -204,7 +200,8 @@ class EntityService(
entityAttributeService.deleteAttributes(entityId, ngsiLdDateTime()).bind()

val replacedAt = ngsiLdDateTime()
replaceEntityPayload(ngsiLdEntity, expandedEntity, replacedAt, sub).bind()
replaceEntityPayload(ngsiLdEntity, expandedEntity, replacedAt).bind()
scopeService.replace(ngsiLdEntity, replacedAt, sub).bind()
entityAttributeService.createAttributes(
ngsiLdEntity,
expandedEntity,
Expand All @@ -224,8 +221,7 @@ class EntityService(
suspend fun replaceEntityPayload(
ngsiLdEntity: NgsiLdEntity,
expandedEntity: ExpandedEntity,
replacedAt: ZonedDateTime,
sub: Sub? = null
replacedAt: ZonedDateTime
): Either<APIException, Unit> = either {
val specificAccessPolicy = ngsiLdEntity.getSpecificAccessPolicy()?.bind()
val createdAt = retrieveCreatedAt(ngsiLdEntity.id).bind()
Expand All @@ -250,9 +246,6 @@ class EntityService(
.bind("payload", Json.of(serializedPayload))
.bind("specific_access_policy", specificAccessPolicy?.toString())
.execute()
.map {
scopeService.replaceHistoryEntry(ngsiLdEntity, createdAt, sub)
}
}

private suspend fun retrieveCreatedAt(entityId: URI): Either<APIException, ZonedDateTime> =
Expand Down Expand Up @@ -464,7 +457,7 @@ class EntityService(
expandedAttributes: ExpandedAttributes,
sub: Sub? = null
): Either<APIException, Unit> = either {
val createdAt = ZonedDateTime.now(ZoneOffset.UTC)
val createdAt = ngsiLdDateTime()
expandedAttributes.forEach { (attributeName, expandedAttributeInstances) ->
expandedAttributeInstances.forEach { expandedAttributeInstance ->
val jsonLdAttribute = mapOf(attributeName to listOf(expandedAttributeInstance))
Expand Down Expand Up @@ -619,7 +612,6 @@ class EntityService(

val entity = permanentyDeleteEntityPayload(entityId).bind()
entityAttributeService.permanentlyDeleteAttributes(entityId).bind()
scopeService.deleteHistory(entityId).bind()
authorizationService.removeRightsOnEntity(entityId).bind()

entityEventService.publishEntityDeleteEvent(sub, entity)
Expand Down Expand Up @@ -695,7 +687,7 @@ class EntityService(
authorizationService.userCanUpdateEntity(entityId, sub.toOption()).bind()

if (attributeName == NGSILD_SCOPE_PROPERTY) {
scopeService.delete(entityId).bind()
scopeService.permanentlyDelete(entityId).bind()
} else {
entityAttributeService.checkEntityAndAttributeExistence(
entityId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import com.egm.stellio.shared.util.JsonLdUtils
import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_SCOPE_PROPERTY
import com.egm.stellio.shared.util.JsonUtils.serializeObject
import com.egm.stellio.shared.util.Sub
import com.egm.stellio.shared.util.getSubFromSecurityContext
import com.egm.stellio.shared.util.ngsiLdDateTime
import io.r2dbc.postgresql.codec.Json
import org.springframework.r2dbc.core.DatabaseClient
import org.springframework.r2dbc.core.bind
Expand Down Expand Up @@ -72,7 +74,7 @@ class ScopeService(
suspend fun addHistoryEntry(
entityId: URI,
scopes: List<String>,
temportalProperty: TemporalProperty,
temporalProperty: TemporalProperty,
createdAt: ZonedDateTime,
sub: Sub? = null
): Either<APIException, Unit> =
Expand All @@ -85,7 +87,7 @@ class ScopeService(
.bind("entity_id", entityId)
.bind("value", scopes.toTypedArray())
.bind("time", createdAt)
.bind("time_property", temportalProperty.toString())
.bind("time_property", temporalProperty.toString())
.bind("sub", sub)
.execute()

Expand Down Expand Up @@ -341,12 +343,12 @@ class ScopeService(
}

@Transactional
suspend fun replaceHistoryEntry(
suspend fun replace(
ngsiLdEntity: NgsiLdEntity,
createdAt: ZonedDateTime,
sub: Sub? = null
): Either<APIException, Unit> = either {
deleteHistory(ngsiLdEntity.id).bind()
delete(ngsiLdEntity.id).bind()
createHistory(ngsiLdEntity, createdAt, sub).bind()
}

Expand All @@ -364,10 +366,17 @@ class ScopeService(
.execute()
.bind()

deleteHistory(entityId).bind()
addHistoryEntry(
entityId,
emptyList(),
TemporalProperty.DELETED_AT,
ngsiLdDateTime(),
getSubFromSecurityContext().getOrNull()
)
}

suspend fun deleteHistory(entityId: URI): Either<APIException, Unit> =
@Transactional
suspend fun permanentlyDelete(entityId: URI): Either<APIException, Unit> =
databaseClient.sql(
"""
DELETE FROM scope_history
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class TemporalService(
if (isDeleted) {
val (expandedEntity, ngsiLdEntity) =
parseExpandedInstances(sortedJsonLdInstances, jsonLdTemporalEntity).bind()
entityService.createEntityPayload(ngsiLdEntity, expandedEntity, ngsiLdDateTime(), sub).bind()
entityService.createEntityPayload(ngsiLdEntity, expandedEntity, ngsiLdDateTime()).bind()
}
entityService.upsertAttributes(
entityId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import com.egm.stellio.shared.util.shouldSucceed
import com.egm.stellio.shared.util.shouldSucceedAndResult
import com.egm.stellio.shared.util.shouldSucceedWith
import com.egm.stellio.shared.util.toUri
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.AfterEach
Expand Down Expand Up @@ -64,10 +63,6 @@ class ScopeServiceTests : WithTimescaleContainer, WithKafkaContainer {
@AfterEach
fun clearEntityPayloadTable() {
r2dbcEntityTemplate.delete<Entity>().from("entity_payload").all().block()

runBlocking {
scopeService.delete(beehiveTestCId)
}
}

@Suppress("unused")
Expand Down

0 comments on commit 44d85c2

Please sign in to comment.