All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.wicket.bean.validation.DefaultViolationTranslator Maven / Gradle / Ivy

There is a newer version: 10.1.0
Show newest version
package org.apache.wicket.bean.validation;

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

import javax.validation.ConstraintViolation;
import javax.validation.metadata.ConstraintDescriptor;

import org.apache.wicket.util.string.Strings;
import org.apache.wicket.validation.ValidationError;

/**
 * A default implementation of {@link IViolationTranslator}. The validation error is created with
 * the constraint violation's default error message. Further, the violation is checked for a message
 * key and if is found it is also added as a message key to the validation error. The keys are only
 * used if they are in the bean validation's default format of '{key}'.
 * 
 * @author igor
 */
public class DefaultViolationTranslator implements IViolationTranslator
{
	@Override
	public  ValidationError convert(ConstraintViolation violation)
	{
		ConstraintDescriptor desc = violation.getConstraintDescriptor();

		ValidationError error = new ValidationError();
		error.setMessage(violation.getMessage());

		List messages = getViolationMessages(violation, desc);
		addErrorKeys(error, violation.getInvalidValue(), messages);

		for (String key : desc.getAttributes().keySet())
		{
			error.setVariable(key, desc.getAttributes().get(key));
		}
		
		return error;
	}

	private List getViolationMessages(ConstraintViolation violation,
		ConstraintDescriptor desc)
	{
		String defaultMessage = (String)desc.getAttributes().get("message");
		String violationMessage = violation.getMessage();
		String violationMessageTemplate = violation.getMessageTemplate();		
		List messages = new ArrayList();

		//violation message is considered only if it is different from
		//the interpolated message
		if (!Strings.isEqual(violationMessage, violationMessageTemplate))
		{
			messages.add(violationMessageTemplate);
		}
		
		messages.add(violationMessage);
		
		//the default message is considered only if it is different from
		//the violation message template
		if (!Strings.isEqual(defaultMessage, violationMessageTemplate))
		{
			messages.add(defaultMessage);
		}

		return messages;
	}

	private void addErrorKeys(ValidationError error, Object invalidValue, List messages)
	{
		for (String message : messages)
		{
			String messageKey = getMessageKey(message);

			if (messageKey != null)
			{
				if (invalidValue != null)
				{
					error.addKey(messageKey + "." + invalidValue.getClass().getSimpleName());
				}

				error.addKey(messageKey);
			}
		}
	}

	private String getMessageKey(String message)
	{
		if (!Strings.isEmpty(message) && message.startsWith("{") && message.endsWith("}"))
		{
			return message.substring(1, message.length() - 1);
		}
	
		return null;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy