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

import java.util.Collections;
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.performance.PerformanceNoLog;

public abstract class AbstractImporterExporter<Context> {

  @Inject
  private transient Messages messages;

  @Inject
  private transient CsvDateiDAO csvDateiDAO;

  protected AbstractImporterExporter() {
    super();
  }

  @PerformanceNoLog
  public List<FileFormat> getSupportedFileFormats(Context context) {
    Exchange<Context> exchange = getExchange();
    if (exchange == null) {
      return Collections.emptyList();
    }
    return exchange.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;
  }
}
