Search in sources :

Example 1 with Size

use of javax.validation.constraints.Size in project hibernate-orm by hibernate.

the class TypeSafeActivator method applySize.

private static void applySize(Property property, ConstraintDescriptor<?> descriptor, PropertyDescriptor propertyDescriptor) {
    if (Size.class.equals(descriptor.getAnnotation().annotationType()) && String.class.equals(propertyDescriptor.getElementClass())) {
        @SuppressWarnings("unchecked") ConstraintDescriptor<Size> sizeConstraint = (ConstraintDescriptor<Size>) descriptor;
        int max = sizeConstraint.getAnnotation().max();
        @SuppressWarnings("unchecked") final Iterator<Selectable> itor = property.getColumnIterator();
        if (itor.hasNext()) {
            final Selectable selectable = itor.next();
            Column col = (Column) selectable;
            if (max < Integer.MAX_VALUE) {
                col.setLength(max);
            }
        }
    }
}
Also used : Selectable(org.hibernate.mapping.Selectable) Column(org.hibernate.mapping.Column) Size(javax.validation.constraints.Size) ConstraintDescriptor(javax.validation.metadata.ConstraintDescriptor)

Example 2 with Size

use of javax.validation.constraints.Size in project candlepin by candlepin.

the class CandlepinSwaggerModelConverter method applyBeanValidatorAnnotations.

protected void applyBeanValidatorAnnotations(Property property, Annotation[] annotations) {
    Map<String, Annotation> annos = new HashMap<>();
    if (annotations != null) {
        for (Annotation anno : annotations) {
            annos.put(anno.annotationType().getName(), anno);
        }
    }
    if (annos.containsKey("javax.validation.constraints.NotNull")) {
        property.setRequired(true);
    }
    if (annos.containsKey("javax.validation.constraints.Min")) {
        if (property instanceof AbstractNumericProperty) {
            Min min = (Min) annos.get("javax.validation.constraints.Min");
            AbstractNumericProperty ap = (AbstractNumericProperty) property;
            ap.setMinimum(new Double(min.value()));
        }
    }
    if (annos.containsKey("javax.validation.constraints.Max")) {
        if (property instanceof AbstractNumericProperty) {
            Max max = (Max) annos.get("javax.validation.constraints.Max");
            AbstractNumericProperty ap = (AbstractNumericProperty) property;
            ap.setMaximum(new Double(max.value()));
        }
    }
    if (annos.containsKey("javax.validation.constraints.Size")) {
        Size size = (Size) annos.get("javax.validation.constraints.Size");
        if (property instanceof AbstractNumericProperty) {
            AbstractNumericProperty ap = (AbstractNumericProperty) property;
            ap.setMinimum(new Double(size.min()));
            ap.setMaximum(new Double(size.max()));
        } else if (property instanceof StringProperty) {
            StringProperty sp = (StringProperty) property;
            sp.minLength(new Integer(size.min()));
            sp.maxLength(new Integer(size.max()));
        } else if (property instanceof ArrayProperty) {
            ArrayProperty sp = (ArrayProperty) property;
            sp.setMinItems(size.min());
            sp.setMaxItems(size.max());
        }
    }
    if (annos.containsKey("javax.validation.constraints.DecimalMin")) {
        DecimalMin min = (DecimalMin) annos.get("javax.validation.constraints.DecimalMin");
        if (property instanceof AbstractNumericProperty) {
            AbstractNumericProperty ap = (AbstractNumericProperty) property;
            ap.setMinimum(new Double(min.value()));
        }
    }
    if (annos.containsKey("javax.validation.constraints.DecimalMax")) {
        DecimalMax max = (DecimalMax) annos.get("javax.validation.constraints.DecimalMax");
        if (property instanceof AbstractNumericProperty) {
            AbstractNumericProperty ap = (AbstractNumericProperty) property;
            ap.setMaximum(new Double(max.value()));
        }
    }
    if (annos.containsKey("javax.validation.constraints.Pattern")) {
        Pattern pattern = (Pattern) annos.get("javax.validation.constraints.Pattern");
        if (property instanceof StringProperty) {
            StringProperty ap = (StringProperty) property;
            ap.setPattern(pattern.regexp());
        }
    }
}
Also used : Pattern(javax.validation.constraints.Pattern) ArrayProperty(io.swagger.models.properties.ArrayProperty) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Max(javax.validation.constraints.Max) DecimalMax(javax.validation.constraints.DecimalMax) Size(javax.validation.constraints.Size) StringProperty(io.swagger.models.properties.StringProperty) DecimalMin(javax.validation.constraints.DecimalMin) Annotation(java.lang.annotation.Annotation) AbstractNumericProperty(io.swagger.models.properties.AbstractNumericProperty) Min(javax.validation.constraints.Min) DecimalMin(javax.validation.constraints.DecimalMin) DecimalMax(javax.validation.constraints.DecimalMax)

Example 3 with Size

use of javax.validation.constraints.Size in project irida by phac-nml.

the class SequenceFilePair method getForwardSequenceFile.

/**
 * Gets the forward {@link SequenceFile} from the pair.
 *
 * @return The forward {@link SequenceFile} from the pair.
 */
public SequenceFile getForwardSequenceFile() {
    IridaSequenceFile[] pair = getFiles().toArray(new IridaSequenceFile[getFiles().size()]);
    String[] filenames = { pair[0].getFile().getFileName().toString(), pair[1].getFile().getFileName().toString() };
    int index = StringUtils.indexOfDifference(filenames[0], filenames[1]);
    if (Stream.of(forwardMatches).anyMatch(x -> String.valueOf(filenames[0].charAt(index)).equals(x))) {
        return (SequenceFile) pair[0];
    } else if (Stream.of(forwardMatches).anyMatch(x -> String.valueOf(filenames[1].charAt(index)).equals(x))) {
        return (SequenceFile) pair[1];
    } else {
        throw new NoSuchElementException();
    }
}
Also used : Size(javax.validation.constraints.Size) Date(java.util.Date) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) Table(javax.persistence.Table) Lists(com.google.common.collect.Lists) Audited(org.hibernate.envers.Audited) FetchMode(org.hibernate.annotations.FetchMode) EntityListeners(javax.persistence.EntityListeners) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) AuditingEntityListener(org.springframework.data.jpa.domain.support.AuditingEntityListener) NoSuchElementException(java.util.NoSuchElementException) CollectionTable(javax.persistence.CollectionTable) IridaSequenceFilePair(ca.corefacility.bioinformatics.irida.model.irida.IridaSequenceFilePair) Entity(javax.persistence.Entity) Fetch(org.hibernate.annotations.Fetch) ImmutableSet(com.google.common.collect.ImmutableSet) Iterator(java.util.Iterator) CascadeType(javax.persistence.CascadeType) UniqueConstraint(javax.persistence.UniqueConstraint) JoinColumn(javax.persistence.JoinColumn) IridaSequenceFile(ca.corefacility.bioinformatics.irida.model.irida.IridaSequenceFile) OneToMany(javax.persistence.OneToMany) Set(java.util.Set) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) FetchType(javax.persistence.FetchType) IridaSequenceFile(ca.corefacility.bioinformatics.irida.model.irida.IridaSequenceFile) UniqueConstraint(javax.persistence.UniqueConstraint) NoSuchElementException(java.util.NoSuchElementException) IridaSequenceFile(ca.corefacility.bioinformatics.irida.model.irida.IridaSequenceFile)

Example 4 with Size

use of javax.validation.constraints.Size in project irida by phac-nml.

the class SequenceFilePair method getReverseSequenceFile.

/**
 * Gets the reverse {@link SequenceFile} from the pair.
 *
 * @return The reverse {@link SequenceFile} from the pair.
 */
public SequenceFile getReverseSequenceFile() {
    IridaSequenceFile[] pair = getFiles().toArray(new IridaSequenceFile[getFiles().size()]);
    String[] filenames = { pair[0].getFile().getFileName().toString(), pair[1].getFile().getFileName().toString() };
    int index = StringUtils.indexOfDifference(filenames[0], filenames[1]);
    if (Stream.of(reverseMatches).anyMatch(x -> String.valueOf(filenames[0].charAt(index)).equals(x))) {
        return (SequenceFile) pair[0];
    } else if (Stream.of(reverseMatches).anyMatch(x -> String.valueOf(filenames[1].charAt(index)).equals(x))) {
        return (SequenceFile) pair[1];
    } else {
        throw new NoSuchElementException();
    }
}
Also used : Size(javax.validation.constraints.Size) Date(java.util.Date) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) Table(javax.persistence.Table) Lists(com.google.common.collect.Lists) Audited(org.hibernate.envers.Audited) FetchMode(org.hibernate.annotations.FetchMode) EntityListeners(javax.persistence.EntityListeners) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) AuditingEntityListener(org.springframework.data.jpa.domain.support.AuditingEntityListener) NoSuchElementException(java.util.NoSuchElementException) CollectionTable(javax.persistence.CollectionTable) IridaSequenceFilePair(ca.corefacility.bioinformatics.irida.model.irida.IridaSequenceFilePair) Entity(javax.persistence.Entity) Fetch(org.hibernate.annotations.Fetch) ImmutableSet(com.google.common.collect.ImmutableSet) Iterator(java.util.Iterator) CascadeType(javax.persistence.CascadeType) UniqueConstraint(javax.persistence.UniqueConstraint) JoinColumn(javax.persistence.JoinColumn) IridaSequenceFile(ca.corefacility.bioinformatics.irida.model.irida.IridaSequenceFile) OneToMany(javax.persistence.OneToMany) Set(java.util.Set) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) FetchType(javax.persistence.FetchType) IridaSequenceFile(ca.corefacility.bioinformatics.irida.model.irida.IridaSequenceFile) UniqueConstraint(javax.persistence.UniqueConstraint) NoSuchElementException(java.util.NoSuchElementException) IridaSequenceFile(ca.corefacility.bioinformatics.irida.model.irida.IridaSequenceFile)

Example 5 with Size

use of javax.validation.constraints.Size in project AngularBeans by bessemHmidi.

the class BeanValidationProcessor method processBeanValidationParsing.

public void processBeanValidationParsing(Method method) {
    String modelName = CommonUtils.obtainFieldNameFromAccessor(method.getName());
    Annotation[] scannedAnnotations = method.getAnnotations();
    buffer.append("\nif (modelName === '").append(modelName).append("') {");
    for (Annotation a : scannedAnnotations) {
        if (!validationAnnotations.contains(a.annotationType())) {
            continue;
        }
        String name = (a.annotationType().getName());
        switch(name) {
            case "javax.validation.constraints.NotNull":
                buffer.append("\nentries[e].setAttribute('required', 'true');");
                break;
            case "org.hibernate.validator.constraints.Email":
                buffer.append("\nentries[e].setAttribute('type', 'email');");
                break;
            case "javax.validation.constraints.Pattern":
                String regex = ((Pattern) a).regexp();
                buffer.append("\nentries[e].setAttribute('ng-pattern', '").append(regex).append("');");
                break;
            case "javax.validation.constraints.Size":
                buffer.append("\nentries[e].setAttribute('required', 'true');");
                Size sizeConstraint = (Size) a;
                int maxLength = sizeConstraint.max();
                int minLength = sizeConstraint.min();
                if (minLength < 0) {
                    throw new IllegalArgumentException("The min parameter cannot be negative.");
                }
                if (maxLength < 0) {
                    throw new IllegalArgumentException("The max parameter cannot be negative.");
                }
                if (minLength > 0) {
                    buffer.append("\nentries[e].setAttribute('ng-minlength', '").append(minLength).append("');");
                }
                if (maxLength < Integer.MAX_VALUE) {
                    buffer.append("\nentries[e].setAttribute('ng-maxlength', '").append(maxLength).append("');");
                }
                break;
            case "javax.validation.constraints.DecimalMin":
                String dMin = ((DecimalMin) a).value();
                buffer.append("\nentries[e].setAttribute('min', '").append(dMin).append("');");
                break;
            case "javax.validation.constraints.DecimalMax":
                String dMax = ((DecimalMax) a).value();
                buffer.append("\nentries[e].setAttribute('max', '").append(dMax).append("');");
                break;
            case "javax.validation.constraints.Min":
                long min = ((Min) a).value();
                buffer.append("\nentries[e].setAttribute('min', '").append(min).append("');");
                break;
            case "javax.validation.constraints.Max":
                long max = ((Max) a).value();
                buffer.append("\nentries[e].setAttribute('max', '").append(max).append("');");
                break;
            default:
                break;
        }
    }
    buffer.append("\n}");
}
Also used : Pattern(javax.validation.constraints.Pattern) DecimalMin(javax.validation.constraints.DecimalMin) Min(javax.validation.constraints.Min) DecimalMax(javax.validation.constraints.DecimalMax) Max(javax.validation.constraints.Max) Size(javax.validation.constraints.Size) DecimalMin(javax.validation.constraints.DecimalMin) Annotation(java.lang.annotation.Annotation) DecimalMax(javax.validation.constraints.DecimalMax)

Aggregations

Size (javax.validation.constraints.Size)11 Annotation (java.lang.annotation.Annotation)7 Max (javax.validation.constraints.Max)7 Min (javax.validation.constraints.Min)7 Pattern (javax.validation.constraints.Pattern)7 DecimalMax (javax.validation.constraints.DecimalMax)6 DecimalMin (javax.validation.constraints.DecimalMin)6 HashMap (java.util.HashMap)5 NotNull (javax.validation.constraints.NotNull)3 IridaSequenceFile (ca.corefacility.bioinformatics.irida.model.irida.IridaSequenceFile)2 IridaSequenceFilePair (ca.corefacility.bioinformatics.irida.model.irida.IridaSequenceFilePair)2 JsonIgnore (com.fasterxml.jackson.annotation.JsonIgnore)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Lists (com.google.common.collect.Lists)2 AbstractNumericProperty (io.swagger.models.properties.AbstractNumericProperty)2 ArrayProperty (io.swagger.models.properties.ArrayProperty)2 StringProperty (io.swagger.models.properties.StringProperty)2 BigDecimal (java.math.BigDecimal)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2