Skip to content

Commit

Permalink
Make sure to unproxy before type check.
Browse files Browse the repository at this point in the history
  • Loading branch information
flexable777 committed Dec 27, 2023
1 parent 2166d9c commit cc68355
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 39 deletions.
55 changes: 30 additions & 25 deletions src/main/kotlin/no/nav/klage/dokument/api/mapper/DokumentMapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import no.nav.klage.oppgave.domain.klage.Saksdokument
import no.nav.klage.oppgave.util.getLogger
import no.nav.klage.oppgave.util.getSecureLogger
import no.nav.klage.oppgave.util.getSortKey
import org.hibernate.Hibernate
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
Expand Down Expand Up @@ -117,44 +118,46 @@ class DokumentMapper {
}

fun mapToDokumentView(dokumentUnderArbeid: DokumentUnderArbeid, journalpost: Journalpost?): DokumentView {
val unproxiedDUA = Hibernate.unproxy(dokumentUnderArbeid) as DokumentUnderArbeid

var journalfoertDokumentReference: DokumentView.JournalfoertDokumentReference? = null

var tittel = dokumentUnderArbeid.name
var tittel = unproxiedDUA.name

if (dokumentUnderArbeid is JournalfoertDokumentUnderArbeidAsVedlegg) {
if (unproxiedDUA is JournalfoertDokumentUnderArbeidAsVedlegg) {
if (journalpost == null) {
throw RuntimeException("Need journalpost to handle JournalfoertDokumentUnderArbeidAsVedlegg")
}
val dokument =
journalpost.dokumenter?.find { it.dokumentInfoId == dokumentUnderArbeid.dokumentInfoId }
journalpost.dokumenter?.find { it.dokumentInfoId == unproxiedDUA.dokumentInfoId }
?: throw RuntimeException("Document not found in Dokarkiv")

tittel = (dokument.tittel ?: "Tittel ikke funnet i SAF")

journalfoertDokumentReference = DokumentView.JournalfoertDokumentReference(
journalpostId = dokumentUnderArbeid.journalpostId,
dokumentInfoId = dokumentUnderArbeid.dokumentInfoId,
journalpostId = unproxiedDUA.journalpostId,
dokumentInfoId = unproxiedDUA.dokumentInfoId,
harTilgangTilArkivvariant = harTilgangTilArkivvariant(dokument),
datoOpprettet = dokumentUnderArbeid.opprettet,
sortKey = dokumentUnderArbeid.sortKey!!
datoOpprettet = unproxiedDUA.opprettet,
sortKey = unproxiedDUA.sortKey!!
)
}

return DokumentView(
id = dokumentUnderArbeid.id,
id = unproxiedDUA.id,
tittel = tittel,
dokumentTypeId = dokumentUnderArbeid.dokumentType?.id,
created = dokumentUnderArbeid.created,
modified = dokumentUnderArbeid.modified,
isSmartDokument = dokumentUnderArbeid is DokumentUnderArbeidAsSmartdokument,
templateId = if (dokumentUnderArbeid is DokumentUnderArbeidAsSmartdokument) dokumentUnderArbeid.smartEditorTemplateId else null,
isMarkertAvsluttet = dokumentUnderArbeid.markertFerdig != null,
parent = if (dokumentUnderArbeid is DokumentUnderArbeidAsVedlegg) dokumentUnderArbeid.parentId else null,
parentId = if (dokumentUnderArbeid is DokumentUnderArbeidAsVedlegg) dokumentUnderArbeid.parentId else null,
type = dokumentUnderArbeid.getType(),
dokumentTypeId = unproxiedDUA.dokumentType?.id,
created = unproxiedDUA.created,
modified = unproxiedDUA.modified,
isSmartDokument = unproxiedDUA is DokumentUnderArbeidAsSmartdokument,
templateId = if (unproxiedDUA is DokumentUnderArbeidAsSmartdokument) unproxiedDUA.smartEditorTemplateId else null,
isMarkertAvsluttet = unproxiedDUA.markertFerdig != null,
parent = if (unproxiedDUA is DokumentUnderArbeidAsVedlegg) unproxiedDUA.parentId else null,
parentId = if (unproxiedDUA is DokumentUnderArbeidAsVedlegg) unproxiedDUA.parentId else null,
type = unproxiedDUA.getType(),
journalfoertDokumentReference = journalfoertDokumentReference,
creatorIdent = dokumentUnderArbeid.creatorIdent,
creatorRole = dokumentUnderArbeid.creatorRole,
creatorIdent = unproxiedDUA.creatorIdent,
creatorRole = unproxiedDUA.creatorRole,
)
}

Expand All @@ -163,7 +166,7 @@ class DokumentMapper {
duplicateJournalfoerteDokumenter: List<DokumentUnderArbeid>,
journalpostList: List<Journalpost>,
): DokumentViewWithList {
val firstDokument = dokumentUnderArbeidList.firstOrNull()
val firstDokument = Hibernate.unproxy(dokumentUnderArbeidList.firstOrNull())
val firstDokumentView = if (firstDokument != null) {
if (firstDokument is JournalfoertDokumentUnderArbeidAsVedlegg) {
mapToDokumentView(
Expand All @@ -188,19 +191,21 @@ class DokumentMapper {
parentId = firstDokumentView?.parentId,
journalfoertDokumentReference = firstDokumentView?.journalfoertDokumentReference,
alteredDocuments = dokumentUnderArbeidList.map { dokumentUnderArbeid ->
if (dokumentUnderArbeid is JournalfoertDokumentUnderArbeidAsVedlegg) {
val duaUnproxied = Hibernate.unproxy(dokumentUnderArbeid)
if (duaUnproxied is JournalfoertDokumentUnderArbeidAsVedlegg) {
mapToDokumentView(
dokumentUnderArbeid = dokumentUnderArbeid,
journalpost = journalpostList.find { it.journalpostId == dokumentUnderArbeid.journalpostId })
journalpost = journalpostList.find { it.journalpostId == duaUnproxied.journalpostId })
} else {
mapToDokumentView(dokumentUnderArbeid = dokumentUnderArbeid, journalpost = null)
}
},
duplicateJournalfoerteDokumenter = duplicateJournalfoerteDokumenter.map { duplicateJournalfoertDokument ->
if (duplicateJournalfoertDokument is JournalfoertDokumentUnderArbeidAsVedlegg) {
val duaUnproxied = Hibernate.unproxy(duplicateJournalfoertDokument)
if (duaUnproxied is JournalfoertDokumentUnderArbeidAsVedlegg) {
mapToDokumentView(
dokumentUnderArbeid = duplicateJournalfoertDokument,
journalpost = journalpostList.find { it.journalpostId == duplicateJournalfoertDokument.journalpostId })
dokumentUnderArbeid = duaUnproxied,
journalpost = journalpostList.find { it.journalpostId == duaUnproxied.journalpostId })
} else {
mapToDokumentView(dokumentUnderArbeid = duplicateJournalfoertDokument, journalpost = null)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import jakarta.persistence.*
import no.nav.klage.kodeverk.DokumentType
import no.nav.klage.kodeverk.DokumentTypeConverter
import no.nav.klage.oppgave.domain.klage.BehandlingRole
import org.hibernate.Hibernate
import org.hibernate.annotations.BatchSize
import org.hibernate.annotations.DynamicUpdate
import org.hibernate.annotations.Fetch
Expand Down Expand Up @@ -100,7 +101,7 @@ abstract class DokumentUnderArbeid(
}

fun getType(): DokumentUnderArbeidType {
return when (this) {
return when (Hibernate.unproxy(this)) {
is DokumentUnderArbeidAsSmartdokument -> {
DokumentUnderArbeidType.SMART
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import no.nav.klage.oppgave.service.InnloggetSaksbehandlerService
import no.nav.klage.oppgave.util.getLogger
import no.nav.klage.oppgave.util.getSecureLogger
import no.nav.klage.oppgave.util.getSortKey
import org.hibernate.Hibernate
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.ApplicationEventPublisher
import org.springframework.http.MediaType
Expand Down Expand Up @@ -89,7 +90,7 @@ class DokumentUnderArbeidService(
if (behandling.avsluttetAvSaksbehandler == null) {
validateCanCreateDocuments(
behandlingRole = behandlingRole,
parentDocument = if (parentId != null) dokumentUnderArbeidRepository.getReferenceById(parentId) else null
parentDocument = if (parentId != null) dokumentUnderArbeidRepository.findById(parentId).get() else null
)
}

Expand Down Expand Up @@ -202,7 +203,7 @@ class DokumentUnderArbeidService(
if (behandling.avsluttetAvSaksbehandler == null) {
validateCanCreateDocuments(
behandlingRole = behandlingRole,
parentDocument = if (parentId != null) dokumentUnderArbeidRepository.getReferenceById(parentId) else null
parentDocument = if (parentId != null) dokumentUnderArbeidRepository.findById(parentId).get() else null
)
}

Expand Down Expand Up @@ -318,7 +319,7 @@ class DokumentUnderArbeidService(
innloggetIdent: String,
journalpostListForUser: List<Journalpost>,
): Pair<List<JournalfoertDokumentUnderArbeidAsVedlegg>, List<JournalfoertDokumentReference>> {
val parentDocument = dokumentUnderArbeidRepository.getReferenceById(parentId)
val parentDocument = dokumentUnderArbeidRepository.findById(parentId).get()

if (parentDocument.erMarkertFerdig()) {
throw DokumentValidationException("Kan ikke koble til et dokument som er ferdigstilt")
Expand Down Expand Up @@ -476,9 +477,10 @@ class DokumentUnderArbeidService(
}

private fun DokumentUnderArbeid.isVedlegg(): Boolean {
return this is SmartdokumentUnderArbeidAsVedlegg ||
this is OpplastetDokumentUnderArbeidAsVedlegg ||
this is JournalfoertDokumentUnderArbeidAsVedlegg
val duaUnproxied = Hibernate.unproxy(this)
return duaUnproxied is SmartdokumentUnderArbeidAsVedlegg ||
duaUnproxied is OpplastetDokumentUnderArbeidAsVedlegg ||
duaUnproxied is JournalfoertDokumentUnderArbeidAsVedlegg
}

fun updateDokumentTitle(
Expand All @@ -487,8 +489,7 @@ class DokumentUnderArbeidService(
dokumentTitle: String,
innloggetIdent: String
): DokumentUnderArbeid {

val dokument = dokumentUnderArbeidRepository.getReferenceById(dokumentId)
val dokument = dokumentUnderArbeidRepository.findById(dokumentId).get()

val behandling = behandlingService.getBehandlingAndCheckLeseTilgangForPerson(dokument.behandlingId)

Expand Down Expand Up @@ -523,7 +524,7 @@ class DokumentUnderArbeidService(
fun validateDocument(
dokumentId: UUID,
) {
val dokument = dokumentUnderArbeidRepository.getReferenceById(dokumentId)
val dokument = dokumentUnderArbeidRepository.findById(dokumentId).get()
if (dokument.erMarkertFerdig()) {
throw DokumentValidationException("Dokument er allerede ferdigstilt.")
}
Expand Down Expand Up @@ -600,7 +601,7 @@ class DokumentUnderArbeidService(
): List<DocumentValidationResponse> {
val documentValidationResults = mutableListOf<DocumentValidationResponse>()

val hovedDokument = dokumentUnderArbeidRepository.getReferenceById(dokumentId)
val hovedDokument = dokumentUnderArbeidRepository.findById(dokumentId).get()
val vedlegg = getVedlegg(hovedDokument.id)

(vedlegg + hovedDokument).forEach {
Expand Down Expand Up @@ -638,7 +639,7 @@ class DokumentUnderArbeidService(
ident: String,
brevmottakerIdents: Set<String>?,
): DokumentUnderArbeid {
val hovedDokument = dokumentUnderArbeidRepository.getReferenceById(dokumentId)
val hovedDokument = dokumentUnderArbeidRepository.findById(dokumentId).get()

if (hovedDokument !is DokumentUnderArbeidAsHoveddokument) {
throw RuntimeException("document is not hoveddokument")
Expand Down Expand Up @@ -1142,7 +1143,7 @@ class DokumentUnderArbeidService(
fun ferdigstillDokumentEnhet(hovedDokumentId: UUID): DokumentUnderArbeidAsHoveddokument {
logger.debug("ferdigstillDokumentEnhet hoveddokument with id {}", hovedDokumentId)
val hovedDokument =
dokumentUnderArbeidRepository.getReferenceById(hovedDokumentId) as DokumentUnderArbeidAsHoveddokument
dokumentUnderArbeidRepository.findById(hovedDokumentId).get() as DokumentUnderArbeidAsHoveddokument
val vedlegg = dokumentUnderArbeidCommonService.findVedleggByParentId(hovedDokument.id)
val behandling: Behandling =
behandlingService.getBehandlingForReadWithoutCheckForAccess(hovedDokument.behandlingId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class InnholdsfortegnelseService(
fun getInnholdsfortegnelseAsPdf(dokumentUnderArbeidId: UUID, fnr: String): ByteArray {
logger.debug("Received getInnholdsfortegnelseAsPdf")

val document = dokumentUnderArbeidRepository.getReferenceById(dokumentUnderArbeidId)
val document = dokumentUnderArbeidRepository.findById(dokumentUnderArbeidId).get()

if (document is DokumentUnderArbeidAsVedlegg) {
throw IllegalArgumentException("must be hoveddokument")
Expand Down

0 comments on commit cc68355

Please sign in to comment.