From ea66cf115d17850c76846addd2d5b13daa60fe2b Mon Sep 17 00:00:00 2001 From: Radheshhathwar Date: Mon, 24 Jul 2023 18:37:43 +0530 Subject: [PATCH 1/3] Added comment in email template, sendNotification and sendEmailNotification --- .../service/impl/NotificationServiceImpl.java | 425 +++++++++--------- 1 file changed, 216 insertions(+), 209 deletions(-) diff --git a/src/main/java/org/sunbird/workflow/service/impl/NotificationServiceImpl.java b/src/main/java/org/sunbird/workflow/service/impl/NotificationServiceImpl.java index a98316f2..1304f004 100644 --- a/src/main/java/org/sunbird/workflow/service/impl/NotificationServiceImpl.java +++ b/src/main/java/org/sunbird/workflow/service/impl/NotificationServiceImpl.java @@ -32,237 +32,244 @@ @Service public class NotificationServiceImpl { - public static final String EMAILTEMPLATE = "emailtemplate"; - Logger logger = LogManager.getLogger(ApplicationProcessingConsumer.class); + public static final String EMAILTEMPLATE = "emailtemplate"; + Logger logger = LogManager.getLogger(ApplicationProcessingConsumer.class); - @Autowired - private WfStatusRepo wfStatusRepo; + @Autowired + private WfStatusRepo wfStatusRepo; - @Autowired - private Configuration configuration; + @Autowired + private Configuration configuration; - @Autowired - private RequestServiceImpl requestService; + @Autowired + private RequestServiceImpl requestService; - @Autowired - private ObjectMapper mapper; + @Autowired + private ObjectMapper mapper; - @Autowired - private Workflowservice workflowservice; + @Autowired + private Workflowservice workflowservice; - @Autowired - private UserProfileWfServiceImpl userProfileWfService; + @Autowired + private UserProfileWfServiceImpl userProfileWfService; - @Autowired - private EmailTemplateRepo emailTemplateRepo; + @Autowired + private EmailTemplateRepo emailTemplateRepo; - private static final String WORK_FLOW_EVENT_NAME = "workflow_service_notification"; + private static final String WORK_FLOW_EVENT_NAME = "workflow_service_notification"; - private static final String USER_NAME_CONSTANT = "user"; + private static final String USER_NAME_CONSTANT = "user"; - private static final String USER_NAME_TAG = "#userName"; + private static final String USER_NAME_TAG = "#userName"; - private static final String STATE_NAME_TAG = "#state"; + private static final String STATE_NAME_TAG = "#state"; - private static final String FIELD_KEY_TAG = "#fieldKey"; + private static final String FIELD_KEY_TAG = "#fieldKey"; - private static final String TO_VALUE_TAG = "#toValue"; + private static final String COMMENT_TAG = "#comment"; - private static final String TO_VALUE_CONST = "toValue"; + private static final String TO_VALUE_TAG = "#toValue"; - private static final String MAIL_SUBJECT = "Your request is #state"; - private static final String MDO_MAIL_SUBJECT = "Request for approval"; + private static final String TO_VALUE_CONST = "toValue"; - private static final String MAIL_BODY = "Your request to update #fieldKey to #toValue is #state."; + private static final String MAIL_SUBJECT = "Your request is #state"; + private static final String MDO_MAIL_SUBJECT = "Request for approval"; - /** - * Send notification to the user based on state of application - * - * @param wfRequest workflow request - */ - public void sendNotification(WfRequest wfRequest) { - WfStatusEntity wfStatusEntity = wfStatusRepo.findByApplicationIdAndWfId(wfRequest.getApplicationId(), - wfRequest.getWfId()); - WfStatus wfStatus = workflowservice.getWorkflowStates(wfStatusEntity.getRootOrg(), wfStatusEntity.getOrg(), - wfStatusEntity.getServiceName(), wfStatusEntity.getCurrentStatus()); - try { - logger.info("Notification workflow status entity, {}", mapper.writeValueAsString(wfStatusEntity)); - logger.info("Notification workflow status model, {}", mapper.writeValueAsString(wfStatus)); - } catch (JsonProcessingException e) { - e.printStackTrace(); - } - if (!ObjectUtils.isEmpty(wfStatus.getNotificationEnable()) && wfStatus.getNotificationEnable()) { - logger.info("Enter's in the notification block"); + private static final String MAIL_BODY = "Your request to update #fieldKey to #toValue is #state, due to #comment"; + + /** + * Send notification to the user based on state of application + * + * @param wfRequest workflow request + */ + public void sendNotification(WfRequest wfRequest) { + WfStatusEntity wfStatusEntity = wfStatusRepo.findByApplicationIdAndWfId(wfRequest.getApplicationId(), + wfRequest.getWfId()); + WfStatus wfStatus = workflowservice.getWorkflowStates(wfStatusEntity.getRootOrg(), wfStatusEntity.getOrg(), + wfStatusEntity.getServiceName(), wfStatusEntity.getCurrentStatus()); + try { + logger.info("Notification workflow status entity, {}", mapper.writeValueAsString(wfStatusEntity)); + logger.info("Notification workflow status model, {}", mapper.writeValueAsString(wfStatus)); + } catch (JsonProcessingException e) { + e.printStackTrace(); + } + if (!ObjectUtils.isEmpty(wfStatus.getNotificationEnable()) && wfStatus.getNotificationEnable()) { + logger.info("Enter's in the notification block"); Set usersId = new HashSet<>(); usersId.add(wfRequest.getActorUserId()); - usersId.add(wfStatusEntity.getApplicationId()); - HashMap usersObj = userProfileWfService.getUsersResult(usersId); - Map recipientInfo = (Map)usersObj.get(wfStatusEntity.getApplicationId()); - Map senderInfo = (Map)usersObj.get(wfRequest.getActorUserId()); - Map params = new HashMap<>(); - NotificationRequest request = new NotificationRequest(); - request.setDeliveryType("message"); - request.setIds(Arrays.asList((String)recipientInfo.get("email"))); - request.setMode("email"); - Template template = new Template(); - template.setId(EMAILTEMPLATE); - Optional> updatedFieldValue = wfRequest.getUpdateFieldValues().stream().findFirst(); - if (updatedFieldValue.isPresent()) { - HashMap toValue = (HashMap) updatedFieldValue.get().get(TO_VALUE_CONST); - params.put("body", MAIL_BODY.replace(STATE_NAME_TAG, wfStatusEntity.getCurrentStatus()).replace(FIELD_KEY_TAG, toValue.entrySet().iterator().next().getKey()) - .replace(TO_VALUE_TAG, (String)toValue.entrySet().iterator().next().getValue())); - } - params.put("orgImageUrl", null); - template.setParams(params); - Config config = new Config(); - config.setSubject(MAIL_SUBJECT.replace(STATE_NAME_TAG, wfStatusEntity.getCurrentStatus())); - config.setSender((String)senderInfo.get("email")); - Map req = new HashMap<>(); - request.setTemplate(template); - request.setConfig(config); - Map> notificationMap = new HashMap<>(); - notificationMap.put("notifications", Arrays.asList(request)); - req.put("request", notificationMap); - sendNotification(req); - } - } + usersId.add(wfStatusEntity.getApplicationId()); + HashMap usersObj = userProfileWfService.getUsersResult(usersId); + Map recipientInfo = (Map) usersObj.get(wfStatusEntity.getApplicationId()); + Map senderInfo = (Map) usersObj.get(wfRequest.getActorUserId()); + Map params = new HashMap<>(); + NotificationRequest request = new NotificationRequest(); + request.setDeliveryType("message"); + request.setIds(Arrays.asList((String) recipientInfo.get("email"))); + request.setMode("email"); + Template template = new Template(); + template.setId(EMAILTEMPLATE); + Optional> updatedFieldValue = wfRequest.getUpdateFieldValues().stream().findFirst(); + if (updatedFieldValue.isPresent()) { + HashMap toValue = (HashMap) updatedFieldValue.get().get(TO_VALUE_CONST); + params.put("body", MAIL_BODY.replace(STATE_NAME_TAG, wfStatusEntity.getCurrentStatus()) + .replace(FIELD_KEY_TAG, toValue.entrySet().iterator().next().getKey()) + .replace(COMMENT_TAG, (String) toValue.entrySet().iterator().next().getValue()) + .replace(TO_VALUE_TAG, (String) toValue.entrySet().iterator().next().getValue())); + } + params.put("orgImageUrl", null); + template.setParams(params); + Config config = new Config(); + config.setSubject(MAIL_SUBJECT.replace(STATE_NAME_TAG, wfStatusEntity.getCurrentStatus())); + config.setSender((String) senderInfo.get("email")); + Map req = new HashMap<>(); + request.setTemplate(template); + request.setConfig(config); + Map> notificationMap = new HashMap<>(); + notificationMap.put("notifications", Arrays.asList(request)); + req.put("request", notificationMap); + sendNotification(req); + } + } - public void sendEmailNotification(WfRequest wfRequest) { - WfStatusEntity wfStatusEntity = wfStatusRepo.findByApplicationIdAndWfId(wfRequest.getApplicationId(), - wfRequest.getWfId()); - WfStatus wfStatus = workflowservice.getWorkflowStates(wfStatusEntity.getRootOrg(), wfStatusEntity.getOrg(), - wfStatusEntity.getServiceName(), wfStatusEntity.getCurrentStatus()); - try { - logger.info("Notification workflow status entity, {}", mapper.writeValueAsString(wfStatusEntity)); - logger.info("Notification workflow status model, {}", mapper.writeValueAsString(wfStatus)); - } catch (JsonProcessingException e) { - e.printStackTrace(); - } - if (!ObjectUtils.isEmpty(wfStatus.getNotificationEnable()) && wfStatus.getNotificationEnable()) { - logger.info("Enters in the email notification block"); - List> updatedFieldValues = wfRequest.getUpdateFieldValues(); - Set emailSet = new HashSet<>(); - // Find the email address from the updated field values - for (Map fieldValue : updatedFieldValues) { - if (fieldValue.containsKey("email")) { - String email = (String) fieldValue.get("email"); - emailSet.add(email); - break; - } - } - if (!emailSet.isEmpty()) { - HashMap params = new HashMap<>(); - NotificationRequest request = new NotificationRequest(); - request.setDeliveryType("message"); - request.setIds(new ArrayList<>(emailSet)); - request.setMode("email"); - Template template = new Template(); - template.setId(EMAILTEMPLATE); - Optional> updatedFieldValue = wfRequest.getUpdateFieldValues().stream().findFirst(); - if (updatedFieldValue.isPresent()) { - HashMap toValue = (HashMap) updatedFieldValue.get().get(TO_VALUE_CONST); - params.put("body", MAIL_BODY.replace(STATE_NAME_TAG, wfStatusEntity.getCurrentStatus()).replace(FIELD_KEY_TAG, toValue.entrySet().iterator().next().getKey()) - .replace(TO_VALUE_TAG, (String) toValue.entrySet().iterator().next().getValue())); - } - params.put("orgImageUrl", null); - template.setParams(params); - Config config = new Config(); - config.setSubject(MAIL_SUBJECT.replace(STATE_NAME_TAG, wfStatusEntity.getCurrentStatus())); - config.setSender(configuration.getSenderMail()); - Map req = new HashMap<>(); - request.setTemplate(template); - request.setConfig(config); - Map> notificationMap = new HashMap<>(); - notificationMap.put("notifications", Arrays.asList(request)); - req.put("request", notificationMap); - sendNotification(req); - } else { - logger.warn("Email address not found in the update field values."); - } - } - } + public void sendEmailNotification(WfRequest wfRequest) { + WfStatusEntity wfStatusEntity = wfStatusRepo.findByApplicationIdAndWfId(wfRequest.getApplicationId(), + wfRequest.getWfId()); + WfStatus wfStatus = workflowservice.getWorkflowStates(wfStatusEntity.getRootOrg(), wfStatusEntity.getOrg(), + wfStatusEntity.getServiceName(), wfStatusEntity.getCurrentStatus()); + try { + logger.info("Notification workflow status entity, {}", mapper.writeValueAsString(wfStatusEntity)); + logger.info("Notification workflow status model, {}", mapper.writeValueAsString(wfStatus)); + } catch (JsonProcessingException e) { + e.printStackTrace(); + } + if (!ObjectUtils.isEmpty(wfStatus.getNotificationEnable()) && wfStatus.getNotificationEnable()) { + logger.info("Enters in the email notification block"); + List> updatedFieldValues = wfRequest.getUpdateFieldValues(); + Set emailSet = new HashSet<>(); + // Find the email address from the updated field values + for (Map fieldValue : updatedFieldValues) { + if (fieldValue.containsKey("email")) { + String email = (String) fieldValue.get("email"); + emailSet.add(email); + break; + } + } + if (!emailSet.isEmpty()) { + HashMap params = new HashMap<>(); + NotificationRequest request = new NotificationRequest(); + request.setDeliveryType("message"); + request.setIds(new ArrayList<>(emailSet)); + request.setMode("email"); + Template template = new Template(); + template.setId(EMAILTEMPLATE); + Optional> updatedFieldValue = wfRequest.getUpdateFieldValues().stream().findFirst(); + if (updatedFieldValue.isPresent()) { + HashMap toValue = (HashMap) updatedFieldValue.get().get(TO_VALUE_CONST); + params.put("body", MAIL_BODY.replace(STATE_NAME_TAG, wfStatusEntity.getCurrentStatus()) + .replace(FIELD_KEY_TAG, toValue.entrySet().iterator().next().getKey()) + .replace(COMMENT_TAG, (String) toValue.entrySet().iterator().next().getValue()) + .replace(TO_VALUE_TAG, (String) toValue.entrySet().iterator().next().getValue())); + } + params.put("orgImageUrl", null); + template.setParams(params); + Config config = new Config(); + config.setSubject(MAIL_SUBJECT.replace(STATE_NAME_TAG, wfStatusEntity.getCurrentStatus())); + config.setSender(configuration.getSenderMail()); + Map req = new HashMap<>(); + request.setTemplate(template); + request.setConfig(config); + Map> notificationMap = new HashMap<>(); + notificationMap.put("notifications", Arrays.asList(request)); + req.put("request", notificationMap); + sendNotification(req); + } else { + logger.warn("Email address not found in the update field values."); + } + } + } - public void sendNotificationToMdoAdmin(WfRequest wfRequest) { - WfStatusEntity wfStatusEntity = wfStatusRepo.findByApplicationIdAndWfId(wfRequest.getApplicationId(), - wfRequest.getWfId()); - WfStatus wfStatus = workflowservice.getWorkflowStates(wfStatusEntity.getRootOrg(), wfStatusEntity.getOrg(), - wfStatusEntity.getServiceName(), wfStatusEntity.getCurrentStatus()); - if (!ObjectUtils.isEmpty(wfStatus.getNotificationEnable()) && wfStatus.getNotificationEnable() - && !Arrays.asList(Constants.REJECTED, Constants.APPROVED).contains(wfStatus.getState())) { - logger.info("Enter in the notification block"); - List mdoAdminList = userProfileWfService.getMdoAdminDetails(wfRequest.getRootOrgId()); - Map params = new HashMap<>(); - NotificationRequest request = new NotificationRequest(); - request.setDeliveryType("message"); - List mdoMailList = mdoAdminList.stream().collect(Collectors.toList()); - if (!CollectionUtils.isEmpty(mdoMailList)) { - request.setIds(mdoMailList); - request.setMode("email"); - Template template = new Template(); - template.setId(configuration.getMdoEmailTemplate()); - HashMap usersObj = userProfileWfService.getUsersResult(Collections.singleton(wfRequest.getUserId())); - Map recipientInfo = (Map) usersObj.get(wfStatusEntity.getUserId()); - params.put(Constants.USER_NAME, recipientInfo.get(Constants.FIRST_NAME)); - Optional> updatedFieldValue = wfRequest.getUpdateFieldValues().stream().findFirst(); - if (updatedFieldValue.isPresent()) { - HashMap toValue = (HashMap) updatedFieldValue.get().get(TO_VALUE_CONST); - List fieldNames = toValue.keySet().stream().collect(Collectors.toList()); - String approvalUrl = configuration.getDomainHost() + configuration.getMdoBaseUrl().replace("{id}", wfRequest.getApplicationId()); - params.put(Constants.LINK, approvalUrl); - params.put(Constants.FIELDS, fieldNames); - params.put(Constants.SUPPORT_EMAIL, configuration.getSenderMail()); - } - String constructedEmailTemplate = constructEmailTemplate(configuration.getMdoEmailTemplate(), params); - if (StringUtils.isNotEmpty(constructedEmailTemplate)) { - template.setData(constructedEmailTemplate); - } - template.setParams(params); - Config config = new Config(); - config.setSubject(MDO_MAIL_SUBJECT); - config.setSender(configuration.getSenderMail()); - Map req = new HashMap<>(); - request.setTemplate(template); - request.setConfig(config); - Map> notificationMap = new HashMap<>(); - notificationMap.put("notifications", Arrays.asList(request)); - req.put("request", notificationMap); - sendNotification(req); - } - } - } + public void sendNotificationToMdoAdmin(WfRequest wfRequest) { + WfStatusEntity wfStatusEntity = wfStatusRepo.findByApplicationIdAndWfId(wfRequest.getApplicationId(), + wfRequest.getWfId()); + WfStatus wfStatus = workflowservice.getWorkflowStates(wfStatusEntity.getRootOrg(), wfStatusEntity.getOrg(), + wfStatusEntity.getServiceName(), wfStatusEntity.getCurrentStatus()); + if (!ObjectUtils.isEmpty(wfStatus.getNotificationEnable()) && wfStatus.getNotificationEnable() + && !Arrays.asList(Constants.REJECTED, Constants.APPROVED).contains(wfStatus.getState())) { + logger.info("Enter in the notification block"); + List mdoAdminList = userProfileWfService.getMdoAdminDetails(wfRequest.getRootOrgId()); + Map params = new HashMap<>(); + NotificationRequest request = new NotificationRequest(); + request.setDeliveryType("message"); + List mdoMailList = mdoAdminList.stream().collect(Collectors.toList()); + if (!CollectionUtils.isEmpty(mdoMailList)) { + request.setIds(mdoMailList); + request.setMode("email"); + Template template = new Template(); + template.setId(configuration.getMdoEmailTemplate()); + HashMap usersObj = userProfileWfService.getUsersResult(Collections.singleton(wfRequest.getUserId())); + Map recipientInfo = (Map) usersObj.get(wfStatusEntity.getUserId()); + params.put(Constants.USER_NAME, recipientInfo.get(Constants.FIRST_NAME)); + Optional> updatedFieldValue = wfRequest.getUpdateFieldValues().stream().findFirst(); + if (updatedFieldValue.isPresent()) { + HashMap toValue = (HashMap) updatedFieldValue.get().get(TO_VALUE_CONST); + List fieldNames = toValue.keySet().stream().collect(Collectors.toList()); + String approvalUrl = configuration.getDomainHost() + configuration.getMdoBaseUrl().replace("{id}", wfRequest.getApplicationId()); + params.put(Constants.LINK, approvalUrl); + params.put(Constants.FIELDS, fieldNames); + params.put(Constants.SUPPORT_EMAIL, configuration.getSenderMail()); + } + String constructedEmailTemplate = constructEmailTemplate(configuration.getMdoEmailTemplate(), params); + if (StringUtils.isNotEmpty(constructedEmailTemplate)) { + template.setData(constructedEmailTemplate); + } + template.setParams(params); + Config config = new Config(); + config.setSubject(MDO_MAIL_SUBJECT); + config.setSender(configuration.getSenderMail()); + Map req = new HashMap<>(); + request.setTemplate(template); + request.setConfig(config); + Map> notificationMap = new HashMap<>(); + notificationMap.put("notifications", Arrays.asList(request)); + req.put("request", notificationMap); + sendNotification(req); + } + } + } - private String constructEmailTemplate(String templateName, Map params) { - String replacedHTML = new String(); - try { - EmailTemplateEntity templateEntity = emailTemplateRepo.findByName(templateName); - String htmlTemplate = templateEntity.getTemplate(); - VelocityEngine velocityEngine = new VelocityEngine(); - velocityEngine.init(); - VelocityContext context = new VelocityContext(); - for (Map.Entry entry : params.entrySet()) { - context.put(entry.getKey(), entry.getValue()); - } - StringWriter writer = new StringWriter(); - velocityEngine.evaluate(context, writer, "HTMLTemplate", htmlTemplate); - replacedHTML = writer.toString(); - } catch (Exception e) { - logger.error("Unable to create template "+e); - } - return replacedHTML; - } + private String constructEmailTemplate(String templateName, Map params) { + String replacedHTML = new String(); + try { + EmailTemplateEntity templateEntity = emailTemplateRepo.findByName(templateName); + String htmlTemplate = templateEntity.getTemplate(); + VelocityEngine velocityEngine = new VelocityEngine(); + velocityEngine.init(); + VelocityContext context = new VelocityContext(); + for (Map.Entry entry : params.entrySet()) { + context.put(entry.getKey(), entry.getValue()); + } + StringWriter writer = new StringWriter(); + velocityEngine.evaluate(context, writer, "HTMLTemplate", htmlTemplate); + replacedHTML = writer.toString(); + } catch (Exception e) { + logger.error("Unable to create template " + e); + } + return replacedHTML; + } - /** - * Post to the Notification service - * @param request - */ - public void sendNotification(Map request) { - StringBuilder builder = new StringBuilder(); - builder.append(configuration.getNotifyServiceHost()).append(configuration.getNotifyServicePath()); - try { - requestService.fetchResultUsingPost(builder, request, Map.class, null); - } catch (Exception e) { - logger.error("Exception while posting the data in notification service: ", e); - } + /** + * Post to the Notification service + * + * @param request + */ + public void sendNotification(Map request) { + StringBuilder builder = new StringBuilder(); + builder.append(configuration.getNotifyServiceHost()).append(configuration.getNotifyServicePath()); + try { + requestService.fetchResultUsingPost(builder, request, Map.class, null); + } catch (Exception e) { + logger.error("Exception while posting the data in notification service: ", e); + } - } + } } From ad36a44d4e6ab6ffff60e473a21b8e019496129f Mon Sep 17 00:00:00 2001 From: Radheshhathwar Date: Thu, 10 Aug 2023 18:12:31 +0530 Subject: [PATCH 2/3] Removed extra spaces while adding COMMENT_TAG into the email template body --- .../service/impl/NotificationServiceImpl.java | 425 +++++++++--------- 1 file changed, 210 insertions(+), 215 deletions(-) diff --git a/src/main/java/org/sunbird/workflow/service/impl/NotificationServiceImpl.java b/src/main/java/org/sunbird/workflow/service/impl/NotificationServiceImpl.java index 1304f004..4e6f53fe 100644 --- a/src/main/java/org/sunbird/workflow/service/impl/NotificationServiceImpl.java +++ b/src/main/java/org/sunbird/workflow/service/impl/NotificationServiceImpl.java @@ -32,244 +32,239 @@ @Service public class NotificationServiceImpl { - public static final String EMAILTEMPLATE = "emailtemplate"; - Logger logger = LogManager.getLogger(ApplicationProcessingConsumer.class); + public static final String EMAILTEMPLATE = "emailtemplate"; + Logger logger = LogManager.getLogger(ApplicationProcessingConsumer.class); - @Autowired - private WfStatusRepo wfStatusRepo; + @Autowired + private WfStatusRepo wfStatusRepo; - @Autowired - private Configuration configuration; + @Autowired + private Configuration configuration; - @Autowired - private RequestServiceImpl requestService; + @Autowired + private RequestServiceImpl requestService; - @Autowired - private ObjectMapper mapper; + @Autowired + private ObjectMapper mapper; - @Autowired - private Workflowservice workflowservice; + @Autowired + private Workflowservice workflowservice; - @Autowired - private UserProfileWfServiceImpl userProfileWfService; + @Autowired + private UserProfileWfServiceImpl userProfileWfService; - @Autowired - private EmailTemplateRepo emailTemplateRepo; + @Autowired + private EmailTemplateRepo emailTemplateRepo; - private static final String WORK_FLOW_EVENT_NAME = "workflow_service_notification"; + private static final String WORK_FLOW_EVENT_NAME = "workflow_service_notification"; - private static final String USER_NAME_CONSTANT = "user"; + private static final String USER_NAME_CONSTANT = "user"; - private static final String USER_NAME_TAG = "#userName"; + private static final String USER_NAME_TAG = "#userName"; - private static final String STATE_NAME_TAG = "#state"; + private static final String STATE_NAME_TAG = "#state"; - private static final String FIELD_KEY_TAG = "#fieldKey"; + private static final String FIELD_KEY_TAG = "#fieldKey"; - private static final String COMMENT_TAG = "#comment"; + private static final String TO_VALUE_TAG = "#toValue"; - private static final String TO_VALUE_TAG = "#toValue"; + private static final String COMMENT_TAG = "#comment"; - private static final String TO_VALUE_CONST = "toValue"; + private static final String TO_VALUE_CONST = "toValue"; - private static final String MAIL_SUBJECT = "Your request is #state"; - private static final String MDO_MAIL_SUBJECT = "Request for approval"; + private static final String MAIL_SUBJECT = "Your request is #state"; + private static final String MDO_MAIL_SUBJECT = "Request for approval"; - private static final String MAIL_BODY = "Your request to update #fieldKey to #toValue is #state, due to #comment"; + private static final String MAIL_BODY = "Your request to update #fieldKey to #toValue is #state."; - /** - * Send notification to the user based on state of application - * - * @param wfRequest workflow request - */ - public void sendNotification(WfRequest wfRequest) { - WfStatusEntity wfStatusEntity = wfStatusRepo.findByApplicationIdAndWfId(wfRequest.getApplicationId(), - wfRequest.getWfId()); - WfStatus wfStatus = workflowservice.getWorkflowStates(wfStatusEntity.getRootOrg(), wfStatusEntity.getOrg(), - wfStatusEntity.getServiceName(), wfStatusEntity.getCurrentStatus()); - try { - logger.info("Notification workflow status entity, {}", mapper.writeValueAsString(wfStatusEntity)); - logger.info("Notification workflow status model, {}", mapper.writeValueAsString(wfStatus)); - } catch (JsonProcessingException e) { - e.printStackTrace(); - } - if (!ObjectUtils.isEmpty(wfStatus.getNotificationEnable()) && wfStatus.getNotificationEnable()) { - logger.info("Enter's in the notification block"); + /** + * Send notification to the user based on state of application + * + * @param wfRequest workflow request + */ + public void sendNotification(WfRequest wfRequest) { + WfStatusEntity wfStatusEntity = wfStatusRepo.findByApplicationIdAndWfId(wfRequest.getApplicationId(), + wfRequest.getWfId()); + WfStatus wfStatus = workflowservice.getWorkflowStates(wfStatusEntity.getRootOrg(), wfStatusEntity.getOrg(), + wfStatusEntity.getServiceName(), wfStatusEntity.getCurrentStatus()); + try { + logger.info("Notification workflow status entity, {}", mapper.writeValueAsString(wfStatusEntity)); + logger.info("Notification workflow status model, {}", mapper.writeValueAsString(wfStatus)); + } catch (JsonProcessingException e) { + e.printStackTrace(); + } + if (!ObjectUtils.isEmpty(wfStatus.getNotificationEnable()) && wfStatus.getNotificationEnable()) { + logger.info("Enter's in the notification block"); Set usersId = new HashSet<>(); usersId.add(wfRequest.getActorUserId()); - usersId.add(wfStatusEntity.getApplicationId()); - HashMap usersObj = userProfileWfService.getUsersResult(usersId); - Map recipientInfo = (Map) usersObj.get(wfStatusEntity.getApplicationId()); - Map senderInfo = (Map) usersObj.get(wfRequest.getActorUserId()); - Map params = new HashMap<>(); - NotificationRequest request = new NotificationRequest(); - request.setDeliveryType("message"); - request.setIds(Arrays.asList((String) recipientInfo.get("email"))); - request.setMode("email"); - Template template = new Template(); - template.setId(EMAILTEMPLATE); - Optional> updatedFieldValue = wfRequest.getUpdateFieldValues().stream().findFirst(); - if (updatedFieldValue.isPresent()) { - HashMap toValue = (HashMap) updatedFieldValue.get().get(TO_VALUE_CONST); - params.put("body", MAIL_BODY.replace(STATE_NAME_TAG, wfStatusEntity.getCurrentStatus()) - .replace(FIELD_KEY_TAG, toValue.entrySet().iterator().next().getKey()) - .replace(COMMENT_TAG, (String) toValue.entrySet().iterator().next().getValue()) - .replace(TO_VALUE_TAG, (String) toValue.entrySet().iterator().next().getValue())); - } - params.put("orgImageUrl", null); - template.setParams(params); - Config config = new Config(); - config.setSubject(MAIL_SUBJECT.replace(STATE_NAME_TAG, wfStatusEntity.getCurrentStatus())); - config.setSender((String) senderInfo.get("email")); - Map req = new HashMap<>(); - request.setTemplate(template); - request.setConfig(config); - Map> notificationMap = new HashMap<>(); - notificationMap.put("notifications", Arrays.asList(request)); - req.put("request", notificationMap); - sendNotification(req); - } - } + usersId.add(wfStatusEntity.getApplicationId()); + HashMap usersObj = userProfileWfService.getUsersResult(usersId); + Map recipientInfo = (Map)usersObj.get(wfStatusEntity.getApplicationId()); + Map senderInfo = (Map)usersObj.get(wfRequest.getActorUserId()); + Map params = new HashMap<>(); + NotificationRequest request = new NotificationRequest(); + request.setDeliveryType("message"); + request.setIds(Arrays.asList((String)recipientInfo.get("email"))); + request.setMode("email"); + Template template = new Template(); + template.setId(EMAILTEMPLATE); + Optional> updatedFieldValue = wfRequest.getUpdateFieldValues().stream().findFirst(); + if (updatedFieldValue.isPresent()) { + HashMap toValue = (HashMap) updatedFieldValue.get().get(TO_VALUE_CONST); + params.put("body", MAIL_BODY.replace(STATE_NAME_TAG, wfStatusEntity.getCurrentStatus()).replace(FIELD_KEY_TAG, toValue.entrySet().iterator().next().getKey()) + .replace(COMMENT_TAG, (String) toValue.entrySet().iterator().next().getValue()).replace(TO_VALUE_TAG, (String) toValue.entrySet().iterator().next().getValue())); + } + params.put("orgImageUrl", null); + template.setParams(params); + Config config = new Config(); + config.setSubject(MAIL_SUBJECT.replace(STATE_NAME_TAG, wfStatusEntity.getCurrentStatus())); + config.setSender((String)senderInfo.get("email")); + Map req = new HashMap<>(); + request.setTemplate(template); + request.setConfig(config); + Map> notificationMap = new HashMap<>(); + notificationMap.put("notifications", Arrays.asList(request)); + req.put("request", notificationMap); + sendNotification(req); + } + } - public void sendEmailNotification(WfRequest wfRequest) { - WfStatusEntity wfStatusEntity = wfStatusRepo.findByApplicationIdAndWfId(wfRequest.getApplicationId(), - wfRequest.getWfId()); - WfStatus wfStatus = workflowservice.getWorkflowStates(wfStatusEntity.getRootOrg(), wfStatusEntity.getOrg(), - wfStatusEntity.getServiceName(), wfStatusEntity.getCurrentStatus()); - try { - logger.info("Notification workflow status entity, {}", mapper.writeValueAsString(wfStatusEntity)); - logger.info("Notification workflow status model, {}", mapper.writeValueAsString(wfStatus)); - } catch (JsonProcessingException e) { - e.printStackTrace(); - } - if (!ObjectUtils.isEmpty(wfStatus.getNotificationEnable()) && wfStatus.getNotificationEnable()) { - logger.info("Enters in the email notification block"); - List> updatedFieldValues = wfRequest.getUpdateFieldValues(); - Set emailSet = new HashSet<>(); - // Find the email address from the updated field values - for (Map fieldValue : updatedFieldValues) { - if (fieldValue.containsKey("email")) { - String email = (String) fieldValue.get("email"); - emailSet.add(email); - break; - } - } - if (!emailSet.isEmpty()) { - HashMap params = new HashMap<>(); - NotificationRequest request = new NotificationRequest(); - request.setDeliveryType("message"); - request.setIds(new ArrayList<>(emailSet)); - request.setMode("email"); - Template template = new Template(); - template.setId(EMAILTEMPLATE); - Optional> updatedFieldValue = wfRequest.getUpdateFieldValues().stream().findFirst(); - if (updatedFieldValue.isPresent()) { - HashMap toValue = (HashMap) updatedFieldValue.get().get(TO_VALUE_CONST); - params.put("body", MAIL_BODY.replace(STATE_NAME_TAG, wfStatusEntity.getCurrentStatus()) - .replace(FIELD_KEY_TAG, toValue.entrySet().iterator().next().getKey()) - .replace(COMMENT_TAG, (String) toValue.entrySet().iterator().next().getValue()) - .replace(TO_VALUE_TAG, (String) toValue.entrySet().iterator().next().getValue())); - } - params.put("orgImageUrl", null); - template.setParams(params); - Config config = new Config(); - config.setSubject(MAIL_SUBJECT.replace(STATE_NAME_TAG, wfStatusEntity.getCurrentStatus())); - config.setSender(configuration.getSenderMail()); - Map req = new HashMap<>(); - request.setTemplate(template); - request.setConfig(config); - Map> notificationMap = new HashMap<>(); - notificationMap.put("notifications", Arrays.asList(request)); - req.put("request", notificationMap); - sendNotification(req); - } else { - logger.warn("Email address not found in the update field values."); - } - } - } + public void sendEmailNotification(WfRequest wfRequest) { + WfStatusEntity wfStatusEntity = wfStatusRepo.findByApplicationIdAndWfId(wfRequest.getApplicationId(), + wfRequest.getWfId()); + WfStatus wfStatus = workflowservice.getWorkflowStates(wfStatusEntity.getRootOrg(), wfStatusEntity.getOrg(), + wfStatusEntity.getServiceName(), wfStatusEntity.getCurrentStatus()); + try { + logger.info("Notification workflow status entity, {}", mapper.writeValueAsString(wfStatusEntity)); + logger.info("Notification workflow status model, {}", mapper.writeValueAsString(wfStatus)); + } catch (JsonProcessingException e) { + e.printStackTrace(); + } + if (!ObjectUtils.isEmpty(wfStatus.getNotificationEnable()) && wfStatus.getNotificationEnable()) { + logger.info("Enters in the email notification block"); + List> updatedFieldValues = wfRequest.getUpdateFieldValues(); + Set emailSet = new HashSet<>(); + // Find the email address from the updated field values + for (Map fieldValue : updatedFieldValues) { + if (fieldValue.containsKey("email")) { + String email = (String) fieldValue.get("email"); + emailSet.add(email); + break; + } + } + if (!emailSet.isEmpty()) { + HashMap params = new HashMap<>(); + NotificationRequest request = new NotificationRequest(); + request.setDeliveryType("message"); + request.setIds(new ArrayList<>(emailSet)); + request.setMode("email"); + Template template = new Template(); + template.setId(EMAILTEMPLATE); + Optional> updatedFieldValue = wfRequest.getUpdateFieldValues().stream().findFirst(); + if (updatedFieldValue.isPresent()) { + HashMap toValue = (HashMap) updatedFieldValue.get().get(TO_VALUE_CONST); + params.put("body", MAIL_BODY.replace(STATE_NAME_TAG, wfStatusEntity.getCurrentStatus()).replace(FIELD_KEY_TAG, toValue.entrySet().iterator().next().getKey()) + .replace(COMMENT_TAG, (String) toValue.entrySet().iterator().next().getValue()).replace(TO_VALUE_TAG, (String) toValue.entrySet().iterator().next().getValue())); + } + params.put("orgImageUrl", null); + template.setParams(params); + Config config = new Config(); + config.setSubject(MAIL_SUBJECT.replace(STATE_NAME_TAG, wfStatusEntity.getCurrentStatus())); + config.setSender(configuration.getSenderMail()); + Map req = new HashMap<>(); + request.setTemplate(template); + request.setConfig(config); + Map> notificationMap = new HashMap<>(); + notificationMap.put("notifications", Arrays.asList(request)); + req.put("request", notificationMap); + sendNotification(req); + } else { + logger.warn("Email address not found in the update field values."); + } + } + } - public void sendNotificationToMdoAdmin(WfRequest wfRequest) { - WfStatusEntity wfStatusEntity = wfStatusRepo.findByApplicationIdAndWfId(wfRequest.getApplicationId(), - wfRequest.getWfId()); - WfStatus wfStatus = workflowservice.getWorkflowStates(wfStatusEntity.getRootOrg(), wfStatusEntity.getOrg(), - wfStatusEntity.getServiceName(), wfStatusEntity.getCurrentStatus()); - if (!ObjectUtils.isEmpty(wfStatus.getNotificationEnable()) && wfStatus.getNotificationEnable() - && !Arrays.asList(Constants.REJECTED, Constants.APPROVED).contains(wfStatus.getState())) { - logger.info("Enter in the notification block"); - List mdoAdminList = userProfileWfService.getMdoAdminDetails(wfRequest.getRootOrgId()); - Map params = new HashMap<>(); - NotificationRequest request = new NotificationRequest(); - request.setDeliveryType("message"); - List mdoMailList = mdoAdminList.stream().collect(Collectors.toList()); - if (!CollectionUtils.isEmpty(mdoMailList)) { - request.setIds(mdoMailList); - request.setMode("email"); - Template template = new Template(); - template.setId(configuration.getMdoEmailTemplate()); - HashMap usersObj = userProfileWfService.getUsersResult(Collections.singleton(wfRequest.getUserId())); - Map recipientInfo = (Map) usersObj.get(wfStatusEntity.getUserId()); - params.put(Constants.USER_NAME, recipientInfo.get(Constants.FIRST_NAME)); - Optional> updatedFieldValue = wfRequest.getUpdateFieldValues().stream().findFirst(); - if (updatedFieldValue.isPresent()) { - HashMap toValue = (HashMap) updatedFieldValue.get().get(TO_VALUE_CONST); - List fieldNames = toValue.keySet().stream().collect(Collectors.toList()); - String approvalUrl = configuration.getDomainHost() + configuration.getMdoBaseUrl().replace("{id}", wfRequest.getApplicationId()); - params.put(Constants.LINK, approvalUrl); - params.put(Constants.FIELDS, fieldNames); - params.put(Constants.SUPPORT_EMAIL, configuration.getSenderMail()); - } - String constructedEmailTemplate = constructEmailTemplate(configuration.getMdoEmailTemplate(), params); - if (StringUtils.isNotEmpty(constructedEmailTemplate)) { - template.setData(constructedEmailTemplate); - } - template.setParams(params); - Config config = new Config(); - config.setSubject(MDO_MAIL_SUBJECT); - config.setSender(configuration.getSenderMail()); - Map req = new HashMap<>(); - request.setTemplate(template); - request.setConfig(config); - Map> notificationMap = new HashMap<>(); - notificationMap.put("notifications", Arrays.asList(request)); - req.put("request", notificationMap); - sendNotification(req); - } - } - } + public void sendNotificationToMdoAdmin(WfRequest wfRequest) { + WfStatusEntity wfStatusEntity = wfStatusRepo.findByApplicationIdAndWfId(wfRequest.getApplicationId(), + wfRequest.getWfId()); + WfStatus wfStatus = workflowservice.getWorkflowStates(wfStatusEntity.getRootOrg(), wfStatusEntity.getOrg(), + wfStatusEntity.getServiceName(), wfStatusEntity.getCurrentStatus()); + if (!ObjectUtils.isEmpty(wfStatus.getNotificationEnable()) && wfStatus.getNotificationEnable() + && !Arrays.asList(Constants.REJECTED, Constants.APPROVED).contains(wfStatus.getState())) { + logger.info("Enter in the notification block"); + List mdoAdminList = userProfileWfService.getMdoAdminDetails(wfRequest.getRootOrgId()); + Map params = new HashMap<>(); + NotificationRequest request = new NotificationRequest(); + request.setDeliveryType("message"); + List mdoMailList = mdoAdminList.stream().collect(Collectors.toList()); + if (!CollectionUtils.isEmpty(mdoMailList)) { + request.setIds(mdoMailList); + request.setMode("email"); + Template template = new Template(); + template.setId(configuration.getMdoEmailTemplate()); + HashMap usersObj = userProfileWfService.getUsersResult(Collections.singleton(wfRequest.getUserId())); + Map recipientInfo = (Map) usersObj.get(wfStatusEntity.getUserId()); + params.put(Constants.USER_NAME, recipientInfo.get(Constants.FIRST_NAME)); + Optional> updatedFieldValue = wfRequest.getUpdateFieldValues().stream().findFirst(); + if (updatedFieldValue.isPresent()) { + HashMap toValue = (HashMap) updatedFieldValue.get().get(TO_VALUE_CONST); + List fieldNames = toValue.keySet().stream().collect(Collectors.toList()); + String approvalUrl = configuration.getDomainHost() + configuration.getMdoBaseUrl().replace("{id}", wfRequest.getApplicationId()); + params.put(Constants.LINK, approvalUrl); + params.put(Constants.FIELDS, fieldNames); + params.put(Constants.SUPPORT_EMAIL, configuration.getSenderMail()); + } + String constructedEmailTemplate = constructEmailTemplate(configuration.getMdoEmailTemplate(), params); + if (StringUtils.isNotEmpty(constructedEmailTemplate)) { + template.setData(constructedEmailTemplate); + } + template.setParams(params); + Config config = new Config(); + config.setSubject(MDO_MAIL_SUBJECT); + config.setSender(configuration.getSenderMail()); + Map req = new HashMap<>(); + request.setTemplate(template); + request.setConfig(config); + Map> notificationMap = new HashMap<>(); + notificationMap.put("notifications", Arrays.asList(request)); + req.put("request", notificationMap); + sendNotification(req); + } + } + } - private String constructEmailTemplate(String templateName, Map params) { - String replacedHTML = new String(); - try { - EmailTemplateEntity templateEntity = emailTemplateRepo.findByName(templateName); - String htmlTemplate = templateEntity.getTemplate(); - VelocityEngine velocityEngine = new VelocityEngine(); - velocityEngine.init(); - VelocityContext context = new VelocityContext(); - for (Map.Entry entry : params.entrySet()) { - context.put(entry.getKey(), entry.getValue()); - } - StringWriter writer = new StringWriter(); - velocityEngine.evaluate(context, writer, "HTMLTemplate", htmlTemplate); - replacedHTML = writer.toString(); - } catch (Exception e) { - logger.error("Unable to create template " + e); - } - return replacedHTML; - } + private String constructEmailTemplate(String templateName, Map params) { + String replacedHTML = new String(); + try { + EmailTemplateEntity templateEntity = emailTemplateRepo.findByName(templateName); + String htmlTemplate = templateEntity.getTemplate(); + VelocityEngine velocityEngine = new VelocityEngine(); + velocityEngine.init(); + VelocityContext context = new VelocityContext(); + for (Map.Entry entry : params.entrySet()) { + context.put(entry.getKey(), entry.getValue()); + } + StringWriter writer = new StringWriter(); + velocityEngine.evaluate(context, writer, "HTMLTemplate", htmlTemplate); + replacedHTML = writer.toString(); + } catch (Exception e) { + logger.error("Unable to create template "+e); + } + return replacedHTML; + } - /** - * Post to the Notification service - * - * @param request - */ - public void sendNotification(Map request) { - StringBuilder builder = new StringBuilder(); - builder.append(configuration.getNotifyServiceHost()).append(configuration.getNotifyServicePath()); - try { - requestService.fetchResultUsingPost(builder, request, Map.class, null); - } catch (Exception e) { - logger.error("Exception while posting the data in notification service: ", e); - } + /** + * Post to the Notification service + * @param request + */ + public void sendNotification(Map request) { + StringBuilder builder = new StringBuilder(); + builder.append(configuration.getNotifyServiceHost()).append(configuration.getNotifyServicePath()); + try { + requestService.fetchResultUsingPost(builder, request, Map.class, null); + } catch (Exception e) { + logger.error("Exception while posting the data in notification service: ", e); + } - } + } } From dcaae3f6b2d5eaf97947c5daa32f6ebd05a84224 Mon Sep 17 00:00:00 2001 From: "Radhesh.B.H" <44744695+Radheshhathwar@users.noreply.github.com> Date: Thu, 10 Aug 2023 18:47:41 +0530 Subject: [PATCH 3/3] Added comment --- .../sunbird/workflow/service/impl/NotificationServiceImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/sunbird/workflow/service/impl/NotificationServiceImpl.java b/src/main/java/org/sunbird/workflow/service/impl/NotificationServiceImpl.java index 4e6f53fe..501ec3f5 100644 --- a/src/main/java/org/sunbird/workflow/service/impl/NotificationServiceImpl.java +++ b/src/main/java/org/sunbird/workflow/service/impl/NotificationServiceImpl.java @@ -75,7 +75,7 @@ public class NotificationServiceImpl { private static final String MAIL_SUBJECT = "Your request is #state"; private static final String MDO_MAIL_SUBJECT = "Request for approval"; - private static final String MAIL_BODY = "Your request to update #fieldKey to #toValue is #state."; + private static final String MAIL_BODY = "Your request to update #fieldKey to #toValue is #state, due to #comment"; /** * Send notification to the user based on state of application