package de.ivu.elect.exchange.wahl;

import java.io.OutputStream;

import javax.inject.Inject;
import javax.xml.bind.JAXBException;

import de.ivu.elect.business.datei.entity.FileType;
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.wahl.entity.WahlKonfiguration;
import de.ivu.elect.business.wahl.entity.WahlKonfigurationExchangeContext;
import de.ivu.elect.jee.exchange.XmlMarshaller;

/**
 *  on 01.07.2015.
 */
public class DefaultXMLWahlKonfigurationExportMapper implements WahlKonfigurationExportMapper {

  @Inject
  protected transient XmlMarshaller<de.ivu.elect.interfaces.core.konfiguration.WahlKonfiguration> xmlMarshaller;

  @Inject
  private transient WahlKonfigurationMapper wahlkonfigurationMapper;

  @Override
  public void serialize(WahlKonfiguration wahlKonfiguration, OutputStream os, WahlKonfigurationExchangeContext noContext, FileKonfiguration fileKonfiguration) throws MappingException {
    try {
      xmlMarshaller.serialize(wahlkonfigurationMapper.mappe(wahlKonfiguration), os);
    } catch (JAXBException e) {
      throw new MappingException(e.getMessage(), e);
    }
  }

  @Override
  public FileFormat getFileFormat() {
    return new FileFormat(DefaultXMLWahlKonfigurationImportMapper.FILE_FORMAT, FileType.XML);
  }

  @Override
  public boolean isAcceptContext(WahlKonfigurationExchangeContext noContext) {
    return true;
  }
}
