package de.ivu.elect.jee.database;

import javax.annotation.Nullable;
import javax.annotation.PostConstruct;
import javax.ejb.DependsOn;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.ivu.elect.jee.cron.AbstractSingleCronTimer;

@Startup
@Singleton
@DependsOn("StartupSingleton")
public class DatabaseStatistikCronTimer extends AbstractSingleCronTimer {

  private static final Logger LOG = LoggerFactory.getLogger(DatabaseStatistikCronTimer.class);

  @Inject
  private transient DatabaseStatistikKonfiguration konfiguration;

  @Inject
  private transient DatabaseStatistik databaseStatistik;

  @PostConstruct
  public void startup() {
    createTimer();
  }

  @Override
  protected void postCreateTimer() {
    super.postCreateTimer();
    databaseStatistik.speichereDaten();
  }

  @Nullable
  @Override
  protected String getCrontab() {
    return konfiguration.getCrontab();
  }

  @Override
  protected void timeout() {
    DatabaseStatistikData data = databaseStatistik.speichereDaten();
    if (konfiguration.isLoggeAusgaben()) {
      LOG.info("DatabaseStatistikTimeout: " + data);
    }
  }

  @Override
  public String getJobName() {
    return konfiguration.getJobName();
  }

  @Override
  protected boolean isLoggeHandleTimeout() {
    return false;
  }
}
