1710 Rebase - Second Attempt
[so.git] / bpmn / MSOCoreBPMN / src / main / java / org / openecomp / mso / bpmn / core / json / JsonUtils.java
index 4c0d068..fc59b2b 100644 (file)
@@ -20,7 +20,9 @@
 \r
 package org.openecomp.mso.bpmn.core.json;\r
 \r
+import java.util.ArrayList;\r
 import java.util.Iterator;\r
+import java.util.List;\r
 import java.util.Map;\r
 import java.util.HashMap;\r
 import java.util.StringTokenizer;\r
@@ -95,9 +97,11 @@ public class JsonUtils {
                try {\r
                        JSONObject jsonObj = new JSONObject(jsonStr);\r
                        if (pretty) {\r
-                               //      use the local class method which properly handles certain JSONArray content\r
+//                             return XmlTool.normalize(XML.toString(jsonObj));\r
+//                             use the local class method which properly handles certain JSONArray content\r
                                return XmlTool.normalize(toXMLString(jsonObj, null));\r
                        } else {\r
+//                             return XML.toString(jsonObj);\r
 //                             use the local class method which properly handles certain JSONArray content\r
                                return toXMLString(jsonObj, null);\r
                        }\r
@@ -106,7 +110,7 @@ public class JsonUtils {
                                return null;\r
                }\r
        }\r
-       \r
+\r
        /**\r
         * Uses a modified version of the org.json.XML toString() algorithm\r
         * to convert a JSONObject to an XML Doc. The intent of this is to\r
@@ -173,7 +177,7 @@ public class JsonUtils {
                                                curObj = jsonArr.get(i);\r
                                                if (curObj instanceof JSONArray) {\r
 //                                                     The XML tags for the nested array should be generated below when this method\r
-//                                                     is called recursively and the JSONArray object is passed                                                        \r
+//                                                     is called recursively and the JSONArray object is passed\r
 //                                                     strBuf.append("<");\r
 //                                                     strBuf.append(key);\r
 //                                                     strBuf.append(">");\r
@@ -237,7 +241,17 @@ public class JsonUtils {
                                                        + tagName + ">" + str + "</" + tagName + ">";\r
                }\r
        }\r
-       \r
+\r
+       /**\r
+        * Invokes json2xml(String, Boolean) defaulting to 'pretty' output.\r
+        *\r
+        * @param  jsonStr      String containing the XML doc\r
+        * @return String containing the JSON translation\r
+        */\r
+       public static String json2xml(String jsonStr) {\r
+               return json2xml(jsonStr, true);\r
+       }\r
+\r
        /**\r
         * Formats the JSON String using the value of MSOJsonIndentFactor.\r
         *\r
@@ -254,17 +268,7 @@ public class JsonUtils {
                        return null;\r
                }\r
        }\r
-       \r
-       /**\r
-        * Invokes json2xml(String, Boolean) defaulting to 'pretty' output.\r
-        *\r
-        * @param  jsonStr      String containing the XML doc\r
-        * @return String containing the JSON translation\r
-        */\r
-       public static String json2xml(String jsonStr) {\r
-               return json2xml(jsonStr, true);\r
-       }\r
-       \r
+\r
        /**\r
         * Returns an Iterator over the JSON keys in the specified JSON doc.\r
         *\r
@@ -275,7 +279,7 @@ public class JsonUtils {
        public static Iterator <String> getJsonIterator(String jsonStr) throws JSONException {\r
                return new JSONObject(jsonStr).keys();\r
        }\r
-       \r
+\r
        /**\r
         * Returns the name of the "root" property in the specified JSON doc. The\r
         * "root" property is the single top-level property in the JSON doc. An\r
@@ -306,7 +310,7 @@ public class JsonUtils {
        /**\r
         * Invokes the getJsonRawValue() method and returns the String equivalent of\r
         * the object returned.\r
-        * \r
+        *\r
         * TBD: May need separate methods for boolean, float, and integer fields if the\r
         * String representation is not sufficient to meet client needs.\r
         *\r
@@ -334,8 +338,7 @@ public class JsonUtils {
                }\r
                return null;\r
        }\r
-       \r
-       \r
+\r
        /**\r
         * Invokes the getJsonRawValue() method with the wrap flag set to true\r
         * and returns the String equivalent of the json node object returned.\r
@@ -368,7 +371,7 @@ public class JsonUtils {
        /**\r
         * Invokes the getJsonRawValue() method and returns the String equivalent of\r
         * the object returned.\r
-        * \r
+        *\r
         * TBD: May need separate methods for boolean, float, and integer fields if the\r
         * String representation is not sufficient to meet client needs.\r
         *\r
@@ -397,9 +400,38 @@ public class JsonUtils {
                return 0;\r
        }\r
 \r
+       /**\r
+        * Invokes the getJsonRawValue() method and returns the boolean equivalent of\r
+        * the object returned.\r
+        *\r
+        * @param  jsonStr      String containing the JSON doc\r
+        * @param  keys         full key path to the target value in the format of "key1.key2.key3..."\r
+        * @return boolean field value associated with keys - default is false\r
+        */\r
+       public static boolean getJsonBooleanValue(String jsonStr, String keys) {\r
+               String isDebugLogEnabled = "true";\r
+               try {\r
+                               Object rawValue = getJsonRawValue(jsonStr, keys);\r
+                               if (rawValue == null) {\r
+                                       return false;\r
+                               } else {\r
+                                       if (rawValue instanceof Boolean) {\r
+                                               msoLogger.debug("getJsonValue(): the raw value is a Boolean Object=" + ((String) rawValue).toString());\r
+                                               return (Boolean) rawValue;\r
+                                       } else {\r
+                                               msoLogger.debug("getJsonValue(): the raw value is NOT an Boolean Object=" + rawValue.toString());\r
+                                               return false;\r
+                                       }\r
+                               }\r
+               } catch (Exception e) {\r
+                               msoLogger.debug("getJsonValue(): unable to parse json to retrieve value for field=" + keys + ". Exception was: " + e.toString());\r
+               }\r
+               return false;\r
+       }\r
+\r
        /**\r
         * Invokes the getJsonParamValue() method to obtain the JSONArray associated with\r
-         * the specified keys. The JSONArray is then walked to retrieve the first array\r
+        * the specified keys. The JSONArray is then walked to retrieve the first array\r
         * value associated with the specified field name (index=0).\r
         *\r
         * @param  jsonStr      String containing the JSON doc\r
@@ -414,7 +446,7 @@ public class JsonUtils {
        /**\r
         * Invokes the getJsonRawValue() method to obtain the JSONArray associated with\r
         * the specified keys. The JSONArray is then walked to retrieve the nth array\r
-        * value associated with the specified field name and index\r
+        * value associated with the specified field name and index.\r
         *\r
         * @param  jsonStr      String containing the JSON doc\r
         * @param  keys         full key path to the target value in the format of "key1.key2.key3..."\r
@@ -510,7 +542,11 @@ public class JsonUtils {
                try {\r
                        if (jsonObj.has(key)) {\r
                                msoLogger.debug("getJsonValueForKey(): found value for key=" + key);\r
-                               return ((String) jsonObj.get(key));\r
+                               Object value = jsonObj.get(key);\r
+                               if (value == null)\r
+                                       return null;\r
+                               else\r
+                                       return ((String) value);\r
                        } else {\r
                                msoLogger.debug("getJsonValueForKey(): iterating over the keys");\r
                                Iterator <String> itr = jsonObj.keys();\r
@@ -538,7 +574,7 @@ public class JsonUtils {
                }\r
                return keyValue;\r
        }\r
-       \r
+\r
        /**\r
         * Walks the JSONObject (and sub-objects recursively), searching for the first value associated with the\r
         * single key/field name specified. Returns the associated value if found or null if the key is not found\r
@@ -581,7 +617,50 @@ public class JsonUtils {
                }\r
                return keyValue;\r
        }\r
-       \r
+\r
+       /**\r
+        * Walks the JSONObject (and sub-objects recursively), searching for the first value associated with the\r
+        * single key/field name specified. Returns the associated value if found or null if the key is not found\r
+        *\r
+        * @param  jsonObj      JSONObject representation of the the JSON doc\r
+        * @param  key          key to the target value\r
+        * @return String field value associated with key\r
+        */\r
+       public static Boolean getJsonBooleanValueForKey(JSONObject jsonObj, String key) {\r
+               String isDebugLogEnabled = "true";\r
+               Boolean keyValue = false;\r
+               try {\r
+                       if (jsonObj.has(key)) {\r
+                               msoLogger.debug("getJsonBooleanValueForKey(): found value for key=" + key);\r
+                               return ((Boolean) jsonObj.get(key));\r
+                       } else {\r
+                               msoLogger.debug("getJsonBooleanValueForKey(): iterating over the keys");\r
+                               Iterator <String> itr = jsonObj.keys();\r
+                               while (itr.hasNext()) {\r
+                                       String nextKey = (String) itr.next();\r
+                                       Object obj = jsonObj.get(nextKey);\r
+                                       if (obj instanceof JSONObject) {\r
+                                               msoLogger.debug("getJsonBooleanValueForKey(): key=" + nextKey + ", points to JSONObject, recursive call");\r
+                                               keyValue = getJsonBooleanValueForKey((JSONObject) obj, key);\r
+                                               if (keyValue != null) {\r
+                                                       msoLogger.debug("getJsonBooleanValueForKey(): found value=" + keyValue + ", for key=" + key);\r
+                                                       break;\r
+                                               }\r
+                                       } else {\r
+                                               msoLogger.debug("getJsonBooleanValueForKey(): key=" + nextKey + ", does not point to a JSONObject, next key");\r
+                                       }\r
+                               }\r
+                       }\r
+               } catch (JSONException je) {\r
+                               // JSONObject::get() throws this exception if one of the specified keys is not found\r
+                               msoLogger.debug("getJsonBooleanValueForKey(): caught JSONException attempting to retrieve value for key=" + key);\r
+                               keyValue = null;\r
+               } catch (Exception e) {\r
+                               msoLogger.debug("getJsonBooleanValueForKey(): unable to parse json to retrieve value for field=" + key + ". Exception was: " + e.toString());\r
+               }\r
+               return keyValue;\r
+       }\r
+\r
        /**\r
         * Boolean method to determine if a key path is valid for the JSON doc. Invokes\r
         * getJsonValue().\r
@@ -597,7 +676,7 @@ public class JsonUtils {
                        return true;\r
                }\r
        }\r
-       \r
+\r
        /**\r
         * Inserts the new key/value pair at the appropriate location in the JSON\r
         * document after first determining if keyed field already exists. If\r
@@ -673,7 +752,7 @@ public class JsonUtils {
         */\r
        private static Object getJsonRawValue(String jsonStr, String keys) {\r
                return getJsonRawValue(jsonStr, keys, false);\r
-       }       \r
+       }\r
 \r
        /**\r
         * Walks the JSON doc using the full key path to retrieve the associated\r
@@ -683,8 +762,8 @@ public class JsonUtils {
         *\r
         * @param  jsonStr      String containing the JSON doc\r
         * @param  keys         full key path to the target value in the format of "key1.key2.key3..."\r
-        * * @param  wrap       Boolean which determines if returned JSONObjects sould be "wrapped"\r
-        *                  Note: wrap does not apply to returned scalar values \r
+        * @param  wrap         Boolean which determines if returned JSONObjects sould be "wrapped"\r
+        *                  Note: wrap does not apply to returned scalar values\r
         * @return Object field value associated with keys\r
         */\r
        private static Object getJsonRawValue(String jsonStr, String keys, Boolean wrap) {\r
@@ -735,7 +814,7 @@ public class JsonUtils {
         * @param  keys         full key path to the value to be deleted in the format of "key1.key2.key3..."\r
         * @return String containing the updated JSON doc\r
         */\r
-       private static String putJsonValue(String jsonStr, String keys, String value) {         \r
+       private static String putJsonValue(String jsonStr, String keys, String value) {\r
 //             String isDebugLogEnabled = "true";\r
                String keyStr = "";\r
                try {\r
@@ -760,7 +839,7 @@ public class JsonUtils {
                        }\r
                        // should not hit this point if the key points to a valid key value\r
                        return null;\r
-                       \r
+\r
                } catch (JSONException je) {\r
                                // JSONObject::get() throws this exception if one of the specified keys is not found\r
                                msoLogger.debug("putJsonValue(): caught JSONException attempting to retrieve value for key=" + keyStr);\r
@@ -772,14 +851,13 @@ public class JsonUtils {
        }\r
 \r
        /**\r
-        * This json util method converts a json "Key" and "Value"\r
-        * entry Array to a Java map.\r
+        * This json util method converts a json array of Key Value\r
+        * pair objects into a Java Map.\r
         *\r
         * @param execution\r
-        * @param entryArray - the json value of the entry Array\r
-        *\r
-        * @return map - a Map containing the entries\r
+        * @param entryArray - the getJsonValue of a json Array of key/value pairs\r
         *\r
+        * @return Map - a Map containing the entries\r
         */\r
        public Map<String, String> entryArrayToMap(Execution execution, String entryArray) {\r
                msoLogger.debug("Started Entry Array To Map Util Method");\r
@@ -801,8 +879,68 @@ public class JsonUtils {
                return map;\r
        }\r
 \r
+       /**\r
+        * This json util method converts a json array of Key Value\r
+        * pair objects into a Java Map.\r
+        *\r
+        * @param execution\r
+        * @param entryArray - the getJsonValue of a json Array of key/value pairs\r
+        * @param keyNode - the name of the node that represents the key\r
+        * @param valueNode - the name of the node that represents the value\r
+        *\r
+        * @return Map - a Map containing the entries\r
+        *\r
+        */\r
+       public Map<String, String> entryArrayToMap(Execution execution, String entryArray, String keyNode, String valueNode) {\r
+               msoLogger.debug("Started Entry Array To Map Util Method");\r
+\r
+               Map<String, String> map = new HashMap<String, String>();\r
+               //Populate Map\r
+               String entryListJson = "{ \"entry\":" + entryArray + "}";\r
+               JSONObject obj = new JSONObject(entryListJson);\r
+               JSONArray arr = obj.getJSONArray("entry");\r
+               for (int i = 0; i < arr.length(); i++){\r
+                       JSONObject jo = arr.getJSONObject(i);\r
+                       String key = jo.getString(keyNode);\r
+                       String value =jo.getString(valueNode);\r
+                       map.put(key, value);\r
+               }\r
+               msoLogger.debug("Outgoing Map is: " + map);\r
+               msoLogger.debug("Completed Entry Array To Map Util Method");\r
+               return map;\r
+       }\r
 \r
        /**\r
+        * This json util method converts a json Array of Strings\r
+        * to a Java List. It takes each String in the json Array\r
+        * and puts it in a Java List<String>.\r
+        *\r
+        * @param execution\r
+        * @param stringArray - the getJsonValue of a json array of strings\r
+        *\r
+        * @return List - a java list containing the strings\r
+        *\r
+        *\r
+        */\r
+       public List<String> StringArrayToList(Execution execution, String jsonArrayOfStrings) {\r
+               msoLogger.debug("Started  String Array To List Util Method");\r
+\r
+               List<String> list = new ArrayList<String>();\r
+               //Populate List\r
+               String stringListJson = "{ \"strings\":" + jsonArrayOfStrings + "}";\r
+               JSONObject obj = new JSONObject(stringListJson);\r
+               JSONArray arr = obj.getJSONArray("strings");\r
+               for (int i = 0; i < arr.length(); i++){\r
+                       String s = arr.getString(i);\r
+                       list.add(s);\r
+               }\r
+               msoLogger.debug("Outgoing List is: " + list);\r
+               msoLogger.debug("Completed String Array To List Util Method");\r
+               return list;\r
+       }\r
+\r
+       /**\r
+        *\r
         * Invokes the getJsonRawValue() method to determine if the\r
         * json element/variable exist. Returns true if the\r
         * json element exist\r
@@ -810,6 +948,8 @@ public class JsonUtils {
         * @param  jsonStr      String containing the JSON doc\r
         * @param  keys         full key path to the target value in the format of "key1.key2.key3..."\r
         * @return boolean field value associated with keys\r
+        *\r
+        *\r
         */\r
        public static boolean jsonElementExist(String jsonStr, String keys) {\r
 \r
@@ -826,5 +966,4 @@ public class JsonUtils {
                return true;\r
        }\r
 \r
-}\r
-\r
+}
\ No newline at end of file