Cluster.java

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

  2. import lombok.AllArgsConstructor;
  3. import lombok.Builder;
  4. import lombok.Data;
  5. import lombok.NoArgsConstructor;
  6. import no.nav.data.common.storage.domain.ChangeStamp;
  7. import no.nav.data.common.storage.domain.DomainObject;
  8. import no.nav.data.common.utils.StreamUtils;
  9. import no.nav.data.team.cluster.dto.ClusterRequest;
  10. import no.nav.data.team.cluster.dto.ClusterResponse;
  11. import no.nav.data.team.shared.domain.HistorizedDomainObject;
  12. import no.nav.data.team.shared.domain.Membered;
  13. import no.nav.data.team.shared.dto.Links;
  14. import no.nav.data.team.shared.domain.DomainObjectStatus;

  15. import java.time.LocalDateTime;
  16. import java.util.Comparator;
  17. import java.util.List;
  18. import java.util.UUID;

  19. import static no.nav.data.common.utils.StreamUtils.copyOf;

  20. @Data
  21. @Builder
  22. @NoArgsConstructor
  23. @AllArgsConstructor
  24. public class Cluster implements DomainObject, Membered, HistorizedDomainObject {

  25.     private UUID id;
  26.     private String name;
  27.     private String description;
  28.     private String slackChannel;
  29.     private List<String> tags;
  30.     private UUID productAreaId;
  31.     private List<ClusterMember> members;

  32.     private DomainObjectStatus status;

  33.     private LocalDateTime lastNudge;
  34.     private ChangeStamp changeStamp;
  35.     private boolean updateSent;

  36.     public List<ClusterMember> getMembers() {
  37.         return members == null ? List.of() : members;
  38.     }

  39.     public Cluster convert(ClusterRequest request) {
  40.         name = request.getName();
  41.         description = request.getDescription();
  42.         slackChannel = request.getSlackChannel();
  43.         tags = copyOf(request.getTags());
  44.         // If an update does not contain member array don't update
  45.         if (!request.isUpdate() || request.getMembers() != null) {
  46.             members = StreamUtils.convert(request.getMembers(), ClusterMember::convert);
  47.         }
  48.         members.sort(Comparator.comparing(ClusterMember::getNavIdent));
  49.         status = request.getStatus();
  50.         productAreaId = request.productAreaIdAsUUID();
  51.         updateSent = false;
  52.         return this;
  53.     }

  54.     public ClusterResponse convertToResponse() {
  55.         return ClusterResponse.builder()
  56.                 .id(id)
  57.                 .name(name)
  58.                 .description(description)
  59.                 .slackChannel(slackChannel)
  60.                 .tags(copyOf(tags))
  61.                 .productAreaId(productAreaId)
  62.                 .members(StreamUtils.convert(members, ClusterMember::convertToResponse))
  63.                 .status(status)
  64.                 .changeStamp(convertChangeStampResponse())
  65.                 .links(Links.getFor(this))
  66.                 .build();
  67.     }
  68. }