Resource.java

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

  2. import com.fasterxml.jackson.annotation.JsonIgnore;
  3. import lombok.AllArgsConstructor;
  4. import lombok.Builder;
  5. import lombok.Data;
  6. import lombok.NoArgsConstructor;
  7. import no.nav.data.common.storage.domain.ChangeStamp;
  8. import no.nav.data.common.storage.domain.DomainObject;
  9. import no.nav.data.team.resource.dto.NomRessurs;
  10. import no.nav.data.team.resource.dto.ResourceResponse;
  11. import no.nav.data.team.shared.dto.Links;
  12. import org.apache.commons.lang3.BooleanUtils;

  13. import java.lang.management.ManagementFactory;
  14. import java.lang.management.RuntimeMXBean;
  15. import java.time.Duration;
  16. import java.time.LocalDate;
  17. import java.time.ZonedDateTime;
  18. import java.util.UUID;

  19. import static java.util.Objects.isNull;

  20. @Data
  21. @Builder
  22. @AllArgsConstructor
  23. @NoArgsConstructor
  24. public class Resource implements DomainObject {

  25.     private static final RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
  26.     private static final long MIN_UPTIME_BEFORE_STALE = Duration.ofMinutes(10).toMillis();

  27.     private UUID id;
  28.     private ChangeStamp changeStamp;

  29.     private int resourceHashCode;
  30.     private int partition;
  31.     private long offset;
  32.     private ZonedDateTime readTime;
  33.     @JsonIgnore
  34.     private boolean stale;

  35.     private String navIdent;
  36.     private String givenName;
  37.     private String familyName;
  38.     private String fullName;
  39.     private String email;
  40.     private boolean onLeave;
  41.     private ResourceType resourceType;
  42.     private String originalResourceType;
  43.     private LocalDate startDate;
  44.     private LocalDate endDate;

  45.     public Resource(NomRessurs nomRessurs) {
  46.         cloneFrom(nomRessurs);
  47.     }

  48.     private void cloneFrom(NomRessurs nomRessurs) {
  49.         resourceHashCode = nomRessurs.hashCode();
  50.         partition = nomRessurs.getPartition();
  51.         offset = nomRessurs.getOffset();
  52.         readTime = ZonedDateTime.now();

  53.         navIdent = nomRessurs.getNavident().toUpperCase();
  54.         givenName = nomRessurs.getFornavn();
  55.         familyName = nomRessurs.getEtternavn();
  56.         fullName = nomRessurs.getFullName();
  57.         email = nomRessurs.getEpost();
  58.         onLeave = BooleanUtils.isTrue(nomRessurs.getPermisjon());
  59.         resourceType = ResourceType.fromRessursType(nomRessurs.getRessurstype());
  60.         originalResourceType = resourceType == ResourceType.OTHER ? nomRessurs.getRessurstype() : resourceType.name();
  61.         startDate = nomRessurs.getStartdato();
  62.         endDate = nomRessurs.getSluttdato();
  63.     }

  64.     public Resource stale() {
  65.         stale = runtime.getUptime() > MIN_UPTIME_BEFORE_STALE;
  66.         return this;
  67.     }

  68.     public boolean isInactive() {
  69.         return endDate != null && endDate.isBefore(LocalDate.now().plusDays(1));
  70.     }

  71.     public ResourceResponse convertToResponse() {
  72.         return ResourceResponse.builder()
  73.                 .navIdent(navIdent)
  74.                 .givenName(givenName)
  75.                 .familyName(familyName)
  76.                 .fullName(fullName)
  77.                 .email(email)
  78.                 .onLeave(onLeave)
  79.                 .resourceType(resourceType)
  80.                 .startDate(startDate)
  81.                 .endDate(shouldHideEndDateIfBeforeNow())
  82.                 .stale(stale)
  83.                 .links(Links.getFor(this))
  84.                 .build();
  85.     }

  86.     private LocalDate shouldHideEndDateIfBeforeNow() {
  87.         if (isNull(this.endDate) || this.endDate.isAfter(LocalDate.now())) return null;
  88.         else return this.endDate;
  89.     }
  90. }