CSV export

This commit is contained in:
Claude Brisson
2024-03-15 20:59:19 +01:00
parent fb710fd219
commit 5ebb21b3b0
5 changed files with 50 additions and 6 deletions

View File

@@ -2,6 +2,7 @@ package org.jeudego.pairgoth.api
import com.republicate.kson.Json
import org.jeudego.pairgoth.model.Criterion
import org.jeudego.pairgoth.model.DatabaseId
import org.jeudego.pairgoth.model.MacMahon
import org.jeudego.pairgoth.model.Pairable
import org.jeudego.pairgoth.model.Pairable.Companion.MIN_RANK
@@ -90,7 +91,7 @@ fun Tournament<*>.getSortedPairables(round: Int): List<Json.Object> {
val pairables = pairables.values.filter { it.final }.map { it.toMutableJson() }
pairables.forEach { player ->
for (crit in criteria) {
player[crit.first] = crit.second[player.getID()] ?: 0.0
player[crit.first] = (crit.second[player.getID()] ?: 0.0).toInt()
}
player["results"] = Json.MutableArray(List(round) { "0=" })
}

View File

@@ -101,6 +101,12 @@ object StandingsHandler: PairgothApiHandler {
writer.flush()
return null
}
"text/csv" -> {
response.contentType = "text/csv;charset=${encoding}"
exportToCSVFormat(tournament, sortedPairables, writer)
writer.flush()
return null
}
else -> ApiHandler.badRequest("invalid Accept header: $accept")
}
}
@@ -224,6 +230,40 @@ ${
writer.println(ret)
}
private fun exportToCSVFormat(tournament: Tournament<*>, lines: List<Json.Object>, writer: PrintWriter) {
val fields = listOf(
Pair("num", false),
Pair("place", false),
// Pair("egf", false), TODO configure display of egf / ffg field
Pair("name", true),
Pair("firstname", true),
Pair("country", false),
Pair("club", true),
Pair("rank", false),
Pair("NBW", false)
);
// headers
writer.println("${
fields.joinToString(";") { it.first }
};${
(1..tournament.rounds).joinToString(";") { "R$it" }
};${
tournament.pairing.placementParams.criteria.joinToString(";") { it.name }
}")
// lines
lines.forEach { line ->
writer.println("${
fields.joinToString(";") { if (it.second) "\"${line[it.first]}\"" else "${line[it.first]}" }
};${
line.getArray("results")!!.joinToString(";")
};${
tournament.pairing.placementParams.criteria.joinToString(";") { line.getString(it.name) ?: "" }
}")
}
}
private val numFormat = DecimalFormat("###0.#")
private val frDate: DateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy")
}

View File

@@ -235,7 +235,7 @@ class ApiServlet: HttpServlet() {
// 2) there will be other content types: .tou, .h9, .html
if (!isJson(accept) &&
(!isXml(accept) || !request.requestURI.matches(Regex("/api/tour/\\d+"))) &&
(!accept.startsWith("application/ffg") && !accept.startsWith("application/egf") || !request.requestURI.matches(Regex("/api/tour/\\d+/standings/\\d+")))
(!accept.startsWith("application/ffg") && !accept.startsWith("application/egf") && !accept.startsWith("text/csv") || !request.requestURI.matches(Regex("/api/tour/\\d+/standings/\\d+")))
) throw ApiException(
HttpServletResponse.SC_BAD_REQUEST,