From a784e93ad5c54b859ec36b3242c04afb0603c580 Mon Sep 17 00:00:00 2001 From: nipuni Date: Mon, 10 Jun 2024 16:02:18 +0530 Subject: [PATCH] DAO and Services for APIs to populate the installation details static UI. --- .../mgt/common/dto/DeviceCountsDTO.java | 59 ++++++++++++++ .../mgt/common/dto/DeviceSubscriptionDTO.java | 18 +++++ .../SubscriptionStatusDeviceCountsDTO.java | 45 +++++++++++ .../common/services/SubscriptionManager.java | 11 ++- .../mgt/core/dao/SubscriptionDAO.java | 10 +++ .../GenericSubscriptionDAOImpl.java | 45 +++++++++++ .../core/impl/SubscriptionManagerImpl.java | 76 +++++++++++++++++++ 7 files changed, 263 insertions(+), 1 deletion(-) create mode 100644 components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/DeviceCountsDTO.java create mode 100644 components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/SubscriptionStatusDeviceCountsDTO.java diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/DeviceCountsDTO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/DeviceCountsDTO.java new file mode 100644 index 0000000000..97cdad3650 --- /dev/null +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/DeviceCountsDTO.java @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package io.entgra.device.mgt.core.application.mgt.common.dto; + +public class DeviceCountsDTO { + + private int group; + private int role; + private int user; + private int device; + + public int getGroup() { + return group; + } + + public void setGroup(int group) { + this.group = group; + } + + public int getRole() { + return role; + } + + public void setRole(int role) { + this.role = role; + } + + public int getUser() { + return user; + } + + public void setUser(int user) { + this.user = user; + } + + public int getDevice() { + return device; + } + + public void setDevice(int device) { + this.device = device; + } +} diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/DeviceSubscriptionDTO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/DeviceSubscriptionDTO.java index 9a30ec07c2..9ca27584bc 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/DeviceSubscriptionDTO.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/DeviceSubscriptionDTO.java @@ -31,6 +31,8 @@ public class DeviceSubscriptionDTO { private String actionTriggeredFrom; private String status; private int deviceId; + private int appReleaseId; + private String appUuid; public int getId() { return id; } @@ -71,4 +73,20 @@ public class DeviceSubscriptionDTO { public int getDeviceId() { return deviceId; } public void setDeviceId(int deviceId) { this.deviceId = deviceId; } + + public int getAppReleaseId() { + return appReleaseId; + } + + public void setAppReleaseId(int appReleaseId) { + this.appReleaseId = appReleaseId; + } + + public String getAppUuid() { + return appUuid; + } + + public void setAppUuid(String appUuid) { + this.appUuid = appUuid; + } } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/SubscriptionStatusDeviceCountsDTO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/SubscriptionStatusDeviceCountsDTO.java new file mode 100644 index 0000000000..96d753705b --- /dev/null +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/SubscriptionStatusDeviceCountsDTO.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package io.entgra.device.mgt.core.application.mgt.common.dto; + +public class SubscriptionStatusDeviceCountsDTO { + private String status; + private DeviceCountsDTO deviceCounts; + + public SubscriptionStatusDeviceCountsDTO(String status, DeviceCountsDTO deviceCounts) { + this.status = status; + this.deviceCounts = deviceCounts; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + public DeviceCountsDTO getDeviceCounts() { + return deviceCounts; + } + + public void setDeviceCounts(DeviceCountsDTO deviceCounts) { + this.deviceCounts = deviceCounts; + } +} diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/SubscriptionManager.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/SubscriptionManager.java index 9fa1defa8b..9daf1c05aa 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/SubscriptionManager.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/SubscriptionManager.java @@ -23,6 +23,7 @@ import io.entgra.device.mgt.core.application.mgt.common.ExecutionStatus; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionType; import io.entgra.device.mgt.core.application.mgt.common.dto.GroupSubscriptionDetailDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.ScheduledSubscriptionDTO; +import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionStatusDeviceCountsDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.UserSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.RoleSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceOperationDTO; @@ -35,7 +36,6 @@ import io.entgra.device.mgt.core.device.mgt.common.app.mgt.App; import io.entgra.device.mgt.core.device.mgt.common.operation.mgt.Activity; import java.util.List; -import java.util.Map; import java.util.Properties; /** @@ -264,4 +264,13 @@ public interface SubscriptionManager { List getDeviceSubscriptionsOperationsByUUID(String uuid) throws ApplicationManagementException; + /** + * This method is responsible for retrieving device counts details related to the given UUID. + * + * @param uuid the UUID of the application release. + * @return List of device subscription details. + * @throws SubscriptionManagementException if there is an error while fetching the details. + */ + List getDeviceCountsForActionTypesByUUID(String uuid) throws ApplicationManagementException; + } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java index 925bd9c231..01493e1366 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java @@ -374,4 +374,14 @@ public interface SubscriptionDAO { */ List getDeviceSubscriptionsDetails(int appReleaseId, int tenantId) throws ApplicationManagementDAOException; + + /** + * This method is used to get the counts of devices for subscription types related to a UUID. + * + * @param uuid the UUID of the application release. + * @param tenantId id of the current tenant. + * @throws ApplicationManagementDAOException if connection establishment or SQL execution fails. + */ + List getAppReleaseDevicesByActionType(String uuid, int tenantId) + throws ApplicationManagementDAOException; } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java index 2dca5ac0a0..7425a878f6 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java @@ -1892,5 +1892,50 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } } + @Override + public List getAppReleaseDevicesByActionType(String uuid, int tenantId) + throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Getting device details for app UUID: " + uuid + " for tenant ID: " + tenantId); + } + String sql = "SELECT " + + "DS.ACTION_TRIGGERED_FROM AS ACTION_TRIGGERED_FROM, " + + "DS.DM_DEVICE_ID AS DEVICE_ID, " + + "DS.UNSUBSCRIBED AS UNSUBSCRIBED " + + "FROM AP_APP_RELEASE AR " + + "INNER JOIN AP_DEVICE_SUBSCRIPTION DS ON AR.ID = DS.AP_APP_RELEASE_ID " + + "WHERE AR.UUID = ? AND DS.TENANT_ID = ?"; + + try (Connection conn = this.getDBConnection(); + PreparedStatement ps = conn.prepareStatement(sql)) { + + ps.setString(1, uuid); + ps.setInt(2, tenantId); + try (ResultSet rs = ps.executeQuery()) { + if (log.isDebugEnabled()) { + log.debug("Successfully retrieved device details for app UUID: " + uuid + " for tenant ID: " + tenantId); + } + List deviceSubscriptionList = new ArrayList<>(); + + while (rs.next()) { + DeviceSubscriptionDTO details = new DeviceSubscriptionDTO(); + details.setActionTriggeredFrom(rs.getString("ACTION_TRIGGERED_FROM")); + details.setDeviceId(rs.getInt("DEVICE_ID")); + details.setUnsubscribed(rs.getBoolean("UNSUBSCRIBED")); + deviceSubscriptionList.add(details); + } + return deviceSubscriptionList; + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection for getting device details for app UUID: " + + uuid + "."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while executing SQL to get device details for app UUID: " + uuid; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/SubscriptionManagerImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/SubscriptionManagerImpl.java index 35f74f9c43..478563c4ee 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/SubscriptionManagerImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/SubscriptionManagerImpl.java @@ -24,8 +24,10 @@ import io.entgra.device.mgt.core.application.mgt.common.ApplicationSubscriptionI import io.entgra.device.mgt.core.application.mgt.common.ApplicationType; import io.entgra.device.mgt.core.application.mgt.common.CategorizedSubscriptionResult; import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionData; +import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceCountsDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.GroupSubscriptionDetailDTO; +import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionStatusDeviceCountsDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.UserSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.RoleSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceOperationDTO; @@ -2132,4 +2134,78 @@ public class SubscriptionManagerImpl implements SubscriptionManager { ConnectionManagerUtil.closeDBConnection(); } } + + @Override + public List getDeviceCountsForActionTypesByUUID(String uuid) throws ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + if (uuid == null || uuid.isEmpty()) { + throw new IllegalArgumentException("UUID cannot be null or empty."); + } + + try { + ConnectionManagerUtil.openDBConnection(); + List deviceSubscriptions = subscriptionDAO.getAppReleaseDevicesByActionType(uuid, tenantId); + if (deviceSubscriptions == null || deviceSubscriptions.isEmpty()) { + throw new ApplicationManagementException("No device subscriptions found for app UUID: " + uuid); + } + + DeviceCountsDTO subscribedCounts = new DeviceCountsDTO(); + DeviceCountsDTO unsubscribedCounts = new DeviceCountsDTO(); + + for (DeviceSubscriptionDTO subscription : deviceSubscriptions) { + String actionTriggeredFrom = subscription.getActionTriggeredFrom().toLowerCase(); + + if (subscription.isUnsubscribed()) { + switch (actionTriggeredFrom) { + case "group": + unsubscribedCounts.setGroup(unsubscribedCounts.getGroup() + 1); + break; + case "role": + unsubscribedCounts.setRole(unsubscribedCounts.getRole() + 1); + break; + case "user": + unsubscribedCounts.setUser(unsubscribedCounts.getUser() + 1); + break; + case "device": + unsubscribedCounts.setDevice(unsubscribedCounts.getDevice() + 1); + break; + } + } else { + switch (actionTriggeredFrom) { + case "group": + subscribedCounts.setGroup(subscribedCounts.getGroup() + 1); + break; + case "role": + subscribedCounts.setRole(subscribedCounts.getRole() + 1); + break; + case "user": + subscribedCounts.setUser(subscribedCounts.getUser() + 1); + break; + case "device": + subscribedCounts.setDevice(subscribedCounts.getDevice() + 1); + break; + } + } + } + + List deviceCountsList = new ArrayList<>(); + deviceCountsList.add(new SubscriptionStatusDeviceCountsDTO("subscribed", subscribedCounts)); + deviceCountsList.add(new SubscriptionStatusDeviceCountsDTO("unsubscribed", unsubscribedCounts)); + + return deviceCountsList; + + } catch (ApplicationManagementDAOException e) { + String msg = "Error occurred while retrieving device count for UUID: " + uuid; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (DBConnectionException e) { + String msg = "Error occurred while retrieving the database connection"; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (ApplicationManagementException e) { + throw new RuntimeException(e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } }