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

import java.io.File;
import java.io.Serializable;

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

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.ivu.elect.business.konfiguration.entity.KonfigurationLoader;


import static de.ivu.elect.business.konfiguration.entity.ElectKonfigurationWerte.ELECT_COMMON_TEMP_VERZEICHNIS_PFAD;

/**
 *  on 11.11.2016.
 */
@Stateless
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public class TempDir implements Serializable {

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

  @Inject
  private transient KonfigurationLoader konfigurationLoader;

  public File getTempDir() {
    String pathname = konfigurationLoader.resolveStringValue(ELECT_COMMON_TEMP_VERZEICHNIS_PFAD);
    if (StringUtils.isBlank(pathname) || !new File(pathname).isDirectory()) {
      String property = System.getProperty("java.io.tmpdir");
      LOG.warn("Temp nicht konfiguriert oder kein Verzeichnis: '" + pathname + "' - fallback: '" + property + "'");
      return new File(property);
    }
    return new File(pathname);
  }
}
