package org.minimalj.model.validation;

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

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

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<ValidationMessage> filterValidationMessage(List<ValidationMessage> validationMessages, PropertyInterface property) {
//		List<ValidationMessage> filteredMessages = Collections.emptyList();
//		if (validationMessages != null) {
//			for (ValidationMessage validationMessage : validationMessages) {
//				if (validationMessage.getProperty().equals(property)) {
//					if (filteredMessages.isEmpty()) {
//						filteredMessages = new ArrayList<ValidationMessage>();
//					}
//					filteredMessages.add(validationMessage);
//				}
//			}
//		}
//		return filteredMessages;
//	}

	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;
		}
	}

}

@@ -793,607 +793,8 @@
 ;%0A%09%7D
-%0A%09%0A//%09public static List%3CValidationMessage%3E filterValidationMessage(List%3CValidationMessage%3E validationMessages, PropertyInterface property) %7B%0A//%09%09List%3CValidationMessage%3E filteredMessages = Collections.emptyList();%0A//%09%09if (validationMessages != null) %7B%0A//%09%09%09for (ValidationMessage validationMessage : validationMessages) %7B%0A//%09%09%09%09if (validationMessage.getProperty().equals(property)) %7B%0A//%09%09%09%09%09if (filteredMessages.isEmpty()) %7B%0A//%09%09%09%09%09%09filteredMessages = new ArrayList%3CValidationMessage%3E();%0A//%09%09%09%09%09%7D%0A//%09%09%09%09%09filteredMessages.add(validationMessage);%0A//%09%09%09%09%7D%0A//%09%09%09%7D%0A//%09%09%7D%0A//%09%09return filteredMessages;%0A//%09%7D
 %0A%0A%09p

remove dead code