PaOwnerGroupRequest.java

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


  2. import lombok.Builder;
  3. import lombok.Data;
  4. import lombok.Getter;
  5. import lombok.experimental.FieldNameConstants;
  6. import no.nav.data.common.utils.StreamUtils;
  7. import no.nav.data.common.validator.Validated;
  8. import no.nav.data.common.validator.Validator;
  9. import org.apache.commons.lang3.StringUtils;

  10. import java.util.ArrayList;
  11. import java.util.List;
  12. import java.util.function.Function;

  13. import static no.nav.data.common.validator.Validator.NAV_IDENT_PATTERN;
  14. import static no.nav.data.team.po.dto.ProductAreaRequest.paOwnerGroupError;
  15. import static org.apache.commons.lang3.StringUtils.upperCase;

  16. @Data
  17. @Getter
  18. @Builder
  19. @FieldNameConstants
  20. public class PaOwnerGroupRequest implements Validated {
  21.     private String ownerNavId;
  22.     private List<String> ownerGroupMemberNavIdList;

  23.     public PaOwnerGroupRequest(){
  24.         this.ownerNavId = null;
  25.         this.ownerGroupMemberNavIdList = null;
  26.     }

  27.     public PaOwnerGroupRequest(String ownerNavId, List<String> ownerGroupMemberNavIdList){
  28.         this.ownerNavId = ownerNavId;
  29.         this.ownerGroupMemberNavIdList = new ArrayList<>(ownerGroupMemberNavIdList);
  30.     }

  31.     @Override
  32.     public void format() {
  33.         setOwnerNavId(upperCase(ownerNavId));
  34.         StreamUtils.nullToEmptyList(ownerGroupMemberNavIdList);
  35.         setOwnerGroupMemberNavIdList(ownerGroupMemberNavIdList.stream().map(StringUtils::upperCase).toList());
  36.     }

  37.     @Override
  38.     public void validateFieldValues(Validator<?> validator) {
  39.         checkPaOwnerField(validator);
  40.         checkPaOwnerGroupField(validator);
  41.     }


  42.     private void checkPaOwnerField(Validator<?> validator) {
  43.         // either null or is a valid ID
  44.         if (this.ownerNavId != null) {
  45.             if (!this.ownerNavId.matches(NAV_IDENT_PATTERN.pattern())) {
  46.                 validator.addError(Fields.ownerNavId, "badId", "Id is wrongly formatted: " + this.ownerNavId);
  47.             }
  48.         }
  49.     }
  50. //
  51.     private void checkPaOwnerGroupField(Validator<?> validator) {
  52.         if(this.ownerGroupMemberNavIdList == null) return;

  53.         var noOwnerAndNonzeroMemberCount = this.ownerNavId == null && !this.ownerGroupMemberNavIdList.isEmpty();
  54.         if (noOwnerAndNonzeroMemberCount) {
  55.             validator.addError(Fields.ownerGroupMemberNavIdList, paOwnerGroupError, "OwnerId was null while list of owner group members was non-empty");
  56.             return;
  57.         }

  58.         var ownerAlsoInOwnerGroup = this.ownerNavId != null && this.ownerGroupMemberNavIdList.contains(this.ownerNavId);
  59.         if (ownerAlsoInOwnerGroup) {
  60.             validator.addError(Fields.ownerGroupMemberNavIdList, paOwnerGroupError, "Owner cannot also be member of the owner group");
  61.         }

  62.         var badIdsInOwnerGroup = this.ownerGroupMemberNavIdList.stream().filter(it -> !it.matches(NAV_IDENT_PATTERN.pattern())).toList();
  63.         if (badIdsInOwnerGroup.size() != 0) {
  64.             validator.addError(Fields.ownerGroupMemberNavIdList, paOwnerGroupError, "Owner group contained invalid Id(s): " + badIdsInOwnerGroup.toString());
  65.         }

  66.         var duplicatesInOwnerGroup = StreamUtils.duplicates(this.ownerGroupMemberNavIdList, Function.identity());
  67.         if (duplicatesInOwnerGroup) {
  68.             validator.addError(Fields.ownerGroupMemberNavIdList, paOwnerGroupError, "Duplicate IDs in owner group");
  69.         }
  70.     }
  71. }