Search in sources :

Example 1 with Level

use of org.mongodb.morphia.mapping.validation.ConstraintViolation.Level in project morphia by mongodb.

the class MappingValidator method validate.

/**
     * Validates a List of MappedClasses
     *
     * @param classes the MappedClasses to validate
     * @param mapper the Mapper to use for validation
     */
public void validate(final Mapper mapper, final List<MappedClass> classes) {
    final Set<ConstraintViolation> ve = new TreeSet<ConstraintViolation>(new Comparator<ConstraintViolation>() {

        @Override
        public int compare(final ConstraintViolation o1, final ConstraintViolation o2) {
            return o1.getLevel().ordinal() > o2.getLevel().ordinal() ? -1 : 1;
        }
    });
    final List<ClassConstraint> rules = getConstraints();
    for (final MappedClass c : classes) {
        for (final ClassConstraint v : rules) {
            v.check(mapper, c, ve);
        }
    }
    if (!ve.isEmpty()) {
        final ConstraintViolation worst = ve.iterator().next();
        final Level maxLevel = worst.getLevel();
        if (maxLevel.ordinal() >= Level.FATAL.ordinal()) {
            throw new ConstraintViolationException(ve);
        }
        // sort by class to make it more readable
        final List<LogLine> l = new ArrayList<LogLine>();
        for (final ConstraintViolation v : ve) {
            l.add(new LogLine(v));
        }
        sort(l);
        for (final LogLine line : l) {
            line.log(LOG);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) MappedClass(org.mongodb.morphia.mapping.MappedClass) TreeSet(java.util.TreeSet) Level(org.mongodb.morphia.mapping.validation.ConstraintViolation.Level)

Aggregations

ArrayList (java.util.ArrayList)1 TreeSet (java.util.TreeSet)1 MappedClass (org.mongodb.morphia.mapping.MappedClass)1 Level (org.mongodb.morphia.mapping.validation.ConstraintViolation.Level)1