Done ticket GEPFINBE-94
This commit is contained in:
@@ -975,14 +975,32 @@ public class ApplicationDao {
|
||||
applicationEntity = saveApplicationEntity(applicationEntity);
|
||||
return getApplicationResponse(applicationEntity);
|
||||
}
|
||||
public byte[] downloadApplicationDocumentsAsZip(HttpServletRequest request,Long applicationId) {
|
||||
public byte[] downloadApplicationDocumentsAsZip(HttpServletRequest request, Long applicationId) {
|
||||
ApplicationEntity applicationEntity = validateApplication(applicationId);
|
||||
AssignedApplicationsEntity assignedApplications = assignedApplicationsRepository.findByApplicationIdAndIsDeletedFalse(applicationId).orElse(null);
|
||||
validateAssignedUser(request, applicationId);
|
||||
|
||||
List<ApplicationFormEntity> applicationForms = applicationFormRepository.findByApplicationId(applicationId);
|
||||
if(assignedApplications!=null){
|
||||
validator.validatePreInstructor(request,assignedApplications.getUserId());}
|
||||
Set<Long> documentIds = extractDocumentIdsFromApplicationForms(applicationId);
|
||||
List<DocumentEntity> documents = documentRepository.findAllByIdInAndIsDeletedFalse(documentIds);
|
||||
|
||||
ApplicationSignedDocumentEntity signedDocument = applicationSignedDocumentRepository
|
||||
.findByApplicationIdAndStatus(applicationId, ApplicationSignedDocumentStatusEnum.ACTIVE.getValue());
|
||||
if (documents.isEmpty() && signedDocument == null) {
|
||||
throw new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.DOCUMENT_NOT_FOUND));
|
||||
}
|
||||
return createZipWithDocuments(applicationEntity, documents, signedDocument, applicationId);
|
||||
}
|
||||
private void validateAssignedUser(HttpServletRequest request, Long applicationId) {
|
||||
AssignedApplicationsEntity assignedApplications = assignedApplicationsRepository
|
||||
.findByApplicationIdAndIsDeletedFalse(applicationId).orElse(null);
|
||||
if (assignedApplications != null) {
|
||||
validator.validatePreInstructor(request, assignedApplications.getUserId());
|
||||
}
|
||||
}
|
||||
|
||||
private Set<Long> extractDocumentIdsFromApplicationForms(Long applicationId) {
|
||||
Set<Long> documentIds = new HashSet<>();
|
||||
List<ApplicationFormEntity> applicationForms = applicationFormRepository.findByApplicationId(applicationId);
|
||||
|
||||
applicationForms.forEach(applicationForm -> {
|
||||
FormEntity formEntity = applicationForm.getForm();
|
||||
if (formEntity != null) {
|
||||
@@ -1004,36 +1022,35 @@ public class ApplicationDao {
|
||||
});
|
||||
}
|
||||
});
|
||||
List<DocumentEntity> documents = documentRepository.findAllById(documentIds);
|
||||
ApplicationSignedDocumentEntity signedDocument = applicationSignedDocumentRepository
|
||||
.findByApplicationIdAndStatus(applicationId, ApplicationSignedDocumentStatusEnum.ACTIVE.getValue());
|
||||
if (documents.isEmpty() && signedDocument == null) {
|
||||
throw new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.DOCUMENT_NOT_FOUND));
|
||||
return documentIds;
|
||||
}
|
||||
|
||||
private void addDocumentToZip(ZipOutputStream zos, String s3Folder, String filePath, String fileName) {
|
||||
try (InputStream fileInputStream = amazonS3Service.getFile(s3Folder, filePath)) {
|
||||
zos.putNextEntry(new ZipEntry(fileName));
|
||||
IOUtils.copy(fileInputStream, zos);
|
||||
zos.closeEntry();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Error downloading or adding document to ZIP: " + fileName, e);
|
||||
}
|
||||
}
|
||||
|
||||
private byte[] createZipWithDocuments(ApplicationEntity applicationEntity, List<DocumentEntity> documents,
|
||||
ApplicationSignedDocumentEntity signedDocument, Long applicationId) {
|
||||
try (ByteArrayOutputStream zipOutputStream = new ByteArrayOutputStream();
|
||||
ZipOutputStream zos = new ZipOutputStream(zipOutputStream)) {
|
||||
|
||||
String s3Folder = s3PathConfig.generateDocumentPath(DocumentSourceTypeEnum.APPLICATION, applicationEntity.getCall().getId(), applicationId);
|
||||
|
||||
for (DocumentEntity document : documents) {
|
||||
String s3Folder = s3PathConfig.generateDocumentPath(DocumentSourceTypeEnum.APPLICATION, applicationEntity.getCall().getId(), applicationId);
|
||||
try (InputStream fileInputStream = amazonS3Service.getFile(s3Folder, document.getFilePath())) {
|
||||
String fileName = Utils.extractFileName(document.getFilePath());
|
||||
zos.putNextEntry(new ZipEntry(fileName));
|
||||
IOUtils.copy(fileInputStream, zos);
|
||||
zos.closeEntry();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Error downloading or adding document to ZIP: " + document.getFileName(), e);
|
||||
}
|
||||
String fileName = Utils.extractFileName(document.getFilePath());
|
||||
addDocumentToZip(zos, s3Folder, document.getFilePath(), fileName);
|
||||
}
|
||||
|
||||
if (signedDocument != null) {
|
||||
String s3Folder = s3PathConfig.generateDocumentPathForOther(DocOtherSourceTypeEnum.USER_SIGNED_DOCUMENT, applicationEntity.getCall().getId(), applicationId);
|
||||
try (InputStream fileInputStream = amazonS3Service.getFile(s3Folder, signedDocument.getFilePath())) {
|
||||
String fileName = signedDocument.getFileName();
|
||||
zos.putNextEntry(new ZipEntry(fileName));
|
||||
IOUtils.copy(fileInputStream, zos);
|
||||
zos.closeEntry();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Error downloading or adding signed document to ZIP: " + signedDocument.getFileName(), e);
|
||||
}
|
||||
String signedDocS3Folder = s3PathConfig.generateDocumentPathForOther(DocOtherSourceTypeEnum.USER_SIGNED_DOCUMENT, applicationEntity.getCall().getId(), applicationId);
|
||||
String signedDocFileName = signedDocument.getFileName();
|
||||
addDocumentToZip(zos, signedDocS3Folder, signedDocument.getFilePath(), signedDocFileName);
|
||||
}
|
||||
|
||||
zos.finish();
|
||||
@@ -1044,4 +1061,5 @@ public class ApplicationDao {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -7,14 +7,12 @@ 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.Map;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import net.gepafin.tendermanagement.entities.*;
|
||||
import net.gepafin.tendermanagement.enums.DocumentSourceTypeEnum;
|
||||
import net.gepafin.tendermanagement.model.response.*;
|
||||
@@ -22,6 +20,7 @@ import net.gepafin.tendermanagement.repositories.*;
|
||||
import net.gepafin.tendermanagement.service.*;
|
||||
import net.gepafin.tendermanagement.util.DateTimeUtil;
|
||||
import net.gepafin.tendermanagement.util.Utils;
|
||||
import net.gepafin.tendermanagement.util.Validator;
|
||||
import org.h2.util.IOUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -93,6 +92,8 @@ public class CallDao {
|
||||
private S3PathConfig s3PathConfig;
|
||||
@Autowired
|
||||
private BeneficiaryPreferredCallRepository beneficiaryPreferredCallRepository;
|
||||
@Autowired
|
||||
private Validator validator;
|
||||
|
||||
|
||||
public CallResponse createCallStep1(CreateCallRequestStep1 createCallRequest, UserEntity userEntity) {
|
||||
@@ -425,15 +426,24 @@ public class CallDao {
|
||||
Translator.toLocale(GepafinConstant.CALL_NOT_FOUND)));
|
||||
}
|
||||
|
||||
public CallResponse getCallById(UserEntity user, CallEntity callEntity) {
|
||||
public CallResponse getCallById(HttpServletRequest request,UserEntity user, CallEntity callEntity, Long companyId) {
|
||||
Long userId = user.getId();
|
||||
Long callId = callEntity.getId();
|
||||
|
||||
BeneficiaryPreferredCallEntity preferredCall = beneficiaryPreferredCallRepository
|
||||
.findByUserIdAndCallIdInAndIsDeletedFalse(userId, List.of(callId))
|
||||
.stream()
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
BeneficiaryPreferredCallEntity preferredCall;
|
||||
if (companyId != null) {
|
||||
validator.validateUserWithCompany(request, companyId);
|
||||
preferredCall = beneficiaryPreferredCallRepository
|
||||
.findByUserIdAndCallIdAndCompanyIdAndIsDeletedFalse(userId, callId, companyId)
|
||||
.orElse(null);
|
||||
} else {
|
||||
preferredCall = beneficiaryPreferredCallRepository
|
||||
.findByUserIdAndCallIdInAndIsDeletedFalse(userId, List.of(callId))
|
||||
.stream()
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
CallResponse callResponse = getCallResponseBean(callEntity);
|
||||
callResponse.setPreferredCallId(preferredCall != null ? preferredCall.getId() : null);
|
||||
|
||||
@@ -646,24 +656,21 @@ public class CallDao {
|
||||
return createCallResponseBean;
|
||||
}
|
||||
|
||||
public List<CallDetailsResponseBean> getAllCalls(UserEntity user) {
|
||||
public List<CallDetailsResponseBean> getAllCalls(HttpServletRequest request,UserEntity user, Long companyId) {
|
||||
String type = user.getRoleEntity().getRoleType();
|
||||
List<String> callStatusList = CallStatusEnum.getStatusValues();
|
||||
|
||||
if (Boolean.FALSE.equals(ROLE_SUPER_ADMIN.getValue().equals(type))) {
|
||||
callStatusList = List.of(CallStatusEnum.PUBLISH.getValue());
|
||||
}
|
||||
|
||||
List<CallEntity> calls = callRepository.findByStatusInAndHubId(callStatusList, user.getHub().getId());
|
||||
List<Long> callIds = calls.stream().map(CallEntity::getId).collect(Collectors.toList());
|
||||
|
||||
Map<String, BeneficiaryPreferredCallEntity> preferredCallsMap = getBeneficiaryPreferredCallsForUser(user, callIds);
|
||||
Map<String, BeneficiaryPreferredCallEntity> preferredCallsMap =
|
||||
getBeneficiaryPreferredCallsForUser(request,user, callIds, companyId);
|
||||
|
||||
return calls.stream()
|
||||
.map(call -> {
|
||||
CallDetailsResponseBean responseBean = convertToCallDetailsResponseBean(call);
|
||||
String key = user.getId() + "_" + call.getId();
|
||||
|
||||
BeneficiaryPreferredCallEntity preferredCall = preferredCallsMap.get(key);
|
||||
Long preferredId = (preferredCall != null && !preferredCall.getIsDeleted()) ? preferredCall.getId() : null;
|
||||
responseBean.setPreferredCallId(preferredId);
|
||||
@@ -673,9 +680,27 @@ public class CallDao {
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public Map<String, BeneficiaryPreferredCallEntity> getBeneficiaryPreferredCallsForUser(UserEntity user, List<Long> callIds) {
|
||||
List<BeneficiaryPreferredCallEntity> beneficiaryPreferredCalls = beneficiaryPreferredCallRepository
|
||||
.findByUserIdAndCallIdInAndIsDeletedFalse(user.getId(), callIds);
|
||||
public Map<String, BeneficiaryPreferredCallEntity> getBeneficiaryPreferredCallsForUser(HttpServletRequest request, UserEntity user, List<Long> callIds, Long companyId) {
|
||||
List<BeneficiaryPreferredCallEntity> beneficiaryPreferredCalls;
|
||||
|
||||
if (companyId != null) {
|
||||
validator.validateUserWithCompany(request, companyId);
|
||||
beneficiaryPreferredCalls = beneficiaryPreferredCallRepository
|
||||
.findByUserIdAndCallIdInAndCompanyIdAndIsDeletedFalse(user.getId(), callIds, companyId);
|
||||
} else {
|
||||
beneficiaryPreferredCalls = beneficiaryPreferredCallRepository
|
||||
.findByUserIdAndCallIdInAndIsDeletedFalse(user.getId(), callIds);
|
||||
beneficiaryPreferredCalls = beneficiaryPreferredCalls.stream()
|
||||
.collect(Collectors.collectingAndThen(
|
||||
Collectors.toMap(
|
||||
BeneficiaryPreferredCallEntity::getCallId,
|
||||
call -> call,
|
||||
(existing, replacement) -> existing
|
||||
),
|
||||
map -> new ArrayList<>(map.values())
|
||||
));
|
||||
|
||||
}
|
||||
|
||||
return beneficiaryPreferredCalls.stream()
|
||||
.collect(Collectors.toMap(
|
||||
|
||||
Reference in New Issue
Block a user