+ } else {
+ sb = new StringBuilder();
+ parseData(triggerSig, parseKey);
+ }
+ }
+ policyAdapter.setJsonBody(finalJson.toString());
+ }
+ return policyAdapter;
+ }
+
+ private JsonNode putRuletoJson(JsonNode tmpJsonNode, int item, StringBuilder sb, String parseKey,
+ String primaryKey) {
+ JsonNode tmp = tmpJsonNode;
+ ObjectNode objectNode = (ObjectNode) tmp;
+ JsonNode jsonNode = tmpJsonNode.get("content").get(primaryKey).get(item);
+ JsonNode tempRuleJsonNode = tmpJsonNode.get("content").get(primaryKey).get(item);
+ String[] tempSt = parseKey.split("\\.");
+ for (String value : tempSt) {
+ if (value.contains("[")) {
+ if (tempRuleJsonNode instanceof ArrayNode) {
+ JsonNode tempRuleNode = tempRuleJsonNode.get(item);
+ ((ArrayNode) tempRuleJsonNode).removeAll();
+ ((ArrayNode) tempRuleJsonNode).add(tempRuleNode);
+ objectNode = (ObjectNode) tempRuleJsonNode.get(item);
+ }
+ String key = value.substring(0, value.indexOf('['));
+ objectNode.remove(key);
+ objectNode.put(key, sb.toString());
+ return tmp;
+ } else {
+ jsonNode = jsonNode.get(value);
+ if (jsonNode instanceof ArrayNode) {
+ tempRuleJsonNode = jsonNode;
+ jsonNode = jsonNode.get(item);
+ }
+ }
+ }
+ return tmp;
+ }
+
+ public boolean checkPattern(String patternString) {
+ return PATTERN.matcher(patternString).find();
+ }
+
+ /**
+ * Construct rule.
+ *
+ * @param sb the sb
+ * @param jsonNode the json node
+ * @param sigRules2 the sig rules 2
+ */
+ public void constructRule(StringBuilder sb, String jsonNode, Map<String, String> sigRules2) {
+ int count = 0;
+ String cleanJsonNode = jsonNode.replace("\"\"", " ");
+ cleanJsonNode = cleanJsonNode.replaceAll("\"", "");
+ cleanJsonNode = cleanJsonNode.replaceAll("\\(", "");
+ cleanJsonNode = cleanJsonNode.replaceAll("\\)", "");
+ boolean flag = false;
+ if (cleanJsonNode.contains("OR")) {
+ sb.append("(");
+ flag = true;
+ }
+ for (String rowValue : cleanJsonNode.split(" ")) {
+ if (checkPattern(rowValue)) {
+ String value = sigRules2.get(rowValue);
+ LOGGER.info(" Value is:" + value);
+ constructRule(sb, value, sigRules2);
+ } else {
+ if ((count == 0) && (!("AND").equals(rowValue)) && (!("OR").equals(rowValue))) {
+ sb.append("(");
+ }
+ count++;
+ LOGGER.info(" " + rowValue + " ");
+ sb.append(" " + rowValue + " ");
+ if (count % 3 == 0) {
+ sb.append(")");
+ count = 0;
+ }
+ }
+ }
+ if (flag) {
+ sb.append(")");
+ }
+ }
+
+ /**
+ * Parses the data.
+ *
+ * @param jsonNode the json node
+ * @param string the string
+ */
+ public void parseData(JsonNode jsonNode, String string) {
+ if (string.contains(".")) {
+ String firstIndex = string.substring(0, string.indexOf('.'));
+ JsonNode signtures = jsonNode.get(firstIndex);
+ String subIndex = string.substring(firstIndex.length() + 1);
+ if (signtures instanceof ArrayNode) {
+ for (int i = 0; i < signtures.size(); i++) {
+ parseData(signtures.get(i), subIndex);
+ }
+ } else {
+ parseData(signtures, subIndex);
+ }
+ } else {
+ if (string.contains("[")) {
+ String ruleIndex = string.substring(0, string.indexOf('['));
+ String[] keys = string.substring(string.indexOf('[') + 1, string.lastIndexOf(']')).split(",");
+ String key = "A" + Integer.valueOf(sigRules.size() + 1);
+ JsonNode node = jsonNode.get(ruleIndex);
+ StringBuilder sb = new StringBuilder("(");
+ for (int i = 0; i < keys.length; i++) {
+ sb.append(node.get(keys[i].trim()));
+ }
+ sb.append(")");
+ sigRules.put(key, sb.toString());
+ }
+ }
+ }
+
+ public String removeNullAttributes(String cleanJson) {
+ ObjectMapper mapper = new ObjectMapper();
+
+ try {
+ JsonNode rootNode = mapper.readTree(cleanJson);
+ JsonNode returnNode = mapper.readTree(cleanJson);
+ Iterator<Map.Entry<String, JsonNode>> fieldsIterator = rootNode.fields();
+ boolean remove = false;
+ JsonObject removed = null;
+ boolean contentChanged = false;
+ while (fieldsIterator.hasNext()) {
+ Map.Entry<String, JsonNode> field = fieldsIterator.next();
+ final String key = field.getKey();
+ final JsonNode value = field.getValue();
+ if ("content".equalsIgnoreCase(key)) {
+ String contentStr = value.toString();
+ try (JsonReader reader = Json.createReader(new StringReader(contentStr))) {
+ JsonObject jsonContent = reader.readObject();
+ removed = removeNull(jsonContent);
+ if (!jsonContent.toString().equals(removed.toString())) {
+ contentChanged = true;
+ }
+ }
+
+ if (value == null || value.isNull()) {
+ ((ObjectNode) returnNode).remove(key);
+ remove = true;
+ }
+ }
+ if (remove) {
+ cleanJson = returnNode.toString();
+ }
+ if (value == null || value.isNull()) {
+ ((ObjectNode) returnNode).remove(key);
+ remove = true;
+ }
+ }
+ if (remove) {
+ cleanJson = returnNode.toString();
+ }
+
+ if (contentChanged) {
+ // set modified content to cleanJson
+ JSONObject jObject = new JSONObject(cleanJson);
+ jObject.put("content", removed.toString());
+ cleanJson = cleanUPJson(jObject.toString());
+ }
+
+ } catch (IOException e) {
+ LOGGER.error("Error writing out the JsonNode", e);
+ }
+ return cleanJson;
+ }
+
+ /**
+ * To verify if it is a JSON string. If it is, then return its original value.
+ *
+ * @param key holds the values
+ * @return
+ */
+ private String getOriginalValue(String key) {
+ for (String k : jsonStringValues.keySet()) {
+ if (k.contains("@")) {
+ String[] arrOfKeys = k.split("@");
+ for (int i = 0; i < arrOfKeys.length; i++) {
+ if (arrOfKeys[i].contains(".")) {
+ arrOfKeys[i] = arrOfKeys[i].substring(arrOfKeys[i].indexOf(".") + 1);
+ if (arrOfKeys[i].equals(key)) {
+ return StringUtils.replaceEach(jsonStringValues.get(k), new String[] {"\""},
+ new String[] {"\\\""});
+ }
+ }
+ }
+ }
+ if (k.endsWith(key)) {
+ return StringUtils.replaceEach(jsonStringValues.get(k), new String[] {"\""}, new String[] {"\\\""});
+ }
+ }
+
+ return null;
+ }
+
+ public JsonArray removeNull(JsonArray array) {
+ JsonArrayBuilder builder = Json.createArrayBuilder();
+ int i = 0;
+ for (Iterator<JsonValue> it = array.iterator(); it.hasNext(); ++i) {
+ JsonValue value = it.next();
+ switch (value.getValueType()) {
+ case ARRAY:
+ JsonArray a = removeNull(array.getJsonArray(i));
+ if (!a.isEmpty())
+ builder.add(a);
+ break;
+ case OBJECT:
+ JsonObject object = removeNull(array.getJsonObject(i));
+ if (!object.isEmpty())
+ builder.add(object);
+ break;
+ case STRING:
+ String s = array.getString(i);
+ if (s != null && !s.isEmpty())
+ builder.add(s);
+ break;
+ case NUMBER:
+ builder.add(array.getJsonNumber(i));
+ break;
+ case TRUE:
+ case FALSE:
+ builder.add(array.getBoolean(i));
+ break;
+ case NULL:
+ break;
+ }
+ }
+ return builder.build();
+ }
+
+ public JsonObject removeNull(JsonObject obj) {
+ JsonObjectBuilder builder = Json.createObjectBuilder();
+ for (Iterator<Entry<String, JsonValue>> it = obj.entrySet().iterator(); it.hasNext();) {
+ Entry<String, JsonValue> e = it.next();
+ String key = e.getKey();
+ JsonValue value = e.getValue();
+ switch (value.getValueType()) {
+ case ARRAY:
+ JsonArray array = removeNull(obj.getJsonArray(key));
+ if (!array.isEmpty())
+ builder.add(key, array);
+ break;
+ case OBJECT:
+ JsonObject object = removeNull(obj.getJsonObject(key));
+ if (!object.isEmpty()) {
+ if (!jsonStringValues.isEmpty()) {
+ String originalValue = getOriginalValue(key);
+ if (originalValue != null) {
+ builder.add(key, object.toString());
+ break;
+ }
+ }
+ builder.add(key, object);
+ }
+ break;
+ case STRING:
+ String s = obj.getString(key);
+ if (s != null && !s.isEmpty()) {
+ if (!jsonStringValues.isEmpty()) {
+ String originalValue = getOriginalValue(key);
+ if (originalValue != null) {
+ s = getOriginalValue(key);
+ }
+ }
+ builder.add(key, s);
+ }
+ break;
+ case NUMBER:
+ builder.add(key, obj.getJsonNumber(key));
+ break;
+ case TRUE:
+ case FALSE:
+ builder.add(key, obj.getBoolean(key));
+ break;
+ case NULL:
+ break;
+ }
+ }
+ return builder.build();
+ }
+
+ public String cleanUPJson(String json) {
+ String cleanJson = StringUtils.replaceEach(json, new String[] {"\\\\", "\\\\\\", "\\\\\\\\"},
+ new String[] {"\\", "\\", "\\"});
+ cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\\\\\\"}, new String[] {"\\"});
+ cleanJson =
+ StringUtils.replaceEach(cleanJson, new String[] {"\\\\", "[[", "]]"}, new String[] {"\\", "[", "]"});
+
+ cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\\\\\"", "\\\"", "\"[{", "}]\""},
+ new String[] {"\"", "\"", "[{", "}]"});
+ cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\"[{", "}]\""}, new String[] {"[{", "}]"});
+ cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\"[", "]\""}, new String[] {"[", "]"});
+ cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\"{", "}\""}, new String[] {"{", "}"});
+ cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\"\"\"", "\"\""}, new String[] {"\"", "\""});
+ cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\\\""}, new String[] {""});
+ cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\"\""}, new String[] {"\""});
+ cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\"\\\\\\"}, new String[] {"\""});
+ cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\\\\\\\""}, new String[] {"\""});
+ cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\"[", "]\""}, new String[] {"[", "]"});
+ return cleanJson;
+ }
+
+ public JSONObject decodeContent(JsonNode jsonNode) {
+ Iterator<JsonNode> jsonElements = jsonNode.elements();
+ Iterator<String> jsonKeys = jsonNode.fieldNames();
+ Map<String, String> element = new TreeMap<>();
+ while (jsonElements.hasNext() && jsonKeys.hasNext()) {
+ element.put(jsonKeys.next(), jsonElements.next().toString());
+ }
+ JSONObject jsonResult = new JSONObject();
+ JSONArray jsonArray = null;
+ String oldValue = null;
+ String nodeKey = null;
+ String arryKey = null;
+ Boolean isArray = false;
+ JsonNodeFactory nodeFactory = JsonNodeFactory.instance;
+ ObjectNode node = nodeFactory.objectNode();
+ String prevKey = null;
+ String presKey;
+ for (Entry<String, String> entry : element.entrySet()) {
+ String key = entry.getKey();
+ String value = entry.getValue();
+ if (key.contains(".")) {
+ presKey = key.substring(0, key.indexOf('.'));
+ } else if (key.contains("@")) {
+ presKey = key.substring(0, key.indexOf('@'));
+ } else {
+ presKey = key;
+ }
+ // first check if we are different from old.
+ LOGGER.info(key + "\n");
+ if (jsonArray != null && jsonArray.length() > 0 && key.contains("@") && !key.contains(".")
+ && oldValue != null) {
+ if (!oldValue.equals(key.substring(0, key.indexOf('@')))) {
+ jsonResult.put(oldValue, jsonArray);
+ jsonArray = new JSONArray();
+ }
+ } else if (jsonArray != null && jsonArray.length() > 0 && !presKey.equals(prevKey) && oldValue != null) {
+ jsonResult.put(oldValue, jsonArray);
+ isArray = false;
+ jsonArray = new JSONArray();
+ }
+
+ prevKey = presKey;
+ //
+ if (key.contains(".")) {
+ if (nodeKey == null) {
+ nodeKey = key.substring(0, key.indexOf('.'));
+ }
+ if (nodeKey.equals(key.substring(0, key.indexOf('.')))) {
+ node.put(key.substring(key.indexOf('.') + 1), value);
+ } else {
+ if (node.size() != 0) {
+ if (nodeKey.contains("@")) {
+ if (arryKey == null) {
+ arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
+ }
+ if (nodeKey.endsWith("@0")) {
+ isArray = true;
+ jsonArray = new JSONArray();
+ }
+ if (jsonArray != null && arryKey.equals(nodeKey.substring(0, nodeKey.indexOf('@')))) {
+ jsonArray.put(decodeContent(node));
+ }
+ if ((key.contains("@") && !arryKey.equals(key.substring(0, nodeKey.indexOf('@'))))
+ || !key.contains("@")) {
+ jsonResult.put(arryKey, jsonArray);
+ jsonArray = new JSONArray();
+ }
+ arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
+ } else {
+ isArray = false;
+ jsonResult.put(nodeKey, decodeContent(node));
+ }
+ node = nodeFactory.objectNode();
+ }
+ nodeKey = key.substring(0, key.indexOf('.'));
+ if (nodeKey.contains("@")) {
+ arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
+ }
+ node.put(key.substring(key.indexOf('.') + 1), value);
+ }
+ } else {
+ if (node.size() != 0) {
+ if (nodeKey.contains("@")) {
+ if (arryKey == null) {
+ arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
+ }
+ if (nodeKey.endsWith("@0")) {
+ isArray = true;
+ jsonArray = new JSONArray();
+ }
+ if (jsonArray != null && arryKey.equals(nodeKey.substring(0, nodeKey.indexOf('@')))) {
+ jsonArray.put(decodeContent(node));
+ }
+ jsonResult.put(arryKey, jsonArray);
+ jsonArray = new JSONArray();
+ arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
+ } else {
+ isArray = false;
+ jsonResult.put(nodeKey, decodeContent(node));
+ }
+ node = nodeFactory.objectNode();
+ }
+ if (key.contains("@")) {
+ isArray = true;
+ if (key.endsWith("@0") || jsonArray == null) {
+ jsonArray = new JSONArray();
+ }
+ } else if (!key.contains("@")) {
+ isArray = false;
+ }
+ if (isArray) {
+ if (oldValue == null) {
+ oldValue = key.substring(0, key.indexOf('@'));
+ }
+ if (oldValue != prevKey) {
+ oldValue = key.substring(0, key.indexOf('@'));
+ }
+ if (oldValue.equals(key.substring(0, key.indexOf('@')))) {
+ jsonArray.put(value);
+ } else {
+ jsonResult.put(oldValue, jsonArray);
+ jsonArray = new JSONArray();
+ }
+ oldValue = key.substring(0, key.indexOf('@'));
+ } else {
+ jsonResult.put(key, value);
+ }
+ }
+ }
+ if (node.size() > 0) {
+ if (nodeKey.contains("@")) {
+ if (jsonArray == null) {
+ jsonArray = new JSONArray();
+ }
+ if (arryKey == null) {
+ arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
+ }
+ jsonArray.put(decodeContent(node));
+ jsonResult.put(arryKey, jsonArray);
+ isArray = false;
+ } else {
+ jsonResult.put(nodeKey, decodeContent(node));
+ }
+ }
+ if (isArray && jsonArray.length() > 0) {
+ jsonResult.put(oldValue, jsonArray);
+ }
+ return jsonResult;
+ }
+
+ @RequestMapping(value = {"/policyController/getDCAEMSTemplateData.htm"},
+ method = {org.springframework.web.bind.annotation.RequestMethod.POST})
+ public ModelAndView getDCAEMSTemplateData(HttpServletRequest request, HttpServletResponse response)
+ throws IOException {
+ // TreeSet is used to ensure that individual items appear before their containing collection.
+ allManyTrueKeys = new TreeSet<>();
+ ObjectMapper mapper = new ObjectMapper();
+ mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
+ JsonNode root = mapper.readTree(request.getReader());
+
+ String value = root.get("policyData").toString().replaceAll("^\"|\"$", "");
+ String servicename = value.split("-v")[0];
+ String version = null;
+ if (value.contains("-v")) {
+ version = value.split("-v")[1];
+ }
+ MicroServiceModels returnModel = getAttributeObject(servicename, version);
+
+ MicroserviceHeaderdeFaults returnHeaderDefauls = getHeaderDefaultsObject(value);
+ JSONObject jsonHdDefaultObj = null;
+ if (returnHeaderDefauls != null) {
+ jsonHdDefaultObj = new JSONObject();
+ jsonHdDefaultObj.put("onapName", returnHeaderDefauls.getOnapName());
+ jsonHdDefaultObj.put("guard", returnHeaderDefauls.getGuard());
+ jsonHdDefaultObj.put("riskLevel", returnHeaderDefauls.getRiskLevel());
+ jsonHdDefaultObj.put("riskType", returnHeaderDefauls.getRiskType());
+ jsonHdDefaultObj.put("priority", returnHeaderDefauls.getPriority());
+ }
+ String headDefautlsData = "";
+ if (jsonHdDefaultObj != null) {
+ headDefautlsData = jsonHdDefaultObj.toString();
+ } else {
+ headDefautlsData = "null";
+ }
+
+ // Get all keys with "MANY-true" defined in their value from subAttribute
+ Set<String> allkeys = null;
+ if (returnModel.getSub_attributes() != null && !returnModel.getSub_attributes().isEmpty()) {
+ JSONObject json = new JSONObject(returnModel.getSub_attributes());
+ getAllKeys(json);
+ allkeys = allManyTrueKeys;
+ allManyTrueKeys = new TreeSet<>();
+ LOGGER.info("allkeys : " + allkeys);
+ }
+
+ // Get element order info
+ String dataOrderInfo = returnModel.getDataOrderInfo();
+ if (dataOrderInfo != null && !dataOrderInfo.startsWith("\"")) {
+ dataOrderInfo = "\"" + dataOrderInfo + "\"";
+ }
+ LOGGER.info("dataOrderInfo : " + dataOrderInfo);
+
+ String allMnyTrueKeys = "";
+ if (allkeys != null) {
+ allMnyTrueKeys = allkeys.toString();
+ }
+
+ String jsonModel = createMicroSeriveJson(returnModel, allkeys);
+
+ JSONObject jsonObject = new JSONObject(jsonModel);
+
+ JSONObject finalJsonObject = null;
+ if (allkeys != null) {
+ Iterator<String> iter = allkeys.iterator();
+ while (iter.hasNext()) {
+ // Convert to array values for MANY-true keys
+ finalJsonObject = convertToArrayElement(jsonObject, iter.next());
+ jsonObject = finalJsonObject;
+ }
+ }
+
+ if (finalJsonObject != null) {
+ LOGGER.info(finalJsonObject.toString());
+ jsonModel = finalJsonObject.toString();
+ }
+
+ // get all properties with "MANY-true" defined in Ref_attributes
+ Set<String> manyTrueProperties = getManyTrueProperties(returnModel.getRef_attributes());
+ if (manyTrueProperties != null) {
+ JSONObject jsonObj = new JSONObject(jsonModel);
+ for (String s : manyTrueProperties) {
+ LOGGER.info(s);
+ // convert to array element for MANY-true properties
+ finalJsonObject = convertToArrayElement(jsonObj, s.trim());
+ jsonObj = finalJsonObject;
+ }
+
+ if (finalJsonObject != null) {
+ LOGGER.info(finalJsonObject.toString());
+ jsonModel = finalJsonObject.toString();
+ }
+ }
+
+ response.setCharacterEncoding("UTF-8");
+ response.setContentType("application / json");
+ request.setCharacterEncoding("UTF-8");
+ List<Object> list = new ArrayList<>();
+ PrintWriter out = response.getWriter();
+ String responseString = mapper.writeValueAsString(returnModel);
+
+ JSONObject j = null;
+
+ if ("".equals(allMnyTrueKeys)) {
+ j = new JSONObject("{dcaeModelData: " + responseString + ",jsonValue: " + jsonModel + ",dataOrderInfo:"
+ + dataOrderInfo + ",headDefautlsData:" + headDefautlsData + "}");
+ } else {
+ j = new JSONObject("{dcaeModelData: " + responseString + ",jsonValue: " + jsonModel + ",allManyTrueKeys: "
+ + allMnyTrueKeys + ",dataOrderInfo:" + dataOrderInfo + ",headDefautlsData:" + headDefautlsData
+ + "}");
+ }
+ list.add(j);
+ out.write(list.toString());
+ return null;
+ }
+
+ @SuppressWarnings({"unchecked", "rawtypes"})
+ private String createMicroSeriveJson(MicroServiceModels returnModel, Set<String> allkeys) {
+ Map<String, String> attributeMap = new HashMap<>();
+ Map<String, String> refAttributeMap = new HashMap<>();
+ String attribute = returnModel.getAttributes();
+ if (attribute != null) {
+ attribute = attribute.trim();
+ }
+ String refAttribute = returnModel.getRef_attributes();
+ if (refAttribute != null) {
+ refAttribute = refAttribute.trim();
+ }
+ String enumAttribute = returnModel.getEnumValues();
+ if (enumAttribute != null) {
+ enumAttribute = enumAttribute.trim();
+ }
+ if (!StringUtils.isEmpty(attribute)) {
+ attributeMap = convert(attribute, ",");
+ }
+ if (!StringUtils.isEmpty(refAttribute)) {
+ refAttributeMap = convert(refAttribute, ",");
+ }
+
+ Gson gson = new Gson();
+
+ String subAttributes = returnModel.getSub_attributes();
+ if (subAttributes != null) {
+ subAttributes = subAttributes.trim();
+ } else {
+ subAttributes = "";
+ }
+
+ Map gsonObject = (Map) gson.fromJson(subAttributes, Object.class);
+
+ JSONObject object = new JSONObject();
+ JSONArray array = new JSONArray();
+
+ for (Entry<String, String> keySet : attributeMap.entrySet()) {
+ array = new JSONArray();
+ String value = keySet.getValue();
+ if ("true".equalsIgnoreCase(keySet.getValue().split("MANY-")[1])) {
+ array.put(value);
+ object.put(keySet.getKey().trim(), array);
+ } else {
+ object.put(keySet.getKey().trim(), value.trim());
+ }
+ }
+
+ for (Entry<String, String> keySet : refAttributeMap.entrySet()) {
+ array = new JSONArray();
+ String value = keySet.getValue().split(":")[0];
+ if (gsonObject.containsKey(value)) {
+ if ("true".equalsIgnoreCase(keySet.getValue().split("MANY-")[1])) {
+ array.put(recursiveReference(value, gsonObject, enumAttribute));
+ object.put(keySet.getKey().trim(), array);
+ } else {
+ object.put(keySet.getKey().trim(), recursiveReference(value, gsonObject, enumAttribute));
+ }
+ } else {
+ if ("true".equalsIgnoreCase(keySet.getValue().split("MANY-")[1])) {
+ array.put(value.trim());
+ object.put(keySet.getKey().trim(), array);
+ } else {
+ object.put(keySet.getKey().trim(), value.trim());
+ }
+ }
+ }
+
+ return object.toString();
+ }
+
+ @SuppressWarnings("unchecked")
+ private JSONObject recursiveReference(String name, Map<String, String> subAttributeMap, String enumAttribute) {
+ JSONObject object = new JSONObject();
+ Map<String, String> map;
+ Object returnClass = subAttributeMap.get(name);
+ map = (Map<String, String>) returnClass;
+ JSONArray array;
+
+ for (Entry<String, String> m : map.entrySet()) {
+ String[] splitValue = m.getValue().split(":");
+ array = new JSONArray();
+ if (subAttributeMap.containsKey(splitValue[0])) {
+ if ("true".equalsIgnoreCase(m.getValue().split("MANY-")[1])) {
+ array.put(recursiveReference(splitValue[0], subAttributeMap, enumAttribute));
+ object.put(m.getKey().trim(), array);
+ } else {
+ object.put(m.getKey().trim(), recursiveReference(splitValue[0], subAttributeMap, enumAttribute));
+ }
+ } else {
+ if ("true".equalsIgnoreCase(m.getValue().split("MANY-")[1])) {
+ array.put(splitValue[0].trim());
+ object.put(m.getKey().trim(), array);
+ } else {
+ object.put(m.getKey().trim(), splitValue[0].trim());
+ }
+ }
+ }
+
+ return object;
+ }
+
+
+ public JSONObject convertToArrayElement(JSONObject json, String keyValue) {
+ return convertToArrayElement(json, new HashSet<>(), keyValue);
+ }
+
+ private JSONObject convertToArrayElement(JSONObject json, Set<String> keys, String keyValue) {
+ for (String key : json.keySet()) {
+ Object obj = json.get(key);
+ if (key.equals(keyValue.trim())) {
+ if (!(obj instanceof JSONArray)) {
+ JSONArray newJsonArray = new JSONArray();
+ newJsonArray.put(obj);
+ json.put(key, newJsonArray);
+ }
+ LOGGER.info("key : " + key);
+ LOGGER.info("obj : " + obj);
+ LOGGER.info("json.get(key) : " + json.get(key));
+ LOGGER.info("keyValue : " + keyValue);
+ keys.addAll(json.keySet());
+
+ return json;
+ }
+
+ if (obj instanceof JSONObject) {
+ convertToArrayElement(json.getJSONObject(key), keyValue);
+ }
+
+ if (obj instanceof JSONArray) {
+ convertToArrayElement(json.getJSONArray(key).getJSONObject(0), keyValue);
+ }
+ }
+
+ return json;
+ }
+
+ // call this method to get all MANY-true properties
+ public Set<String> getManyTrueProperties(String referAttributes) {
+ LOGGER.info("referAttributes : " + referAttributes);
+ Set<String> manyTrueProperties = new HashSet<>();
+
+ if (referAttributes != null) {
+ String[] referAarray = referAttributes.split(",");
+ String[] element = null;
+ for (int i = 0; i < referAarray.length; i++) {
+ element = referAarray[i].split("=");
+ if (element.length > 1 && element[1].contains("MANY-true")) {
+ manyTrueProperties.add(element[0]);
+ }
+ }
+ }
+
+ return manyTrueProperties;
+ }
+
+ // call this method to start the recursive
+ private Set<String> getAllKeys(JSONObject json) {
+ return getAllKeys(json, new HashSet<>());
+ }
+
+ private Set<String> getAllKeys(JSONArray arr) {
+ return getAllKeys(arr, new HashSet<>());
+ }
+
+ private Set<String> getAllKeys(JSONArray arr, Set<String> keys) {
+ for (int i = 0; i < arr.length(); i++) {
+ Object obj = arr.get(i);
+ if (obj instanceof JSONObject)
+ keys.addAll(getAllKeys(arr.getJSONObject(i)));
+ if (obj instanceof JSONArray)
+ keys.addAll(getAllKeys(arr.getJSONArray(i)));
+ }
+
+ return keys;
+ }