Done ticket GEPADINBE-6144
This commit is contained in:
@@ -33,7 +33,9 @@ import net.gepafin.tendermanagement.service.feignClient.AppointmentApiService;
|
||||
import net.gepafin.tendermanagement.service.impl.ApplicationEvaluationServiceImpl;
|
||||
import net.gepafin.tendermanagement.util.LoggingUtil;
|
||||
import net.gepafin.tendermanagement.util.Utils;
|
||||
import net.gepafin.tendermanagement.util.Validator;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.CustomValidationException;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.ResourceNotFoundException;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.Status;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -141,6 +143,12 @@ public class AppointmentDao {
|
||||
@Autowired
|
||||
private NdganagRepository ndganagRepository;
|
||||
|
||||
@Autowired
|
||||
private ApplicationSignedDocumentRepository applicationSignedDocumentRepository;
|
||||
|
||||
@Autowired
|
||||
private Validator validator;
|
||||
|
||||
private final Map<Long, ScheduledExecutorService> executorMap = new ConcurrentHashMap<>();
|
||||
|
||||
|
||||
@@ -1129,35 +1137,55 @@ public class AppointmentDao {
|
||||
return appointmentCreationRequest;
|
||||
}
|
||||
|
||||
public DocumentUploadResponse uploadDocumentToExternalSystem(Long documentId, UploadDocToExternalSystemRequest docToExternalSystemRequest) {
|
||||
public DocumentUploadResponse uploadDocumentToExternalSystem(Long documentId,Boolean isSignedDocument, UploadDocToExternalSystemRequest docToExternalSystemRequest) {
|
||||
log.info("Initiating upload to external system for documentId: {}", documentId);
|
||||
// Check if the document is already being processed
|
||||
DocumentEntity systemDoc = documentDao.validateDocument(documentId);
|
||||
|
||||
ApplicationEntity application = null;
|
||||
DocumentEntity systemDoc =null;
|
||||
ApplicationSignedDocumentEntity applicationSignedDocument =null;
|
||||
if(isSignedDocument!=null && Boolean.TRUE.equals(isSignedDocument)) {
|
||||
|
||||
if (systemDoc != null) {
|
||||
DocumentSourceTypeEnum sourceType = DocumentSourceTypeEnum.valueOf(systemDoc.getSource());
|
||||
//all flow in incomplete and testing for document for that unique id
|
||||
applicationSignedDocument = applicationSignedDocumentRepository
|
||||
.findByIdAndStatus(documentId, ApplicationSignedDocumentStatusEnum.ACTIVE.getValue());
|
||||
if(applicationSignedDocument == null) {
|
||||
log.warn("No active signed document found for id: {}", documentId);
|
||||
throw new ResourceNotFoundException(Status.NOT_FOUND,
|
||||
Translator.toLocale(GepafinConstant.APPLICATION_SIGNED_DOCUMENT_NOT_FOUND));
|
||||
}
|
||||
application=applicationDao.validateApplication(applicationSignedDocument.getApplication().getId());
|
||||
// if (Boolean.TRUE.equals(validator.isProductionProfileActivated()) && application.getCall().getId().equals(23l)) {
|
||||
// TO UPLOAD SIGNED DOCUMENT TO COMPANY DOCUMENT FOR SPECIFIC CALL 23 IN GEPAFIN
|
||||
//// MultipartFile file=amazonS3Service.getFileFromS3Path();
|
||||
// }
|
||||
}
|
||||
else {
|
||||
systemDoc = documentDao.validateDocument(documentId);
|
||||
|
||||
switch (sourceType) {
|
||||
case APPLICATION:
|
||||
application = applicationDao.validateApplication(systemDoc.getSourceId());
|
||||
break;
|
||||
case AMENDMENT:
|
||||
ApplicationAmendmentRequestEntity applicationAmendmentEntity = applicationAmendmentRequestDao.validateApplicationAmendmentRequest(systemDoc.getSourceId());
|
||||
application = applicationDao.validateApplication(applicationAmendmentEntity.getApplicationId());
|
||||
break;
|
||||
case EVALUATION:
|
||||
ApplicationEvaluationEntity applicationEvaluationEntity = applicationEvaluationDao.validateApplicationEvaluation(systemDoc.getSourceId());
|
||||
application = applicationDao.validateApplication(applicationEvaluationEntity.getApplicationId());
|
||||
break;
|
||||
|
||||
case CALL:
|
||||
break;
|
||||
if (systemDoc != null) {
|
||||
DocumentSourceTypeEnum sourceType = DocumentSourceTypeEnum.valueOf(systemDoc.getSource());
|
||||
|
||||
default:
|
||||
log.warn("Unhandled document source type: {}", sourceType);
|
||||
break;
|
||||
switch (sourceType) {
|
||||
case APPLICATION:
|
||||
application = applicationDao.validateApplication(systemDoc.getSourceId());
|
||||
break;
|
||||
case AMENDMENT:
|
||||
ApplicationAmendmentRequestEntity applicationAmendmentEntity = applicationAmendmentRequestDao.validateApplicationAmendmentRequest(systemDoc.getSourceId());
|
||||
application = applicationDao.validateApplication(applicationAmendmentEntity.getApplicationId());
|
||||
break;
|
||||
case EVALUATION:
|
||||
ApplicationEvaluationEntity applicationEvaluationEntity = applicationEvaluationDao.validateApplicationEvaluation(systemDoc.getSourceId());
|
||||
application = applicationDao.validateApplication(applicationEvaluationEntity.getApplicationId());
|
||||
break;
|
||||
|
||||
case CALL:
|
||||
break;
|
||||
|
||||
default:
|
||||
log.warn("Unhandled document source type: {}", sourceType);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1167,7 +1195,15 @@ public class AppointmentDao {
|
||||
// Authenticate the hub before proceeding
|
||||
HubEntity hub = hubRepository.findByHubId(hubId);
|
||||
authenticateAndSaveToken(hub, application);
|
||||
if (systemDoc != null && systemDoc.getDocumentAttachmentId() != null) {
|
||||
String documentAttachmentId=null;
|
||||
if (applicationSignedDocument!=null){
|
||||
documentAttachmentId=applicationSignedDocument.getDocumentAttachmentId();
|
||||
}
|
||||
if(systemDoc!=null) {
|
||||
documentAttachmentId=systemDoc.getDocumentAttachmentId();
|
||||
}
|
||||
|
||||
if (documentAttachmentId!=null) {
|
||||
// If the documentAttachmentId is already set, return the response
|
||||
log.info("Document already uploaded with documentAttachmentId: {}", systemDoc.getDocumentAttachmentId());
|
||||
DocumentUploadResponse response = new DocumentUploadResponse();
|
||||
@@ -1192,7 +1228,7 @@ public class AppointmentDao {
|
||||
threadLocalHubId.set(hubId);
|
||||
try {
|
||||
log.info("Starting async document upload for documentId: {}", documentId);
|
||||
uploadDocumentToExternalSystemSync(documentId, docToExternalSystemRequest, finalApplication);
|
||||
uploadDocumentToExternalSystemSync(documentId, docToExternalSystemRequest, finalApplication,isSignedDocument);
|
||||
} catch (Exception e) {
|
||||
log.error("Error in async document upload for documentId: {}", documentId, e);
|
||||
} finally {
|
||||
@@ -1208,10 +1244,21 @@ public class AppointmentDao {
|
||||
return null;
|
||||
}
|
||||
|
||||
private void uploadDocumentToExternalSystemSync(Long documentId, UploadDocToExternalSystemRequest docToExternalSystemRequest, ApplicationEntity application) {
|
||||
private void uploadDocumentToExternalSystemSync(Long documentId, UploadDocToExternalSystemRequest docToExternalSystemRequest, ApplicationEntity application,Boolean isSignedDocument) {
|
||||
log.info("Starting sync document upload for documentId: {}", documentId);
|
||||
// Synchronous upload logic
|
||||
DocumentEntity systemDoc = documentDao.validateDocument(documentId);
|
||||
ApplicationSignedDocumentEntity applicationSignedDocument=null;
|
||||
DocumentEntity systemDoc=null;
|
||||
String oldUrl = null;
|
||||
|
||||
if(Boolean.TRUE.equals(isSignedDocument)){
|
||||
applicationSignedDocument = applicationSignedDocumentRepository
|
||||
.findByIdAndStatus(documentId, ApplicationSignedDocumentStatusEnum.ACTIVE.getValue());
|
||||
oldUrl=applicationSignedDocument.getFilePath();
|
||||
}else {
|
||||
systemDoc = documentDao.validateDocument(documentId);
|
||||
oldUrl=systemDoc.getFilePath();
|
||||
}
|
||||
|
||||
Long hubId = threadLocalHubId.get();
|
||||
HubEntity hub = hubRepository.findByHubId(hubId);
|
||||
@@ -1222,13 +1269,17 @@ public class AppointmentDao {
|
||||
}
|
||||
|
||||
log.info("Got Document in system: {}", systemDoc);
|
||||
String oldUrl = systemDoc.getFilePath();
|
||||
String authorizationToken = getBearerToken(hub);
|
||||
|
||||
try {
|
||||
File localFile = downloadFileFromS3(oldUrl);
|
||||
MultipartFile multipartFile = convertFileToMultipartFile(localFile);
|
||||
|
||||
// delete temp file
|
||||
if (!localFile.delete()) {
|
||||
log.warn("Failed to delete temp file: {}", localFile.getAbsolutePath());
|
||||
}
|
||||
|
||||
UploadDocToExternalSystemRequest externalSystemRequest = new UploadDocToExternalSystemRequest();
|
||||
externalSystemRequest.setInput(getUploadDocumentInput(docToExternalSystemRequest));
|
||||
|
||||
@@ -1245,14 +1296,20 @@ public class AppointmentDao {
|
||||
}
|
||||
|
||||
// Save the documentAttachmentId to the database
|
||||
systemDoc.setDocumentAttachmentId(parsedResponse.getDocumentAttachmentId());
|
||||
documentRepository.save(systemDoc);
|
||||
if(systemDoc!=null) {
|
||||
systemDoc.setDocumentAttachmentId(parsedResponse.getDocumentAttachmentId());
|
||||
documentRepository.save(systemDoc);
|
||||
}
|
||||
if(applicationSignedDocument!=null){
|
||||
applicationSignedDocument.setDocumentAttachmentId(parsedResponse.getDocumentAttachmentId());
|
||||
applicationSignedDocumentRepository.save(applicationSignedDocument);
|
||||
}
|
||||
|
||||
log.info("Document uploaded successfully to external system: {}", parsedResponse);
|
||||
} catch (FeignException.Forbidden forbiddenException) {
|
||||
log.error("403 Forbidden from external system during upload for documentId: {}. Retrying with new token...", documentId);
|
||||
regenerateTokenAndSave(hub, application);
|
||||
uploadDocumentToExternalSystemSync(documentId, docToExternalSystemRequest, application);
|
||||
uploadDocumentToExternalSystemSync(documentId, docToExternalSystemRequest, application,isSignedDocument);
|
||||
} catch (Exception e) {
|
||||
log.error("Exception during document upload: {}", e.getMessage(), e);
|
||||
throw new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale(GepafinConstant.EXTERNAL_DOCUMENT_UPLOAD_FAILURE_MSG));
|
||||
@@ -1282,13 +1339,21 @@ public class AppointmentDao {
|
||||
}
|
||||
|
||||
private File downloadFileFromS3(String fileUrl) throws Exception {
|
||||
|
||||
String key = amazonS3Service.extractS3KeyFromUrl(fileUrl);
|
||||
String fileName = extractFileName(key);
|
||||
String folderPath = key.substring(0, key.lastIndexOf("/"));
|
||||
File localFile = new File(GepafinConstant.TEMP_FILE_PATH + fileName);
|
||||
// Use OS-independent temp folder
|
||||
String tempFolder = System.getProperty("java.io.tmpdir");
|
||||
|
||||
try (InputStream s3Stream = amazonS3Service.getFile(folderPath, key); FileOutputStream outputStream = new FileOutputStream(localFile)) {
|
||||
File tempDir = new File(tempFolder);
|
||||
if (!tempDir.exists() && !tempDir.mkdirs()) {
|
||||
throw new IOException("Failed to create temp folder: " + tempDir.getAbsolutePath());
|
||||
}
|
||||
|
||||
File localFile = new File(tempDir, fileName);
|
||||
|
||||
try (InputStream s3Stream = amazonS3Service.getFile(folderPath, key);
|
||||
FileOutputStream outputStream = new FileOutputStream(localFile)) {
|
||||
s3Stream.transferTo(outputStream);
|
||||
}
|
||||
|
||||
@@ -1296,6 +1361,7 @@ public class AppointmentDao {
|
||||
return localFile;
|
||||
}
|
||||
|
||||
|
||||
private String extractFileName(String filePath) {
|
||||
|
||||
String[] parts = filePath.split("/");
|
||||
|
||||
Reference in New Issue
Block a user