Search in sources :

Example 1 with Length

use of org.hibernate.validator.constraints.Length in project swagger-core by swagger-api.

the class BeanValidator method resolveProperty.

@Override
public Property resolveProperty(Type type, ModelConverterContext context, Annotation[] annotations, Iterator<ModelConverter> chain) {
    Map<String, Annotation> annos = new HashMap<String, Annotation>();
    if (annotations != null) {
        for (Annotation anno : annotations) {
            annos.put(anno.annotationType().getName(), anno);
        }
    }
    Property property = null;
    if (chain.hasNext()) {
        property = chain.next().resolveProperty(type, context, annotations, chain);
    }
    if (property != null) {
        if (annos.containsKey("org.hibernate.validator.constraints.NotEmpty")) {
            property.setRequired(true);
            if (property instanceof StringProperty) {
                ((StringProperty) property).minLength(1);
            } else if (property instanceof ArrayProperty) {
                ((ArrayProperty) property).setMinItems(1);
            }
        }
        if (annos.containsKey("org.hibernate.validator.constraints.NotBlank")) {
            property.setRequired(true);
            if (property instanceof StringProperty) {
                ((StringProperty) property).minLength(1);
            }
        }
        if (annos.containsKey("org.hibernate.validator.constraints.Range")) {
            if (property instanceof AbstractNumericProperty) {
                Range range = (Range) annos.get("org.hibernate.validator.constraints.Range");
                AbstractNumericProperty ap = (AbstractNumericProperty) property;
                ap.setMinimum(new BigDecimal(range.min()));
                ap.setMaximum(new BigDecimal(range.max()));
            }
        }
        if (annos.containsKey("org.hibernate.validator.constraints.Length")) {
            if (property instanceof StringProperty) {
                Length length = (Length) annos.get("org.hibernate.validator.constraints.Length");
                StringProperty sp = (StringProperty) property;
                sp.minLength(new Integer(length.min()));
                sp.maxLength(new Integer(length.max()));
            }
        }
        if (annos.containsKey("org.hibernate.validator.constraints.Email")) {
            if (property instanceof StringProperty) {
                EmailProperty sp = new EmailProperty((StringProperty) property);
                property = sp;
            }
        }
        return property;
    }
    return super.resolveProperty(type, context, annotations, chain);
}
Also used : ArrayProperty(io.swagger.models.properties.ArrayProperty) HashMap(java.util.HashMap) StringProperty(io.swagger.models.properties.StringProperty) Range(org.hibernate.validator.constraints.Range) Annotation(java.lang.annotation.Annotation) BigDecimal(java.math.BigDecimal) AbstractNumericProperty(io.swagger.models.properties.AbstractNumericProperty) EmailProperty(io.swagger.models.properties.EmailProperty) Length(org.hibernate.validator.constraints.Length) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) EmailProperty(io.swagger.models.properties.EmailProperty) AbstractNumericProperty(io.swagger.models.properties.AbstractNumericProperty) Property(io.swagger.models.properties.Property)

Example 2 with Length

use of org.hibernate.validator.constraints.Length in project cuba by cuba-platform.

the class WebAbstractTextField method setDatasource.

@Override
public void setDatasource(Datasource datasource, String property) {
    super.setDatasource(datasource, property);
    if (metaProperty != null) {
        Map<String, Object> annotations = metaProperty.getAnnotations();
        if (this instanceof CaseConversionSupported && ((CaseConversionSupported) this).getCaseConversion() == CaseConversion.NONE) {
            String caseConversionAnnotation = com.haulmont.cuba.core.entity.annotation.CaseConversion.class.getName();
            // noinspection unchecked
            Map<String, Object> caseConversion = (Map<String, Object>) annotations.get(caseConversionAnnotation);
            if (MapUtils.isNotEmpty(caseConversion)) {
                ConversionType conversionType = (ConversionType) caseConversion.get("type");
                CaseConversion conversion = CaseConversion.valueOf(conversionType.name());
                ((CaseConversionSupported) this).setCaseConversion(conversion);
            }
        }
        if (this instanceof TextInputField.MaxLengthLimited) {
            MaxLengthLimited maxLengthLimited = (MaxLengthLimited) this;
            Integer maxLength = (Integer) annotations.get("length");
            if (maxLength != null) {
                maxLengthLimited.setMaxLength(maxLength);
            }
            Integer sizeMax = (Integer) annotations.get(Size.class.getName() + "_max");
            if (sizeMax != null) {
                maxLengthLimited.setMaxLength(sizeMax);
            }
            Integer lengthMax = (Integer) annotations.get(Length.class.getName() + "_max");
            if (lengthMax != null) {
                maxLengthLimited.setMaxLength(lengthMax);
            }
        }
    }
}
Also used : Size(javax.validation.constraints.Size) ConversionType(com.haulmont.cuba.core.entity.annotation.ConversionType) Length(org.hibernate.validator.constraints.Length) Map(java.util.Map)

Example 3 with Length

use of org.hibernate.validator.constraints.Length in project cuba by cuba-platform.

the class MetaModelLoader method loadBeanValidationAnnotations.

protected void loadBeanValidationAnnotations(MetaProperty metaProperty, AnnotatedElement annotatedElement) {
    NotNull notNull = annotatedElement.getAnnotation(NotNull.class);
    if (notNull != null) {
        if (isDefinedForDefaultValidationGroup(notNull)) {
            metaProperty.getAnnotations().put(NotNull.class.getName() + VALIDATION_NOTNULL_MESSAGE, notNull.message());
        }
        if (isDefinedForValidationGroup(notNull, UiComponentChecks.class, true)) {
            metaProperty.getAnnotations().put(NotNull.class.getName() + VALIDATION_NOTNULL_MESSAGE, notNull.message());
            metaProperty.getAnnotations().put(NotNull.class.getName() + VALIDATION_NOTNULL_UI_COMPONENT, true);
        }
    }
    Size size = annotatedElement.getAnnotation(Size.class);
    if (size != null && isDefinedForDefaultValidationGroup(size)) {
        metaProperty.getAnnotations().put(Size.class.getName() + VALIDATION_MIN, size.min());
        metaProperty.getAnnotations().put(Size.class.getName() + VALIDATION_MAX, size.max());
    }
    Length length = annotatedElement.getAnnotation(Length.class);
    if (length != null && isDefinedForDefaultValidationGroup(length)) {
        metaProperty.getAnnotations().put(Length.class.getName() + VALIDATION_MIN, length.min());
        metaProperty.getAnnotations().put(Length.class.getName() + VALIDATION_MAX, length.max());
    }
    Min min = annotatedElement.getAnnotation(Min.class);
    if (min != null && isDefinedForDefaultValidationGroup(min)) {
        metaProperty.getAnnotations().put(Min.class.getName(), min.value());
    }
    Max max = annotatedElement.getAnnotation(Max.class);
    if (max != null && isDefinedForDefaultValidationGroup(max)) {
        metaProperty.getAnnotations().put(Max.class.getName(), max.value());
    }
    DecimalMin decimalMin = annotatedElement.getAnnotation(DecimalMin.class);
    if (decimalMin != null && isDefinedForDefaultValidationGroup(decimalMin)) {
        metaProperty.getAnnotations().put(DecimalMin.class.getName(), decimalMin.value());
    }
    DecimalMax decimalMax = annotatedElement.getAnnotation(DecimalMax.class);
    if (decimalMax != null && isDefinedForDefaultValidationGroup(decimalMax)) {
        metaProperty.getAnnotations().put(DecimalMax.class.getName(), decimalMax.value());
    }
    Past past = annotatedElement.getAnnotation(Past.class);
    if (past != null && isDefinedForDefaultValidationGroup(past)) {
        metaProperty.getAnnotations().put(Past.class.getName(), true);
    }
    Future future = annotatedElement.getAnnotation(Future.class);
    if (future != null && isDefinedForDefaultValidationGroup(future)) {
        metaProperty.getAnnotations().put(Future.class.getName(), true);
    }
    PastOrPresent pastOrPresent = annotatedElement.getAnnotation(PastOrPresent.class);
    if (pastOrPresent != null && isDefinedForDefaultValidationGroup(pastOrPresent)) {
        metaProperty.getAnnotations().put(PastOrPresent.class.getName(), true);
    }
    FutureOrPresent futureOrPresent = annotatedElement.getAnnotation(FutureOrPresent.class);
    if (futureOrPresent != null && isDefinedForDefaultValidationGroup(futureOrPresent)) {
        metaProperty.getAnnotations().put(FutureOrPresent.class.getName(), true);
    }
}
Also used : Length(org.hibernate.validator.constraints.Length)

Example 4 with Length

use of org.hibernate.validator.constraints.Length in project cuba by cuba-platform.

the class DataAwareComponentsTools method setupMaxLength.

/**
 * Sets max length for textual UI component using Entity metadata.
 *
 * @param component UI component
 * @param valueSource value source
 */
public void setupMaxLength(TextInputField.MaxLengthLimited component, EntityValueSource valueSource) {
    MetaProperty metaProperty = valueSource.getMetaPropertyPath().getMetaProperty();
    Map<String, Object> annotations = metaProperty.getAnnotations();
    Integer maxLength = (Integer) annotations.get("length");
    if (maxLength != null) {
        component.setMaxLength(maxLength);
    }
    Integer sizeMax = (Integer) annotations.get(Size.class.getName() + "_max");
    if (sizeMax != null) {
        component.setMaxLength(sizeMax);
    }
    Integer lengthMax = (Integer) annotations.get(Length.class.getName() + "_max");
    if (lengthMax != null) {
        component.setMaxLength(lengthMax);
    }
}
Also used : Length(org.hibernate.validator.constraints.Length) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Aggregations

Length (org.hibernate.validator.constraints.Length)4 MetaProperty (com.haulmont.chile.core.model.MetaProperty)1 ConversionType (com.haulmont.cuba.core.entity.annotation.ConversionType)1 AbstractNumericProperty (io.swagger.models.properties.AbstractNumericProperty)1 ArrayProperty (io.swagger.models.properties.ArrayProperty)1 EmailProperty (io.swagger.models.properties.EmailProperty)1 Property (io.swagger.models.properties.Property)1 StringProperty (io.swagger.models.properties.StringProperty)1 Annotation (java.lang.annotation.Annotation)1 BigDecimal (java.math.BigDecimal)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Size (javax.validation.constraints.Size)1 Range (org.hibernate.validator.constraints.Range)1