
package de.ivu.elect.presentation.exchange;

import de.ivu.elect.business.datei.entity.FileType;
import de.ivu.elect.business.exchange.control.FileFormat;
import org.apache.commons.lang3.StringUtils;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

public abstract class AbstractFileFormatConverter implements Converter {

  private static final String TRENNZEICHEN = ">";

  @Override
  public Object getAsObject(FacesContext arg0, UIComponent arg1, String string) {

    if (StringUtils.isBlank(string)) {
      return null;
    }
    String[] splitted = string.split(TRENNZEICHEN);
    if (splitted.length < 2) {
      return null;
    }
    return new FileFormat(splitted[1], FileType.valueOf(splitted[0]));
  }

  @Override
  public String getAsString(FacesContext arg0, UIComponent arg1, Object object) {

    if (object == null) {
      return null;
    }
    FileFormat fileFormat = (FileFormat) object;

    return fileFormat.getFileType().name() + TRENNZEICHEN + fileFormat.getName();
  }

}
