Java 类org.hibernate.validator.constraintvalidation.HibernateConstraintValidatorContext 实例源码

项目:Re-Collector    文件:IsOneOfValidator.java   
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
    if (value == null) {
        return true;
    }
    final boolean valid = Arrays.asList(strings).contains(value);

    if (!valid) {
        HibernateConstraintValidatorContext hibernateContext = context.unwrap(HibernateConstraintValidatorContext.class);
        hibernateContext.disableDefaultConstraintViolation();

        hibernateContext.addExpressionVariable("validValues", Joiner.on(" ").join(strings))
                .buildConstraintViolationWithTemplate(hibernateContext.getDefaultConstraintMessageTemplate())
                .addConstraintViolation();
    }

    return valid;
}
项目:UniqueValidator    文件:UniqueValidator.java   
public boolean isValidInSession(Object value, ConstraintValidatorContext context){
    if(value == null){
        return true;
    }

    TreeMap<String, Object> fieldMap = _countRows(value);
    if(fieldMap != null){
        String message = _messageSource.getMessage(context.getDefaultConstraintMessageTemplate(), null, _defaultMesssage, LocaleContextHolder.getLocale());
        Map.Entry<String, Object> field = fieldMap.entrySet().iterator().next();
        context.unwrap(HibernateConstraintValidatorContext.class)
                .addExpressionVariable("name", value.getClass().getSimpleName())
                .addExpressionVariable("fullName", value.getClass().getName())
                .addExpressionVariable("field", field.getKey())
                .addExpressionVariable("value", field.getValue())
                .addExpressionVariable("allFields", StringUtils.join(fieldMap.keySet(), ", "))
                .addExpressionVariable("values", StringUtils.join(fieldMap.values(), ", "))
                .buildConstraintViolationWithTemplate(message)
                .addPropertyNode(field.getKey())
                .addConstraintViolation()
                .disableDefaultConstraintViolation();

        return false;
    }

    return true;
}
项目:collector    文件:IsOneOfValidator.java   
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
    if (value == null) {
        return true;
    }
    final boolean valid = Arrays.asList(strings).contains(value);

    if (!valid) {
        HibernateConstraintValidatorContext hibernateContext = context.unwrap(HibernateConstraintValidatorContext.class);
        hibernateContext.disableDefaultConstraintViolation();

        hibernateContext.addExpressionVariable("validValues", Joiner.on(" ").join(strings))
                .buildConstraintViolationWithTemplate(hibernateContext.getDefaultConstraintMessageTemplate())
                .addConstraintViolation();
    }

    return valid;
}
项目:gwt-bean-validators    文件:PatternValidator.java   
@Override
public boolean isValid(final CharSequence value,
    final ConstraintValidatorContext constraintValidatorContext) {
  if (value == null || this.pattern == null) {
    return true;
  }
  if (constraintValidatorContext instanceof HibernateConstraintValidatorContext) {
    constraintValidatorContext.unwrap(HibernateConstraintValidatorContext.class)
        .addMessageParameter("regexp", this.escapedRegexp);
  }

  final MatchResult match = this.pattern.exec(value.toString());
  if (match == null) {
    return false;
  }
  // Must match the entire string
  return match.getGroup(0).length() == value.length();
}
项目:syndesis    文件:UniquePropertyValidator.java   
@Override
public boolean isValid(final WithId<?> value, final ConstraintValidatorContext context) {
    if (value == null) {
        return true;
    }

    final PropertyAccessor bean = new BeanWrapperImpl(value);

    final String propertyValue = String.valueOf(bean.getPropertyValue(property));

    @SuppressWarnings({"rawtypes", "unchecked"})
    final Class<WithId> modelClass = (Class) value.getKind().modelClass;

    @SuppressWarnings("unchecked")
    final Set<String> ids = dataManager.fetchIdsByPropertyValue(modelClass, property, propertyValue);

    final boolean isUnique = ids.isEmpty() || value.getId().map(id -> ids.contains(id)).orElse(false);

    if (!isUnique) {
        if (ids.stream().allMatch(id -> consideredValidByException(modelClass, id))) {
            return true;
        }

        context.disableDefaultConstraintViolation();
        context.unwrap(HibernateConstraintValidatorContext.class).addExpressionVariable("nonUnique", propertyValue)
            .buildConstraintViolationWithTemplate(context.getDefaultConstraintMessageTemplate())
            .addPropertyNode(property).addConstraintViolation();
    }

    return isUnique;
}
项目:syndesis    文件:UniquePropertyValidatorTest.java   
@Test
public void shouldAscertainPropertyUniqueness() {
    final HibernateConstraintValidatorContext context = mock(HibernateConstraintValidatorContext.class);
    when(context.unwrap(HibernateConstraintValidatorContext.class)).thenReturn(context);
    when(context.addExpressionVariable(eq("nonUnique"), anyString())).thenReturn(context);
    when(context.getDefaultConstraintMessageTemplate()).thenReturn("template");
    final ConstraintViolationBuilder builder = mock(ConstraintViolationBuilder.class);
    when(context.buildConstraintViolationWithTemplate("template")).thenReturn(builder);
    when(builder.addPropertyNode(anyString())).thenReturn(mock(NodeBuilderCustomizableContext.class));

    assertThat(validator.isValid(connection, context)).isEqualTo(validity);
}
项目:common-libraries    文件:PresentOrFutureValidatorForLocalDate.java   
@Override
public boolean isValid(LocalDate value, ConstraintValidatorContext context) {
    // null values are valid
    if (value == null) {
        return true;
    }
    TimeProvider timeProvider = context.unwrap(HibernateConstraintValidatorContext.class)
            .getTimeProvider();
    long now = timeProvider.getCurrentTime();
    final LocalDate localDate = Instant.ofEpochMilli(now)
            .atZone(ZoneId.systemDefault())
            .toLocalDate();
    return localDate
            .isBefore(value) || localDate.isEqual(value);
}
项目:common-libraries    文件:FutureValidatorForLocalDate.java   
@Override
public boolean isValid(LocalDate value, ConstraintValidatorContext context) {
    // null values are valid
    if (value == null) {
        return true;
    }
    TimeProvider timeProvider = context.unwrap(HibernateConstraintValidatorContext.class)
            .getTimeProvider();
    long now = timeProvider.getCurrentTime();
    return Instant.ofEpochMilli(now)
            .atZone(ZoneId.systemDefault())
            .toLocalDate()
            .isBefore(value);
}
项目:common-libraries    文件:PresentOrFutureValidatorForLocalDateTime.java   
@Override
public boolean isValid(LocalDateTime value, ConstraintValidatorContext constraintValidatorContext) {
    if(value ==null){
        return true;
    }
    TimeProvider timeProvider=constraintValidatorContext.unwrap(HibernateConstraintValidatorContext.class).getTimeProvider();
    long now=timeProvider.getCurrentTime();
    final LocalDateTime localDateTime = Instant.ofEpochMilli(now)
            .atZone(ZoneId.systemDefault()).toLocalDateTime();
    return localDateTime.isBefore(value) || localDateTime.isEqual(value);
}
项目:common-libraries    文件:StartOfTodayOrFutureValidatorForLocalDateTime.java   
@Override
public boolean isValid(LocalDateTime value, ConstraintValidatorContext constraintValidatorContext) {
    if (value == null) {
        return true;
    }
    TimeProvider timeProvider = constraintValidatorContext.unwrap(HibernateConstraintValidatorContext.class).getTimeProvider();
    long now = timeProvider.getCurrentTime();
    final LocalDate localDate = Instant.ofEpochMilli(now)
            .atZone(ZoneId.systemDefault())
            .toLocalDate();
    final LocalDateTime localDateTime = LocalDateTime.of(localDate, LocalTime.MIN);
    return localDateTime.isBefore(value) || localDateTime.isEqual(value);
}
项目:common-libraries    文件:FutureValidatorForLocalDateTime.java   
@Override
public boolean isValid(LocalDateTime value, ConstraintValidatorContext constraintValidatorContext) {
    if (value == null) {
        return true;
    }
    TimeProvider timeProvider = constraintValidatorContext.unwrap(HibernateConstraintValidatorContext.class).getTimeProvider();
    long now = timeProvider.getCurrentTime();
    return Instant.ofEpochMilli(now).atZone(ZoneId.systemDefault()).toLocalDateTime().isBefore(value);
}
项目:common-libraries    文件:PastOrPresentValidatorForLocalDate.java   
@Override
public boolean isValid(LocalDate value, ConstraintValidatorContext context) {
    // null values are valid
    if (value == null) {
        return true;
    }
    TimeProvider timeProvider = context.unwrap(HibernateConstraintValidatorContext.class)
            .getTimeProvider();
    long now = timeProvider.getCurrentTime();
    final LocalDate localDate = Instant.ofEpochMilli(now)
            .atZone(ZoneId.systemDefault())
            .toLocalDate();
    return localDate
            .isAfter(value) || localDate.isEqual(value);
}
项目:common-libraries    文件:PastOrPresentValidatorForLocalDateTime.java   
@Override
public boolean isValid(LocalDateTime value, ConstraintValidatorContext constraintValidatorContext) {
    if (value == null) {
        return true;
    }
    TimeProvider timeProvider = constraintValidatorContext.unwrap(HibernateConstraintValidatorContext.class).getTimeProvider();
    long now = timeProvider.getCurrentTime();
    final LocalDateTime localDateTime = Instant.ofEpochMilli(now).atZone(ZoneId.systemDefault()).toLocalDateTime();
    return localDateTime.isAfter(value) || localDateTime.isEqual(value);
}
项目:common-libraries    文件:PastValidatorForLocalDate.java   
@Override
public boolean isValid(LocalDate value, ConstraintValidatorContext context) {
    // null values are valid
    if (value == null) {
        return true;
    }
    TimeProvider timeProvider = context.unwrap(HibernateConstraintValidatorContext.class)
            .getTimeProvider();
    long now = timeProvider.getCurrentTime();
    return Instant.ofEpochMilli(now)
            .atZone(ZoneId.systemDefault())
            .toLocalDate()
            .isAfter(value);
}
项目:syndesis-rest    文件:UniquePropertyValidator.java   
@Override
public boolean isValid(final WithId<?> value, final ConstraintValidatorContext context) {
    if (value == null) {
        return true;
    }

    final PropertyAccessor bean = new BeanWrapperImpl(value);

    final String propertyValue = String.valueOf(bean.getPropertyValue(property));

    @SuppressWarnings({"rawtypes", "unchecked"})
    final Class<WithId> modelClass = (Class) value.getKind().modelClass;

    @SuppressWarnings("unchecked")
    final Set<String> ids = dataManager.fetchIdsByPropertyValue(modelClass, property, propertyValue);

    final boolean isUnique = ids.isEmpty() || value.getId().map(id -> ids.contains(id)).orElse(false);

    if (!isUnique) {
        if (ids.stream().allMatch(id -> consideredValidByException(modelClass, id))) {
            return true;
        }

        context.disableDefaultConstraintViolation();
        context.unwrap(HibernateConstraintValidatorContext.class).addExpressionVariable("nonUnique", propertyValue)
            .buildConstraintViolationWithTemplate(context.getDefaultConstraintMessageTemplate())
            .addPropertyNode(property).addConstraintViolation();
    }

    return isUnique;
}
项目:syndesis-rest    文件:UniquePropertyValidatorTest.java   
@Test
public void shouldAscertainPropertyUniqueness() {
    final HibernateConstraintValidatorContext context = mock(HibernateConstraintValidatorContext.class);
    when(context.unwrap(HibernateConstraintValidatorContext.class)).thenReturn(context);
    when(context.addExpressionVariable(eq("nonUnique"), anyString())).thenReturn(context);
    when(context.getDefaultConstraintMessageTemplate()).thenReturn("template");
    final ConstraintViolationBuilder builder = mock(ConstraintViolationBuilder.class);
    when(context.buildConstraintViolationWithTemplate("template")).thenReturn(builder);
    when(builder.addPropertyNode(anyString())).thenReturn(mock(NodeBuilderCustomizableContext.class));

    assertThat(validator.isValid(connection, context)).isEqualTo(validity);
}
项目:WQP-WQX-Services    文件:PointLocationValidatorTest.java   
@SuppressWarnings("unchecked")
protected void assertNoLat(ConstraintValidatorContext context) {
    ConstraintViolationCreationContext x = ((ConstraintValidatorContextImpl) context.unwrap(HibernateConstraintValidatorContext.class)).getConstraintViolationCreationContexts().get(0);
    Map<String, Object> ev = x.getExpressionVariables();
    assertEquals(2, ev.size());
    Set<String> contains = (Set<String>) ev.get("containsSet");
    assertThat(contains, containsInAnyOrder("long", "within"));
    Set<String> missing = (Set<String>) ev.get("missingSet");
    assertThat(missing, containsInAnyOrder("lat"));
    assertEquals("{message}", x.getMessage());
}
项目:WQP-WQX-Services    文件:PointLocationValidatorTest.java   
@SuppressWarnings("unchecked")
protected void assertNoLong(ConstraintValidatorContext context) {
    ConstraintViolationCreationContext x = ((ConstraintValidatorContextImpl) context.unwrap(HibernateConstraintValidatorContext.class)).getConstraintViolationCreationContexts().get(0);
    Map<String, Object> ev = x.getExpressionVariables();
    assertEquals(2, ev.size());
    Set<String> contains = (Set<String>) ev.get("containsSet");
    assertThat(contains, containsInAnyOrder("lat", "within"));
    Set<String> missing = (Set<String>) ev.get("missingSet");
    assertThat(missing, containsInAnyOrder("long"));
    assertEquals("{message}", x.getMessage());
}
项目:WQP-WQX-Services    文件:PointLocationValidatorTest.java   
@SuppressWarnings("unchecked")
protected void assertNoWithin(ConstraintValidatorContext context) {
    ConstraintViolationCreationContext x = ((ConstraintValidatorContextImpl) context.unwrap(HibernateConstraintValidatorContext.class)).getConstraintViolationCreationContexts().get(0);
    Map<String, Object> ev = x.getExpressionVariables();
    assertEquals(2, ev.size());
    Set<String> contains = (Set<String>) ev.get("containsSet");
    assertThat(contains, containsInAnyOrder("lat", "long"));
    Set<String> missing = (Set<String>) ev.get("missingSet");
    assertThat(missing, containsInAnyOrder("within"));
    assertEquals("{message}", x.getMessage());
}
项目:Re-Collector    文件:FileInputConfigurationValidator.java   
private void setMessageTemplate(ConstraintValidatorContext context, String messageTemplate, String value) {
    HibernateConstraintValidatorContext hibernateContext = context.unwrap(HibernateConstraintValidatorContext.class);
    hibernateContext.disableDefaultConstraintViolation();
    hibernateContext.addExpressionVariable("value", value).buildConstraintViolationWithTemplate(messageTemplate).addConstraintViolation();
}
项目:common-libraries    文件:PastValidatorForLocalDateTime.java   
@Override
public boolean isValid(LocalDateTime value, ConstraintValidatorContext constraintValidatorContext) {
    TimeProvider timeProvider= constraintValidatorContext.unwrap(HibernateConstraintValidatorContext.class).getTimeProvider();
    long now=timeProvider.getCurrentTime();
    return Instant.ofEpochMilli(now).atZone(ZoneId.systemDefault()).toLocalDateTime().isAfter(value);
}
项目:collector    文件:FileInputConfigurationValidator.java   
private void setMessageTemplate(ConstraintValidatorContext context, String messageTemplate, String value) {
    HibernateConstraintValidatorContext hibernateContext = context.unwrap(HibernateConstraintValidatorContext.class);
    hibernateContext.disableDefaultConstraintViolation();
    hibernateContext.addExpressionVariable("value", value).buildConstraintViolationWithTemplate(messageTemplate).addConstraintViolation();
}
项目:WQP-WQX-Services    文件:PointLocationValidator.java   
@Override
public boolean isValid(FilterParameters value, ConstraintValidatorContext context) {
    if (value == null) {
        return true;
    }

    SortedSet<String> containsSet = new TreeSet<String>();
    SortedSet<String> missingSet = new TreeSet<String>();

    if (StringUtils.isBlank(value.getLat())) {
        missingSet.add(Parameters.LATITUDE.toString());
    } else {
        containsSet.add(Parameters.LATITUDE.toString());
    }

    if (StringUtils.isBlank(value.getLong())) {
        missingSet.add(Parameters.LONGITUDE.toString());
    } else {
        containsSet.add(Parameters.LONGITUDE.toString());
    }

    if (StringUtils.isBlank(value.getWithin())) {
        missingSet.add(Parameters.WITHIN.toString());
    } else {
        containsSet.add(Parameters.WITHIN.toString());
    }

    if (containsSet.size() > 0 && missingSet.size() > 0) {
        HibernateConstraintValidatorContext hibernateContext = context.unwrap(HibernateConstraintValidatorContext.class);

        hibernateContext.disableDefaultConstraintViolation();
        hibernateContext
            .addExpressionVariable("containsSet", containsSet)
            .addExpressionVariable("missingSet", missingSet)
            .buildConstraintViolationWithTemplate("{message}")
            .addConstraintViolation();

        return false;
    } else {
        return true;
    }
}