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

import javax.ejb.ApplicationException;

import de.ivu.elect.jee.exchange.AbstractViolation;
import de.ivu.elect.jee.exchange.Violations;

// HINT: rollback=true führt zum Transaktionsabbruch
@ApplicationException(rollback = true)
public class ViolationsVorhandenException extends Exception {

  private static final long serialVersionUID = 1L;
  private final Violations violations;

  public ViolationsVorhandenException(Violations violations) {
    super("Es sind Violations vorhanden! [" + listViolationsAsString(violations) + "]");
    this.violations = violations;
  }

  private static String listViolationsAsString(Violations violations) {
    StringBuilder sb = new StringBuilder();

    for (AbstractViolation violation : violations.getViolations()) {
      if (sb.length() > 0) {
        sb.append(", ");
      }

      sb.append(violation.getMessageKey());
    }

    return sb.toString();
  }

  public Violations getViolations() {
    return violations;
  }

}
