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

import java.io.Serializable;

import javax.annotation.Nullable;
import javax.inject.Inject;

import org.apache.commons.lang3.StringUtils;

import de.ivu.elect.business.konfiguration.entity.ElectKonfigurationWerte;
import de.ivu.elect.business.konfiguration.entity.KonfigurationLoader;
import de.ivu.elect.business.mandant.boundary.MandantDAO;
import de.ivu.elect.business.mandant.entity.AbstractMandant;
import de.ivu.elect.business.mandant.entity.Mandant;
import de.ivu.elect.business.system.boundary.SystemLogWriter;
import de.ivu.elect.business.system.entity.SystemLogQuelle;
import de.ivu.elect.jee.cron.AbstractSingleCronTimer;

/**
 * Abgeleitete Klasse mit @Startup und @Singleton und @DependsOn("StartupSingleton") annotieren
 */
@Deprecated // Für neue Timer AbstractElectMultipleCronTimer nutzen, bietet mehr Möglichkeiten...
public abstract class AbstractElectCronTimer extends AbstractSingleCronTimer {

  @Inject
  private transient KonfigurationLoader konfigurationLoader;

  @Inject
  private transient SystemLogWriter systemLogWriter;

  public AbstractElectCronTimer() {
    super();
  }

  @Override
  @Nullable
  protected String getCrontab() {
    return konfigurationLoader.resolveStringValue(getElectKonfigurationWerte());
  }

  protected abstract ElectKonfigurationWerte getElectKonfigurationWerte();

  @Override
  protected void logError(String jobName, String messageKey, Serializable payload, Exception e) {
    super.logError(jobName, messageKey, payload, e);
    String messageForMessageKey = messages.getMessageForMessageKey(messageKey, payload, e.getLocalizedMessage());
    if (StringUtils.isBlank(messageForMessageKey)) {
      systemLogWriter.writeErrorLogMessageFuerSystemMandant("Key: " + messageKey + ", payload: " + payload + ", exception: " + e.getLocalizedMessage(), SystemLogQuelle.SYSTEM);
    } else {
      systemLogWriter.writeErrorLogMessageFuerSystemMandant(messageForMessageKey, SystemLogQuelle.SYSTEM);
    }
  }


}
