package com.aemreunal.controller.project;

import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
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.transaction.TransactionSystemException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import com.aemreunal.exception.project.ProjectNotFoundException;

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

@ControllerAdvice
public class ProjectControllerAdvice {
    @ExceptionHandler(ProjectNotFoundException.class)
    public ResponseEntity<JSONObject> projectNotFoundExceptionHandler(ProjectNotFoundException ex) {
        Map<String, String> errorMessage = new HashMap<>(2);
        errorMessage.put("reason", "project");
        errorMessage.put("error", ex.getLocalizedMessage());
        JSONObject responseBody = new JSONObject(errorMessage);
        return new ResponseEntity<JSONObject>(responseBody, HttpStatus.BAD_REQUEST);
    }

    @ExceptionHandler(value = {ConstraintViolationException.class, TransactionSystemException.class })
    public ResponseEntity<JSONObject> constraintViolationExceptionHandler(ConstraintViolationException ex) {
        Set<ConstraintViolation<?>> violations = ex.getConstraintViolations();
        Map<String, Object> errorMessage = new HashMap<>(3);
        errorMessage.put("reason", "project");
        errorMessage.put("error", "Constraint violation error ocurred! Unable to save project.");
        errorMessage.put("violations", formatViolations(violations));
        JSONObject responseBody = new JSONObject(errorMessage);
        return new ResponseEntity<JSONObject>(responseBody, HttpStatus.BAD_REQUEST);
    }

    private JSONArray formatViolations(Set<ConstraintViolation<?>> violations) {
        JSONArray violationDescriptions = new JSONArray();
        for (ConstraintViolation<?> violation : violations) {
            Map<String, String> violationDescription = new HashMap<>(2);
            violationDescription.put("property", violation.getPropertyPath().toString());
            violationDescription.put("violation", violation.getMessage());
            violationDescriptions.add(violationDescription);
        }
        return violationDescriptions;
    }
}

@@ -630,16 +630,57 @@
 ception;
+%0Aimport com.aemreunal.helper.JsonBuilder;
 %0A%0A/*%0A **
@@ -1282,85 +1282,55 @@
     
-Map%3CString, String%3E errorMessage = new HashMap%3C%3E(2);%0A        errorMessage.put
+JSONObject responseBody = new JsonBuilder().add
 (%22re
@@ -1338,33 +1338,32 @@
 son%22, %22project%22)
-;
 %0A        errorMe
@@ -1355,32 +1355,63 @@
         
-errorMessage.put
+                                           .add
 (%22error%22
@@ -1429,33 +1429,32 @@
 alizedMessage())
-;
 %0A        JSONObj
@@ -1450,61 +1450,58 @@
     
-JSONObject responseBody = new JSONObject(errorMessage
+                                           .build(
 );%0A 
@@ -1622,16 +1622,17 @@
 alue = %7B
+ 
 Constrai
@@ -1816,164 +1816,55 @@
     
-Set%3CConstraintViolation%3C?%3E%3E violations = ex.getConstraintViolations();%0A        Map%3CString, Object%3E errorMessage = new HashMap%3C%3E(3);%0A        errorMessage.put
+JSONObject responseBody = new JsonBuilder().add
 (%22re
@@ -1880,17 +1880,16 @@
 roject%22)
-;
 %0A       
@@ -1889,32 +1889,63 @@
         
-errorMessage.put
+                                           .add
 (%22error%22
@@ -2008,17 +2008,16 @@
 oject.%22)
-;
 %0A       
@@ -2021,24 +2021,55 @@
     
-errorMessage.put
+                                           .add
 (%22vi
@@ -2096,17 +2096,33 @@
 lations(
-v
+ex.getConstraintV
 iolation
@@ -2122,19 +2122,20 @@
 olations
+(
 ))
-;
+)
 %0A       
@@ -2139,61 +2139,58 @@
     
-JSONObject responseBody = new JSONObject(errorMessage
+                                           .build(
 );%0A 

Use JsonBuilder for responses