
package de.ivu.elect.exchange;

import java.io.OutputStream;

import javax.inject.Inject;

import de.ivu.elect.business.datei.entity.DateiFileNames;
import de.ivu.elect.business.exchange.control.ExportExchange;
import de.ivu.elect.business.exchange.control.FileFormat;
import de.ivu.elect.business.exchange.control.FileKonfiguration;
import de.ivu.elect.business.exchange.control.MappingException;
import de.ivu.elect.business.exchange.entity.DateiExportArt;
import de.ivu.elect.business.exchange.entity.DateiExportArtFileNames;
import de.ivu.elect.jee.faces.Messages;

public abstract class AbstractExportExchange<T, C> extends AbstractExchange<C, ExportMapper<T, C>> implements ExportExchange<T, C> {

  @Inject
  private transient Messages messages;

  public AbstractExportExchange() {
    super();
  }

  /**
   * Export von Daten in eine Datei durch Auswahl des geeigneten {@link ExportMapper}s und Aufruf von {@link ExportMapper#serialize()}.
   *
   * @throws MappingException
   *
   * @see de.ivu.elect.business.exchange.control.ExportExchange#export(java.lang.Object, java.io.OutputStream, de.ivu.elect.business.datei.entity.FileType, java.lang.Object)
   */
  @Override
  public void export(T object, OutputStream os, FileKonfiguration fileKonfiguration, C context) throws MappingException {
    getMapperForFileTypeAndContext(fileKonfiguration, context).serialize(object, os, context, fileKonfiguration);
  }

  public DateiFileNames getDateiFileNames(DateiExportArt dateiExportArt, FileFormat fileFormat, C context) {
    ExportMapper<T, C> exportMapper = getMapperForFileTypeAndContext(fileFormat, context);

    if (exportMapper instanceof ExportMapperProvidingDateiFileNames) {
      return ((ExportMapperProvidingDateiFileNames<T, C>) exportMapper).getDateiExportFileNames(dateiExportArt, context);
    }

    return new DateiExportArtFileNames(messages, dateiExportArt);
  }
}
