From 7c0ca194a11aff26d7e54de1b4e0c0d35485a32a Mon Sep 17 00:00:00 2001 From: manoj Date: Fri, 20 Mar 2015 16:16:57 +0530 Subject: [PATCH] Email functionality --- .../mgt/common/DeviceManagementConstants.java | 6 ++ .../DeviceManagementServiceProviderImpl.java | 35 +++++--- .../config/DeviceConfigurationManager.java | 85 ++++++++++++------- .../src/main/resources/p2.inf | 1 + 4 files changed, 83 insertions(+), 44 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManagementConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManagementConstants.java index 23ff9d0c3a..6bd72a0e82 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManagementConstants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManagementConstants.java @@ -71,4 +71,10 @@ public final class DeviceManagementConstants { public static final String LICENSE_REGISTRY_KEY = "license"; } + public static final class NotificationProperties { + private NotificationProperties() { + throw new AssertionError(); + } + public static final String NOTIFICATION_CONFIG_FILE = "notification-messages.xml"; + } } 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 4b00db5f87..3b8a335691 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 @@ -18,22 +18,24 @@ package org.wso2.carbon.device.mgt.core; import org.wso2.carbon.device.mgt.common.*; -import org.wso2.carbon.device.mgt.common.Device; -import org.wso2.carbon.device.mgt.common.spi.DeviceManager; import org.wso2.carbon.device.mgt.common.license.mgt.License; +import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException; +import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManager; +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.common.operation.mgt.OperationManager; +import org.wso2.carbon.device.mgt.common.spi.DeviceManager; +import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; +import org.wso2.carbon.device.mgt.core.config.EnrolmentNotifications; import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; -import org.wso2.carbon.device.mgt.core.dto.*; -import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException; -import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManager; +import org.wso2.carbon.device.mgt.core.dto.DeviceType; +import org.wso2.carbon.device.mgt.core.dto.Status; import org.wso2.carbon.device.mgt.core.internal.EmailServiceDataHolder; import org.wso2.carbon.device.mgt.core.license.mgt.LicenseManagerImpl; -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.common.operation.mgt.OperationManager; 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; @@ -188,7 +190,8 @@ public class DeviceManagementServiceProviderImpl implements DeviceManagementServ List devicesOfUser = new ArrayList(); try { int tenantId = DeviceManagerUtil.getTenantId(); - List devicesList = this.deviceDAO.getDeviceListOfUser(username, tenantId); + List devicesList = this.deviceDAO + .getDeviceListOfUser(username, tenantId); for (int x = 0; x < devicesList.size(); x++) { org.wso2.carbon.device.mgt.core.dto.Device device = devicesList.get(x); device.setDeviceType(deviceTypeDAO.getDeviceType(device.getDeviceTypeId())); @@ -207,7 +210,7 @@ public class DeviceManagementServiceProviderImpl implements DeviceManagementServ } } catch (DeviceManagementDAOException e) { throw new DeviceManagementException("Error occurred while obtaining devices for user " + - "'" + username + "'", e); + "'" + username + "'", e); } return devicesOfUser; } @@ -215,8 +218,14 @@ public class DeviceManagementServiceProviderImpl implements DeviceManagementServ @Override public void sendEnrollInvitation(String emailAddress) throws DeviceManagementException { EmailMessageProperties emailMessageProperties = new EmailMessageProperties(); - emailMessageProperties.setSubject(""); - // EmailServiceDataHolder.getInstance().getEmailServiceProvider().sendEmail(); + EnrolmentNotifications enrolmentNotifications = DeviceConfigurationManager.getInstance() + .getNotificationMessagesConfig() + .getEnrolmentNotifications(); + + emailMessageProperties.setMailTo(new String[]{emailAddress}); + emailMessageProperties.setSubject(enrolmentNotifications.getSubject()); + emailMessageProperties.setMessageBody(enrolmentNotifications.getMessage()); + EmailServiceDataHolder.getInstance().getEmailServiceProvider().sendEmail(emailMessageProperties); } @Override @@ -288,7 +297,7 @@ public class DeviceManagementServiceProviderImpl implements DeviceManagementServ @Override public boolean addOperation(Operation operation, - List devices) throws OperationManagementException { + List devices) throws OperationManagementException { return operationManager.addOperation(operation, devices); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceConfigurationManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceConfigurationManager.java index fa1ed6d72c..0348f8a7f8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceConfigurationManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceConfigurationManager.java @@ -25,6 +25,7 @@ import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import org.wso2.carbon.utils.CarbonUtils; import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import java.io.File; @@ -33,42 +34,64 @@ import java.io.File; */ public class DeviceConfigurationManager { - private DeviceManagementConfig currentDeviceConfig; - private static DeviceConfigurationManager deviceConfigManager; + private DeviceManagementConfig currentDeviceConfig; + private NotificationMessagesConfig notificationMessagesConfig; + private static DeviceConfigurationManager deviceConfigManager; - private static final String deviceMgtConfigXMLPath = - CarbonUtils.getCarbonConfigDirPath() + File.separator + - DeviceManagementConstants.DataSourceProperties.DEVICE_CONFIG_XML_NAME; + private static final String deviceMgtConfigXMLPath = + CarbonUtils.getCarbonConfigDirPath() + File.separator + + DeviceManagementConstants.DataSourceProperties.DEVICE_CONFIG_XML_NAME; + private static final String notificationMessagesConfigXMLPath = + CarbonUtils.getCarbonConfigDirPath() + File.separator + + DeviceManagementConstants.NotificationProperties.NOTIFICATION_CONFIG_FILE; - public static DeviceConfigurationManager getInstance() { - if (deviceConfigManager == null) { - synchronized (DeviceConfigurationManager.class) { - if (deviceConfigManager == null) { - deviceConfigManager = new DeviceConfigurationManager(); - } - } - } - return deviceConfigManager; - } + public static DeviceConfigurationManager getInstance() { + if (deviceConfigManager == null) { + synchronized (DeviceConfigurationManager.class) { + if (deviceConfigManager == null) { + deviceConfigManager = new DeviceConfigurationManager(); + } + } + } + return deviceConfigManager; + } - public synchronized void initConfig() throws DeviceManagementException { + public synchronized void initConfig() throws DeviceManagementException { - //catch generic exception.if any exception occurs wrap and throw DeviceManagementException - try { - File deviceMgtConfig = new File(deviceMgtConfigXMLPath); - Document doc = DeviceManagerUtil.convertToDocument(deviceMgtConfig); + //catch generic exception.if any exception occurs wrap and throw DeviceManagementException + try { + File deviceMgtConfig = new File(deviceMgtConfigXMLPath); + Document doc = DeviceManagerUtil.convertToDocument(deviceMgtConfig); /* Un-marshaling Device Management configuration */ - JAXBContext cdmContext = JAXBContext.newInstance(DeviceManagementConfig.class); - Unmarshaller unmarshaller = cdmContext.createUnmarshaller(); - this.currentDeviceConfig = (DeviceManagementConfig) unmarshaller.unmarshal(doc); - } catch (Exception e) { - throw new DeviceManagementException("Error occurred while initializing RSS config", e); - } - } - - public DeviceManagementConfig getDeviceManagementConfig() { - return currentDeviceConfig; - } + JAXBContext cdmContext = JAXBContext.newInstance(DeviceManagementConfig.class); + Unmarshaller unmarshaller = cdmContext.createUnmarshaller(); + this.currentDeviceConfig = (DeviceManagementConfig) unmarshaller.unmarshal(doc); + } catch (JAXBException jaxbEx) { + throw new DeviceManagementException("Error occurred while initializing Data Source config", jaxbEx); + } + + try { + File notificationConfig = new File(notificationMessagesConfigXMLPath); + Document doc = DeviceManagerUtil.convertToDocument(notificationConfig); + + /* Un-marshaling Notifications Management configuration */ + JAXBContext notificationContext = JAXBContext.newInstance(NotificationMessagesConfig.class); + Unmarshaller unmarshaller = notificationContext.createUnmarshaller(); + this.notificationMessagesConfig = (NotificationMessagesConfig) unmarshaller.unmarshal(doc); + }catch(JAXBException jaxbEx){ + throw new DeviceManagementException("Error occurred while initializing Notification settings config", + jaxbEx); + } + + } + + public DeviceManagementConfig getDeviceManagementConfig() { + return currentDeviceConfig; + } + + public NotificationMessagesConfig getNotificationMessagesConfig() { + return notificationMessagesConfig; + } } diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/p2.inf b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/p2.inf index c00882bffa..51eb8e435a 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/p2.inf +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/p2.inf @@ -1,5 +1,6 @@ instructions.configure = \ org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.server_${feature.version}/conf/cdm-config.xml,target:${installFolder}/../../conf/cdm-config.xml,overwrite:true);\ +org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.server_${feature.version}/conf/notification-messages.xml,target:${installFolder}/../../conf/notification-messages.xml,overwrite:true);\ org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.server_${feature.version}/conf/license-config.xml,target:${installFolder}/../../conf/etc/license-config.xml,overwrite:true);\ org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.server_${feature.version}/dbscripts/cdm,target:${installFolder}/../../../dbscripts/cdm,overwrite:true);\ org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.server_${feature.version}/rxts/license.rxt,target:${installFolder}/../../../repository/resources/rxts/license.rxt,overwrite:true);\ \ No newline at end of file