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

import javax.enterprise.inject.Specializes;
import javax.inject.Inject;

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.exception.SystemExceptionWriter;
import de.ivu.elect.presentation.mandant.MandantSessionSelect;

@Specializes
public class ElectSystemExceptionWriter extends SystemExceptionWriter {
  private static final long serialVersionUID = 1L;

  @Inject
  private transient SystemLogWriter systemLogWriter;

  @Inject
  private transient MandantSessionSelect mandantSessionSelect;

  @Override
  public void writeSystemException(String message, Throwable e) {
    try {
      Mandant mandantOrNull = mandantSessionSelect.getMandantOrNull();
      if (mandantOrNull != null && !mandantOrNull.getArt().isSystemMandant()) {
        systemLogWriter.writeErrorLogMessageFuerMandant(mandantOrNull, message, e, SystemLogQuelle.SYSTEM);
      }
    } catch (Exception ex) {
      // Ignore
    }
    systemLogWriter.writeErrorLogMessageFuerSystemMandant(message, e, SystemLogQuelle.SYSTEM);
  }
}
