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

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;

import javax.ejb.ConcurrencyManagement;
import javax.ejb.ConcurrencyManagementType;
import javax.ejb.DependsOn;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.inject.Inject;

import de.ivu.elect.business.system.control.AbstractElectBackgroundService;
import de.ivu.elect.business.system.entity.SystemLogQuelle;

@Startup
@Singleton
@DependsOn("DbInit")
// https://issues.jboss.org/browse/EJBTHREE-2070 (keine Transaktion)
@TransactionManagement(TransactionManagementType.BEAN)
@ConcurrencyManagement(ConcurrencyManagementType.BEAN)
public class PersonenPruefBackgroundService extends AbstractElectBackgroundService {

  @Inject
  private transient PersonenPruefer personenPruefer;

  public static final AtomicBoolean DISABLED = createDisabled();

  public static final AtomicLong RUNNING = createRunning();

  public PersonenPruefBackgroundService() {
    super();
  }

  @Override
  protected AtomicBoolean getDisabledInternal() {
    return DISABLED;
  }

  @Override
  protected boolean isSomethingToDoInternal() {
    return personenPruefer.isZuPruefendePersonenVorhanden();
  }

  @Override
  protected void doSomethingInternal() {
    personenPruefer.pruefe();
  }

  @Override
  public SystemLogQuelle getSystemLogQuelle() {
    return SystemLogQuelle.PERSONENPRUEFUNG;
  }

  @Override
  protected AtomicLong getRunning() {
    return RUNNING;
  }

  public static boolean isPersonenpruefungWirdAusgefuehrt() {
    return RUNNING.get() > 0;
  }
}
