package com.jwipc.log.config;

import com.jwipc.log.endpoint.LogBookInterceptor;
import com.jwipc.log.endpoint.LogbookThrowsAdvice;
import com.jwipc.log.prop.LogbookProp;
import lombok.RequiredArgsConstructor;
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * <p> </p>
 *
 * @author BOGON
 * @version 1.0
 * @since JDK 1.8
 */
@Configuration
@ConditionalOnProperty(value = "logbook.execution")
@RequiredArgsConstructor
public class LogbookAutoConfiguration {
    private final LogbookProp logbookConfig;
    private final LogbookThrowsAdvice logbookThrowsAdvice;
    private final LogBookInterceptor logBookInterceptor;

    @Bean
    public Advisor operationThrowsAop() {
        AspectJExpressionPointcut adapterPointcut = new AspectJExpressionPointcut();
        adapterPointcut.setExpression(logbookConfig.getExecution());
        Advisor throwsAdviceAdvisor = new DefaultPointcutAdvisor(adapterPointcut, logbookThrowsAdvice);
        return throwsAdviceAdvisor;
    }

    @Bean
    public Advisor operationLogAop() {
        AspectJExpressionPointcut adapterPointcut = new AspectJExpressionPointcut();
        adapterPointcut.setExpression(logbookConfig.getExecution());
        Advisor throwsAdviceAdvisor = new DefaultPointcutAdvisor(adapterPointcut, logBookInterceptor);
        return throwsAdviceAdvisor;
    }
}
