/*
* This file is part of ETL-Processor
*
* Copyright (c) 2023 Comprehensive Cancer Center Mainfranken
* Copyright (c) 2023-2026 Paul-Christian Volkmer, Datenintegrationszentrum Philipps-Universität Marburg and Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
package dev.dnpm.etl.processor.services
import dev.dnpm.etl.processor.PatientPseudonym
import dev.dnpm.etl.processor.RequestId
import dev.dnpm.etl.processor.Tan
import dev.dnpm.etl.processor.monitoring.*
import dev.dnpm.etl.processor.toPage
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.stereotype.Service
import java.util.*
@Service
class RequestService(private val requestRepository: RequestRepository) {
fun save(request: Request) = requestRepository.save(request)
fun findAll(): Iterable = requestRepository.findAll()
fun findAll(pageable: Pageable): Page = requestRepository.findAll(pageable)
fun searchRequestLike(patientPseudonym: PatientPseudonym, tan: Tan, pageable: Pageable): Page =
requestRepository.findByPatientPseudonymContainingIgnoreCaseOrTanContainingIgnoreCase(patientPseudonym, tan, pageable)
fun searchRequestLike(patientPseudonym: PatientPseudonym, tan: Tan): List =
requestRepository.findByPatientPseudonymContainingIgnoreCaseOrTanContainingIgnoreCase(patientPseudonym, tan)
fun findByUuid(uuid: RequestId): Optional = requestRepository.findByUuidEquals(uuid)
fun findRequestByPatientId(
patientPseudonym: PatientPseudonym,
pageable: Pageable,
): Page = requestRepository.findRequestByPatientPseudonym(patientPseudonym, pageable)
fun allRequestsByPatientPseudonym(patientPseudonym: PatientPseudonym) =
requestRepository.findAllByPatientPseudonymOrderByProcessedAtDesc(patientPseudonym)
fun lastMtbFileRequestForPatientPseudonym(patientPseudonym: PatientPseudonym) =
Companion.lastMtbFileRequestForPatientPseudonym(
allRequestsByPatientPseudonym(patientPseudonym)
)
fun isLastRequestWithKnownStatusDeletion(patientPseudonym: PatientPseudonym) =
Companion.isLastRequestWithKnownStatusDeletion(
allRequestsByPatientPseudonym(patientPseudonym)
)
fun countStates(): Iterable = requestRepository.countStates()
fun countDeleteStates(): Iterable = requestRepository.countDeleteStates()
fun findPatientUniqueStates(): List = requestRepository.findPatientUniqueStates()
fun findPatientUniqueDeleteStates(): List =
requestRepository.findPatientUniqueDeleteStates()
companion object {
fun lastMtbFileRequestForPatientPseudonym(allRequests: List) =
allRequests
.filter { it.type == RequestType.MTB_FILE }
.sortedByDescending { it.processedAt }
.firstOrNull {
it.status == RequestStatus.SUCCESS || it.status == RequestStatus.WARNING
}
fun isLastRequestWithKnownStatusDeletion(allRequests: List) =
allRequests
.filter { it.status != RequestStatus.UNKNOWN }
.maxByOrNull { it.processedAt }
?.type == RequestType.DELETE
}
enum class Filter(val value: String) {
ALL_DIP("all-dip"),
CONFIRMED("confirmed"),
UNCONFIRMED("unconfirmed");
}
}
fun List.filter(filter: RequestService.Filter, pageable: Pageable): Page {
return this
.toList()
.sortedByDescending { it.processedAt }
.filter {
it.type == RequestType.MTB_FILE
&& sequenceOf(RequestStatus.SUCCESS, RequestStatus.WARNING).contains(it.status)
}
.filter {
filter == RequestService.Filter.ALL_DIP
|| filter == RequestService.Filter.CONFIRMED && it.submissionAccepted
|| filter == RequestService.Filter.UNCONFIRMED && !it.submissionAccepted
}
.toPage(pageable)
}