diff --git a/backend/src/main/java/com/realnet/Dashboard_builder/Controllers/ChartBuilder.java b/backend/src/main/java/com/realnet/Dashboard_builder/Controllers/ChartBuilder.java index c9f07f3..00723d9 100644 --- a/backend/src/main/java/com/realnet/Dashboard_builder/Controllers/ChartBuilder.java +++ b/backend/src/main/java/com/realnet/Dashboard_builder/Controllers/ChartBuilder.java @@ -8,14 +8,21 @@ import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import java.util.stream.Collectors; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -24,7 +31,8 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; -import com.realnet.Payment.Paytm.PaytmPayment; +import com.realnet.SureConnect.Entities.Sure_Connect; +import com.realnet.SureConnect.Service.SureService; import lombok.extern.slf4j.Slf4j; @@ -34,6 +42,9 @@ import lombok.extern.slf4j.Slf4j; @RestController public class ChartBuilder { + @Autowired + private SureService sureService; + public List> getAllDataFromTable(String tableName) { List> tableData = new ArrayList<>(); @@ -178,14 +189,15 @@ public class ChartBuilder { // } @GetMapping(value = "/getdashjson/{job_type}") - public ResponseEntity jsonretun2(@RequestParam String tableName, @PathVariable String job_type, - @RequestParam(required = false) String xAxis, @RequestParam(required = false) List yAxes) - throws IOException { + public ResponseEntity jsonretun2(@PathVariable String job_type, @RequestParam String tableName, + @RequestParam(required = false) String xAxis, @RequestParam(required = false) List yAxes, + @RequestParam Integer sureId, @RequestParam(required = false) String parameter, + @RequestParam(required = false) String parameterValue) throws IOException { StringBuilder jsonmap = new StringBuilder(); - if (job_type.equalsIgnoreCase("Grid View")) { - List> allData = getAllDataFromApi(tableName); + if (job_type.equalsIgnoreCase("Grid")) { + List> allData = getAllDataFromApi(tableName, sureId); jsonmap.append("[\n"); @@ -219,7 +231,7 @@ public class ChartBuilder { } if (job_type.equalsIgnoreCase("Todo List") && yAxes != null && !yAxes.isEmpty()) { - List> allData = getAllDataFromApi(tableName); + List> allData = getAllDataFromApi(tableName, sureId); String listName = yAxes.get(0); // Assuming the first column in yAxes to be the list name List listData = new ArrayList<>(); @@ -239,10 +251,19 @@ public class ChartBuilder { return new ResponseEntity<>(response, HttpStatus.CREATED); } - List> tableData = getAllDataFromApi(tableName); // Retrieve all data from the table + List> tableData = getAllDataFromApi(tableName, sureId); // Retrieve all data from the table + // โœ… Filter table data if parameter and parameterValue are provided + if (parameter != null && !parameter.trim().isEmpty() && parameterValue != null + && !parameterValue.trim().isEmpty()) { + tableData = tableData.stream().filter(row -> { + Object paramVal = row.get(parameter); + return paramVal != null && paramVal.toString().equalsIgnoreCase(parameterValue); + }).collect(Collectors.toList()); + } List> yAxisValuesList = new ArrayList<>(); List xAxisValues = new ArrayList<>(); + List parameterValues = new ArrayList<>(); // Initialize a list for each y-axis parameter for (int i = 0; i < yAxes.size(); i++) { @@ -254,7 +275,7 @@ public class ChartBuilder { String key = entry.getKey(); Object value = entry.getValue(); - if (key.equalsIgnoreCase(xAxis)) { + if (value != null && key.equalsIgnoreCase(xAxis)) { xAxisValues.add(value.toString()); } else { int yIndex = yAxes.indexOf(key); @@ -262,361 +283,576 @@ public class ChartBuilder { yAxisValuesList.get(yIndex).add(value); } } + } } - if (job_type.equalsIgnoreCase("Bar Chart")) { - jsonmap.append("{\n \"barChartData\": [\n"); + jsonmap = getJson(jsonmap, yAxes, xAxisValues, yAxisValuesList, parameterValues); - for (int j = 0; j < yAxes.size(); j++) { - String yAxis = yAxes.get(j); +// if (job_type.equalsIgnoreCase("Bar Chart") || job_type.equalsIgnoreCase("Bar")) { +// jsonmap.append("{\n \"barChartData\": [\n"); +// +// for (int j = 0; j < yAxes.size(); j++) { +// String yAxis = yAxes.get(j); +// +// jsonmap.append("{"); +// jsonmap.append("\"data\": ["); +// +// for (int i = 0; i < xAxisValues.size(); i++) { +//// Object yValue = yAxisValuesList.get(j).get(i); +// +// List list = yAxisValuesList.get(j); +// if (list.isEmpty()) { +// continue; +// +// } +// Object yValue = list.get(i); +// // โœ… Accept Integer, Long, Double, Float, or numeric Strings +// if (yValue instanceof Number) { +// jsonmap.append(yValue); +// } else if (yValue instanceof String) { +// String yStr = ((String) yValue).trim(); +// try { +// // Parse and append only if numeric +// Double num = Double.parseDouble(yStr); +// jsonmap.append(num); +// } catch (NumberFormatException e) { +// // not numeric โ€” skip +// continue; +// } +// } else { +// continue; // skip non-numeric values +// } +// +// if (i < xAxisValues.size() - 1) { +// jsonmap.append(","); +// } +// } +// +// // ๐Ÿงน Remove trailing comma if any +// int lastIndex = jsonmap.lastIndexOf(","); +// if (lastIndex == jsonmap.length() - 1) { +// jsonmap.deleteCharAt(lastIndex); +// } +// jsonmap.append("],"); +// jsonmap.append("\"label\": \"" + yAxis + "\""); +// jsonmap.append("}"); +// +// if (j < yAxes.size() - 1) { +// jsonmap.append(","); +// } +// } +// +// jsonmap.append("],\n \"barChartLabels\": [ "); +// +// for (String xValue : xAxisValues) { +// jsonmap.append("\"" + xValue + "\","); +// } +// +// if (!xAxisValues.isEmpty()) { +// jsonmap.deleteCharAt(jsonmap.lastIndexOf(",")); +// } +// +// jsonmap.append("] \n }\n"); +// } +// +// else if (job_type.equalsIgnoreCase("Line Chart") || job_type.equalsIgnoreCase("Line")) { +// jsonmap.append("{\n \"chartData\": [\n"); +// +// for (int j = 0; j < yAxes.size(); j++) { +// String yAxis = yAxes.get(j); +// +// jsonmap.append("{"); +// jsonmap.append("\"data\": ["); +// +// for (int i = 0; i < xAxisValues.size(); i++) { +// List list = yAxisValuesList.get(j); +// if (list.isEmpty()) { +// continue; +// +// } +// Object yValue = list.get(i); +// // โœ… Accept Integer, Long, Double, Float, or numeric Strings +// if (yValue instanceof Number) { +// jsonmap.append(yValue); +// } else if (yValue instanceof String) { +// String yStr = ((String) yValue).trim(); +// try { +// // Parse and append only if numeric +// Double num = Double.parseDouble(yStr); +// jsonmap.append(num); +// } catch (NumberFormatException e) { +// // not numeric โ€” skip +// continue; +// } +// } else { +// continue; // skip non-numeric values +// } +// +// if (i < xAxisValues.size() - 1) { +// jsonmap.append(","); +// } +// } +// +// // ๐Ÿงน Remove trailing comma if any +// int lastIndex = jsonmap.lastIndexOf(","); +// if (lastIndex == jsonmap.length() - 1) { +// jsonmap.deleteCharAt(lastIndex); +// } +// +// jsonmap.append("],"); +// jsonmap.append("\"label\": \"" + yAxis + "\""); +// jsonmap.append("}"); +// +// if (j < yAxes.size() - 1) { +// jsonmap.append(","); +// } +// } +// +// jsonmap.append("],\n \"chartLabels\": [ "); +// +// for (String xValue : xAxisValues) { +// jsonmap.append("\"" + xValue + "\","); +// } +// +// if (!xAxisValues.isEmpty()) { +// jsonmap.deleteCharAt(jsonmap.lastIndexOf(",")); +// } +// +// jsonmap.append("] \n }\n"); +// } else if (job_type.equalsIgnoreCase("Doughnut Chart") || job_type.equalsIgnoreCase("Doughnut")) { +// jsonmap.append("{\"chartData\": [\n"); +// +// for (int j = 0; j < yAxes.size(); j++) { +// String yAxis = yAxes.get(j); +// +// jsonmap.append("["); +// +// for (int i = 0; i < xAxisValues.size(); i++) { +// Object yValue = yAxisValuesList.get(j).get(i); +// jsonmap.append(yValue); +// +// if (i < xAxisValues.size() - 1) { +// jsonmap.append(","); +// } +// } +// +// jsonmap.append("]"); +// +// if (j < yAxes.size() - 1) { +// jsonmap.append(","); +// } +// } +// +// jsonmap.append("],\n \"chartLabels\": ["); +// +// for (String xValue : xAxisValues) { +// jsonmap.append("\"" + xValue + "\","); +// } +// +// if (!xAxisValues.isEmpty()) { +// jsonmap.deleteCharAt(jsonmap.lastIndexOf(",")); +// } +// +// jsonmap.append("]\n}"); +// } +// +// else if (job_type.equalsIgnoreCase("Radar Chart") || job_type.equalsIgnoreCase("Radar")) { +// jsonmap.append("{\n \"radarChartData\": [\n"); +// +// for (int j = 0; j < yAxes.size(); j++) { +// String yAxis = yAxes.get(j); +// +// jsonmap.append("{"); +// jsonmap.append("\"data\": ["); +// +// for (int i = 0; i < xAxisValues.size(); i++) { +// Object yValue = yAxisValuesList.get(j).get(i); +// jsonmap.append(yValue); +// +// if (i < xAxisValues.size() - 1) { +// jsonmap.append(","); +// } +// } +// +// jsonmap.append("],"); +// jsonmap.append("\"label\": \"" + yAxis + "\""); +// jsonmap.append("}"); +// +// if (j < yAxes.size() - 1) { +// jsonmap.append(","); +// } +// } +// +// jsonmap.append("],\n \"radarChartLabels\": [ "); +// +// for (String xValue : xAxisValues) { +// jsonmap.append("\"" + xValue + "\","); +// } +// +// if (!xAxisValues.isEmpty()) { +// jsonmap.deleteCharAt(jsonmap.lastIndexOf(",")); +// } +// +// jsonmap.append("] \n }\n"); +// } +// +// else if (job_type.equalsIgnoreCase("PolarArea Chart") || job_type.equalsIgnoreCase("PolarArea")) { +// jsonmap.append("{\n \"polarAreaChartData\": [\n"); +// +// for (int j = 0; j < yAxes.size(); j++) { +// String yAxis = yAxes.get(j); +// +// jsonmap.append("{"); +// jsonmap.append("\"data\": ["); +// +// for (int i = 0; i < xAxisValues.size(); i++) { +// Object yValue = yAxisValuesList.get(j).get(i); +// jsonmap.append(yValue); +// +// if (i < xAxisValues.size() - 1) { +// jsonmap.append(","); +// } +// } +// +// jsonmap.append("],"); +// jsonmap.append("\"label\": \"" + yAxis + "\""); +// jsonmap.append("}"); +// +// if (j < yAxes.size() - 1) { +// jsonmap.append(","); +// } +// } +// +// jsonmap.append("],\n \"polarAreaChartLabels\": [ "); +// +// for (String xValue : xAxisValues) { +// jsonmap.append("\"" + xValue + "\","); +// } +// +// if (!xAxisValues.isEmpty()) { +// jsonmap.deleteCharAt(jsonmap.lastIndexOf(",")); +// } +// +// jsonmap.append("] \n }\n"); +// } +// +// if (job_type.equalsIgnoreCase("Pie Chart") || job_type.equalsIgnoreCase("Pie")) { +// jsonmap.append("{\n \"pieChartData\": ["); +// +// for (int i = 0; i < yAxisValuesList.get(0).size(); i++) { // Assuming "mark" is the first item in yAxes +// jsonmap.append(yAxisValuesList.get(0).get(i)); // Use the y-axis values +// +// if (i < yAxisValuesList.get(0).size() - 1) { +// jsonmap.append(","); +// } +// } +// +// jsonmap.append("],\n \"pieChartLabels\": [ "); +// +// for (int i = 0; i < xAxisValues.size(); i++) { // Assuming "name" is the x-axis +// jsonmap.append("\"" + xAxisValues.get(i) + "\""); // Use the x-axis values +// +// if (i < xAxisValues.size() - 1) { +// jsonmap.append(","); +// } +// } +// +// jsonmap.append("] \n }\n"); +// } +// +// else if (job_type.equalsIgnoreCase("Bubble Chart") || job_type.equalsIgnoreCase("Bubble")) { +// jsonmap.append("{\n \"bubbleChartData\": [\n"); +// +// for (int j = 0; j < yAxes.size(); j++) { +// String yAxis = yAxes.get(j); +// +// jsonmap.append("{"); +// jsonmap.append("\"data\": ["); +// +// for (int i = 0; i < xAxisValues.size(); i++) { +// Object xValue = xAxisValues.get(i); +// Object yValue = yAxisValuesList.get(j).get(i); +// int radius = 5 + (i % 3) * 3; // Adjust the radius as needed +// +// jsonmap.append("{ \"x\": " + xValue + ", \"y\": " + yValue + ", \"r\": " + radius + "}"); +// +// if (i < xAxisValues.size() - 1) { +// jsonmap.append(","); +// } +// } +// +// jsonmap.append("],"); +// jsonmap.append("\"label\": \"" + yAxis + "\""); +// jsonmap.append("}"); +// +// if (j < yAxes.size() - 1) { +// jsonmap.append(","); +// } +// } +// +// jsonmap.append("],\n \"bubbleChartLabels\": [ "); +// +// for (String label : xAxisValues) { +// jsonmap.append("\"" + label + "\","); +// } +// +// if (!xAxisValues.isEmpty()) { +// jsonmap.deleteCharAt(jsonmap.lastIndexOf(",")); +// } +// +// jsonmap.append("] \n }\n"); +// } else if (job_type.equalsIgnoreCase("Scatter Chart") || job_type.equalsIgnoreCase("Scatter")) { +// jsonmap.append("{\n \"scatterChartData\": [\n"); +// +// for (int j = 0; j < yAxes.size(); j++) { +// String yAxis = yAxes.get(j); +// +// jsonmap.append("{"); +// jsonmap.append("\"data\": ["); +// +// for (int i = 0; i < xAxisValues.size(); i++) { +// Object xValue = xAxisValues.get(i); +// Object yValue = yAxisValuesList.get(j).get(i); +// +// jsonmap.append("{ \"x\": " + xValue + ", \"y\": " + yValue + "}"); +// +// if (i < xAxisValues.size() - 1) { +// jsonmap.append(","); +// } +// } +// +// jsonmap.append("],"); +// jsonmap.append("\"label\": \"" + yAxis + "\""); +// jsonmap.append("}"); +// +// if (j < yAxes.size() - 1) { +// jsonmap.append(","); +// } +// } +// +// jsonmap.append("],\n \"scatterChartLabels\": [ "); +// +// for (String label : xAxisValues) { +// jsonmap.append("\"" + label + "\","); +// } +// +// if (!xAxisValues.isEmpty()) { +// jsonmap.deleteCharAt(jsonmap.lastIndexOf(",")); +// } +// +// jsonmap.append("] \n }\n"); +// } +// +// else if (job_type.equalsIgnoreCase("Dynamic Chart") || job_type.equalsIgnoreCase("Dynamic")) { +// jsonmap.append("{\n \"dynamicChartData\": [\n"); +// +// for (int j = 0; j < yAxes.size(); j++) { +// String yAxis = yAxes.get(j); +// +// jsonmap.append("{"); +// jsonmap.append("\"data\": ["); +// +// for (int i = 0; i < xAxisValues.size(); i++) { +// Object yValue = yAxisValuesList.get(j).get(i); +// jsonmap.append(yValue); +// +// if (i < xAxisValues.size() - 1) { +// jsonmap.append(","); +// } +// } +// +// jsonmap.append("],"); +// jsonmap.append("\"label\": \"" + yAxis + "\""); +// jsonmap.append("}"); +// +// if (j < yAxes.size() - 1) { +// jsonmap.append(","); +// } +// } +// +// jsonmap.append("],\n \"dynamicChartLabels\": [ "); +// +// for (String xValue : xAxisValues) { +// jsonmap.append("\"" + xValue + "\","); +// } +// +// if (!xAxisValues.isEmpty()) { +// jsonmap.deleteCharAt(jsonmap.lastIndexOf(",")); +// } +// +// jsonmap.append("] \n }\n"); +// } - jsonmap.append("{"); - jsonmap.append("\"data\": ["); + return new ResponseEntity<>(jsonmap.toString(), HttpStatus.CREATED); + } - for (int i = 0; i < xAxisValues.size(); i++) { - Object yValue = yAxisValuesList.get(j).get(i); + public StringBuilder getJson(StringBuilder jsonmap, List yAxes, List xAxisValues, + List> yAxisValuesList, List parameterValues) { + + jsonmap.append("{\n \"chartData\": [\n"); + + for (int j = 0; j < yAxes.size(); j++) { + String yAxis = yAxes.get(j); + + jsonmap.append("{"); + jsonmap.append("\"data\": ["); + + // --- Y-Axis Data --- + for (int i = 0; i < xAxisValues.size(); i++) { + List list = yAxisValuesList.get(j); + if (list == null || list.isEmpty() || i >= list.size()) { + continue; + } + + Object yValue = list.get(i); + if (yValue instanceof Number) { jsonmap.append(yValue); - - if (i < xAxisValues.size() - 1) { - jsonmap.append(","); + } else if (yValue instanceof String) { + String yStr = ((String) yValue).trim(); + try { + Double num = Double.parseDouble(yStr); + jsonmap.append(num); + } catch (NumberFormatException e) { + continue; // skip non-numeric } + } else { + continue; // skip invalid } - jsonmap.append("],"); - jsonmap.append("\"label\": \"" + yAxis + "\""); - jsonmap.append("}"); - - if (j < yAxes.size() - 1) { - jsonmap.append(","); - } - } - - jsonmap.append("],\n \"barChartLabels\": [ "); - - for (String xValue : xAxisValues) { - jsonmap.append("\"" + xValue + "\","); - } - - if (!xAxisValues.isEmpty()) { - jsonmap.deleteCharAt(jsonmap.lastIndexOf(",")); - } - - jsonmap.append("] \n }\n"); - } - - else if (job_type.equalsIgnoreCase("Line Chart")) { - jsonmap.append("{\n \"chartData\": [\n"); - - for (int j = 0; j < yAxes.size(); j++) { - String yAxis = yAxes.get(j); - - jsonmap.append("{"); - jsonmap.append("\"data\": ["); - - for (int i = 0; i < xAxisValues.size(); i++) { - Object yValue = yAxisValuesList.get(j).get(i); - jsonmap.append(yValue); - - if (i < xAxisValues.size() - 1) { - jsonmap.append(","); - } - } - - jsonmap.append("],"); - jsonmap.append("\"label\": \"" + yAxis + "\""); - jsonmap.append("}"); - - if (j < yAxes.size() - 1) { - jsonmap.append(","); - } - } - - jsonmap.append("],\n \"chartLabels\": [ "); - - for (String xValue : xAxisValues) { - jsonmap.append("\"" + xValue + "\","); - } - - if (!xAxisValues.isEmpty()) { - jsonmap.deleteCharAt(jsonmap.lastIndexOf(",")); - } - - jsonmap.append("] \n }\n"); - } else if (job_type.equalsIgnoreCase("Doughnut Chart")) { - jsonmap.append("{\"chartData\": [\n"); - - for (int j = 0; j < yAxes.size(); j++) { - String yAxis = yAxes.get(j); - - jsonmap.append("["); - - for (int i = 0; i < xAxisValues.size(); i++) { - Object yValue = yAxisValuesList.get(j).get(i); - jsonmap.append(yValue); - - if (i < xAxisValues.size() - 1) { - jsonmap.append(","); - } - } - - jsonmap.append("]"); - - if (j < yAxes.size() - 1) { - jsonmap.append(","); - } - } - - jsonmap.append("],\n \"chartLabels\": ["); - - for (String xValue : xAxisValues) { - jsonmap.append("\"" + xValue + "\","); - } - - if (!xAxisValues.isEmpty()) { - jsonmap.deleteCharAt(jsonmap.lastIndexOf(",")); - } - - jsonmap.append("]\n}"); - } - - else if (job_type.equalsIgnoreCase("Radar Chart")) { - jsonmap.append("{\n \"radarChartData\": [\n"); - - for (int j = 0; j < yAxes.size(); j++) { - String yAxis = yAxes.get(j); - - jsonmap.append("{"); - jsonmap.append("\"data\": ["); - - for (int i = 0; i < xAxisValues.size(); i++) { - Object yValue = yAxisValuesList.get(j).get(i); - jsonmap.append(yValue); - - if (i < xAxisValues.size() - 1) { - jsonmap.append(","); - } - } - - jsonmap.append("],"); - jsonmap.append("\"label\": \"" + yAxis + "\""); - jsonmap.append("}"); - - if (j < yAxes.size() - 1) { - jsonmap.append(","); - } - } - - jsonmap.append("],\n \"radarChartLabels\": [ "); - - for (String xValue : xAxisValues) { - jsonmap.append("\"" + xValue + "\","); - } - - if (!xAxisValues.isEmpty()) { - jsonmap.deleteCharAt(jsonmap.lastIndexOf(",")); - } - - jsonmap.append("] \n }\n"); - } - - else if (job_type.equalsIgnoreCase("PolarArea Chart")) { - jsonmap.append("{\n \"polarAreaChartData\": [\n"); - - for (int j = 0; j < yAxes.size(); j++) { - String yAxis = yAxes.get(j); - - jsonmap.append("{"); - jsonmap.append("\"data\": ["); - - for (int i = 0; i < xAxisValues.size(); i++) { - Object yValue = yAxisValuesList.get(j).get(i); - jsonmap.append(yValue); - - if (i < xAxisValues.size() - 1) { - jsonmap.append(","); - } - } - - jsonmap.append("],"); - jsonmap.append("\"label\": \"" + yAxis + "\""); - jsonmap.append("}"); - - if (j < yAxes.size() - 1) { - jsonmap.append(","); - } - } - - jsonmap.append("],\n \"polarAreaChartLabels\": [ "); - - for (String xValue : xAxisValues) { - jsonmap.append("\"" + xValue + "\","); - } - - if (!xAxisValues.isEmpty()) { - jsonmap.deleteCharAt(jsonmap.lastIndexOf(",")); - } - - jsonmap.append("] \n }\n"); - } - - if (job_type.equalsIgnoreCase("Pie Chart")) { - jsonmap.append("{\n \"pieChartData\": ["); - - for (int i = 0; i < yAxisValuesList.get(0).size(); i++) { // Assuming "mark" is the first item in yAxes - jsonmap.append(yAxisValuesList.get(0).get(i)); // Use the y-axis values - - if (i < yAxisValuesList.get(0).size() - 1) { - jsonmap.append(","); - } - } - - jsonmap.append("],\n \"pieChartLabels\": [ "); - - for (int i = 0; i < xAxisValues.size(); i++) { // Assuming "name" is the x-axis - jsonmap.append("\"" + xAxisValues.get(i) + "\""); // Use the x-axis values - if (i < xAxisValues.size() - 1) { jsonmap.append(","); } } - jsonmap.append("] \n }\n"); + // ๐Ÿงน Remove trailing comma + int lastIndex = jsonmap.lastIndexOf(","); + if (lastIndex == jsonmap.length() - 1) { + jsonmap.deleteCharAt(lastIndex); + } + + jsonmap.append("],"); + jsonmap.append("\"label\": \"" + yAxis + "\""); + jsonmap.append("}"); + + if (j < yAxes.size() - 1) { + jsonmap.append(","); + } } - else if (job_type.equalsIgnoreCase("Bubble Chart")) { - jsonmap.append("{\n \"bubbleChartData\": [\n"); - - for (int j = 0; j < yAxes.size(); j++) { - String yAxis = yAxes.get(j); - - jsonmap.append("{"); - jsonmap.append("\"data\": ["); - - for (int i = 0; i < xAxisValues.size(); i++) { - Object xValue = xAxisValues.get(i); - Object yValue = yAxisValuesList.get(j).get(i); - int radius = 5 + (i % 3) * 3; // Adjust the radius as needed - - jsonmap.append("{ \"x\": " + xValue + ", \"y\": " + yValue + ", \"r\": " + radius + "}"); - - if (i < xAxisValues.size() - 1) { - jsonmap.append(","); - } - } - - jsonmap.append("],"); - jsonmap.append("\"label\": \"" + yAxis + "\""); - jsonmap.append("}"); - - if (j < yAxes.size() - 1) { - jsonmap.append(","); - } - } - - jsonmap.append("],\n \"bubbleChartLabels\": [ "); - - for (String label : xAxisValues) { - jsonmap.append("\"" + label + "\","); - } - - if (!xAxisValues.isEmpty()) { - jsonmap.deleteCharAt(jsonmap.lastIndexOf(",")); - } - - jsonmap.append("] \n }\n"); - } else if (job_type.equalsIgnoreCase("Scatter Chart")) { - jsonmap.append("{\n \"scatterChartData\": [\n"); - - for (int j = 0; j < yAxes.size(); j++) { - String yAxis = yAxes.get(j); - - jsonmap.append("{"); - jsonmap.append("\"data\": ["); - - for (int i = 0; i < xAxisValues.size(); i++) { - Object xValue = xAxisValues.get(i); - Object yValue = yAxisValuesList.get(j).get(i); - - jsonmap.append("{ \"x\": " + xValue + ", \"y\": " + yValue + "}"); - - if (i < xAxisValues.size() - 1) { - jsonmap.append(","); - } - } - - jsonmap.append("],"); - jsonmap.append("\"label\": \"" + yAxis + "\""); - jsonmap.append("}"); - - if (j < yAxes.size() - 1) { - jsonmap.append(","); - } - } - - jsonmap.append("],\n \"scatterChartLabels\": [ "); - - for (String label : xAxisValues) { - jsonmap.append("\"" + label + "\","); - } - - if (!xAxisValues.isEmpty()) { - jsonmap.deleteCharAt(jsonmap.lastIndexOf(",")); - } - - jsonmap.append("] \n }\n"); + // --- Chart Labels (X-Axis) --- + jsonmap.append("],\n \"chartLabels\": ["); + for (String xValue : xAxisValues) { + jsonmap.append("\"").append(xValue).append("\","); } - else if (job_type.equalsIgnoreCase("Dynamic Chart")) { - jsonmap.append("{\n \"dynamicChartData\": [\n"); - - for (int j = 0; j < yAxes.size(); j++) { - String yAxis = yAxes.get(j); - - jsonmap.append("{"); - jsonmap.append("\"data\": ["); - - for (int i = 0; i < xAxisValues.size(); i++) { - Object yValue = yAxisValuesList.get(j).get(i); - jsonmap.append(yValue); - - if (i < xAxisValues.size() - 1) { - jsonmap.append(","); - } - } - - jsonmap.append("],"); - jsonmap.append("\"label\": \"" + yAxis + "\""); - jsonmap.append("}"); - - if (j < yAxes.size() - 1) { - jsonmap.append(","); - } - } - - jsonmap.append("],\n \"dynamicChartLabels\": [ "); - - for (String xValue : xAxisValues) { - jsonmap.append("\"" + xValue + "\","); - } - - if (!xAxisValues.isEmpty()) { - jsonmap.deleteCharAt(jsonmap.lastIndexOf(",")); - } - - jsonmap.append("] \n }\n"); + if (!xAxisValues.isEmpty()) { + jsonmap.deleteCharAt(jsonmap.lastIndexOf(",")); } + jsonmap.append("],"); - return new ResponseEntity<>(jsonmap.toString(), HttpStatus.CREATED); + // --- Parameter Values --- + jsonmap.append("\n \"parameterValues\": ["); + if (parameterValues != null && !parameterValues.isEmpty()) { + for (String param : parameterValues) { + if (param != null && !param.trim().isEmpty()) { + jsonmap.append("\"").append(param.trim()).append("\","); + } + } + // Remove trailing comma + int lastComma = jsonmap.lastIndexOf(","); + if (lastComma == jsonmap.length() - 1) { + jsonmap.deleteCharAt(lastComma); + } + } + jsonmap.append("]\n}"); + + return jsonmap; } -// @GetMapping("/getKey") - public List> getAllKeyFromApi(String apiUrl) { +// get json data +// public StringBuilder getJson(StringBuilder jsonmap, List yAxes, List xAxisValues, +// List> yAxisValuesList, List parameterValues) { +// jsonmap.append("{\n \"chartData\": [\n"); +// +// for (int j = 0; j < yAxes.size(); j++) { +// String yAxis = yAxes.get(j); +// +// jsonmap.append("{"); +// jsonmap.append("\"data\": ["); +// +// for (int i = 0; i < xAxisValues.size(); i++) { +// List list = yAxisValuesList.get(j); +// if (list.isEmpty()) { +// continue; +// +// } +// Object yValue = list.get(i); +// // โœ… Accept Integer, Long, Double, Float, or numeric Strings +// if (yValue instanceof Number) { +// jsonmap.append(yValue); +// } else if (yValue instanceof String) { +// String yStr = ((String) yValue).trim(); +// try { +// // Parse and append only if numeric +// Double num = Double.parseDouble(yStr); +// jsonmap.append(num); +// } catch (NumberFormatException e) { +// // not numeric โ€” skip +// continue; +// } +// } else { +// continue; // skip non-numeric values +// } +// +// if (i < xAxisValues.size() - 1) { +// jsonmap.append(","); +// } +// } +// +// // ๐Ÿงน Remove trailing comma if any +// int lastIndex = jsonmap.lastIndexOf(","); +// if (lastIndex == jsonmap.length() - 1) { +// jsonmap.deleteCharAt(lastIndex); +// } +// +// jsonmap.append("],"); +// jsonmap.append("\"label\": \"" + yAxis + "\""); +// jsonmap.append("}"); +// +// if (j < yAxes.size() - 1) { +// jsonmap.append(","); +// } +// } +// +// jsonmap.append("],\n \"chartLabels\": [ "); +// +// for (String xValue : xAxisValues) { +// jsonmap.append("\"" + xValue + "\","); +// } +// +// if (!xAxisValues.isEmpty()) { +// jsonmap.deleteCharAt(jsonmap.lastIndexOf(",")); +// } +// +// jsonmap.append("] \n }\n"); +// +// return jsonmap; +// } +// + + @GetMapping("/getAllKeys") + public Set getAllKeys(@RequestParam String apiUrl, @RequestParam Integer sureId) { + List> apiData = getAllKeyFromApi(apiUrl, sureId); + return getAllKeys(apiData); + } + + public List> getAllKeyFromApi(String apiUrl, Integer sureId) { List> apiData = new ArrayList<>(); try { // Make a GET request using the provided URL - ResponseEntity responseEntity = GET(apiUrl); + ResponseEntity responseEntity = GETWithObject(apiUrl, sureId); // Convert the response to a List> if (responseEntity.getBody() instanceof List) { @@ -636,12 +872,6 @@ public class ChartBuilder { return apiData; } - @GetMapping("/getAllKeys") - public Set getAllKeys(@RequestParam String apiUrl) { - List> apiData = getAllKeyFromApi(apiUrl); - return getAllKeys(apiData); - } - private Set getAllKeys(List> apiData) { Set allKeys = new HashSet<>(); @@ -652,12 +882,12 @@ public class ChartBuilder { return allKeys; } - public List> getAllDataFromApi(String apiUrl) { + public List> getAllDataFromApi(String apiUrl, Integer sureId) { List> apiData = new ArrayList<>(); try { // Make a GET request using the provided URL - ResponseEntity responseEntity = GET(apiUrl); + ResponseEntity responseEntity = GETWithObject(apiUrl, sureId); // Convert the response to a List> if (responseEntity.getBody() instanceof List) { @@ -678,7 +908,7 @@ public class ChartBuilder { return apiData; } - public ResponseEntity GET(String get) { + public ResponseEntity GET1(String get) { RestTemplate restTemplate = new RestTemplate(); ResponseEntity u = restTemplate.getForEntity(get, Object.class); @@ -686,4 +916,52 @@ public class ChartBuilder { return u; } + + public ResponseEntity GET(String url, Integer sureid) { + RestTemplate restTemplate = new RestTemplate(); + + HttpHeaders headers = new HttpHeaders(); + headers.setAccept(Collections.singletonList(MediaType.ALL)); // accept JSON or XML + headers.setContentType(MediaType.APPLICATION_JSON); + + // ๐Ÿ”น Add your token here (you can make it dynamic) + String token = getToken(sureid); // helper method (see below) + if (token != null && !token.isEmpty()) { + headers.set("Authorization", "Bearer " + token); + } + + HttpEntity entity = new HttpEntity<>(headers); + + ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); + + return response; + } + + public ResponseEntity GETWithObject(String url, Integer sureid) { + RestTemplate restTemplate = new RestTemplate(); + + HttpHeaders headers = new HttpHeaders(); + headers.setAccept(Collections.singletonList(MediaType.ALL)); // accept JSON or XML + headers.setContentType(MediaType.APPLICATION_JSON); + + // ๐Ÿ”น Add your token here (you can make it dynamic) + String token = getToken(sureid); // helper method (see below) + if (token != null && !token.isEmpty()) { + headers.set("Authorization", "Bearer " + token); + } + + HttpEntity entity = new HttpEntity<>(headers); + + ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET, entity, Object.class); + + return response; + } + + private String getToken(Integer sureid) { + Sure_Connect connect = sureService.getbyid(sureid); + String access_token = connect.getAccess_token(); + + return access_token; // optional + } + } diff --git a/backend/src/main/java/com/realnet/DataLake/Controllers/Data_lakeController.java b/backend/src/main/java/com/realnet/DataLake/Controllers/Data_lakeController.java new file mode 100644 index 0000000..4d7eaad --- /dev/null +++ b/backend/src/main/java/com/realnet/DataLake/Controllers/Data_lakeController.java @@ -0,0 +1,124 @@ +package com.realnet.DataLake.Controllers; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.realnet.DataLake.Entity.Data_lake; +import com.realnet.DataLake.Services.Data_lakeService; +import com.realnet.fnd.response.EntityResponse; + +@RequestMapping(value = "/Data_lake") +@CrossOrigin("*") +@RestController +public class Data_lakeController { + @Autowired + private Data_lakeService Service; + + @Value("${projectPath}") + private String projectPath; + + @PostMapping("/Data_lake") + public Data_lake Savedata(@RequestBody Data_lake data) { + Data_lake save = Service.Savedata(data); + + System.out.println("data saved..." + save); + + return save; + } + + @PutMapping("/Data_lake/{id}") + public Data_lake update(@RequestBody Data_lake data, @PathVariable Integer id) { + Data_lake update = Service.update(data, id); + System.out.println("data update..." + update); + return update; + } + +// get all with pagination + @GetMapping("/Data_lake/getall/page") + public Page getall(@RequestParam(value = "page", required = false) Integer page, + @RequestParam(value = "size", required = false) Integer size) { + Pageable paging = PageRequest.of(page, size); + Page get = Service.getAllWithPagination(paging); + + return get; + + } + + @GetMapping("/Data_lake") + public List getdetails() { + List get = Service.getdetails(); + return get; + } +// get all without authentication + + @GetMapping("/token/Data_lake") + public List getallwioutsec() { + List get = Service.getdetails(); + return get; + } + + @GetMapping("/Data_lake/{id}") + public Data_lake getdetailsbyId(@PathVariable Integer id) { + Data_lake get = Service.getdetailsbyId(id); + return get; + } + + @DeleteMapping("/Data_lake/{id}") + public ResponseEntity delete_by_id(@PathVariable Integer id) { + Service.delete_by_id(id); + return new ResponseEntity<>(new EntityResponse("Deleted"), HttpStatus.OK); + + } + +// json Updtaed + + @PutMapping("/Data_lake/json/{id}") + public Data_lake update(@PathVariable Integer id) throws JsonProcessingException { + Data_lake update = Service.Updatejson(id); + System.out.println("json update..." + update); + return update; + } + + @GetMapping("/Data_lake/merge/{id}") + public ResponseEntity mergeBatchData(@PathVariable Integer id) { + try { + // Get merged JSON string from service + String mergedJson = Service.mergeBatchData(id); + + // Parse the merged JSON string back into JSON structure + ObjectMapper mapper = new ObjectMapper(); + JsonNode jsonNode = mapper.readTree(mergedJson); + + // Return as actual JSON (not as string) + return ResponseEntity.ok(jsonNode); + + } catch (Exception e) { + // Build error response manually (Map.of() not available in Java 8) + Map error = new HashMap<>(); + error.put("error", e.getMessage()); + return ResponseEntity.status(500).body(error); + } + } +} \ No newline at end of file diff --git a/backend/src/main/java/com/realnet/DataLake/Controllers/XmlApiExample.java b/backend/src/main/java/com/realnet/DataLake/Controllers/XmlApiExample.java new file mode 100644 index 0000000..19e5b09 --- /dev/null +++ b/backend/src/main/java/com/realnet/DataLake/Controllers/XmlApiExample.java @@ -0,0 +1,57 @@ +package com.realnet.DataLake.Controllers; + +import javax.xml.bind.annotation.XmlRootElement; + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@SpringBootApplication +@RestController +@RequestMapping("/token/api") +public class XmlApiExample { + + @GetMapping(value = "/getUser", produces = MediaType.APPLICATION_XML_VALUE) + public User getUser() { + User user = new User(); + user.setId(101); + user.setName("John Doe"); + user.setEmail("john.doe@example.com"); + return user; + } + + // Dummy XML model class + @XmlRootElement(name = "user") + public static class User { + private int id; + private String name; + private String email; + + // Getters and setters + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + } +} diff --git a/backend/src/main/java/com/realnet/DataLake/Controllers/tokenFree_Data_lakeController.java b/backend/src/main/java/com/realnet/DataLake/Controllers/tokenFree_Data_lakeController.java new file mode 100644 index 0000000..2522f0d --- /dev/null +++ b/backend/src/main/java/com/realnet/DataLake/Controllers/tokenFree_Data_lakeController.java @@ -0,0 +1,115 @@ +package com.realnet.DataLake.Controllers; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.realnet.DataLake.Entity.Data_lake; +import com.realnet.DataLake.Services.Data_lakeService; +import com.realnet.fnd.response.EntityResponse; + +@RequestMapping(value = "/token/Data_lake") +@CrossOrigin("*") +@RestController +public class tokenFree_Data_lakeController { + @Autowired + private Data_lakeService Service; + + @Value("${projectPath}") + private String projectPath; + + @PostMapping("/Data_lake") + public Data_lake Savedata(@RequestBody Data_lake data) { + Data_lake save = Service.Savedata(data); + + System.out.println("data saved..." + save); + + return save; + } + + @PutMapping("/Data_lake/{id}") + public Data_lake update(@RequestBody Data_lake data, @PathVariable Integer id) { + Data_lake update = Service.update(data, id); + System.out.println("data update..." + update); + return update; + } + +// get all with pagination + @GetMapping("/Data_lake/getall/page") + public Page getall(@RequestParam(value = "page", required = false) Integer page, + @RequestParam(value = "size", required = false) Integer size) { + Pageable paging = PageRequest.of(page, size); + Page get = Service.getAllWithPagination(paging); + + return get; + + } + + @GetMapping("/Data_lake") + public List getdetails() { + List get = Service.getdetails(); + return get; + } +// get all without authentication + + @GetMapping("/token/Data_lake") + public List getallwioutsec() { + List get = Service.getdetails(); + return get; + } + + @GetMapping("/Data_lake/{id}") + public Data_lake getdetailsbyId(@PathVariable Integer id) { + Data_lake get = Service.getdetailsbyId(id); + return get; + } + + @DeleteMapping("/Data_lake/{id}") + public ResponseEntity delete_by_id(@PathVariable Integer id) { + Service.delete_by_id(id); + return new ResponseEntity<>(new EntityResponse("Deleted"), HttpStatus.OK); + + } + + @GetMapping("/Data_lake/merge/{id}") + public ResponseEntity mergeBatchData(@PathVariable Integer id) { + try { + // Get merged JSON string from service + String mergedJson = Service.mergeBatchData(id); + + // Parse the merged JSON string back into JSON structure + ObjectMapper mapper = new ObjectMapper(); + JsonNode jsonNode = mapper.readTree(mergedJson); + + // Return as actual JSON (not as string) + return ResponseEntity.ok(jsonNode); + + } catch (Exception e) { + // Build error response manually (Map.of() not available in Java 8) + Map error = new HashMap<>(); + error.put("error", e.getMessage()); + return ResponseEntity.status(500).body(error); + } + } + +} \ No newline at end of file diff --git a/backend/src/main/java/com/realnet/DataLake/Entity/BatchData.java b/backend/src/main/java/com/realnet/DataLake/Entity/BatchData.java new file mode 100644 index 0000000..73c937d --- /dev/null +++ b/backend/src/main/java/com/realnet/DataLake/Entity/BatchData.java @@ -0,0 +1,26 @@ +package com.realnet.DataLake.Entity; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Lob; + +import lombok.Data; + +@Entity +@Data +public class BatchData { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; + + private Integer datalake_id; + + @Lob + @Column(columnDefinition = "TEXT") + private String batchjson; + +} diff --git a/backend/src/main/java/com/realnet/DataLake/Entity/Data_lake.java b/backend/src/main/java/com/realnet/DataLake/Entity/Data_lake.java new file mode 100644 index 0000000..dfe3deb --- /dev/null +++ b/backend/src/main/java/com/realnet/DataLake/Entity/Data_lake.java @@ -0,0 +1,51 @@ +package com.realnet.DataLake.Entity; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Lob; + +import com.realnet.WhoColumn.Entity.Extension; + +import lombok.Data; + +@Entity +@Data +public class Data_lake extends Extension { + /** + * + */ + private static final long serialVersionUID = 1L; + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; + + private String name; + + private String url; + + private String schedule; + + private String cron_job; + + @Lob + @Column(columnDefinition = "TEXT") + private String json; + + private Integer batch_volume; + + private Integer sure_connect_id; + private String sureconnect_name; + + private String url_endpoint; + + @Lob + @Column(columnDefinition = "TEXT") + private String calculated_field_json; + + private Boolean iscalculatedfield; + +} diff --git a/backend/src/main/java/com/realnet/DataLake/Repository/BatchDataRepository.java b/backend/src/main/java/com/realnet/DataLake/Repository/BatchDataRepository.java new file mode 100644 index 0000000..dc55649 --- /dev/null +++ b/backend/src/main/java/com/realnet/DataLake/Repository/BatchDataRepository.java @@ -0,0 +1,24 @@ +package com.realnet.DataLake.Repository; + +import java.util.List; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.stereotype.Repository; + +import com.realnet.DataLake.Entity.BatchData; + +@Repository +public interface BatchDataRepository extends JpaRepository { + + @Query(value = "select * from batch_data where created_by=?1", nativeQuery = true) + List findAll(Long creayedBy); + + @Query(value = "select * from batch_data where created_by=?1", nativeQuery = true) + Page findAll(Long creayedBy, Pageable page); + + @Query(value = "select * from batch_data", nativeQuery = true) + List findByDatalake_id(Integer datalake_id); +} \ No newline at end of file diff --git a/backend/src/main/java/com/realnet/DataLake/Repository/Data_lakeRepository.java b/backend/src/main/java/com/realnet/DataLake/Repository/Data_lakeRepository.java new file mode 100644 index 0000000..6601dee --- /dev/null +++ b/backend/src/main/java/com/realnet/DataLake/Repository/Data_lakeRepository.java @@ -0,0 +1,21 @@ +package com.realnet.DataLake.Repository; + +import java.util.List; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.stereotype.Repository; + +import com.realnet.DataLake.Entity.Data_lake; + +@Repository +public interface Data_lakeRepository extends JpaRepository { + + @Query(value = "select * from data_lake where created_by=?1", nativeQuery = true) + List findAll(Long creayedBy); + + @Query(value = "select * from data_lake where created_by=?1", nativeQuery = true) + Page findAll(Long creayedBy, Pageable page); +} \ No newline at end of file diff --git a/backend/src/main/java/com/realnet/DataLake/Services/Data_lakeService.java b/backend/src/main/java/com/realnet/DataLake/Services/Data_lakeService.java new file mode 100644 index 0000000..2fb3dd8 --- /dev/null +++ b/backend/src/main/java/com/realnet/DataLake/Services/Data_lakeService.java @@ -0,0 +1,428 @@ +package com.realnet.DataLake.Services; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.dataformat.xml.XmlMapper; +import com.realnet.DataLake.Entity.BatchData; +import com.realnet.DataLake.Entity.Data_lake; +import com.realnet.DataLake.Repository.BatchDataRepository; +import com.realnet.DataLake.Repository.Data_lakeRepository; +import com.realnet.SureConnect.Service.SureService; +import com.realnet.realm.Entity.Realm; +import com.realnet.realm.Services.RealmService; +import com.realnet.users.entity1.AppUser; +import com.realnet.users.service1.AppUserServiceImpl; +import com.realnet.utils.Port_Constant; + +@Service +public class Data_lakeService { + @Autowired + private Data_lakeRepository Repository; + + @Autowired + private BatchDataRepository batchRepo; + @Autowired + private AppUserServiceImpl userService; + @Autowired + private RealmService realmService; + + @Autowired + private SureService sureService; + + public Data_lake Savedata(Data_lake data) { + + data.setUpdatedBy(getUser().getUserId()); + data.setCreatedBy(getUser().getUserId()); + data.setAccountId(getUser().getAccount().getAccount_id()); + + if (data.getSure_connect_id() != null) { + + data.setSureconnect_name(sureService.getbyid(data.getSure_connect_id()).getConnection_name()); + } + + Data_lake save = Repository.save(data); + return save; + } + +// get all with pagination + public Page getAllWithPagination(Pageable page) { + return Repository.findAll(getUser().getUserId(), page); + } + + public List getdetails() { + List realm = realmService.findByUserId(getUser().getUserId()); + List all = Repository.findAll(getUser().getUserId()); + + return all; + } + + public Data_lake getdetailsbyId(Integer id) { + return Repository.findById(id).get(); + } + + public void delete_by_id(Integer id) { + Repository.deleteById(id); + } + + public Data_lake update(Data_lake data, Integer id) { + Data_lake old = Repository.findById(id).get(); + // id auto-generated hai โ†’ update nahi karenge + + if (data.getName() != null) { + old.setName(data.getName()); + } + + if (data.getUrl() != null) { + old.setUrl(data.getUrl()); + } + + if (data.getSchedule() != null) { + old.setSchedule(data.getSchedule()); + } + + if (data.getCron_job() != null) { + old.setCron_job(data.getCron_job()); + } + + if (data.getJson() != null) { + old.setJson(data.getJson()); + } + + if (data.getBatch_volume() != null) { + old.setBatch_volume(data.getBatch_volume()); + } + + if (data.getSure_connect_id() != null) { + old.setSure_connect_id(data.getSure_connect_id()); + + old.setSureconnect_name(sureService.getbyid(data.getSure_connect_id()).getConnection_name()); + + } + + if (data.getUrl_endpoint() != null) { + old.setUrl_endpoint(data.getUrl_endpoint()); + } + + if (data.getCalculated_field_json() != null) { + old.setCalculated_field_json(data.getCalculated_field_json()); + } + + if (data.getIscalculatedfield() != null) { + old.setIscalculatedfield(data.getIscalculatedfield()); + } + + final Data_lake test = Repository.save(old); + + return test; + } + + public Data_lake Updatejson(Integer id) throws JsonProcessingException { + + Data_lake old = Repository.findById(id).get(); + + String url = old.getUrl(); + + ResponseEntity response = GETAsString(url); + + // Convert the JSON object (ArrayList, Map, etc.) to a String +// Object responseBody = response.getBody(); + ObjectMapper mapper = new ObjectMapper(); + + String rawBody = response.getBody(); +// String jsonString = mapper.writeValueAsString(rawBody); + ObjectMapper jsonMapper = new ObjectMapper(); + Object responseBody; + + // Detect and convert if XML + if (isXml(rawBody)) { + try { + // Convert XML โ†’ JSON tree + + XmlMapper xmlMapper = new XmlMapper(); + + JsonNode xmlNode = xmlMapper.readTree(rawBody.getBytes()); + // Convert to standard JSON structure + String jsonFromXml = jsonMapper.writeValueAsString(xmlNode); + responseBody = jsonMapper.readValue(jsonFromXml, Object.class); + System.out.println("XML detected and converted to JSON successfully."); + } catch (Exception e) { + throw new RuntimeException("Failed to convert XML to JSON: " + e.getMessage()); + } + } else { + // Normal JSON response + try { + responseBody = jsonMapper.readValue(rawBody, Object.class); + System.out.println("JSON response detected."); + } catch (Exception e) { + throw new RuntimeException("Invalid JSON format: " + e.getMessage()); + } + } + + // โœ… Handle calculated fields before batch processing + if (Boolean.TRUE.equals(old.getIscalculatedfield()) && old.getCalculated_field_json() != null) { + try { + responseBody = applyCalculatedFields(responseBody, old.getCalculated_field_json(), mapper); + System.out.println("Calculated fields applied successfully."); + } catch (Exception e) { + System.err.println("Failed to process calculated fields: " + e.getMessage()); + } + } + + String jsonString = mapper.writeValueAsString(responseBody); + old.setJson(jsonString); + + // Process and insert into BatchData as before + + // Process and insert into BatchData + processBatchData(old, responseBody, mapper); + + String Url = Port_Constant.DOMAIN + "/Data_lake/Data_lake/merge/" + old.getId(); + old.setUrl_endpoint(Url); + + old.setUpdatedBy(getUser().getUserId()); + + final Data_lake saved = Repository.save(old); + + System.out.println(" json updated.."); + return saved; + + } + + @SuppressWarnings("unchecked") + private Object applyCalculatedFields(Object responseBody, String calculatedFieldJson, ObjectMapper mapper) + throws JsonProcessingException { + + // Parse the calculated field JSON + List> calcFields = mapper.readValue(calculatedFieldJson, List.class); + + if (!(responseBody instanceof List)) { + // Wrap single object into a list for consistent processing + responseBody = Arrays.asList(responseBody); + } + + List> records = (List>) responseBody; + + for (Map record : records) { + + for (Map calc : calcFields) { + String fieldName = (String) calc.get("fieldName"); + String operation = (String) calc.get("operation"); + List> components = (List>) calc.get("fieldComponents"); + + // Create constant fields separately + for (Map comp : components) { + String subField = (String) comp.get("field"); + Boolean isConstant = comp.get("isConstant") != null && (Boolean) comp.get("isConstant"); + Object constantValue = comp.get("constant"); + + if (isConstant && subField != null) { + record.put(subField, parseNumberOrString(constantValue)); + } + } + + // Collect operand values + List values = new ArrayList<>(); + for (Map comp : components) { + Boolean isConstant = comp.get("isConstant") != null && (Boolean) comp.get("isConstant"); + Object val = isConstant ? comp.get("constant") : record.get(comp.get("field")); + if (val != null) { + values.add(val); + } + } + + // Compute final value + Object result = performOperation(values, operation); + record.put(fieldName, result); + } + } + + return records; + } + + private Object performOperation(List values, String operation) { + if (values.isEmpty()) + return null; + + switch (operation.toLowerCase()) { + case "add": + double sum = 0; + for (Object v : values) + sum += toDouble(v); + return sum; + + case "subtract": + double result = toDouble(values.get(0)); + for (int i = 1; i < values.size(); i++) + result -= toDouble(values.get(i)); + return result; + + case "multiply": + double prod = 1; + for (Object v : values) + prod *= toDouble(v); + return prod; + + case "divide": + double div = toDouble(values.get(0)); + for (int i = 1; i < values.size(); i++) { + double val = toDouble(values.get(i)); + if (val != 0) + div /= val; + } + return div; + + case "percentage": + if (values.size() < 2) + return null; + double num = toDouble(values.get(0)); + double den = toDouble(values.get(1)); + return den == 0 ? null : (num / den) * 100; + + case "concat": + return values.stream().map(Object::toString).collect(Collectors.joining("_")); + + default: + return null; + } + } + + private double toDouble(Object val) { + if (val == null) + return 0.0; + try { + return Double.parseDouble(val.toString().trim()); + } catch (NumberFormatException e) { + return 0.0; + } + } + + private Object parseNumberOrString(Object val) { + if (val == null) + return null; + String str = val.toString().trim(); + try { + return Double.parseDouble(str); + } catch (NumberFormatException e) { + return str; + } + } + + private boolean isXml(String content) { + if (content == null) + return false; + String trimmed = content.trim(); + // XML usually starts with '<' and ends with '>' + return trimmed.startsWith("<") && trimmed.endsWith(">"); + } + + private void processBatchData(Data_lake dataLake, Object responseBody, ObjectMapper mapper) + throws JsonProcessingException { + + int batchVolume = (dataLake.getBatch_volume() != null && dataLake.getBatch_volume() > 0) + ? dataLake.getBatch_volume() + : 100; // default batch size if not given + + // Convert to JsonNode + JsonNode jsonNode = mapper.valueToTree(responseBody); + + if (jsonNode.isArray()) { + ArrayNode arrayNode = (ArrayNode) jsonNode; + int total = arrayNode.size(); + System.out.println("Total records: " + total); + + for (int i = 0; i < total; i += batchVolume) { + int end = Math.min(i + batchVolume, total); + + // Create a sub-array manually + ArrayNode subArray = mapper.createArrayNode(); + for (int j = i; j < end; j++) { + subArray.add(arrayNode.get(j)); + } + + String subJson = mapper.writeValueAsString(subArray); + + BatchData batch = new BatchData(); + batch.setDatalake_id(dataLake.getId()); + batch.setBatchjson(subJson); + batchRepo.save(batch); + } + + System.out.println("Inserted " + (int) Math.ceil((double) total / batchVolume) + " batch records."); + + } else { + // Single object โ†’ one record + BatchData batch = new BatchData(); + batch.setDatalake_id(dataLake.getId()); + batch.setBatchjson(mapper.writeValueAsString(jsonNode)); + batchRepo.save(batch); + + System.out.println("Inserted single batch record."); + } + } + + public String mergeBatchData(Integer datalakeId) throws Exception { + List batchList = batchRepo.findByDatalake_id(datalakeId); + + if (batchList.isEmpty()) { + throw new RuntimeException("No batch data found for datalake_id: " + datalakeId); + } + + ObjectMapper mapper = new ObjectMapper(); + ArrayNode mergedArray = mapper.createArrayNode(); + + for (BatchData batch : batchList) { + String json = batch.getBatchjson(); + JsonNode node = mapper.readTree(json); + + if (node.isArray()) { + // Add each element to merged array + for (JsonNode item : node) { + mergedArray.add(item); + } + } else { + // Single object, just add directly + mergedArray.add(node); + } + } + + String mergedJson = mapper.writeValueAsString(mergedArray); + System.out.println("Merged JSON size: " + mergedArray.size()); + return mergedJson; + } + + public ResponseEntity GET(String get) { + RestTemplate restTemplate = new RestTemplate(); + + ResponseEntity u = restTemplate.getForEntity(get, Object.class); + + return u; + + } + + public ResponseEntity GETAsString(String url) { + RestTemplate restTemplate = new RestTemplate(); + return restTemplate.getForEntity(url, String.class); + } + + public AppUser getUser() { + AppUser user = userService.getLoggedInUser(); + return user; + + } + +}