
package de.ivu.elect.jee.faces;

import de.ivu.elect.jee.entity.DAO;
import de.ivu.elect.jee.entity.Entity;
import de.ivu.elect.jee.entity.EntityLongPK;
import de.ivu.elect.jee.entity.tk.EntityUK;

public abstract class AbstractEntityUniqueUKFacesValidator<E extends Entity<PK, UK>, PK extends EntityLongPK, UK extends EntityUK>
    extends
      AbstractEntityFacesValidator<PK> {

  private static final long serialVersionUID = 1L;

  protected abstract UK createUK(Object value);

  protected abstract DAO<E, PK, UK> getDao();

  @Override
  protected boolean isValid(PK pk, Object value) {
    UK uk = createUK(value);
    return isValid(pk, uk);

  }

  public boolean isValid(PK pk, UK uk) {
    if (pk == null) {
      return !getDao().isEntityWithUKPresent(uk);
    }

    return getDao().isEntityWithUKHasPK(uk, pk);
  }

  public boolean isValid(E entity) {
    return isValid(entity.getPk(), entity.getUk());
  }
}
