111 lines
4.5 KiB
Java
111 lines
4.5 KiB
Java
package net.gepafin.tendermanagement.dao;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
import net.gepafin.tendermanagement.enums.DocumentSourceTypeEnum;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
import net.gepafin.tendermanagement.config.Translator;
|
|
import net.gepafin.tendermanagement.constants.GepafinConstant;
|
|
import net.gepafin.tendermanagement.entities.CallEntity;
|
|
import net.gepafin.tendermanagement.entities.DocumentEntity;
|
|
import net.gepafin.tendermanagement.enums.DocumentTypeEnum;
|
|
import net.gepafin.tendermanagement.model.response.DocumentResponseBean;
|
|
import net.gepafin.tendermanagement.model.response.UploadFileOnAmazonS3Response;
|
|
import net.gepafin.tendermanagement.repositories.DocumentRepository;
|
|
import net.gepafin.tendermanagement.service.AmazonS3Service;
|
|
import net.gepafin.tendermanagement.service.CallService;
|
|
import net.gepafin.tendermanagement.web.rest.api.errors.ResourceNotFoundException;
|
|
import net.gepafin.tendermanagement.web.rest.api.errors.Status;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
@Component
|
|
public class DocumentDao {
|
|
|
|
@Autowired
|
|
private AmazonS3Service amazonS3Service;
|
|
|
|
@Autowired
|
|
private DocumentRepository documentRepository;
|
|
|
|
@Autowired
|
|
private CallDao callDao;
|
|
|
|
@Autowired
|
|
private CallService callService;
|
|
|
|
@Value("${aws.s3.url.folder}")
|
|
private String s3Folder;
|
|
|
|
public List<DocumentResponseBean> uploadFiles(List<MultipartFile> files, Long sourceId, DocumentSourceTypeEnum sourceType, DocumentTypeEnum fileType) {
|
|
List<DocumentEntity> documentEntities = new ArrayList<>();
|
|
Long source = resolveSourceId(sourceId, sourceType);
|
|
for (MultipartFile file : files) {
|
|
UploadFileOnAmazonS3Response uploadFileOnAmazonS3Response = amazonS3Service.uploadFileOnAmazonS3(s3Folder,
|
|
file);
|
|
if (uploadFileOnAmazonS3Response != null) {
|
|
DocumentEntity documentEntity = new DocumentEntity();
|
|
documentEntity.setFileName(uploadFileOnAmazonS3Response.getFileName());
|
|
documentEntity.setSource(sourceType.getValue());
|
|
documentEntity.setSourceId(source);
|
|
documentEntity.setType(fileType.getValue());
|
|
documentEntity.setFilePath(uploadFileOnAmazonS3Response.getFilePath());
|
|
documentEntity.setIsDeleted(false);
|
|
documentEntities.add(documentEntity);
|
|
}
|
|
}
|
|
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;
|
|
}
|
|
|
|
public void deleteFile(Long documentId) {
|
|
DocumentEntity documentEntity = validateDocument(documentId);
|
|
// String fileName= Utils.extractFileName(documentEntity.getFilePath());
|
|
// deleteFileOnAmazonS3(fileName);
|
|
documentEntity.setIsDeleted(true);
|
|
documentRepository.save(documentEntity);
|
|
}
|
|
|
|
|
|
public DocumentEntity validateDocument(Long id) {
|
|
return documentRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND,
|
|
Translator.toLocale(GepafinConstant.DOCUMENT_NOT_FOUND)));
|
|
}
|
|
|
|
public DocumentResponseBean updateDocument(Long documentId, MultipartFile file, DocumentTypeEnum documentTypeEnum) {
|
|
DocumentEntity documentEntity = validateDocument(documentId);
|
|
UploadFileOnAmazonS3Response uploadFileOnAmazonS3Response = amazonS3Service.uploadFileOnAmazonS3(s3Folder, file);
|
|
if (uploadFileOnAmazonS3Response != null) {
|
|
documentEntity.setFileName(uploadFileOnAmazonS3Response.getFileName());
|
|
documentEntity.setFilePath(uploadFileOnAmazonS3Response.getFilePath());
|
|
documentEntity.setType(documentTypeEnum.getValue());
|
|
documentEntity.setSource(documentEntity.getSource());
|
|
documentEntity.setSourceId(documentEntity.getSourceId());
|
|
documentRepository.save(documentEntity);
|
|
}
|
|
return callDao.convertToDocumentResponseBean(documentEntity);
|
|
}
|
|
|
|
public DocumentResponseBean getDocument(Long documentId) {
|
|
DocumentEntity documentEntity = validateDocument(documentId);
|
|
return callDao.convertToDocumentResponseBean(documentEntity);
|
|
}
|
|
}
|