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

@@ -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);
}
}
}