From 1919fbde165a8e95fb8b17334b7828ccd3ec3d0e Mon Sep 17 00:00:00 2001 From: Dharmakeerthi Lasantha Date: Tue, 25 Aug 2020 18:04:46 +0000 Subject: [PATCH] Add new feature to install applications for device --- .../common/services/SubscriptionManager.java | 13 ++++ .../core/impl/SubscriptionManagerImpl.java | 73 +++++++++++++++++-- .../mgt/core/mgt/impl/PolicyManagerImpl.java | 6 +- 3 files changed, 84 insertions(+), 8 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 0382d33516..8ac4cf4b13 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 @@ -21,6 +21,7 @@ import org.wso2.carbon.device.application.mgt.common.ExecutionStatus; import org.wso2.carbon.device.application.mgt.common.dto.ScheduledSubscriptionDTO; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; import org.wso2.carbon.device.application.mgt.common.exception.SubscriptionManagementException; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.PaginationResult; import java.util.List; @@ -101,6 +102,18 @@ public interface SubscriptionManager { void performEntAppSubscription(String applicationUUID, List params, String subType, String action, boolean requiresUpdatingExternal) throws ApplicationManagementException; + /** + * Install given application releases for given device. If application is already installed that application skips. + * This is used in enterprise app installing policy. + * + * @param deviceIdentifier Device identifiers + * @param releaseUUID UUIs of applicatios + * @throws ApplicationManagementException if error occurred while installing given applications into the given + * device + */ + void installAppsForDevice(DeviceIdentifier deviceIdentifier, List releaseUUID) + throws ApplicationManagementException; + /*** * This method used to get the app id ,device ids and pass them to DM service method. * 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 dee1b7a6d1..47545d4c83 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 @@ -94,6 +94,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.stream.Collectors; /** @@ -261,7 +262,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { log.error(msg, e); throw new SubscriptionManagementException(msg, e); } catch (DBConnectionException e) { - String msg = "Error occurred while retrieving the database connection"; + String msg = "Error occurred while retrieving the database connection to clean the scheduled subscriptions"; log.error(msg, e); throw new SubscriptionManagementException(msg, e); } finally { @@ -454,6 +455,70 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } } + @Override public void installAppsForDevice(DeviceIdentifier deviceIdentifier, List releaseUUIDs) + throws ApplicationManagementException { + + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + Device device; + try { + device = DataHolder.getInstance().getDeviceManagementService().getDevice(deviceIdentifier, false); + if (device == null) { + String msg = "Invalid device identifier is received and couldn't find an deveice for the requested " + + "device identifier. Device UUID: " + deviceIdentifier.getId() + " Device Type: " + + deviceIdentifier.getType(); + log.error(msg); + throw new BadRequestException(msg); + } + } catch (DeviceManagementException e) { + String msg = "Error occured while getting device data for given device identifier.Device UUID: " + + deviceIdentifier.getId() + " Device Type: " + deviceIdentifier.getType(); + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } + + List appInstallingDevices = new ArrayList<>(); + + for (String releaseUUID : releaseUUIDs) { + try { + ConnectionManagerUtil.openDBConnection(); + ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(releaseUUID, tenantId); + if (applicationDTO != null) { + List deviceSubscriptionDTOS = this.subscriptionDAO + .getDeviceSubscriptions(applicationDTO.getApplicationReleaseDTOs().get(0).getId(), + tenantId); + AtomicBoolean isAppSubscribable = new AtomicBoolean(true); + for (DeviceSubscriptionDTO deviceSubscriptionDTO : deviceSubscriptionDTOS) { + if (device.getId() == deviceSubscriptionDTO.getDeviceId() && !deviceSubscriptionDTO + .isUnsubscribed()) { + isAppSubscribable.set(false); + break; + } + } + if (isAppSubscribable.get()) { + appInstallingDevices.add(deviceIdentifier); + } + } + } catch (DBConnectionException e) { + String msg = " Error occurred while getting DB connection to retrieve app data data from DB. Device " + + "UUID: " + deviceIdentifier.getId() + " Device Type: " + deviceIdentifier.getType(); + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (ApplicationManagementDAOException e) { + String msg = " Error occurred while getting application data from DB. Device UUID: " + deviceIdentifier + .getId() + " Device Type: " + deviceIdentifier.getType(); + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + + if (!appInstallingDevices.isEmpty()) { + performBulkAppOperation(releaseUUID, appInstallingDevices, SubscriptionType.DEVICE.toString(), + SubAction.INSTALL.toString()); + } + } + } + /** * This method is responsible to update subscription data for google enterprise install. * @@ -584,13 +649,11 @@ public class SubscriptionManagerImpl implements SubscriptionManager { List identifiers; if (!deviceIdentifierMap.containsKey(identifier.getType())) { identifiers = new ArrayList<>(); - identifiers.add(identifier); - deviceIdentifierMap.put(identifier.getType(), identifiers); } else { identifiers = deviceIdentifierMap.get(identifier.getType()); - identifiers.add(identifier); - deviceIdentifierMap.put(identifier.getType(), identifiers); } + identifiers.add(identifier); + deviceIdentifierMap.put(identifier.getType(), identifiers); } for (Map.Entry> entry : deviceIdentifierMap.entrySet()) { Activity activity = addAppOperationOnDevices(applicationDTO, new ArrayList<>(entry.getValue()), diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java index ffacfe8787..1ac0f519d1 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java @@ -930,9 +930,9 @@ public class PolicyManagerImpl implements PolicyManager { try { device = deviceManagementService.getDevice(deviceIdentifier, false); } catch (DeviceManagementException e) { - PolicyManagementDAOFactory.rollbackTransaction(); - throw new PolicyManagementException("Error occurred while getting the device details (" + - deviceIdentifier.getId() + ")", e); + String msg = "Error occurred while getting the device details (" + deviceIdentifier.getId() + ")"; + log.error(msg, e); + throw new PolicyManagementException(msg, e); } int deviceId = device.getId(); try {