Example #1
0
  private static String convertEnum(Attribute attr, Object value) {
    if (value == null) {
      return null;
    }

    String stringValue;
    if (value instanceof String) {
      stringValue = (String) value;
    } else if (value instanceof Enum) {
      stringValue = value.toString();
    } else {
      throw new MolgenisValidationException(
          new ConstraintViolation(
              format(
                  "Attribute [%s] value is of type [%s] instead of [%s] or [%s]",
                  attr.getName(),
                  value.getClass().getSimpleName(),
                  String.class.getSimpleName(),
                  Enum.class.getSimpleName())));
    }

    if (!attr.getEnumOptions().contains(stringValue)) {
      throw new MolgenisValidationException(
          new ConstraintViolation(
              format(
                  "Attribute [%s] value [%s] is not a valid enum option",
                  attr.getName(), stringValue)));
    }

    return stringValue;
  }