All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.code.morphia.validation.ValidationExtension Maven / Gradle / Ivy

There is a newer version: 0.104
Show newest version
package com.google.code.morphia.validation;

import java.util.Set;

import javax.validation.Configuration;
import javax.validation.Validation;
import javax.validation.ValidatorFactory;

import com.google.code.morphia.AbstractEntityInterceptor;
import com.google.code.morphia.Morphia;
import com.google.code.morphia.mapping.Mapper;
import com.mongodb.DBObject;

/**
 * @author [email protected]
 */
public class ValidationExtension extends AbstractEntityInterceptor
{
    private ValidatorFactory validationFactory;
    private Mapper mapper;

    @Deprecated
    public ValidationExtension()
    {
        // use the new ValidationExtension(morphia) convention
    }

    public ValidationExtension(final Morphia m)
    {
        final Configuration configuration = Validation.byDefaultProvider().configure();
        this.validationFactory = configuration.buildValidatorFactory();

        m.getMapper().addInterceptor(this);
    }

    @Override
    public void prePersist(final Object ent, final DBObject dbObj, final Mapper mapper)
    {
        final Set validate = this.validationFactory.getValidator().validate(ent);
        if (!validate.isEmpty())
        {
            throw new VerboseJSR303ConstraintViolationException(validate);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy