
package com.github.ljtfreitas.restify.http.spring.contract.metadata;

import org.springframework.beans.factory.config.BeanExpressionContext;
import org.springframework.beans.factory.config.BeanExpressionResolver;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.expression.StandardBeanExpressionResolver;

import com.github.ljtfreitas.restify.http.contract.metadata.ContractExpressionResolver;

public class SpelDynamicParameterExpressionResolver implements ContractExpressionResolver {

	private final ConfigurableBeanFactory beanFactory;
	private final BeanExpressionResolver resolver;
	private final BeanExpressionContext context;

	public SpelDynamicParameterExpressionResolver(ConfigurableBeanFactory beanFactory) {
		this.beanFactory = beanFactory;
		this.resolver = beanFactory.getBeanExpressionResolver();
		this.context = new BeanExpressionContext(beanFactory, null);
	}

	@Override
	public String resolve(String expression) {
		if (bean(expression)) {
			return resolver.evaluate(expression, context).toString();
		} else {
			return beanFactory.resolveEmbeddedValue(expression);
		}
	}

	private boolean bean(String expression) {
		return expression.startsWith(StandardBeanExpressionResolver.DEFAULT_EXPRESSION_PREFIX)
				&& expression.endsWith(StandardBeanExpressionResolver.DEFAULT_EXPRESSION_SUFFIX);
	}
}
