feat: import gottesdienste from zelebrationsplan
Some checks failed
Deploy Miniplan / build (push) Failing after 1m0s

This commit is contained in:
2025-04-16 00:40:20 +02:00
parent 67cbb650f0
commit 6a335247ca
8 changed files with 386 additions and 3 deletions

View File

@@ -44,6 +44,9 @@ dependencies {
implementation("io.github.cdimascio:dotenv-kotlin:6.4.1")
implementation("at.favre.lib:bcrypt:0.10.2")
implementation("org.apache.poi:poi:5.2.3") // Check for the latest version
implementation("org.apache.poi:poi-ooxml:5.2.3") // Check for the latest version
implementation("org.apache.xmlbeans:xmlbeans:5.0.2") // Check for the latest version
testImplementation("io.ktor:ktor-server-test-host")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")

View File

@@ -10,9 +10,13 @@ fun Application.configureHTTP() {
allowMethod(HttpMethod.Put)
allowMethod(HttpMethod.Delete)
allowMethod(HttpMethod.Patch)
allowMethod(HttpMethod.Post)
allowMethod(HttpMethod.Options)
allowHeader(HttpHeaders.Authorization)
allowHeader(HttpHeaders.AccessControlAllowOrigin)
allowHeader(HttpHeaders.ContentType)
allowHeader(HttpHeaders.CacheControl)
allowHeader("x-requested-with")
allowNonSimpleContentTypes = true
// allowHeader("MyCustomHeader")
anyHost() // @TODO: Don't do this in production if possible. Try to limit it.

View File

@@ -0,0 +1,83 @@
package de.walamana.service
import de.walamana.models.Gottesdienst
import org.apache.poi.ss.usermodel.Row
import org.apache.poi.xssf.usermodel.XSSFWorkbook
import java.io.File
import java.io.InputStream
import java.lang.Exception
import java.time.*
import java.time.format.DateTimeFormatter
import java.util.Date
object ZelebrationsplanParser {
private val weekdays = listOf("Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag")
fun parse(ins: InputStream, planId: Int): HashMap<String, ArrayList<Gottesdienst>> {
val workbook = XSSFWorkbook(ins)
val sheet = workbook.getSheetAt(0)
var currentDate: String? = null
val gottesdienste = hashMapOf<String, ArrayList<Gottesdienst>>()
for (row in sheet) {
val r = rowToArray(row)
val weekday = weekdays.firstOrNull { r[0]?.startsWith(it) == true }
if(weekday != null) {
currentDate = r[0]!!
}else if(r[0]?.isBlank() == true) {
println("Empty line")
}else if(r.all { it != null }){
try {
val time = formatTime(currentDate!!, r[1]!!)
val pfarrei = r[0]!!
val attendance = time - Duration.ofMinutes(30)
gottesdienste[pfarrei] = gottesdienste.getOrElse(pfarrei) { arrayListOf() }.apply {
add(Gottesdienst(
id = 0,
name = r[2]!!,
date = time.toDate(),
attendance = attendance.toDate(),
planId = planId
))
}
} catch (e: Exception) {
e.printStackTrace()
}
}
}
return gottesdienste
}
private fun formatTime(date: String, time: String): LocalDateTime {
try{
return LocalDateTime.parse("$date, $time", DateTimeFormatter.ofPattern("EEEE, d. MMMM yyyy, H.mm 'Uhr'"))
} catch (_: Exception) {}
try{
return LocalDateTime.parse("$date, $time", DateTimeFormatter.ofPattern("EEEE, d. MMMM yyyy, H:mm 'Uhr'"))
} catch (_: Exception) {}
try{
return LocalDateTime.parse("$date, $time", DateTimeFormatter.ofPattern("EEEE, d. MMMM yyyy, 'a. 'H.mm 'Uhr'"))
} catch (_: Exception) {}
return LocalDateTime.parse("$date, $time", DateTimeFormatter.ofPattern("EEEE, d. MMMM yyyy, 'a. 'H:mm 'Uhr'"))
}
private fun rowToArray(row: Row): Array<String?> {
return arrayOf(row.getCell(0)?.stringCellValue, row.getCell(1)?.stringCellValue, row.getCell(2)?.stringCellValue, row.getCell(3)?.stringCellValue)
}
private fun LocalDateTime.toDate() = Date.from(this.atZone(ZoneId.systemDefault()).toInstant())
}
fun main() {
File("Zelebrationsplan Mai 2025.xlsx").inputStream().use {
ZelebrationsplanParser.parse(it, 0)
}
}

View File

@@ -1,9 +1,10 @@
package de.walamana.views
import de.walamana.models.Gottesdienst
import de.walamana.models.Gottesdienste
import de.walamana.models.GottesdiensteDao
import de.walamana.service.ZelebrationsplanParser
import io.ktor.http.*
import io.ktor.http.content.*
import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.response.*
@@ -43,5 +44,17 @@ fun Route.configureGottesdiensteRoutes() {
GottesdiensteDao.deleteGottesdienst(id)
call.respond(HttpStatusCode.OK)
}
post("/parseZelebrationsplan") {
val id = call.parameters.getOrFail("id").toInt()
val multipartData = call.receiveMultipart()
multipartData.forEachPart { part ->
if(part is PartData.FileItem) {
val data = ZelebrationsplanParser.parse(part.streamProvider(), planId = id)
call.respond(data)
}
part.dispose()
}
}
}
}