GenericStorage.java

  1. package no.nav.data.common.storage.domain;

  2. import com.fasterxml.jackson.databind.JsonNode;
  3. import jakarta.persistence.Column;
  4. import jakarta.persistence.Entity;
  5. import jakarta.persistence.Id;
  6. import jakarta.persistence.Table;
  7. import jakarta.validation.constraints.NotNull;
  8. import lombok.AllArgsConstructor;
  9. import lombok.Data;
  10. import lombok.EqualsAndHashCode;
  11. import lombok.NoArgsConstructor;
  12. import no.nav.data.common.auditing.domain.Auditable;
  13. import no.nav.data.common.security.azure.support.MailLog;
  14. import no.nav.data.common.utils.JsonUtils;
  15. import no.nav.data.common.utils.StreamUtils;
  16. import no.nav.data.team.cluster.domain.Cluster;
  17. import no.nav.data.team.po.domain.ProductArea;
  18. import no.nav.data.team.resource.domain.Resource;
  19. import no.nav.data.team.team.domain.Team;
  20. import org.hibernate.annotations.JdbcTypeCode;
  21. import org.hibernate.type.SqlTypes;
  22. import org.springframework.util.Assert;

  23. import java.util.Collection;
  24. import java.util.List;
  25. import java.util.UUID;

  26. import static no.nav.data.common.utils.StreamUtils.convert;


  27. @Data
  28. @EqualsAndHashCode(callSuper = false)
  29. @AllArgsConstructor
  30. @NoArgsConstructor
  31. @Entity
  32. @Table(name = "GENERIC_STORAGE")
  33. public class GenericStorage extends Auditable {

  34.     @Id
  35.     @Column(name = "ID")
  36.     private UUID id;

  37.     @NotNull
  38.     @Column(name = "TYPE", nullable = false, updatable = false)
  39.     private String type;

  40.     @JdbcTypeCode(SqlTypes.JSON)
  41.     @Column(name = "DATA", nullable = false)
  42.     private JsonNode data;

  43.     public GenericStorage generateId() {
  44.         Assert.isTrue(id == null, "id already set");
  45.         id = UUID.randomUUID();
  46.         return this;
  47.     }

  48.     public <T extends DomainObject> GenericStorage setDomainObjectData(T object) {
  49.         Assert.isTrue(id != null, "id not set");
  50.         Assert.isTrue(type == null || object.type().equals(type), "cannot change object type");
  51.         object.setId(id);
  52.         type = object.type();
  53.         data = JsonUtils.toJsonNode(object);
  54.         return this;
  55.     }

  56.     public <T extends DomainObject> T getDomainObjectData(Class<T> clazz) {
  57.         validateType(clazz);
  58.         T object = JsonUtils.toObject(data, clazz);
  59.         object.setChangeStamp(new ChangeStamp(getCreatedBy(), getCreatedDate(), getLastModifiedBy(), getLastModifiedDate()));
  60.         return object;
  61.     }

  62.     public <T extends DomainObject> void validateType(Class<T> clazz) {
  63.         Assert.isTrue(type.equals(TypeRegistration.typeOf(clazz)), "Incorrect type");
  64.     }

  65.     public Team toTeam() {
  66.         return getDomainObjectData(Team.class);
  67.     }

  68.     public ProductArea toProductArea() {
  69.         return getDomainObjectData(ProductArea.class);
  70.     }

  71.     public Cluster toCluster() {
  72.         return getDomainObjectData(Cluster.class);
  73.     }

  74.     public Resource toResource() {
  75.         return getDomainObjectData(Resource.class);
  76.     }

  77.     public MailLog toMailLog() {
  78.         return getDomainObjectData(MailLog.class);
  79.     }

  80.     public static <T extends DomainObject> List<T> getOfType(Collection<GenericStorage> storages, Class<T> type) {
  81.         return convert(StreamUtils.filter(storages, r -> r.getType().equals(TypeRegistration.typeOf(type))), gs -> gs.getDomainObjectData(type));
  82.     }

  83.     public static <T extends DomainObject> List<T> to(List<GenericStorage> collection, Class<T> type) {
  84.         return convert(collection, item -> item.getDomainObjectData(type));
  85.     }
  86. }