PaOwnerGroup.java

  1. package no.nav.data.team.po.domain;

  2. import lombok.Builder;
  3. import lombok.Getter;
  4. import no.nav.data.team.po.dto.PaOwnerGroupRequest;
  5. import no.nav.data.team.po.dto.PaOwnerGroupResponse;
  6. import no.nav.data.team.resource.NomClient;
  7. import no.nav.data.team.resource.dto.ResourceResponse;

  8. import java.util.List;

  9. @Getter
  10. @Builder
  11. public class PaOwnerGroup {
  12.     private final String ownerNavId;
  13.     private final List<String> ownerGroupMemberNavIdList;

  14.     public PaOwnerGroup() {
  15.         this.ownerNavId = null;
  16.         this.ownerGroupMemberNavIdList = List.of();
  17.     }

  18.     public PaOwnerGroup(String ownerNavId, List<String> ownerGroupMemberNavIdList) {
  19.         this.ownerNavId = ownerNavId;
  20.         this.ownerGroupMemberNavIdList = ownerGroupMemberNavIdList;
  21.     }

  22.     public PaOwnerGroupResponse convertToResponse() {
  23.         var builder = PaOwnerGroupResponse.builder();
  24.         var nomClient = NomClient.getInstance();


  25.         if(getOwnerNavId() !=null) {
  26.             var ownerResourceOptional = nomClient.getByNavIdent(getOwnerNavId());
  27.             if (ownerResourceOptional.isPresent()) {
  28.                 builder.ownerResource(ownerResourceOptional.get().convertToResponse());
  29.             } else {
  30.                 builder.ownerResource(ResourceResponse.builder().navIdent(getOwnerNavId()).stale(true).build());
  31.             }
  32.         }

  33.         var ownerMemberResourceResponses = ownerGroupMemberNavIdList.stream().map(
  34.                 memberNavId -> {
  35.                     var optionalRes = nomClient.getByNavIdent(memberNavId);
  36.                     if (optionalRes.isPresent()) {
  37.                         return optionalRes.get().convertToResponse();
  38.                     } else {
  39.                         return ResourceResponse.builder().navIdent(memberNavId).stale(true).build();
  40.                     }
  41.                 }
  42.         ).toList();

  43.         builder.ownerGroupMemberResourceList(ownerMemberResourceResponses);

  44.         return builder.build();
  45.     }

  46.     public static PaOwnerGroup convertFromRequest(PaOwnerGroupRequest request) {
  47.         if (request == null) return new PaOwnerGroup();

  48.         return PaOwnerGroup.builder()
  49.                 .ownerNavId(request.getOwnerNavId())
  50.                 .ownerGroupMemberNavIdList(request.getOwnerGroupMemberNavIdList())
  51.                 .build();
  52.     }
  53. }