Merge pull request #41 from Kitzanos/feature/GEPAFINBE-39

GEPAFINBE-39(Email Management)
This commit is contained in:
rajeshkhore
2024-10-13 12:11:27 +05:30
committed by GitHub
18 changed files with 539 additions and 3 deletions

View File

@@ -199,6 +199,8 @@ public class GepafinConstant {
public static final String DELEGATION_NOT_FOUND = "delegation.not.found";
public static final String USER_COMPANY_RELATION_NOT_FOUND = "user.company.relation.not.found";
public static final String DELEGATION_DELETE_SUCCESS = "delegation.delete.success";
public static final String HH_MM_SS = "HH:mm:ss";
public static final String USER_NOT_AUTHORIZED_TO_CREATE_APPLICATION = "user.not.authorized.create.application";
public static final String APPLICATION_SUBMITTED_CANNOT_CHANGE = "application.submitted.cannot.change";
public static final String CALL_DOCUMENTS_FETCH_SUCCESS_MSG = "call.documents.fetch.success";

View File

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

View File

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

View File

@@ -0,0 +1,56 @@
package net.gepafin.tendermanagement.entities;
import com.fasterxml.jackson.annotation.JsonValue;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.Data;
@Entity
@Table(name = "system_email_template")
@Data
public class SystemEmailTemplatesEntity extends BaseEntity {
@Column(name = "TEMPLATE_NAME")
private String templateName;
@Column(name = "TYPE")
private String type;
@Column(name = "HTML_CONTENT", columnDefinition = "TEXT")
private String htmlContent;
@Column(name = "SUBJECT")
private String subject;
@Column(name = "JSON", columnDefinition = "TEXT")
private String json;
@Column(name = "SYSTEM")
private Boolean system;
@Column(name ="IS_DELETED", nullable = false)
private Boolean isDeleted = false;
public enum SystemEmailTemplatesEntityTypeEnum {
APPLICATION_SUBMISSION_TO_USER_AND_COMPANY("APPLICATION_SUBMISSION_TO_USER_AND_COMPANY"),
APPLICATION_SUBMISSION_TO_GEPAFIN("APPLICATION_SUBMISSION_TO_GEPAFIN");
private String value;
SystemEmailTemplatesEntityTypeEnum(String value) {
this.value = value;
}
@JsonValue
public String getValue() {
return value;
}
}
}

View File

@@ -0,0 +1,16 @@
package net.gepafin.tendermanagement.model.response;
import java.util.Map;
import lombok.Data;
@Data
public class SystemEmailTemplateResponse {
String htmlContent;
String subject;
Map<String, String> jsonMap;
}

View File

@@ -0,0 +1,17 @@
package net.gepafin.tendermanagement.repositories;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import net.gepafin.tendermanagement.entities.SystemEmailTemplatesEntity;
public interface SystemEmailTemplatesRespository extends JpaRepository<SystemEmailTemplatesEntity, Long> {
// @Query("select s from SystemEmailTemplatesEntity s where s.type=:type and s.call.id=:callId and s.isDeleted =false and s.system = false")
// SystemEmailTemplatesEntity findByTypeAndCallId(@Param("type") String type, @Param("callId") Long callId);
@Query("select s from SystemEmailTemplatesEntity s where s.type=:type and s.isDeleted =false and s.system = true")
SystemEmailTemplatesEntity findByType(@Param("type") String type);
}

View File

@@ -0,0 +1,13 @@
package net.gepafin.tendermanagement.service;
import java.util.Locale;
import net.gepafin.tendermanagement.entities.CallEntity;
import net.gepafin.tendermanagement.entities.SystemEmailTemplatesEntity.SystemEmailTemplatesEntityTypeEnum;
import net.gepafin.tendermanagement.model.response.SystemEmailTemplateResponse;
public interface SystemEmailTemplatesService {
SystemEmailTemplateResponse retrieveTemplateByTypeAndCall(SystemEmailTemplatesEntityTypeEnum type, CallEntity call, Locale language);
}

View File

@@ -80,7 +80,8 @@ public class ApplicationServiceImpl implements ApplicationService {
@Override
@Transactional(rollbackFor = Exception.class)
public ApplicationResponse updateApplicationStatus(HttpServletRequest request, Long applicationId, ApplicationStatusTypeEnum status) {
return applicationDao.updateApplicationStatus(applicationId, status);
UserEntity userEntity = validator.validateUser(request);
return applicationDao.updateApplicationStatus(userEntity, applicationId, status);
}

View File

@@ -0,0 +1,27 @@
package net.gepafin.tendermanagement.service.impl;
import java.util.Locale;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import net.gepafin.tendermanagement.dao.SystemEmailTemplatesDao;
import net.gepafin.tendermanagement.entities.CallEntity;
import net.gepafin.tendermanagement.entities.SystemEmailTemplatesEntity.SystemEmailTemplatesEntityTypeEnum;
import net.gepafin.tendermanagement.model.response.SystemEmailTemplateResponse;
import net.gepafin.tendermanagement.service.SystemEmailTemplatesService;
@Service
public class SystemEmailTemplatesServiceImpl implements SystemEmailTemplatesService {
@Autowired
private SystemEmailTemplatesDao systemEmailTemplatesDao;
@Override
public SystemEmailTemplateResponse retrieveTemplateByTypeAndCall(SystemEmailTemplatesEntityTypeEnum type, CallEntity call, Locale language) {
return systemEmailTemplatesDao.retrieveTemplate(type, call, language);
}
}

View File

@@ -92,4 +92,20 @@ public class DateTimeUtil {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
return LocalDateTime.parse(dateTimeStr, formatter);
}
public static String parseLocalTimeToString(LocalTime time, String format) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
return time.format(formatter);
}
// Method 2: Convert String and format to LocalTime
public static LocalTime parseStringToLocalTime(String timeString, String format) {
try {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
return LocalTime.parse(timeString, formatter);
} catch (DateTimeParseException e) {
System.out.println("Invalid time format: " + e.getMessage());
return null;
}
}
}

View File

@@ -0,0 +1,78 @@
package net.gepafin.tendermanagement.util;
import java.util.Arrays;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import com.mailgun.api.v3.MailgunMessagesApi;
import com.mailgun.client.MailgunClient;
@Component
public class MailUtil {
@Value("${apiKey}")
private String apiKeyValue;
@Value("${mailGun_user}")
private String mailGunUser;
@Value("${mailGun_apiKey}")
private String mailGunApiKey;
@Value("${mailGun_domainName}")
private String mailGunDomainName;
@Value("${mailGun_base_url}")
private String mailGunBaseUrl;
@Value("${isMailSendingEnabled}")
private String isEmailSendingEnabled;
@Autowired
private Environment environment;
public Boolean isTestProfileActivated() {
String[] activeProfiles = environment.getActiveProfiles();
return Arrays.stream(activeProfiles).anyMatch("test"::equals);
}
public void sendMailByMailGunAPI(List<String> recipents, List<String> CC, List<String> BCC, String subject,
String body, String replyTo) {
if (Boolean.FALSE.equals(Boolean.parseBoolean(isEmailSendingEnabled))) {
return;
}
MailgunMessagesApi mailgunMessagesApi = MailgunClient.config(mailGunBaseUrl, mailGunApiKey)
.createApi(MailgunMessagesApi.class);
String mailFrom = mailGunUser;
com.mailgun.model.message.Message.MessageBuilder temp = com.mailgun.model.message.Message.builder()
.replyTo(replyTo).from(mailFrom).to(recipents).subject(subject).html(body);
if (Boolean.FALSE.equals(CollectionUtils.isEmpty(CC))) {
temp.cc(CC);
}
if (Boolean.FALSE.equals(CollectionUtils.isEmpty(BCC))) {
temp.bcc(BCC);
}
if (Boolean.FALSE.equals(isTestProfileActivated())) {
com.mailgun.model.message.Message message = temp.build();
mailgunMessagesApi.sendMessage(mailGunDomainName, message);
}
}
public void sendByMailGun(String subject, String body, List<String> receiverEmails, String replyTo) {
sendMailByMailGunAPI(receiverEmails, null, null, subject, body, replyTo);
}
}

View File

@@ -5,6 +5,7 @@ import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
@@ -276,4 +277,30 @@ public class Utils {
}
}
public static Map<String, Map<String, String>> parseJsonContent(String jsonContent) {
ObjectMapper objectMapper = new ObjectMapper();
try {
return mapper.readValue(jsonContent, HashMap.class);
} catch (Exception exception) {
log.error(exception.getMessage());
}
return new HashMap<>();
}
// Utility method to replace placeholders with their values, handling nulls
public static String replacePlaceholders(String text, Map<String, String> placeholders) {
if (text == null) {
return "";
}
for (Map.Entry<String, String> entry : placeholders.entrySet()) {
text = replaceNull(text, entry.getKey(), entry.getValue());
}
return text;
}
// Method to safely replace nulls with an empty string or a default value
private static String replaceNull(String text, String target, String replacement) {
return text.replace(target, replacement != null ? replacement : "");
}
}