package com.amee.base.resource;

import org.jdom.Document;
import org.jdom.Element;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * A utility class containing helper methods for producing response representation.
 */
public class ResponseHelper {

    /**
     * Private constructor to prevent direct instantiation.
     */
    private ResponseHelper() {
        throw new AssertionError();
    }

    /**
     * Get an 'OK' response representation.
     *
     * @param requestWrapper RequestWrapper for this request
     * @return the response representation object
     */
    public static Object getOK(RequestWrapper requestWrapper) {
        return getOK(requestWrapper, null);
    }

    /**
     * Get an 'OK' response representation.
     *
     * @param requestWrapper RequestWrapper for this request
     * @param location       the location to include in the response
     * @return the response representation object
     */
    public static Object getOK(RequestWrapper requestWrapper, String location) {
        if (requestWrapper.getAcceptedMediaTypes().contains("application/json")) {
            return getStatusJSONObject("OK", location);
        } else {
            return getStatusDocument("OK", location);
        }
    }

    /**
     * Get a {@link JSONObject} response representation with the given status identifier.
     *
     * @param status identifier
     * @return the response representation object
     */
    public static JSONObject getStatusJSONObject(String status) {
        return getStatusJSONObject(status, null);
    }

    /**
     * Get a {@link JSONObject} response representation with the given status identifier.
     *
     * @param status   identifier
     * @param location the location to include in the response
     * @return the response representation object
     */
    public static JSONObject getStatusJSONObject(String status, String location) {
        try {
            JSONObject o = new JSONObject();
            o.put("status", status);
            if (location != null) {
                o.put("location", location);
            }
            return o;
        } catch (JSONException e) {
            throw new RuntimeException("Caught JSONException: " + e.getMessage(), e);
        }
    }

    /**
     * Get a {@link Document} response representation with the given status identifier.
     *
     * @param status identifier
     * @return the response representation object
     */
    public static Document getStatusDocument(String status) {
        return getStatusDocument(status, null);
    }

    /**
     * Get a {@link Document} response representation with the given status identifier.
     *
     * @param status   identifier
     * @param location the location to include in the response
     * @return the response representation object
     */
    public static Document getStatusDocument(String status, String location) {
        Element rootElem = new Element("Representation");
        rootElem.addContent(new Element("Status").setText(status));
        if (location != null) {
            rootElem.addContent(new Element("Location").setText(location));
        }
        return new Document(rootElem);
    }

    /**
     * Add an object to a {@link JSONObject} with the given key. Any {@link JSONException} is instead thrown
     * as a {@link RuntimeException}.
     *
     * @param jsonObject {@link JSONObject} to add value to
     * @param key        the value key
     * @param value      the value object
     * @return the {@link JSONObject} passed in, to support chained building
     */
    public static JSONObject put(JSONObject jsonObject, String key, Object value) {
        try {
            return jsonObject.put(key, value);
        } catch (JSONException e) {
            throw new RuntimeException("Caught JSONException: " + e.getMessage(), e);
        }
    }
}
@@ -1023,32 +1023,190 @@
 ing location) %7B%0A
+        return getOK(requestWrapper, location, null);%0A    %7D%0A    %0A    public static Object getOK(RequestWrapper requestWrapper, String location, String uid) %7B%0A
         if (requ
@@ -1317,32 +1317,37 @@
 t(%22OK%22, location
+, uid
 );%0A        %7D els
@@ -1393,32 +1393,37 @@
 t(%22OK%22, location
+, uid
 );%0A        %7D%0A   
@@ -1721,32 +1721,38 @@
 ONObject(status,
+ null,
  null);%0A    %7D%0A%0A 
@@ -2081,32 +2081,44 @@
  String location
+, String uid
 ) %7B%0A        try 
@@ -2201,16 +2201,17 @@
 tatus);%0A
+%0A
         
@@ -2288,32 +2288,235 @@
 );%0A            %7D
+%0A%0A            if (uid != null) %7B%0A                JSONObject entityObj = new JSONObject();%0A                entityObj.put(%22uid%22, uid);%0A                o.put(%22entity%22, entityObj);%0A            %7D%0A            
 %0A            ret
@@ -2952,24 +2952,30 @@
 ment(status,
+ null,
  null);%0A    
@@ -3302,32 +3302,44 @@
  String location
+, String uid
 ) %7B%0A        Elem
@@ -3448,24 +3448,25 @@
 t(status));%0A
+%0A
         if (
@@ -3563,32 +3563,152 @@
 on));%0A        %7D%0A
+%0A        if (uid != null) %7B%0A            rootElem.addContent(new Element(%22Entity%22).setAttribute(%22uid%22, uid));%0A        %7D%0A%0A
         return n

Update ResponseHelper to use UID in PUT/POST/DELETE responses. PL-11235.