
package de.ivu.elect.exchange.wahl.xml;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

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.jee.exchange.Violations;
import de.ivu.elect.business.wahl.control.WahlParameterExchangeContext;
import de.ivu.elect.business.wahl.entity.WahlParameter;
import de.ivu.elect.exchange.konfiguration.xml.DefaultExchangeKonfigurationXml;
import de.ivu.elect.exchange.konfiguration.xml.DefaultExchangeXmlKonfigurationListe;
import de.ivu.elect.exchange.wahl.AbstractDefaultWahlParameterMapper;
import de.ivu.elect.jee.exchange.XmlMarshaller;

public class DefaultXmlWahlParameterMapper extends AbstractDefaultWahlParameterMapper<DefaultExchangeKonfigurationXml> {

  @Inject
  protected XmlMarshaller<DefaultExchangeXmlKonfigurationListe> xmlMarshaller;

  @Override
  public void serialize(List<WahlParameter> object, OutputStream os, WahlParameterExchangeContext context,
                        FileKonfiguration fileKonfiguration) throws MappingException {
    try {
      xmlMarshaller.serialize(mappeEntityInternToExtern(object), os);
    } catch (JAXBException e) {
      throw new MappingException(e.getMessage(), e);
    }
  }

  @Override
  public List<WahlParameter> deserialize(InputStream is, WahlParameterExchangeContext context, FileKonfiguration fileKonfiguration)
      throws MappingException {
    try {
      return mappeEntityExternToIntern(xmlMarshaller.deserialize(DefaultExchangeXmlKonfigurationListe.class, is));
    } catch (JAXBException e) {
      throw new MappingException(e.getMessage(), e);
    }
  }

  @Override
  public Violations validateInputFormat(InputStream is, WahlParameterExchangeContext context, FileKonfiguration fileKonfiguration) {
    return new Violations();
  }

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

  @Override
  protected DefaultExchangeKonfigurationXml createEntity() {
    return new DefaultExchangeKonfigurationXml();
  }

  protected DefaultExchangeXmlKonfigurationListe mappeEntityInternToExtern(List<WahlParameter> entityListe) {
    DefaultExchangeXmlKonfigurationListe result = new DefaultExchangeXmlKonfigurationListe();

    for (WahlParameter entity : entityListe) {
      result.getKonfigurationen().add(mappeEntity(entity));
    }

    return result;
  }

  protected List<WahlParameter> mappeEntityExternToIntern(DefaultExchangeXmlKonfigurationListe entityListe) {
    List<WahlParameter> mappedEntities = new ArrayList<>(entityListe.getKonfigurationen().size());

    for (DefaultExchangeKonfigurationXml entity : entityListe.getKonfigurationen()) {
      mappedEntities.add(mappeEntity(entity));
    }

    return mappedEntities;
  }

}
