Skip to content

Commit

Permalink
WiP: Fixing removing of task types, yet broken
Browse files Browse the repository at this point in the history
  • Loading branch information
sauterl committed Oct 6, 2023
1 parent 630084a commit 44973b8
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
package dev.dres.api.rest.types.template.tasks

import com.fasterxml.jackson.annotation.JsonIgnore
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper
import dev.dres.api.rest.types.template.tasks.options.*
import dev.dres.data.model.template.task.DbTaskType
import io.javalin.openapi.OpenApiIgnore
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.StandardOpenOption

typealias TaskTypeId = String

/**
* The RESTful API equivalent of a [DbTaskType].
*
* @author Ralph Gasser
* @author Loris Sauter
* @author Ralph Gasser & Loris Sauter
* @version 1.1.0
*/
data class ApiTaskType(
val id: String?,
val id: TaskTypeId? = null,
val name: String,
val duration: Long,
val targetOption: ApiTargetOption,
Expand Down Expand Up @@ -46,4 +49,9 @@ data class ApiTaskType(
ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).readValue(it, ApiTaskType::class.java)
}
}

val taskTypeId: TaskTypeId
@JsonIgnore
@OpenApiIgnore
get() = this.id ?: "N/A"
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ import dev.dres.data.model.template.DbEvaluationTemplate
import dev.dres.data.model.media.DbMediaCollection
import dev.dres.data.model.template.team.DbTeam
import dev.dres.data.model.run.interfaces.TaskRun
import dev.dres.data.model.template.TemplateId
import jetbrains.exodus.entitystore.Entity
import kotlinx.dnq.*
import kotlinx.dnq.link.OnDeletePolicy
import kotlinx.dnq.query.*
import kotlinx.dnq.simple.min
import java.lang.IllegalStateException
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dev.dres.data.model.template.task

import dev.dres.api.rest.types.template.tasks.ApiTaskType
import dev.dres.api.rest.types.template.tasks.TaskTypeId
import dev.dres.data.model.PersistentEntity
import dev.dres.data.model.template.DbEvaluationTemplate
import dev.dres.data.model.template.task.options.*
import dev.dres.data.model.template.task.options.DbConfiguredOption
Expand All @@ -15,14 +17,19 @@ import kotlinx.dnq.simple.min
* @author Luca Rossetto & Ralph Gasser
* @version 2.0.0
*/
class DbTaskType(entity: Entity) : XdEntity(entity) {
class DbTaskType(entity: Entity) : PersistentEntity(entity) {
/** Combination of [DbTaskType] name / competition must be unique. */
companion object: XdNaturalEntityType<DbTaskType>() {
override val compositeIndices = listOf(
listOf(DbTaskType::name, DbTaskType::evaluation)
)
}

/** The [TaskTypeId] of this [DbTaskType]. */
var taskTypeId: TaskTypeId
get() = this.id
set(value) { this.id = value }

/** The name of this [DbTaskType]. */
var name by xdRequiredStringProp(unique = false, trimmed = false)

Expand Down
38 changes: 36 additions & 2 deletions backend/src/main/kotlin/dev/dres/mgmt/TemplateManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import dev.dres.data.model.template.DbEvaluationTemplate
import dev.dres.data.model.template.TemplateId
import dev.dres.data.model.template.task.*
import dev.dres.data.model.template.task.options.DbConfiguredOption
import dev.dres.data.model.template.task.options.DbHintOption
import dev.dres.data.model.template.task.options.DbSubmissionOption
import dev.dres.data.model.template.task.options.DbTaskOption
import dev.dres.data.model.template.team.DbTeam
import dev.dres.data.model.template.team.DbTeamGroup
import dev.dres.data.model.template.team.TeamId
Expand Down Expand Up @@ -92,9 +95,40 @@ object TemplateManager {
dbEvaluationTemplate.modified = DateTime.now()

/* Update task type information. */
val taskTypes = apiEvaluationTemplate.taskTypes.map { it.name }.toTypedArray()

/* Update task type information: Remove deleted types. */
val typesIds = apiEvaluationTemplate.taskTypes.mapNotNull { it.id }.toTypedArray()
val typesToDeleteQuery = DbTaskType.query(
DbTaskType::evaluation eq dbEvaluationTemplate and not(
DbTaskType::id.containsIn(*typesIds)
)
)
val hintOptsToDelIds = typesToDeleteQuery.toList().map {
it.hints.toList().map { hint -> hint.entityId }
}.flatten().toTypedArray()
val submissionOptsToDelIds = typesToDeleteQuery.toList().map{
it.submission.toList().map{target -> target.entityId}
}.flatten().toTypedArray()
val taskOptsToDelIds = typesToDeleteQuery.toList().map{
it.options.toList().map{target -> target.entityId}
}.flatten().toTypedArray()
val configOptsToDelIds = typesToDeleteQuery.toList().map{
it.configurations.toList().map{target -> target.entityId}
}.flatten().toTypedArray()

/*
DbTaskTemplate has children relationships with both, DbHint and DbTaskTarget.
Despite being written in the documentation, for some reason the .removeAll above does not
delete the children, hence we have to take care of it ourselves.
https://jetbrains.github.io/xodus-dnq/properties.html
*/
DbHintOption.all().toList().filter{hintOptsToDelIds.contains(it.entityId)}.forEach { it.delete() }
DbSubmissionOption.all().toList().filter{submissionOptsToDelIds.contains(it.entityId)}.forEach{it.delete()}
DbTaskOption.all().toList().filter{taskOptsToDelIds.contains(it.entityId)}.forEach{it.delete()}
DbConfiguredOption.all().toList().filter{configOptsToDelIds.contains(it.entityId)}.forEach{it.delete()}

dbEvaluationTemplate.taskTypes.removeAll(
DbTaskType.query(DbTaskType::evaluation eq dbEvaluationTemplate and not(DbTaskType::name.containsIn(*taskTypes)))
typesToDeleteQuery
)
for (apiTaskType in apiEvaluationTemplate.taskTypes) {
val taskType =
Expand Down

0 comments on commit 44973b8

Please sign in to comment.