Merge pull request #41 from Kitzanos/feature/GEPAFINBE-39
GEPAFINBE-39(Email Management)
This commit is contained in:
@@ -3,6 +3,7 @@ package net.gepafin.tendermanagement.dao;
|
||||
import net.gepafin.tendermanagement.config.Translator;
|
||||
import net.gepafin.tendermanagement.constants.GepafinConstant;
|
||||
import net.gepafin.tendermanagement.entities.*;
|
||||
import net.gepafin.tendermanagement.entities.SystemEmailTemplatesEntity.SystemEmailTemplatesEntityTypeEnum;
|
||||
import net.gepafin.tendermanagement.enums.ApplicationSignedDocumentStatusEnum;
|
||||
import net.gepafin.tendermanagement.enums.ApplicationStatusTypeEnum;
|
||||
import net.gepafin.tendermanagement.enums.DocumentSourceTypeEnum;
|
||||
@@ -18,13 +19,16 @@ import net.gepafin.tendermanagement.service.CallService;
|
||||
import net.gepafin.tendermanagement.service.CompanyService;
|
||||
import net.gepafin.tendermanagement.service.DocumentService;
|
||||
import net.gepafin.tendermanagement.service.FormService;
|
||||
import net.gepafin.tendermanagement.service.SystemEmailTemplatesService;
|
||||
import net.gepafin.tendermanagement.util.DateTimeUtil;
|
||||
import net.gepafin.tendermanagement.util.FieldValidator;
|
||||
import net.gepafin.tendermanagement.util.MailUtil;
|
||||
import net.gepafin.tendermanagement.util.Utils;
|
||||
import net.gepafin.tendermanagement.util.Validator;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.CustomValidationException;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.ResourceNotFoundException;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.Status;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -90,6 +94,24 @@ public class ApplicationDao {
|
||||
private ProtocolRepository protocolRepository;
|
||||
|
||||
@Autowired
|
||||
|
||||
private SystemEmailTemplatesService systemEmailTemplatesService;
|
||||
|
||||
@Autowired
|
||||
private MailUtil mailUtil;
|
||||
|
||||
@Value("${default_System_Receiver_Email}")
|
||||
private String defaultSystemReceiverEmail;
|
||||
|
||||
@Value("${gepafin_email}")
|
||||
private String gepafinEmail;
|
||||
|
||||
@Value("${rinaldo_email}")
|
||||
private String rinaldoEmail;
|
||||
|
||||
@Value("${carlo_email}")
|
||||
private String carloEmail;
|
||||
|
||||
private AmazonS3Service amazonS3Service;
|
||||
|
||||
@Autowired
|
||||
@@ -552,7 +574,7 @@ public class ApplicationDao {
|
||||
}
|
||||
}
|
||||
|
||||
public ApplicationResponse updateApplicationStatus(Long applicationId, ApplicationStatusTypeEnum status) {
|
||||
public ApplicationResponse updateApplicationStatus(UserEntity userEntity, Long applicationId, ApplicationStatusTypeEnum status) {
|
||||
ApplicationEntity applicationEntity = validateApplication(applicationId);
|
||||
if (ApplicationStatusTypeEnum.SUBMIT.getValue().equals(applicationEntity.getStatus())) {
|
||||
throw new CustomValidationException(Status.BAD_REQUEST, Translator.toLocale(GepafinConstant.APPLICATION_SUBMITTED_CANNOT_CHANGE));
|
||||
@@ -586,6 +608,8 @@ public class ApplicationDao {
|
||||
applicationEntity.setProtocol(protocolEntity);
|
||||
applicationEntity.setStatus(ApplicationStatusTypeEnum.SUBMIT.getValue());
|
||||
applicationEntity.setSubmissionDate(DateTimeUtil.DateServerToUTC(LocalDateTime.now()));
|
||||
sendMailToUserAndCompany(userEntity, applicationEntity);
|
||||
sendMailTodefaultSystemAndGepafin(userEntity, applicationEntity);
|
||||
} else {
|
||||
applicationEntity.setStatus(status.getValue());
|
||||
}
|
||||
@@ -594,7 +618,7 @@ public class ApplicationDao {
|
||||
return getApplicationResponse(applicationEntity);
|
||||
}
|
||||
|
||||
public Integer calculateProgress(Long totalSteps, Long completedSteps) {
|
||||
public Integer calculateProgress(Long totalSteps, Long completedSteps) {
|
||||
if (FieldValidator.isNullOrZero(totalSteps)) {
|
||||
throw new CustomValidationException(Status.BAD_REQUEST,Translator.toLocale(GepafinConstant.TOTAL_STEPS_NOT_BE_ZERO));
|
||||
}
|
||||
@@ -678,7 +702,70 @@ public class ApplicationDao {
|
||||
protocolRepository.save(protocolEntity);
|
||||
return protocolEntity;
|
||||
}
|
||||
private void sendMailToUserAndCompany(UserEntity userEntity, ApplicationEntity applicationEntity) {
|
||||
CallEntity call =applicationEntity.getCall();
|
||||
CompanyEntity company = applicationEntity.getCompany();
|
||||
ProtocolEntity protocol = applicationEntity.getProtocol();
|
||||
SystemEmailTemplateResponse systemEmailTemplateResponse = systemEmailTemplatesService
|
||||
.retrieveTemplateByTypeAndCall(SystemEmailTemplatesEntityTypeEnum.APPLICATION_SUBMISSION_TO_USER_AND_COMPANY,
|
||||
call, null);
|
||||
|
||||
// Create the map for subject placeholders
|
||||
Map<String, String> subjectPlaceholders = new HashMap<>();
|
||||
subjectPlaceholders.put("{{call_name}}", call.getName());
|
||||
subjectPlaceholders.put("{{company_name}}", company.getCompanyName());
|
||||
|
||||
// Create the map for body placeholders
|
||||
Map<String, String> bodyPlaceholders = new HashMap<>();
|
||||
bodyPlaceholders.put("{{call_name}}", call.getName());
|
||||
bodyPlaceholders.put("{{protocol_number}}", protocol.getProtocolNumber().toString());
|
||||
bodyPlaceholders.put("{{date}}", DateTimeUtil.formatLocalDateTime(protocol.getCreatedDate(), GepafinConstant.YYYY_MM_DD_SLASH));
|
||||
bodyPlaceholders.put("{{time}}", DateTimeUtil.parseLocalTimeToString(protocol.getTime(), GepafinConstant.HH_MM_SS));
|
||||
|
||||
// Replace placeholders in the subject and body
|
||||
String subject = Utils.replacePlaceholders(systemEmailTemplateResponse.getSubject(), subjectPlaceholders);
|
||||
String body = Utils.replacePlaceholders(systemEmailTemplateResponse.getHtmlContent(), bodyPlaceholders);
|
||||
|
||||
String email = userEntity.getEmail();
|
||||
if (userEntity.getBeneficiary() != null) {
|
||||
email = userEntity.getBeneficiary().getEmail();
|
||||
}
|
||||
mailUtil.sendByMailGun(subject, body, List.of(email), null);
|
||||
mailUtil.sendByMailGun(subject, body, List.of(applicationEntity.getCompany().getEmail()), null);
|
||||
|
||||
}
|
||||
|
||||
private void sendMailTodefaultSystemAndGepafin(UserEntity userEntity, ApplicationEntity applicationEntity) {
|
||||
CallEntity call = applicationEntity.getCall();
|
||||
CompanyEntity company = applicationEntity.getCompany();
|
||||
ProtocolEntity protocol = applicationEntity.getProtocol();
|
||||
SystemEmailTemplateResponse systemEmailTemplateResponse = systemEmailTemplatesService
|
||||
.retrieveTemplateByTypeAndCall(SystemEmailTemplatesEntityTypeEnum.APPLICATION_SUBMISSION_TO_GEPAFIN,
|
||||
call, null);
|
||||
|
||||
// Create the map for subject placeholders
|
||||
Map<String, String> subjectPlaceholders = new HashMap<>();
|
||||
subjectPlaceholders.put("{{call_name}}", call.getName());
|
||||
subjectPlaceholders.put("{{company_name}}", company.getCompanyName());
|
||||
|
||||
// Create the map for body placeholders
|
||||
Map<String, String> bodyPlaceholders = new HashMap<>();
|
||||
bodyPlaceholders.put("{{call_name}}", call.getName());
|
||||
bodyPlaceholders.put("{{protocol_number}}", protocol.getProtocolNumber().toString());
|
||||
bodyPlaceholders.put("{{date}}", DateTimeUtil.formatLocalDateTime(protocol.getCreatedDate(), GepafinConstant.YYYY_MM_DD_SLASH));
|
||||
bodyPlaceholders.put("{{time}}", DateTimeUtil.parseLocalTimeToString(protocol.getTime(), GepafinConstant.HH_MM_SS));
|
||||
|
||||
// Replace placeholders in the subject and body
|
||||
String subject = Utils.replacePlaceholders(systemEmailTemplateResponse.getSubject(), subjectPlaceholders);
|
||||
String body = Utils.replacePlaceholders(systemEmailTemplateResponse.getHtmlContent(), bodyPlaceholders);
|
||||
|
||||
|
||||
mailUtil.sendByMailGun(subject, body, List.of(defaultSystemReceiverEmail), null);
|
||||
mailUtil.sendByMailGun(subject, body, List.of(gepafinEmail), null);
|
||||
mailUtil.sendByMailGun(subject, body, List.of(rinaldoEmail), null);
|
||||
mailUtil.sendByMailGun(subject, body, List.of(carloEmail), null);
|
||||
|
||||
}
|
||||
public ApplicationSignedDocumentResponse uploadSignedDocument(HttpServletRequest request, Long applicationId,
|
||||
MultipartFile file) {
|
||||
ApplicationEntity applicationEntity = validateApplication(applicationId);
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
package net.gepafin.tendermanagement.dao;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import net.gepafin.tendermanagement.entities.CallEntity;
|
||||
import net.gepafin.tendermanagement.entities.SystemEmailTemplatesEntity;
|
||||
import net.gepafin.tendermanagement.entities.SystemEmailTemplatesEntity.SystemEmailTemplatesEntityTypeEnum;
|
||||
import net.gepafin.tendermanagement.model.response.SystemEmailTemplateResponse;
|
||||
import net.gepafin.tendermanagement.repositories.SystemEmailTemplatesRespository;
|
||||
import net.gepafin.tendermanagement.util.Utils;
|
||||
|
||||
@Component
|
||||
public class SystemEmailTemplatesDao {
|
||||
|
||||
@Autowired
|
||||
private SystemEmailTemplatesRespository systemEmailTemplatesRespository;
|
||||
|
||||
@Value("${fe.base.url}")
|
||||
private String feBaseUrl;
|
||||
|
||||
|
||||
public SystemEmailTemplateResponse retrieveTemplate(SystemEmailTemplatesEntityTypeEnum type, CallEntity call, Locale language) {
|
||||
SystemEmailTemplatesEntity dbSystemEmailTemplatesEntity = null;
|
||||
if(call != null){
|
||||
// dbSystemEmailTemplatesEntity = systemEmailTemplatesRespository
|
||||
// .findByTypeAndCallId(type.getValue(), call.getId());
|
||||
}
|
||||
if(dbSystemEmailTemplatesEntity == null){
|
||||
dbSystemEmailTemplatesEntity = systemEmailTemplatesRespository
|
||||
.findByType(type.getValue());
|
||||
}
|
||||
|
||||
SystemEmailTemplateResponse systemEmailTemplateResponse = replaceHtmlContant(dbSystemEmailTemplatesEntity, call, language, Boolean.TRUE);
|
||||
return systemEmailTemplateResponse;
|
||||
}
|
||||
|
||||
private SystemEmailTemplateResponse replaceHtmlContant(SystemEmailTemplatesEntity dbSystemEmailTemplatesEntity,
|
||||
CallEntity call, Locale language1, Boolean isDefaultReplace) {
|
||||
String language = null;
|
||||
String htmlContent = dbSystemEmailTemplatesEntity.getHtmlContent();
|
||||
String subject = dbSystemEmailTemplatesEntity.getSubject();
|
||||
|
||||
if (language1 == null) {
|
||||
// language = getLanguage(LocaleContextHolder.getLocale());
|
||||
language="italian";
|
||||
}else{
|
||||
language="italian";
|
||||
}
|
||||
Map<String, String> languageMap = new HashMap<>();
|
||||
String jsonContent = dbSystemEmailTemplatesEntity.getJson();
|
||||
if (Boolean.FALSE.equals(StringUtils.isEmpty(jsonContent))) {
|
||||
Map<String, Map<String, String>> jsonMap = Utils.parseJsonContent(jsonContent);
|
||||
if (jsonMap != null && jsonMap.containsKey(language)) {
|
||||
languageMap = jsonMap.get(language);
|
||||
htmlContent = replacePlaceholders(htmlContent, languageMap);
|
||||
subject = replaceSubjectPlaceholders(subject, languageMap);
|
||||
|
||||
}
|
||||
}
|
||||
if(Boolean.TRUE.equals(StringUtils.isEmpty(subject))){
|
||||
subject = "";
|
||||
}
|
||||
|
||||
|
||||
htmlContent = replacePlatformLinkPlaceholder(call, htmlContent, languageMap);
|
||||
SystemEmailTemplateResponse systemEmailTemplateResponse = new SystemEmailTemplateResponse();
|
||||
systemEmailTemplateResponse.setHtmlContent(htmlContent);
|
||||
systemEmailTemplateResponse.setSubject(subject);
|
||||
systemEmailTemplateResponse.setJsonMap(languageMap);
|
||||
return systemEmailTemplateResponse;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// private String getLanguage(Locale locale) {
|
||||
// return switch (locale.getLanguage()) {
|
||||
// case "en" -> "english";
|
||||
// case "it" -> "italian";
|
||||
// default -> "italian";
|
||||
// };
|
||||
// }
|
||||
|
||||
private String replacePlaceholders(String htmlContent, Map<String, String> languageMap) {
|
||||
for (Map.Entry<String, String> entry : languageMap.entrySet()) {
|
||||
htmlContent = htmlContent.replace("{{" + entry.getKey() + "}}", entry.getValue());
|
||||
}
|
||||
return htmlContent;
|
||||
}
|
||||
|
||||
private String replaceSubjectPlaceholders(String subject, Map<String, String> languageMap) {
|
||||
if(languageMap.containsKey("subject") && subject != null){
|
||||
String value = languageMap.get("subject");
|
||||
subject = subject.replace("{{subject}}", value);
|
||||
return subject;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
private String replacePlatformLinkPlaceholder(CallEntity call, String htmlContent,
|
||||
Map<String, String> languageMap) {
|
||||
String platformLink = feBaseUrl;
|
||||
|
||||
// if(hubEntity != null && Boolean.FALSE.equals(isEmpty(hubEntity.getDomainName()))){
|
||||
// platformLink = hubEntity.getDomainName();
|
||||
// }
|
||||
htmlContent = htmlContent.replace("{{platform_link}}", platformLink);
|
||||
return htmlContent;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user