
package de.ivu.elect.jee.exchange;

import java.io.Serializable;

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

public abstract class AbstractViolation implements Serializable {

  private static final long serialVersionUID = 1L;

  private final ViolationLevel violationLevel;

  public AbstractViolation(ViolationLevel violationLevel) {
    super();

    this.violationLevel = violationLevel;
  }

  public abstract String getMessageKey();

  public abstract Object[] getMessageParameters(Messages messages);

  protected abstract String createDefaultMessage();

  @Override
  public String toString() {
    return "Violation: Level: " + getViolationLevel() + ", message: " + createDefaultMessage();
  }

  public ViolationLevel getViolationLevel() {
    return violationLevel;
  }

  public void appendMessage(Messages messages) {
    if (ViolationLevel.ERROR.equals(violationLevel)) {
      appendErrorMessage(messages);
    } else {
      appendWarnMessage(messages);
    }
  }

  public void appendWarnMessage(Messages messages) {
    messages.addWarnMessage(getMessageKey(), getMessageParameters(messages));
  }

  public void appendErrorMessage(Messages messages) {
    messages.addErrorMessage(getMessageKey(), getMessageParameters(messages));
  }

  public String getMessage(Messages messages) {
    return messages.getMessageForMessageKey(getMessageKey(), getMessageParameters(messages));
  }
}
