下面列出了怎么用org.hibernate.validator.cfg.ConstraintMapping的API类实例代码及写法,或者点击链接到github查看源代码。
protected HibernateValidatorConfiguration getValidatorFactoryConfiguration(Locale locale) {
HibernateValidatorConfiguration configuration = Validation.byProvider(HibernateValidator.class)
.configure()
.clockProvider(new CubaValidationTimeProvider(timeSource))
.traversableResolver(new CubaValidationTraversableResolver(metadata, entityStates))
.messageInterpolator(new CubaValidationMessagesInterpolator(messages, locale));
ConstraintMapping constraintMapping = configuration.createConstraintMapping();
//Hibernate validators doesn't support java.sql.Date.
//Replace standard validators for java.util.Date with support java.sql.Date
registerPastValidators(constraintMapping.constraintDefinition(Past.class));
registerPastOrPresentValidators(constraintMapping.constraintDefinition(PastOrPresent.class));
registerFutureValidators(constraintMapping.constraintDefinition(Future.class));
registerFutureOrPresentValidators(constraintMapping.constraintDefinition(FutureOrPresent.class));
configuration.addMapping(constraintMapping);
return configuration;
}
public static HibernateValidatorConfiguration getPolicyTriggerRange(HibernateValidatorConfiguration config) {
Map<String, Map<String, Map<String, Map<String, String>>>> mulit_range = Constants.getTriggerRangeByTriggerType();
logger.debug("mulit trigger range: " + mulit_range.toString());
for (String class_type : mulit_range.keySet()){
Map<String, Map<String, Map<String, String>>> range = mulit_range.get(class_type);
Class<?> class_obj=null;
if (class_type.equals("trigger_Memory")) //only one metricType supported now
class_obj = PolicyTrigger.class;
for (String key : range.keySet()) {
Map<String, Map<String, String>> value = range.get(key);
for(String value_key : value.keySet()){
Map<String, String> value_item = value.get(value_key);
ConstraintMapping mapping = config.createConstraintMapping();
if (value_key.endsWith("Min")){
mapping.type(class_obj).property(key, ElementType.FIELD).constraint( new MinDef().value(Integer.parseInt(value_item.get("value"))).message(value_item.get("message")));
}
else if(value_key.endsWith("Max")){
mapping.type(class_obj).property(key, ElementType.FIELD).constraint( new MaxDef().value(Integer.parseInt(value_item.get("value"))).message(value_item.get("message")));
}
else if(value_key.endsWith("NotNull")){
mapping.type(class_obj).property(key, ElementType.FIELD).constraint( new NotNullDef().message(value_item.get("message")));
}
config.addMapping( mapping );
}
}
}
return config;
}
@Override
public Validator getValidator(@Nullable ConstraintMapping constraintMapping, ValidationOptions options) {
checkNotNullArgument(options);
if (constraintMapping == null
&& options.getFailFast() == null
&& options.getLocale() != null) {
return getValidatorWithDefaultFactory(options.getLocale());
}
Locale locale;
if (options.getLocale() != null) {
locale = options.getLocale();
} else {
locale = getCurrentLocale();
}
HibernateValidatorConfiguration configuration = getValidatorFactoryConfiguration(locale);
if (options.getFailFast() != null) {
configuration.failFast(options.getFailFast());
}
if (constraintMapping != null) {
configuration.addMapping(constraintMapping);
}
ValidatorFactory factory = configuration.buildValidatorFactory();
return factory.getValidator();
}
private ConstraintMapping configureTransforms(ConstraintMapping constraints) {
constraints.type(TransformSort.class)
.property("sortedColumnName", FIELD).constraint(new NotBlankDef());
// order
constraints.type(TransformSorts.class)
.property("columns", FIELD).constraint(new NotEmptyDef());
constraints.type(TransformDrop.class)
.property("droppedColumnName", FIELD).constraint(new NotBlankDef());
constraints.type(TransformRename.class)
.property("oldColumnName", FIELD).constraint(new NotBlankDef())
.property("newColumnName", FIELD).constraint(new NotBlankDef());
constraints.type(TransformAddCalculatedField.class)
.property("newColumnName", FIELD).constraint(new NotBlankDef())
.property("expression", FIELD).constraint(new NotBlankDef());
constraints.type(TransformUpdateSQL.class)
.property("sql", FIELD).constraint(new NotBlankDef());
constraints.type(TransformField.class)
.property("sourceColumnName", FIELD).constraint(new NotBlankDef())
.property("fieldTransformation", FIELD).constraint(new NotNullDef());
// newColumnName
// dropSourceColumn
constraints.type(FieldConvertTextToDate.class)
.property("format", FIELD).constraint(new NotBlankDef());
// optional but one of the date/time types:
// .property("desiredType", FIELD).constraint(new ());
constraints.type(TransformConvertToSingleType.class)
.property("sourceColumnName", FIELD).constraint(new NotBlankDef())
.property("newColumnName", FIELD).constraint(new NotBlankDef())
.property("dropSourceColumn", FIELD).constraint(new NotNullDef())
.property("desiredType", FIELD).constraint(new NotNullDef())
.property("castWhenPossible", FIELD).constraint(new NotNullDef())
.property("actionForNonMatchingValue", FIELD).constraint(new NotNullDef());
// defaultValue // optional
constraints.type(TransformSplitByDataType.class)
.property("sourceColumnName", FIELD).constraint(new NotBlankDef())
.property("newColumnNamePrefix", FIELD).constraint(new NotBlankDef())
.property("dropSourceColumn", FIELD).constraint(new NotNullDef())
.property("selectedTypes", FIELD).constraint(new NotEmptyDef());
constraints.type(TransformFilter.class)
.property("sourceColumnName", FIELD).constraint(new NotBlankDef())
.property("filter", FIELD).constraint(new NotNullDef());
// keepNull
// exclude
constraints.type(Dimension.class)
.property("column", FIELD).constraint(new NotBlankDef());
constraints.type(Measure.class)
.property("column", FIELD).constraint(new NotBlankDef())
.property("type", FIELD).constraint(new NotNullDef());
constraints.type(TransformGroupBy.class).constraint(new TransformGroupByConstraintDef());
return constraints;
}
@Override
public Validator getValidator(ConstraintMapping constraintMapping) {
checkNotNullArgument(constraintMapping);
return getValidator(constraintMapping, NO_VALIDATION_OPTIONS);
}
@Override
public Validator getValidator(ConstraintMapping constraintMapping) {
return new ValidatorStub();
}
@Override
public Validator getValidator(@Nullable ConstraintMapping constraintMapping, ValidationOptions options) {
return new ValidatorStub();
}
/**
* Get validator with custom constraint mapping and current locale if there is current UserSession or with default locale.
*
* @param constraintMapping constraint mapping
* @return validator
*/
Validator getValidator(ConstraintMapping constraintMapping);
/**
* Get validator with custom constraint mapping and additional validation options.
*
* @param constraintMapping constraint mapping
* @param options options
* @return validator
*/
Validator getValidator(@Nullable ConstraintMapping constraintMapping, ValidationOptions options);