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

import java.io.Serializable;
import java.math.BigDecimal;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

import de.ivu.elect.jee.faces.FacesMessageUtil;

public class BigDecimalValidator implements Validator, Serializable {
  private static final long serialVersionUID = 1L;

  @Override
  public void validate(FacesContext facesContext, UIComponent uiComponent, Object o) throws ValidatorException {

    if (o != null) {
      String value = o.toString().trim();
      try {
        new BigDecimal(value);
      } catch (Exception e) {
        throw new ValidatorException(FacesMessageUtil
            .createErrorMessage(facesContext, "bigdecimal.validation.failed", "bigdecimal.validation.failed"));
      }
    }
  }

}
