Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/refaktorere dokumenter fra bestilling og maler #3698

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import no.nav.dolly.domain.resultset.dokarkiv.RsDokarkiv;
import no.nav.dolly.domain.resultset.dolly.DollyPerson;
import no.nav.dolly.errorhandling.ErrorStatusDecoder;
import no.nav.dolly.service.DokumentService;
import no.nav.dolly.service.TransaksjonMappingService;
import no.nav.dolly.util.TransactionHelperService;
import no.nav.testnav.libs.reactivecore.utils.WebClientFilter;
Expand Down Expand Up @@ -47,6 +48,7 @@ public class DokarkivClient implements ClientRegister {

private final ApplicationConfig applicationConfig;
private final DokarkivConsumer dokarkivConsumer;
private final DokumentService dokumentService;
private final ErrorStatusDecoder errorStatusDecoder;
private final MapperFacade mapperFacade;
private final ObjectMapper objectMapper;
Expand All @@ -63,7 +65,7 @@ public Flux<ClientFuture> gjenopprett(RsDollyUtvidetBestilling bestilling, Dolly
.flatMap(dokarkiv -> Flux.from(getPersonData(dollyPerson.getIdent())
.flatMap(person -> getFilteredMiljoer(bestilling.getEnvironments())
.flatMapMany(miljoer -> Flux.fromIterable(miljoer)
.flatMap(env -> buildRequest(dokarkiv, person)
.flatMap(env -> buildRequest(dokarkiv, person, progress.getBestilling().getId())
.flatMap(request ->
!transaksjonMappingService.existAlready(DOKARKIV,
dollyPerson.getIdent(), env, bestilling.getId()) || isOpprettEndre ?
Expand Down Expand Up @@ -142,10 +144,11 @@ private Flux<PdlPersonBolk.PersonBolk> getPersonData(String ident) {
.filter(personBolk -> nonNull(personBolk.getPerson()));
}

private Mono<DokarkivRequest> buildRequest(RsDokarkiv rsDokarkiv, PdlPersonBolk.PersonBolk personBolk) {
private Mono<DokarkivRequest> buildRequest(RsDokarkiv rsDokarkiv, PdlPersonBolk.PersonBolk personBolk, Long bestillingId) {

var context = new MappingContext.Factory().getContext();
context.setProperty("personBolk", personBolk);
context.setProperty("dokumenter", dokumentService.getDokumenter(bestillingId));

return Mono.just(mapperFacade.map(rsDokarkiv, DokarkivRequest.class, context));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package no.nav.dolly.bestilling.dokarkiv.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down Expand Up @@ -100,6 +101,8 @@ public static class DokumentVariant {
private String filtype;
private String fysiskDokument;
private String variantformat;
@JsonIgnore
private Long dokumentReferanse;

@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,5 @@ public List<DokumentInfo> getDokumenter() {
public static class DokumentInfo {

private String dokumentInfoId;
private String brevkode;
private String tittel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
import no.nav.dolly.bestilling.dokarkiv.domain.DokarkivRequest;
import no.nav.dolly.domain.PdlPerson;
import no.nav.dolly.domain.PdlPersonBolk;
import no.nav.dolly.domain.jpa.Dokument;
import no.nav.dolly.domain.resultset.dokarkiv.RsDokarkiv;
import no.nav.dolly.mapper.MappingStrategy;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import static java.util.Objects.isNull;
Expand All @@ -37,6 +39,7 @@ public void register(MapperFactory factory) {
@Override
public void mapAtoB(RsDokarkiv rsDokarkiv, DokarkivRequest dokarkivRequest, MappingContext context) {

var dokumenter = (List<Dokument>) context.getProperty("dokumenter");

dokarkivRequest.setEksternReferanseId(UUID.randomUUID().toString());
dokarkivRequest.setTittel(rsDokarkiv.getTittel());
Expand Down Expand Up @@ -72,7 +75,7 @@ public void mapAtoB(RsDokarkiv rsDokarkiv, DokarkivRequest dokarkivRequest, Mapp

dokarkivRequest.getDokumenter()
.addAll(mapperFacade.mapAsList(rsDokarkiv.getDokumenter(), DokarkivRequest.Dokument.class));
fyllDokarkivDokument(dokarkivRequest);
fyllDokarkivDokument(dokarkivRequest, dokumenter);
dokarkivRequest.setFerdigstill(rsDokarkiv.getFerdigstill());
}

Expand All @@ -86,7 +89,7 @@ private String getNavn(PdlPersonBolk.PersonBolk personBolk) {
.register();
}

private void fyllDokarkivDokument(DokarkivRequest dokarkivRequest) {
private void fyllDokarkivDokument(DokarkivRequest dokarkivRequest, List<Dokument> dokumenter) {

if (dokarkivRequest.getDokumenter().isEmpty()) {
dokarkivRequest.getDokumenter().add(new DokarkivRequest.Dokument());
Expand All @@ -100,8 +103,12 @@ private void fyllDokarkivDokument(DokarkivRequest dokarkivRequest) {
if (isBlank(dokarkivRequest.getDokumenter().getFirst().getDokumentvarianter().getFirst().getVariantformat())) {
dokarkivRequest.getDokumenter().getFirst().getDokumentvarianter().getFirst().setVariantformat(ARKIV);
}
if (isBlank(dokarkivRequest.getDokumenter().getFirst().getDokumentvarianter().getFirst().getFysiskDokument())) {
dokarkivRequest.getDokumenter().getFirst().getDokumentvarianter().getFirst().setFysiskDokument(PDF_VEDLEGG);
}
dokarkivRequest.getDokumenter()
.forEach(dokument -> dokument.getDokumentvarianter()
.forEach(dokumentVariant ->
dokumentVariant.setFysiskDokument(dokumenter.stream()
.filter(doku -> doku.getId().equals(dokumentVariant.getDokumentReferanse()))
.map(Dokument::getContents)
.findFirst().orElse(PDF_VEDLEGG))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import no.nav.dolly.domain.resultset.dolly.DollyPerson;
import no.nav.dolly.domain.resultset.histark.RsHistark;
import no.nav.dolly.errorhandling.ErrorStatusDecoder;
import no.nav.dolly.service.DokumentService;
import no.nav.dolly.service.TransaksjonMappingService;
import no.nav.dolly.util.TransactionHelperService;
import org.springframework.stereotype.Service;
Expand All @@ -43,14 +44,15 @@ public class HistarkClient implements ClientRegister {
private final ObjectMapper objectMapper;
private final TransactionHelperService transactionHelperService;
private final ErrorStatusDecoder errorStatusDecoder;
private final DokumentService dokumentService;

@Override
public Flux<ClientFuture> gjenopprett(RsDollyUtvidetBestilling bestilling, DollyPerson dollyPerson, BestillingProgress progress, boolean isOpprettEndre) {

if (nonNull(bestilling.getHistark())) {

return Flux.just(dollyPerson.getIdent())
.map(person -> buildRequest(bestilling.getHistark(), person))
.map(person -> buildRequest(bestilling.getHistark(), person, progress.getBestilling().getId()))
.flatMap(request -> !transaksjonMappingService.existAlready(HISTARK,
dollyPerson.getIdent(), "NA", bestilling.getId()) || isOpprettEndre ?

Expand Down Expand Up @@ -101,10 +103,11 @@ private String getStatus(String ident, Long bestillingId, HistarkResponse respon
}
}

private HistarkRequest buildRequest(RsHistark rsHistark, String ident) {
private HistarkRequest buildRequest(RsHistark rsHistark, String ident, Long bestillingId) {

var context = new MappingContext.Factory().getContext();
context.setProperty("personIdent", ident);
context.setProperty("dokumenter", dokumentService.getDokumenter(bestillingId));

return mapperFacade.map(rsHistark, HistarkRequest.class, context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.MappingContext;
import no.nav.dolly.bestilling.histark.domain.HistarkRequest;
import no.nav.dolly.domain.jpa.Dokument;
import no.nav.dolly.domain.resultset.histark.RsHistark;
import no.nav.dolly.exceptions.DollyFunctionalException;
import no.nav.dolly.mapper.MappingStrategy;
Expand All @@ -16,9 +17,9 @@
import java.io.IOException;
import java.io.InputStream;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Optional;

import static io.micrometer.common.util.StringUtils.isBlank;
import static no.nav.dolly.bestilling.dokarkiv.mapper.PdfVedlegg.PDF_VEDLEGG;

@Slf4j
Expand All @@ -36,11 +37,15 @@ public void register(MapperFactory factory) {
@Override
public void mapAtoB(RsHistark histark, HistarkRequest histarkRequest, MappingContext context) {

var dokumenter = (List<Dokument>) context.getProperty("dokumenter");

histark.getDokumenter().forEach(dokument -> {


String fysiskDokument = isBlank(dokument.getFysiskDokument()) ? PDF_VEDLEGG : dokument.getFysiskDokument();
var fysiskDokument = dokumenter.stream()
.filter(dok -> dokument.getDokumentReferanse().equals(dok.getId()))
.map(Dokument::getContents)
.findFirst()
.orElse(PDF_VEDLEGG);

histarkRequest.getHistarkDokumenter().add(HistarkRequest.HistarkDokument.builder()
.file(fysiskDokument)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package no.nav.dolly.config;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import no.nav.dolly.service.MigrateDokumentService;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

@Slf4j
@Configuration
@RequiredArgsConstructor
public class MigrationConfig {

private final MigrateDokumentService migrateDokumentService;

@PostConstruct
public void migrateDokumenter() {

var time = System.currentTimeMillis();
log.info("Starter migrering av dokumenter.");

migrateDokumentService.migrateDokumenter();

log.info("Migrering av dokumenter ferdig, medgått tid: {} sekunder", (System.currentTimeMillis() - time) / 1000);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package no.nav.dolly.domain.jpa;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import jakarta.persistence.Version;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.hibernate.annotations.UpdateTimestamp;

import java.io.Serializable;
import java.time.LocalDateTime;

@Entity
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "DOKUMENT")
public class Dokument implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Version
@Column(name = "VERSJON")
private Long versjon;

@Column(name = "BESTILLING_ID")
private Long bestillingId;

@Column(name = "DOKUMENT_TYPE")
@Enumerated(value = EnumType.STRING)
private DokumentType dokumentType;

@Column(name = "SIST_OPPDATERT")
@UpdateTimestamp
private LocalDateTime sistOppdatert;

@Column(name = "CONTENTS")
private String contents;

@Override
public boolean equals(Object o) {
if (this == o) return true;

if (o == null || getClass() != o.getClass()) return false;

Dokument dokument = (Dokument) o;

return new EqualsBuilder()
.append(id, dokument.id)
.append(versjon, dokument.versjon)
.append(bestillingId, dokument.bestillingId)
.append(dokumentType, dokument.dokumentType)
.append(sistOppdatert, dokument.sistOppdatert)
.append(contents, dokument.contents)
.isEquals();
}

@Override
public int hashCode() {
return new HashCodeBuilder(17, 37)
.append(id)
.append(versjon)
.append(bestillingId)
.append(dokumentType)
.append(sistOppdatert)
.append(contents)
.toHashCode();
}

public enum DokumentType {
BESTILLING_DOKARKIV,
BESTILLING_HISTARK,
MAL_BESTILLING_DOKARKIV,
MAL_BESTILLING_HISTARK
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ public static class DokumentVariant {
@Field(enabled = false)
private String fysiskDokument;

@Schema(description = "Referanse til dokumentet")
private Long dokumentReferanse;

@Schema(description = """
ARKIV brukes for dokumentvarianter i menneskelesbart format (for eksempel PDFA).
Gosys og nav.no henter arkivvariant og viser denne til bruker.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,8 @@ public static class RsHistarkDokument {
@Schema(description = "Selve PDF dokumentet. Ved fysisk dokument brukes bytearray.")
@Field(enabled = false)
private String fysiskDokument;

@Schema(description = "Referanse til dokumentet")
private Long dokumentReferanse;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package no.nav.dolly.provider.api;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import no.nav.dolly.domain.jpa.Dokument;
import no.nav.dolly.service.DokumentService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/dokument")
public class DokumentController {

private final DokumentService dokumentService;

@Operation(description = "Henter dokumenter basert på bestillingId")
@GetMapping("/bestilling/{bestillingId}")
public List<Dokument> getDokumenter(@Parameter(description = "bestillingId kan være fra bestilling eller malbestilling")
@PathVariable("bestillingId") Long bestilllingId) {

return dokumentService.getDokumenter(bestilllingId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,19 @@ public interface BestillingMalRepository extends CrudRepository<BestillingMal, L

List<BestillingMal> findByBruker(Bruker bruker);

@Query(value = """
from BestillingMal b
where b.bestKriterier like '%dokarkiv%'
and length(b.bestKriterier) > 10000
order by b.id
""")
Iterable<BestillingMal> findAllByDokumentArkiv();

@Query(value = """
from BestillingMal b
where b.bestKriterier like '%histark%'
and length(b.bestKriterier) > 10000
order by b.id
""")
Iterable<BestillingMal> findAllByHistArk();
}
Loading
Loading