
package de.ivu.elect.business.wahlvorschlag.control;

import java.io.Serializable;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.NoResultException;

import de.ivu.elect.business.wahlvorschlag.boundary.ReferendumOptionDAO;
import de.ivu.elect.business.wahlvorschlag.entity.ReferendumOption;
import de.ivu.elect.business.wahlvorschlag.entity.ReferendumOptionUK;
import de.ivu.elect.business.wahlvorschlag.entity.WahlvorschlagPK;

@Stateless
public class UniqueReferendumOptionUkValidator implements Serializable {

  private static final long serialVersionUID = 1L;

  @Inject
  private ReferendumOptionDAO referendumOptionDAO;

  public UniqueReferendumOptionUkValidator() {
    super();
  }

  public boolean isUniqueReferendumOptionUk(ReferendumOption entity) {
    return isUniqueReferendumOptionUk(entity.getPk(), entity.getReferendumOptionUK());
  }

  public boolean isUniqueReferendumOptionUk(WahlvorschlagPK pk, ReferendumOptionUK uk) {
    try {
      return getReferendumOptionByUk(uk).getPk().equals(pk);
    } catch (NoResultException ignored) {
      // Do nothing - be happy.
    }

    return true;
  }

  protected ReferendumOption getReferendumOptionByUk(ReferendumOptionUK uk) {
    return referendumOptionDAO.findByUK(uk);
  }
}
