From a458c71789ce2ac1afc3cfc43e5f1c91cff3e137 Mon Sep 17 00:00:00 2001 From: manoj Date: Fri, 20 Mar 2015 19:50:07 +0530 Subject: [PATCH] Email format --- .../DeviceManagementServiceProviderImpl.java | 45 ++++++++++++++++--- .../core/config/EnrolmentNotifications.java | 42 ++++++++++++++--- .../core/service/DeviceManagementService.java | 2 +- .../service/DeviceManagementServiceImpl.java | 7 +-- .../resources/conf/notification-messages.xml | 11 ++++- 5 files changed, 89 insertions(+), 18 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java index 3b8a335691a..9e12d09cfe2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java @@ -17,6 +17,8 @@ */ package org.wso2.carbon.device.mgt.core; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.*; import org.wso2.carbon.device.mgt.common.license.mgt.License; import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException; @@ -40,6 +42,8 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl; import org.wso2.carbon.device.mgt.core.service.DeviceManagementService; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; +import java.io.IOException; +import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; @@ -51,6 +55,8 @@ public class DeviceManagementServiceProviderImpl implements DeviceManagementServ private OperationManager operationManager; private LicenseManager licenseManager; + private static Log log = LogFactory.getLog(DeviceManagementServiceProviderImpl.class); + public DeviceManagementServiceProviderImpl(DeviceManagementRepository pluginRepository) { this.pluginRepository = pluginRepository; this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); @@ -216,16 +222,45 @@ public class DeviceManagementServiceProviderImpl implements DeviceManagementServ } @Override - public void sendEnrollInvitation(String emailAddress) throws DeviceManagementException { + public void sendEnrollInvitation(String title, String userName, String emailAddress, String enrolUrl) throws + DeviceManagementException { + EmailMessageProperties emailMessageProperties = new EmailMessageProperties(); EnrolmentNotifications enrolmentNotifications = DeviceConfigurationManager.getInstance() - .getNotificationMessagesConfig() - .getEnrolmentNotifications(); + .getNotificationMessagesConfig().getEnrolmentNotifications(); - emailMessageProperties.setMailTo(new String[]{emailAddress}); + emailMessageProperties.setMailTo(new String[] { emailAddress }); emailMessageProperties.setSubject(enrolmentNotifications.getSubject()); - emailMessageProperties.setMessageBody(enrolmentNotifications.getMessage()); + + String messageHeader = enrolmentNotifications.getHeader(); + String messageBody = enrolmentNotifications.getBody(); + String messageFooter = enrolmentNotifications.getFooter(); + + StringBuilder messageBuilder = new StringBuilder(); + + try { + messageHeader = messageHeader.replaceAll("\\{title\\}", + URLEncoder.encode(title, "UTF-8")); + + messageHeader = messageHeader.replaceAll("\\{user-name\\}", + URLEncoder.encode(userName, "UTF-8")); + + messageBody = messageBody+ System.getProperty("line.separator") + enrolmentNotifications.getUrl() + .replaceAll("\\{downloadUrl\\}", URLEncoder.encode(enrolUrl, "UTF-8")); + + messageBuilder.append(messageHeader).append(System.getProperty("line.separator")) + .append(System.getProperty("line.separator")); + + messageBuilder.append(messageBody).append(System.getProperty("line.separator")) + .append(System.getProperty("line.separator")).append(messageFooter); + } catch (IOException ioEx) { + String errorMsg = "Error replacing tags in email template" + title; + log.error(errorMsg, ioEx); + throw new DeviceManagementException(errorMsg, ioEx); + } + emailMessageProperties.setMessageBody(messageBuilder.toString()); EmailServiceDataHolder.getInstance().getEmailServiceProvider().sendEmail(emailMessageProperties); + } @Override diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/EnrolmentNotifications.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/EnrolmentNotifications.java index 70244ecd5da..9eb832690db 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/EnrolmentNotifications.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/EnrolmentNotifications.java @@ -24,19 +24,39 @@ import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "EnrolmentNotifications") public class EnrolmentNotifications { - private String message; + private String header; + private String body; + private String footer; private String subject; + private String url; - @XmlElement(name = "message", required = true) - public String getMessage() { - return message; + @XmlElement(name = "Header", required = true) + public String getHeader() { + return header; } - public void setMessage(String message) { - this.message = message; + public void setHeader(String header) { + this.header = header; } - @XmlElement(name = "subject", required = true) + @XmlElement(name = "Body", required = true) + public String getBody() { + return body; + } + + public void setBody(String body) { + this.body = body; + } + + @XmlElement(name = "Footer", required = true) + public String getFooter() { + return footer; + } + + public void setFooter(String footer) { + this.footer = footer; + } + @XmlElement(name = "Subject", required = true) public String getSubject() { return subject; } @@ -44,4 +64,12 @@ public class EnrolmentNotifications { public void setSubject(String subject) { this.subject = subject; } + @XmlElement(name = "Url", required = true) + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementService.java index efbb9593233..1adea0a12ae 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementService.java @@ -38,6 +38,6 @@ public interface DeviceManagementService extends DeviceManager, LicenseManager, List getDeviceListOfUser(String username) throws DeviceManagementException; - void sendEnrollInvitation(String emailAddress) throws DeviceManagementException; + void sendEnrollInvitation(String title, String userName, String emailAddress, String enrolUrl) throws DeviceManagementException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java index 48b6853b524..b2c50136415 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java @@ -26,7 +26,6 @@ import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; -import org.wso2.carbon.device.mgt.core.internal.EmailServiceDataHolder; import java.util.List; @@ -87,8 +86,10 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { } @Override - public void sendEnrollInvitation(String emailAddress) throws DeviceManagementException { - DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().sendEnrollInvitation(emailAddress); + public void sendEnrollInvitation(String title, String userName, String emailAddress, String enrolUrl) + throws DeviceManagementException { + DeviceManagementDataHolder.getInstance().getDeviceManagementProvider() + .sendEnrollInvitation(title, userName, emailAddress, enrolUrl); } @Override diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/notification-messages.xml b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/notification-messages.xml index 922e115333a..807329de5ac 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/notification-messages.xml +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/notification-messages.xml @@ -20,7 +20,14 @@ - Please enroll your device - Enroll your device +
Dear {title} {user-name},
+ Please go to the following url and enrol your device + {downloadUrl} +
+ Best Regards, + WSO2 Carbon Team + http://www.wso2.com +
+ Enrol your device
\ No newline at end of file