Skip to content

Commit

Permalink
Fikset noen avhengigheter++
Browse files Browse the repository at this point in the history
  • Loading branch information
farjamm committed Jul 10, 2024
1 parent f3267f4 commit 1d7228f
Show file tree
Hide file tree
Showing 43 changed files with 239 additions and 203 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.FieldNameConstants;
import no.nav.data.common.auditing.dto.AuditResponse;
import no.nav.data.common.auditing.event.EventResponse;
import no.nav.data.common.utils.JsonUtils;
import no.nav.data.polly.codelist.CodelistService;
import no.nav.data.polly.codelist.CodelistStaticService;
import no.nav.data.polly.codelist.domain.Codelist;
import no.nav.data.polly.codelist.domain.ListName;
import no.nav.data.polly.policy.domain.Policy;
Expand Down Expand Up @@ -110,7 +109,7 @@ private List<String> getPurposes(JsonNode json) {
var purposes = purposesNewFormat.isEmpty() ? Optional.ofNullable(json.get("purposeCode")).map(JsonNode::textValue).map(List::of).orElse(List.of()) : purposesNewFormat;
return purposes.stream()
.map(purpose -> {
Codelist codelist = CodelistService.getCodelist(ListName.PURPOSE, purpose);
Codelist codelist = CodelistStaticService.getCodelist(ListName.PURPOSE, purpose);
return codelist == null ? purpose : codelist.getShortName();
})
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package no.nav.data.common.validator;

import no.nav.data.polly.codelist.CodelistService;
import no.nav.data.polly.codelist.CodelistStaticService;
import no.nav.data.polly.codelist.domain.ListName;
import org.apache.commons.lang3.EnumUtils;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -136,7 +136,7 @@ public void checkCodelist(String fieldName, String fieldValue, ListName listName
}

private void checkCode(String fieldName, String fieldValue, ListName listName) {
if (CodelistService.getCodelist(listName, fieldValue) == null) {
if (CodelistStaticService.getCodelist(listName, fieldValue) == null) {
validationErrors.add(new ValidationError(reference, ERROR_TYPE_CODELIST, String.format(ERROR_MESSAGE_CODELIST, getFieldName(fieldName), fieldValue, listName)));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.List;
import java.util.stream.Collectors;

import static no.nav.data.common.utils.StreamUtils.convert;
import static no.nav.data.common.utils.StringUtils.toUpperCaseAndTrim;

@Slf4j
Expand All @@ -56,7 +57,7 @@ public AllCodelistResponse findAll(@RequestParam(value = "refresh", required = f
if (refresh) {
service.refreshCache();
}
return new AllCodelistResponse(CodelistService.getAll().stream().map(Codelist::convertToResponse).collect(Collectors.groupingBy(CodelistResponse::getList)));
return new AllCodelistResponse(CodelistStaticService.getAll().stream().map(CodelistResponse::buildFrom).collect(Collectors.groupingBy(CodelistResponse::getList)));
}

@Operation(summary = "Get codes and descriptions for listName")
Expand All @@ -66,7 +67,7 @@ public List<CodelistResponse> getByListName(@PathVariable String listName) {
String listUpper = toUpperCaseAndTrim(listName);
log.info("Received a request for all codelists with listName={}", listUpper);
requestValidator.validateListName(listUpper);
return CodelistService.getCodelistResponseList(ListName.valueOf(listUpper));
return CodelistResponse.convertToCodelistResponses(CodelistStaticService.getCodelists(ListName.valueOf(listUpper)));
}

@Operation(summary = "Get for code in listName")
Expand All @@ -77,7 +78,7 @@ public CodelistResponse getByListNameAndCode(@PathVariable String listName, @Pat
String codeUpper = toUpperCaseAndTrim(code);
log.info("Received a request for the codelist with the code={} in listName={}", codeUpper, listUpper);
requestValidator.validateListNameAndCode(listUpper, codeUpper);
return CodelistService.getCodelistResponse(ListName.valueOf(listUpper), codeUpper);
return CodelistStaticService.getCodelistResponse(ListName.valueOf(listUpper), codeUpper);
}

@Operation(summary = "Create Codelist")
Expand All @@ -89,7 +90,7 @@ public List<CodelistResponse> save(@Valid @RequestBody List<CodelistRequest> req
requests = StreamUtils.nullToEmptyList(requests);
requestValidator.validateRequest(requests, false);

return service.save(requests).stream().map(Codelist::convertToResponse).collect(Collectors.toList());
return CodelistResponse.convertToCodelistResponses(service.save(CodelistRequest.convertToCodelists(requests)));
}

@Operation(summary = "Update Codelist")
Expand All @@ -100,7 +101,7 @@ public List<CodelistResponse> update(@Valid @RequestBody List<CodelistRequest> r
requests = StreamUtils.nullToEmptyList(requests);
requestValidator.validateRequest(requests, true);

return service.update(requests).stream().map(Codelist::convertToResponse).collect(Collectors.toList());
return CodelistResponse.convertToCodelistResponses(service.update(requests));
}

@Operation(summary = "Delete Codelist")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,70 +7,26 @@
import no.nav.data.polly.codelist.domain.ListName;
import no.nav.data.polly.codelist.dto.CodelistRequest;
import no.nav.data.polly.codelist.dto.CodelistRequestValidator;
import no.nav.data.polly.codelist.dto.CodelistResponse;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static no.nav.data.common.utils.StreamUtils.convert;

@Slf4j
@Lazy(false)
@Service // Likevel, tjenestene som tilbys her har state (gjennom CodelistCache)
@Component
@RequiredArgsConstructor
public class CodelistService implements InitializingBean {

private final CodelistRepository codelistRepository;
private final CodelistRequestValidator codelistRequestValidator; // TODO: Avhengighet utover

// TODO: Denne skal ikke være static
public static Codelist getCodelist(ListName listName, String code) {
return CodelistCache.getCodelist(listName, code);
}

// TODO: Snu avhengigheten innover
// TODO: Denne skal ikke være static
public static CodelistResponse getCodelistResponse(ListName listName, String code) {
if (code == null) {
return null;
}
Codelist codelist = getCodelist(listName, code);
if (codelist == null) {
return new CodelistResponse(listName, code, null, null);
}
return codelist.convertToResponse();
}

// TODO: Snu avhengigheten innover
// TODO: Denne skal ikke være static
public static List<CodelistResponse> getCodelistResponseList(ListName listName) {
return convert(CodelistCache.getCodelist(listName), Codelist::convertToResponse);
}

// TODO: Snu avhengigheten innover
// TODO: Denne skal ikke være static
public static List<CodelistResponse> getCodelistResponseList(ListName listName, Collection<String> codes) {
return convert(codes, code -> getCodelistResponse(listName, code));
}

// TODO: Denne skal ikke være static
public static List<Codelist> getCodelist(ListName name) {
return CodelistCache.getCodelist(name);
}

// TODO: Denne skal ikke være static
public static List<Codelist> getAll() {
return CodelistCache.getAll();
}

@Scheduled(initialDelayString = "PT1M", fixedRateString = "PT1M")
@Transactional
public void refreshCache() {
Expand All @@ -80,16 +36,24 @@ public void refreshCache() {
}

// TODO: Snu avhengigheten innover
@Transactional
public List<Codelist> save(List<CodelistRequest> requests) {
@Transactional // FIXME: FJERN
public List<Codelist> savese(List<CodelistRequest> requests) {
List<Codelist> codelists = requests.stream()
.map(CodelistRequest::convert)
.map(CodelistRequest::convertToCodelist)
.collect(Collectors.toList());
List<Codelist> saved = codelistRepository.saveAll(codelists);
saved.forEach(CodelistCache::set);
return saved;
}

// TODO: Snu avhengigheten innover
@Transactional
public List<Codelist> save(List<Codelist> codelists) {
List<Codelist> saved = codelistRepository.saveAll(codelists);
saved.forEach(CodelistCache::set);
return saved;
}

// TODO: Snu avhengigheten innover
@Transactional
public List<Codelist> update(List<CodelistRequest> requests) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package no.nav.data.polly.codelist;

import no.nav.data.polly.codelist.domain.Codelist;
import no.nav.data.polly.codelist.domain.ListName;
import no.nav.data.polly.codelist.dto.CodelistResponse;

import java.util.Collection;
import java.util.List;

import static no.nav.data.common.utils.StreamUtils.convert;

@Deprecated // TODO: Skal fjernes (midlertidig hjem for alt som var static i CodelistService)
public final class CodelistStaticService {

private CodelistStaticService() {}

public static Codelist getCodelist(ListName listName, String code) {
return CodelistCache.getCodelist(listName, code);
}

public static CodelistResponse getCodelistResponse(ListName listName, String code) {
if (code == null) {
return null;
}
Codelist codelist = getCodelist(listName, code);
if (codelist == null) {
return new CodelistResponse(listName, code, null, null);
}
return CodelistResponse.buildFrom(codelist);
}

public static List<Codelist> getCodelists(ListName listName) {
return CodelistCache.getCodelist(listName);
}

// TODO: Snu avhengigheten innover
public static List<CodelistResponse> getCodelistResponseList(ListName listName, Collection<String> codes) {
return convert(codes, code -> getCodelistResponse(listName, code));
}

public static List<Codelist> getCodelist(ListName name) {
return CodelistCache.getCodelist(name);
}

public static List<Codelist> getAll() {
return CodelistCache.getAll();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import io.prometheus.client.Summary;
import no.nav.data.common.utils.MetricUtils;
import no.nav.data.polly.codelist.CodelistService;
import no.nav.data.polly.codelist.CodelistStaticService;
import no.nav.data.polly.codelist.domain.ListName;
import no.nav.data.polly.codelist.dto.CodeUsageRequest;
import no.nav.data.polly.codelist.dto.CodeUsageResponse;
Expand Down Expand Up @@ -89,7 +89,7 @@ void validateRequests(List<CodeUsageRequest> requests) {
}

public List<CodeUsageResponse> findCodeUsageOfList(ListName list) {
return CodelistService.getCodelist(list).stream().map(c -> findCodeUsage(c.getList(), c.getCode())).collect(toList());
return CodelistStaticService.getCodelist(list).stream().map(c -> findCodeUsage(c.getList(), c.getCode())).collect(toList());
}

public CodeUsageResponse findCodeUsage(ListName listName, String code) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,6 @@ public class Codelist extends Auditable {
@Column(name = "DESCRIPTION")
private String description;

public CodelistResponse convertToResponse() {
return CodelistResponse.builder()
.list(list)
.code(code)
.shortName(shortName)
.description(description)
.build();
}

@Override
public UUID getId() {
throw new NotImplementedException("does not apply");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import no.nav.data.polly.codelist.domain.ListName;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

import static no.nav.data.common.utils.StringUtils.toUpperCaseAndTrim;

@Data
Expand All @@ -29,14 +32,21 @@ public class CodelistRequest implements RequestElement {
private boolean update;
private int requestIndex;

public Codelist convert() {
public Codelist convertToCodelist() {
return Codelist.builder()
.list(ListName.valueOf(list))
.code(code)
.shortName(shortName)
.description(description)
.build();
}

public static List<Codelist> convertToCodelists(List<CodelistRequest> requests) {
return requests.stream()
.map(CodelistRequest::convertToCodelist)
.collect(Collectors.toList());
}


@JsonIgnore
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package no.nav.data.polly.codelist.dto;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import no.nav.data.common.exceptions.CodelistNotErasableException;
import no.nav.data.common.utils.StreamUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import no.nav.data.common.utils.StreamUtils;
import no.nav.data.polly.codelist.domain.Codelist;
import no.nav.data.polly.codelist.domain.ListName;

import java.util.Collection;
import java.util.List;

@Data
@Builder
@NoArgsConstructor
Expand All @@ -30,4 +35,19 @@ public String toString() {
public Boolean isInvalidCode() {
return description == null ? Boolean.TRUE : null;
}

public static CodelistResponse buildFrom(Codelist cl) {
return CodelistResponse.builder()
.list(cl.getList())
.code(cl.getCode())
.shortName(cl.getShortName())
.description(cl.getDescription())
.build();
}

public static List<CodelistResponse> convertToCodelistResponses(Collection<Codelist> codelists) {
return StreamUtils.convert(codelists, CodelistResponse::buildFrom);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import no.nav.data.common.storage.domain.GenericStorage;
import no.nav.data.polly.alert.domain.AlertEvent;
import no.nav.data.polly.alert.domain.AlertRepository;
import no.nav.data.polly.codelist.CodelistService;
import no.nav.data.polly.codelist.CodelistStaticService;
import no.nav.data.polly.codelist.domain.ListName;
import no.nav.data.polly.dashboard.dto.DashResponse;
import no.nav.data.polly.dashboard.dto.DashResponse.Counter;
Expand Down Expand Up @@ -78,7 +78,7 @@ public ResponseEntity<DashResponse> getDashboardData(@RequestParam(value = "filt
private DashResponse calcDash(ProcessStatusFilter filter) {
var dash = new DashResponse();
// init all departments and load teamId -> productArea mapping
CodelistService.getCodelist(ListName.DEPARTMENT).forEach(d -> dash.department(d.getCode()));
CodelistStaticService.getCodelist(ListName.DEPARTMENT).forEach(d -> dash.department(d.getCode()));
teamService.getAllTeams().forEach(t -> dash.registerTeam(t.getId(), t.getProductAreaId()));

doPaged(processRepository, 50, processes -> {
Expand Down
Loading

0 comments on commit 1d7228f

Please sign in to comment.