data import
This commit is contained in:
		
							parent
							
								
									c2b22d5319
								
							
						
					
					
						commit
						bbca71f125
					
				| @ -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,185 +266,21 @@ 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, | ||||
| 	public ResponseEntity<?> importdatadownloadexcel(@PathVariable Long id, | ||||
| 			@RequestParam(name = "jsonData") String jsonDataParam, | ||||
| 			@RequestParam(name = "ruleData") String ruleDataParam) | ||||
| 			throws JsonMappingException, JsonProcessingException, UnsupportedEncodingException { | ||||
| 
 | ||||
| 		 | ||||
| 
 | ||||
| 		// Inside your method | ||||
| 		ObjectMapper objectMapper = new ObjectMapper(); | ||||
| 		JsonNode ruleDataNode = objectMapper.readTree(ruleDataParam); | ||||
| 
 | ||||
| 		if (ruleDataNode.isArray() && ruleDataNode.isEmpty()) { | ||||
| 		    // If ruleDataParam is an empty JSON array, directly upload data to the table | ||||
| 		    return uploadDataToTable(id, jsonDataParam); | ||||
| 			// If ruleDataParam is an empty JSON array, directly upload data to the table | ||||
| 			return uploadDataToTable(id, jsonDataParam); | ||||
| 		} | ||||
| 		 | ||||
| 	 | ||||
| 
 | ||||
| 		// Parse jsonDataParam to extract the JSON data | ||||
| 		Map<String, List<Map<String, Object>>> jsonData = objectMapper.readValue( | ||||
| @ -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); | ||||
| @ -508,7 +328,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<>(); | ||||
| @ -605,7 +424,7 @@ public class DataImportController { | ||||
| 					if (siteRow.containsKey(responseMessage)) { | ||||
| 						siteRow.put(replacementcolumn, generatedId); | ||||
| 						// siteRow.remove(responseMessage); | ||||
| 					//	siteRow.remove(responseMessage); | ||||
| 						// siteRow.remove(responseMessage); | ||||
| 					} | ||||
| 					newMatchedSiteData.add(siteRow); | ||||
| 				} | ||||
| @ -731,74 +550,74 @@ public class DataImportController { | ||||
| 
 | ||||
| 	} | ||||
| 
 | ||||
| 	 | ||||
| 	 | ||||
| 	private ResponseEntity<?> uploadDataToTable(Long id, String jsonDataParam) { | ||||
| 	    try { | ||||
| 	        // Get table name from TemplateFileUpload entity | ||||
| 	        Optional<TemplateFileUpload> fileUploadOptional = temprepo.findById(id); | ||||
| 	        if (fileUploadOptional.isPresent()) { | ||||
| 	            TemplateFileUpload fileUpload = fileUploadOptional.get(); | ||||
| 		try { | ||||
| 			// Get table name from TemplateFileUpload entity | ||||
| 			Optional<TemplateFileUpload> fileUploadOptional = temprepo.findById(id); | ||||
| 			if (fileUploadOptional.isPresent()) { | ||||
| 				TemplateFileUpload fileUpload = fileUploadOptional.get(); | ||||
| //	            String tableName = fileUpload.getTablename(); | ||||
| 	            String tableName = "aa"; | ||||
| 	            // Insert data into the specified table | ||||
| 	            insertDataIntoTable(tableName, jsonDataParam); | ||||
| 	            return ResponseEntity.ok().body("Data uploaded successfully to table: " + tableName); | ||||
| 	        } else { | ||||
| 	            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Template file with id " + id + " not found"); | ||||
| 	        } | ||||
| 	    } catch (Exception e) { | ||||
| 	        e.printStackTrace(); | ||||
| 	        // Handle exceptions and return an appropriate response | ||||
| 	        return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); | ||||
| 	    } | ||||
| 				String tableName = fileUpload.getEntity_name(); | ||||
| //				String tableName = "aa"; | ||||
| 				// Insert data into the specified table | ||||
| 				insertDataIntoTable(tableName, jsonDataParam); | ||||
| 				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"); | ||||
| 			} | ||||
| 		} catch (Exception e) { | ||||
| 			e.printStackTrace(); | ||||
| 			// Handle exceptions and return an appropriate response | ||||
| 			return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); | ||||
| 		} | ||||
| 	} | ||||
| 	 | ||||
| 	 | ||||
| 
 | ||||
| 	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>>>>() {}); | ||||
| 		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>>>>() { | ||||
| 					}); | ||||
| 
 | ||||
| 	        // 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 | ||||
| 	            for (Map<String, Object> rowData : data) { | ||||
| 	                StringBuilder columns = new StringBuilder(); | ||||
| 	                StringBuilder values = new StringBuilder(); | ||||
| 	                List<Object> params = new ArrayList<>(); | ||||
| 			// 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 | ||||
| 
 | ||||
| 	                // Build column names and values dynamically | ||||
| 	                for (Map.Entry<String, Object> columnEntry : rowData.entrySet()) { | ||||
| 	                    if (columns.length() > 0) { | ||||
| 	                        columns.append(", "); | ||||
| 	                        values.append(", "); | ||||
| 	                    } | ||||
| 	                    columns.append(columnEntry.getKey()); | ||||
| 	                    values.append("?"); | ||||
| 	                    params.add(columnEntry.getValue()); | ||||
| 	                } | ||||
| 				// 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(); | ||||
| 					List<Object> params = new ArrayList<>(); | ||||
| 
 | ||||
| 	                // Construct the SQL query | ||||
| 	                String sql = "INSERT INTO " + tableName + " (" + columns + ") VALUES (" + values + ")"; | ||||
| 					// Build column names and values dynamically | ||||
| 					for (Map.Entry<String, Object> columnEntry : rowData.entrySet()) { | ||||
| 						if (columns.length() > 0) { | ||||
| 							columns.append(", "); | ||||
| 							values.append(", "); | ||||
| 						} | ||||
| 						columns.append(columnEntry.getKey()); | ||||
| 						values.append("?"); | ||||
| 						params.add(columnEntry.getValue()); | ||||
| 					} | ||||
| 
 | ||||
| 	                // Execute the SQL query | ||||
| 	                jdbcTemplate.update(sql, params.toArray()); | ||||
| 	            } | ||||
| 	        } | ||||
| 	    } catch (Exception e) { | ||||
| 	        e.printStackTrace(); | ||||
| 	        // Handle exceptions appropriately | ||||
| 	    } | ||||
| 					// Construct the SQL query | ||||
| 					String sql = "INSERT INTO " + tableName + " (" + columns + ") VALUES (" + values + ")"; | ||||
| 
 | ||||
| 					// Execute the SQL query | ||||
| 					jdbcTemplate.update(sql, params.toArray()); | ||||
| 				} | ||||
| 			} | ||||
| 		} catch (Exception e) { | ||||
| 			e.printStackTrace(); | ||||
| 			// Handle exceptions appropriately | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| 	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 | ||||
| @ -895,9 +712,8 @@ public class DataImportController { | ||||
| 			return ResponseEntity.status(500).body("Error occurred while reading the file."); | ||||
| 		} | ||||
| 	} | ||||
| 	 | ||||
| 	 | ||||
| 	//get table column | ||||
| 
 | ||||
| 	// get table column | ||||
| 
 | ||||
| 	@GetMapping("/columns/{tableName}") | ||||
| 	public List<String> getColumnNames(@PathVariable String tableName) { | ||||
| @ -917,77 +733,62 @@ public class DataImportController { | ||||
| 		return new ArrayList<>(columnNamesSet); | ||||
| 	} | ||||
| 
 | ||||
| 	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(); | ||||
| 			System.out.println(size); | ||||
| 			Map<String, Object> firstRowData = sheetData.get(0); | ||||
| 
 | ||||
| 	 | ||||
| 	 | ||||
| 	 | ||||
| 	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(); | ||||
| 	        System.out.println(size); | ||||
| 	        Map<String, Object> firstRowData = sheetData.get(0);  | ||||
| 			// Separate the numeric part (row part) and non-numeric part (column part) | ||||
| 			String rowPart = ""; | ||||
| 			String columnPart = ""; | ||||
| 
 | ||||
| 	        // Separate the numeric part (row part) and non-numeric part (column part) | ||||
| 	        String rowPart = ""; | ||||
| 	        String columnPart = ""; | ||||
| 			for (char c : cellAddress.toCharArray()) { | ||||
| 				if (Character.isDigit(c)) { | ||||
| 					rowPart += c; | ||||
| 				} else { | ||||
| 					columnPart += c; | ||||
| 				} | ||||
| 			} | ||||
| 
 | ||||
| 	        for (char c : cellAddress.toCharArray()) { | ||||
| 	            if (Character.isDigit(c)) { | ||||
| 	                rowPart += c; | ||||
| 	            } else { | ||||
| 	                columnPart += c; | ||||
| 	            } | ||||
| 	        } | ||||
| 			System.out.println("Column Part: " + columnPart); | ||||
| 			System.out.println("Row Part: " + rowPart); | ||||
| 
 | ||||
| 	        System.out.println("Column Part: " + columnPart); | ||||
| 	        System.out.println("Row Part: " + rowPart); | ||||
| 
 | ||||
| 	        int columnIndex = getColumnIndex(columnPart); // Get the column index based on the columnPart | ||||
| 			int columnIndex = getColumnIndex(columnPart); // Get the column index based on the columnPart | ||||
| //	        int rowIndex = Integer.parseInt(rowPart) - 1; // Excel-style 1-based index | ||||
| 	        System.out.println("Column Index: " + columnIndex); | ||||
| 			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()); | ||||
| 
 | ||||
| 	        if (columnIndex >= 0 ) { | ||||
| 	            // Get the list of keys from the firstRowData map | ||||
| 	            List<String> keys = new ArrayList<>(firstRowData.keySet()); | ||||
| 				// Check if the columnIndex is within the range of keys | ||||
| 				if (columnIndex < keys.size()) { | ||||
| 					String columnName = keys.get(columnIndex); | ||||
| 					return columnName; | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
| 
 | ||||
| 	            // Check if the columnIndex is within the range of keys | ||||
| 	            if (columnIndex < keys.size()) { | ||||
| 	                String columnName = keys.get(columnIndex); | ||||
| 	                return columnName; | ||||
| 	            } | ||||
| 	        } | ||||
| 	    } | ||||
| 	       | ||||
| 	     | ||||
| 
 | ||||
| 	    return null; | ||||
| 		return null; | ||||
| 	} | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| 	private int getColumnIndex(String columnPart) { | ||||
| 	    int index = 0; | ||||
| 	    int multiplier = 1; | ||||
| 		int index = 0; | ||||
| 		int multiplier = 1; | ||||
| 
 | ||||
| 	    // Iterate through the columnPart in reverse order (right to left) | ||||
| 	    for (int i = columnPart.length() - 1; i >= 0; i--) { | ||||
| 	        char c = columnPart.charAt(i); | ||||
| 	        index += (c - 'A' + 1) * multiplier; | ||||
| 	        multiplier *= 26; // Multiply by 26 for each position to calculate the index | ||||
| 	    } | ||||
| 		// Iterate through the columnPart in reverse order (right to left) | ||||
| 		for (int i = columnPart.length() - 1; i >= 0; i--) { | ||||
| 			char c = columnPart.charAt(i); | ||||
| 			index += (c - 'A' + 1) * multiplier; | ||||
| 			multiplier *= 26; // Multiply by 26 for each position to calculate the index | ||||
| 		} | ||||
| 
 | ||||
| 	    return index - 1; // Subtract 1 to get a 0-based index | ||||
| 		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,80 +804,235 @@ 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 { | ||||
| 	        String convertedTableName = entityName; | ||||
| 	        String sql = getInsertQuery(convertedTableName, data.get(0)); | ||||
| 	        for (int i = 0; i < data.size(); i++) { | ||||
| 	            Map<String, Object> row = data.get(i); | ||||
| 	            Map<String, Object> processedRow = new HashMap<>(); | ||||
| 	            try { | ||||
| 	                Object[] values = new Object[row.size() + 5]; // +5 for the additional columns | ||||
| 	                int index = 0; | ||||
| 	                for (Object value : row.values()) { | ||||
| 	                    values[index++] = value; | ||||
| 	                } | ||||
| 	                values[index++] = new Timestamp(System.currentTimeMillis()); // created_at | ||||
| 	                values[index++] = null; // created_by | ||||
| 	                values[index++] = null; // updated_by | ||||
| 	                values[index++] = new Timestamp(System.currentTimeMillis()); // updated_at | ||||
| 	                values[index] = null; // account_id | ||||
| 	                jdbcTemplate.update(sql, values); | ||||
| 	                processedRow.putAll(row); | ||||
| 	                processedRow.put("Status", "Success"); | ||||
| 	                processedRow.put("Exception", "NA"); | ||||
| 	            } catch (DataIntegrityViolationException e) { | ||||
| 	                processedRow.putAll(row); | ||||
| 	                processedRow.put("Status", "Error"); | ||||
| 	                String exceptionMessage = extractExceptionMessage(e); | ||||
| 	                processedRow.put("Exception", exceptionMessage); | ||||
| 	            } | ||||
| 	            processedData.add(processedRow); | ||||
| 	        } | ||||
| 	    } catch (Exception e) { | ||||
| 	        // Handle any exceptions here if needed | ||||
| 	    } | ||||
| 	    return processedData; | ||||
| 		List<Map<String, Object>> processedData = new ArrayList<>(); | ||||
| 		try { | ||||
| 			String convertedTableName = entityName; | ||||
| 			String sql = getInsertQuery(convertedTableName, data.get(0)); | ||||
| 			for (int i = 0; i < data.size(); i++) { | ||||
| 				Map<String, Object> row = data.get(i); | ||||
| 				Map<String, Object> processedRow = new HashMap<>(); | ||||
| 				try { | ||||
| 					Object[] values = new Object[row.size() + 5]; // +5 for the additional columns | ||||
| 					int index = 0; | ||||
| 					for (Object value : row.values()) { | ||||
| 						values[index++] = value; | ||||
| 					} | ||||
| 					values[index++] = new Timestamp(System.currentTimeMillis()); // created_at | ||||
| 					values[index++] = null; // created_by | ||||
| 					values[index++] = null; // updated_by | ||||
| 					values[index++] = new Timestamp(System.currentTimeMillis()); // updated_at | ||||
| 					values[index] = null; // account_id | ||||
| 					jdbcTemplate.update(sql, values); | ||||
| 					processedRow.putAll(row); | ||||
| 					processedRow.put("Status", "Success"); | ||||
| 					processedRow.put("Exception", "NA"); | ||||
| 				} catch (DataIntegrityViolationException e) { | ||||
| 					processedRow.putAll(row); | ||||
| 					processedRow.put("Status", "Error"); | ||||
| 					String exceptionMessage = extractExceptionMessage(e); | ||||
| 					processedRow.put("Exception", exceptionMessage); | ||||
| 				} | ||||
| 				processedData.add(processedRow); | ||||
| 			} | ||||
| 		} catch (Exception e) { | ||||
| 			// Handle any exceptions here if needed | ||||
| 		} | ||||
| 		return processedData; | ||||
| 	} | ||||
| 
 | ||||
| 
 | ||||
| 	 | ||||
| 	private Workbook createWorkbook(List<Map<String, Object>> processedData) { | ||||
| 	    Workbook workbook = new XSSFWorkbook(); | ||||
| 	    Sheet sheet = workbook.createSheet("Failed Records"); | ||||
| 		Workbook workbook = new XSSFWorkbook(); | ||||
| 		Sheet sheet = workbook.createSheet("Failed Records"); | ||||
| 
 | ||||
| 	    // Add column headers to the worksheet | ||||
| 	    Row headerRow = sheet.createRow(0); | ||||
| 	    int columnIndex = 0; | ||||
| 	    for (String key : processedData.get(0).keySet()) { | ||||
| 	        Cell cell = headerRow.createCell(columnIndex++); | ||||
| 	        cell.setCellValue(key); | ||||
| 	    } | ||||
| 		// Add column headers to the worksheet | ||||
| 		Row headerRow = sheet.createRow(0); | ||||
| 		int columnIndex = 0; | ||||
| 		for (String key : processedData.get(0).keySet()) { | ||||
| 			Cell cell = headerRow.createCell(columnIndex++); | ||||
| 			cell.setCellValue(key); | ||||
| 		} | ||||
| 
 | ||||
| 	    // Add data rows to the worksheet | ||||
| 	    int rowIndex = 1; | ||||
| 	    for (Map<String, Object> row : processedData) { | ||||
| 	        Row dataRow = sheet.createRow(rowIndex++); | ||||
| 	        columnIndex = 0; | ||||
| 	        for (Object value : row.values()) { | ||||
| 	            Cell cell = dataRow.createCell(columnIndex++); | ||||
| 	            cell.setCellValue(value.toString()); | ||||
| 	        } | ||||
| 	    } | ||||
| 		// Add data rows to the worksheet | ||||
| 		int rowIndex = 1; | ||||
| 		for (Map<String, Object> row : processedData) { | ||||
| 			Row dataRow = sheet.createRow(rowIndex++); | ||||
| 			columnIndex = 0; | ||||
| 			for (Object value : row.values()) { | ||||
| 				Cell cell = dataRow.createCell(columnIndex++); | ||||
| 				cell.setCellValue(value.toString()); | ||||
| 			} | ||||
| 		} | ||||
| 
 | ||||
| 	    return workbook; | ||||
| 		return workbook; | ||||
| 	} | ||||
| 
 | ||||
| 	private Resource createExcelResource(Workbook workbook) throws IOException { | ||||
| 	    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||||
| 	    workbook.write(outputStream); | ||||
| 	    ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); | ||||
| 	    return new InputStreamResource(inputStream); | ||||
| 		ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||||
| 		workbook.write(outputStream); | ||||
| 		ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); | ||||
| 		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); | ||||
| //	} | ||||
| //} | ||||
| 
 | ||||
| } | ||||
|  | ||||
| @ -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); | ||||
| 				} | ||||
| 
 | ||||
| 
 | ||||
|  | ||||
| @ -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; | ||||
| 
 | ||||
| @ -15,22 +13,20 @@ import com.realnet.Notification.Repository.NotRepo; | ||||
| @RestController | ||||
| @RequestMapping("/notification") | ||||
| public class NotController { | ||||
| 	 | ||||
| 
 | ||||
| 	@Autowired | ||||
| 	private NotRepo notRepo; | ||||
| 	 | ||||
| 
 | ||||
| //	@PostMapping("/save_notification") | ||||
| //	  public NotEntity Savedata(@RequestBody NotEntity entity) { | ||||
| //		NotEntity dash = notRepo.save(entity)	; | ||||
| //		 return dash; | ||||
| //	  } | ||||
| 		  | ||||
| 	 | ||||
| 
 | ||||
| 	@GetMapping("/get_notification") | ||||
| 	public List<NotEntity> getdetails() { | ||||
| 		 List<NotEntity> dash = notRepo.findTopByOrderByd();		 | ||||
| 		List<NotEntity> dash = notRepo.findTopByOrderByd(); | ||||
| 		return dash; | ||||
| 	} | ||||
| 	 | ||||
| 
 | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user