package com.github.aureliano.damihilogs.validation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import com.github.aureliano.damihilogs.annotation.validation.Pattern;
import com.github.aureliano.damihilogs.config.IConfiguration;
import com.github.aureliano.damihilogs.exception.DaMihiLogsException;
import com.github.aureliano.damihilogs.helper.ReflectionHelper;

public class PatternValidator implements IValidator {

	public PatternValidator() {
		super();
	}

	@Override
	public ConstraintViolation validate(IConfiguration configuration, Method method, Annotation annotation) {
		String property = ReflectionHelper.getSimpleAccessMethodName(method);
		Object returnedValue = ReflectionHelper.callMethod(configuration, method.getName(), null, null);
		
		if (returnedValue == null) {
			returnedValue = "";
		}
		
		String message = ((Pattern) annotation).message();
		String regex = ((Pattern) annotation).value();
		
		if (!(returnedValue instanceof String)) {
			throw new DaMihiLogsException(Pattern.class.getName() + " can be applyed only for " +
					String.class.getName() + " types.");
		}
		
		if (!returnedValue.toString().matches(regex)) {
			return new ConstraintViolation()
				.withValidator(Pattern.class)
				.withMessage(message
					.replaceFirst("#\\{0\\}", property)
					.replaceFirst("#\\{1\\}", regex.replace("\\", "\\\\")));
		}
		
		return null;
	}
}
@@ -783,16 +783,17 @@
 %09%09%0A%09%09if 
+(
 (returne
@@ -812,151 +812,11 @@
 ll) 
-%7B%0A%09%09%09returnedValue = %22%22;%0A%09%09%7D%0A%09%09%0A%09%09String message = ((Pattern) annotation).message();%0A%09%09String regex = ((Pattern) annotation).value();%0A%09%09%0A%09%09if (
+%7C%7C 
 !(re
@@ -983,24 +983,129 @@
 .%22);%0A%09%09%7D%0A%09%09%0A
+%09%09String message = ((Pattern) annotation).message();%0A%09%09String regex = ((Pattern) annotation).value();%0A%09%09%0A
 %09%09if (!retur

Create pattern validation.