
package de.ivu.elect.jee.entity;

import java.io.Serializable;
import java.util.Set;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.ValidatorFactory;

@ApplicationScoped
public class EntityValidation implements Serializable {

  private transient ValidatorFactory factory;

  @Inject
  private transient EntityValidationRequestScope entityValidationRequestScope;

  @Deprecated // könnte insgesamt zu lahm sein validateEntity verwenden
  public Set<ConstraintViolation<Entity>> validate(Entity entity) {
    return entityValidationRequestScope.getValidator(getFactory()).validate(entity);
  }


  public Set<EntityConstraintViolation> validateEntity(Entity entity) {
    return entity.validateEntity();
  }

  private ValidatorFactory getFactory() {
    if (factory == null) {
      factory = Validation.buildDefaultValidatorFactory();
    }
    return factory;
  }
}

