feat: Add TheTable form element
This commit is contained in:
@@ -11,6 +11,8 @@ import com.betriebsratkanzlei.legalconsenthub_api.model.ApplicationFormSnapshotD
|
||||
import com.betriebsratkanzlei.legalconsenthub_api.model.FormElementSectionSnapshotDto
|
||||
import com.betriebsratkanzlei.legalconsenthub_api.model.FormElementSnapshotDto
|
||||
import com.betriebsratkanzlei.legalconsenthub_api.model.FormElementSubSectionSnapshotDto
|
||||
import com.fasterxml.jackson.core.type.TypeReference
|
||||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
|
||||
import org.springframework.stereotype.Service
|
||||
import org.thymeleaf.TemplateEngine
|
||||
import org.thymeleaf.context.Context
|
||||
@@ -126,9 +128,53 @@ class ApplicationFormFormatService(
|
||||
"SWITCH" -> {
|
||||
if (element.options.any { it.value == "true" }) "Ja" else "Nein"
|
||||
}
|
||||
"TABLE" -> {
|
||||
renderTableValue(element)
|
||||
}
|
||||
else -> "Keine Auswahl getroffen"
|
||||
}
|
||||
|
||||
private fun renderTableValue(element: FormElementSnapshotDto): String {
|
||||
if (element.options.isEmpty()) return "Keine Daten"
|
||||
|
||||
val objectMapper = jacksonObjectMapper()
|
||||
val headers = element.options.map { LatexEscaper.escape(it.label ?: "") }
|
||||
val columnData =
|
||||
element.options.map { option ->
|
||||
try {
|
||||
val typeRef = object : TypeReference<List<String>>() {}
|
||||
objectMapper.readValue(option.value ?: "[]", typeRef)
|
||||
} catch (e: Exception) {
|
||||
emptyList<String>()
|
||||
}
|
||||
}
|
||||
|
||||
val rowCount = columnData.maxOfOrNull { col -> col.size } ?: 0
|
||||
if (rowCount == 0) return "Keine Daten"
|
||||
|
||||
val columnSpec = headers.joinToString(" | ") { "l" }
|
||||
val headerRow = headers.joinToString(" & ")
|
||||
val dataRows =
|
||||
(0 until rowCount).map { rowIndex ->
|
||||
columnData.joinToString(" & ") { col: List<String> ->
|
||||
val value = col.getOrNull(rowIndex) ?: ""
|
||||
if (value.isBlank()) "-" else LatexEscaper.escape(value)
|
||||
}
|
||||
}
|
||||
|
||||
return buildString {
|
||||
appendLine("\\begin{tabular}{$columnSpec}")
|
||||
appendLine("\\hline")
|
||||
appendLine("$headerRow \\\\")
|
||||
appendLine("\\hline")
|
||||
dataRows.forEach { row: String ->
|
||||
appendLine("$row \\\\")
|
||||
}
|
||||
appendLine("\\hline")
|
||||
appendLine("\\end{tabular}")
|
||||
}
|
||||
}
|
||||
|
||||
private fun filterVisibleElements(snapshot: ApplicationFormSnapshotDto): ApplicationFormSnapshotDto {
|
||||
val allElements = collectAllFormElements(snapshot)
|
||||
val formElementsByRef = buildSnapshotFormElementsByRefMap(allElements)
|
||||
|
||||
Reference in New Issue
Block a user