From e811e3e3ec134f2a5e2dcdfd896ddcbb08a5fa0f Mon Sep 17 00:00:00 2001
From: nivetha <nivetha.mariappan@tarento.com>
Date: Mon, 4 Apr 2022 17:04:13 +0530
Subject: [PATCH] Remove empty key value from data object

---
 .../formservice/utils/ValidationService.java  | 40 +++++++++++++++++++
 1 file changed, 40 insertions(+)

diff --git a/src/main/java/com/tarento/formservice/utils/ValidationService.java b/src/main/java/com/tarento/formservice/utils/ValidationService.java
index 1cefd0d..0678a00 100644
--- a/src/main/java/com/tarento/formservice/utils/ValidationService.java
+++ b/src/main/java/com/tarento/formservice/utils/ValidationService.java
@@ -2,11 +2,15 @@ package com.tarento.formservice.utils;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 
 import org.apache.commons.lang.StringUtils;
 import org.springframework.stereotype.Service;
 
 import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
 import com.tarento.formservice.model.IncomingData;
 import com.tarento.formservice.model.Status;
 import com.tarento.formservice.models.Field;
@@ -55,6 +59,10 @@ public class ValidationService {
 		}
 		if (incomingData.getDataObject() == null) {
 			return Constants.ResponseMessages.DATA_OBJECT_MISSING;
+		} else {
+			if (incomingData.getDataObject() instanceof Map) {
+				incomingData.setDataObject(removeEmptyFields(incomingData.getDataObject()));
+			}
 		}
 		return Constants.ResponseCodes.SUCCESS;
 	}
@@ -94,6 +102,10 @@ public class ValidationService {
 		}
 		if (incomingData.getDataObject() == null) {
 			return Constants.ResponseMessages.DATA_OBJECT_MISSING;
+		} else {
+			if (incomingData.getDataObject() instanceof Map) {
+				incomingData.setDataObject(removeEmptyFields(incomingData.getDataObject()));
+			}
 		}
 		if (incomingData.getInspectorSummaryDataObject() == null) {
 			return Constants.ResponseMessages.INSPECTOR_SUMMARY_MISSING;
@@ -101,6 +113,34 @@ public class ValidationService {
 		return Constants.ResponseCodes.SUCCESS;
 	}
 
+	/**
+	 * Iterates the data object and removes if any empty key or value present with
+	 * in the data object
+	 * 
+	 * @param dataObject
+	 *            Object
+	 * @return Object
+	 */
+	public Object removeEmptyFields(Object dataObject) {
+		try {
+			ConcurrentHashMap<String, Object> dataObjectMap = new ObjectMapper().convertValue(dataObject,
+					new TypeReference<ConcurrentHashMap<String, Object>>() {
+					});
+			for (Map.Entry<String, Object> entry : dataObjectMap.entrySet()) {
+				if (entry.getKey().equals(StringUtils.EMPTY) || (entry.getValue() instanceof String
+						&& (entry.getValue() == null || entry.getValue().equals(StringUtils.EMPTY)))) {
+					dataObjectMap.remove(entry.getKey());
+				} else if (entry.getValue() instanceof Map) {
+					entry.setValue(removeEmptyFields(entry.getValue()));
+				}
+			}
+			return dataObjectMap;
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+		return dataObject;
+	}
+
 	public String validateReturnedApplication(IncomingData incomingData) {
 		if (incomingData == null) {
 			return Constants.ResponseMessages.CHECK_REQUEST_PARAMS;
-- 
GitLab