package org.minimalj.model.validation;

import java.util.ArrayList;
import java.util.List;

import org.minimalj.model.Keys;
import org.minimalj.model.properties.PropertyInterface;

/**
 * The text of this message should already be localized. You get the current Locale
 * from the LocaleContext class.
 *
 */
public class ValidationMessage {

	private String formattedText;
	private Object key;

	public ValidationMessage(Object key, String formattedText) {
		this.key = key;
		this.formattedText = formattedText;
	}

	public String getFormattedText() {
		return formattedText;
	}

	public Object getKey() {
		return key;
	}

	public PropertyInterface getProperty() {
		if (key instanceof PropertyInterface) {
			return (PropertyInterface) key;
		} else {
			return Keys.getProperty(key);
		}
	}
	
	@Override
	public String toString() {
		return "ValidationMessage [key=" + key + ", formattedText=" + formattedText + "]";
	}

	public static List<String> filterValidationMessage(List<ValidationMessage> validationMessages, PropertyInterface property) {
		List<String> filteredMessages = new ArrayList<String>();
		if (validationMessages != null) {
			for (ValidationMessage validationMessage : validationMessages) {
				if (validationMessage.getProperty().equals(property)) {
					filteredMessages.add(validationMessage.getFormattedText());
				}
			}
		}
		return filteredMessages;
	}

	
	public static String formatHtml(List<ValidationMessage> validationMessages) {
		if (validationMessages != null && validationMessages.size() > 0) {
			StringBuilder s = new StringBuilder();
			s.append("<html>");
			for (int i = 0; i<validationMessages.size(); i++) {
				s.append(validationMessages.get(i).formattedText);
				if (i < validationMessages.size() - 1) {
					s.append("<br>");
				}
			}
			s.append("</html>");
			return s.toString();
		} else {
			return null;
		}
	}

}

@@ -172,16 +172,54 @@
 terface;
+%0Aimport org.minimalj.util.StringUtils;
 %0A%0A/**%0A *
@@ -1250,24 +1250,42 @@
 ) %7B%0A%09%09%09%09if (
+isEqual(property, 
 validationMe
@@ -1307,24 +1307,8 @@
 ty()
-.equals(property
 )) %7B
@@ -1419,16 +1419,187 @@
 es;%0A%09%7D%0A%0A
+%09public static boolean isEqual(PropertyInterface p1, PropertyInterface p2) %7B%0A%09%09return p1.getClazz() == p2.getClazz() && StringUtils.equals(p1.getPath(), p2.getPath());%0A%09%7D%0A
 %09%0A%09publi

handle properties