From 6b51d1fd068b71a6c2f55431a3d778c72f62941f Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Fri, 4 Oct 2019 08:35:14 +0530 Subject: [PATCH] Improve app subscribing functionality --- .../mgt/common/SubscribingDeviceIdHolder.java | 21 ++--- .../mgt/core/dao/SubscriptionDAO.java | 4 +- .../GenericSubscriptionDAOImpl.java | 10 ++- .../core/impl/SubscriptionManagerImpl.java | 89 ++++++++++++------- 4 files changed, 75 insertions(+), 49 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/SubscribingDeviceIdHolder.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/SubscribingDeviceIdHolder.java index 3f2866e8f7..cba18e6733 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/SubscribingDeviceIdHolder.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/SubscribingDeviceIdHolder.java @@ -23,21 +23,22 @@ import java.util.HashMap; import java.util.Map; public class SubscribingDeviceIdHolder { - private Map subscribedDevices = new HashMap<>(); - private Map subscribableDevices = new HashMap<>(); - public Map getSubscribedDevices() { - return subscribedDevices; + private Map appInstalledDevices = new HashMap<>(); + private Map appInstallableDevices = new HashMap<>(); + + public Map getAppInstalledDevices() { + return appInstalledDevices; } - public void setSubscribedDevices(Map subscribedDevices) { - this.subscribedDevices = subscribedDevices; + public void setAppInstalledDevices(Map appInstalledDevices) { + this.appInstalledDevices = appInstalledDevices; } - public Map getSubscribableDevices() { - return subscribableDevices; + public Map getAppInstallableDevices() { + return appInstallableDevices; } - public void setSubscribableDevices(Map subscribableDevices) { - this.subscribableDevices = subscribableDevices; + public void setAppInstallableDevices(Map appInstallableDevices) { + this.appInstallableDevices = appInstallableDevices; } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java index e6ff3a3f53..4e36639d5f 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java @@ -83,8 +83,8 @@ public interface SubscriptionDAO { List getDeviceSubscriptions(int appReleaseId, int tenantId) throws ApplicationManagementDAOException; - Map getDeviceSubscriptions(List deviceIds, int tenantId) throws - ApplicationManagementDAOException; + Map getDeviceSubscriptions(List deviceIds, int appReleaseId, int tenantId) + throws ApplicationManagementDAOException; List getSubscribedUserNames(List users, int tenantId) throws ApplicationManagementDAOException; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java index 8eb297f4da..6fa065185b 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java @@ -385,7 +385,8 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } @Override - public Map getDeviceSubscriptions(List deviceIds, int tenantId) + public Map getDeviceSubscriptions(List deviceIds, int appReleaseId, + int tenantId) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { log.debug("Request received in DAO Layer to get device subscriptions for given device ids."); @@ -406,13 +407,14 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc + "DS.DM_DEVICE_ID AS DEVICE_ID, " + "DS.STATUS AS STATUS " + "FROM AP_DEVICE_SUBSCRIPTION DS " - + "WHERE DS.DM_DEVICE_ID IN (", ") AND TENANT_ID = ?"); + + "WHERE DS.DM_DEVICE_ID IN (", ") AND AP_APP_RELEASE_ID = ? AND TENANT_ID = ?"); deviceIds.stream().map(ignored -> "?").forEach(joiner::add); String query = joiner.toString(); try (PreparedStatement ps = conn.prepareStatement(query)) { for (Integer deviceId : deviceIds) { ps.setObject(index++, deviceId); } + ps.setInt(index++, appReleaseId); ps.setInt(index, tenantId); try (ResultSet rs = ps.executeQuery()) { while (rs.next()) { @@ -575,8 +577,8 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc for (Integer deviceId : deviceIds) { ps.setObject(index++, deviceId); } - ps.setInt(index++, tenantId); - ps.setInt(index, applicationReleaseId); + ps.setInt(index++, applicationReleaseId); + ps.setInt(index, tenantId); try (ResultSet rs = ps.executeQuery()) { while (rs.next()) { subscribedDevices.add(rs.getInt("DM_DEVICE_ID")); 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 7d7d18b896..49b56f63e9 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 @@ -18,9 +18,6 @@ package org.wso2.carbon.device.application.mgt.core.impl; import com.google.gson.Gson; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonParser; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.methods.PostMethod; @@ -77,7 +74,6 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus; 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.policy.mgt.ProfileFeature; import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.operation.mgt.ProfileOperation; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; @@ -346,35 +342,45 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } } + /*** + * This method perform given action (i.e APP INSTALL or APP UNINSTALL) on given set of devices. + * + * @param deviceType Application supported device type. + * @param devices List of devices that action is triggered. + * @param applicationDTO Application data + * @param subType Subscription type (i.e USER, ROLE, GROUP or DEVICE) + * @param subscribers Subscribers + * @param action Performing action. (i.e INSTALL or UNINSTALL) + * @return {@link ApplicationInstallResponse} + * @throws ApplicationManagementException if error occured when adding operation on device or updating subscription + * data. + */ private ApplicationInstallResponse performActionOnDevices(String deviceType, List devices, ApplicationDTO applicationDTO, String subType, List subscribers, String action) throws ApplicationManagementException { - SubscribingDeviceIdHolder subscribingDeviceIdHolder = getSubscribingDeviceIdHolder(devices); + SubscribingDeviceIdHolder subscribingDeviceIdHolder = getSubscribingDeviceIdHolder(devices, + applicationDTO.getApplicationReleaseDTOs().get(0).getId()); List activityList = new ArrayList<>(); List deviceIdentifiers = new ArrayList<>(); List ignoredDeviceIdentifiers = new ArrayList<>(); Map> deviceIdentifierMap = new HashMap<>(); if (SubAction.INSTALL.toString().equalsIgnoreCase(action)) { - deviceIdentifiers = new ArrayList<>(subscribingDeviceIdHolder.getSubscribableDevices().keySet()); - ignoredDeviceIdentifiers = new ArrayList<>(subscribingDeviceIdHolder.getSubscribedDevices().keySet()); - - if (deviceIdentifiers.isEmpty()) { - ApplicationInstallResponse applicationInstallResponse = new ApplicationInstallResponse(); - applicationInstallResponse.setIgnoredDeviceIdentifiers(ignoredDeviceIdentifiers); - return applicationInstallResponse; - } + deviceIdentifiers = new ArrayList<>(subscribingDeviceIdHolder.getAppInstallableDevices().keySet()); + ignoredDeviceIdentifiers = new ArrayList<>(subscribingDeviceIdHolder.getAppInstalledDevices().keySet()); } else if (SubAction.UNINSTALL.toString().equalsIgnoreCase(action)) { - deviceIdentifiers = new ArrayList<>(subscribingDeviceIdHolder.getSubscribedDevices().keySet()); - ignoredDeviceIdentifiers = new ArrayList<>(subscribingDeviceIdHolder.getSubscribableDevices().keySet()); - if (deviceIdentifiers.isEmpty()) { - ApplicationInstallResponse applicationInstallResponse = new ApplicationInstallResponse(); - applicationInstallResponse.setIgnoredDeviceIdentifiers(ignoredDeviceIdentifiers); - return applicationInstallResponse; - } + deviceIdentifiers = new ArrayList<>(subscribingDeviceIdHolder.getAppInstalledDevices().keySet()); + ignoredDeviceIdentifiers = new ArrayList<>(subscribingDeviceIdHolder.getAppInstallableDevices().keySet()); + } + + if (deviceIdentifiers.isEmpty()) { + ApplicationInstallResponse applicationInstallResponse = new ApplicationInstallResponse(); + applicationInstallResponse.setIgnoredDeviceIdentifiers(ignoredDeviceIdentifiers); + return applicationInstallResponse; } + //device type is getting null when we try to perform action on Web Clip. if (deviceType == null) { for (DeviceIdentifier identifier : deviceIdentifiers) { List identifiers; @@ -417,28 +423,37 @@ public class SubscriptionManagerImpl implements SubscriptionManager { return applicationInstallResponse; } - private SubscribingDeviceIdHolder getSubscribingDeviceIdHolder(List devices) + /*** + * Filter given devices and davide given list of device into two sets, those are already application installed + * devices and application installable devices. + * + * @param devices List of {@link Device} + * @param appReleaseId Application release id. + * @return {@link SubscribingDeviceIdHolder} + * @throws ApplicationManagementException if error occured while getting device subscriptions for applicaion. + */ + private SubscribingDeviceIdHolder getSubscribingDeviceIdHolder(List devices, int appReleaseId) throws ApplicationManagementException { - Map subscribedDevices = new HashMap<>(); - Map subscribableDevices = new HashMap<>(); + Map appInstalledDevices = new HashMap<>(); + Map appInstallableDevices = new HashMap<>(); - List filteredDeviceIds = devices.stream().map(Device::getId).collect(Collectors.toList()); + List deviceIds = devices.stream().map(Device::getId).collect(Collectors.toList()); //get device subscriptions for given device id list. - Map deviceSubscriptions = getDeviceSubscriptions(filteredDeviceIds); + Map deviceSubscriptions = getDeviceSubscriptions(deviceIds, appReleaseId); for (Device device : devices) { DeviceIdentifier deviceIdentifier = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()); DeviceSubscriptionDTO deviceSubscriptionDTO = deviceSubscriptions.get(device.getId()); if (deviceSubscriptionDTO != null && !deviceSubscriptionDTO.isUnsubscribed() && Operation.Status.COMPLETED .toString().equals(deviceSubscriptionDTO.getStatus())) { - subscribedDevices.put(deviceIdentifier, device.getId()); + appInstalledDevices.put(deviceIdentifier, device.getId()); } else { - subscribableDevices.put(deviceIdentifier, device.getId()); + appInstallableDevices.put(deviceIdentifier, device.getId()); } } SubscribingDeviceIdHolder subscribingDeviceIdHolder = new SubscribingDeviceIdHolder(); - subscribingDeviceIdHolder.setSubscribableDevices(subscribableDevices); - subscribingDeviceIdHolder.setSubscribedDevices(subscribedDevices); + subscribingDeviceIdHolder.setAppInstallableDevices(appInstallableDevices); + subscribingDeviceIdHolder.setAppInstalledDevices(appInstalledDevices); return subscribingDeviceIdHolder; } @@ -458,6 +473,14 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } } + /*** + * Get Application with application release which has given UUID. + * + * @param uuid UUID of the application release. + * @return {@link ApplicationDTO} + * @throws ApplicationManagementException if error occurred while getting application data from database or + * verifying whether application is in installable state. + */ private ApplicationDTO getApplicationDTO(String uuid) throws ApplicationManagementException { ApplicationDTO applicationDTO; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); @@ -531,7 +554,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { for (Activity activity : activities) { int operationId = Integer.parseInt(activity.getActivityId().split("ACTIVITY_")[1]); List operationAddedDeviceIds = getOperationAddedDeviceIds(activity, - subscribingDeviceIdHolder.getSubscribableDevices()); + subscribingDeviceIdHolder.getAppInstallableDevices()); List alreadySubscribedDevices = subscriptionDAO .getSubscribedDeviceIds(operationAddedDeviceIds, applicationReleaseId, tenantId); if (SubAction.INSTALL.toString().equalsIgnoreCase(action)) { @@ -548,7 +571,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { deviceSubIds.addAll(subscribingDevices); } else if (SubAction.UNINSTALL.toString().equalsIgnoreCase(action) && !alreadySubscribedDevices.isEmpty()) { List deviceResubscribingIds = subscriptionDAO - .updateDeviceSubscription(username, alreadySubscribedDevices, false, subType, + .updateDeviceSubscription(username, alreadySubscribedDevices, true, subType, Operation.Status.PENDING.toString(), applicationReleaseId, tenantId); deviceSubIds.addAll(deviceResubscribingIds); } @@ -586,13 +609,13 @@ public class SubscriptionManagerImpl implements SubscriptionManager { return deviceIds; } - private Map getDeviceSubscriptions(List filteredDeviceIds) + private Map getDeviceSubscriptions(List deviceIds, int appReleaseId) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); try { ConnectionManagerUtil.openDBConnection(); - return this.subscriptionDAO.getDeviceSubscriptions(filteredDeviceIds, tenantId); + return this.subscriptionDAO.getDeviceSubscriptions(deviceIds, appReleaseId, tenantId); } catch (ApplicationManagementDAOException e) { String msg = "Error occured when getting device subscriptions for given device IDs"; log.error(msg, e);