/*
 * $Id$
 * $URL$
 */

package org.subethamail.web.model;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;

/**
 * Models with a simple error map.
 *
 * @author Jeff Schnitzer
 */
public class ErrorMapModel
{
	/** */
	private static Validator validator = Validation.buildDefaultValidatorFactory().getValidator();


	/** */
	Map<String, String> errors;

	/** */
	@SuppressWarnings("unchecked")
	public Map<String, String> getErrors()
	{
		if (this.errors == null)
			return Collections.EMPTY_MAP;
		else
			return this.errors;
	}

	/** */
	public void setError(String key, String msg)
	{
		if (this.errors == null)
			this.errors = new HashMap<String, String>();

		this.errors.put(key, msg);
	}

	/**
	 * Reflect any public fields that have been annotated with contraints
	 * and modify the error map accordingly.
	 */
	public void validate() throws IllegalAccessException
	{
		for (ConstraintViolation<?> invalid: validator.validate(this))
		{
			String simplePath = invalid.getPropertyPath().iterator().next().getName();
			
			Object existingError = this.getErrors().get(simplePath);
			if (existingError == null)
				this.setError(simplePath, invalid.getMessage());
			else
				this.setError(simplePath, existingError + "\n" + invalid.getMessage());
		}
	}
}

@@ -262,16 +262,59 @@
 idator;%0D
+%0Aimport javax.validation.ValidatorFactory;%0D
 %0A%0D%0A/**%0D%0A
@@ -460,16 +460,57 @@
 alidator
+;%0D%0A%09static %0D%0A%09%7B%0D%0A%09    ValidatorFactory vf
  = Valid
@@ -545,16 +545,36 @@
 actory()
+;%0D%0A%09    validator=vf
 .getVali
@@ -581,16 +581,23 @@
 dator();
+   %0D%0A%09%7D
 %0D%0A%0D%0A%0D%0A%09/

break into multiple lines for stack trace purposes