package de.ivu.elect.jee.entity;

import java.util.Set;

public class EntityConstraintCheck {

  public static void checkNotNull(Object value, Entity entity, String propertyKey, Set<EntityConstraintViolation> violations) {
    if (value == null) {
      violations.add(new EntityConstraintViolation(entity, propertyKey, EntityConstraintViolationType.NOT_NULL));
    }
  }

  public static void checkStringLength(int maxLenghth, String value, Entity entity, String propertyKey, Set<EntityConstraintViolation> violations) {
    if (value == null) {
      return;
    }

    if (value.length() > maxLenghth) {
      violations.add(new EntityConstraintViolation(entity, propertyKey, EntityConstraintViolationType.STRING_LENGTH_MAX, maxLenghth, value.length()));
    }
  }

  public static void checkStringLength(int maxLenghth, int minLength, String value, Entity entity, String propertyKey, Set<EntityConstraintViolation> violations) {
    if (value == null) {
      return;
    }

    checkStringLength(maxLenghth, value, entity, propertyKey, violations);

    if (value.length() < minLength) {
      violations.add(new EntityConstraintViolation(entity, propertyKey, EntityConstraintViolationType.STRING_LENGTH_MAX, minLength, value.length()));
    }
  }

  public static void checkValueRange(long maxValue, long minValue, Number value, Entity entity, String propertyKey, Set<EntityConstraintViolation> violations) {
    if (value == null) {
      return;
    }

    if (value.longValue() > maxValue) {
      violations.add(new EntityConstraintViolation(entity, propertyKey, EntityConstraintViolationType.NUMERIC_MAX, maxValue, value));
    }

    if (value.longValue() < minValue) {
      violations.add(new EntityConstraintViolation(entity, propertyKey, EntityConstraintViolationType.NUMERIC_MIN, minValue, value));
    }

  }

}
