Skip to content

Commit

Permalink
#337: Added support for importing from templates on the server
Browse files Browse the repository at this point in the history
  • Loading branch information
sauterl committed Oct 10, 2023
1 parent 44973b8 commit 7a7e39a
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ 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].
Expand All @@ -20,7 +19,6 @@ typealias TaskTypeId = String
* @version 1.1.0
*/
data class ApiTaskType(
val id: TaskTypeId? = null,
val name: String,
val duration: Long,
val targetOption: ApiTargetOption,
Expand All @@ -31,7 +29,7 @@ data class ApiTaskType(
val configuration: Map<String, String>
) {

constructor() : this("---NO_ID---","---Default TaskType DO NOT USE!---",
constructor() : this("---Default TaskType DO NOT USE!---",
1,
ApiTargetOption.TEXT, listOf(ApiHintOption.TEXT),
listOf(ApiSubmissionOption.TEXTUAL_SUBMISSION),
Expand All @@ -50,8 +48,4 @@ data class ApiTaskType(
}
}

val taskTypeId: TaskTypeId
@JsonIgnore
@OpenApiIgnore
get() = this.id ?: "N/A"
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
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 @@ -17,19 +15,14 @@ import kotlinx.dnq.simple.min
* @author Luca Rossetto & Ralph Gasser
* @version 2.0.0
*/
class DbTaskType(entity: Entity) : PersistentEntity(entity) {
class DbTaskType(entity: Entity) : XdEntity(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 Expand Up @@ -63,7 +56,6 @@ class DbTaskType(entity: Entity) : PersistentEntity(entity) {
* @return [ApiTaskType]
*/
fun toApi(): ApiTaskType = ApiTaskType(
id=this.xdId,
name = this.name,
duration = this.duration,
targetOption = this.target.toApi(),
Expand Down
38 changes: 2 additions & 36 deletions backend/src/main/kotlin/dev/dres/mgmt/TemplateManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ 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 @@ -95,40 +92,9 @@ object TemplateManager {
dbEvaluationTemplate.modified = DateTime.now()

/* Update task type information. */

/* 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()}

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

0 comments on commit 7a7e39a

Please sign in to comment.