- private static final Logger LOGGER = FlexLogger.getLogger(CreateOptimizationController.class);
- private static CommonClassDao commonClassDao;
-
- public static CommonClassDao getCommonClassDao() {
- return commonClassDao;
- }
-
- private OptimizationModels newModel;
- private String newFile;
- private String directory;
- private List<String> modelList = new ArrayList<>();
- private List<String> dirDependencyList = new ArrayList<>();
- private LinkedHashMap<String,MSAttributeObject > classMap = new LinkedHashMap<>();
- String referenceAttributes;
- String attributeString;
- Set<String> allManyTrueKeys= new HashSet <>();
-
- public static final String DATATYPE = "data_types.policy.data.";
- public static final String PROPERTIES=".properties.";
- public static final String TYPE=".type";
- public static final String STRING="string";
- public static final String INTEGER="integer";
- public static final String LIST="list";
- public static final String DEFAULT=".default";
- public static final String REQUIRED=".required";
- public static final String MATCHABLE=".matchable";
- public static final String MANYFALSE=":MANY-false";
- public static final String MODEL = "model";
- public static final String MANY = "MANY-";
- public static final String UTF8 = "UTF-8";
- public static final String MODELNAME = "modelName";
- public static final String APPLICATIONJSON = "application / json";
-
-
- @Autowired
- private CreateOptimizationController(CommonClassDao commonClassDao){
- setCommonClassDao(commonClassDao);
- }
-
- public static void setCommonClassDao(CommonClassDao commonClassDao) {
- CreateOptimizationController.commonClassDao = commonClassDao;
- }
-
- public CreateOptimizationController(){
- // Empty Constructor
- }
-
- protected PolicyRestAdapter policyAdapter = null;
- private Map<String, String> attributesListRefMap = new HashMap<>();
- private Map<String, LinkedList<String>> arrayTextList = new HashMap<>();
- CreateDcaeMicroServiceController msController = new CreateDcaeMicroServiceController();
-
- public PolicyRestAdapter setDataToPolicyRestAdapter(PolicyRestAdapter policyData, JsonNode root) {
- String jsonContent = null;
- try{
- LOGGER.info("policyJSON :" + (root.get("policyJSON")).toString());
-
- String tempJson = root.get("policyJSON").toString();
-
- //---replace empty value with the value below before calling decodeContent method.
- String dummyValue = "*empty-value*" + UUID.randomUUID().toString();
- LOGGER.info("dummyValue:" + dummyValue);
- tempJson = StringUtils.replaceEach(tempJson, new String[]{"\"\""}, new String[]{"\""+dummyValue+"\""});
- ObjectMapper mapper = new ObjectMapper();
- JsonNode tempJsonNode = mapper.readTree(tempJson);
- jsonContent = msController.decodeContent(tempJsonNode).toString();
- constructJson(policyData, jsonContent, dummyValue);
- }catch(Exception e){
- LOGGER.error("Error while decoding microservice content", e);
- }
-
- return policyData;
- }
-
- private PolicyRestAdapter constructJson(PolicyRestAdapter policyAdapter, String jsonContent, String dummyValue) {
- ObjectWriter om = new ObjectMapper().writer();
- String json="";
- OptimizationObject optimizationObject = setOptimizationObjectValues(policyAdapter);
-
- optimizationObject.setContent(jsonContent);
-
- try {
- json = om.writeValueAsString(optimizationObject);
- } catch (JsonProcessingException e) {
- LOGGER.error("Error writing out the object", e);
- }
- LOGGER.info("input json: " + json);
- LOGGER.info("input jsonContent: " + jsonContent);
- String cleanJson = msController.cleanUPJson(json);
-
- //--- reset empty value back after called cleanUPJson method and before calling removeNullAttributes
- String tempJson = StringUtils.replaceEach(cleanJson, new String[]{"\""+dummyValue+"\""}, new String[]{"\"\""});
- LOGGER.info("tempJson: " + tempJson);
- cleanJson = msController.removeNullAttributes(tempJson);
- policyAdapter.setJsonBody(cleanJson);
- return policyAdapter;
- }
-
- @RequestMapping(value={"/policyController/getOptimizationTemplateData.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
- public ModelAndView getOptimizationTemplateData(HttpServletRequest request, HttpServletResponse response) throws IOException{
- 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.toString().split("-v")[0];
- String version = null;
- if (value.toString().contains("-v")){
- version = value.toString().split("-v")[1];
- }
-
- OptimizationModels 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();
- LOGGER.info("returnHeaderDefauls headDefautlsData: " + headDefautlsData);
- }else{
- headDefautlsData = "null";
- }
-
- //Get all keys with "MANY-true" defined in their value from subAttribute
- Set<String> allkeys = null;
- if(returnModel.getSubattributes() != null && !returnModel.getSubattributes().isEmpty()){
- JSONObject json = new JSONObject(returnModel.getSubattributes());
- getAllKeys(json);
- allkeys = allManyTrueKeys;
- allManyTrueKeys = new HashSet <>();
- LOGGER.info("allkeys : " + allkeys);
- }
-
- //Get element order info
- String dataOrderInfo = returnModel.getDataOrderInfo();
- if(dataOrderInfo != null && !dataOrderInfo.startsWith("\"")){
- dataOrderInfo = "\"" + dataOrderInfo + "\"";
- }
-
- String nameOfTrueKeys = "";
- if(allkeys != null){
- nameOfTrueKeys = allkeys.toString();
- }
-
- String jsonModel = createOptimizationJson(returnModel);
-
- 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 = CreateDcaeMicroServiceController.convertToArrayElement(jsonObject, iter.next());
- }
- }
-
- if(finalJsonObject != null){
- LOGGER.info(finalJsonObject.toString());
- jsonModel = finalJsonObject.toString();
- }
-
- //get all properties with "MANY-true" defined in Ref_attributes
- Set<String> manyTrueProperties = CreateDcaeMicroServiceController.getManyTrueProperties(returnModel.getRefattributes());
- JSONObject jsonObj = new JSONObject(jsonModel);
- for (String s : manyTrueProperties) {
- LOGGER.info(s);
- //convert to array element for MANY-true properties
- finalJsonObject = CreateDcaeMicroServiceController.convertToArrayElement(jsonObj, s.trim());
- }
-
- if(finalJsonObject != null){
- LOGGER.info(finalJsonObject.toString());
- jsonModel = finalJsonObject.toString();
- }
-
- response.setCharacterEncoding(UTF8);
- response.setContentType(APPLICATIONJSON);
- request.setCharacterEncoding(UTF8);
- List<Object> list = new ArrayList<>();
- PrintWriter out = response.getWriter();
- String responseString = mapper.writeValueAsString(returnModel);
- JSONObject j = null;
- if("".equals(nameOfTrueKeys)){
- j = new JSONObject("{optimizationModelData: " + responseString + ",jsonValue: " + jsonModel + ",dataOrderInfo:" + dataOrderInfo + ",headDefautlsData:" + headDefautlsData +"}");
- }else{
- j = new JSONObject("{optimizationModelData: " + responseString + ",jsonValue: " + jsonModel + ",allManyTrueKeys: " + allManyTrueKeys+",dataOrderInfo:" + dataOrderInfo + ",headDefautlsData:" + headDefautlsData+ "}");
- }
- list.add(j);
- out.write(list.toString());
- return null;
- }
-
- @SuppressWarnings({ "rawtypes", "unchecked" })
- private String createOptimizationJson(OptimizationModels returnModel) {
- 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.getRefattributes();
- if(refAttribute != null){
- refAttribute = refAttribute.trim();
- }
-
- String enumAttribute = returnModel.getEnumValues();
- if(enumAttribute != null){
- enumAttribute = enumAttribute.trim();
- }
-
- if (!StringUtils.isEmpty(attribute)){
- attributeMap = CreateDcaeMicroServiceController.convert(attribute, ",");
- }
-
- if (!StringUtils.isEmpty(refAttribute)){
- refAttributeMap = CreateDcaeMicroServiceController.convert(refAttribute, ",");
- }
-
- Gson gson = new Gson();
-
- String subAttributes = returnModel.getSubattributes();
- if(subAttributes != null){
- subAttributes = subAttributes.trim();
- }else{
- subAttributes = "";
- }
-
- Map gsonObject = (Map) gson.fromJson(subAttributes, Object.class);
-
- JSONObject object = new JSONObject();
- JSONArray array;
-
- 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;
- }
-
- //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;
- }
-
+ private static final Logger LOGGER = FlexLogger.getLogger(CreateOptimizationController.class);
+
+ private static final int BUFFER = 2048;
+
+ private static CommonClassDao commonClassDao;
+
+ public static CommonClassDao getCommonClassDao() {
+ return commonClassDao;
+ }
+
+ private OptimizationModels newModel;
+ private String newFile;
+ private String directory;
+ private List<String> modelList = new ArrayList<>();
+ private List<String> dirDependencyList = new ArrayList<>();
+ private LinkedHashMap<String, MSAttributeObject> classMap = new LinkedHashMap<>();
+ String referenceAttributes;
+ String attributeString;
+ Set<String> allManyTrueKeys = new HashSet<>();
+
+ public static final String DATATYPE = "data_types.policy.data.";
+ public static final String PROPERTIES = ".properties.";
+ public static final String TYPE = ".type";
+ public static final String STRING = "string";
+ public static final String INTEGER = "integer";
+ public static final String LIST = "list";
+ public static final String DEFAULT = ".default";
+ public static final String REQUIRED = ".required";
+ public static final String MATCHABLE = ".matchable";
+ public static final String MANYFALSE = ":MANY-false";
+ public static final String MODEL = "model";
+ public static final String MANY = "MANY-";
+ public static final String MODELNAME = "modelName";
+
+ @Autowired
+ private CreateOptimizationController(CommonClassDao commonClassDao) {
+ setCommonClassDao(commonClassDao);
+ }
+
+ public static void setCommonClassDao(CommonClassDao commonClassDao) {
+ CreateOptimizationController.commonClassDao = commonClassDao;
+ }
+
+ public CreateOptimizationController() {
+ // Empty Constructor
+ }
+
+ protected PolicyRestAdapter policyAdapter = null;
+ private Map<String, String> attributesListRefMap = new HashMap<>();
+ private Map<String, LinkedList<String>> arrayTextList = new HashMap<>();
+ CreateDcaeMicroServiceController msController = new CreateDcaeMicroServiceController();
+
+ /**
+ * setDataToPolicyRestAdapter.
+ *
+ * @param policyData PolicyRestAdapter
+ * @param root JsonNode
+ * @return PolicyRestAdapter
+ */
+ public PolicyRestAdapter setDataToPolicyRestAdapter(PolicyRestAdapter policyData, JsonNode root) {
+ String jsonContent = null;
+ try {
+ LOGGER.info("policyJSON :" + (root.get("policyJSON")).toString());
+
+ String tempJson = root.get("policyJSON").toString();
+
+ // ---replace empty value with the value below before calling decodeContent method.
+ String dummyValue = "*empty-value*" + UUID.randomUUID().toString();
+ LOGGER.info("dummyValue:" + dummyValue);
+ tempJson =
+ StringUtils.replaceEach(tempJson, new String[] {"\"\""}, new String[] {"\"" + dummyValue + "\""});
+ ObjectMapper mapper = new ObjectMapper();
+ JsonNode tempJsonNode = mapper.readTree(tempJson);
+ jsonContent = msController.decodeContent(tempJsonNode).toString();
+ constructJson(policyData, jsonContent, dummyValue);
+ } catch (Exception e) {
+ LOGGER.error("Error while decoding microservice content", e);
+ }
+
+ return policyData;
+ }
+
+ private PolicyRestAdapter constructJson(PolicyRestAdapter policyAdapter, String jsonContent, String dummyValue) {
+ ObjectWriter om = new ObjectMapper().writer();
+ String json = "";
+ OptimizationObject optimizationObject = setOptimizationObjectValues(policyAdapter);
+
+ optimizationObject.setContent(jsonContent);
+
+ try {
+ json = om.writeValueAsString(optimizationObject);
+ } catch (JsonProcessingException e) {
+ LOGGER.error("Error writing out the object", e);
+ }
+ LOGGER.info("input json: " + json);
+ LOGGER.info("input jsonContent: " + jsonContent);
+ String cleanJson = msController.cleanUpJson(json);
+
+ // --- reset empty value back after called cleanUPJson method and before calling removeNullAttributes
+ String tempJson =
+ StringUtils.replaceEach(cleanJson, new String[] {"\"" + dummyValue + "\""}, new String[] {"\"\""});
+ LOGGER.info("tempJson: " + tempJson);
+ cleanJson = msController.removeNullAttributes(tempJson);
+ policyAdapter.setJsonBody(cleanJson);
+ return policyAdapter;
+ }
+
+ /**
+ * getOptimizationTemplateData.
+ *
+ * @param request HttpServletRequest
+ * @param response HttpServletResponse
+ * @return ModelAndView
+ * @throws IOException IOException
+ */
+ @RequestMapping(
+ value = {"/policyController/getOptimizationTemplateData.htm"},
+ method = {org.springframework.web.bind.annotation.RequestMethod.POST})
+ public ModelAndView getOptimizationTemplateData(HttpServletRequest request, HttpServletResponse response)
+ throws IOException {
+ CreateDcaeMicroServiceController controller = new CreateDcaeMicroServiceController();
+ 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];
+ }
+
+ OptimizationModels 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();
+ LOGGER.info("returnHeaderDefauls headDefautlsData: " + headDefautlsData);
+ } else {
+ headDefautlsData = "null";
+ }
+
+ // Get all keys with "MANY-true" defined in their value from subAttribute
+ Set<String> allkeys = null;
+ if (returnModel.getSubattributes() != null && !returnModel.getSubattributes().isEmpty()) {
+ getAllKeys(new JSONObject(returnModel.getSubattributes()));
+ allkeys = allManyTrueKeys;
+ allManyTrueKeys = new HashSet<>();
+ LOGGER.info("allkeys : " + allkeys);
+ }
+
+ // Get element order info
+ String dataOrderInfo = returnModel.getDataOrderInfo();
+ if (dataOrderInfo != null && !dataOrderInfo.startsWith("\"")) {
+ dataOrderInfo = "\"" + dataOrderInfo + "\"";
+ }
+
+ String nameOfTrueKeys = "";
+ if (allkeys != null) {
+ nameOfTrueKeys = allkeys.toString();
+ }
+
+ String jsonModel = createOptimizationJson(returnModel);
+
+ 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 = controller.convertToArrayElement(jsonObject, iter.next());
+ }
+ }
+
+ if (finalJsonObject != null) {
+ LOGGER.info(finalJsonObject.toString());
+ jsonModel = finalJsonObject.toString();
+ }
+
+ // get all properties with "MANY-true" defined in Ref_attributes
+ Set<String> manyTrueProperties = controller.getManyTrueProperties(returnModel.getRefattributes());
+ JSONObject jsonObj = new JSONObject(jsonModel);
+ for (String s : manyTrueProperties) {
+ LOGGER.info(s);
+ // convert to array element for MANY-true properties
+ finalJsonObject = controller.convertToArrayElement(jsonObj, s.trim());
+ }
+
+ if (finalJsonObject != null) {
+ LOGGER.info(finalJsonObject.toString());
+ jsonModel = finalJsonObject.toString();
+ }
+
+ response.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING);
+ response.setContentType(PolicyUtils.APPLICATION_JSON);
+ request.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING);
+ List<Object> list = new ArrayList<>();
+ String responseString = mapper.writeValueAsString(returnModel);
+ JSONObject json = null;
+ if ("".equals(nameOfTrueKeys)) {
+ json = new JSONObject("{optimizationModelData: " + responseString + ",jsonValue: " + jsonModel
+ + ",dataOrderInfo:" + dataOrderInfo + ",headDefautlsData:" + headDefautlsData + "}");
+ } else {
+ json = new JSONObject("{optimizationModelData: " + responseString + ",jsonValue: " + jsonModel
+ + ",allManyTrueKeys: " + allManyTrueKeys + ",dataOrderInfo:" + dataOrderInfo + ",headDefautlsData:"
+ + headDefautlsData + "}");
+ }
+ list.add(json);
+ response.getWriter().write(list.toString());
+ return null;
+ }
+
+ @SuppressWarnings({"rawtypes", "unchecked"})
+ private String createOptimizationJson(OptimizationModels returnModel) {
+ 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.getRefattributes();
+ if (refAttribute != null) {
+ refAttribute = refAttribute.trim();
+ }
+
+ String enumAttribute = returnModel.getEnumValues();
+ if (enumAttribute != null) {
+ enumAttribute = enumAttribute.trim();
+ }
+
+ CreateDcaeMicroServiceController controller = new CreateDcaeMicroServiceController();
+ if (!StringUtils.isEmpty(attribute)) {
+ attributeMap = controller.convert(attribute, ",");
+ }
+
+ if (!StringUtils.isEmpty(refAttribute)) {
+ refAttributeMap = controller.convert(refAttribute, ",");
+ }
+
+ Gson gson = new Gson();
+
+ String subAttributes = returnModel.getSubattributes();
+ if (subAttributes != null) {
+ subAttributes = subAttributes.trim();
+ } else {
+ subAttributes = "";
+ }
+
+ Map gsonObject = (Map) gson.fromJson(subAttributes, Object.class);
+
+ JSONObject object = new JSONObject();
+ JSONArray array;
+
+ 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;
+ }
+
+ // 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;
+ }
+