Skip to content

Commit

Permalink
refactor: clean up all the expand / compact processing utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
bobeal committed Dec 31, 2023
1 parent 0dc4c84 commit 77bbfd6
Show file tree
Hide file tree
Showing 83 changed files with 1,154 additions and 1,287 deletions.
8 changes: 4 additions & 4 deletions config/detekt/detekt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ complexity:
TooManyFunctions:
active: true
excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/androidUnitTest/**', '**/androidInstrumentedTest/**', '**/jsTest/**', '**/iosTest/**']
thresholdInFiles: 20
thresholdInClasses: 20
thresholdInInterfaces: 20
thresholdInObjects: 20
thresholdInFiles: 30
thresholdInClasses: 30
thresholdInInterfaces: 30
thresholdInObjects: 30
thresholdInEnums: 11
ignoreDeprecated: false
ignorePrivate: false
Expand Down
1 change: 0 additions & 1 deletion search-service/config/detekt/baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,5 @@
<ID>ReturnCount:EntitiesQueryUtils.kt$fun buildTemporalQuery( params: MultiValueMap&lt;String, String&gt;, inQueryEntities: Boolean = false, withAggregatedValues: Boolean = false ): Either&lt;APIException, TemporalQuery&gt;</ID>
<ID>SwallowedException:EntitiesQueryUtils.kt$e: IllegalArgumentException</ID>
<ID>TooManyFunctions:EntityPayloadService.kt$EntityPayloadService</ID>
<ID>TooManyFunctions:TemporalEntityAttributeService.kt$TemporalEntityAttributeService</ID>
</CurrentIssues>
</SmellBaseline>
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ interface AuthorizationService {

suspend fun getAuthorizedEntities(
entitiesQuery: EntitiesQuery,
contextLink: String,
contexts: List<String>,
sub: Option<Sub>
): Either<APIException, Pair<Int, List<ExpandedEntity>>>

suspend fun getGroupsMemberships(
offset: Int,
limit: Int,
contextLink: String,
contexts: List<String>,
sub: Option<Sub>
): Either<APIException, Pair<Int, List<ExpandedEntity>>>

suspend fun getUsers(
offset: Int,
limit: Int,
contextLink: String,
contexts: List<String>,
): Either<APIException, Pair<Int, List<ExpandedEntity>>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,20 @@ class DisabledAuthorizationService : AuthorizationService {

override suspend fun getAuthorizedEntities(
entitiesQuery: EntitiesQuery,
contextLink: String,
contexts: List<String>,
sub: Option<Sub>
): Either<APIException, Pair<Int, List<ExpandedEntity>>> = Pair(-1, emptyList<ExpandedEntity>()).right()

override suspend fun getGroupsMemberships(
offset: Int,
limit: Int,
contextLink: String,
contexts: List<String>,
sub: Option<Sub>
): Either<APIException, Pair<Int, List<ExpandedEntity>>> = Pair(-1, emptyList<ExpandedEntity>()).right()

override suspend fun getUsers(
offset: Int,
limit: Int,
contextLink: String,
contexts: List<String>,
): Either<APIException, Pair<Int, List<ExpandedEntity>>> = Pair(-1, emptyList<ExpandedEntity>()).right()
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class EnabledAuthorizationService(

override suspend fun getAuthorizedEntities(
entitiesQuery: EntitiesQuery,
contextLink: String,
contexts: List<String>,
sub: Option<Sub>
): Either<APIException, Pair<Int, List<ExpandedEntity>>> = either {
val accessRights = entitiesQuery.attrs.mapNotNull { AccessRight.forExpandedAttributeName(it).getOrNull() }
Expand Down Expand Up @@ -127,8 +127,8 @@ class EnabledAuthorizationService(
)
} else entityAccessControl
}
.map { it.serializeProperties(contextLink) }
.map { ExpandedEntity(it, listOf(contextLink)) }
.map { it.serializeProperties(contexts) }
.map { ExpandedEntity(it, contexts) }

val count = entityAccessRightsService.getSubjectAccessRightsCount(
sub,
Expand All @@ -143,7 +143,7 @@ class EnabledAuthorizationService(
override suspend fun getGroupsMemberships(
offset: Int,
limit: Int,
contextLink: String,
contexts: List<String>,
sub: Option<Sub>
): Either<APIException, Pair<Int, List<ExpandedEntity>>> = either {
val groups =
Expand All @@ -162,10 +162,7 @@ class EnabledAuthorizationService(
}

val jsonLdEntities = groups.second.map {
ExpandedEntity(
it.serializeProperties(),
listOf(contextLink)
)
ExpandedEntity(it.serializeProperties(), contexts)
}

Pair(groups.first, jsonLdEntities)
Expand All @@ -174,16 +171,13 @@ class EnabledAuthorizationService(
override suspend fun getUsers(
offset: Int,
limit: Int,
contextLink: String
contexts: List<String>,
): Either<APIException, Pair<Int, List<ExpandedEntity>>> = either {
val users = subjectReferentialService.getUsers(offset, limit)
val usersCount = subjectReferentialService.getUsersCount().bind()

val jsonLdEntities = users.map {
ExpandedEntity(
it.serializeProperties(contextLink),
listOf(contextLink)
)
ExpandedEntity(it.serializeProperties(contexts), contexts)
}

Pair(usersCount, jsonLdEntities)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import com.egm.stellio.shared.model.ExpandedAttributeInstances
import com.egm.stellio.shared.model.ExpandedTerm
import com.egm.stellio.shared.model.addNonReifiedProperty
import com.egm.stellio.shared.model.addSubAttribute
import com.egm.stellio.shared.util.*
import com.egm.stellio.shared.util.AccessRight
import com.egm.stellio.shared.util.AuthContextModel
import com.egm.stellio.shared.util.AuthContextModel.AUTH_PROP_RIGHT
import com.egm.stellio.shared.util.AuthContextModel.AUTH_PROP_SAP
import com.egm.stellio.shared.util.AuthContextModel.AUTH_PROP_SUBJECT_INFO
Expand All @@ -18,6 +19,8 @@ import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_DATASET_ID_PROPERTY
import com.egm.stellio.shared.util.JsonLdUtils.buildExpandedPropertyMapValue
import com.egm.stellio.shared.util.JsonLdUtils.buildExpandedPropertyValue
import com.egm.stellio.shared.util.JsonLdUtils.buildExpandedRelationshipValue
import com.egm.stellio.shared.util.extractSub
import com.egm.stellio.shared.util.toUri
import java.net.URI

data class EntityAccessRights(
Expand All @@ -36,16 +39,16 @@ data class EntityAccessRights(
) {
val datasetId: URI = (DATASET_ID_PREFIX + uri.extractSub()).toUri()

suspend fun serializeProperties(contextLink: String): ExpandedAttributeInstances =
suspend fun serializeProperties(contexts: List<String>): ExpandedAttributeInstances =
buildExpandedRelationshipValue(uri)
.addNonReifiedProperty(NGSILD_DATASET_ID_PROPERTY, datasetId.toString())
.addSubAttribute(
AUTH_PROP_SUBJECT_INFO,
buildExpandedPropertyMapValue(subjectInfo, listOf(contextLink))
buildExpandedPropertyMapValue(subjectInfo, contexts)
)
}

suspend fun serializeProperties(contextLink: String): Map<String, Any> {
suspend fun serializeProperties(contexts: List<String>): Map<String, Any> {
val resultEntity = mutableMapOf<String, Any>()

resultEntity[JSONLD_ID] = id.toString()
Expand All @@ -58,17 +61,17 @@ data class EntityAccessRights(

rCanAdminUsers?.run {
resultEntity[AUTH_REL_CAN_ADMIN] = this.map {
it.serializeProperties(contextLink)
it.serializeProperties(contexts)
}.flatten()
}
rCanWriteUsers?.run {
resultEntity[AUTH_REL_CAN_WRITE] = this.map {
it.serializeProperties(contextLink)
it.serializeProperties(contexts)
}.flatten()
}
rCanReadUsers?.run {
resultEntity[AUTH_REL_CAN_READ] = this.map {
it.serializeProperties(contextLink)
it.serializeProperties(contexts)
}.flatten()
}
return resultEntity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ data class User(
val familyName: String? = null,
val subjectInfo: Map<String, String>
) {
suspend fun serializeProperties(contextLink: String): Map<String, Any> {
suspend fun serializeProperties(contexts: List<String>): Map<String, Any> {
val resultEntity = mutableMapOf<String, Any>()
resultEntity[JSONLD_ID] = USER_ENTITY_PREFIX + id
resultEntity[JSONLD_TYPE] = listOf(type)
Expand All @@ -43,7 +43,7 @@ data class User(
}.run {
if (this.isNotEmpty())
resultEntity[AUTH_PROP_SUBJECT_INFO] =
buildExpandedPropertyMapValue(this, listOf(contextLink))
buildExpandedPropertyMapValue(this, contexts)
}

return resultEntity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ data class EntitiesQuery(
val paginationQuery: PaginationQuery,
val attrs: Set<ExpandedTerm> = emptySet(),
val geoQuery: GeoQuery? = null,
val context: String
val contexts: List<String>
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.egm.stellio.search.model.UpdateResult
import com.egm.stellio.search.model.UpdatedDetails
import com.egm.stellio.shared.model.*
import com.egm.stellio.shared.util.JsonLdUtils.JSONLD_TYPE
import com.egm.stellio.shared.util.JsonLdUtils.getAttributeFromExpandedAttributes
import com.egm.stellio.shared.util.JsonUtils.serializeObject
import com.egm.stellio.shared.util.getTenantFromContext
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -253,8 +252,7 @@ class EntityEventService(
when (attributeName) {
JSONLD_TYPE -> Pair(JSONLD_TYPE, serializeObject(jsonLdAttributes[JSONLD_TYPE]!!))
else -> {
val extractedPayload = getAttributeFromExpandedAttributes(
jsonLdAttributes as ExpandedAttributes,
val extractedPayload = (jsonLdAttributes as ExpandedAttributes).getAttributeFromExpandedAttributes(
attributeName,
datasetId
)!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ class EntityPayloadService(
accessRightFilter
).let {
if (entitiesQuery.q != null)
it.wrapToAndClause(buildQQuery(entitiesQuery.q, listOf(entitiesQuery.context)))
it.wrapToAndClause(buildQQuery(entitiesQuery.q, entitiesQuery.contexts))
else it
}.let {
if (entitiesQuery.scopeQ != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ class QueryService(

val entitiesPayloads =
entityPayloadService.retrieve(entitiesIds)
.map { toJsonLdEntity(it, listOf(entitiesQuery.context)) }
.map { toJsonLdEntity(it, entitiesQuery.contexts) }

Pair(entitiesPayloads, count).right().bind()
}

suspend fun queryTemporalEntity(
entityId: URI,
temporalEntitiesQuery: TemporalEntitiesQuery,
contextLink: String
contexts: List<String>
): Either<APIException, ExpandedEntity> = either {
val attrs = temporalEntitiesQuery.entitiesQuery.attrs
val temporalEntityAttributes = temporalEntityAttributeService.getForEntity(entityId, attrs).let {
Expand Down Expand Up @@ -84,7 +84,7 @@ class QueryService(
TemporalEntityBuilder.buildTemporalEntity(
EntityTemporalResult(entityPayload, scopeHistory, temporalEntityAttributesWithInstances),
temporalEntitiesQuery,
listOf(contextLink)
contexts
)
}

Expand Down Expand Up @@ -176,7 +176,7 @@ class QueryService(
TemporalEntityBuilder.buildTemporalEntities(
attributeInstancesPerEntityAndAttribute,
temporalEntitiesQuery,
listOf(temporalEntitiesQuery.entitiesQuery.context)
temporalEntitiesQuery.entitiesQuery.contexts
),
count
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_PROPERTY_VALUE
import com.egm.stellio.shared.util.JsonLdUtils.NGSILD_RELATIONSHIP_OBJECT
import com.egm.stellio.shared.util.JsonLdUtils.buildNonReifiedTemporalValue
import com.egm.stellio.shared.util.JsonLdUtils.expandJsonLdEntity
import com.egm.stellio.shared.util.JsonLdUtils.getAttributeFromExpandedAttributes
import com.egm.stellio.shared.util.JsonLdUtils.getPropertyValueFromMap
import com.egm.stellio.shared.util.JsonLdUtils.getPropertyValueFromMapAsDateTime
import com.egm.stellio.shared.util.JsonUtils.serializeObject
import com.savvasdalkitsis.jsonmerger.JsonMerger
import io.r2dbc.postgresql.codec.Json
Expand Down Expand Up @@ -149,8 +146,7 @@ class TemporalEntityAttributeService(
}
.forEach {
val (expandedAttributeName, attributeMetadata) = it
val attributePayload = getAttributeFromExpandedAttributes(
expandedEntity.getAttributes(),
val attributePayload = expandedEntity.getAttributes().getAttributeFromExpandedAttributes(
expandedAttributeName,
attributeMetadata.datasetId
)!!
Expand Down Expand Up @@ -535,8 +531,7 @@ class TemporalEntityAttributeService(
getForEntityAndAttribute(entityUri, ngsiLdAttribute.name, ngsiLdAttributeInstance.datasetId)
.fold({ null }, { it })
val attributeMetadata = ngsiLdAttributeInstance.toTemporalAttributeMetadata().bind()
val attributePayload = getAttributeFromExpandedAttributes(
expandedAttributes,
val attributePayload = expandedAttributes.getAttributeFromExpandedAttributes(
ngsiLdAttribute.name,
ngsiLdAttributeInstance.datasetId
)!!
Expand Down Expand Up @@ -601,8 +596,7 @@ class TemporalEntityAttributeService(
.fold({ null }, { it })
if (currentTea != null) {
val attributeMetadata = ngsiLdAttributeInstance.toTemporalAttributeMetadata().bind()
val attributePayload = getAttributeFromExpandedAttributes(
expandedAttributes,
val attributePayload = expandedAttributes.getAttributeFromExpandedAttributes(
ngsiLdAttribute.name,
ngsiLdAttributeInstance.datasetId
)!!
Expand Down Expand Up @@ -711,8 +705,7 @@ class TemporalEntityAttributeService(
getForEntityAndAttribute(entityUri, ngsiLdAttribute.name, ngsiLdAttributeInstance.datasetId)
.fold({ null }, { it })
val attributeMetadata = ngsiLdAttributeInstance.toTemporalAttributeMetadata().bind()
val attributePayload = getAttributeFromExpandedAttributes(
expandedAttributes,
val attributePayload = expandedAttributes.getAttributeFromExpandedAttributes(
ngsiLdAttribute.name,
ngsiLdAttributeInstance.datasetId
)!!
Expand Down Expand Up @@ -757,8 +750,7 @@ class TemporalEntityAttributeService(
getForEntityAndAttribute(entityUri, ngsiLdAttribute.name, ngsiLdAttributeInstance.datasetId)
.fold({ null }, { it })
val attributeMetadata = ngsiLdAttributeInstance.toTemporalAttributeMetadata().bind()
val attributePayload = getAttributeFromExpandedAttributes(
expandedAttributes,
val attributePayload = expandedAttributes.getAttributeFromExpandedAttributes(
ngsiLdAttribute.name,
ngsiLdAttributeInstance.datasetId
)!!
Expand Down Expand Up @@ -850,21 +842,21 @@ class TemporalEntityAttributeService(
when (tea.attributeType) {
TemporalEntityAttribute.AttributeType.Property ->
Triple(
valueToStringOrNull(getPropertyValueFromMap(attributePayload, NGSILD_PROPERTY_VALUE)!!),
valueToDoubleOrNull(getPropertyValueFromMap(attributePayload, NGSILD_PROPERTY_VALUE)!!),
valueToStringOrNull(attributePayload.getMemberValue(NGSILD_PROPERTY_VALUE)!!),
valueToDoubleOrNull(attributePayload.getMemberValue(NGSILD_PROPERTY_VALUE)!!),
null
)
TemporalEntityAttribute.AttributeType.Relationship ->
Triple(
getPropertyValueFromMap(attributePayload, NGSILD_RELATIONSHIP_OBJECT)!! as String,
attributePayload.getMemberValue(NGSILD_RELATIONSHIP_OBJECT)!! as String,
null,
null
)
TemporalEntityAttribute.AttributeType.GeoProperty ->
Triple(
null,
null,
WKTCoordinates(getPropertyValueFromMap(attributePayload, NGSILD_PROPERTY_VALUE)!! as String)
WKTCoordinates(attributePayload.getMemberValue(NGSILD_PROPERTY_VALUE)!! as String)
)
}

Expand Down Expand Up @@ -893,7 +885,7 @@ class TemporalEntityAttributeService(
val timeAndProperty =
if (expandedAttributeInstance.containsKey(NGSILD_OBSERVED_AT_PROPERTY))
Pair(
getPropertyValueFromMapAsDateTime(expandedAttributeInstance, NGSILD_OBSERVED_AT_PROPERTY)!!,
expandedAttributeInstance.getMemberValueAsDateTime(NGSILD_OBSERVED_AT_PROPERTY)!!,
AttributeInstance.TemporalProperty.OBSERVED_AT
)
else
Expand Down
Loading

0 comments on commit 77bbfd6

Please sign in to comment.