ClusterService.java

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

  2. import lombok.extern.slf4j.Slf4j;
  3. import no.nav.data.common.exceptions.ValidationException;
  4. import no.nav.data.common.storage.StorageService;
  5. import no.nav.data.common.storage.domain.GenericStorage;
  6. import no.nav.data.common.validator.Validator;
  7. import no.nav.data.team.cluster.domain.Cluster;
  8. import no.nav.data.team.cluster.dto.ClusterRequest;
  9. import no.nav.data.team.po.domain.ProductArea;
  10. import no.nav.data.team.shared.domain.DomainObjectStatus;
  11. import no.nav.data.team.team.TeamRepository;
  12. import no.nav.data.team.team.domain.Team;
  13. import no.nav.data.team.team.dto.TeamRequest.Fields;
  14. import org.springframework.stereotype.Service;

  15. import java.util.List;
  16. import java.util.UUID;

  17. import static no.nav.data.common.utils.StreamUtils.convert;
  18. import static no.nav.data.common.validator.Validator.*;

  19. @Slf4j
  20. @Service
  21. public class ClusterService {

  22.     private final StorageService storage;
  23.     private final TeamRepository teamRepository;
  24.     private final ClusterRepository repository;

  25.     public ClusterService(StorageService storage, TeamRepository teamRepository, ClusterRepository repository) {
  26.         this.storage = storage;
  27.         this.teamRepository = teamRepository;
  28.         this.repository = repository;
  29.     }

  30.     public Cluster save(ClusterRequest request) {
  31.         Validator.validate(request, storage)
  32.                 .addValidations(this::validateName)
  33.                 .addValidations(validator -> validator.checkExists(request.getProductAreaId(), storage, ProductArea.class))
  34.                 .addValidations(this::validateStatusNotNull)
  35.                 .ifErrorsThrowValidationException();
  36.         var cluster = request.isUpdate() ? storage.get(request.getIdAsUUID(), Cluster.class) : new Cluster();
  37.         return storage.save(cluster.convert(request));
  38.     }

  39.     private void validateStatusNotNull(Validator<ClusterRequest> clusterRequestValidator) {
  40.         if(clusterRequestValidator.getItem().getStatus() == null){
  41.             clusterRequestValidator.addError("status", ILLEGAL_ARGUMENT, "Status cannot be null");
  42.         }
  43.     }

  44.     public Cluster get(UUID id) {
  45.         return storage.get(id, Cluster.class);
  46.     }

  47.     public List<Cluster> search(String name) {
  48.         return convert(repository.findByNameLike(name), GenericStorage::toCluster);
  49.     }

  50.     public Cluster delete(UUID id) {
  51.         List<Team> teams = teamRepository.findByCluster(id);
  52.         if (!teams.isEmpty()) {
  53.             String message = "Cannot delete cluster, it is in use by " + teams.size() + " teams";
  54.             log.debug(message);
  55.             throw new ValidationException(message);
  56.         }

  57.         Cluster delete = storage.delete(id, Cluster.class);
  58.         return delete;
  59.     }

  60.     public List<Cluster> getAll() {
  61.         return storage.getAll(Cluster.class);
  62.     }

  63.     public List<Cluster> getAllActive() {
  64.         return getAll().stream().filter(cluster -> cluster.getStatus() == DomainObjectStatus.ACTIVE).toList();
  65.     }

  66.     private void validateName(Validator<ClusterRequest> validator) {
  67.         String name = validator.getItem().getName();
  68.         if (name == null || name == "") {
  69.             validator.addError(Fields.name, ERROR_MESSAGE_MISSING, "Name is required");
  70.         }

  71.     }
  72. }