CSV export
This commit is contained in:
@@ -2,6 +2,7 @@ package org.jeudego.pairgoth.api
|
|||||||
|
|
||||||
import com.republicate.kson.Json
|
import com.republicate.kson.Json
|
||||||
import org.jeudego.pairgoth.model.Criterion
|
import org.jeudego.pairgoth.model.Criterion
|
||||||
|
import org.jeudego.pairgoth.model.DatabaseId
|
||||||
import org.jeudego.pairgoth.model.MacMahon
|
import org.jeudego.pairgoth.model.MacMahon
|
||||||
import org.jeudego.pairgoth.model.Pairable
|
import org.jeudego.pairgoth.model.Pairable
|
||||||
import org.jeudego.pairgoth.model.Pairable.Companion.MIN_RANK
|
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() }
|
val pairables = pairables.values.filter { it.final }.map { it.toMutableJson() }
|
||||||
pairables.forEach { player ->
|
pairables.forEach { player ->
|
||||||
for (crit in criteria) {
|
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=" })
|
player["results"] = Json.MutableArray(List(round) { "0=" })
|
||||||
}
|
}
|
||||||
|
@@ -101,6 +101,12 @@ object StandingsHandler: PairgothApiHandler {
|
|||||||
writer.flush()
|
writer.flush()
|
||||||
return null
|
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")
|
else -> ApiHandler.badRequest("invalid Accept header: $accept")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -224,6 +230,40 @@ ${
|
|||||||
writer.println(ret)
|
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 numFormat = DecimalFormat("###0.#")
|
||||||
private val frDate: DateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy")
|
private val frDate: DateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy")
|
||||||
}
|
}
|
||||||
|
@@ -235,7 +235,7 @@ class ApiServlet: HttpServlet() {
|
|||||||
// 2) there will be other content types: .tou, .h9, .html
|
// 2) there will be other content types: .tou, .h9, .html
|
||||||
if (!isJson(accept) &&
|
if (!isJson(accept) &&
|
||||||
(!isXml(accept) || !request.requestURI.matches(Regex("/api/tour/\\d+"))) &&
|
(!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(
|
) throw ApiException(
|
||||||
HttpServletResponse.SC_BAD_REQUEST,
|
HttpServletResponse.SC_BAD_REQUEST,
|
||||||
|
@@ -3,7 +3,7 @@ function publish(format, extension) {
|
|||||||
let shortName = form.val('shortName');
|
let shortName = form.val('shortName');
|
||||||
let encoding = $('#encoding')[0].value;
|
let encoding = $('#encoding')[0].value;
|
||||||
let hdrs = headers();
|
let hdrs = headers();
|
||||||
hdrs['Accept'] = `application/${format};charset=${encoding}`
|
hdrs['Accept'] = `${format};charset=${encoding}`
|
||||||
fetch(`api/tour/${tour_id}/standings/${activeRound}`, {
|
fetch(`api/tour/${tour_id}/standings/${activeRound}`, {
|
||||||
headers: hdrs
|
headers: hdrs
|
||||||
}).then(resp => {
|
}).then(resp => {
|
||||||
@@ -20,7 +20,6 @@ function publish(format, extension) {
|
|||||||
|
|
||||||
function publishHtml() {
|
function publishHtml() {
|
||||||
let html = $('#standings-table')[0].outerHTML;
|
let html = $('#standings-table')[0].outerHTML;
|
||||||
console.log(html)
|
|
||||||
let form = $('#tournament-infos')[0];
|
let form = $('#tournament-infos')[0];
|
||||||
let shortName = form.val('shortName');
|
let shortName = form.val('shortName');
|
||||||
let blob = new Blob(['\uFEFF', html], {type: 'text/html;charset=utf-8'});
|
let blob = new Blob(['\uFEFF', html], {type: 'text/html;charset=utf-8'});
|
||||||
@@ -78,10 +77,13 @@ onLoad(() => {
|
|||||||
});
|
});
|
||||||
*/
|
*/
|
||||||
$('.publish-ffg').on('click', e => {
|
$('.publish-ffg').on('click', e => {
|
||||||
publish('ffg', 'tou');
|
publish('application/ffg', 'tou');
|
||||||
});
|
});
|
||||||
$('.publish-egf').on('click', e => {
|
$('.publish-egf').on('click', e => {
|
||||||
publish('egf', 'h9');
|
publish('application/egf', 'h9');
|
||||||
|
});
|
||||||
|
$('.publish-csv').on('click', e => {
|
||||||
|
publish('text/csv', 'csv');
|
||||||
});
|
});
|
||||||
$('.publish-html').on('click', e => {
|
$('.publish-html').on('click', e => {
|
||||||
publishHtml();
|
publishHtml();
|
||||||
|
@@ -112,6 +112,7 @@
|
|||||||
<label>Choose format</label>
|
<label>Choose format</label>
|
||||||
<div class="inline fields">
|
<div class="inline fields">
|
||||||
<button type="button" class="ui blue floating publish-html button">HTML</button>
|
<button type="button" class="ui blue floating publish-html button">HTML</button>
|
||||||
|
<button type="button" class="ui blue floating publish-csv button">CSV</button>
|
||||||
<button type="button" class="ui blue floating publish-egf button">EGF</button>
|
<button type="button" class="ui blue floating publish-egf button">EGF</button>
|
||||||
<button type="button" class="ui blue floating publish-ffg button">FFG</button>
|
<button type="button" class="ui blue floating publish-ffg button">FFG</button>
|
||||||
</div>
|
</div>
|
||||||
|
Reference in New Issue
Block a user