Links.java

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

  2. import com.fasterxml.jackson.annotation.JsonInclude;
  3. import com.fasterxml.jackson.annotation.JsonInclude.Include;
  4. import com.fasterxml.jackson.annotation.JsonPropertyOrder;
  5. import lombok.AllArgsConstructor;
  6. import lombok.Builder;
  7. import lombok.Data;
  8. import lombok.NoArgsConstructor;
  9. import no.nav.data.common.storage.domain.DomainObject;
  10. import no.nav.data.team.cluster.domain.Cluster;
  11. import no.nav.data.team.notify.UrlGenerator;
  12. import no.nav.data.team.po.domain.ProductArea;
  13. import no.nav.data.team.resource.domain.Resource;
  14. import no.nav.data.team.team.domain.Team;

  15. import java.util.List;

  16. @Data
  17. @Builder
  18. @NoArgsConstructor
  19. @AllArgsConstructor
  20. @JsonPropertyOrder({"ui", "slackChannels"})
  21. public class Links {

  22.     private String ui;
  23.     @JsonInclude(Include.NON_NULL)
  24.     private List<NamedLink> slackChannels;

  25.     @Data
  26.     @Builder
  27.     @NoArgsConstructor
  28.     @AllArgsConstructor
  29.     @JsonPropertyOrder({"name", "url"})
  30.     public static class NamedLink {

  31.         private String name;
  32.         private String url;
  33.     }

  34.     public Links(String ui) {
  35.         this.ui = ui;
  36.     }

  37.     public static Links getFor(DomainObject domainObject) {
  38.         UrlGenerator urlGenerator = UrlGenerator.instance();
  39.         if (domainObject instanceof Resource r) {
  40.             return Links.builder().ui(urlGenerator.resourceUrl(r.getNavIdent())).build();
  41.         }
  42.         return Links.builder()
  43.                 .ui(urlGenerator.urlFor(domainObject.getClass(), domainObject.getId()))
  44.                 .slackChannels(slackUrls(domainObject))
  45.                 .build();
  46.     }

  47.     private static List<NamedLink> slackUrls(DomainObject domainObject) {
  48.         String slack = null;
  49.         if (domainObject instanceof Team t) {
  50.             slack = t.getSlackChannel();
  51.         } else if (domainObject instanceof ProductArea pa) {
  52.             slack = pa.getSlackChannel();
  53.         } else if (domainObject instanceof Cluster c) {
  54.             slack = c.getSlackChannel();
  55.         }
  56.         if (slack == null) {
  57.             return null;
  58.         }
  59.         return UrlGenerator.instance().slackUrls(slack);
  60.     }
  61. }