Amendment document changes

This commit is contained in:
rajesh
2024-12-18 15:58:17 +05:30
parent 94086428e8
commit 47f67975c1
8 changed files with 167 additions and 80 deletions

View File

@@ -108,6 +108,9 @@ public class ApplicationAmendmentRequestDao {
@Autowired
private ApplicationEvaluationDao applicationEvaluationDao;
@Autowired
private DocumentRepository documentRepository;
public ApplicationAmendmentRequestResponse getApplicationDataForAmendment(Long applicationEvaluationId) {
log.info("Fetching the application data for the Amendment process {}", applicationEvaluationId);
ApplicationEvaluationEntity applicationEvaluationEntity = applicationEvaluationService.validateApplicationEvaluation(applicationEvaluationId);
@@ -330,15 +333,24 @@ public class ApplicationAmendmentRequestDao {
return applicationAmendment;
}
private void setAmendmentDocuments(String amendmentNotes, String amendmentFieldRequest,
ApplicationAmendmentRequestEntity applicationAmendmentRequestEntity) {
AmendmentDetailsResponseBean amendmentDetails = new AmendmentDetailsResponseBean();
if (amendmentFieldRequest != null && !amendmentFieldRequest.trim().isEmpty()) {
String[] documentIds = amendmentFieldRequest.split(",");
String validDocumentIds = Arrays.stream(documentIds)
.map(String::trim)
.filter(id -> !id.isEmpty())
.collect(Collectors.joining(","));
private void setAmendmentDocuments(List<AmendmentFieldRequest> amendmentFieldRequest, ApplicationAmendmentRequestEntity applicationAmendmentRequestEntity) {
amendmentFieldRequest.stream().forEach(amendmentData->{
String fieldValue=amendmentData.getFileValue();
if(fieldValue!=null){
documentService.validateDocument(Long.valueOf(fieldValue));
}
});
applicationAmendmentRequestEntity.setAmendmentDocument(Utils.convertListToJsonString(amendmentFieldRequest));
amendmentDetails.setAmendmentDocuments(validDocumentIds);
}
if (amendmentNotes != null && !amendmentNotes.trim().isEmpty()) {
amendmentDetails.setAmendmentNotes(amendmentNotes.trim());
}
amendmentDetails.setValid(null);
String amendmentDetailsJson = Utils.convertListToJsonString(Collections.singletonList(amendmentDetails));
applicationAmendmentRequestEntity.setAmendmentDocument(amendmentDetailsJson);
}
public ApplicationAmendmentRequestEntity saveApplicationAmendmentRequestEntity(ApplicationAmendmentRequestEntity applicationAmendmentRequestEntity,ApplicationAmendmentRequestEntity oldApplicationAmendmentEntity,VersionActionTypeEnum actionTypeEnum) {
@@ -355,39 +367,70 @@ public class ApplicationAmendmentRequestDao {
List<ApplicationFormEntity> forms = applicationFormRepository.findByApplicationId(applicationAmendmentRequestEntity.getApplicationId());
Map<String, String> fieldIdToLabelMap = extractFieldIdToLabelMap(forms);
List<AmendmentFieldRequest> amendmentFieldRequests= new ArrayList<>();
// List<AmendmentFieldRequest> amendmentFieldRequests= new ArrayList<>();
List<AmendmentFormField> amendmentFormFields = Utils.convertJsonStringToList(
applicationAmendmentRequestEntity.getFormFields(), AmendmentFormField.class);
Map<String, ApplicationFormFieldEntity> formFieldEntityMap = getApplicationFormFieldEntityMap(applicationAmendmentRequestEntity, amendmentFormFields);
if(applicationAmendmentRequestEntity.getAmendmentDocument() !=null ){
amendmentFieldRequests = Utils.convertJsonStringToList(applicationAmendmentRequestEntity.getAmendmentDocument(),AmendmentFieldRequest.class);
}
if (amendmentFieldRequests != null) {
List<AmendmentDocumentResponse> amendmentDocumentResponses = amendmentFieldRequests.stream()
.map(this::createAmendmentDocumentResponse)
.toList();
response.setAmendmentDocuments(amendmentDocumentResponses);
if (applicationAmendmentRequestEntity.getAmendmentDocument() != null) {
List<AmendmentDetailsResponseBean> amendmentDetailsList =
Utils.convertJsonStringToList(applicationAmendmentRequestEntity.getAmendmentDocument(),
AmendmentDetailsResponseBean.class);
List<DocumentResponseBean> documentResponseBeans = new ArrayList<>();
for (AmendmentDetailsResponseBean amendmentDetails:amendmentDetailsList) {
if (amendmentDetails.getAmendmentDocuments()!=null) {
// Extract the comma-separated document IDs as a string
String documentIdsString = amendmentDetails.getAmendmentDocuments();
if (documentIdsString != null && !documentIdsString.trim().isEmpty()) {
// Split the comma-separated values and process them
List<String> documentIds = Arrays.stream(documentIdsString.split(","))
.map(String::trim)
.filter(id -> !id.isEmpty())
.collect(Collectors.toList());
documentResponseBeans.addAll(
documentIds.stream()
.map(id -> {
try {
return createDocumentResponseBean(id); // Convert to Long
} catch (NumberFormatException e) {
// Handle invalid document IDs gracefully
return null;
}
})
.filter(Objects::nonNull) // Skip null responses
.collect(Collectors.toList())
);
response.setAmendmentNotes(amendmentDetails.getAmendmentNotes());
response.setValid(amendmentDetails.getValid()!=null?amendmentDetails.getValid():null);
}
}
}
response.setAmendmentDocuments(documentResponseBeans);
}
processFormFields(amendmentFormFields, fieldIdToLabelMap, formFieldEntityMap, response);
return response;
}
public AmendmentDocumentResponse createAmendmentDocumentResponse(AmendmentFieldRequest amendmentFieldRequest) {
AmendmentDocumentResponse amendmentDocumentResponse = new AmendmentDocumentResponse();
amendmentDocumentResponse.setFieldId(amendmentFieldRequest.getFieldId());
amendmentDocumentResponse.setNameValue(amendmentFieldRequest.getNameValue());
amendmentDocumentResponse.setValid(amendmentFieldRequest.getValid());
amendmentDocumentResponse.setFileValue(null);
if(amendmentFieldRequest.getFileValue()!=null) {
DocumentEntity documentEntity = documentService.validateDocument(Long.valueOf(amendmentFieldRequest.getFileValue()));
DocumentResponseBean responseBean = applicationEvaluationDao.createDocumentResponseBean(documentEntity);
amendmentDocumentResponse.setFileValue(List.of(responseBean));
}
public DocumentResponseBean createDocumentResponseBean(String documentId) {
return amendmentDocumentResponse;
if (!StringUtils.isEmpty(documentId)) {
Optional<DocumentEntity> documentEntity = documentRepository.findByIdAndNotDeleted(Long.valueOf(documentId));
if(documentEntity.isPresent()){
return applicationEvaluationDao.createDocumentResponseBean(documentEntity.get());
}}
return null;
}
private ApplicationAmendmentRequestResponse initializeBasicResponse(ApplicationAmendmentRequestEntity entity) {
ApplicationAmendmentRequestResponse response = new ApplicationAmendmentRequestResponse();
response.setId(entity.getId());
@@ -581,7 +624,7 @@ public class ApplicationAmendmentRequestDao {
}
existingApplicationAmendment.setUpdatedDate(DateTimeUtil.DateServerToUTC(LocalDateTime.now()));
if(updateRequest.getAmendmentDocuments()!=null && Boolean.FALSE.equals(updateRequest.getAmendmentDocuments().isEmpty())) {
setAmendmentDocuments(updateRequest.getAmendmentDocuments(), existingApplicationAmendment);
setAmendmentDocuments(updateRequest.getAmendmentNotes(),updateRequest.getAmendmentDocuments(), existingApplicationAmendment);
}
ApplicationAmendmentRequestEntity updatedApplicationAmendment = saveApplicationAmendmentRequestEntity(existingApplicationAmendment,oldApplicationAmendmentEntity,VersionActionTypeEnum.UPDATE);
ApplicationAmendmentRequestResponse response = convertEntityToResponse(updatedApplicationAmendment);

View File

@@ -157,12 +157,21 @@ public class ApplicationEvaluationDao {
amendmentDocumentResponseBean.setAmendmentId(applicationAmendmentRequestEntity.getId());
String amendmentDocument=applicationAmendmentRequestEntity.getAmendmentDocument();
String formField=applicationAmendmentRequestEntity.getFormFields();
if (Boolean.FALSE.equals(StringUtils.isEmpty(amendmentDocument))) {
List<AmendmentFieldRequest> amendmentFieldRequests=Utils.convertJsonStringToList(amendmentDocument,AmendmentFieldRequest.class);
List<AmendmentDocumentResponse> amendmentDocumentResponses = amendmentFieldRequests.stream()
.map(applicationAmendmentRequestDao::createAmendmentDocumentResponse)
.toList();
amendmentDocumentResponseBean.setAmendmentDocuments(amendmentDocumentResponses);
List<AmendmentDetailsResponseBean> amendmentDetailsList = Utils.convertJsonStringToList(amendmentDocument, AmendmentDetailsResponseBean.class);
if (amendmentDetailsList != null && !amendmentDetailsList.isEmpty()) {
AmendmentDetailsResponseBean amendmentDetails = amendmentDetailsList.get(0);
if (amendmentDetails.getAmendmentDocuments() != null) {
List<DocumentResponseBean> documentResponseBeans = Arrays.stream(amendmentDetails.getAmendmentDocuments().split(","))
.map(String::trim)
.filter(id -> !id.isEmpty())
.map(documentId -> applicationAmendmentRequestDao.createDocumentResponseBean(documentId))
.filter(Objects::nonNull)
.collect(Collectors.toList());
amendmentDocumentResponseBean.setAmendmentDocuments(documentResponseBeans);
}
amendmentDocumentResponseBean.setAmendmentNotes(amendmentDetails.getAmendmentNotes());
amendmentDocumentResponseBean.setValid(amendmentDetails.getValid());
}
List<AmendmentFormField> amendmentFormFields=Utils.convertJsonStringToList(formField,AmendmentFormField.class);
amendmentDocumentResponseBean.setFormFieldDocuments(setFormFieldDocuments(amendmentFormFields));
@@ -657,39 +666,69 @@ public class ApplicationEvaluationDao {
// Process amendment documents if present
if (applicationAmendmentRequestEntity.getAmendmentDocument() != null) {
// Parse existing amendment fields from JSON
List<AmendmentFieldRequest> existingAmendmentFields =
Utils.convertJsonStringToList(applicationAmendmentRequestEntity.getAmendmentDocument(), AmendmentFieldRequest.class);
String existingDocumentIds = applicationAmendmentRequestEntity.getAmendmentDocument();
// Prepare a new list to hold updated amendment fields
List<AmendmentFieldRequest> updatedAmendmentFields = new ArrayList<>();
// Split comma-separated document IDs into a list
List<String> existingDocumentIdList = Arrays.stream(existingDocumentIds.split(","))
.map(String::trim)
.filter(id -> !id.isEmpty())
.collect(Collectors.toList());
// Map amendment details for quick lookup by amendment ID
Map<Long, List<AmendmentFieldRequest>> amendmentDetailsMap = amendmentFormFields.stream()
List<String> updatedDocumentIdList = new ArrayList<>();
Map<Long, String> amendmentDetailsMap = amendmentFormFields.stream()
.filter(details -> applicationAmendmentRequestEntity.getId().equals(details.getAmendmentId()))
.collect(Collectors.toMap(AmendmentDetailsRequest::getAmendmentId, AmendmentDetailsRequest::getAmendmentDocuments));
.collect(Collectors.toMap(
AmendmentDetailsRequest::getAmendmentId,
AmendmentDetailsRequest::getAmendmentDocuments
));
// Get corresponding amendment documents for the current entity
List<AmendmentFieldRequest> amendmentDocuments = amendmentDetailsMap.get(applicationAmendmentRequestEntity.getId());
if (amendmentDocuments != null) {
// Update existing amendment fields with new values
for (AmendmentFieldRequest existingField : existingAmendmentFields) {
for (AmendmentFieldRequest newField : amendmentDocuments) {
if (existingField.getFieldId().equals(newField.getFieldId())) {
// Update fields if there are changes
Utils.setIfUpdated(existingField::getValid, existingField::setValid, newField.getValid());
Utils.setIfUpdated(existingField::getFileValue, existingField::setFileValue, newField.getFileValue());
Utils.setIfUpdated(existingField::getNameValue, existingField::setNameValue, newField.getNameValue());
String amendmentDocumentIds = amendmentDetailsMap.get(applicationAmendmentRequestEntity.getId());
if (amendmentDocumentIds != null) {
// Split and validate new document IDs
List<String> newDocumentIdList = Arrays.stream(amendmentDocumentIds.split(","))
.map(String::trim)
.filter(id -> !id.isEmpty())
.collect(Collectors.toList());
updatedAmendmentFields.add(existingField);
break; // Move to the next existing field
for (String existingId : existingDocumentIdList) {
for (String newId : newDocumentIdList) {
if (existingId.equals(newId)) {
Optional<DocumentEntity> documentEntity = documentRepository.findByIdAndNotDeleted(Long.valueOf(newId));
if(documentEntity.isPresent()) {
updatedDocumentIdList.add(newId);
break;
}
}
}
}
// Convert updated fields back to JSON and save to the database
applicationAmendmentRequestEntity.setAmendmentDocument(Utils.convertListToJsonString(updatedAmendmentFields));
applicationAmendmentRequestRepository.save(applicationAmendmentRequestEntity);
// Add any new IDs not in the existing list
for (String newId : newDocumentIdList) {
if (!existingDocumentIdList.contains(newId)) {
Optional<DocumentEntity> documentEntity = documentRepository.findByIdAndNotDeleted(Long.valueOf(newId));
if(documentEntity.isPresent()) {
updatedDocumentIdList.add(newId);
}
}
}
String updatedDocumentIds = String.join(",", updatedDocumentIdList);
// Create the AmendmentDetailsResponseBean for structured data
AmendmentDetailsResponseBean amendmentDetails = new AmendmentDetailsResponseBean();
amendmentDetails.setAmendmentDocuments(updatedDocumentIds);
AmendmentDetailsRequest amendmentDetailsRequest = amendmentFormFields.stream()
.filter(details -> applicationAmendmentRequestEntity.getId().equals(details.getAmendmentId()))
.findFirst()
.orElse(null);
if (amendmentDetailsRequest != null) {
amendmentDetails.setValid(amendmentDetailsRequest.getValid());
} else {
amendmentDetails.setValid(false);
}
String amendmentDetailsJson = Utils.convertListToJsonString(Collections.singletonList(amendmentDetails));
applicationAmendmentRequestEntity.setAmendmentDocument(amendmentDetailsJson);
applicationAmendmentRequestRepository.save(applicationAmendmentRequestEntity);
}
}
}