Skip to content

Commit

Permalink
Merge pull request #45 from ArpithaSureshappa/cbrelease-4.8.19
Browse files Browse the repository at this point in the history
file format error added
  • Loading branch information
sureshece16 authored Nov 21, 2024
2 parents e16fa13 + f5cb3a4 commit 28007ad
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.igot.cios.dto.DeleteContentRequestDto;
import com.igot.cios.dto.RequestDto;
import com.igot.cios.dto.SBApiResponse;
import com.igot.cios.entity.FileInfoEntity;
import com.igot.cios.exception.CiosContentException;
import com.igot.cios.service.CiosContentService;
Expand All @@ -16,6 +17,7 @@
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

Expand All @@ -27,17 +29,13 @@ public class CiosContentController {
CiosContentService ciosContentService;

@PostMapping(value = "/v1/loadContentFromExcel/{partnercode}/{partnerId}", consumes = "multipart/form-data")
public ResponseEntity<Object> loadContentFromExcel(
public ResponseEntity<SBApiResponse> loadContentFromExcel(
@RequestParam(value = "file") MultipartFile file,
@PathVariable("partnercode") String partnerCode,
@PathVariable("partnerId") String partnerId){
try {
ciosContentService.loadContentFromExcel(file, partnerCode, partnerId);
return ResponseEntity.status(HttpStatus.OK).body(new HashMap<>());
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body("Error during loading of content from excel: " + e.getMessage());
}
@PathVariable("partnerId") String partnerId) throws IOException {

SBApiResponse apiResponse = ciosContentService.loadContentFromExcel(file, partnerCode, partnerId);
return new ResponseEntity<>(apiResponse, apiResponse.getResponseCode());
}

@PostMapping(value = "/v1/readAllContentFromDb")
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/igot/cios/service/CiosContentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.igot.cios.dto.DeleteContentRequestDto;
import com.igot.cios.dto.PaginatedResponse;
import com.igot.cios.dto.RequestDto;
import com.igot.cios.dto.SBApiResponse;
import com.igot.cios.entity.FileInfoEntity;
import com.igot.cios.util.elasticsearch.dto.SearchCriteria;
import com.igot.cios.util.elasticsearch.dto.SearchResult;
Expand All @@ -17,7 +18,7 @@

@Service
public interface CiosContentService {
void loadContentFromExcel(MultipartFile file, String partnerCode, String partnerId) throws IOException;
SBApiResponse loadContentFromExcel(MultipartFile file, String partnerCode, String partnerId) throws IOException;

PaginatedResponse<?> fetchAllContentFromSecondaryDb(RequestDto dto);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
import com.igot.cios.repository.FileInfoRepository;
import com.igot.cios.service.CiosContentService;
import com.igot.cios.storage.StoreFileToGCP;
import com.igot.cios.util.CbServerProperties;
import com.igot.cios.util.Constants;
import com.igot.cios.util.PayloadValidation;
import com.igot.cios.util.*;
import com.igot.cios.util.elasticsearch.dto.SearchCriteria;
import com.igot.cios.util.elasticsearch.dto.SearchResult;
import com.igot.cios.util.elasticsearch.service.EsUtilService;
Expand Down Expand Up @@ -74,13 +72,21 @@ public class CiosContentServiceImpl implements CiosContentService {
private CbServerProperties cbServerProperties;

@Override
public void loadContentFromExcel(MultipartFile file, String partnerCode, String partnerId) {
public SBApiResponse loadContentFromExcel(MultipartFile file, String partnerCode, String partnerId) {
log.info("CiosContentServiceImpl::loadContentFromExcel");
SBApiResponse response = SBApiResponse.createDefaultResponse(Constants.API_CIOS_LOAD_EXCEL_CONTENT);
String fileName = file.getOriginalFilename();
Timestamp initiatedOn = new Timestamp(System.currentTimeMillis());
String fileId = dataTransformUtility.createFileInfo(partnerId, null, fileName, initiatedOn, null, Constants.CONTENT_UPLOAD_IN_PROGRESS , null);
try {
List<Map<String, String>> processedData = dataTransformUtility.processExcelFile(file);
if(processedData == null || processedData.isEmpty()){
dataTransformUtility.createFileInfo(partnerId, fileId, fileName, initiatedOn, new Timestamp(System.currentTimeMillis()), Constants.CONTENT_UPLOAD_FAILED, null);
response.getParams().setErrmsg(Constants.FILE_FORMAT_ERROR);
response.getParams().setStatus(Constants.FAILED);
response.setResponseCode(HttpStatus.BAD_REQUEST);
return response;
}
log.info("No.of processedData from excel: " + processedData.size());
int batchSize = 500;
List<List<Map<String, String>>> batches = splitIntoBatches(processedData, batchSize);
Expand All @@ -95,11 +101,16 @@ public void loadContentFromExcel(MultipartFile file, String partnerCode, String

kafkaProducer.push(cbServerProperties.getCiosContentOnboardTopic(), batchDataMap);
log.info("Batch of size {} sent to Kafka", batch.size());
return response;
}
} catch (Exception e) {
dataTransformUtility.createFileInfo(partnerId, fileId, fileName, initiatedOn, new Timestamp(System.currentTimeMillis()), Constants.CONTENT_UPLOAD_FAILED, null);
throw new RuntimeException(e.getMessage());
response.getParams().setErrmsg(e.getMessage());
response.getParams().setStatus(Constants.FAILED);
response.setResponseCode(HttpStatus.BAD_REQUEST);
return response;
}
return null;
}

@Override
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/igot/cios/util/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ public class Constants {
public static final String INITIATED_ON = "initiatedOn";
public static final String CONTENT_UPLOAD_IN_PROGRESS = "InProgress";
public static final String PARTNER_ID = "partnerId";
public static final String API_CIOS_LOAD_EXCEL_CONTENT = "api.cios.load.excel.content";
public static final String FILE_FORMAT_ERROR = "File format is not correct, Please upload valid file format";


private Constants() {
Expand Down

0 comments on commit 28007ad

Please sign in to comment.