From c89db6787da92aafed471796964eaa4e81300b18 Mon Sep 17 00:00:00 2001 From: Chandima Date: Wed, 26 Jan 2022 16:53:49 +0530 Subject: [PATCH] Add installed version for installation details view --- .../mgt/common/DeviceSubscriptionData.java | 5 ++ .../common/services/SubscriptionManager.java | 3 +- .../mgt/core/dao/SubscriptionDAO.java | 9 +++ .../GenericSubscriptionDAOImpl.java | 62 +++++++++++++++++++ .../core/impl/SubscriptionManagerImpl.java | 11 +++- .../admin/SubscriptionManagementAdminAPI.java | 4 ++ .../SubscriptionManagementAdminAPIImpl.java | 3 +- 7 files changed, 94 insertions(+), 3 deletions(-) diff --git a/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/DeviceSubscriptionData.java b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/DeviceSubscriptionData.java index d769a8b139..6484080bef 100644 --- a/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/DeviceSubscriptionData.java +++ b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/DeviceSubscriptionData.java @@ -29,6 +29,7 @@ public class DeviceSubscriptionData { private String actionType; private String status; private Device device; + private String currentInstalledVersion; public String getAction() { return action; @@ -77,4 +78,8 @@ public class DeviceSubscriptionData { public void setDevice(Device device) { this.device = device; } + + public String getCurrentInstalledVersion() { return currentInstalledVersion; } + + public void setCurrentInstalledVersion(String currentInstalledVersion) { this.currentInstalledVersion = currentInstalledVersion; } } diff --git a/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/services/SubscriptionManager.java b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/services/SubscriptionManager.java index 4fd68a490e..d77ee1401f 100644 --- a/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/services/SubscriptionManager.java +++ b/components/application-mgt/io.entgra.application.mgt.common/src/main/java/io/entgra/application/mgt/common/services/SubscriptionManager.java @@ -175,12 +175,13 @@ public interface SubscriptionManager { * @param actionStatus status of the operation. * @param action action related to the device. * @param appUUID application release UUID + * @param installedVersion installed version * @return {@link PaginationResult} * @throws ApplicationManagementException if offset or limit contains incorrect values, if it couldn't find an * application release for given UUID, if an error occurred while getting device details of subscribed device ids, * if an error occurred while getting subscription details of given application release UUID. */ - PaginationResult getAppSubscriptionDetails(PaginationRequest request, String appUUID, String actionStatus, String action) + PaginationResult getAppSubscriptionDetails(PaginationRequest request, String appUUID, String actionStatus, String action, String installedVersion) throws ApplicationManagementException; /*** diff --git a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/SubscriptionDAO.java b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/SubscriptionDAO.java index a84446d6a3..9913d8e19c 100644 --- a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/SubscriptionDAO.java +++ b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/SubscriptionDAO.java @@ -239,4 +239,13 @@ public interface SubscriptionDAO { */ List getAppSubscribedDevicesForGroups(int appReleaseId, String subtype, int tenantId) throws ApplicationManagementDAOException; + + /** + * This method is used to get the currently installed version for given app release id + * @param appId id of the application + * @param deviceIdList id list of devices + * @return Map with device id as a key and currently installed version as value + * @throws {@link ApplicationManagementDAOException} if connections establishment fails. + */ + Map getCurrentInstalledAppVersion(int appId, List deviceIdList, String installedVersion) throws ApplicationManagementDAOException; } diff --git a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java index 521981849d..74b7741c18 100644 --- a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java +++ b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java @@ -1311,4 +1311,66 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc throw new ApplicationManagementDAOException(msg, e); } } + + @Override + public Map getCurrentInstalledAppVersion(int appId, List deviceIdList, String installedVersion ) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to get current installed version of the app for " + + "given app release id."); + } + try { + + Map installedVersionsMap = new HashMap<>(); + Connection conn = this.getDBConnection(); + int index = 1; + boolean isInstalledVersionAvailable = false; + StringJoiner joiner = new StringJoiner(",", + " SELECT DM_DEVICE_ID AS DEVICE,VERSION FROM " + + " (SELECT AP_APP.ID, VERSION FROM AP_APP_RELEASE AP_APP " + + " WHERE ID IN (SELECT ID FROM AP_APP_RELEASE " + + " WHERE AP_APP_ID = ?) " + + " ) AP_APP_V" + + " INNER JOIN " + + " (SELECT AP_APP_RELEASE_ID, DM_DEVICE_ID FROM AP_DEVICE_SUBSCRIPTION AP_DEV_1 " + + " INNER JOIN (" + + " SELECT MAX(ID) AS ID FROM AP_DEVICE_SUBSCRIPTION " + + " WHERE STATUS = 'COMPLETED' AND DM_DEVICE_ID IN (", + ") GROUP BY DM_DEVICE_ID " + + ") AP_DEV_2 " + + "ON AP_DEV_2.ID = AP_DEV_1.ID ) AP_APP_R " + + "ON AP_APP_R.AP_APP_RELEASE_ID = AP_APP_V.ID"); + deviceIdList.stream().map(ignored -> "?").forEach(joiner::add); + String query = joiner.toString(); + if(installedVersion != null && !installedVersion.isEmpty()){ + query += " WHERE VERSION = ? "; + isInstalledVersionAvailable = true; + } + try (PreparedStatement ps = conn.prepareStatement(query)) { + ps.setInt(index++, appId); + for (int deviceId : deviceIdList) { + ps.setInt(index++, deviceId); + } + if(isInstalledVersionAvailable){ + ps.setString(index++, installedVersion); + } + try (ResultSet rs = ps.executeQuery()) { + while (rs.next()) { + installedVersionsMap.put(rs.getInt("DEVICE"),rs.getString("VERSION")); + } + } + return installedVersionsMap; + } + + }catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection to get current installed version of the app for " + + "given app id."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "SQL Error occurred while getting current installed version of the app for given " + + "app id."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } } diff --git a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/impl/SubscriptionManagerImpl.java b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/impl/SubscriptionManagerImpl.java index a6ceff06d8..8f87f9b3f9 100644 --- a/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/impl/SubscriptionManagerImpl.java +++ b/components/application-mgt/io.entgra.application.mgt.core/src/main/java/io/entgra/application/mgt/core/impl/SubscriptionManagerImpl.java @@ -1361,7 +1361,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { @Override public PaginationResult getAppSubscriptionDetails(PaginationRequest request, String appUUID, String actionStatus, - String action) throws ApplicationManagementException { + String action, String installedVersion) throws ApplicationManagementException { int limitValue = request.getRowCount(); int offsetValue = request.getStartIndex(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); @@ -1395,6 +1395,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } List deviceIdList = deviceSubscriptionDTOS.stream().map(DeviceSubscriptionDTO::getDeviceId) .collect(Collectors.toList()); + Map currentVersionsMap = subscriptionDAO.getCurrentInstalledAppVersion(applicationDTO.getId(),deviceIdList, installedVersion); try { //pass the device id list to device manager service method PaginationResult paginationResult = deviceManagementProviderService.getAppSubscribedDevices @@ -1404,7 +1405,15 @@ public class SubscriptionManagerImpl implements SubscriptionManager { if (!paginationResult.getData().isEmpty()) { List devices = (List) paginationResult.getData(); for (Device device : devices) { + if(installedVersion != null && !installedVersion.isEmpty() && !currentVersionsMap.containsKey(device.getId())){ + continue; + } DeviceSubscriptionData deviceSubscriptionData = new DeviceSubscriptionData(); + if(currentVersionsMap.containsKey(device.getId())){ + deviceSubscriptionData.setCurrentInstalledVersion(currentVersionsMap.get(device.getId())); + }else{ + deviceSubscriptionData.setCurrentInstalledVersion("-"); + } for (DeviceSubscriptionDTO subscription : deviceSubscriptionDTOS) { if (subscription.getDeviceId() == device.getId()) { deviceSubscriptionData.setDevice(device); diff --git a/components/application-mgt/io.entgra.application.mgt.store.api/src/main/java/io/entgra/application/mgt/store/api/services/admin/SubscriptionManagementAdminAPI.java b/components/application-mgt/io.entgra.application.mgt.store.api/src/main/java/io/entgra/application/mgt/store/api/services/admin/SubscriptionManagementAdminAPI.java index fc7719bc63..f9e5079095 100644 --- a/components/application-mgt/io.entgra.application.mgt.store.api/src/main/java/io/entgra/application/mgt/store/api/services/admin/SubscriptionManagementAdminAPI.java +++ b/components/application-mgt/io.entgra.application.mgt.store.api/src/main/java/io/entgra/application/mgt/store/api/services/admin/SubscriptionManagementAdminAPI.java @@ -139,6 +139,10 @@ public interface SubscriptionManagementAdminAPI { name = "status", value = "Provide the device status details, such as active or inactive.") @QueryParam("status") List status, + @ApiParam( + name = "installedVersion", + value = "Provide the installed version of the application.") + @QueryParam("installedVersion") String installedVersion, @ApiParam( name = "uuid", value = "uuid of the application release.", diff --git a/components/application-mgt/io.entgra.application.mgt.store.api/src/main/java/io/entgra/application/mgt/store/api/services/impl/admin/SubscriptionManagementAdminAPIImpl.java b/components/application-mgt/io.entgra.application.mgt.store.api/src/main/java/io/entgra/application/mgt/store/api/services/impl/admin/SubscriptionManagementAdminAPIImpl.java index b767c39ccd..d95853fcc7 100644 --- a/components/application-mgt/io.entgra.application.mgt.store.api/src/main/java/io/entgra/application/mgt/store/api/services/impl/admin/SubscriptionManagementAdminAPIImpl.java +++ b/components/application-mgt/io.entgra.application.mgt.store.api/src/main/java/io/entgra/application/mgt/store/api/services/impl/admin/SubscriptionManagementAdminAPIImpl.java @@ -60,6 +60,7 @@ public class SubscriptionManagementAdminAPIImpl implements SubscriptionManagemen @QueryParam("action") String action, @QueryParam("actionStatus") String actionStatus, @QueryParam("status") List status, + @QueryParam("installedVersion") String installedVersion, @PathParam("uuid") String uuid, @DefaultValue("0") @QueryParam("offset") int offset, @@ -97,7 +98,7 @@ public class SubscriptionManagementAdminAPIImpl implements SubscriptionManagemen } SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager(); PaginationResult subscriptionData = subscriptionManager.getAppSubscriptionDetails - (request, uuid, actionStatus, action); + (request, uuid, actionStatus, action, installedVersion); return Response.status(Response.Status.OK).entity(subscriptionData).build(); } catch (NotFoundException e) { String msg = "Application with application release UUID: " + uuid + " is not found";