ProductAreaService.java

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

  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.ClusterRepository;
  8. import no.nav.data.team.po.domain.ProductArea;
  9. import no.nav.data.team.po.dto.AddTeamsToProductAreaRequest;
  10. import no.nav.data.team.po.dto.ProductAreaRequest;
  11. import no.nav.data.team.shared.domain.DomainObjectStatus;
  12. import no.nav.data.team.team.TeamRepository;
  13. import no.nav.data.team.team.domain.Team;
  14. import no.nav.data.team.team.dto.TeamRequest.Fields;
  15. import org.springframework.stereotype.Service;

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

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

  20. @Slf4j
  21. @Service
  22. public class ProductAreaService {

  23.     private final StorageService storage;
  24.     private final TeamRepository teamRepository;
  25.     private final ClusterRepository clusterRepository;
  26.     private final ProductAreaRepository repository;

  27.     public ProductAreaService(StorageService storage, TeamRepository teamRepository,
  28.             ClusterRepository clusterRepository, ProductAreaRepository repository) {
  29.         this.storage = storage;
  30.         this.teamRepository = teamRepository;
  31.         this.clusterRepository = clusterRepository;
  32.         this.repository = repository;
  33.     }

  34.     public ProductArea save(ProductAreaRequest request) {
  35.         Validator.validate(request, storage)
  36.                 .addValidations(this::validateName)
  37.                 .addValidations(this::validateStatusNotNull)
  38.                 .ifErrorsThrowValidationException();
  39.         var productArea = request.isUpdate() ? storage.get(request.getIdAsUUID(), ProductArea.class) : new ProductArea();
  40.         return storage.save(productArea.convert(request));
  41.     }

  42.     private void validateStatusNotNull(Validator<ProductAreaRequest> productAreaRequestValidator) {
  43.         if(productAreaRequestValidator.getItem().getStatus() == null){
  44.             productAreaRequestValidator.addError("status", ILLEGAL_ARGUMENT, "Status cannot be null");
  45.         }
  46.     }

  47.     public ProductArea get(UUID id) {
  48.         return storage.get(id, ProductArea.class);
  49.     }

  50.     public List<ProductArea> search(String name) {
  51.         return convert(repository.findByNameLike(name), GenericStorage::toProductArea);
  52.     }

  53.     public ProductArea delete(UUID id) {
  54.         List<GenericStorage> teams = teamRepository.findByProductArea(id);
  55.         if (!teams.isEmpty()) {
  56.             String message = "Cannot delete product area, it is in use by " + teams.size() + " teams";
  57.             log.debug(message);
  58.             throw new ValidationException(message);
  59.         }
  60.         List<GenericStorage> clusters = clusterRepository.findByProductArea(id);
  61.         if (!clusters.isEmpty()) {
  62.             String message = "Cannot delete product area, it is in use by " + clusters.size() + " clusters";
  63.             log.debug(message);
  64.             throw new ValidationException(message);
  65.         }
  66.         ProductArea delete = storage.delete(id, ProductArea.class);
  67.         return delete;
  68.     }

  69.     public List<ProductArea> getAll() {
  70.         return storage.getAll(ProductArea.class);
  71.     }

  72.     public List<ProductArea> getAllActive() {
  73.         return getAll().stream().filter(po-> po.getStatus() == DomainObjectStatus.ACTIVE).toList();
  74.     }

  75.     public void addTeams(AddTeamsToProductAreaRequest request) {
  76.         Validator.validate(request)
  77.                 .addValidations(validator -> validator.checkExists(request.getProductAreaId(), storage, ProductArea.class))
  78.                 .addValidations(AddTeamsToProductAreaRequest::getTeamIds, (validator, teamId) -> validator.checkExists(teamId, storage, Team.class))
  79.                 .ifErrorsThrowValidationException();

  80.         request.getTeamIds().forEach(teamId -> {
  81.             var team = storage.get(UUID.fromString(teamId), Team.class);
  82.             team.setProductAreaId(request.productAreaIdAsUUID());
  83.             team.setUpdateSent(false);
  84.             storage.save(team);
  85.         });
  86.     }

  87.     private void validateName(Validator<ProductAreaRequest> validator) {
  88.         String name = validator.getItem().getName();
  89.         if (name == null || name == "") {
  90.             validator.addError(Fields.name, ERROR_MESSAGE_MISSING, "Name is required");
  91.         }

  92.     }
  93. }