Commit b8741e83 authored by Sridhar Pothanaveni's avatar Sridhar Pothanaveni

File upload functionality done

parent 20a3e65d
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version> <java.version>11</java.version>
</properties> </properties>
<dependencies> <dependencies>
...@@ -115,6 +115,13 @@ ...@@ -115,6 +115,13 @@
</dependency> </dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
</dependencies> </dependencies>
......
...@@ -6,14 +6,10 @@ import com.nisum.task.service.BPNService; ...@@ -6,14 +6,10 @@ import com.nisum.task.service.BPNService;
import com.nisum.task.service.FileUploadService; import com.nisum.task.service.FileUploadService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import java.io.IOException;
@CrossOrigin(origins = "*") @CrossOrigin(origins = "*")
@Slf4j @Slf4j
...@@ -29,13 +25,13 @@ public class BPNController { ...@@ -29,13 +25,13 @@ public class BPNController {
@PostMapping("/createBPN") @PostMapping("/createBPN")
public Mono<BPN> saveBPN(@RequestBody BPN bpn) { public Mono<BPN> saveBPN(@RequestBody BPN bpn) {
log.info("saveBPN {}",bpn); log.info("saveBPN {}", bpn);
return bPNService.saveBPN(bpn); return bPNService.saveBPN(bpn);
} }
@GetMapping("/findById/{bpnID}") @GetMapping("/findById/{bpnID}")
public Mono<BPN> getBPN(@PathVariable String bpnID) { public Mono<BPN> getBPN(@PathVariable String bpnID) {
log.info("getBPN {}",bpnID); log.info("getBPN {}", bpnID);
return bPNService.findById(Long.valueOf(bpnID)); return bPNService.findById(Long.valueOf(bpnID));
} }
...@@ -48,7 +44,7 @@ public class BPNController { ...@@ -48,7 +44,7 @@ public class BPNController {
@PostMapping("/processBPN") @PostMapping("/processBPN")
public Mono<BPN> processBPN(@RequestBody BPN bpn) { public Mono<BPN> processBPN(@RequestBody BPN bpn) {
log.info("processBPN {}",bpn); log.info("processBPN {}", bpn);
return bPNService.processBPN(bpn); return bPNService.processBPN(bpn);
} }
...@@ -60,40 +56,36 @@ public class BPNController { ...@@ -60,40 +56,36 @@ public class BPNController {
// return fileUploadService.uploadFile(file); // return fileUploadService.uploadFile(file);
// } // }
@RequestMapping(value = "/upload", method = RequestMethod.PUT,consumes="multipart/form-data") // @RequestMapping(value = "/upload", method = RequestMethod.PUT,consumes="multipart/form-data")
@ResponseStatus(HttpStatus.OK) // @ResponseStatus(HttpStatus.OK)
public Flux<BPN> uploadFile(@RequestParam("file") MultipartFile file) throws IOException { // public Flux<BPN> uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
log.info("file upload"); // log.info("file upload");
//
// Log details of the file // // Log details of the file
log.info("File Name: {}", file.getOriginalFilename()); // log.info("File Name: {}", file.getOriginalFilename());
log.info("Content Type: {}", file.getContentType()); // log.info("Content Type: {}", file.getContentType());
log.info("File Size: {}", file.getSize()); // log.info("File Size: {}", file.getSize());
//
return fileUploadService.uploadFile(file); // return fileUploadService.uploadFile(file);
} // }
@PostMapping("/uploadFile")
public ResponseEntity<BPN> uploadFiles(@RequestParam("file") MultipartFile file) {
String message = "";
try {
fileUploadService.save(file);
message = "Uploaded the file successfully: " + file.getOriginalFilename(); // @PostMapping("/uploadFile")
return ResponseEntity.status(HttpStatus.OK).body(new BPN()); // public ResponseEntity<BPN> uploadFiles(@RequestParam("file") MultipartFile file) {
} catch (Exception e) { // try {
message = "Could not upload the file: " + file.getOriginalFilename() + ". Error: " + e.getMessage(); // fileUploadService.save(file);
return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(new BPN()); //
} // return ResponseEntity.status(HttpStatus.OK).body(new BPN());
} // } catch (Exception e) {
// return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(new BPN());
// }
// }
@PostMapping("/createMultipleBPN") @PostMapping("/createMultipleBPN")
public Flux<BPN> saveBPN(@RequestBody BPNDto bpnDto) { public Flux<BPN> saveBPN(@RequestBody BPNDto bpnDto) {
log.info("createMultipleBPN {}",bpnDto); log.info("createMultipleBPN {}", bpnDto);
return bPNService.saveMultipleBPN(bpnDto); return bPNService.saveMultipleBPN(bpnDto);
} }
} }
...@@ -4,13 +4,12 @@ package com.nisum.task.controller; ...@@ -4,13 +4,12 @@ package com.nisum.task.controller;
import com.nisum.task.entity.BPN; import com.nisum.task.entity.BPN;
import com.nisum.task.service.FileUploadService; import com.nisum.task.service.FileUploadService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.codec.multipart.FilePart;
import org.springframework.http.codec.multipart.Part;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import java.io.IOException; import java.io.IOException;
...@@ -18,53 +17,25 @@ import java.io.IOException; ...@@ -18,53 +17,25 @@ import java.io.IOException;
//@CrossOrigin(origins = "*") //@CrossOrigin(origins = "*")
@RestController @RestController
@RequestMapping("/file") @RequestMapping("/file")
@Slf4j
public class FileUploadController { public class FileUploadController {
@Autowired @Autowired
private FileUploadService fileUploadService; private FileUploadService fileUploadService;
@PostMapping("/uploadFile")
public Flux<BPN> uploadExcelFile(@RequestParam("file") MultipartFile file) throws IOException {
log.info("file upload");
return fileUploadService.uploadFile(file);
}
private static final Logger log = LoggerFactory.getLogger(FileUploadController.class);
@PostMapping("/import")
@ResponseStatus(HttpStatus.OK)
public ResponseEntity<String> importFile(@RequestParam("file") MultipartFile file) {
log.info("Received file: {}", file.getOriginalFilename());
// You can perform further operations with the file, e.g., save it to the server
// For demonstration purposes, just log the file details
try {
byte[] fileBytes = file.getBytes();
// Perform operations with fileBytes as needed
return ResponseEntity.ok("File uploaded successfully!");
} catch (IOException e) {
log.error("Error processing the file: {}", e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error processing the file.");
}
}
@PostMapping("/upload")
public ResponseEntity<String> uploadExcel(@RequestParam("file") MultipartFile file) {
fileUploadService.processExcelSheet(file);
return ResponseEntity.ok("File uploaded successfully!");
}
@PostMapping("/importExcel") @PostMapping(value = "/uploadExcel", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> importExcel(@RequestParam("excel") MultipartFile file,BPN bpn) { public Flux<BPN> uploadHandler(@RequestBody Flux<Part> parts) {
try { log.info("file upload {}", parts);
fileUploadService.processExcelSheet(file); return parts
return ResponseEntity.ok("File uploaded successfully!"); .filter(part -> part instanceof FilePart) // only retain file parts
} catch (Exception e) { .ofType(FilePart.class) // convert the flux to FilePart
return ResponseEntity.badRequest().body("Bad Request ss: " + e.getMessage()); .flatMap(filePart -> {
} try {
return fileUploadService.saveExcelData(filePart);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
} }
} }
...@@ -21,8 +21,8 @@ public class BPN { ...@@ -21,8 +21,8 @@ public class BPN {
private String rog; private String rog;
private String fac; private String fac;
private String forceOptCd; private String forceOptCd;
private Date firstEffectiveDate; private String firstEffectiveDate;
private Date lasttEffectiveDate; private String lasttEffectiveDate;
private String bpnStatus; private String bpnStatus;
private String bpnProcessMessage; private String bpnProcessMessage;
private Date createdTime; private Date createdTime;
......
...@@ -20,7 +20,7 @@ public class BPNService { ...@@ -20,7 +20,7 @@ public class BPNService {
private BPNRepository bpnRepository; private BPNRepository bpnRepository;
public Mono<BPN> saveBPN(BPN bpn) { public Mono<BPN> saveBPN(BPN bpn) {
bpn.setFirstEffectiveDate(new Date()); bpn.setFirstEffectiveDate(new Date().toString());
bpn.setCreatedTime(new Date()); bpn.setCreatedTime(new Date());
bpn.setUpdatedTime(new Date()); bpn.setUpdatedTime(new Date());
log.info("bpn {} ", bpn); log.info("bpn {} ", bpn);
...@@ -89,7 +89,7 @@ public class BPNService { ...@@ -89,7 +89,7 @@ public class BPNService {
private BPN setAllFieldsToEntity(BPN bpnDB, BPNDto uiBPN) { private BPN setAllFieldsToEntity(BPN bpnDB, BPNDto uiBPN) {
bpnDB.setBpnStatus(uiBPN.getForceOptCd()); bpnDB.setBpnStatus(uiBPN.getForceOptCd());
bpnDB.setLasttEffectiveDate(uiBPN.getLasttEffectiveDate()); bpnDB.setLasttEffectiveDate(uiBPN.getLasttEffectiveDate().toString());
bpnDB.setUpdatedTime(new Date()); bpnDB.setUpdatedTime(new Date());
bpnDB.setFac(uiBPN.getFac() != null ? uiBPN.getFac() : ""); bpnDB.setFac(uiBPN.getFac() != null ? uiBPN.getFac() : "");
bpnDB.setForceOptCd(uiBPN.getForceOptCd()); bpnDB.setForceOptCd(uiBPN.getForceOptCd());
......
...@@ -2,21 +2,22 @@ package com.nisum.task.service; ...@@ -2,21 +2,22 @@ package com.nisum.task.service;
import com.nisum.task.entity.BPN; import com.nisum.task.entity.BPN;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.codec.multipart.FilePart;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.file.FileAlreadyExistsException; import java.nio.ByteBuffer;
import java.nio.file.Files; import java.nio.file.*;
import java.nio.file.Path; import java.text.DecimalFormat;
import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.List; import java.util.List;
@Service @Service
...@@ -25,54 +26,35 @@ public class FileUploadService { ...@@ -25,54 +26,35 @@ public class FileUploadService {
@Autowired @Autowired
private BPNService bpnService; private BPNService bpnService;
private final Path root = Paths.get("uploads");
public Flux<BPN> saveExcelData(FilePart filePart) throws IOException {
public Flux<BPN> uploadFile(MultipartFile file) throws IOException { Flux<DataBuffer> dataBufferFlux = filePart.content();
// Convert the Flux of DataBuffers to a byte array
byte[] bytes = dataBufferFlux.collectList().block().stream()
.map(DataBuffer::asByteBuffer)
.reduce(ByteBuffer::put)
.orElse(ByteBuffer.allocate(0))
.array();
List<BPN> tempBPNList = new ArrayList<>(); List<BPN> tempBPNList = new ArrayList<>();
XSSFWorkbook workbook = new XSSFWorkbook(file.getInputStream()); XSSFWorkbook workbook = new XSSFWorkbook(new ByteArrayInputStream(bytes));
XSSFSheet worksheet = workbook.getSheetAt(0); XSSFSheet worksheet = workbook.getSheetAt(0);
log.info("worksheet {} ", worksheet);
for (int i = 1; i < worksheet.getPhysicalNumberOfRows(); i++) { for (int i = 1; i < worksheet.getPhysicalNumberOfRows(); i++) {
BPN tempBPN = new BPN(); BPN tempBPN = new BPN();
XSSFRow row = worksheet.getRow(i); XSSFRow row = worksheet.getRow(i);
tempBPN.setBpn(String.valueOf(row.getCell(0))); tempBPN.setBpn(String.format("%.0f", row.getCell(0).getNumericCellValue()));
tempBPN.setForceOptCd(String.valueOf(row.getCell(1))); tempBPN.setForceOptCd(String.valueOf(row.getCell(1)));
tempBPN.setLasttEffectiveDate(String.valueOf(row.getCell(2)));
tempBPNList.add(tempBPN); tempBPNList.add(tempBPN);
}
log.info("Data {}",tempBPNList);
return Flux.empty();
}
public void save(MultipartFile file) {
try {
Files.createDirectories(root);
Files.copy(file.getInputStream(), this.root.resolve(file.getOriginalFilename()));
} catch (Exception e) {
if (e instanceof FileAlreadyExistsException) {
throw new RuntimeException("A file of that name already exists.");
}
throw new RuntimeException(e.getMessage());
} }
log.info("Data {}", tempBPNList);
return Flux.fromIterable(tempBPNList).flatMap(bpn -> bpnService.saveBPN(bpn));
} }
public void processExcelSheet(MultipartFile file) {
try {
Files.createDirectories(root);
Files.copy(file.getInputStream(), this.root.resolve(file.getOriginalFilename()));
log.info("file imported successfully");
} catch (Exception e) {
if (e instanceof FileAlreadyExistsException) {
throw new RuntimeException("A file of that name already exists.");
}
throw new RuntimeException(e.getMessage());
}
}
} }
...@@ -38,7 +38,7 @@ public class SmicService { ...@@ -38,7 +38,7 @@ public class SmicService {
return bpnService.findByRog(smic.getRog()).flatMap(bpn -> { return bpnService.findByRog(smic.getRog()).flatMap(bpn -> {
bpn.setBpnStatus(smic.getForceOptCd()); bpn.setBpnStatus(smic.getForceOptCd());
bpn.setForceOptCd(smic.getForceOptCd()); bpn.setForceOptCd(smic.getForceOptCd());
bpn.setLasttEffectiveDate(smic.getLasttEffectiveDate()); bpn.setLasttEffectiveDate(smic.getLasttEffectiveDate().toString());
return bpnService.saveBPN(bpn); return bpnService.saveBPN(bpn);
}); });
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment