Merge pull request #288 from Kitzanos/feature/GEPAFINBE-213
GEPAFINBE-213 (Change amendment status to 'AWAITING' from 'EXPIRED when expiration date is updated)
This commit is contained in:
@@ -27,6 +27,7 @@ import net.gepafin.tendermanagement.web.rest.api.errors.ResourceNotFoundExceptio
|
|||||||
import net.gepafin.tendermanagement.web.rest.api.errors.Status;
|
import net.gepafin.tendermanagement.web.rest.api.errors.Status;
|
||||||
|
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.apache.commons.lang3.tuple.Pair;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
@@ -1049,7 +1050,7 @@ public class ApplicationAmendmentRequestDao {
|
|||||||
public ApplicationAmendmentRequestResponse closeAmendmentRequest(Long id, CloseAmendmentRequest closeAmendmentRequest) {
|
public ApplicationAmendmentRequestResponse closeAmendmentRequest(Long id, CloseAmendmentRequest closeAmendmentRequest) {
|
||||||
|
|
||||||
log.info("Closing application amendement with ID: {}", id);
|
log.info("Closing application amendement with ID: {}", id);
|
||||||
ApplicationAmendmentRequestEntity existingApplicationAmendment = validateApplicationAmendmentRequest(id);
|
ApplicationAmendmentRequestEntity existingApplicationAmendment = validatApplicationAmendmentRequestByStatus(id,ApplicationAmendmentRequestEnum.AWAITING.getValue());
|
||||||
//cloned entity for old data and versioning
|
//cloned entity for old data and versioning
|
||||||
ApplicationAmendmentRequestEntity oldApplicationAmendmentEntity = Utils.getClonedEntityForData(existingApplicationAmendment);
|
ApplicationAmendmentRequestEntity oldApplicationAmendmentEntity = Utils.getClonedEntityForData(existingApplicationAmendment);
|
||||||
List<ApplicationAmendmentRequestEntity> amendmentRequestList = applicationAmendmentRequestRepository.findAllByApplicationEvaluationIdAndIsDeletedFalse(
|
List<ApplicationAmendmentRequestEntity> amendmentRequestList = applicationAmendmentRequestRepository.findAllByApplicationEvaluationIdAndIsDeletedFalse(
|
||||||
@@ -1059,17 +1060,18 @@ public class ApplicationAmendmentRequestDao {
|
|||||||
// Check if this is the last amendment being closed
|
// Check if this is the last amendment being closed
|
||||||
boolean isLastRemaining = amendmentRequestList.stream()
|
boolean isLastRemaining = amendmentRequestList.stream()
|
||||||
.filter(amendment -> !amendment.getId().equals(id)) // Exclude the current amendment
|
.filter(amendment -> !amendment.getId().equals(id)) // Exclude the current amendment
|
||||||
.allMatch(amendment -> amendment.getStatus().equals(ApplicationAmendmentRequestEnum.CLOSE.getValue()));
|
.allMatch(amendment -> amendment.getStatus().equals(ApplicationAmendmentRequestEnum.CLOSE.getValue()) || amendment.getStatus().equals(ApplicationAmendmentRequestEnum.EXPIRED.getValue()));
|
||||||
|
|
||||||
|
|
||||||
if (isLastRemaining) {
|
|
||||||
log.info("The current amendment is the last remaining one to be closed.");
|
|
||||||
applicationAmendmentRequestDao.calculateEndDateAndSuspensionDays(existingApplicationAmendment.getApplicationEvaluationEntity());
|
|
||||||
}
|
|
||||||
setIfUpdated(existingApplicationAmendment::getInternalNote, existingApplicationAmendment::setInternalNote, closeAmendmentRequest.getInternalNote());
|
setIfUpdated(existingApplicationAmendment::getInternalNote, existingApplicationAmendment::setInternalNote, closeAmendmentRequest.getInternalNote());
|
||||||
setIfUpdated(existingApplicationAmendment::getStatus, existingApplicationAmendment::setStatus, ApplicationAmendmentRequestEnum.CLOSE.getValue());
|
setIfUpdated(existingApplicationAmendment::getStatus, existingApplicationAmendment::setStatus, ApplicationAmendmentRequestEnum.CLOSE.getValue());
|
||||||
existingApplicationAmendment.setClosingDate(LocalDateTime.now());
|
existingApplicationAmendment.setClosingDate(LocalDateTime.now());
|
||||||
ApplicationAmendmentRequestEntity updatedApplicationAmendment = saveApplicationAmendmentRequestEntity(existingApplicationAmendment, oldApplicationAmendmentEntity,
|
ApplicationAmendmentRequestEntity updatedApplicationAmendment = saveApplicationAmendmentRequestEntity(existingApplicationAmendment, oldApplicationAmendmentEntity,
|
||||||
VersionActionTypeEnum.UPDATE);
|
VersionActionTypeEnum.UPDATE);
|
||||||
|
if (isLastRemaining) {
|
||||||
|
log.info("The current amendment is the last remaining one to be closed.");
|
||||||
|
applicationAmendmentRequestDao.calculateEndDateAndSuspensionDaysOnExpirationOrClosing(existingApplicationAmendment.getApplicationEvaluationEntity());
|
||||||
|
}
|
||||||
ApplicationAmendmentRequestResponse response = convertEntityToResponse(updatedApplicationAmendment,false);
|
ApplicationAmendmentRequestResponse response = convertEntityToResponse(updatedApplicationAmendment,false);
|
||||||
|
|
||||||
List<ApplicationAmendmentRequestEntity> amendmentRequests = applicationAmendmentRequestRepository.findAllByApplicationEvaluationIdAndIsDeletedFalse(
|
List<ApplicationAmendmentRequestEntity> amendmentRequests = applicationAmendmentRequestRepository.findAllByApplicationEvaluationIdAndIsDeletedFalse(
|
||||||
@@ -1121,15 +1123,17 @@ public class ApplicationAmendmentRequestDao {
|
|||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public ApplicationAmendmentRequestResponse extendResponseDays(Long id, Long newResponseDays) {
|
public ApplicationAmendmentRequestResponse extendResponseDays(Long id, Long newResponseDays) {
|
||||||
ApplicationAmendmentRequestEntity applicationAmendmentRequestEntity = validateApplicationAmendmentRequest(id);
|
ApplicationAmendmentRequestEntity applicationAmendmentRequestEntity = validatApplicationAmendmentRequestByStatus(id,ApplicationAmendmentRequestEnum.EXPIRED.getValue());
|
||||||
|
|
||||||
if (newResponseDays != null && newResponseDays > 0) {
|
if (newResponseDays != null && newResponseDays > 0) {
|
||||||
ApplicationAmendmentRequestEntity oldApplicationAmendmentEntity = Utils.getClonedEntityForData(applicationAmendmentRequestEntity);
|
ApplicationAmendmentRequestEntity oldApplicationAmendmentEntity = Utils.getClonedEntityForData(applicationAmendmentRequestEntity);
|
||||||
Long currentResponseDays = applicationAmendmentRequestEntity.getResponseDays() != null ? applicationAmendmentRequestEntity.getResponseDays() : 0L;
|
Long currentResponseDays = applicationAmendmentRequestEntity.getResponseDays() != null ? applicationAmendmentRequestEntity.getResponseDays() : 0L;
|
||||||
applicationAmendmentRequestEntity.setResponseDays(currentResponseDays + newResponseDays);
|
applicationAmendmentRequestEntity.setResponseDays(currentResponseDays + newResponseDays);
|
||||||
applicationAmendmentRequestEntity.setEndDate(DateTimeUtil.DateServerToUTC(LocalDateTime.now().plusDays(applicationAmendmentRequestEntity.getResponseDays())));
|
applicationAmendmentRequestEntity.setEndDate(DateTimeUtil.DateServerToUTC(LocalDateTime.now().plusDays(newResponseDays)));
|
||||||
|
applicationAmendmentRequestEntity.setStatus(ApplicationAmendmentRequestEnum.AWAITING.getValue());
|
||||||
|
applicationAmendmentRequestEntity.getApplicationEvaluationEntity().setStatus(ApplicationEvaluationStatusTypeEnum.SOCCORSO.getValue());
|
||||||
|
applicationAmendmentRequestEntity.getApplicationEvaluationEntity().getAssignedApplicationsEntity().getApplication().setStatus(ApplicationStatusTypeEnum.SOCCORSO.getValue());
|
||||||
applicationAmendmentRequestRepository.save(applicationAmendmentRequestEntity);
|
applicationAmendmentRequestRepository.save(applicationAmendmentRequestEntity);
|
||||||
|
|
||||||
/** This code is responsible for adding a version history log for the "Update Application Amendment" operation. **/
|
/** This code is responsible for adding a version history log for the "Update Application Amendment" operation. **/
|
||||||
@@ -1241,26 +1245,26 @@ public class ApplicationAmendmentRequestDao {
|
|||||||
return Utils.replacePlaceholders(template.getHtmlContent(), bodyPlaceholders);
|
return Utils.replacePlaceholders(template.getHtmlContent(), bodyPlaceholders);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ApplicationEvaluationEntity calculateEndDateAndSuspensionDays(ApplicationEvaluationEntity applicationEvaluationEntity){
|
// public ApplicationEvaluationEntity calculateEndDateAndSuspensionDays(ApplicationEvaluationEntity applicationEvaluationEntity){
|
||||||
LocalDateTime currentDate=DateTimeUtil.DateServerToUTC(LocalDateTime.now());
|
// LocalDateTime currentDate=DateTimeUtil.DateServerToUTC(LocalDateTime.now());
|
||||||
LocalDateTime endDate=currentDate.plusDays(applicationEvaluationEntity.getRemainingDays());
|
// LocalDateTime endDate=currentDate.plusDays(applicationEvaluationEntity.getRemainingDays());
|
||||||
Long suspendedDays = ChronoUnit.DAYS.between(applicationEvaluationEntity.getStopDateTime(), currentDate);
|
// Long suspendedDays = ChronoUnit.DAYS.between(applicationEvaluationEntity.getStopDateTime(), currentDate);
|
||||||
|
//
|
||||||
ApplicationEvaluationEntity oldApplicationEvaluationEntity = Utils.getClonedEntityForData(applicationEvaluationEntity);
|
// ApplicationEvaluationEntity oldApplicationEvaluationEntity = Utils.getClonedEntityForData(applicationEvaluationEntity);
|
||||||
applicationEvaluationEntity.setEndDate(endDate);
|
// applicationEvaluationEntity.setEndDate(endDate);
|
||||||
if(applicationEvaluationEntity.getSuspendedDays() == null) {
|
// if(applicationEvaluationEntity.getSuspendedDays() == null) {
|
||||||
applicationEvaluationEntity.setSuspendedDays(0L);
|
// applicationEvaluationEntity.setSuspendedDays(0L);
|
||||||
}
|
// }
|
||||||
applicationEvaluationEntity.setSuspendedDays(applicationEvaluationEntity.getSuspendedDays()+suspendedDays);
|
// applicationEvaluationEntity.setSuspendedDays(applicationEvaluationEntity.getSuspendedDays()+suspendedDays);
|
||||||
ApplicationEvaluationEntity applicationEvaluation = applicationEvaluationRepository.save(applicationEvaluationEntity);
|
// ApplicationEvaluationEntity applicationEvaluation = applicationEvaluationRepository.save(applicationEvaluationEntity);
|
||||||
|
//
|
||||||
/** This code is responsible for adding a version history log for the "Update Application Amendment" operation. **/
|
// /** This code is responsible for adding a version history log for the "Update Application Amendment" operation. **/
|
||||||
loggingUtil.addVersionHistory(VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.UPDATE).oldData(oldApplicationEvaluationEntity).newData(applicationEvaluation).build());
|
// loggingUtil.addVersionHistory(VersionHistoryRequest.builder().request(request).actionType(VersionActionTypeEnum.UPDATE).oldData(oldApplicationEvaluationEntity).newData(applicationEvaluation).build());
|
||||||
|
//
|
||||||
return applicationEvaluation;
|
// return applicationEvaluation;
|
||||||
|
//
|
||||||
|
//
|
||||||
}
|
// }
|
||||||
|
|
||||||
private GetAllAmendmentResponseBean initializeGetAllBasicResponse(ApplicationAmendmentRequestEntity entity) {
|
private GetAllAmendmentResponseBean initializeGetAllBasicResponse(ApplicationAmendmentRequestEntity entity) {
|
||||||
GetAllAmendmentResponseBean response = new GetAllAmendmentResponseBean();
|
GetAllAmendmentResponseBean response = new GetAllAmendmentResponseBean();
|
||||||
@@ -1565,6 +1569,95 @@ public class ApplicationAmendmentRequestDao {
|
|||||||
return applicationAmendmentRequestViewResponse;
|
return applicationAmendmentRequestViewResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ApplicationEvaluationEntity calculateEndDateAndSuspensionDaysOnExpirationOrClosing
|
||||||
|
(ApplicationEvaluationEntity applicationEvaluationEntity) {
|
||||||
|
|
||||||
|
|
||||||
|
// Fetch all linked amendments
|
||||||
|
List<ApplicationAmendmentRequestEntity> amendments =
|
||||||
|
applicationAmendmentRequestRepository.findAllByApplicationEvaluationIdAndIsDeletedFalse(applicationEvaluationEntity.getId());
|
||||||
|
|
||||||
|
// Recalculate suspension
|
||||||
|
long suspendedDays = calculateSuspendedDays(amendments);
|
||||||
|
|
||||||
|
// Update end date and save
|
||||||
|
|
||||||
|
ApplicationEvaluationEntity oldEntity = Utils.getClonedEntityForData(applicationEvaluationEntity);
|
||||||
|
HubEntity hub = hubService.valdateHub(applicationEvaluationEntity.getAssignedApplicationsEntity().getApplication().getHubId());
|
||||||
|
Long initialDays = (hub != null) ? hub.getEvaluationExpirationDays() : 30L;
|
||||||
|
LocalDateTime endDate = applicationEvaluationEntity.getStartDate().plusDays(suspendedDays+initialDays);
|
||||||
|
applicationEvaluationEntity.setEndDate(endDate);
|
||||||
|
applicationEvaluationEntity.setSuspendedDays(suspendedDays);
|
||||||
|
ApplicationEvaluationEntity updated = applicationEvaluationRepository.save(applicationEvaluationEntity);
|
||||||
|
|
||||||
|
loggingUtil.addVersionHistory(VersionHistoryRequest.builder()
|
||||||
|
.request(request)
|
||||||
|
.actionType(VersionActionTypeEnum.UPDATE)
|
||||||
|
.oldData(oldEntity)
|
||||||
|
.newData(updated)
|
||||||
|
.build());
|
||||||
|
|
||||||
|
return updated;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long calculateSuspendedDays(List<ApplicationAmendmentRequestEntity> amendments) {
|
||||||
|
List<Pair<LocalDateTime, LocalDateTime>> periods = amendments.stream()
|
||||||
|
.filter(amendmentRequest -> amendmentRequest.getStartDate() != null)
|
||||||
|
.map(amendmentRequest -> {
|
||||||
|
LocalDateTime start = amendmentRequest.getStartDate();
|
||||||
|
LocalDateTime end;
|
||||||
|
|
||||||
|
String status = amendmentRequest.getStatus();
|
||||||
|
if (Boolean.TRUE.equals(ApplicationAmendmentRequestEnum.CLOSE.getValue().equals(status)) && amendmentRequest.getClosingDate() != null) {
|
||||||
|
end = amendmentRequest.getClosingDate();
|
||||||
|
} else if (Boolean.TRUE.equals(ApplicationAmendmentRequestEnum.EXPIRED.getValue().equals(status)) && amendmentRequest.getEndDate() != null) {
|
||||||
|
end = amendmentRequest.getStartDate().plusDays(amendmentRequest.getResponseDays());
|
||||||
|
}else {
|
||||||
|
end= amendmentRequest.getEndDate();
|
||||||
|
}
|
||||||
|
|
||||||
|
return Pair.of(start, end);
|
||||||
|
})
|
||||||
|
.filter(Objects::nonNull)
|
||||||
|
.sorted(Comparator.comparing(Pair::getLeft))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
long totalDays = 0;
|
||||||
|
LocalDateTime currentStart = null;
|
||||||
|
LocalDateTime currentEnd = null;
|
||||||
|
|
||||||
|
for (Pair<LocalDateTime, LocalDateTime> period : periods) {
|
||||||
|
if (currentStart == null) {
|
||||||
|
currentStart = period.getLeft();
|
||||||
|
currentEnd = period.getRight();
|
||||||
|
} else if (!period.getLeft().isAfter(currentEnd)) {
|
||||||
|
// Merge overlapping/touching periods
|
||||||
|
currentEnd = currentEnd.isAfter(period.getRight()) ? currentEnd : period.getRight();
|
||||||
|
} else {
|
||||||
|
// Non-overlapping: count previous period
|
||||||
|
totalDays += ChronoUnit.DAYS.between(currentStart.toLocalDate(), currentEnd.toLocalDate());
|
||||||
|
currentStart = period.getLeft();
|
||||||
|
currentEnd = period.getRight();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currentStart != null && currentEnd != null) {
|
||||||
|
totalDays += ChronoUnit.DAYS.between(currentStart.toLocalDate(), currentEnd.toLocalDate());
|
||||||
|
}
|
||||||
|
|
||||||
|
return totalDays;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public ApplicationAmendmentRequestEntity validatApplicationAmendmentRequestByStatus(Long id,String status) {
|
||||||
|
ApplicationAmendmentRequestEntity applicationAmendmentRequestEntity = applicationAmendmentRequestRepository.findByIdAndIsDeletedFalseAndStatus(id, status);
|
||||||
|
if (applicationAmendmentRequestEntity == null) {
|
||||||
|
throw new ResourceNotFoundException(Status.NOT_FOUND,
|
||||||
|
Translator.toLocale(GepafinConstant.APPLICATION_AMENDMENT_NOT_FOUND_MSG));
|
||||||
|
}
|
||||||
|
return applicationAmendmentRequestEntity;
|
||||||
|
}
|
||||||
private void saveEmailSendResponse(EmailSendResponse newResponses, ApplicationAmendmentRequestEntity amendment) {
|
private void saveEmailSendResponse(EmailSendResponse newResponses, ApplicationAmendmentRequestEntity amendment) {
|
||||||
List<EmailSendResponse> mergedResponses = Utils.mergeEmailSendResponses(amendment.getEmailSendResponse(), newResponses);
|
List<EmailSendResponse> mergedResponses = Utils.mergeEmailSendResponses(amendment.getEmailSendResponse(), newResponses);
|
||||||
|
|
||||||
|
|||||||
@@ -150,4 +150,6 @@ public interface ApplicationAmendmentRequestRepository extends JpaRepository<App
|
|||||||
|
|
||||||
@Query("SELECT COUNT(a) FROM ApplicationAmendmentRequestEntity a WHERE a.applicationId IN :applicationIds AND a.status IN :statuses AND a.isDeleted = false")
|
@Query("SELECT COUNT(a) FROM ApplicationAmendmentRequestEntity a WHERE a.applicationId IN :applicationIds AND a.status IN :statuses AND a.isDeleted = false")
|
||||||
Long countAmendmentsByApplicationIds(@Param("applicationIds") List<Long> applicationIds, @Param("statuses") List<String> statuses);
|
Long countAmendmentsByApplicationIds(@Param("applicationIds") List<Long> applicationIds, @Param("statuses") List<String> statuses);
|
||||||
|
|
||||||
|
ApplicationAmendmentRequestEntity findByIdAndIsDeletedFalseAndStatus(Long id,String status);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -122,7 +122,7 @@ public class ApplicationAmendmentScheduler {
|
|||||||
.findEvaluationsWithoutActiveAmendmentsByIds(applicationEvaluationIds);
|
.findEvaluationsWithoutActiveAmendmentsByIds(applicationEvaluationIds);
|
||||||
evaluationsWithoutActiveAmendmentList.forEach(evaluation -> {
|
evaluationsWithoutActiveAmendmentList.forEach(evaluation -> {
|
||||||
try {
|
try {
|
||||||
applicationAmendmentRequestDao.calculateEndDateAndSuspensionDays(evaluation);
|
applicationAmendmentRequestDao.calculateEndDateAndSuspensionDaysOnExpirationOrClosing(evaluation);
|
||||||
|
|
||||||
updateEvaluationStatus(evaluation);
|
updateEvaluationStatus(evaluation);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user