data import

This commit is contained in:
string 2025-08-15 10:05:53 +05:30
parent c2b22d5319
commit bbca71f125
3 changed files with 329 additions and 377 deletions

View File

@ -1,7 +1,5 @@
package com.realnet.BulkUpload.Controllers;
import org.apache.commons.lang3.StringUtils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
@ -24,7 +22,6 @@ import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
@ -54,7 +51,6 @@ import org.springframework.jdbc.core.JdbcTemplate;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@ -64,11 +60,9 @@ import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.realnet.BulkUpload.Repository.BulkUpload_Repository;
import com.realnet.BulkUpload.Services.BulkUpload_Service;
import com.realnet.fnd.response.EntityResponse;
import com.realnet.template.entity.TemplateFileUpload;
import com.realnet.template.repository.TemplatedataRepo;
import com.realnet.template.service.DynamicTemplateService;
import com.realnet.template.service.FileUploadService;
@RestController
@ -78,14 +72,6 @@ public class DataImportController {
private JdbcTemplate jdbcTemplate;
@Autowired
private TemplatedataRepo temprepo;
@Autowired
private DynamicTemplateService dynamicTemplateService;
@Autowired
private BulkUpload_Service bulkUpload_Service;
@Autowired
private BulkUpload_Repository bulkUpload_Repository;
@Autowired
private FileUploadService fileUploadService;
@ -280,166 +266,6 @@ public class DataImportController {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
/////////// VERY IMPORTANT NOT DELETE THE BELOW///////////
// @PostMapping("/DownloadExcel/{id}")
// public ResponseEntity<?> importdatadownloadexcel(@PathVariable Long id,
// @RequestBody Map<String, List<Map<String, Object>>> jsonData) {
//
// TemplateFileUpload templateFileUpload = fileUploadService.getTemplatebyid(id);
//
// String entity_name = templateFileUpload.getEntity_name();
//
// BulkUpload_t bulkUpload_t = bulkUpload_Repository.getentityName(entity_name);
// String ruleLine = bulkUpload_t.getRule_line();
//
// JsonParser parser = new JsonParser();
// JsonElement element = parser.parse(ruleLine);
// JsonArray array = element.getAsJsonArray();
// Iterator<JsonElement> iterator = array.iterator();
//
// String fromsheet = null;
// String fromColumn = null;
// String validationTable = null;
// String checkColumn = null;
// String useColumn = null;
// String replacementtable = null;
// String replacementcolumn = null;
//
// while (iterator.hasNext()) {
// JsonElement next = iterator.next();
// JsonObject jsonObject = next.getAsJsonObject();
//
// fromsheet = jsonObject.get("fromsheet").getAsString();
// fromColumn = jsonObject.get("fromColumn").getAsString();
// validationTable = jsonObject.get("validationTable").getAsString();
// checkColumn = jsonObject.get("checkColumn").getAsString();
// useColumn = jsonObject.get("useColumn").getAsString();
// replacementtable = jsonObject.get("useTable").getAsString();
// replacementcolumn = jsonObject.get("replacementcolumn").getAsString();
//
// break;
// }
//
// String customerTableName = validationTable;
// String siteTableName = replacementtable;
//
// try {
// Set<String> tableNames = jsonData.keySet(); // Get all unique table names
//
// String siteEntityName = null;
// for (String tableName : tableNames) {
// List<Map<String, Object>> tableData = jsonData.get(tableName);
//
// // Process tableData based on the tableName (e.g., "Site" or "Customer")
// System.out.println("Table Name: " + tableName);
// for (Map<String, Object> row : tableData) {
// // Process individual rows within the table data
// System.out.println("Row Data: " + row);
// }
// }
//
// List<Map<String, Object>> processedDataList = new ArrayList<>(); // List to hold processed data rows
//
// // Iterate through each customer data entry
// List<Map<String, Object>> customerData = jsonData.get("Customer");
// for (Map<String, Object> insertCustomerData : customerData) {
// String customerName = (String) insertCustomerData.get(checkColumn);
//
// // Check if the customerName is not null and iterate through "Site" data
// List<Map<String, Object>> siteData = jsonData.get(fromsheet);
//
// List<Map<String, Object>> matchedSiteData = new ArrayList<>();
// if (customerName != null) {
// // Iterate through "Site" data and check for a matching "entity_name"
// for (Map<String, Object> siteRow : siteData) {
// // Specify the index as "AM" (39th column)
//// String columnIndex = "AM";
// String columnIndex = fromColumn;
//
// // Retrieve the value at the specified index
//
// for (Map.Entry<String, Object> entry : siteRow.entrySet()) {
// if (entry.getKey().equals(columnIndex)) {
// siteEntityName = (String) entry.getValue();
// break; // Exit the loop once the value is found
// }
// }
//
// if (customerName.equals(siteEntityName)) {
// // Add the matching "Site" data to the list
// matchedSiteData.add(siteRow);
// }
// }
// }
//
// ObjectMapper objectMapper = new ObjectMapper();
// String insertCustomerDataJson = objectMapper.writeValueAsString(insertCustomerData);
// String matchedSiteDataJson = objectMapper.writeValueAsString(matchedSiteData);
//
// String customerSql = getInsertQuery(customerTableName, insertCustomerData);
//
// // Insert data into the customer table
// Object[] customerValues = new Object[insertCustomerData.size() + 5];
// int index = 0;
// for (Object value : insertCustomerData.values()) {
// customerValues[index++] = value;
// }
// customerValues[index++] = new Timestamp(System.currentTimeMillis()); // created_at
// customerValues[index++] = null; // created_by
// customerValues[index++] = null; // updated_by
// customerValues[index++] = new Timestamp(System.currentTimeMillis()); // updated_at
// customerValues[index] = null; // account_id
// jdbcTemplate.update(customerSql, customerValues);
//
// // we can use useColumn here
// Long generatedId = jdbcTemplate.queryForObject("SELECT LAST_INSERT_ID()", Long.class);
//
// List<Map<String, Object>> insertMatchedSiteData = new ArrayList<>(); // List to hold processed data rows
//
// for (Map<String, Object> siteRow : matchedSiteData) {
// // Replace "Customer Name" with "customer_master_id" if it exists
// if (siteRow.containsKey(siteEntityName)) {
// siteRow.put(replacementcolumn, generatedId);
// siteRow.remove(siteEntityName);
// }
// insertMatchedSiteData.add(siteRow);
// }
//
// for (Map<String, Object> siteRow : insertMatchedSiteData) {
// Object[] siteValues = new Object[siteRow.size() + 5]; // Create a new array for each row
//
// int siteIndex = 0;
// for (Object value : siteRow.values()) {
// siteValues[siteIndex++] = value;
// }
//
// siteValues[siteIndex++] = new Timestamp(System.currentTimeMillis()); // created_at
// siteValues[siteIndex++] = null; // created_by
// siteValues[siteIndex++] = null; // updated_by
// siteValues[siteIndex++] = new Timestamp(System.currentTimeMillis()); // updated_at
// siteValues[siteIndex] = null; // account_id
//
// String siteSql = getInsertQuery(siteTableName, siteRow);
// jdbcTemplate.update(siteSql, siteValues);
// }
//
// // Add the processed customer data to the list
// processedDataList.add(insertCustomerData);
// }
//
// // Use a LinkedHashMap to preserve insertion order in the response
// Map<String, List<Map<String, Object>>> response = new LinkedHashMap<>();
// response.put("result", processedDataList);
//
// return new ResponseEntity<>(response, HttpStatus.OK);
//
// } catch (Exception e) {
// e.printStackTrace();
// // Handle exceptions and return an appropriate response
// return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
// }
// }
@PostMapping("/DownloadExcel/{id}")
public ResponseEntity<?> importdatadownloadexcel(@PathVariable Long id,
@ -447,8 +273,6 @@ public class DataImportController {
@RequestParam(name = "ruleData") String ruleDataParam)
throws JsonMappingException, JsonProcessingException, UnsupportedEncodingException {
// Inside your method
ObjectMapper objectMapper = new ObjectMapper();
JsonNode ruleDataNode = objectMapper.readTree(ruleDataParam);
@ -458,8 +282,6 @@ public class DataImportController {
return uploadDataToTable(id, jsonDataParam);
}
// Parse jsonDataParam to extract the JSON data
Map<String, List<Map<String, Object>>> jsonData = objectMapper.readValue(
URLDecoder.decode(jsonDataParam, "UTF-8"), new TypeReference<Map<String, List<Map<String, Object>>>>() {
@ -478,8 +300,6 @@ public class DataImportController {
String replacementtable = null;
String replacementcolumn = null;
if (ruleData != null && !ruleData.isEmpty()) {
// Assuming you want to access the first element in the array
Map<String, String> rule = ruleData.get(0);
@ -509,7 +329,6 @@ public class DataImportController {
// Create a sheet for site data
XSSFSheet siteSheet = workbook.createSheet("Site");
List<Map<String, Object>> customerDataList = new ArrayList<>();
List<Map<String, Object>> siteDataList = new ArrayList<>();
@ -731,8 +550,6 @@ public class DataImportController {
}
private ResponseEntity<?> uploadDataToTable(Long id, String jsonDataParam) {
try {
// Get table name from TemplateFileUpload entity
@ -740,10 +557,12 @@ public class DataImportController {
if (fileUploadOptional.isPresent()) {
TemplateFileUpload fileUpload = fileUploadOptional.get();
// String tableName = fileUpload.getTablename();
String tableName = "aa";
String tableName = fileUpload.getEntity_name();
// String tableName = "aa";
// Insert data into the specified table
insertDataIntoTable(tableName, jsonDataParam);
return ResponseEntity.ok().body("Data uploaded successfully to table: " + tableName);
System.out.println("Data uploaded successfully to table...");
return ResponseEntity.ok().body(new EntityResponse("Data uploaded successfully to table: " + tableName));
} else {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Template file with id " + id + " not found");
}
@ -754,20 +573,22 @@ public class DataImportController {
}
}
private void insertDataIntoTable(String tableName, String jsonDataParam) {
try {
ObjectMapper objectMapper = new ObjectMapper();
// Parse jsonDataParam to extract the JSON data
Map<String, List<Map<String, Object>>> jsonData = objectMapper.readValue(
URLDecoder.decode(jsonDataParam, "UTF-8"), new TypeReference<Map<String, List<Map<String, Object>>>>() {});
URLDecoder.decode(jsonDataParam, "UTF-8"),
new TypeReference<Map<String, List<Map<String, Object>>>>() {
});
// Iterate over the entries of the jsonData map
for (Map.Entry<String, List<Map<String, Object>>> entry : jsonData.entrySet()) {
String sheetName = entry.getKey(); // Get the sheet name dynamically
List<Map<String, Object>> data = entry.getValue(); // Get the list of maps dynamically
// Now you have the list of maps for the current sheet, you can iterate through it and perform your database insertion logic
// Now you have the list of maps for the current sheet, you can iterate through
// it and perform your database insertion logic
for (Map<String, Object> rowData : data) {
StringBuilder columns = new StringBuilder();
StringBuilder values = new StringBuilder();
@ -797,8 +618,6 @@ public class DataImportController {
}
}
private String extractExceptionMessage(DataIntegrityViolationException e) {
String errorMessage = e.getMessage();
int startIndex = errorMessage.indexOf("Incorrect ");
@ -865,8 +684,6 @@ public class DataImportController {
}
}
@GetMapping("/downloadImportStatement/{id}")
public ResponseEntity<?> downloadImportStatement(@PathVariable Long id) {
// Retrieve the file data from the database based on the ID
@ -896,7 +713,6 @@ public class DataImportController {
}
}
// get table column
@GetMapping("/columns/{tableName}")
@ -917,12 +733,8 @@ public class DataImportController {
return new ArrayList<>(columnNamesSet);
}
private String getCellValue(Map<String, List<Map<String, Object>>> jsonData, String sheetName,
String cellAddress) throws IOException {
private String getCellValue(Map<String, List<Map<String, Object>>> jsonData, String sheetName, String cellAddress)
throws IOException {
if (jsonData.containsKey(sheetName)) {
List<Map<String, Object>> sheetData = jsonData.get(sheetName);
int size = sheetData.size();
@ -948,7 +760,6 @@ public class DataImportController {
// int rowIndex = Integer.parseInt(rowPart) - 1; // Excel-style 1-based index
System.out.println("Column Index: " + columnIndex);
if (columnIndex >= 0) {
// Get the list of keys from the firstRowData map
List<String> keys = new ArrayList<>(firstRowData.keySet());
@ -961,15 +772,9 @@ public class DataImportController {
}
}
return null;
}
private int getColumnIndex(String columnPart) {
int index = 0;
int multiplier = 1;
@ -984,10 +789,6 @@ public class DataImportController {
return index - 1; // Subtract 1 to get a 0-based index
}
// @PostMapping("/DownloadExcel/{entityName}")
// public ResponseEntity<Resource> importdatadownloadexcel(@PathVariable String entityName,
// @RequestBody List<Map<String, Object>> data) throws IOException {
@ -1003,8 +804,6 @@ public class DataImportController {
// return ResponseEntity.ok().body(null);
// }
private List<Map<String, Object>> processData(String entityName, List<Map<String, Object>> data) {
List<Map<String, Object>> processedData = new ArrayList<>();
try {
@ -1042,8 +841,6 @@ public class DataImportController {
return processedData;
}
private Workbook createWorkbook(List<Map<String, Object>> processedData) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Failed Records");
@ -1077,6 +874,165 @@ public class DataImportController {
return new InputStreamResource(inputStream);
}
/////////// VERY IMPORTANT NOT DELETE THE BELOW///////////
//@PostMapping("/DownloadExcel/{id}")
//public ResponseEntity<?> importdatadownloadexcel(@PathVariable Long id,
// @RequestBody Map<String, List<Map<String, Object>>> jsonData) {
//
// TemplateFileUpload templateFileUpload = fileUploadService.getTemplatebyid(id);
//
// String entity_name = templateFileUpload.getEntity_name();
//
// BulkUpload_t bulkUpload_t = bulkUpload_Repository.getentityName(entity_name);
// String ruleLine = bulkUpload_t.getRule_line();
//
// JsonParser parser = new JsonParser();
// JsonElement element = parser.parse(ruleLine);
// JsonArray array = element.getAsJsonArray();
// Iterator<JsonElement> iterator = array.iterator();
//
// String fromsheet = null;
// String fromColumn = null;
// String validationTable = null;
// String checkColumn = null;
// String useColumn = null;
// String replacementtable = null;
// String replacementcolumn = null;
//
// while (iterator.hasNext()) {
// JsonElement next = iterator.next();
// JsonObject jsonObject = next.getAsJsonObject();
//
// fromsheet = jsonObject.get("fromsheet").getAsString();
// fromColumn = jsonObject.get("fromColumn").getAsString();
// validationTable = jsonObject.get("validationTable").getAsString();
// checkColumn = jsonObject.get("checkColumn").getAsString();
// useColumn = jsonObject.get("useColumn").getAsString();
// replacementtable = jsonObject.get("useTable").getAsString();
// replacementcolumn = jsonObject.get("replacementcolumn").getAsString();
//
// break;
// }
//
// String customerTableName = validationTable;
// String siteTableName = replacementtable;
//
// try {
// Set<String> tableNames = jsonData.keySet(); // Get all unique table names
//
// String siteEntityName = null;
// for (String tableName : tableNames) {
// List<Map<String, Object>> tableData = jsonData.get(tableName);
//
// // Process tableData based on the tableName (e.g., "Site" or "Customer")
// System.out.println("Table Name: " + tableName);
// for (Map<String, Object> row : tableData) {
// // Process individual rows within the table data
// System.out.println("Row Data: " + row);
// }
// }
//
// List<Map<String, Object>> processedDataList = new ArrayList<>(); // List to hold processed data rows
//
// // Iterate through each customer data entry
// List<Map<String, Object>> customerData = jsonData.get("Customer");
// for (Map<String, Object> insertCustomerData : customerData) {
// String customerName = (String) insertCustomerData.get(checkColumn);
//
// // Check if the customerName is not null and iterate through "Site" data
// List<Map<String, Object>> siteData = jsonData.get(fromsheet);
//
// List<Map<String, Object>> matchedSiteData = new ArrayList<>();
// if (customerName != null) {
// // Iterate through "Site" data and check for a matching "entity_name"
// for (Map<String, Object> siteRow : siteData) {
// // Specify the index as "AM" (39th column)
//// String columnIndex = "AM";
// String columnIndex = fromColumn;
//
// // Retrieve the value at the specified index
//
// for (Map.Entry<String, Object> entry : siteRow.entrySet()) {
// if (entry.getKey().equals(columnIndex)) {
// siteEntityName = (String) entry.getValue();
// break; // Exit the loop once the value is found
// }
// }
//
// if (customerName.equals(siteEntityName)) {
// // Add the matching "Site" data to the list
// matchedSiteData.add(siteRow);
// }
// }
// }
//
// ObjectMapper objectMapper = new ObjectMapper();
// String insertCustomerDataJson = objectMapper.writeValueAsString(insertCustomerData);
// String matchedSiteDataJson = objectMapper.writeValueAsString(matchedSiteData);
//
// String customerSql = getInsertQuery(customerTableName, insertCustomerData);
//
// // Insert data into the customer table
// Object[] customerValues = new Object[insertCustomerData.size() + 5];
// int index = 0;
// for (Object value : insertCustomerData.values()) {
// customerValues[index++] = value;
// }
// customerValues[index++] = new Timestamp(System.currentTimeMillis()); // created_at
// customerValues[index++] = null; // created_by
// customerValues[index++] = null; // updated_by
// customerValues[index++] = new Timestamp(System.currentTimeMillis()); // updated_at
// customerValues[index] = null; // account_id
// jdbcTemplate.update(customerSql, customerValues);
//
// // we can use useColumn here
// Long generatedId = jdbcTemplate.queryForObject("SELECT LAST_INSERT_ID()", Long.class);
//
// List<Map<String, Object>> insertMatchedSiteData = new ArrayList<>(); // List to hold processed data rows
//
// for (Map<String, Object> siteRow : matchedSiteData) {
// // Replace "Customer Name" with "customer_master_id" if it exists
// if (siteRow.containsKey(siteEntityName)) {
// siteRow.put(replacementcolumn, generatedId);
// siteRow.remove(siteEntityName);
// }
// insertMatchedSiteData.add(siteRow);
// }
//
// for (Map<String, Object> siteRow : insertMatchedSiteData) {
// Object[] siteValues = new Object[siteRow.size() + 5]; // Create a new array for each row
//
// int siteIndex = 0;
// for (Object value : siteRow.values()) {
// siteValues[siteIndex++] = value;
// }
//
// siteValues[siteIndex++] = new Timestamp(System.currentTimeMillis()); // created_at
// siteValues[siteIndex++] = null; // created_by
// siteValues[siteIndex++] = null; // updated_by
// siteValues[siteIndex++] = new Timestamp(System.currentTimeMillis()); // updated_at
// siteValues[siteIndex] = null; // account_id
//
// String siteSql = getInsertQuery(siteTableName, siteRow);
// jdbcTemplate.update(siteSql, siteValues);
// }
//
// // Add the processed customer data to the list
// processedDataList.add(insertCustomerData);
// }
//
// // Use a LinkedHashMap to preserve insertion order in the response
// Map<String, List<Map<String, Object>>> response = new LinkedHashMap<>();
// response.put("result", processedDataList);
//
// return new ResponseEntity<>(response, HttpStatus.OK);
//
// } catch (Exception e) {
// e.printStackTrace();
// // Handle exceptions and return an appropriate response
// return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
// }
//}
}

View File

@ -38,7 +38,7 @@ public class FileuploadService {
File projectdir = new File(Path1);
if (!projectdir.exists()) {
boolean mkdir = projectdir.mkdirs();
System.out.println(Path1 + " folder create = " + mkdir);
System.out.println(Path1 + " folder created = " + mkdir);
}

View File

@ -4,8 +4,6 @@ import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@ -25,12 +23,10 @@ public class NotController {
// return dash;
// }
@GetMapping("/get_notification")
public List<NotEntity> getdetails() {
List<NotEntity> dash = notRepo.findTopByOrderByd();
return dash;
}
}