Updated Document entity to work with sourceId instead of callId

This commit is contained in:
harish
2024-09-10 12:16:59 +05:30
parent 2d5aeb95b5
commit 4929dc01ce
11 changed files with 94 additions and 40 deletions

View File

@@ -6,6 +6,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import net.gepafin.tendermanagement.enums.DocumentSourceTypeEnum;
import net.gepafin.tendermanagement.model.response.*;
import net.gepafin.tendermanagement.service.FaqService;
import net.gepafin.tendermanagement.service.LookUpDataService;
@@ -167,14 +168,14 @@ public class CallDao {
}
public List<DocumentEntity> convertToDocumentEntities(List<DocumentReq> documentReqList, CallEntity callEntity,
public List<DocumentEntity> convertToDocumentEntities(List<DocumentReq> documentReqList, Long sourceId,
DocumentTypeEnum documentType) {
if (documentReqList == null) {
return null;
}
List<DocumentEntity> existingDocuments = documentRepository
.findByCallIdAndTypeAndIsDeletedFalse(callEntity.getId(), documentType.getValue());
.findBySourceIdAndTypeAndIsDeletedFalse(sourceId, documentType.getValue());
List<Long> incomingIds = documentReqList.stream().map(DocumentReq::getId).filter(id -> id != null && id > 0)
.collect(Collectors.toList());
@@ -182,7 +183,7 @@ public class CallDao {
existingDocuments.stream().filter(document -> !incomingIds.contains(document.getId()))
.forEach(this::softDeleteDocument);
List<DocumentEntity> documentEntities = documentReqList.stream()
.map(req -> convertToDocumentEntity(req, callEntity)).collect(Collectors.toList());
.map(req -> convertToDocumentEntity(req, sourceId)).collect(Collectors.toList());
documentRepository.saveAll(documentEntities);
return documentEntities;
}
@@ -192,9 +193,9 @@ public class CallDao {
documentRepository.save(documentEntity);
}
private DocumentEntity convertToDocumentEntity(DocumentReq documentReq, CallEntity callEntity) {
private DocumentEntity convertToDocumentEntity(DocumentReq documentReq,Long sourceId) {
validateDocumentEntity(documentReq.getId());
DocumentEntity documentEntity = documentRepository.findByIdAndCallIdAndIsDeletedFalse(documentReq.getId(), callEntity.getId())
DocumentEntity documentEntity = documentRepository.findByIdAndSourceIdAndIsDeletedFalse(documentReq.getId(),sourceId)
.orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND,
Translator.toLocale(GepafinConstant.DOCUMENT_NOT_FOUND)));
return documentEntity;
@@ -275,7 +276,9 @@ public class CallDao {
DocumentResponseBean responseBean = new DocumentResponseBean();
responseBean.setId(entity.getId());
responseBean.setName(entity.getFileName());
responseBean.setDescription(entity.getDescription());
responseBean.setType(DocumentTypeEnum.valueOf(entity.getType()));
responseBean.setSource(DocumentSourceTypeEnum.valueOf(entity.getSource()));
responseBean.setSourceId(entity.getSourceId());
responseBean.setFilePath(entity.getFilePath());
responseBean.setCreatedDate(entity.getCreatedDate());
responseBean.setUpdatedDate(entity.getUpdatedDate());
@@ -362,9 +365,9 @@ public class CallDao {
callRepository.save(callEntity);
convertToEvaluationCriteriaEntities(createCallRequest.getCriteria(), callEntity, LookUpDataTypeEnum.EVALUATION_CRITERIA);
convertToDocumentEntities(createCallRequest.getDocs(), callEntity, DocumentTypeEnum.DOCUMENT);
convertToDocumentEntities(createCallRequest.getDocs(), callEntity.getId(), DocumentTypeEnum.DOCUMENT);
convertToDocumentEntities(createCallRequest.getImages(), callEntity, DocumentTypeEnum.IMAGES);
convertToDocumentEntities(createCallRequest.getImages(), callEntity.getId(), DocumentTypeEnum.IMAGES);
updateLookUpData(callEntity, createCallRequest.getCheckList(), LookUpDataTypeEnum.CHECKLIST);
@@ -523,9 +526,9 @@ public class CallDao {
}
private CallResponse getCallResponseBean(CallEntity callEntity) {
List<DocumentEntity> documentEntities = documentRepository.findByCallIdAndTypeAndIsDeletedFalse(callEntity.getId(),
List<DocumentEntity> documentEntities = documentRepository.findBySourceIdAndTypeAndIsDeletedFalse(callEntity.getId(),
DocumentTypeEnum.DOCUMENT.getValue());
List<DocumentEntity> imageEntities = documentRepository.findByCallIdAndTypeAndIsDeletedFalse(callEntity.getId(),
List<DocumentEntity> imageEntities = documentRepository.findBySourceIdAndTypeAndIsDeletedFalse(callEntity.getId(),
DocumentTypeEnum.IMAGES.getValue());
List<LookUpDataResponse> amiedTo = callTargetAudienceChecklistRepository
.findByCallIdAndLookupDataTypeAndIsDeletedFalse(callEntity.getId(), LookUpDataTypeEnum.AIMED_TO.getValue()).stream()

View File

@@ -3,6 +3,7 @@ package net.gepafin.tendermanagement.dao;
import java.io.IOException;
import java.util.stream.Collectors;
import net.gepafin.tendermanagement.enums.DocumentSourceTypeEnum;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@@ -40,16 +41,17 @@ public class DocumentDao {
@Autowired
private CallService callService;
public List<DocumentResponseBean> uploadFiles(List<MultipartFile> files, Long callId, DocumentTypeEnum fileType) {
public List<DocumentResponseBean> uploadFiles(List<MultipartFile> files, Long sourceId, DocumentSourceTypeEnum sourceType, DocumentTypeEnum fileType) {
List<DocumentEntity> documentEntities = new ArrayList<>();
CallEntity callEntity = callService.validateCall(callId);
Long source = resolveSourceId(sourceId, sourceType);
for (MultipartFile file : files) {
try {
uploadFileOnAmazonS3 result = uploadFileOnAmazonS3(file);
if (result != null) {
DocumentEntity documentEntity = new DocumentEntity();
documentEntity.setFileName(result.fileName());
documentEntity.setCall(callEntity);
documentEntity.setSource(sourceType.getValue());
documentEntity.setSourceId(source);
documentEntity.setType(fileType.getValue());
documentEntity.setFilePath(result.filepath());
documentEntity.setIsDeleted(false);
@@ -61,6 +63,18 @@ public class DocumentDao {
documentRepository.saveAll(documentEntities);
return documentEntities.stream().map(callDao::convertToDocumentResponseBean).collect(Collectors.toList());
}
private Long resolveSourceId(Long sourceId, DocumentSourceTypeEnum sourceType) {
if (sourceType == DocumentSourceTypeEnum.CALL) {
CallEntity callEntity = callService.validateCall(sourceId);
return callEntity.getId();
}
// else if (sourceType == SourceTypeEnum.APPLICATION) {
// ApplicationEntity applicationEntity = applicationService.validateApplication(sourceId);
// return applicationEntity.getId(); // Assuming ApplicationEntity has getId()
// }
//
return sourceId;
}
private uploadFileOnAmazonS3 uploadFileOnAmazonS3(MultipartFile file) throws IOException {
String extension = FilenameUtils.getExtension(file.getOriginalFilename());
@@ -109,6 +123,8 @@ public class DocumentDao {
documentEntity.setFilePath(result.filepath);
documentEntity.setFileName(result.fileName);
documentEntity.setType(documentTypeEnum.getValue());
documentEntity.setSource(documentEntity.getSource());
documentEntity.setSourceId(documentEntity.getSourceId());
documentRepository.save(documentEntity);
}
return callDao.convertToDocumentResponseBean(documentEntity);