updated code
This commit is contained in:
@@ -1,17 +1,27 @@
|
||||
package net.gepafin.tendermanagement.dao;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import net.gepafin.tendermanagement.enums.DocumentSourceTypeEnum;
|
||||
import net.gepafin.tendermanagement.model.response.*;
|
||||
import net.gepafin.tendermanagement.service.*;
|
||||
import net.gepafin.tendermanagement.util.DateTimeUtil;
|
||||
import net.gepafin.tendermanagement.util.Utils;
|
||||
import org.h2.util.IOUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
@@ -73,20 +83,24 @@ public class CallDao {
|
||||
@Autowired
|
||||
private CallTargetAudienceChecklistRepository callTargetAudienceChecklistRepository;
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private FaqService faqService;
|
||||
|
||||
@Autowired
|
||||
private FlowDao flowDao;
|
||||
|
||||
@Autowired
|
||||
private FormDao formDao;
|
||||
|
||||
@Value("${aws.s3.url.folder}")
|
||||
private String s3Folder;
|
||||
|
||||
@Autowired
|
||||
private AmazonS3Service amazonS3Service;
|
||||
|
||||
public CallResponse createCallStep1(CreateCallRequestStep1 createCallRequest, Long userId) {
|
||||
UserEntity userEntity = userService.validateUser(userId);
|
||||
public CallResponse createCallStep1(CreateCallRequestStep1 createCallRequest, UserEntity userEntity) {
|
||||
createCallRequest.setRegionId(userEntity.getRoleEntity().getRegion().getId());
|
||||
CallEntity callEntity = convertToCallEntity(createCallRequest);
|
||||
CallEntity callEntity = convertToCallEntity(createCallRequest, userEntity);
|
||||
|
||||
updateFaq(createCallRequest.getFaq(), callEntity, userEntity,LookUpDataTypeEnum.FAQ);
|
||||
|
||||
@@ -98,8 +112,37 @@ public class CallDao {
|
||||
return createCallResponseBean;
|
||||
|
||||
}
|
||||
public byte[] downloadCallDocumentsAsZip(Long callId) {
|
||||
List<DocumentEntity> documents = documentRepository.findBySourceIdAndSourceAndTypeAndIsDeletedFalse(callId, DocumentSourceTypeEnum.CALL.getValue(),DocumentTypeEnum.DOCUMENT.getValue());
|
||||
if (documents.isEmpty()) {
|
||||
throw new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.DOCUMENT_NOT_FOUND));
|
||||
}
|
||||
|
||||
public CallEntity convertToCallEntity(CreateCallRequestStep1 createCallRequest) {
|
||||
try (ByteArrayOutputStream zipOutputStream = new ByteArrayOutputStream();
|
||||
ZipOutputStream zos = new ZipOutputStream(zipOutputStream)) {
|
||||
|
||||
for (DocumentEntity document : documents) {
|
||||
try (InputStream fileInputStream = amazonS3Service.getFile(s3Folder, document.getFileName())) {
|
||||
ZipEntry zipEntry = new ZipEntry(document.getFileName());
|
||||
zos.putNextEntry(zipEntry);
|
||||
IOUtils.copy(fileInputStream, zos);
|
||||
zos.closeEntry();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Error downloading or adding document to ZIP: " + document.getFileName(), e);
|
||||
}
|
||||
}
|
||||
|
||||
zos.finish();
|
||||
return zipOutputStream.toByteArray();
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Error while creating ZIP file", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public CallEntity convertToCallEntity(CreateCallRequestStep1 createCallRequest, UserEntity userEntity) {
|
||||
CallEntity callEntity = new CallEntity();
|
||||
// validateCallEntity(createCallRequest);
|
||||
RegionEntity region = regionRepository.findById(createCallRequest.getRegionId())
|
||||
@@ -124,6 +167,18 @@ public class CallDao {
|
||||
callEntity.setConfidi(createCallRequest.getConfidi());
|
||||
}
|
||||
callEntity.setDocumentationRequested(createCallRequest.getDocumentationRequested());
|
||||
if (createCallRequest.getAmountMin() != null && createCallRequest.getAmountMin().compareTo(BigDecimal.ZERO) < 0) {
|
||||
throw new CustomValidationException(Status.VALIDATION_ERROR,Translator.toLocale(GepafinConstant.AMOUNT_GREATER_THAN_ZERO_MSG));
|
||||
}
|
||||
callEntity.setAmountMin(createCallRequest.getAmountMin());
|
||||
if(createCallRequest.getEmail()!=null && Boolean.FALSE.equals(Utils.isValidEmail(createCallRequest.getEmail()))){
|
||||
throw new CustomValidationException(Status.VALIDATION_ERROR,Translator.toLocale(GepafinConstant.VALIDATION_EMAIL,createCallRequest.getEmail()));
|
||||
}
|
||||
callEntity.setEmail(createCallRequest.getEmail());
|
||||
callEntity.setPhoneNumber(createCallRequest.getPhoneNumber());
|
||||
callEntity.setStartTime(DateTimeUtil.parseTime(createCallRequest.getStartTime()));
|
||||
callEntity.setEndTime(DateTimeUtil.parseTime(createCallRequest.getEndTime()));
|
||||
callEntity.setHub(userEntity.getHub());
|
||||
callEntity = callRepository.save(callEntity);
|
||||
return callEntity;
|
||||
}
|
||||
@@ -259,6 +314,11 @@ public class CallDao {
|
||||
createCallResponseBean.setDocumentationRequested(callEntity.getDocumentationRequested());
|
||||
createCallResponseBean.setPriorityArea(callEntity.getPriorityArea());
|
||||
createCallResponseBean.setConfidi(callEntity.getConfidi());
|
||||
createCallResponseBean.setAmountMin(callEntity.getAmountMin());
|
||||
createCallResponseBean.setPhoneNumber(callEntity.getPhoneNumber());
|
||||
createCallResponseBean.setEndTime(callEntity.getEndTime());
|
||||
createCallResponseBean.setStartTime(callEntity.getStartTime());
|
||||
createCallResponseBean.setEmail(callEntity.getEmail());
|
||||
createCallResponseBean.setCreatedDate(callEntity.getCreatedDate());
|
||||
createCallResponseBean.setUpdatedDate(callEntity.getUpdatedDate());
|
||||
return createCallResponseBean;
|
||||
@@ -358,13 +418,11 @@ public class CallDao {
|
||||
Translator.toLocale(GepafinConstant.CALL_NOT_FOUND)));
|
||||
}
|
||||
|
||||
public CallResponse getCallById(Long callId) {
|
||||
CallEntity callEntity = validateCall(callId);
|
||||
public CallResponse getCallById(CallEntity callEntity) {
|
||||
return getCallResponseBean(callEntity);
|
||||
}
|
||||
|
||||
public CallResponse createCallStep2(Long callId, CreateCallRequestStep2 createCallRequest, Long userId) {
|
||||
CallEntity callEntity = validateCall(callId);
|
||||
public CallResponse createCallStep2(CallEntity callEntity, CreateCallRequestStep2 createCallRequest, UserEntity user) {
|
||||
validateUpdate(callEntity);
|
||||
setIfUpdated(callEntity::getThreshold, callEntity::setThreshold, createCallRequest.getThreshold());
|
||||
callRepository.save(callEntity);
|
||||
@@ -424,8 +482,7 @@ public class CallDao {
|
||||
}
|
||||
}
|
||||
|
||||
public CallResponse updateCallStep1(Long callId, UpdateCallRequestStep1 updateCallRequest, Long userId) {
|
||||
CallEntity callEntity = validateCall(callId);
|
||||
public CallResponse updateCallStep1(CallEntity callEntity, UpdateCallRequestStep1 updateCallRequest, UserEntity userEntity) {
|
||||
if(Boolean.TRUE.equals(callEntity.getStatus().equals(CallStatusEnum.PUBLISH.getValue()))) {
|
||||
try {
|
||||
Utils.retainOnlySpecificFields(updateCallRequest, Collections.singletonList("faq"));
|
||||
@@ -433,7 +490,6 @@ public class CallDao {
|
||||
throw new CustomValidationException(Status.BAD_REQUEST,Translator.toLocale(GepafinConstant.FAILED_RETAIN_FIELD));
|
||||
}
|
||||
}
|
||||
UserEntity userEntity = userService.validateUser(userId);
|
||||
isValidDateRange(updateCallRequest, callEntity);
|
||||
setIfUpdated(callEntity::getName, callEntity::setName, updateCallRequest.getName());
|
||||
setIfUpdated(callEntity::getDescriptionShort, callEntity::setDescriptionShort,
|
||||
@@ -456,6 +512,18 @@ public class CallDao {
|
||||
setIfUpdated(callEntity::getAmountMax, callEntity::setAmountMax, updateCallRequest.getAmountMax());
|
||||
setIfUpdated(callEntity::getDocumentationRequested, callEntity::setDocumentationRequested,
|
||||
updateCallRequest.getDocumentationRequested());
|
||||
|
||||
if (updateCallRequest.getAmountMin() != null && updateCallRequest.getAmountMin().compareTo(BigDecimal.ZERO) < 0) {
|
||||
throw new CustomValidationException(Status.VALIDATION_ERROR,Translator.toLocale(GepafinConstant.AMOUNT_GREATER_THAN_ZERO_MSG));
|
||||
}
|
||||
if(updateCallRequest.getEmail()!=null && Boolean.FALSE.equals(Utils.isValidEmail(updateCallRequest.getEmail()))){
|
||||
throw new CustomValidationException(Status.VALIDATION_ERROR,Translator.toLocale(GepafinConstant.VALIDATION_EMAIL,updateCallRequest.getEmail()));
|
||||
}
|
||||
setIfUpdated(callEntity::getAmountMin, callEntity::setAmountMin, updateCallRequest.getAmountMin());
|
||||
setIfUpdated(callEntity::getEmail, callEntity::setEmail, updateCallRequest.getEmail());
|
||||
setIfUpdated(callEntity::getPhoneNumber, callEntity::setPhoneNumber, updateCallRequest.getPhoneNumber());
|
||||
setIfUpdated(callEntity::getStartTime, callEntity::setStartTime, DateTimeUtil.parseTime(updateCallRequest.getStartTime()));
|
||||
setIfUpdated(callEntity::getEndTime, callEntity::setEndTime, DateTimeUtil.parseTime(updateCallRequest.getEndTime()));
|
||||
setIfUpdated(callEntity::getConfidi, callEntity::setConfidi, updateCallRequest.getConfidi());
|
||||
updateLookUpData(callEntity, updateCallRequest.getAimedTo(), LookUpDataTypeEnum.AIMED_TO);
|
||||
updateFaq(updateCallRequest.getFaq(), callEntity, userEntity, LookUpDataTypeEnum.FAQ);
|
||||
@@ -475,9 +543,9 @@ public class CallDao {
|
||||
}
|
||||
List<CallTargetAudienceChecklistEntity> existingChecklist = callTargetAudienceChecklistRepository
|
||||
.findByCallIdAndLookupDataTypeAndIsDeletedFalse(callEntity.getId(), type.getValue());
|
||||
List<Long> incomingIds = lookupDataReqList.stream().map(LookUpDataReq::getLookUpDataId)
|
||||
List<Long> incomingIds = lookupDataReqList.stream().map(LookUpDataReq::getId)
|
||||
.filter(id -> id != null && id > 0).collect(Collectors.toList());
|
||||
existingChecklist.stream().filter(checklist -> !incomingIds.contains(checklist.getLookupData().getId()))
|
||||
existingChecklist.stream().filter(checklist -> !incomingIds.contains(checklist.getId()))
|
||||
.forEach(this::softDeleteCallTargetAudienceChecklist);
|
||||
lookupDataReqList
|
||||
.forEach(lookUpDataReq -> createOrUpdateCallTargetAudienceChecklist(lookUpDataReq, callEntity, type));
|
||||
@@ -531,6 +599,11 @@ public class CallDao {
|
||||
callDetailsResponseBean.setThreshold(callEntity.getThreshold());
|
||||
callDetailsResponseBean.setDocumentationRequested(callEntity.getDocumentationRequested());
|
||||
callDetailsResponseBean.setPriorityArea(callEntity.getPriorityArea());
|
||||
callDetailsResponseBean.setAmountMin(callEntity.getAmountMin());
|
||||
callDetailsResponseBean.setEmail(callEntity.getEmail());
|
||||
callDetailsResponseBean.setEndTime(callEntity.getEndTime());
|
||||
callDetailsResponseBean.setStartTime(callEntity.getStartTime());
|
||||
callDetailsResponseBean.setPhoneNumber(callEntity.getPhoneNumber());
|
||||
callDetailsResponseBean.setCreatedDate(callEntity.getCreatedDate());
|
||||
callDetailsResponseBean.setUpdatedDate(callEntity.getUpdatedDate());
|
||||
return callDetailsResponseBean;
|
||||
@@ -575,7 +648,7 @@ public class CallDao {
|
||||
validateUpdate(callEntity);
|
||||
CallResponse callResponseBean = getCallResponseBean(callEntity);
|
||||
FlowResponseBean flowResponseBean = flowDao.getFlowByCallId(callEntity.getId());
|
||||
List<FormResponseBean> formResponseBean = formDao.getFormsByCallId(callEntity.getId());
|
||||
List<FormResponseBean> formResponseBean = formDao.getFormsByCallId(callEntity);
|
||||
CallValidatorServiceImpl.validateResponse(callResponseBean,flowResponseBean,formResponseBean);
|
||||
callEntity.setStatus(CallStatusEnum.READY_TO_PUBLISH.getValue());
|
||||
callRepository.save(callEntity);
|
||||
@@ -591,8 +664,7 @@ public class CallDao {
|
||||
return callEntity;
|
||||
}
|
||||
|
||||
public CallResponse updateCallStatus(Long callId, CallStatusEnum statusReq) {
|
||||
CallEntity callEntity = validateCall(callId);
|
||||
public CallResponse updateCallStatus(CallEntity callEntity, CallStatusEnum statusReq) {
|
||||
CallStatusEnum currentStatus = CallStatusEnum.valueOf(callEntity.getStatus());
|
||||
validateStatusChange(currentStatus, statusReq);
|
||||
callEntity.setStatus(statusReq.getValue());
|
||||
@@ -638,6 +710,25 @@ public class CallDao {
|
||||
Status.NOT_FOUND,
|
||||
Translator.toLocale(GepafinConstant.CALL_NOT_PUBLISHED));
|
||||
}
|
||||
LocalDate currentDate = LocalDate.now();
|
||||
LocalTime currentTime = LocalTime.now();
|
||||
|
||||
if (currentDate.isBefore(callEntity.getStartDate().toLocalDate()) ||
|
||||
(currentDate.isEqual(callEntity.getStartDate().toLocalDate()) && currentTime.isBefore(callEntity.getStartTime()))) {
|
||||
throw new CustomValidationException(
|
||||
Status.BAD_REQUEST,
|
||||
Translator.toLocale(GepafinConstant.CALL_NOT_STARTED_YET)
|
||||
);
|
||||
}
|
||||
|
||||
if (currentDate.isAfter(callEntity.getEndDate().toLocalDate()) ||
|
||||
(currentDate.isEqual(callEntity.getEndDate().toLocalDate()) && currentTime.isAfter(callEntity.getEndTime()))) {
|
||||
throw new CustomValidationException(
|
||||
Status.BAD_REQUEST,
|
||||
Translator.toLocale(GepafinConstant.CALL_ALREADY_ENDED)
|
||||
);
|
||||
}
|
||||
|
||||
return callEntity;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user