// Path: src/main/java/com/aemreunal/exception/MalformedRequestException.java
// public class MalformedRequestException /*extends Throwable*/ extends RuntimeException {
// 
//     // http://normanmaurer.me/blog/2013/11/09/The-hidden-performance-costs-of-instantiating-Throwables/
// //    public MalformedRequestException() {
// //        super("Your request is malformed. Please try again.", null, true, false);
// //    }
// 
//     public MalformedRequestException() {
//         super("Your request is malformed. Please try again.");
//     }
// }
// 
// Path: src/main/java/com/aemreunal/helper/json/JsonArrayBuilder.java
// public class JsonArrayBuilder {
//     private ArrayList<Object> jsonList = new ArrayList<>();
// 
//     JsonArrayBuilder() {
//     }
// 
//     public JsonArrayBuilder add(Object item) {
//         jsonList.add(item);
//         return this;
//     }
// 
//     public JsonArrayBuilder addAll(Collection items) {
//         jsonList.addAll(items);
//         return this;
//     }
// 
//     public JSONArray build() {
//         JSONArray array = new JSONArray();
//         array.addAll(jsonList);
//         return array;
//     }
// }
// 
// 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.JSONArray;
import net.minidev.json.JSONObject;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
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.MalformedRequestException;
import com.aemreunal.helper.json.JsonArrayBuilder;
import com.aemreunal.helper.json.JsonBuilderFactory;
package com.aemreunal.controller;

/*
 * *********************** *
 * 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 GeneralControllerAdvice {
    @ExceptionHandler(MalformedRequestException.class)
    public ResponseEntity<JSONObject> malformedRequestExceptionHandler(MalformedRequestException ex) {
        JSONObject responseBody = JsonBuilderFactory.object().add("reason", "request")
                                                    .add("error", ex.getLocalizedMessage())
                                                    .build();
        return new ResponseEntity<JSONObject>(responseBody, HttpStatus.BAD_REQUEST);
    }

    @ExceptionHandler(ConstraintViolationException.class)
    public ResponseEntity<JSONObject> constraintViolationExceptionHandler(ConstraintViolationException ex) {
        JSONObject responseBody = JsonBuilderFactory.object().add("error", "Constraint violation error occurred! Unable to save entity.")
                                                    .add("violations", formatViolations(ex.getConstraintViolations()))
                                                    .build();
        return new ResponseEntity<JSONObject>(responseBody, HttpStatus.BAD_REQUEST);
    }

    private JSONArray formatViolations(Set<ConstraintViolation<?>> violations) {
// Path: src/main/java/com/aemreunal/exception/MalformedRequestException.java
// public class MalformedRequestException /*extends Throwable*/ extends RuntimeException {
// 
//     // http://normanmaurer.me/blog/2013/11/09/The-hidden-performance-costs-of-instantiating-Throwables/
// //    public MalformedRequestException() {
// //        super("Your request is malformed. Please try again.", null, true, false);
// //    }
// 
//     public MalformedRequestException() {
//         super("Your request is malformed. Please try again.");
//     }
// }
// 
// Path: src/main/java/com/aemreunal/helper/json/JsonArrayBuilder.java
// public class JsonArrayBuilder {
//     private ArrayList<Object> jsonList = new ArrayList<>();
// 
//     JsonArrayBuilder() {
//     }
// 
//     public JsonArrayBuilder add(Object item) {
//         jsonList.add(item);
//         return this;
//     }
// 
//     public JsonArrayBuilder addAll(Collection items) {
//         jsonList.addAll(items);
//         return this;
//     }
// 
//     public JSONArray build() {
//         JSONArray array = new JSONArray();
//         array.addAll(jsonList);
//         return array;
//     }
// }
// 
// 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/GeneralControllerAdvice.java
import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
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.MalformedRequestException;
import com.aemreunal.helper.json.JsonArrayBuilder;
import com.aemreunal.helper.json.JsonBuilderFactory;

package com.aemreunal.controller;

/*
 * *********************** *
 * 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 GeneralControllerAdvice {
    @ExceptionHandler(MalformedRequestException.class)
    public ResponseEntity<JSONObject> malformedRequestExceptionHandler(MalformedRequestException ex) {
        JSONObject responseBody = JsonBuilderFactory.object().add("reason", "request")
                                                    .add("error", ex.getLocalizedMessage())
                                                    .build();
        return new ResponseEntity<JSONObject>(responseBody, HttpStatus.BAD_REQUEST);
    }

    @ExceptionHandler(ConstraintViolationException.class)
    public ResponseEntity<JSONObject> constraintViolationExceptionHandler(ConstraintViolationException ex) {
        JSONObject responseBody = JsonBuilderFactory.object().add("error", "Constraint violation error occurred! Unable to save entity.")
                                                    .add("violations", formatViolations(ex.getConstraintViolations()))
                                                    .build();
        return new ResponseEntity<JSONObject>(responseBody, HttpStatus.BAD_REQUEST);
    }

    private JSONArray formatViolations(Set<ConstraintViolation<?>> violations) {
        JsonArrayBuilder arrayBuilder = JsonBuilderFactory.array();