
package de.ivu.elect.jee.exchange;

import static java.util.Arrays.stream;

import java.io.Serializable;

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

public class Violation extends AbstractViolation implements Serializable {

  private static final long serialVersionUID = 1L;

  public static final String PARAMETER_NULL = "?"; // ELECTVAPP-1275

  private final String messageKey;

  private final Object[] messageParameters;

  public Violation(ViolationLevel violationLevel, String messageKey, Object... messageParameters) {
    super(violationLevel);

    this.messageKey = messageKey;
    this.messageParameters = messageParameters == null ? new Object[0] : stream(messageParameters).map(param -> param == null ? PARAMETER_NULL : param).toArray();
  }

  @Override
  public String getMessageKey() {
    return messageKey;
  }

  @Override
  public Object[] getMessageParameters(Messages messages) {
    return messageParameters;
  }

  @Override
  protected String createDefaultMessage() {
    return ElectCommonException.createDefaultMessage(messageKey, messageParameters);
  }

  @Override
  public String toString() {
    return createDefaultMessage();
  }
}
