This commit is contained in:
string 2025-08-13 11:06:28 +05:30
parent 778cf14bdc
commit 901b95ffa2
3 changed files with 322 additions and 83 deletions

View File

@ -0,0 +1,258 @@
////package com.realnet.excel.controller;
//package com.realnet.template.controller;
//
//import java.io.ByteArrayInputStream;
//import java.io.ByteArrayOutputStream;
//import java.io.IOException;
//
//import org.apache.poi.ss.usermodel.Cell;
//import org.apache.poi.ss.usermodel.Row;
//import org.apache.poi.ss.usermodel.Sheet;
//import org.apache.poi.ss.usermodel.Workbook;
//import org.apache.poi.xssf.usermodel.XSSFWorkbook;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.core.io.InputStreamResource;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.MediaType;
//import org.springframework.http.ResponseEntity;
//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.RequestMapping;
//import org.springframework.web.bind.annotation.RequestParam;
//import org.springframework.web.bind.annotation.RestController;
//import org.springframework.web.multipart.MultipartFile;
//
//import com.fasterxml.jackson.core.JsonProcessingException;
//import com.fasterxml.jackson.databind.JsonMappingException;
//import com.realnet.template.entity.TemplateFileUpload;
//import com.realnet.template.repository.TemplatedataRepo;
//import com.realnet.template.service.FileUploadService;
//import com.realnet.users.entity1.AppUser;
//import com.realnet.users.service1.AppUserServiceImpl;
//
//@RestController
//@RequestMapping("api/template")
//public class Controller {
// @Autowired
// private TemplatedataRepo temprepo;
//
// @Autowired
// FileUploadService fileupload;
// @Autowired
// private AppUserServiceImpl userService;
////
//// @Autowired
//// private FileUploadService fileUploadService;
//
//// @Autowired
//// private CommFileuploadhelper fileuploadhelper;
//
// @GetMapping("/demo/download/{file_type}")
// public ResponseEntity<?> demoTemplate(@PathVariable String file_type) throws IOException {
//
// if (file_type.equalsIgnoreCase("Customer")) {
//
// String filename = "Customer" + ".xlsx";
// String[] header = { "Priority Name", "Description", "Is Active", "Effective Start Date",
// "Effective End date" };
// ByteArrayInputStream in = demoTemplate(header, file_type);
// InputStreamResource file = new InputStreamResource(in);
// return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + filename)
// .contentType(MediaType.parseMediaType("application/vnd.ms-excel")).body(file);
//
// } else if (file_type.equalsIgnoreCase("impact")) {
//
// String filename = "Sr_impact2_t" + ".xlsx";
// String[] header = { "Impact Name", "Description", "Is Active", "Effective Start Date",
// "Effective End date" };
// ByteArrayInputStream in = demoTemplate(header, file_type);
// InputStreamResource file = new InputStreamResource(in);
// return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + filename)
// .contentType(MediaType.parseMediaType("application/vnd.ms-excel")).body(file);
//
// }
//
// return new ResponseEntity<String>("Not Found", HttpStatus.BAD_REQUEST);
//
// }
//
// public static ByteArrayInputStream demoTemplate(String[] HEADERs, String file_type) throws IOException {
// String SHEET = file_type;
// Workbook workbook = new XSSFWorkbook();
// ByteArrayOutputStream out = new ByteArrayOutputStream();
// Sheet sheet = workbook.createSheet(SHEET);
// Row headerRow = sheet.createRow(0);
// for (int col = 0; col < HEADERs.length; col++) {
// Cell cell = headerRow.createCell(col);
// cell.setCellValue(HEADERs[col]);
// }
// workbook.write(out);
// return new ByteArrayInputStream(out.toByteArray());
//
// }
//
// @GetMapping("/getalltemplate")
// public ResponseEntity<?> getALlTemplate() {
// return new ResponseEntity<>(temprepo.findAll(), HttpStatus.ACCEPTED);
// }
//
// @GetMapping("/gettemplatebyid/{id}")
// public ResponseEntity<?> getTemplateById(@PathVariable Long id) {
// return new ResponseEntity<>(fileupload.getTemplatebyid(id), HttpStatus.ACCEPTED);
// }
//
// @PostMapping("/save/{entityName}")
// public ResponseEntity<?> post(@RequestParam(required = false) MultipartFile file, @PathVariable String entityName)
// throws JsonMappingException, JsonProcessingException {
//
// TemplateFileUpload job;
// AppUser loggedInUser = userService.getLoggedInUser();
//
//// job = new ObjectMapper().readValue(entityName, TemplateFileUpload.class);
//
// if (file != null) {
// System.out.println(file.getOriginalFilename());
//
//// boolean f =
//// fileUploadService.uploadFile(file);
//
// fileupload.uploadFile(file, loggedInUser.getUserId(), entityName);
//
// }
//
// else {
// System.out.println("erro");
// }
//
//// TemplateFileUpload save = temprepo.save(job);
//
// return new ResponseEntity<>(HttpStatus.OK);
//
// }
//
// @DeleteMapping("/deletetemplate/{id}")
// public ResponseEntity<String> deleteTemplateFileUploadById(@PathVariable Long id) {
// try {
// fileupload.deleteTemplateFileUploadById(id);
// return ResponseEntity.ok("Template file deleted successfully");
// } catch (Exception e) {
// // Handle exceptions, e.g., if the entity with the given ID is not found
// return ResponseEntity.badRequest().body("Failed to delete template file: " + e.getMessage());
// }
// }
//
//// @PostMapping("/save/{filetype}")
//// public ResponseEntity<?> uploadFile(@RequestParam MultipartFile file, @PathVariable String filetype)
//// throws IOException, ParseException {
//// BufferedReader br;
//// InputStream is = file.getInputStream();
//// br = new BufferedReader(new InputStreamReader(is));
//// String file_name = file.getOriginalFilename();
//// AppUser loggedInUser = userService.getLoggedInUser();
////
//// if (filetype.equalsIgnoreCase("priority")) {
////
//// if (file_name.contains("Sr_priority2_t")) {
////
//// String[] header = { "Priority Name", "Description", "Is Active", "Effective Start Date",
//// "Effective End date" };
//// ArrayList<Sr_priority2_t> prioritylist = new ArrayList<Sr_priority2_t>();
////
//// Workbook workbook = WorkbookFactory.create(is);
////
////// Create a DataFormatter to format and get each cell's value as String
//// DataFormatter dataFormatter = new DataFormatter();
////
//// Sheet sheet = workbook.getSheetAt(0);
////
//// // Getting number of columns in the Sheet
//// int cols = sheet.getRow(0).getLastCellNum();
////
//// fileupload.uploadFile(file, loggedInUser.getUserId(), filetype);
////
//// for (Row row : sheet) {
////
//// if (row.getRowNum() == 0) {
//// for (int i = 0; i < cols; i++) {
//// String value = dataFormatter.formatCellValue(row.getCell(i));
////
//// if (!header[i].equalsIgnoreCase(value)) {
////
//// return new ResponseEntity<>(
//// "priority file Should have \"" + header + " \"in the header in excel file",
//// HttpStatus.BAD_REQUEST);
//// }
//// }
////
//// }
////
//// if (row.getRowNum() != 0) {
//// Date date = row.getCell(3).getDateCellValue();
//// Date date2 = row.getCell(4).getDateCellValue();
////
//// Sr_priority2_t priority = new Sr_priority2_t();
//// priority.setPriority_name(dataFormatter.formatCellValue(row.getCell(0)));
//// priority.setDescription(dataFormatter.formatCellValue(row.getCell(1)));
//// priority.setActive(Boolean.valueOf(dataFormatter.formatCellValue(row.getCell(2))));
////
//// priority.setEffective_start_date(date);
//// priority.setEffective_end_date(date2);
//// priorityrepo.save(priority);
////
//// prioritylist.add(priority);
//// }
////
//// }
////// priorityrepo.saveAll(prioritylist);
//// workbook.close();
////
//////
////
//// return new ResponseEntity<>("File Uploaded", HttpStatus.ACCEPTED);
//// }
//// return new ResponseEntity<>("File name should contain Sr_priority2_t", HttpStatus.BAD_REQUEST);
////
//// }
////
//// return new ResponseEntity<>("Something Went Wrong please try again....!!!!! ", HttpStatus.BAD_REQUEST);
////
//// }
////
////// Download template data in excel Files
////
//// @GetMapping("/download/{file_type}")
////
//// public ResponseEntity<?> getFile(@PathVariable String file_type) throws IOException {
////
//// if (file_type.equalsIgnoreCase("priority")) {
////
//// String filename = "Sr_priority2_t" + ".xlsx";
//// String[] header = { "Priority Name", "Description", "Is Active", "Effective Start Date",
//// "Effective End date" };
//// List<Sr_priority2_t> findAll = priorityrepo.findAll();
//// ByteArrayInputStream in = templateByte(findAll, header);
//// InputStreamResource file = new InputStreamResource(in);
//// return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + filename)
//// .contentType(MediaType.parseMediaType("application/vnd.ms-excel")).body(file);
////
//// } else if (file_type.equalsIgnoreCase("impact")) {
////
//// String filename = "Sr_impact2_t" + ".xlsx";
//// String[] header = { "Impact Name", "Description", "Is Active", "Effective Start Date",
//// "Effective End date" };
//// List<Sr_impact2_t> findAll = impactrepo.findAll();
//// ByteArrayInputStream in = templateByte3(findAll, header);
//// InputStreamResource file = new InputStreamResource(in);
//// return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + filename)
//// .contentType(MediaType.parseMediaType("application/vnd.ms-excel")).body(file);
////
//// }
////
//// return new ResponseEntity<String>("Not Found", HttpStatus.BAD_REQUEST);
////
//// }
//
//}

View File

@ -16,9 +16,7 @@ import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
@ -51,12 +49,6 @@ public class ExcelController {
FileUploadService fileupload;
@Autowired
private AppUserServiceImpl userService;
//
// @Autowired
// private FileUploadService fileUploadService;
// @Autowired
// private CommFileuploadhelper fileuploadhelper;
@GetMapping("/demo/download/{file_type}")
public ResponseEntity<?> demoTemplate(@PathVariable String file_type) throws IOException {
@ -163,6 +155,56 @@ public class ExcelController {
return result.toString();
}
@GetMapping("/getalltemplate")
public ResponseEntity<?> getALlTemplate() {
return new ResponseEntity<>(temprepo.findAll(), HttpStatus.ACCEPTED);
}
@GetMapping("/gettemplatebyid/{id}")
public ResponseEntity<?> getTemplateById(@PathVariable Long id) {
return new ResponseEntity<>(fileupload.getTemplatebyid(id), HttpStatus.ACCEPTED);
}
@PostMapping("/save/{entityName}")
public ResponseEntity<?> post(@RequestParam(required = false) MultipartFile file, @PathVariable String entityName)
throws JsonMappingException, JsonProcessingException {
TemplateFileUpload job;
AppUser loggedInUser = userService.getLoggedInUser();
// job = new ObjectMapper().readValue(entityName, TemplateFileUpload.class);
if (file != null) {
System.out.println(file.getOriginalFilename());
// boolean f =
// fileUploadService.uploadFile(file);
fileupload.uploadFile(file, loggedInUser.getUserId(), entityName);
}
else {
System.out.println("erro");
}
// TemplateFileUpload save = temprepo.save(job);
return new ResponseEntity<>(HttpStatus.OK);
}
@DeleteMapping("/deletetemplate/{id}")
public ResponseEntity<String> deleteTemplateFileUploadById(@PathVariable Long id) {
try {
fileupload.deleteTemplateFileUploadById(id);
return ResponseEntity.ok("Template file deleted successfully");
} catch (Exception e) {
// Handle exceptions, e.g., if the entity with the given ID is not found
return ResponseEntity.badRequest().body("Failed to delete template file: " + e.getMessage());
}
}
// @PostMapping("/save/{filetype}")
// public ResponseEntity<?> uploadFile(@RequestParam MultipartFile file, @PathVariable String filetype)
// throws IOException, ParseException {
@ -274,54 +316,4 @@ public class ExcelController {
//
// }
@GetMapping("/getalltemplate")
public ResponseEntity<?> getALlTemplate() {
return new ResponseEntity<>(temprepo.findAll(), HttpStatus.ACCEPTED);
}
@GetMapping("/gettemplatebyid/{id}")
public ResponseEntity<?> getTemplateById(@PathVariable Long id) {
return new ResponseEntity<>(fileupload.getTemplatebyid(id), HttpStatus.ACCEPTED);
}
@PostMapping("/save/{entityName}/{name}")
public ResponseEntity<?> post(@RequestParam(required = false) MultipartFile file, @PathVariable String entityName,
@PathVariable String name) throws JsonMappingException, JsonProcessingException {
TemplateFileUpload job;
AppUser loggedInUser = userService.getLoggedInUser();
// job = new ObjectMapper().readValue(entityName, TemplateFileUpload.class);
if (file != null) {
System.out.println(file.getOriginalFilename());
// boolean f =
// fileUploadService.uploadFile(file);
fileupload.uploadFile(file, loggedInUser.getUserId(), entityName, name);
}
else {
System.out.println("erro");
}
// TemplateFileUpload save = temprepo.save(job);
return new ResponseEntity<>(HttpStatus.OK);
}
@DeleteMapping("/deletetemplate/{id}")
public ResponseEntity<String> deleteTemplateFileUploadById(@PathVariable Long id) {
try {
fileupload.deleteTemplateFileUploadById(id);
return ResponseEntity.ok("Template file deleted successfully");
} catch (Exception e) {
// Handle exceptions, e.g., if the entity with the given ID is not found
return ResponseEntity.badRequest().body("Failed to delete template file: " + e.getMessage());
}
}
}

View File

@ -8,14 +8,10 @@ import java.nio.file.StandardCopyOption;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import com.realnet.template.entity.TemplateFileUpload;
import com.realnet.template.repository.TemplatedataRepo;
@ -28,7 +24,7 @@ public class FileUploadService {
@Value("${projectPath}")
private String projectPath;
public String uploadFile(MultipartFile file, Long user_id, String file_type, String name) {
public String uploadFile(MultipartFile file, Long user_id, String entityname) {
try {
String filepath = "import-data";
@ -74,9 +70,8 @@ public class FileUploadService {
exceldata.setFile_name(file.getOriginalFilename());
exceldata.setFile_changed_name(str);
// exceldata.setFile_type(file_type);
exceldata.setEntity_name(file_type);
exceldata.setName(name);
// exceldata.setFile_type(file_type);
exceldata.setEntity_name(entityname);
exceldata.setUser_id(user_id);
repo.save(exceldata);
@ -88,10 +83,6 @@ public class FileUploadService {
}
}
public TemplateFileUpload getTemplatebyid(Long Id) {
TemplateFileUpload one = repo.getOne(Id);
@ -99,17 +90,15 @@ public class FileUploadService {
}
public void deleteTemplateFileUploadById(Long id) {
// Check if the entity with the given ID exists
if (repo.existsById(id)) {
repo.deleteById(id);
}
}
public TemplateFileUpload getFileById(Long id) {
// Use your JPA repository to retrieve the file by its ID
return repo.findById(id).orElse(null);
}
public void deleteTemplateFileUploadById(Long id) {
// Check if the entity with the given ID exists
if (repo.existsById(id)) {
repo.deleteById(id);
}
}
public TemplateFileUpload getFileById(Long id) {
// Use your JPA repository to retrieve the file by its ID
return repo.findById(id).orElse(null);
}
}