
package de.ivu.elect.jee.presentation.dynamicform;

import javax.faces.convert.Converter;

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

public abstract class AbstractStringProperty extends AbstractProperty implements PropertyHavingMinAndMaxLength {

  private Integer minLength;

  private Integer maxLength;

  public AbstractStringProperty() {
    super();
  }

  @Override
  protected boolean mustBeAccessableOnPageLoadByValue() {
    if (super.mustBeAccessableOnPageLoadByValue()) {
      return true;
    }

    return notAllEqual(getDefaultValue(), getValue());
  }

  @Override
  public PropertyType getDefaultType() {
    return PropertyType.INPUT_TEXT;
  }

  @Override
  public Converter getDefaultConverter() {
    return new StringEmptyToNullConverter();
  }

  @Override
  protected boolean hasDefaultValue() {
    return notNullOrBlank(getDefaultValue());
  }

  public String getDefaultValue() {
    return null;
  }

  @Override
  public boolean hasValue() {
    return notNullOrBlank(getValue());
  }

  public abstract String getValue();

  public abstract void setValue(String value);

  public int getDefaultMinLength() {
    return 0;
  }

  @Override
  public int getMinLength() {
    if (minLength == null) {
      return getDefaultMinLength();
    }

    return minLength;
  }

  @Override
  public void setMinLength(int minLength) {
    this.minLength = minLength;
  }

  public abstract int getDefaultMaxLength();

  @Override
  public int getMaxLength() {
    if (maxLength == null) {
      return getDefaultMaxLength();
    }

    return maxLength;
  }

  @Override
  public void setMaxLength(int maxLength) {
    this.maxLength = maxLength;
  }
}
