NotificationDto.java

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

  2. import lombok.AllArgsConstructor;
  3. import lombok.Builder;
  4. import lombok.Data;
  5. import lombok.NoArgsConstructor;
  6. import lombok.experimental.FieldNameConstants;
  7. import no.nav.data.common.validator.Validated;
  8. import no.nav.data.common.validator.Validator;
  9. import no.nav.data.team.notify.domain.Notification.NotificationChannel;
  10. import no.nav.data.team.notify.domain.Notification.NotificationTime;
  11. import no.nav.data.team.notify.domain.Notification.NotificationType;
  12. import org.apache.commons.lang3.StringUtils;

  13. import java.util.List;
  14. import java.util.UUID;

  15. import static org.springframework.util.CollectionUtils.isEmpty;

  16. @Data
  17. @Builder
  18. @AllArgsConstructor
  19. @NoArgsConstructor
  20. @FieldNameConstants
  21. public class NotificationDto implements Validated {

  22.     private UUID id;
  23.     private String ident;
  24.     private UUID target;
  25.     private NotificationType type;
  26.     private NotificationTime time;
  27.     private List<NotificationChannel> channels;

  28.     @Override
  29.     public void format() {
  30.         setIdent(StringUtils.trimToNull(ident));
  31.         if (type == NotificationType.ALL_EVENTS) {
  32.             setTarget(null);
  33.         }
  34.         if (isEmpty(channels)) {
  35.             setChannels(List.of(NotificationChannel.EMAIL));
  36.         }
  37.     }

  38.     @Override
  39.     public void validateFieldValues(Validator<?> validator) {
  40.         validator.checkNull(Fields.type, type);
  41.         validator.checkNull(Fields.time, type);
  42.         validator.checkBlank(Fields.ident, ident);
  43.         if (!NotificationType.ALL_EVENTS.equals(type)) {
  44.             validator.checkBlank(Fields.target, ident);
  45.         }
  46.     }
  47. }