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

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

import de.ivu.elect.business.wahlvorschlag.control.NonUniqueWahlvorschlagUkException;
import de.ivu.elect.business.wahlvorschlag.control.UniqueReferendumOptionUkValidator;
import de.ivu.elect.business.wahlvorschlag.entity.ReferendumOption;
import de.ivu.elect.jee.faces.Messages;

@Stateless
public class ReferendumOptionEdit extends AbstractReferendumOptionEdit {

  @Inject
  private transient UniqueReferendumOptionUkValidator uniqueReferendumOptionUkValidator;
  @Inject
  private transient Messages messages;

  public ReferendumOptionEdit() {
    super();
  }

  @Override
  protected boolean usesMixedTransactionalContexts() {
    // NOTE ELECT-945, bspw. der UniqueWahlvorschlagUkValidator!
    return true;
  }

  @Override
  public void save(ReferendumOption referendumOption) {
    assertUniqueWahlvorschlagUk(referendumOption);

    super.save(referendumOption);
  }

  private void assertUniqueWahlvorschlagUk(ReferendumOption referendumOption) {
    if (!isUniqueWahlvorschlagUk(referendumOption)) {
      throw new NonUniqueWahlvorschlagUkException(referendumOption.getWahlvorschlagUK(), messages);
    }
  }

  private boolean isUniqueWahlvorschlagUk(ReferendumOption referendumOption) {
    return uniqueReferendumOptionUkValidator.isUniqueReferendumOptionUk(referendumOption);
  }
}
