Skip to content

Commit

Permalink
Refactor JSON handling in MigrateDokumentService.
Browse files Browse the repository at this point in the history
Deploy #deploy-test-dolly-backend

Extracted JSON conversion logic into dedicated methods `fromJson` and `toJson` to improve readability and reduce duplication. Improved error handling to ensure non-null values for `bestKriterier`, enhancing robustness.
  • Loading branch information
krharum committed Jan 9, 2025
1 parent f3d059c commit 811aa88
Showing 1 changed file with 33 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static no.nav.dolly.domain.jpa.Dokument.DokumentType.BESTILLING_HISTARK;
import static no.nav.dolly.domain.jpa.Dokument.DokumentType.MAL_BESTILLING_DOKARKIV;
import static no.nav.dolly.domain.jpa.Dokument.DokumentType.MAL_BESTILLING_HISTARK;
import static org.apache.commons.lang3.StringUtils.isNotBlank;

@Slf4j
@Service
Expand All @@ -48,14 +49,12 @@ private void migrateMalBestillinger(Iterable<BestillingMal> query, TriConsumer<R

StreamSupport.stream(Spliterators.spliteratorUnknownSize(
query.iterator(), Spliterator.ORDERED), false)
.forEach(bestilling -> {
try {
var utvidetBestilling = objectMapper.readValue(bestilling.getBestKriterier(), RsDollyUtvidetBestilling.class);
lagreDokument.apply(utvidetBestilling, bestilling.getId(), dokumentType);
var oppdatertBestilling = objectMapper.writeValueAsString(utvidetBestilling);
bestilling.setBestKriterier(oppdatertBestilling);
} catch (JsonProcessingException e) {
log.error("Feil ved konvertering av bestilling {}, {} ", bestilling.getId(), e.getMessage(), e);
.forEach(malBestilling -> {
var utvidetBestilling = fromJson(malBestilling.getBestKriterier(), malBestilling.getId());
if (nonNull(utvidetBestilling)) {
lagreDokument.apply(utvidetBestilling, malBestilling.getId(), dokumentType);
var oppdatertBestilling = toJson(utvidetBestilling, malBestilling.getId());
malBestilling.setBestKriterier(isNotBlank(oppdatertBestilling) ? oppdatertBestilling : malBestilling.getBestKriterier());
}
});
}
Expand All @@ -65,13 +64,11 @@ private void migrateBestillinger(Iterable<Bestilling> query, TriConsumer<RsDolly
StreamSupport.stream(Spliterators.spliteratorUnknownSize(
query.iterator(), Spliterator.ORDERED), false)
.forEach(bestilling -> {
try {
var utvidetBestilling = objectMapper.readValue(bestilling.getBestKriterier(), RsDollyUtvidetBestilling.class);
var utvidetBestilling = fromJson(bestilling.getBestKriterier(), bestilling.getId());
if (nonNull(utvidetBestilling)) {
lagreDokument.apply(utvidetBestilling, bestilling.getId(), dokumentType);
var oppdatertBestilling = objectMapper.writeValueAsString(utvidetBestilling);
bestilling.setBestKriterier(oppdatertBestilling);
} catch (JsonProcessingException e) {
log.error("Feil ved konvertering av bestilling {}, {} ", bestilling.getId(), e.getMessage(), e);
var oppdatertBestilling = toJson(utvidetBestilling, bestilling.getId());
bestilling.setBestKriterier(isNotBlank(oppdatertBestilling) ? oppdatertBestilling : bestilling.getBestKriterier());
}
});
}
Expand Down Expand Up @@ -109,4 +106,26 @@ private Long lagreDokument(String fysiskDokument, Long bestillingId, DokumentTyp
.bestillingId(bestillingId)
.build()).getId();
}

private RsDollyUtvidetBestilling fromJson(String kriterier, Long bestillingId) {

try {
return objectMapper.readValue(kriterier, RsDollyUtvidetBestilling.class);

} catch (JsonProcessingException e) {
log.error("Konvertering fra JSON av bestilling {} feilet: {} ", bestillingId, e.getMessage(), e);
return null;
}
}

private String toJson(RsDollyUtvidetBestilling dollyUtvidetBestilling, Long bestillingId) {

try {
return objectMapper.writeValueAsString(dollyUtvidetBestilling);

} catch (JsonProcessingException e) {
log.error("Konvertering til JSON av bestilling {} feilet, {} ", bestillingId, e.getMessage(), e);
return null;
}
}
}

0 comments on commit 811aa88

Please sign in to comment.