package com.nobodyiam.aop;

import com.nobodyiam.dto.Greeting;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * Created by Jason on 7/9/15.
 */
@Aspect
@Component
public class GreetingAspect {
    private static final Logger logger = LoggerFactory.getLogger(GreetingAspect.class);

    @Pointcut("execution(* com.nobodyiam.api.GreetingService.insertGreeting(..)) && args(greeting)")
    public void insertGreeting(Greeting greeting) {
    }

    @Pointcut("execution(* com.nobodyiam.api.GreetingService.updateGreeting(..)) && args(greeting)")
    public void updateGreeting(Greeting greeting) {
    }

    @Pointcut("execution(* com.nobodyiam.api.GreetingService.getGreetings(..)) && args(limit, offset)")
    public void getGreetings(int limit, int offset) {
    }

    @Pointcut("execution(* com.nobodyiam.api.GreetingService.getGreeting(..))")
    public void getGreeting() {}

    @Around(value = "insertGreeting(greeting)", argNames = "joinPoint, greeting")
    public int logCreatedGreetingId(ProceedingJoinPoint joinPoint, Greeting greeting) throws Throwable {
        logger.debug(String.format("Creating greeting with content: %s", greeting.getContent()));
        int insertedRows = (Integer) joinPoint.proceed();
        logger.debug(String.format("Greeting created - id: %d, content: %s", greeting.getId(), greeting.getContent()));

        return insertedRows;
    }

    @Before(value = "updateGreeting(greeting)", argNames = "greeting")
    public void beforeUpdatingGreeting(Greeting greeting) {
        logger.debug(String.format("Updating greeting(id=%d) with content: %s", greeting.getId(), greeting.getContent()));
    }

    @AfterThrowing(value = "getGreetings(limit, offset)", argNames = "limit, offset, e", throwing = "e")
    public void logInvalidGreetingsBatch(int limit, int offset, Exception e) {
        logger.error(String.format("Invalid batch when querying greetings - limit: %d, offset: %d, exception: %s", limit, offset, e));
    }

    @After("getGreeting()")
    public void logAfterGetGreeting() {
        logger.debug("Greeting queried...");
    }
}

@@ -1295,30 +1295,16 @@
 r.debug(
-String.format(
 %22Creatin
@@ -1320,34 +1320,34 @@
 g with content: 
-%25s
+%7B%7D
 %22, greeting.getC
@@ -1351,25 +1351,24 @@
 etContent())
-)
 ;%0A        in
@@ -1436,30 +1436,16 @@
 r.debug(
-String.format(
 %22Greetin
@@ -1460,18 +1460,18 @@
 d - id: 
-%25d
+%7B%7D
 , conten
@@ -1465,34 +1465,34 @@
 d: %7B%7D, content: 
-%25s
+%7B%7D
 %22, greeting.getI
@@ -1518,17 +1518,16 @@
 ntent())
-)
 ;%0A%0A     
@@ -1709,30 +1709,16 @@
 r.debug(
-String.format(
 %22Updatin
@@ -1731,18 +1731,18 @@
 ting(id=
-%25d
+%7B%7D
 ) with c
@@ -1749,18 +1749,18 @@
 ontent: 
-%25s
+%7B%7D
 %22, greet
@@ -1794,17 +1794,16 @@
 ntent())
-)
 ;%0A    %7D%0A
@@ -2012,22 +2012,8 @@
 ror(
-String.format(
 %22Inv
@@ -2056,18 +2056,18 @@
  limit: 
-%25d
+%7B%7D
 , offset
@@ -2072,10 +2072,10 @@
 et: 
-%25d
+%7B%7D
 , ex
@@ -2087,10 +2087,10 @@
 on: 
-%25s
+%7B%7D
 %22, l
@@ -2105,17 +2105,16 @@
 fset, e)
-)
 ;%0A    %7D%0A

update logger