TeamExportService.java

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

  2. import no.nav.data.common.export.ExcelBuilder;
  3. import no.nav.data.common.utils.DateUtil;
  4. import no.nav.data.team.cluster.ClusterService;
  5. import no.nav.data.team.cluster.domain.Cluster;
  6. import no.nav.data.team.member.dto.MemberResponse;
  7. import no.nav.data.team.po.ProductAreaService;
  8. import no.nav.data.team.po.domain.ProductArea;
  9. import no.nav.data.team.resource.NomClient;
  10. import no.nav.data.team.resource.domain.ResourceType;
  11. import no.nav.data.team.shared.Lang;
  12. import no.nav.data.team.team.domain.OfficeHours;
  13. import no.nav.data.team.team.domain.Team;
  14. import no.nav.data.team.team.domain.TeamMember;
  15. import no.nav.data.team.team.domain.TeamRole;
  16. import org.springframework.stereotype.Service;

  17. import java.util.Arrays;
  18. import java.util.Comparator;
  19. import java.util.List;
  20. import java.util.UUID;
  21. import java.util.function.Function;
  22. import java.util.stream.Collectors;

  23. import static java.lang.String.join;
  24. import static java.util.Optional.ofNullable;
  25. import static no.nav.data.common.utils.StreamUtils.convert;
  26. import static no.nav.data.common.utils.StreamUtils.filter;
  27. import static no.nav.data.common.utils.StreamUtils.nullToEmptyList;
  28. import static no.nav.data.common.utils.StreamUtils.safeStream;

  29. @Service
  30. public class TeamExportService {

  31.     public enum SpreadsheetType {
  32.         ALL,
  33.         CLUSTER,
  34.         AREA
  35.     }

  36.     private final TeamService teamService;
  37.     private final ProductAreaService productAreaService;
  38.     private final ClusterService clusterService;
  39.     private final NomClient nomClient = NomClient.getInstance();

  40.     public TeamExportService(TeamService teamService, ProductAreaService productAreaService, ClusterService clusterService) {
  41.         this.teamService = teamService;
  42.         this.productAreaService = productAreaService;
  43.         this.clusterService = clusterService;
  44.     }

  45.     public byte[] generate(SpreadsheetType type, String filter) {
  46.         UUID filterUuid = toUUID(filter);
  47.         var domainPaMap = productAreaService.getAll().stream().collect(Collectors.toMap(ProductArea::getId, Function.identity()));
  48.         var domainClusterMap = clusterService.getAll().stream().collect(Collectors.toMap(Cluster::getId, Function.identity()));
  49.         var domainTeams = switch (type) {
  50.             case ALL -> teamService.getAll();
  51.             case CLUSTER -> teamService.findByCluster(filterUuid);
  52.             case AREA -> teamService.findByProductArea(filterUuid);
  53.         };

  54.         var teams = convert(domainTeams, t -> new TeamInfo(t, domainPaMap.get(t.getProductAreaId()), convert(t.getClusterIds(), domainClusterMap::get)));

  55.         return generate(teams);
  56.     }

  57.     private byte[] generate(List<TeamInfo> teams) {
  58.         var doc = new ExcelBuilder("Teams");

  59.         doc.addRow()
  60.                 .addCell(Lang.TEAM_ID)
  61.                 .addCell(Lang.NAME)
  62.                 .addCell(Lang.TEAM_LEADS)
  63.                 .addCell(Lang.PRODUCT_OWNERS)
  64.                 .addCell(Lang.OWNERSHIP_TYPE)
  65.                 .addCell(Lang.TEAM_TYPE)
  66.                 .addCell(Lang.AREA_ID)
  67.                 .addCell(Lang.AREA)
  68.                 .addCell(Lang.CLUSTER)
  69.                 .addCell(Lang.STATUS)
  70.                 .addCell(Lang.QA_DONE)
  71.                 .addCell(Lang.NAIS_TEAMS)
  72.                 .addCell(Lang.TAGS)
  73.                 .addCell(Lang.MEMBERS)
  74.                 .addCell(Lang.INTERNAL)
  75.                 .addCell(Lang.EXTERNAL)
  76.                 .addCell(Lang.SLACK)

  77.                 .addCell((Lang.CONTACT_PERSON))
  78.                 .addCell(Lang.LOCATION)
  79.                 .addCell(Lang.OFFICE_HOURS)

  80.                 .addCell(Lang.DESCRIPTION)
  81.         ;

  82.         teams.sort(Comparator.comparing(t -> t.team().getName()));
  83.         teams.forEach(t -> add(doc, t));

  84.         return doc.build();
  85.     }

  86.     private void add(ExcelBuilder doc, TeamInfo teamInfo) {
  87.         var team = teamInfo.team();
  88.         var members = convert(team.getMembers(), TeamMember::convertToResponse);


  89.         doc.addRow()
  90.                 .addCell(team.getId().toString())
  91.                 .addCell(team.getName())
  92.                 .addCell(names(members, TeamRole.LEAD))
  93.                 .addCell(names(members, TeamRole.PRODUCT_OWNER))
  94.                 .addCell(Lang.teamOwnershipType(team.getTeamOwnershipType()))
  95.                 .addCell(Lang.teamType(team.getTeamType()))
  96.                 .addCell(ofNullable(team.getProductAreaId()).map(UUID::toString).orElse(""))
  97.                 .addCell(ofNullable(teamInfo.productArea()).map(ProductArea::getName).orElse(""))
  98.                 .addCell(safeStream(teamInfo.clusters()).map(Cluster::getName).collect(Collectors.joining(", ")))
  99.                 .addCell(team.getStatus().toString())
  100.                 .addCell(DateUtil.formatDateTimeHumanReadable(team.getQaTime()))
  101.                 .addCell(join(", ", nullToEmptyList(team.getNaisTeams())))
  102.                 .addCell(join(", ", nullToEmptyList(team.getTags())))
  103.                 .addCell(members.size())
  104.                 .addCell(filter(members, m -> m.getResource().getResourceType() == ResourceType.INTERNAL).size())
  105.                 .addCell(filter(members, m -> m.getResource().getResourceType() == ResourceType.EXTERNAL).size())
  106.                 .addCell(team.getSlackChannel())
  107.                 .addCell(contactPerson(team.getContactPersonIdent()))
  108.                 .addCell(location(team.getOfficeHours()))
  109.                 .addCell(officeHours(team.getOfficeHours()))
  110.                 .addCell(team.getDescription())


  111.         ;
  112.     }

  113.     private String names(List<MemberResponse> members, TeamRole role) {
  114.         return filter(members, m -> m.getRoles().contains(role)).stream()
  115.                 .map(MemberResponse::getResource).map(r -> r.getFamilyName() + ", " + r.getGivenName()).collect(Collectors.joining(" - "));
  116.     }

  117.     private String contactPerson(String ident ) {
  118.         if(ident == null){
  119.             return "";
  120.         }
  121.         var nameOptional = nomClient.getNameForIdent(ident);

  122.         if(nameOptional.isPresent()){
  123.             var stringName = nameOptional.get();
  124.             var splitName = stringName.split(" ");
  125.             return splitName[splitName.length-1] + ", " + String.join(" ", Arrays.copyOf(splitName, splitName.length-1));
  126.         }
  127.         return "";
  128.     }

  129.     private String location(OfficeHours officeHours){
  130.         if(officeHours == null){
  131.             return "";
  132.         }

  133.         return officeHours.getLocationCode();
  134.     }

  135.     private String officeHours(OfficeHours officeHours){
  136.         if(officeHours == null){
  137.             return "";
  138.         }

  139.         var days = officeHours.getDays().toString();

  140.         // Removing the "[" at the start of the variable and the "]" at the end of the variable
  141.         days = days.substring(1, days.length() - 1);

  142.         return days;
  143.     }

  144.     record TeamInfo(Team team, ProductArea productArea, List<Cluster> clusters) {

  145.     }

  146.     private UUID toUUID(String uuid) {
  147.         return uuid == null ? null : UUID.fromString(uuid);
  148.     }
  149. }