package net.sf.jasperreports.engine.design;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import net.sf.jasperreports.engine.JRException;

public class JRValidationException extends JRException {
  private static final long serialVersionUID = 10200L;
  
  private final Collection faults;
  
  public JRValidationException(String message, Object source) {
    this(createFault(message, source));
  }
  
  private static JRValidationFault createFault(String message, Object source) {
    JRValidationFault fault = new JRValidationFault();
    fault.setMessage(message);
    fault.setSource(source);
    return fault;
  }
  
  public JRValidationException(JRValidationFault fault) {
    this(Arrays.asList(new JRValidationFault[] { fault }));
  }
  
  public JRValidationException(Collection faults) {
    super(appendMessages(faults));
    this.faults = faults;
  }
  
  public Collection getFaults() {
    return this.faults;
  }
  
  protected static String appendMessages(Collection faults) {
    StringBuffer sbuffer = new StringBuffer();
    sbuffer.append("Report design not valid : ");
    int i = 1;
    for (Iterator it = faults.iterator(); it.hasNext(); i++) {
      JRValidationFault fault = it.next();
      sbuffer.append("\n\t " + i + ". " + fault.getMessage());
    } 
    return sbuffer.toString();
  }
}
