// Path: src/main/java/com/aemreunal/exception/user/InvalidUsernameException.java
// public class InvalidUsernameException extends IllegalArgumentException {
//     public InvalidUsernameException(String causingUsername, String reason) {
//         super("Username \'" + causingUsername + "\' is invalid! " + reason);
//     }
// }
// 
// Path: src/main/java/com/aemreunal/exception/user/UserNotFoundException.java
// public class UserNotFoundException extends NullPointerException {
//     public UserNotFoundException() {
//         super("The requested user can not be found!");
//     }
// }
// 
// Path: src/main/java/com/aemreunal/exception/user/UsernameClashException.java
// public class UsernameClashException extends IllegalArgumentException {
//     public UsernameClashException(String causingUsername) {
//         super("Username \'" + causingUsername + "\' is already taken. Please choose another username.");
//     }
// }
// 
// Path: src/main/java/com/aemreunal/helper/json/JsonBuilderFactory.java
// public class JsonBuilderFactory {
//     public static JsonObjectBuilder object() {
//         return new JsonObjectBuilder();
//     }
// 
//     public static JsonArrayBuilder array() {
//         return new JsonArrayBuilder();
//     }
// }


import net.minidev.json.JSONObject;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import com.aemreunal.exception.user.InvalidUsernameException;
import com.aemreunal.exception.user.UserNotFoundException;
import com.aemreunal.exception.user.UsernameClashException;
import com.aemreunal.helper.json.JsonBuilderFactory;
package com.aemreunal.controller.user;

/*
 * *********************** *
 * Copyright (c) 2015      *
 *                         *
 * This code belongs to:   *
 *                         *
 * @author Ahmet Emre Ünal *
 * S001974                 *
 *                         *
 * aemreunal@gmail.com     *
 * emre.unal@ozu.edu.tr    *
 *                         *
 * aemreunal.com           *
 * *********************** *
 */



@ControllerAdvice
public class UserControllerAdvice {
    @ExceptionHandler(UsernameClashException.class)
    public ResponseEntity<JSONObject> usernameClashExceptionHandler(UsernameClashException ex) {
        JSONObject responseBody = JsonBuilderFactory.object().add("reason", "username")
                                                                     .add("error", ex.getLocalizedMessage())
                                                                     .build();
        return new ResponseEntity<JSONObject>(responseBody, HttpStatus.BAD_REQUEST);
    }

    @ExceptionHandler(InvalidUsernameException.class)
    public ResponseEntity<JSONObject> invalidUsernameExceptionHandler(InvalidUsernameException ex) {
        JSONObject responseBody = JsonBuilderFactory.object().add("reason", "username")
                                                                     .add("error", ex.getLocalizedMessage())
                                                                     .build();
        return new ResponseEntity<JSONObject>(responseBody, HttpStatus.BAD_REQUEST);
    }

    @ExceptionHandler(UserNotFoundException.class)
    public ResponseEntity<JSONObject> userNotFoundExceptionHandler(UserNotFoundException ex) {
// Path: src/main/java/com/aemreunal/exception/user/InvalidUsernameException.java
// public class InvalidUsernameException extends IllegalArgumentException {
//     public InvalidUsernameException(String causingUsername, String reason) {
//         super("Username \'" + causingUsername + "\' is invalid! " + reason);
//     }
// }
// 
// Path: src/main/java/com/aemreunal/exception/user/UserNotFoundException.java
// public class UserNotFoundException extends NullPointerException {
//     public UserNotFoundException() {
//         super("The requested user can not be found!");
//     }
// }
// 
// Path: src/main/java/com/aemreunal/exception/user/UsernameClashException.java
// public class UsernameClashException extends IllegalArgumentException {
//     public UsernameClashException(String causingUsername) {
//         super("Username \'" + causingUsername + "\' is already taken. Please choose another username.");
//     }
// }
// 
// Path: src/main/java/com/aemreunal/helper/json/JsonBuilderFactory.java
// public class JsonBuilderFactory {
//     public static JsonObjectBuilder object() {
//         return new JsonObjectBuilder();
//     }
// 
//     public static JsonArrayBuilder array() {
//         return new JsonArrayBuilder();
//     }
// }


// Path: src/main/java/com/aemreunal/controller/user/UserControllerAdvice.java
import net.minidev.json.JSONObject;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import com.aemreunal.exception.user.InvalidUsernameException;
import com.aemreunal.exception.user.UserNotFoundException;
import com.aemreunal.exception.user.UsernameClashException;
import com.aemreunal.helper.json.JsonBuilderFactory;

package com.aemreunal.controller.user;

/*
 * *********************** *
 * Copyright (c) 2015      *
 *                         *
 * This code belongs to:   *
 *                         *
 * @author Ahmet Emre Ünal *
 * S001974                 *
 *                         *
 * aemreunal@gmail.com     *
 * emre.unal@ozu.edu.tr    *
 *                         *
 * aemreunal.com           *
 * *********************** *
 */



@ControllerAdvice
public class UserControllerAdvice {
    @ExceptionHandler(UsernameClashException.class)
    public ResponseEntity<JSONObject> usernameClashExceptionHandler(UsernameClashException ex) {
        JSONObject responseBody = JsonBuilderFactory.object().add("reason", "username")
                                                                     .add("error", ex.getLocalizedMessage())
                                                                     .build();
        return new ResponseEntity<JSONObject>(responseBody, HttpStatus.BAD_REQUEST);
    }

    @ExceptionHandler(InvalidUsernameException.class)
    public ResponseEntity<JSONObject> invalidUsernameExceptionHandler(InvalidUsernameException ex) {
        JSONObject responseBody = JsonBuilderFactory.object().add("reason", "username")
                                                                     .add("error", ex.getLocalizedMessage())
                                                                     .build();
        return new ResponseEntity<JSONObject>(responseBody, HttpStatus.BAD_REQUEST);
    }

    @ExceptionHandler(UserNotFoundException.class)
    public ResponseEntity<JSONObject> userNotFoundExceptionHandler(UserNotFoundException ex) {
        JSONObject responseBody = JsonBuilderFactory.object().add("reason", "username")