Skip to content

Commit

Permalink
Refactor MalBestillingService and mapping strategies
Browse files Browse the repository at this point in the history
#deploy-test-dolly-backend

Removed unnecessary imports and refactored the createFromIdent method in MalBestillingService to improve readability. Added helper functions for JSON operations. In DollyRequest2MalBestillingMappingStrategy, adjusted the class mapping to use RsDollyUtvidetBestilling instead of RsDollyBestillingRequest for better consistency.
  • Loading branch information
krharum committed Apr 12, 2024
1 parent 139f7c4 commit ba8aec7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import ma.glasnost.orika.CustomMapper;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.MappingContext;
import no.nav.dolly.domain.resultset.RsDollyBestillingRequest;
import no.nav.dolly.domain.resultset.RsDollyUtvidetBestilling;
import no.nav.dolly.domain.resultset.arenaforvalter.Arenadata;
import no.nav.dolly.domain.resultset.inntektstub.InntektMultiplierWrapper;
Expand All @@ -22,7 +21,7 @@ public class DollyRequest2MalBestillingMappingStrategy implements MappingStrateg

@Override
public void register(MapperFactory factory) {
factory.classMap(RsDollyUtvidetBestilling.class, RsDollyBestillingRequest.class)
factory.classMap(RsDollyUtvidetBestilling.class, RsDollyUtvidetBestilling.class)
.mapNulls(false)
.field("arbeidsplassenCV", "arbeidsplassenCV")
.field("arenaforvalter", "arenaforvalter")
Expand All @@ -47,7 +46,7 @@ public void register(MapperFactory factory) {

.customize(new CustomMapper<>() {
@Override
public void mapAtoB(RsDollyUtvidetBestilling request, RsDollyBestillingRequest akkumulert, MappingContext context) {
public void mapAtoB(RsDollyUtvidetBestilling request, RsDollyUtvidetBestilling akkumulert, MappingContext context) {

akkumulert.getAareg().addAll(request.getAareg());
akkumulert.getEnvironments().addAll(request.getEnvironments());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import ma.glasnost.orika.MapperFacade;
import no.nav.dolly.domain.jpa.Bestilling;
import no.nav.dolly.domain.jpa.BestillingMal;
import no.nav.dolly.domain.jpa.Bruker;
import no.nav.dolly.domain.resultset.RsDollyBestillingRequest;
import no.nav.dolly.domain.resultset.RsDollyUtvidetBestilling;
import no.nav.dolly.domain.resultset.entity.bestilling.RsMalBestilling;
import no.nav.dolly.domain.resultset.entity.bestilling.RsMalBestillingWrapper;
Expand All @@ -18,14 +16,18 @@
import no.nav.dolly.repository.BestillingRepository;
import no.nav.testnav.libs.servletsecurity.action.GetUserInfo;
import org.apache.commons.collections4.IterableUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.server.ResponseStatusException;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -187,7 +189,6 @@ public RsMalBestilling updateMalNavnById(Long id, String nyttMalNavn) {
return oppdatertMalBestilling.get();
}

@SneakyThrows
@Transactional
public RsMalBestilling createFromIdent(String ident, String name) {

Expand All @@ -206,31 +207,28 @@ public RsMalBestilling createFromIdent(String ident, String name) {
.filter(bestilling -> isNull(bestilling.getOpprettetFraGruppeId()) &&
isNull(bestilling.getGjenopprettetFraIdent()) &&
isNull(bestilling.getOpprettetFraId()))
.map(bestilling -> {
try {
return objectMapper.readValue(bestilling.getBestKriterier(), RsDollyBestillingRequest.class);
} catch (JsonProcessingException e) {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage(), e);
}
})
.forEach(dollyBestilling -> mapperFacade.map(dollyBestilling, aggregertRequest));
.forEach(bestilling -> {
var dollyBestilling = fromJson(bestilling.getBestKriterier());
dollyBestilling.getEnvironments().addAll(toSet(bestilling.getMiljoer()));
dollyBestilling.setNavSyntetiskIdent(bestilling.getNavSyntetiskIdent());
mapperFacade.map(dollyBestilling, aggregertRequest);
});

BestillingMal akkumulertMal;

var maler = bestillingMalRepository.findByBrukerAndMalNavn(bruker, name);
if (maler.isEmpty()) {

akkumulertMal = bestillingMalRepository.save(BestillingMal.builder()
.bruker(bruker)
.malNavn(name)
.miljoer(String.join(",", aggregertRequest.getEnvironments()))
.bestKriterier(objectMapper.writeValueAsString(aggregertRequest))
.bestKriterier(toJson(aggregertRequest))
.build());
} else {

akkumulertMal = maler.getFirst();
akkumulertMal.setMiljoer(String.join(",", aggregertRequest.getEnvironments()));
akkumulertMal.setBestKriterier(objectMapper.writeValueAsString(aggregertRequest));
akkumulertMal.setBestKriterier(toJson(aggregertRequest));
}

return mapperFacade.map(akkumulertMal, RsMalBestilling.class);
Expand All @@ -246,4 +244,30 @@ public static String getBruker(Bruker bruker) {
case BASIC -> bruker.getNavIdent();
};
}

private static Set<String> toSet(String miljoer) {

return StringUtils.isNotBlank(miljoer) ?
Arrays.stream(miljoer.split(","))
.collect(Collectors.toSet()) :
Collections.emptySet();
}

private String toJson(RsDollyUtvidetBestilling bestilling) {

try {
return objectMapper.writeValueAsString(bestilling);
} catch (JsonProcessingException e) {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage(), e);
}
}

private RsDollyUtvidetBestilling fromJson(String json) {

try {
return objectMapper.readValue(json, RsDollyUtvidetBestilling.class);
} catch (JsonProcessingException e) {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage(), e);
}
}
}

0 comments on commit ba8aec7

Please sign in to comment.