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

import java.util.List;

import javax.inject.Inject;

import de.ivu.elect.business.datei.boundary.CsvDateiDAO;
import de.ivu.elect.business.datei.entity.Datei;
import de.ivu.elect.jee.faces.Messages;
import de.ivu.elect.jee.faces.UserLocale;
import de.ivu.elect.jee.performance.PerformanceNoLog;

public abstract class AbstractImporterExporter<Context> {

  @Inject
  private transient Messages messages;

  @Inject
  private transient UserLocale userLocale;

  @Inject
  private transient CsvDateiDAO csvDateiDAO;

  public AbstractImporterExporter() {
    super();
  }

  @PerformanceNoLog
  public List<FileFormat> getSupportedFileFormats(Context context) {
    return getExchange().getSupportedFileFormats(context);
  }

  protected abstract Exchange<Context> getExchange();

  protected FileKonfiguration createFileKonfiguration(FileFormat fileFormat, Datei datei) {
    DefaultFileKonfiguration result;

    if (datei.isCsvDatei()) {
      result = new DefaultCsvFileKonfiguration(csvDateiDAO.findByPrimaryKey(datei.getPk()), fileFormat);
    } else {
      result = new DefaultFileKonfiguration(fileFormat);
    }

    if (datei.getOriginalDateiname() == null) {
      result.setFileName(datei.getDateiname());
    } else {
      result.setFileName(datei.getOriginalDateiname());
    }

    return result;
  }

  protected String getMessage(Enum< ? > enumValue) {
    return getMessages().lokalisiere(enumValue);
  }

  protected String getMessage(String msgKey, Object... params) {
    return getMessages().getMessageForMessageKey(msgKey, params);
  }

  protected Messages getMessages() {
    return messages;
  }

  protected UserLocale getUserLocale() {
    return userLocale;
  }
}
