From 7eb3a5d762fe41c3e865b5605651c15bf0bc1001 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Sat, 26 Oct 2019 03:01:02 +0530 Subject: [PATCH] Fix issues in APPM subscription flow --- .../mgt/common/SubscribingDeviceIdHolder.java | 9 ++ .../common/services/SubscriptionManager.java | 18 +-- .../mgt/core/dao/SubscriptionDAO.java | 11 +- .../GenericSubscriptionDAOImpl.java | 44 +++--- .../SQLServerSubscriptionDAOImpl.java | 54 ------- .../mgt/core/impl/ApplicationManagerImpl.java | 2 +- .../core/impl/SubscriptionManagerImpl.java | 147 +++++++++--------- ...ApplicationManagementPublisherAPIImpl.java | 2 +- .../services/SubscriptionManagementAPI.java | 20 ++- .../admin/SubscriptionManagementAdminAPI.java | 2 +- .../impl/SubscriptionManagementAPIImpl.java | 21 ++- 11 files changed, 149 insertions(+), 181 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 eb92f07bc0..0dd283b3fe 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 @@ -26,6 +26,7 @@ public class SubscribingDeviceIdHolder { private Map appInstalledDevices = new HashMap<>(); private Map appInstallableDevices = new HashMap<>(); private Map appReInstallableDevices = new HashMap<>(); + private Map appReUnInstallableDevices = new HashMap<>(); private Map skippedDevices = new HashMap<>(); public Map getAppInstalledDevices() { @@ -57,4 +58,12 @@ public class SubscribingDeviceIdHolder { public void setSkippedDevices(Map skippedDevices) { this.skippedDevices = skippedDevices; } + + public Map getAppReUnInstallableDevices() { + return appReUnInstallableDevices; + } + + public void setAppReUnInstallableDevices(Map appReUnInstallableDevices) { + this.appReUnInstallableDevices = appReUnInstallableDevices; + } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java index f5af8db02a..3c8a1d844e 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java @@ -85,19 +85,19 @@ public interface SubscriptionManager { /** * Perform google enterprise app install - * @param applicationUUID UUID of the application to subscribe/unsubscribe + * @param applicationUUID UUID of the application to subscribe/unsubscribe * @param params list of subscribers. This list can be of either * {@link org.wso2.carbon.device.mgt.common.DeviceIdentifier} if {@param subType} is equal * to DEVICE or {@link String} if {@param subType} is USER, ROLE or GROUP * @param subType subscription type. E.g. DEVICE, USER, ROLE, GROUP {@see { - * @param action subscription action. E.g. INSTALL/UNINSTALL {@see { - * @param generic type of the method. - * @return {@link ApplicationInstallResponse} - * @throws ApplicationManagementException ApplicationManagementException if error occurs when subscribing to the - * given application - * @link org.wso2.carbon.device.application.mgt.common.SubscriptionType}} + * @param action subscription action. E.g. INSTALL/UNINSTALL {@see { + * @param generic type of the method. + * @return {@link ApplicationInstallResponse} + * @throws ApplicationManagementException ApplicationManagementException if error occurs when subscribing to the + * given application + * @link org.wso2.carbon.device.application.mgt.common.SubscriptionType}} */ - void performEntAppInstall(String applicationUUID, List params, String subType) + void performEntAppSubscription(String applicationUUID, List params, String subType, String action) throws ApplicationManagementException; /*** @@ -123,7 +123,7 @@ public interface SubscriptionManager { * @return {@link PaginationResult} pagination result of the category details. * @throws {@link ApplicationManagementException} Exception of the application management */ - PaginationResult getAppInstalledCategories(int offsetValue, int limitValue, String appUUID, + PaginationResult getAppInstalledSubscribers(int offsetValue, int limitValue, String appUUID, String subType) throws ApplicationManagementException; /** 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 33fafe57db..5b7f9a00cb 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 @@ -33,7 +33,7 @@ import java.util.Map; */ public interface SubscriptionDAO { - List addDeviceSubscription(String subscribedBy, List deviceIds, String subscribedFrom, + void addDeviceSubscription(String subscribedBy, List deviceIds, String subscribedFrom, String installStatus, int releaseId, int tenantId ) throws ApplicationManagementDAOException; void updateDeviceSubscription(String updateBy, List deviceIds, String action, String actionTriggeredFrom, @@ -66,13 +66,13 @@ public interface SubscriptionDAO { Map getDeviceSubscriptions(List deviceIds, int appReleaseId, int tenantId) throws ApplicationManagementDAOException; - List getSubscribedUserNames(List users, int tenantId) throws + List getAppSubscribedUserNames(List users, int appReleaseId, int tenantId) throws ApplicationManagementDAOException; - List getSubscribedRoleNames(List roles, int tenantId) throws + List getAppSubscribedRoleNames(List roles, int appReleaseId, int tenantId) throws ApplicationManagementDAOException; - List getSubscribedGroupNames(List groups, int tenantId) throws + List getAppSubscribedGroupNames(List groups, int appReleaseId, int tenantId) throws ApplicationManagementDAOException; void updateSubscriptions(int tenantId, String updateBy, List paramList, @@ -81,7 +81,8 @@ public interface SubscriptionDAO { List getDeviceSubIds(List deviceIds, int applicationReleaseId, int tenantId) throws ApplicationManagementDAOException; - List getDeviceSubIdsForOperation (int operationId, int tenantId) throws ApplicationManagementDAOException; + List getDeviceSubIdsForOperation(int operationId, int deviceID, int tenantId) + throws ApplicationManagementDAOException; boolean updateDeviceSubStatus(int deviceId, List deviceSubIds, String status, int tenantcId) 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 444445a288..46621cb9d4 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 @@ -49,7 +49,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc private static Log log = LogFactory.getLog(GenericSubscriptionDAOImpl.class); @Override - public List addDeviceSubscription(String subscribedBy, List deviceIds, + public void addDeviceSubscription(String subscribedBy, List deviceIds, String subscribedFrom, String installStatus, int releaseId, int tenantId) throws ApplicationManagementDAOException { String sql = "INSERT INTO " @@ -82,13 +82,6 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } } stmt.executeBatch(); - try (ResultSet rs = stmt.getGeneratedKeys()) { - List deviceSubIds = new ArrayList<>(); - while (rs.next()) { - deviceSubIds.add(rs.getInt(1)); - } - return deviceSubIds; - } } } catch (DBConnectionException e) { String msg = "Error occured while obtaining database connection to add device subscription for application " @@ -425,8 +418,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } } - @Override - public List getSubscribedUserNames(List users, int tenantId) + public List getAppSubscribedUserNames(List users, int appReleaseId, int tenantId) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { log.debug("Request received in DAO Layer to get already subscribed users for given list of user names."); @@ -438,13 +430,14 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc StringJoiner joiner = new StringJoiner(",", "SELECT US.USER_NAME AS USER_NAME " + "FROM AP_USER_SUBSCRIPTION US " - + "WHERE US.USER_NAME IN (", ") AND TENANT_ID = ?"); + + "WHERE US.USER_NAME IN (", ") AND AP_APP_RELEASE_ID = ? AND TENANT_ID = ?"); users.stream().map(ignored -> "?").forEach(joiner::add); String query = joiner.toString(); try (PreparedStatement ps = conn.prepareStatement(query)) { for (String username : users) { ps.setObject(index++, username); } + ps.setInt(index++, appReleaseId); ps.setInt(index, tenantId); try (ResultSet rs = ps.executeQuery()) { while (rs.next()) { @@ -465,8 +458,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } } - @Override - public List getSubscribedRoleNames(List roles, int tenantId) + public List getAppSubscribedRoleNames(List roles, int appReleaseId, int tenantId) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { log.debug("Request received in DAO Layer to get already subscribed role names for given list of roles."); @@ -478,13 +470,14 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc StringJoiner joiner = new StringJoiner(",", "SELECT RS.ROLE_NAME AS ROLE " + "FROM AP_ROLE_SUBSCRIPTION RS " - + "WHERE RS.ROLE_NAME IN (", ") AND TENANT_ID = ?"); + + "WHERE RS.ROLE_NAME IN (", ") AND AP_APP_RELEASE_ID = ? AND TENANT_ID = ?"); roles.stream().map(ignored -> "?").forEach(joiner::add); String query = joiner.toString(); try (PreparedStatement ps = conn.prepareStatement(query)) { for (String roleName : roles) { ps.setObject(index++, roleName); } + ps.setInt(index++, appReleaseId); ps.setInt(index, tenantId); try (ResultSet rs = ps.executeQuery()) { while (rs.next()) { @@ -506,7 +499,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } @Override - public List getSubscribedGroupNames(List groups, int tenantId) + public List getAppSubscribedGroupNames(List groups, int appReleaseId, int tenantId) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { log.debug("Request received in DAO Layer to get already subscribed groups for given list of groups."); @@ -518,13 +511,14 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc StringJoiner joiner = new StringJoiner(",", "SELECT GS.GROUP_NAME AS GROUP_NAME " + "FROM AP_GROUP_SUBSCRIPTION GS " - + "WHERE GS.GROUP_NAME IN (", ") AND TENANT_ID = ?"); + + "WHERE GS.GROUP_NAME IN (", ") AND AP_APP_RELEASE_ID = ? AND TENANT_ID = ?"); groups.stream().map(ignored -> "?").forEach(joiner::add); String query = joiner.toString(); try (PreparedStatement ps = conn.prepareStatement(query)) { for (String groupName : groups) { ps.setObject(index++, groupName); } + ps.setInt(index++, appReleaseId); ps.setInt(index, tenantId); try (ResultSet rs = ps.executeQuery()) { while (rs.next()) { @@ -647,20 +641,22 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } @Override - public List getDeviceSubIdsForOperation(int operationId, int tenantId) + public List getDeviceSubIdsForOperation(int operationId, int deviceId, int tenantId) throws ApplicationManagementDAOException { try { Connection conn = this.getDBConnection(); List deviceSubIds = new ArrayList<>(); - String sql = "SELECT " - + "AP_DEVICE_SUBSCRIPTION_ID " - + "FROM AP_APP_SUB_OP_MAPPING " - + "WHERE " - + "OPERATION_ID = ? AND " - + "TENANT_ID = ?"; + String sql = "SELECT AP_APP_SUB_OP_MAPPING.AP_DEVICE_SUBSCRIPTION_ID " + + "FROM " + + "AP_APP_SUB_OP_MAPPING INNER JOIN AP_DEVICE_SUBSCRIPTION " + + "ON AP_APP_SUB_OP_MAPPING.AP_DEVICE_SUBSCRIPTION_ID = AP_DEVICE_SUBSCRIPTION.ID " + + "WHERE AP_APP_SUB_OP_MAPPING.OPERATION_ID = ? AND " + + "AP_DEVICE_SUBSCRIPTION.DM_DEVICE_ID = ? AND " + + "AP_APP_SUB_OP_MAPPING.TENANT_ID = ?"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, operationId); - stmt.setInt(2, tenantId); + stmt.setInt(2, deviceId); + stmt.setInt(3, tenantId); try (ResultSet rs = stmt.executeQuery()) { while (rs.next()) { deviceSubIds.add(rs.getInt("AP_DEVICE_SUBSCRIPTION_ID")); 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/SQLServerSubscriptionDAOImpl.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/SQLServerSubscriptionDAOImpl.java index 2c725dde07..26a498af1b 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/SQLServerSubscriptionDAOImpl.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/SQLServerSubscriptionDAOImpl.java @@ -159,58 +159,4 @@ public class SQLServerSubscriptionDAOImpl extends GenericSubscriptionDAOImpl { throw new ApplicationManagementDAOException(msg, e); } } - - @Override - public List addDeviceSubscription(String subscribedBy, List deviceIds, - String subscribedFrom, String installStatus, int releaseId, int tenantId) - throws ApplicationManagementDAOException { - String sql = "INSERT INTO " - + "AP_DEVICE_SUBSCRIPTION(" - + "SUBSCRIBED_BY, " - + "SUBSCRIBED_TIMESTAMP, " - + "ACTION_TRIGGERED_FROM, " - + "STATUS, " - + "DM_DEVICE_ID, " - + "AP_APP_RELEASE_ID," - + "TENANT_ID) " - + "VALUES (?, ?, ?, ?, ?, ?, ?)"; - try { - Connection conn = this.getDBConnection(); - try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { - Calendar calendar = Calendar.getInstance(); - Timestamp timestamp = new Timestamp(calendar.getTime().getTime()); - List deviceSubIds = new ArrayList<>(); - for (Integer deviceId : deviceIds) { - stmt.setString(1, subscribedBy); - stmt.setTimestamp(2, timestamp); - stmt.setString(3, subscribedFrom); - stmt.setString(4, installStatus); - stmt.setInt(5, deviceId); - stmt.setInt(6, releaseId); - stmt.setInt(7, tenantId); - if (log.isDebugEnabled()) { - log.debug("Adding a device subscription for device id " + deviceId + " and application " - + "release which has release id" + releaseId); - } - stmt.executeUpdate(); - try (ResultSet rs = stmt.getGeneratedKeys()) { - if (rs.next()) { - deviceSubIds.add(rs.getInt(1)); - } - } - } - return deviceSubIds; - } - } catch (DBConnectionException e) { - String msg = "Error occured while obtaining database connection to add device subscription for application " - + "release which has release Id" + releaseId; - log.error(msg, e); - throw new ApplicationManagementDAOException(msg, e); - } catch (SQLException e) { - String msg = "Error occured when processing SQL to add device subscription for application release which" - + " has release Id " + releaseId; - log.error(msg, e); - throw new ApplicationManagementDAOException(msg, e); - } - } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java index b7f63a1f79..a937169b83 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java @@ -3163,7 +3163,7 @@ public class ApplicationManagerImpl implements ApplicationManager { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { ConnectionManagerUtil.beginDBTransaction(); - List deviceSubIds = subscriptionDAO.getDeviceSubIdsForOperation(operationId, tenantId); + List deviceSubIds = subscriptionDAO.getDeviceSubIdsForOperation(operationId, deviceId, tenantId); if (deviceSubIds.isEmpty()){ ConnectionManagerUtil.rollbackDBTransaction(); String msg = "Couldn't find device subscription for operation id " + operationId; 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 ffe74120bb..51409bef70 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 @@ -319,7 +319,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } @Override - public void performEntAppInstall(String applicationUUID, List params, String subType) + public void performEntAppSubscription(String applicationUUID, List params, String subType, String action) throws ApplicationManagementException { if (log.isDebugEnabled()) { log.debug("Google Ent app Install operation is received to application which has UUID " @@ -358,8 +358,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager { List devices = new ArrayList<>(); List subscribers = new ArrayList<>(); - List appInstallingDeviceIds; - List appReInstallingDeviceIds = new ArrayList<>(); + List appSubscribingDeviceIds; + List appReSubscribingDeviceIds = new ArrayList<>(); List deviceIdentifiers = new ArrayList<>(); //todo validate users, groups and roles @@ -409,14 +409,6 @@ public class SubscriptionManagerImpl implements SubscriptionManager { throw new BadRequestException(msg); } - appInstallingDeviceIds = devices.stream().map(Device::getId).collect(Collectors.toList()); - Map deviceSubscriptions = getDeviceSubscriptions(appInstallingDeviceIds, - applicationReleaseId); - for (Map.Entry deviceSubscription: deviceSubscriptions.entrySet()){ - appReInstallingDeviceIds.add(deviceSubscription.getKey()); - appInstallingDeviceIds.remove(deviceSubscription.getKey()); - } - /*If subscription type is not device we need to crete device identifiers object list by referring retrieved list of devices.*/ if (!SubscriptionType.DEVICE.toString().equalsIgnoreCase(subType)) { @@ -436,11 +428,19 @@ public class SubscriptionManagerImpl implements SubscriptionManager { ApplicationPolicyDTO applicationPolicyDTO = new ApplicationPolicyDTO(); applicationPolicyDTO.setApplicationDTO(applicationDTO); applicationPolicyDTO.setDeviceIdentifierList(deviceIdentifiers); - applicationPolicyDTO.setAction(SubAction.INSTALL.toString()); + applicationPolicyDTO.setAction(action); installEnrollmentApplications(applicationPolicyDTO); - updateSubscriptionsForEntInstall(applicationReleaseId, appInstallingDeviceIds, appReInstallingDeviceIds, - subscribers, subType); + appSubscribingDeviceIds = devices.stream().map(Device::getId).collect(Collectors.toList()); + Map deviceSubscriptions = getDeviceSubscriptions(appSubscribingDeviceIds, + applicationReleaseId); + for (Map.Entry deviceSubscription: deviceSubscriptions.entrySet()){ + appReSubscribingDeviceIds.add(deviceSubscription.getKey()); + appSubscribingDeviceIds.remove(deviceSubscription.getKey()); + } + + updateSubscriptionsForEntInstall(applicationReleaseId, appSubscribingDeviceIds, appReSubscribingDeviceIds, + subscribers, subType, action); } catch (DeviceManagementException e) { String msg = "Error occurred while getting devices of given users or given roles."; log.error(msg, e); @@ -459,10 +459,11 @@ public class SubscriptionManagerImpl implements SubscriptionManager { * @param params subscribers. If subscription is performed via user, group or role, params is a list of * {@link String} * @param subType Subscription type. i.e USER, GROUP, ROLE or DEVICE + * @param action Performing action. (i.e INSTALL or UNINSTALL) * @throws ApplicationManagementException if error occurred while getting or updating subscription data. */ - private void updateSubscriptionsForEntInstall(int applicationReleaseId, List appInstallingDeviceIds, - List appReInstallingDeviceIds, List params, String subType) + private void updateSubscriptionsForEntInstall(int applicationReleaseId, List appSubscribingDeviceIds, + List appReSubscribingDeviceIds, List params, String subType, String action) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); @@ -470,11 +471,11 @@ public class SubscriptionManagerImpl implements SubscriptionManager { ConnectionManagerUtil.beginDBTransaction(); List subscribedEntities = new ArrayList<>(); if (SubscriptionType.USER.toString().equalsIgnoreCase(subType)) { - subscribedEntities = subscriptionDAO.getSubscribedUserNames(params, tenantId); + subscribedEntities = subscriptionDAO.getAppSubscribedUserNames(params, applicationReleaseId, tenantId); } else if (SubscriptionType.ROLE.toString().equalsIgnoreCase(subType)) { - subscribedEntities = subscriptionDAO.getSubscribedRoleNames(params, tenantId); + subscribedEntities = subscriptionDAO.getAppSubscribedRoleNames(params, applicationReleaseId, tenantId); } else if (SubscriptionType.GROUP.toString().equalsIgnoreCase(subType)) { - subscribedEntities = subscriptionDAO.getSubscribedGroupNames(params, tenantId); + subscribedEntities = subscriptionDAO.getAppSubscribedGroupNames(params, applicationReleaseId, tenantId); } params.removeAll(subscribedEntities); @@ -484,17 +485,16 @@ public class SubscriptionManagerImpl implements SubscriptionManager { if (!subscribedEntities.isEmpty()) { subscriptionDAO .updateSubscriptions(tenantId, username, subscribedEntities, applicationReleaseId, subType, - SubAction.INSTALL.toString()); + action); } - if (!appInstallingDeviceIds.isEmpty()) { - subscriptionDAO.addDeviceSubscription(username, appInstallingDeviceIds, subType, + if (SubAction.INSTALL.toString().equalsIgnoreCase(action) && !appSubscribingDeviceIds.isEmpty()) { + subscriptionDAO.addDeviceSubscription(username, appSubscribingDeviceIds, subType, Operation.Status.COMPLETED.toString(), applicationReleaseId, tenantId); } - if (!appReInstallingDeviceIds.isEmpty()) { - subscriptionDAO - .updateDeviceSubscription(username, appReInstallingDeviceIds, SubAction.INSTALL.toString(), - subType, Operation.Status.COMPLETED.toString(), applicationReleaseId, tenantId); + if (!appReSubscribingDeviceIds.isEmpty()) { + subscriptionDAO.updateDeviceSubscription(username, appReSubscribingDeviceIds, action, subType, + Operation.Status.COMPLETED.toString(), applicationReleaseId, tenantId); } ConnectionManagerUtil.commitDBTransaction(); } catch (ApplicationManagementDAOException e) { @@ -578,10 +578,11 @@ public class SubscriptionManagerImpl implements SubscriptionManager { if (SubAction.INSTALL.toString().equalsIgnoreCase(action)) { deviceIdentifiers.addAll(new ArrayList<>(subscribingDeviceIdHolder.getAppInstallableDevices().keySet())); deviceIdentifiers.addAll(new ArrayList<>(subscribingDeviceIdHolder.getAppReInstallableDevices().keySet())); - ignoredDeviceIdentifiers - .addAll(new ArrayList<>(subscribingDeviceIdHolder.getAppInstalledDevices().keySet())); + deviceIdentifiers.addAll(new ArrayList<>(subscribingDeviceIdHolder.getAppInstalledDevices().keySet())); } else if (SubAction.UNINSTALL.toString().equalsIgnoreCase(action)) { deviceIdentifiers.addAll(new ArrayList<>(subscribingDeviceIdHolder.getAppInstalledDevices().keySet())); + deviceIdentifiers + .addAll(new ArrayList<>(subscribingDeviceIdHolder.getAppReUnInstallableDevices().keySet())); ignoredDeviceIdentifiers .addAll(new ArrayList<>(subscribingDeviceIdHolder.getAppInstallableDevices().keySet())); } @@ -636,10 +637,13 @@ public class SubscriptionManagerImpl implements SubscriptionManager { */ private SubscribingDeviceIdHolder getSubscribingDeviceIdHolder(List devices, int appReleaseId) throws ApplicationManagementException { - Map appInstalledDevices = new HashMap<>(); - Map appInstallableDevices = new HashMap<>(); - Map appReInstallableDevices = new HashMap<>(); - Map skippedDevices = new HashMap<>(); + + SubscribingDeviceIdHolder subscribingDeviceIdHolder = new SubscribingDeviceIdHolder(); + subscribingDeviceIdHolder.setAppInstallableDevices(new HashMap<>()); + subscribingDeviceIdHolder.setAppInstalledDevices(new HashMap<>()); + subscribingDeviceIdHolder.setAppReInstallableDevices(new HashMap<>()); + subscribingDeviceIdHolder.setAppReUnInstallableDevices(new HashMap<>()); + subscribingDeviceIdHolder.setSkippedDevices(new HashMap<>()); List deviceIds = devices.stream().map(Device::getId).collect(Collectors.toList()); //get device subscriptions for given device id list. @@ -650,23 +654,20 @@ public class SubscriptionManagerImpl implements SubscriptionManager { if (deviceSubscriptionDTO != null) { if (!deviceSubscriptionDTO.isUnsubscribed() && Operation.Status.COMPLETED.toString() .equals(deviceSubscriptionDTO.getStatus())) { - appInstalledDevices.put(deviceIdentifier, device.getId()); + subscribingDeviceIdHolder.getAppInstalledDevices().put(deviceIdentifier, device.getId()); + } else if (deviceSubscriptionDTO.isUnsubscribed() && !Operation.Status.COMPLETED.toString() + .equals(deviceSubscriptionDTO.getStatus())) { + subscribingDeviceIdHolder.getAppReUnInstallableDevices().put(deviceIdentifier, device.getId()); } else if (Operation.Status.PENDING.toString().equals(deviceSubscriptionDTO.getStatus()) || Operation.Status.IN_PROGRESS.toString().equals(deviceSubscriptionDTO.getStatus())) { - skippedDevices.put(deviceIdentifier, device.getId()); + subscribingDeviceIdHolder.getSkippedDevices().put(deviceIdentifier, device.getId()); } else { - appReInstallableDevices.put(deviceIdentifier, device.getId()); + subscribingDeviceIdHolder.getAppReInstallableDevices().put(deviceIdentifier, device.getId()); } } else { - appInstallableDevices.put(deviceIdentifier, device.getId()); + subscribingDeviceIdHolder.getAppInstallableDevices().put(deviceIdentifier, device.getId()); } } - - SubscribingDeviceIdHolder subscribingDeviceIdHolder = new SubscribingDeviceIdHolder(); - subscribingDeviceIdHolder.setAppInstallableDevices(appInstallableDevices); - subscribingDeviceIdHolder.setAppInstalledDevices(appInstalledDevices); - subscribingDeviceIdHolder.setAppReInstallableDevices(appReInstallableDevices); - subscribingDeviceIdHolder.setSkippedDevices(skippedDevices); return subscribingDeviceIdHolder; } @@ -756,7 +757,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager { try { ConnectionManagerUtil.beginDBTransaction(); if (SubscriptionType.USER.toString().equalsIgnoreCase(subType)) { - List subscribedEntities = subscriptionDAO.getSubscribedUserNames(params, tenantId); + List subscribedEntities = subscriptionDAO + .getAppSubscribedUserNames(params, applicationReleaseId, tenantId); if (SubAction.INSTALL.toString().equalsIgnoreCase(action)) { params.removeAll(subscribedEntities); subscriptionDAO.addUserSubscriptions(tenantId, username, params, applicationReleaseId); @@ -764,7 +766,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager { subscriptionDAO.updateSubscriptions(tenantId, username, subscribedEntities, applicationReleaseId, subType, action); } else if (SubscriptionType.ROLE.toString().equalsIgnoreCase(subType)) { - List subscribedEntities = subscriptionDAO.getSubscribedRoleNames(params, tenantId); + List subscribedEntities = subscriptionDAO + .getAppSubscribedRoleNames(params, applicationReleaseId, tenantId); if (SubAction.INSTALL.toString().equalsIgnoreCase(action)) { params.removeAll(subscribedEntities); subscriptionDAO.addRoleSubscriptions(tenantId, username, params, applicationReleaseId); @@ -772,7 +775,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager { subscriptionDAO.updateSubscriptions(tenantId, username, subscribedEntities, applicationReleaseId, subType, action); } else if (SubscriptionType.GROUP.toString().equalsIgnoreCase(subType)) { - List subscribedEntities = subscriptionDAO.getSubscribedGroupNames(params, tenantId); + List subscribedEntities = subscriptionDAO + .getAppSubscribedGroupNames(params, applicationReleaseId, tenantId); if (SubAction.INSTALL.toString().equalsIgnoreCase(action)) { params.removeAll(subscribedEntities); subscriptionDAO.addGroupSubscriptions(tenantId, username, params, applicationReleaseId); @@ -785,11 +789,12 @@ public class SubscriptionManagerImpl implements SubscriptionManager { int operationId = Integer.parseInt(activity.getActivityId().split("ACTIVITY_")[1]); List subUpdatingDeviceIds = new ArrayList<>(); List subInsertingDeviceIds = new ArrayList<>(); - List deviceSubIds = new ArrayList<>(); if (SubAction.INSTALL.toString().equalsIgnoreCase(action)) { subUpdatingDeviceIds.addAll(getOperationAddedDeviceIds(activity, subscribingDeviceIdHolder.getAppReInstallableDevices())); + subUpdatingDeviceIds.addAll(getOperationAddedDeviceIds(activity, + subscribingDeviceIdHolder.getAppInstalledDevices())); subInsertingDeviceIds.addAll(getOperationAddedDeviceIds(activity, subscribingDeviceIdHolder.getAppInstallableDevices())); } else if (SubAction.UNINSTALL.toString().equalsIgnoreCase(action)) { @@ -797,19 +802,18 @@ public class SubscriptionManagerImpl implements SubscriptionManager { subscribingDeviceIdHolder.getAppInstalledDevices())); } - List subscribingDevices = subscriptionDAO - .addDeviceSubscription(username, subInsertingDeviceIds, subType, + subscriptionDAO.addDeviceSubscription(username, subInsertingDeviceIds, subType, Operation.Status.PENDING.toString(), applicationReleaseId, tenantId); - subscriptionDAO.updateDeviceSubscription(username, subUpdatingDeviceIds, action, subType, - Operation.Status.PENDING.toString(), applicationReleaseId, tenantId); - if (!subUpdatingDeviceIds.isEmpty()) { - deviceSubIds.addAll(subscriptionDAO - .getDeviceSubIds(subUpdatingDeviceIds, applicationReleaseId, tenantId)); + subscriptionDAO.updateDeviceSubscription(username, subUpdatingDeviceIds, action, subType, + Operation.Status.PENDING.toString(), applicationReleaseId, tenantId); + } + subUpdatingDeviceIds.addAll(subInsertingDeviceIds); + if (!subUpdatingDeviceIds.isEmpty()) { + List deviceSubIds = new ArrayList<>( + subscriptionDAO.getDeviceSubIds(subUpdatingDeviceIds, applicationReleaseId, tenantId)); + subscriptionDAO.addOperationMapping(operationId, deviceSubIds, tenantId); } - deviceSubIds.addAll(subscribingDevices); - - subscriptionDAO.addOperationMapping(operationId, deviceSubIds, tenantId); } ConnectionManagerUtil.commitDBTransaction(); } catch (ApplicationManagementDAOException e) { @@ -1059,14 +1063,21 @@ public class SubscriptionManagerImpl implements SubscriptionManager { List deviceSubscriptionDTOS = subscriptionDAO .getDeviceSubscriptions(applicationReleaseId, tenantId); if (deviceSubscriptionDTOS.isEmpty()) { - String msg = "Couldn't found an subscribed devices for application release id: " - + applicationReleaseId; - log.info(msg); + PaginationResult paginationResult = new PaginationResult(); + paginationResult.setData(new ArrayList<>()); + paginationResult.setRecordsFiltered(0); + paginationResult.setRecordsTotal(0); + return paginationResult; } List deviceIdList = new ArrayList<>(); - for (DeviceSubscriptionDTO deviceIds : deviceSubscriptionDTOS) { - deviceIdList.add(deviceIds.getDeviceId()); - } + deviceSubscriptionDTOS.forEach(deviceSubscriptionDTO -> { + if ((!deviceSubscriptionDTO.isUnsubscribed() && Operation.Status.COMPLETED.toString() + .equalsIgnoreCase(deviceSubscriptionDTO.getStatus())) || (deviceSubscriptionDTO.isUnsubscribed() + && !Operation.Status.COMPLETED.toString() + .equalsIgnoreCase(deviceSubscriptionDTO.getStatus()))) { + deviceIdList.add(deviceSubscriptionDTO.getDeviceId()); + } + }); if (deviceIdList.isEmpty()){ PaginationResult paginationResult = new PaginationResult(); @@ -1109,7 +1120,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } @Override - public PaginationResult getAppInstalledCategories(int offsetValue, int limitValue, String appUUID, String subType) + public PaginationResult getAppInstalledSubscribers(int offsetValue, int limitValue, String appUUID, String subType) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); @@ -1178,18 +1189,14 @@ public class SubscriptionManagerImpl implements SubscriptionManager { List deviceSubscriptionDTOS = subscriptionDAO .getDeviceSubscriptions(applicationReleaseId, tenantId); if (deviceSubscriptionDTOS.isEmpty()) { - String msg = "Couldn't found an subscribed devices for application release id: " + applicationReleaseId; - log.info(msg); - } - List deviceIdList = deviceSubscriptionDTOS.stream().map(DeviceSubscriptionDTO::getDeviceId) - .collect(Collectors.toList()); - if (deviceIdList.isEmpty()) { PaginationResult paginationResult = new PaginationResult(); - paginationResult.setData(deviceIdList); + paginationResult.setData(new ArrayList<>()); paginationResult.setRecordsFiltered(0); paginationResult.setRecordsTotal(0); return paginationResult; } + List deviceIdList = deviceSubscriptionDTOS.stream().map(DeviceSubscriptionDTO::getDeviceId) + .collect(Collectors.toList()); try { //pass the device id list to device manager service method PaginationResult paginationResult = deviceManagementProviderService diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementPublisherAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementPublisherAPIImpl.java index 5e0e765239..ae864cb52e 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementPublisherAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementPublisherAPIImpl.java @@ -954,7 +954,7 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem log.error(msg); throw new BadRequestException(msg); } - applicationArtifact.setInstallerName(installerFileName); + applicationArtifact.setInstallerName(installerFileName.replaceAll("\\s", "")); applicationArtifact.setInstallerStream(installerStream); } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/SubscriptionManagementAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/SubscriptionManagementAPI.java index 6d60abc4f7..897154efc3 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/SubscriptionManagementAPI.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/SubscriptionManagementAPI.java @@ -186,7 +186,7 @@ public interface SubscriptionManagementAPI { ); @POST - @Path("/{uuid}/devices/ent-app-install") + @Path("/{uuid}/devices/ent-app-install/{action}") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @ApiOperation( @@ -206,13 +206,19 @@ public interface SubscriptionManagementAPI { value = { }) - Response performEntAppInstallationOnDevices( + Response performEntAppSubscriptionOnDevices( @ApiParam( name = "UUID", value = "The application UUID", required = true ) @PathParam("uuid") String uuid, + @ApiParam( + name = "action", + value = "Performing action.", + required = true + ) + @PathParam("action") String action, @ApiParam( name = "installationDetails", value = "The list of device identifiers", @@ -227,7 +233,7 @@ public interface SubscriptionManagementAPI { ); @POST - @Path("/{uuid}/{subType}/ent-app-install") + @Path("/{uuid}/{subType}/ent-app-install/{action}") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @ApiOperation( @@ -247,7 +253,7 @@ public interface SubscriptionManagementAPI { value = { }) - Response performBulkEntAppInstallation( + Response performBulkEntAppSubscription( @ApiParam( name = "uuid", value = "The application release UUID", @@ -260,6 +266,12 @@ public interface SubscriptionManagementAPI { required = true ) @PathParam("subType") String subType, + @ApiParam( + name = "action", + value = "Performing action.", + required = true + ) + @PathParam("action") String action, @ApiParam( name = "subscribers", value = "Subscriber list of the application release.", diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/admin/SubscriptionManagementAdminAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/admin/SubscriptionManagementAdminAPI.java index 904e6acdd6..7fb357e4fe 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/admin/SubscriptionManagementAdminAPI.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/admin/SubscriptionManagementAdminAPI.java @@ -92,7 +92,7 @@ public interface SubscriptionManagementAdminAPI { tags = "Subscription Management", extensions = { @Extension(properties = { - @ExtensionProperty(name = SCOPE, value = "perm:app:subscription:view") + @ExtensionProperty(name = SCOPE, value = "perm:admin:app:subscription:view") }) } ) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java index 8eaae60374..ba83ba655f 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java @@ -106,7 +106,6 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{ } } - @Override @POST @Path("/{uuid}/{subType}/{action}") @@ -151,16 +150,17 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{ @Override @POST - @Path("/{uuid}/devices/ent-app-install") - public Response performEntAppInstallationOnDevices( + @Path("/{uuid}/devices/ent-app-install/{action}") + public Response performEntAppSubscriptionOnDevices( @PathParam("uuid") String uuid, + @PathParam("action") String action, @Valid List deviceIdentifiers, @QueryParam("timestamp") String timestamp) { try { if (StringUtils.isEmpty(timestamp)) { SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager(); subscriptionManager - .performEntAppInstall(uuid, deviceIdentifiers, SubscriptionType.DEVICE.toString()); + .performEntAppSubscription(uuid, deviceIdentifiers, SubscriptionType.DEVICE.toString(), action); String msg = "Application release which has UUID " + uuid + " is installed to given valid device " + "identifiers."; return Response.status(Response.Status.OK).entity(msg).build(); @@ -194,16 +194,17 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{ @Override @POST - @Path("/{uuid}/{subType}/ent-app-install") - public Response performBulkEntAppInstallation( + @Path("/{uuid}/{subType}/ent-app-install/{action}") + public Response performBulkEntAppSubscription( @PathParam("uuid") String uuid, @PathParam("subType") String subType, + @PathParam("action") String action, @Valid List subscribers, @QueryParam("timestamp") String timestamp) { try { if (StringUtils.isEmpty(timestamp)) { SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager(); - subscriptionManager.performEntAppInstall(uuid, subscribers, subType); + subscriptionManager.performEntAppSubscription(uuid, subscribers, subType, action); String msg = "Application release which has UUID " + uuid + " is installed to subscriber's valid device" + " identifiers."; return Response.status(Response.Status.OK).entity(msg).build(); @@ -277,15 +278,11 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{ @QueryParam("status") String status) { try { SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager(); - PaginationResult subscribedDeviceDetails = subscriptionManager .getAppInstalledDevices(offset, limit, uuid, status); - DeviceList devices = new DeviceList(); - devices.setList((List) subscribedDeviceDetails.getData()); devices.setCount(subscribedDeviceDetails.getRecordsTotal()); - return Response.status(Response.Status.OK).entity(devices).build(); } catch (NotFoundException e) { String msg = "Application with application release UUID: " + uuid + " is not found"; @@ -324,7 +321,7 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{ SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager(); PaginationResult subscribedCategoryDetails = subscriptionManager - .getAppInstalledCategories(offset, limit, uuid, subType); + .getAppInstalledSubscribers(offset, limit, uuid, subType); if (SubscriptionType.USER.toString().equalsIgnoreCase(subType)) { BasicUserInfoList users = new BasicUserInfoList();