From 8a343fc6b1664671ed4dedd66cea818d3a68b7d6 Mon Sep 17 00:00:00 2001 From: amalhub Date: Wed, 4 Oct 2017 15:49:37 +0530 Subject: [PATCH] App-mgt store device validation for install --- .../impl/SubscriptionManagementAPIImpl.java | 8 +++---- .../core/impl/SubscriptionManagerImpl.java | 21 ++++++------------- 2 files changed, 10 insertions(+), 19 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/SubscriptionManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/SubscriptionManagementAPIImpl.java index cf0de355b5..20effa969a 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/SubscriptionManagementAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/SubscriptionManagementAPIImpl.java @@ -52,17 +52,17 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{ Object result; SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager(); try { - String applicationUUTD = installationDetails.getApplicationUUID(); + String applicationUUID = installationDetails.getApplicationUUID(); String versionName = installationDetails.getVersionName(); if (!installationDetails.getDeviceIdentifiers().isEmpty()) { List deviceList = installationDetails.getDeviceIdentifiers(); - result = subscriptionManager.installApplicationForDevices(applicationUUTD, versionName, deviceList); + result = subscriptionManager.installApplicationForDevices(applicationUUID, versionName, deviceList); } else if (!installationDetails.getUserNameList().isEmpty()) { List userList = installationDetails.getUserNameList(); - result = subscriptionManager.installApplicationForUsers(applicationUUTD, userList); + result = subscriptionManager.installApplicationForUsers(applicationUUID, userList); } else if (!installationDetails.getRoleNameList().isEmpty()) { List roleList = installationDetails.getRoleNameList(); - result = subscriptionManager.installApplicationForRoles(applicationUUTD, roleList); + result = subscriptionManager.installApplicationForRoles(applicationUUID, roleList); } else { result = "Missing request data!"; return Response.status(Response.Status.BAD_REQUEST).entity(result).build(); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java index 6fc19346fd..3667e0953d 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java @@ -19,7 +19,6 @@ package org.wso2.carbon.device.application.mgt.core.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.application.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; import org.wso2.carbon.device.application.mgt.common.services.SubscriptionManager; @@ -41,7 +40,6 @@ import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; -import java.util.Map; /** * This is the default implementation for the Subscription Manager. @@ -63,13 +61,9 @@ public class SubscriptionManagerImpl implements SubscriptionManager { org.wso2.carbon.device.mgt.common.DeviceIdentifier deviceIdentifier = new org.wso2.carbon.device.mgt .common.DeviceIdentifier(device.getId(), device.getType()); try { - DeviceManagementDAOFactory.openConnection(); - // todo: replace this with boolean:deviceExsits(deviceId) operation - Map currentDevices = DeviceManagementDAOFactory.getDeviceDAO().getDevice(deviceIdentifier); - DeviceManagementDAOFactory.closeConnection(); - - if (currentDevices.isEmpty()) { - log.error("Device with ID: " + device.getId() + " not found to install the application."); + DeviceManagementProviderService dmpService = DataHolder.getInstance().getDeviceManagementService(); + if (!dmpService.isEnrolled(deviceIdentifier)) { + log.error("Device with ID: " + device.getId() + " is not enrolled to install the application."); } else { if (log.isDebugEnabled()) { log.debug("Installing application to : " + device.getId()); @@ -96,10 +90,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager { // DAOFactory.getSubscriptionDAO().addDeviceApplicationMapping(device.getId(), applicationUUID, false); failedDeviceList.remove(device); } - } catch (DeviceManagementException | DeviceManagementDAOException | OperationManagementException | InvalidDeviceException | SQLException e) { + } catch (DeviceManagementException | OperationManagementException | InvalidDeviceException e) { throw new ApplicationManagementException("Failed to install application " + applicationUUID + " on device " + deviceIdentifier, e); - } finally { - DeviceManagementDAOFactory.closeConnection(); } } return failedDeviceList; @@ -110,15 +102,14 @@ public class SubscriptionManagerImpl implements SubscriptionManager { throws ApplicationManagementException { log.info("Install application: " + applicationUUID + " to: " + userList.size() + " users."); List deviceList = new ArrayList<>(); - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); for (String user : userList) { try { - List devicesOfUser = DeviceManagementDAOFactory.getDeviceDAO().getDevicesOfUser(user, tenantId); + List devicesOfUser = DataHolder.getInstance().getDeviceManagementService().getDevicesOfUser(user); for (Device device : devicesOfUser) { deviceList.add(new DeviceIdentifier(device .getDeviceIdentifier(), device.getType())); } - } catch (DeviceManagementDAOException e) { + } catch (DeviceManagementException e) { log.error("Error when extracting the device list from user[" + user + "].", e); } }