From 137d970fc7af388b361e1ece1d0f4175a86a77c1 Mon Sep 17 00:00:00 2001 From: amalhub Date: Mon, 11 Sep 2017 15:40:30 +0530 Subject: [PATCH 1/4] App-mgt store response refactor --- .../impl/SubscriptionManagementAPIImpl.java | 18 +++++++++++------- .../common/services/SubscriptionManager.java | 8 ++++---- 2 files changed, 15 insertions(+), 11 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 881da552bc..5c3797e82d 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 @@ -31,6 +31,8 @@ import javax.validation.Valid; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Response; +import javax.ws.rs.core.Variant; +import java.util.HashMap; import java.util.List; /** @@ -45,23 +47,25 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{ @Override public Response installApplication(@ApiParam(name = "installationDetails", value = "The application ID and list" + " the devices/users/roles", required = true) @Valid InstallationDetails installationDetails) { - Object response; + Object result; SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager(); try { - String applicationUUTD = installationDetails.getApplicationUUID(); + String applicationUUID = installationDetails.getApplicationUUID(); if (!installationDetails.getDeviceIdentifiers().isEmpty()) { List deviceList = installationDetails.getDeviceIdentifiers(); - response = subscriptionManager.installApplicationForDevices(applicationUUTD, deviceList); + result = subscriptionManager.installApplicationForDevices(applicationUUID, deviceList); } else if (!installationDetails.getUserNameList().isEmpty()) { List userList = installationDetails.getUserNameList(); - response = subscriptionManager.installApplicationForUsers(applicationUUTD, userList); + result = subscriptionManager.installApplicationForUsers(applicationUUID, userList); } else if (!installationDetails.getRoleNameList().isEmpty()) { List roleList = installationDetails.getRoleNameList(); - response = subscriptionManager.installApplicationForRoles(applicationUUTD, roleList); + result = subscriptionManager.installApplicationForRoles(applicationUUID, roleList); } else { - response = "Missing request data!"; - return Response.status(Response.Status.BAD_REQUEST).entity(response).build(); + result = "Missing request data!"; + return Response.status(Response.Status.BAD_REQUEST).entity(result).build(); } + HashMap response = new HashMap<>(); + response.put("failedDevices", result); return Response.status(Response.Status.OK).entity(response).build(); } catch (ApplicationManagementException e) { String msg = "Error occurred while installing the application"; 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 43ecf89ed8..6f207e3f4d 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 @@ -31,7 +31,7 @@ public interface SubscriptionManager { * To install an application to given list of devices. * @param applicationUUID Application ID * @param deviceList Device list - * @return DeviceList which the application has been installed + * @return Failed Device List which the application was unable to install * @throws ApplicationManagementException Application Management Exception */ List installApplicationForDevices(String applicationUUID, @@ -42,7 +42,7 @@ public interface SubscriptionManager { * To install an application to given list of users. * @param applicationUUID Application ID * @param userList User list - * @return User list which the application has been installed + * @return Failed User List which the application was unable to install * @throws ApplicationManagementException Application Management Exception */ List installApplicationForUsers(String applicationUUID, @@ -53,7 +53,7 @@ public interface SubscriptionManager { * To install an application to given list of users. * @param applicationUUID Application ID * @param roleList Role list - * @return Role list which the application has been installed + * @return Failed Role List which the application was unable to install * @throws ApplicationManagementException Application Management Exception */ List installApplicationForRoles(String applicationUUID, @@ -64,7 +64,7 @@ public interface SubscriptionManager { * To uninstall an application from a given list of devices. * @param applicationUUID Application ID * @param deviceList Device list - * @return DeviceList which the application has been uninstalled + * @return Failed Device List which the application was unable to uninstall * @throws ApplicationManagementException Application Management Exception */ List uninstallApplication(String applicationUUID, From 5beb2b21d723cab1155dcd9cab80d2e9606bfb2e Mon Sep 17 00:00:00 2001 From: amalhub Date: Tue, 12 Sep 2017 16:05:12 +0530 Subject: [PATCH 2/4] Adding App-Mgt store implementations --- .../common/services/SubscriptionManager.java | 8 +- .../core/impl/SubscriptionManagerImpl.java | 99 +++++++++++++------ 2 files changed, 72 insertions(+), 35 deletions(-) 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 6f207e3f4d..fbfa32a2c8 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 @@ -42,10 +42,10 @@ public interface SubscriptionManager { * To install an application to given list of users. * @param applicationUUID Application ID * @param userList User list - * @return Failed User List which the application was unable to install + * @return Failed Device List which the application was unable to install * @throws ApplicationManagementException Application Management Exception */ - List installApplicationForUsers(String applicationUUID, + List installApplicationForUsers(String applicationUUID, List userList) throws ApplicationManagementException; @@ -53,10 +53,10 @@ public interface SubscriptionManager { * To install an application to given list of users. * @param applicationUUID Application ID * @param roleList Role list - * @return Failed Role List which the application was unable to install + * @return Failed Device List which the application was unable to install * @throws ApplicationManagementException Application Management Exception */ - List installApplicationForRoles(String applicationUUID, + List installApplicationForRoles(String applicationUUID, List roleList) 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/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 07eedb1980..ed103814b2 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,16 +19,21 @@ 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; import org.wso2.carbon.device.application.mgt.core.dao.common.DAOFactory; +import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; /** * This is the default implementation for the Subscription Manager. @@ -36,13 +41,69 @@ import java.util.List; public class SubscriptionManagerImpl implements SubscriptionManager { private static final Log log = LogFactory.getLog(SubscriptionManagerImpl.class); + final private String ANDROID = "android"; + final private String IOS = "ios"; @Override public List installApplicationForDevices(String applicationUUID, List deviceList) throws ApplicationManagementException { log.info("Install application: " + applicationUUID + " to: " + deviceList.size() + " devices."); + List failedDevices = installApplication(applicationUUID, deviceList); + return failedDevices; + } + + @Override + public List installApplicationForUsers(String applicationUUID, List userList) + 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); + for (Device device : devicesOfUser) { + deviceList.add(new DeviceIdentifier(device + .getDeviceIdentifier(), device.getType())); + } + } catch (DeviceManagementDAOException e) { + log.error("Error when extracting the device list from user[" + user + "].", e); + } + } + return installApplication(applicationUUID, deviceList); + } + + @Override + public List installApplicationForRoles(String applicationUUID, List roleList) + throws ApplicationManagementException { + log.info("Install application: " + applicationUUID + " to: " + roleList.size() + " roles."); + List deviceList = new ArrayList<>(); + for (String role : roleList) { + try { + List devicesOfRole = DataHolder.getInstance().getDeviceManagementService().getAllDevicesOfRole(role); + for (Device device : devicesOfRole) { + deviceList.add(new DeviceIdentifier(device + .getDeviceIdentifier(), device.getType())); + } + } catch (DeviceManagementException e) { + log.error("Error when extracting the device list from role[" + role + "].", e); + } + } + return installApplication(applicationUUID, deviceList); + } + + @Override + public List uninstallApplication(String applicationUUID, + List deviceList) + throws ApplicationManagementException { + return null; + } + + private List installApplication(String applicationUUID, List deviceList) + throws ApplicationManagementException { List failedDeviceList = new ArrayList<>(deviceList); + List androidDevices = new ArrayList<>(); + List iosDevices = new ArrayList<>(); for (DeviceIdentifier device : deviceList) { org.wso2.carbon.device.mgt.common.DeviceIdentifier deviceIdentifier = new org.wso2.carbon.device.mgt .common.DeviceIdentifier(device.getId(), device.getType()); @@ -52,9 +113,13 @@ public class SubscriptionManagerImpl implements SubscriptionManager { log.error("Device with ID: " + device.getId() + " not found to install the application."); } else { if (log.isDebugEnabled()) { - log.debug("Installing application to : " + device.getId()); + log.debug("Prepare application install to : " + device.getId()); + } + if (device.getType().equals(ANDROID)) { + androidDevices.add(deviceIdentifier); + } else { + iosDevices.add(deviceIdentifier); } - //Todo: generating one time download link for the application and put install operation to device. DAOFactory.getSubscriptionDAO().addDeviceApplicationMapping(device.getId(), applicationUUID, false); failedDeviceList.remove(device); } @@ -64,35 +129,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { DeviceManagementDAOFactory.closeConnection(); } } + //Todo: generating one time download link for the application and put install operation to devices. return failedDeviceList; } - - @Override - public List installApplicationForUsers(String applicationUUID, List userList) - throws ApplicationManagementException { - log.info("Install application: " + applicationUUID + " to: " + userList.size() + " users."); - for (String user : userList) { - //Todo: implementation - //Todo: get the device list and call installApplicationForDevices - } - return userList; - } - - @Override - public List installApplicationForRoles(String applicationUUID, List roleList) - throws ApplicationManagementException { - log.info("Install application: " + applicationUUID + " to: " + roleList.size() + " users."); - for (String role : roleList) { - //Todo: implementation - //Todo: get the device list and call installApplicationForDevices - } - return roleList; - } - - @Override - public List uninstallApplication(String applicationUUID, - List deviceList) - throws ApplicationManagementException { - return null; - } } From 8186f6754eae3bc2f35e2e2ba8ec199945d3edc9 Mon Sep 17 00:00:00 2001 From: amalhub Date: Tue, 12 Sep 2017 16:08:25 +0530 Subject: [PATCH 3/4] App-mgt store code refactors --- .../mgt/api/services/impl/SubscriptionManagementAPIImpl.java | 1 - .../application/mgt/core/impl/SubscriptionManagerImpl.java | 1 - 2 files changed, 2 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 5c3797e82d..09b46f0444 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 @@ -31,7 +31,6 @@ import javax.validation.Valid; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Response; -import javax.ws.rs.core.Variant; import java.util.HashMap; import java.util.List; 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 ed103814b2..ebd0b65b61 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 @@ -33,7 +33,6 @@ import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; -import java.util.stream.Collectors; /** * This is the default implementation for the Subscription Manager. From 81f427f34f924c8a2eed0362abd16ac8c32dd0c3 Mon Sep 17 00:00:00 2001 From: amalhub Date: Tue, 12 Sep 2017 17:05:37 +0530 Subject: [PATCH 4/4] App-mgt store removing device type specific impl --- .../mgt/core/impl/SubscriptionManagerImpl.java | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) 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 ebd0b65b61..31c4817bf4 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 @@ -40,8 +40,6 @@ import java.util.List; public class SubscriptionManagerImpl implements SubscriptionManager { private static final Log log = LogFactory.getLog(SubscriptionManagerImpl.class); - final private String ANDROID = "android"; - final private String IOS = "ios"; @Override public List installApplicationForDevices(String applicationUUID, @@ -101,8 +99,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { private List installApplication(String applicationUUID, List deviceList) throws ApplicationManagementException { List failedDeviceList = new ArrayList<>(deviceList); - List androidDevices = new ArrayList<>(); - List iosDevices = new ArrayList<>(); + List activeDeviceList = new ArrayList<>(); for (DeviceIdentifier device : deviceList) { org.wso2.carbon.device.mgt.common.DeviceIdentifier deviceIdentifier = new org.wso2.carbon.device.mgt .common.DeviceIdentifier(device.getId(), device.getType()); @@ -114,11 +111,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { if (log.isDebugEnabled()) { log.debug("Prepare application install to : " + device.getId()); } - if (device.getType().equals(ANDROID)) { - androidDevices.add(deviceIdentifier); - } else { - iosDevices.add(deviceIdentifier); - } + activeDeviceList.add(deviceIdentifier); DAOFactory.getSubscriptionDAO().addDeviceApplicationMapping(device.getId(), applicationUUID, false); failedDeviceList.remove(device); }