From 0d19e742bd5c904114e02bf47e49625e162681e9 Mon Sep 17 00:00:00 2001 From: Rajitha Kumara Date: Wed, 17 Jul 2024 12:15:50 +0530 Subject: [PATCH] Fix role base subscription --- .../mgt/common/dto/DeviceSubscriptionDTO.java | 14 + .../mgt/core/dao/SubscriptionDAO.java | 3 +- .../GenericSubscriptionDAOImpl.java | 101 ++++++- .../core/impl/SubscriptionManagerImpl.java | 246 ++++++++++++++++++ .../core/util/SubscriptionManagementUtil.java | 29 +++ ...bscriptionManagementHelperServiceImpl.java | 152 +++++++++++ .../mgt/bean/DeviceSubscriptionStatus.java | 24 ++ .../mgt/bean/RoleBasedSubscriptionInfo.java | 61 +++++ .../SubscriptionManagementHelperService.java | 39 +++ .../dao/impl/AbstractEnrollmentDAOImpl.java | 97 ++++++- .../DeviceManagementProviderServiceImpl.java | 56 ++-- 11 files changed, 793 insertions(+), 29 deletions(-) create mode 100644 components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/SubscriptionManagementUtil.java create mode 100644 components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/RoleBasedSubscriptionManagementHelperServiceImpl.java create mode 100644 components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/bean/DeviceSubscriptionStatus.java create mode 100644 components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/bean/RoleBasedSubscriptionInfo.java create mode 100644 components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/service/SubscriptionManagementHelperService.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/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 3306256b19..44885312dc 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 @@ -19,6 +19,7 @@ package io.entgra.device.mgt.core.application.mgt.common.dto; import java.sql.Timestamp; +import java.util.Objects; public class DeviceSubscriptionDTO { @@ -121,4 +122,17 @@ public class DeviceSubscriptionDTO { public void setAppUuid(String appUuid) { this.appUuid = appUuid; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + DeviceSubscriptionDTO that = (DeviceSubscriptionDTO) o; + return deviceId == that.deviceId; + } + + @Override + public int hashCode() { + return Objects.hash(deviceId); + } } 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 ec3717391a..8308669743 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 @@ -399,7 +399,8 @@ public interface SubscriptionDAO { */ List getSubscriptionDetailsByDeviceIds(int appReleaseId, boolean unsubscribe, int tenantId, List deviceIds, String actionStatus, String actionType, - String actionTriggeredBy, String tabActionStatus) throws ApplicationManagementDAOException; + String actionTriggeredBy, String tabActionStatus, + int limit, int offset) throws ApplicationManagementDAOException; /** * This method is used to get the details of device subscriptions related to a UUID. 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 8ffb95f039..c1ab28e092 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 @@ -1913,7 +1913,8 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc @Override public List getSubscriptionDetailsByDeviceIds(int appReleaseId, boolean unsubscribe, int tenantId, List deviceIds, String actionStatus, String actionType, - String actionTriggeredBy, String tabActionStatus) throws ApplicationManagementDAOException { + String actionTriggeredBy, String tabActionStatus, + int offset, int limit) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { log.debug("Getting device subscriptions for the application release id " + appReleaseId + " and device ids " + deviceIds + " from the database"); @@ -1945,7 +1946,8 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc sql.append(" AND DS.SUBSCRIBED_BY LIKE ? "); } - sql.append("ORDER BY ").append(subscriptionStatusTime).append(" DESC"); + sql.append("ORDER BY ").append(subscriptionStatusTime). + append(" DESC ").append("LIMIT ? OFFSET ?"); try (PreparedStatement ps = conn.prepareStatement(sql.toString())) { int paramIdx = 1; @@ -1966,6 +1968,9 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc ps.setString(paramIdx++, "%" + actionTriggeredBy + "%"); } + ps.setInt(paramIdx++, limit); + ps.setInt(paramIdx++, offset); + try (ResultSet rs = ps.executeQuery()) { if (log.isDebugEnabled()) { log.debug("Successfully retrieved device subscriptions for application release id " @@ -2002,6 +2007,98 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } +// @Override +// public List getSubscriptionDetailsByDeviceIds(int appReleaseId, boolean unsubscribe, int tenantId, +// List deviceIds, String actionStatus, String actionType, +// String actionTriggeredBy, String tabActionStatus) throws ApplicationManagementDAOException { +// if (log.isDebugEnabled()) { +// log.debug("Getting device subscriptions for the application release id " + appReleaseId +// + " and device ids " + deviceIds + " from the database"); +// } +// try { +// Connection conn = this.getDBConnection(); +// String subscriptionStatusTime = unsubscribe ? "DS.UNSUBSCRIBED_TIMESTAMP" : "DS.SUBSCRIBED_TIMESTAMP"; +// StringBuilder sql = new StringBuilder("SELECT " +// + "DS.ID AS ID, " +// + "DS.SUBSCRIBED_BY AS SUBSCRIBED_BY, " +// + "DS.SUBSCRIBED_TIMESTAMP AS SUBSCRIBED_AT, " +// + "DS.UNSUBSCRIBED AS IS_UNSUBSCRIBED, " +// + "DS.UNSUBSCRIBED_BY AS UNSUBSCRIBED_BY, " +// + "DS.UNSUBSCRIBED_TIMESTAMP AS UNSUBSCRIBED_AT, " +// + "DS.ACTION_TRIGGERED_FROM AS ACTION_TRIGGERED_FROM, " +// + "DS.STATUS AS STATUS, " +// + "DS.DM_DEVICE_ID AS DEVICE_ID " +// + "FROM AP_DEVICE_SUBSCRIPTION DS " +// + "WHERE DS.AP_APP_RELEASE_ID = ? AND DS.UNSUBSCRIBED = ? AND DS.TENANT_ID = ? AND DS.DM_DEVICE_ID IN (" + +// deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ") "); +// +// if (actionStatus != null && !actionStatus.isEmpty()) { +// sql.append(" AND DS.STATUS = ? "); +// } +// if (actionType != null && !actionType.isEmpty()) { +// sql.append(" AND DS.ACTION_TRIGGERED_FROM = ? "); +// } +// if (actionTriggeredBy != null && !actionTriggeredBy.isEmpty()) { +// sql.append(" AND DS.SUBSCRIBED_BY LIKE ? "); +// } +// +// sql.append("ORDER BY ").append(subscriptionStatusTime).append(" DESC"); +// +// try (PreparedStatement ps = conn.prepareStatement(sql.toString())) { +// int paramIdx = 1; +// ps.setInt(paramIdx++, appReleaseId); +// ps.setBoolean(paramIdx++, unsubscribe); +// ps.setInt(paramIdx++, tenantId); +// for (int i = 0; i < deviceIds.size(); i++) { +// ps.setInt(paramIdx++, deviceIds.get(i)); +// } +// +// if (actionStatus != null && !actionStatus.isEmpty()) { +// ps.setString(paramIdx++, actionStatus); +// } +// if (actionType != null && !actionType.isEmpty()) { +// ps.setString(paramIdx++, actionType); +// } +// if (actionTriggeredBy != null && !actionTriggeredBy.isEmpty()) { +// ps.setString(paramIdx++, "%" + actionTriggeredBy + "%"); +// } +// +// try (ResultSet rs = ps.executeQuery()) { +// if (log.isDebugEnabled()) { +// log.debug("Successfully retrieved device subscriptions for application release id " +// + appReleaseId + " and device ids " + deviceIds); +// } +// List subscriptions = new ArrayList<>(); +// while (rs.next()) { +// DeviceSubscriptionDTO subscription = new DeviceSubscriptionDTO(); +// subscription.setId(rs.getInt("ID")); +// subscription.setSubscribedBy(rs.getString("SUBSCRIBED_BY")); +// subscription.setSubscribedTimestamp(rs.getTimestamp("SUBSCRIBED_AT")); +// subscription.setUnsubscribed(rs.getBoolean("IS_UNSUBSCRIBED")); +// subscription.setUnsubscribedBy(rs.getString("UNSUBSCRIBED_BY")); +// subscription.setUnsubscribedTimestamp(rs.getTimestamp("UNSUBSCRIBED_AT")); +// subscription.setActionTriggeredFrom(rs.getString("ACTION_TRIGGERED_FROM")); +// subscription.setStatus(rs.getString("STATUS")); +// subscription.setDeviceId(rs.getInt("DEVICE_ID")); +// subscriptions.add(subscription); +// } +// return subscriptions; +// } +// } catch (SQLException e) { +// String msg = "Error occurred while running SQL to get device subscription data for application ID: " + appReleaseId +// + " and device ids: " + deviceIds + "."; +// log.error(msg, e); +// throw new ApplicationManagementDAOException(msg, e); +// } +// } catch (DBConnectionException e) { +// String msg = "Error occurred while obtaining the DB connection for getting device subscriptions for " +// + "application Id: " + appReleaseId + " and device ids: " + deviceIds + "."; +// log.error(msg, e); +// throw new ApplicationManagementDAOException(msg, e); +// } +// +// } + @Override public List getAllSubscriptionsDetails(int appReleaseId, boolean unsubscribe, int tenantId, String actionStatus, String actionType, String actionTriggeredBy, 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 065c9576bd..64995450cc 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 @@ -2153,6 +2153,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } } + @Override public List getRoleSubscriptionsByUUID(String uuid, String subscriptionStatus, PaginationRequest request, int offset, int limit) @@ -2209,9 +2210,12 @@ public class SubscriptionManagerImpl implements SubscriptionManager { statusCounts.put("NEW", 0); statusCounts.put("SUBSCRIBED", 0); + // getting the user list for the role List users = this.getUsersForRole(roleName); for (String user : users) { + + // for each user get the device info and device ids OwnerWithDeviceDTO ownerDetailsWithDevices; try { ownerDetailsWithDevices = deviceManagementProviderService.getOwnersWithDeviceIds(user, applicationDTO.getDeviceTypeId(), @@ -2221,6 +2225,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } List deviceIds = ownerDetailsWithDevices.getDeviceIds(); + // now for each device id for (Integer deviceId : deviceIds) { List subscribedDeviceSubscriptions = new ArrayList<>(); @@ -2229,12 +2234,18 @@ public class SubscriptionManagerImpl implements SubscriptionManager { appReleaseId, !unsubscribe, tenantId, deviceIds, request.getActionStatus(), request.getActionType(), request.getActionTriggeredBy(), request.getTabActionStatus()); } + + // why the fuck is this here OwnerWithDeviceDTO ownerWithDeviceByDeviceId = deviceManagementProviderService.getOwnerWithDeviceByDeviceId(deviceId, request.getOwner(), request.getDeviceName(), request.getDeviceStatus()); + + if (ownerWithDeviceByDeviceId == null) { continue; } + + List deviceSubscriptions; try { deviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds( @@ -2388,6 +2399,241 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } } +// @Override +// public List getRoleSubscriptionsByUUID(String uuid, String subscriptionStatus, +// PaginationRequest request, int offset, int limit) +// throws ApplicationManagementException { +// int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); +// boolean unsubscribe = subscriptionStatus.equals("unsubscribed"); +// String roleName; +// String status; +// +// try { +// ConnectionManagerUtil.openDBConnection(); +// +// ApplicationReleaseDTO applicationReleaseDTO = this.applicationReleaseDAO.getReleaseByUUID(uuid, tenantId); +// if (applicationReleaseDTO == null) { +// String msg = "Couldn't find an application release for application release UUID: " + uuid; +// log.error(msg); +// throw new NotFoundException(msg); +// } +// ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(uuid, tenantId); +// int appReleaseId = applicationReleaseDTO.getId(); +// List roleSubscriptionsWithDevices = new ArrayList<>(); +// +// List roleSubscriptions = +// subscriptionDAO.getRoleSubscriptionsByAppReleaseID(appReleaseId, unsubscribe, tenantId, offset, limit); +// if (roleSubscriptions == null) { +// throw new ApplicationManagementException("Role details not found for appReleaseId: " + appReleaseId); +// } +// +// DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); +// +// for (SubscriptionsDTO roleSubscription : roleSubscriptions) { +// +// roleName = StringUtils.isNotBlank(request.getRoleName()) ? request.getRoleName() : roleSubscription.getName(); +// +// SubscriptionsDTO roleSubscriptionDTO = new SubscriptionsDTO(); +// roleSubscriptionDTO.setName(roleSubscription.getName()); +// roleSubscriptionDTO.setSubscribedBy(roleSubscription.getSubscribedBy()); +// roleSubscriptionDTO.setSubscribedTimestamp(roleSubscription.getSubscribedTimestamp()); +// roleSubscriptionDTO.setUnsubscribed(roleSubscription.getUnsubscribed()); +// roleSubscriptionDTO.setUnsubscribedBy(roleSubscription.getUnsubscribedBy()); +// roleSubscriptionDTO.setUnsubscribedTimestamp(roleSubscription.getUnsubscribedTimestamp()); +// roleSubscriptionDTO.setAppReleaseId(roleSubscription.getAppReleaseId()); +// +// List pendingDevices = new ArrayList<>(); +// List installedDevices = new ArrayList<>(); +// List errorDevices = new ArrayList<>(); +// List newDevices = new ArrayList<>(); +// List subscribedDevices = new ArrayList<>(); +// +// Map statusCounts = new HashMap<>(); +// statusCounts.put("PENDING", 0); +// statusCounts.put("COMPLETED", 0); +// statusCounts.put("ERROR", 0); +// statusCounts.put("NEW", 0); +// statusCounts.put("SUBSCRIBED", 0); +// +// List users = this.getUsersForRole(roleName); +// +// for (String user : users) { +// OwnerWithDeviceDTO ownerDetailsWithDevices; +// try { +// ownerDetailsWithDevices = deviceManagementProviderService.getOwnersWithDeviceIds(user, applicationDTO.getDeviceTypeId(), +// request.getOwner(), request.getDeviceName(), request.getDeviceStatus()); +// } catch (DeviceManagementDAOException e) { +// throw new ApplicationManagementException("Error retrieving owner details with devices for user: " + user, e); +// } +// +// List deviceIds = ownerDetailsWithDevices.getDeviceIds(); +// for (Integer deviceId : deviceIds) { +// +// List subscribedDeviceSubscriptions = new ArrayList<>(); +// if (unsubscribe) { +// subscribedDeviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds( +// appReleaseId, !unsubscribe, tenantId, deviceIds, request.getActionStatus(), request.getActionType(), +// request.getActionTriggeredBy(), request.getTabActionStatus()); +// } +// OwnerWithDeviceDTO ownerWithDeviceByDeviceId = +// deviceManagementProviderService.getOwnerWithDeviceByDeviceId(deviceId, request.getOwner(), request.getDeviceName(), +// request.getDeviceStatus()); +// if (ownerWithDeviceByDeviceId == null) { +// continue; +// } +// List deviceSubscriptions; +// try { +// deviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds( +// roleSubscription.getAppReleaseId(), unsubscribe, tenantId, deviceIds, request.getActionStatus(), +// request.getActionType(), request.getActionTriggeredBy(), request.getTabActionStatus()); +// } catch (ApplicationManagementDAOException e) { +// throw new ApplicationManagementException("Error retrieving device subscriptions", e); +// } +// +// boolean isNewDevice = true; +// for (DeviceSubscriptionDTO deviceSubscription : deviceSubscriptions) { +// if (deviceSubscription.getDeviceId() == deviceId) { +// DeviceSubscriptionData deviceDetail = new DeviceSubscriptionData(); +// deviceDetail.setDeviceId(deviceSubscription.getDeviceId()); +// deviceDetail.setDeviceName(ownerWithDeviceByDeviceId.getDeviceNames()); +// deviceDetail.setDeviceOwner(ownerWithDeviceByDeviceId.getUserName()); +// deviceDetail.setDeviceStatus(ownerWithDeviceByDeviceId.getDeviceStatus()); +// deviceDetail.setActionType(deviceSubscription.getActionTriggeredFrom()); +// deviceDetail.setStatus(deviceSubscription.getStatus()); +// deviceDetail.setActionType(deviceSubscription.getActionTriggeredFrom()); +// deviceDetail.setActionTriggeredBy(deviceSubscription.getSubscribedBy()); +// deviceDetail.setSubId(deviceSubscription.getId()); +// deviceDetail.setActionTriggeredTimestamp(deviceSubscription.getSubscribedTimestamp()); +// deviceDetail.setUnsubscribed(deviceSubscription.isUnsubscribed()); +// deviceDetail.setUnsubscribedBy(deviceSubscription.getUnsubscribedBy()); +// deviceDetail.setUnsubscribedTimestamp(deviceSubscription.getUnsubscribedTimestamp()); +// deviceDetail.setType(ownerWithDeviceByDeviceId.getDeviceTypes()); +// deviceDetail.setDeviceIdentifier(ownerWithDeviceByDeviceId.getDeviceIdentifiers()); +// +// status = deviceSubscription.getStatus(); +// switch (status) { +// case "COMPLETED": +// installedDevices.add(deviceDetail); +// statusCounts.put("COMPLETED", statusCounts.get("COMPLETED") + 1); +// break; +// case "ERROR": +// case "INVALID": +// case "UNAUTHORIZED": +// errorDevices.add(deviceDetail); +// statusCounts.put("ERROR", statusCounts.get("ERROR") + 1); +// break; +// case "IN_PROGRESS": +// case "PENDING": +// case "REPEATED": +// pendingDevices.add(deviceDetail); +// statusCounts.put("PENDING", statusCounts.get("PENDING") + 1); +// break; +// } +// isNewDevice = false; +// } +// } +// if (isNewDevice) { +// boolean isSubscribedDevice = false; +// for (DeviceSubscriptionDTO subscribedDevice : subscribedDeviceSubscriptions) { +// if (subscribedDevice.getDeviceId() == deviceId) { +// DeviceSubscriptionData subscribedDeviceDetail = new DeviceSubscriptionData(); +// subscribedDeviceDetail.setDeviceId(subscribedDevice.getDeviceId()); +// subscribedDeviceDetail.setDeviceName(ownerWithDeviceByDeviceId.getDeviceNames()); +// subscribedDeviceDetail.setDeviceOwner(ownerWithDeviceByDeviceId.getUserName()); +// subscribedDeviceDetail.setDeviceStatus(ownerWithDeviceByDeviceId.getDeviceStatus()); +// subscribedDeviceDetail.setSubId(subscribedDevice.getId()); +// subscribedDeviceDetail.setActionTriggeredBy(subscribedDevice.getSubscribedBy()); +// subscribedDeviceDetail.setActionTriggeredTimestamp(subscribedDevice.getSubscribedTimestamp()); +// subscribedDeviceDetail.setActionType(subscribedDevice.getActionTriggeredFrom()); +// subscribedDeviceDetail.setStatus(subscribedDevice.getStatus()); +// subscribedDeviceDetail.setType(ownerWithDeviceByDeviceId.getDeviceTypes()); +// subscribedDeviceDetail.setDeviceIdentifier(ownerWithDeviceByDeviceId.getDeviceIdentifiers()); +// subscribedDevices.add(subscribedDeviceDetail); +// statusCounts.put("SUBSCRIBED", statusCounts.get("SUBSCRIBED") + 1); +// isSubscribedDevice = true; +// break; +// } +// } +// if (!isSubscribedDevice) { +// DeviceSubscriptionData newDeviceDetail = new DeviceSubscriptionData(); +// newDeviceDetail.setDeviceId(deviceId); +// newDeviceDetail.setDeviceName(ownerWithDeviceByDeviceId.getDeviceNames()); +// newDeviceDetail.setDeviceOwner(ownerWithDeviceByDeviceId.getUserName()); +// newDeviceDetail.setDeviceStatus(ownerWithDeviceByDeviceId.getDeviceStatus()); +// newDeviceDetail.setType(ownerWithDeviceByDeviceId.getDeviceTypes()); +// newDeviceDetail.setDeviceIdentifier(ownerWithDeviceByDeviceId.getDeviceIdentifiers()); +// newDevices.add(newDeviceDetail); +// statusCounts.put("NEW", statusCounts.get("NEW") + 1); +// } +// } +// } +// } +// +// int totalDevices = +// pendingDevices.size() + installedDevices.size() + errorDevices.size() + newDevices.size() + subscribedDevices.size(); +// Map statusPercentages = new HashMap<>(); +// for (Map.Entry entry : statusCounts.entrySet()) { +// double percentage = totalDevices == 0 ? 0.0 : ((double) entry.getValue() / totalDevices) * 100; +// String formattedPercentage = String.format("%.2f", percentage); +// statusPercentages.put(entry.getKey(), Double.valueOf(formattedPercentage)); +// } +// +// List requestedDevices = new ArrayList<>(); +// if (StringUtils.isNotBlank(request.getTabActionStatus())) { +// switch (request.getTabActionStatus()) { +// case "COMPLETED": +// requestedDevices = installedDevices; +// break; +// case "PENDING": +// requestedDevices = pendingDevices; +// break; +// case "ERROR": +// requestedDevices = errorDevices; +// break; +// case "NEW": +// requestedDevices = newDevices; +// break; +// case "SUBSCRIBED": +// requestedDevices = subscribedDevices; +// break; +// } +// roleSubscriptionDTO.setDevices(new CategorizedSubscriptionResult(requestedDevices, request.getTabActionStatus())); +// +// } else { +// CategorizedSubscriptionResult categorizedSubscriptionResult; +// if (subscribedDevices.isEmpty()) { +// categorizedSubscriptionResult = +// new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices); +// } else { +// categorizedSubscriptionResult = +// new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices, +// subscribedDevices); +// } +// roleSubscriptionDTO.setDevices(categorizedSubscriptionResult); +// roleSubscriptionDTO.setStatusPercentages(statusPercentages); +// roleSubscriptionDTO.setDeviceCount(totalDevices); +// } +// roleSubscriptionsWithDevices.add(roleSubscriptionDTO); +// } +// +// return roleSubscriptionsWithDevices; +// } catch (ApplicationManagementDAOException | DeviceManagementDAOException e) { +// String msg = "Error occurred in retrieving role subscriptions with devices"; +// 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 (UserStoreException e) { +// String msg = "Error occurred while retrieving users for role"; +// log.error(msg, e); +// throw new ApplicationManagementException(msg, e); +// } finally { +// ConnectionManagerUtil.closeDBConnection(); +// } +// } + // Get user list for each role public List getUsersForRole(String roleName) throws UserStoreException { PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); 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/util/SubscriptionManagementUtil.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/SubscriptionManagementUtil.java new file mode 100644 index 0000000000..9c45f57be4 --- /dev/null +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/SubscriptionManagementUtil.java @@ -0,0 +1,29 @@ +/* + * 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.core.util; + +public class SubscriptionManagementUtil { + public static final class DeviceSubscriptionStatus { + public static final String COMPLETED = "COMPLETED"; + public static final String ERROR ="ERROR"; + public static final String NEW = "NEW"; + public static final String SUBSCRIBED = "SUBSCRIBED"; + } +} 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/util/subscription/mgt/RoleBasedSubscriptionManagementHelperServiceImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/RoleBasedSubscriptionManagementHelperServiceImpl.java new file mode 100644 index 0000000000..854a46fef5 --- /dev/null +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/RoleBasedSubscriptionManagementHelperServiceImpl.java @@ -0,0 +1,152 @@ +/* + * 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.core.util.subscription.mgt; + +import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationReleaseDTO; +import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; +import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionsDTO; +import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException; +import io.entgra.device.mgt.core.application.mgt.common.exception.DBConnectionException; +import io.entgra.device.mgt.core.application.mgt.core.exception.ApplicationManagementDAOException; +import io.entgra.device.mgt.core.application.mgt.core.exception.NotFoundException; +import io.entgra.device.mgt.core.application.mgt.core.internal.DataHolder; +import io.entgra.device.mgt.core.application.mgt.core.util.ConnectionManagerUtil; +import io.entgra.device.mgt.core.application.mgt.core.util.HelperUtil; +import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.bean.DeviceSubscriptionStatus; +import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.bean.RoleBasedSubscriptionInfo; +import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.service.SubscriptionManagementHelperService; +import io.entgra.device.mgt.core.device.mgt.common.Device; +import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest; +import io.entgra.device.mgt.core.device.mgt.common.PaginationResult; +import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.user.api.UserStoreException; +import org.wso2.carbon.user.api.UserStoreManager; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Objects; + +public class RoleBasedSubscriptionManagementHelperServiceImpl implements SubscriptionManagementHelperService { + private static final Log log = LogFactory.getLog(RoleBasedSubscriptionManagementHelperServiceImpl.class); + private final RoleBasedSubscriptionInfo roleBasedSubscriptionInfo; + private final boolean isUnsubscribe; + public RoleBasedSubscriptionManagementHelperServiceImpl(RoleBasedSubscriptionInfo roleBasedSubscriptionInfo) { + this.roleBasedSubscriptionInfo = roleBasedSubscriptionInfo; + this.isUnsubscribe = "unsubscribed".equals(roleBasedSubscriptionInfo.getSubscriptionStatus()); + } + + @SuppressWarnings("unchecked") + @Override + public List getStatusBaseSubscriptions(int limit, int offset) throws ApplicationManagementException { + List deviceSubscriptionDTOS; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { + UserStoreManager userStoreManager = DataHolder.getInstance().getRealmService(). + getTenantUserRealm(tenantId).getUserStoreManager(); + String[] usersWithRole = + userStoreManager.getUserListOfRole(roleBasedSubscriptionInfo.getRoleName()); + List deviceListOwnByRole = new ArrayList<>(); + for (String user : usersWithRole) { + PaginationRequest paginationRequest = new PaginationRequest(offset, limit); + paginationRequest.setOwner(user); + paginationRequest.setStatusList(Arrays.asList("ACTIVE", "INACTIVE", "UNREACHABLE")); + PaginationResult ownDeviceIds = HelperUtil.getDeviceManagementProviderService(). + getAllDevicesIdList(paginationRequest); + if (ownDeviceIds.getData() != null) { + deviceListOwnByRole.addAll((List)ownDeviceIds.getData()); + } + } + + List deviceIds = (List) deviceListOwnByRole.stream().map(Device::getId); + + ConnectionManagerUtil.openDBConnection(); + ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO. + getReleaseByUUID(roleBasedSubscriptionInfo.getApplicationUUID(), tenantId); + if (applicationReleaseDTO == null) { + String msg = "Couldn't find an application release for application release UUID: " + + roleBasedSubscriptionInfo.getApplicationUUID(); + log.error(msg); + throw new NotFoundException(msg); + } + + if (Objects.equals(DeviceSubscriptionStatus.NEW.toString(), + roleBasedSubscriptionInfo.getDeviceSubscriptionStatus().toString())) { + + deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), + isUnsubscribe, tenantId, deviceIds, null, RoleBasedSubscriptionInfo.TRIGGERED_FROM_VALUE, + null, null, limit, offset); + for (Integer deviceId : deviceIds) { + DeviceSubscriptionDTO deviceSubscriptionDTO = new DeviceSubscriptionDTO(); + deviceSubscriptionDTO.setDeviceId(deviceId); + deviceSubscriptionDTOS.remove(deviceSubscriptionDTO); + } + return deviceSubscriptionDTOS; + } + + deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), + isUnsubscribe, tenantId, deviceIds, roleBasedSubscriptionInfo.getDeviceSubscriptionStatus(). + toString(), RoleBasedSubscriptionInfo.TRIGGERED_FROM_VALUE, + null, null, limit, offset); + + } catch (UserStoreException e) { + String msg = "Error encountered while getting the user management store for tenant id " + tenantId; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (DeviceManagementException e) { + String msg = "Error encountered while getting device details"; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (ApplicationManagementDAOException | DBConnectionException e) { + String msg = "Error encountered while connecting to the database"; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } + return deviceSubscriptionDTOS; + } + + @Override + public List getSubscriptions(int limit, int offset) throws ApplicationManagementException { + final int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { + ConnectionManagerUtil.openDBConnection(); + ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO. + getReleaseByUUID(roleBasedSubscriptionInfo.getApplicationUUID(), tenantId); + if (applicationReleaseDTO == null) { + String msg = "Couldn't find an application release for application release UUID: " + + roleBasedSubscriptionInfo.getApplicationUUID(); + log.error(msg); + throw new NotFoundException(msg); + } + return subscriptionDAO. + getRoleSubscriptionsByAppReleaseID(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, offset, limit); + } catch (DBConnectionException | ApplicationManagementDAOException e) { + throw new RuntimeException(e); + } + } + + @Override + public void getSubscriptionStatistics() 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/util/subscription/mgt/bean/DeviceSubscriptionStatus.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/bean/DeviceSubscriptionStatus.java new file mode 100644 index 0000000000..11cbb62386 --- /dev/null +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/bean/DeviceSubscriptionStatus.java @@ -0,0 +1,24 @@ +/* + * 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.core.util.subscription.mgt.bean; + +public enum DeviceSubscriptionStatus { + COMPLETED, ERROR, NEW, SUBSCRIBED +} 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/util/subscription/mgt/bean/RoleBasedSubscriptionInfo.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/bean/RoleBasedSubscriptionInfo.java new file mode 100644 index 0000000000..1e7385d173 --- /dev/null +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/bean/RoleBasedSubscriptionInfo.java @@ -0,0 +1,61 @@ +/* + * 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.core.util.subscription.mgt.bean; + +public class RoleBasedSubscriptionInfo { + + private String applicationUUID; + private String roleName; + private String subscriptionStatus; + public static String TRIGGERED_FROM_VALUE = "role"; + private DeviceSubscriptionStatus deviceSubscriptionStatus; + + public String getApplicationUUID() { + return applicationUUID; + } + + public void setApplicationUUID(String applicationUUID) { + this.applicationUUID = applicationUUID; + } + + public String getRoleName() { + return roleName; + } + + public void setRoleName(String roleName) { + this.roleName = roleName; + } + + public String getSubscriptionStatus() { + return subscriptionStatus; + } + + public void setSubscriptionStatus(String subscriptionStatus) { + this.subscriptionStatus = subscriptionStatus; + } + + public DeviceSubscriptionStatus getDeviceSubscriptionStatus() { + return deviceSubscriptionStatus; + } + + public void setDeviceSubscriptionStatus(DeviceSubscriptionStatus deviceSubscriptionStatus) { + this.deviceSubscriptionStatus = deviceSubscriptionStatus; + } +} 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/util/subscription/mgt/service/SubscriptionManagementHelperService.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/service/SubscriptionManagementHelperService.java new file mode 100644 index 0000000000..256ed05b46 --- /dev/null +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/service/SubscriptionManagementHelperService.java @@ -0,0 +1,39 @@ +/* + * 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.core.util.subscription.mgt.service; + +import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; +import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionsDTO; +import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException; +import io.entgra.device.mgt.core.application.mgt.core.dao.ApplicationDAO; +import io.entgra.device.mgt.core.application.mgt.core.dao.ApplicationReleaseDAO; +import io.entgra.device.mgt.core.application.mgt.core.dao.SubscriptionDAO; +import io.entgra.device.mgt.core.application.mgt.core.dao.common.ApplicationManagementDAOFactory; + +import java.util.List; + +public interface SubscriptionManagementHelperService { + SubscriptionDAO subscriptionDAO = ApplicationManagementDAOFactory.getSubscriptionDAO(); + ApplicationDAO applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO(); + ApplicationReleaseDAO applicationReleaseDAO = ApplicationManagementDAOFactory.getApplicationReleaseDAO(); + List getStatusBaseSubscriptions(int limit, int offset) throws ApplicationManagementException; + List getSubscriptions(int limit, int offset) throws ApplicationManagementException; + void getSubscriptionStatistics() throws ApplicationManagementException; +} diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractEnrollmentDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractEnrollmentDAOImpl.java index 1160b4bb79..70456d3be6 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractEnrollmentDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractEnrollmentDAOImpl.java @@ -587,9 +587,9 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { "d.NAME AS DEVICE_NAME, " + "e.DEVICE_TYPE AS DEVICE_TYPE, " + "e.DEVICE_IDENTIFICATION AS DEVICE_IDENTIFICATION " + - "FROM DM_ENROLMENT e " + - "JOIN DM_DEVICE d ON e.DEVICE_ID = d.ID " + - "WHERE e.OWNER = ? AND e.TENANT_ID = ? AND d.DEVICE_TYPE_ID = ? AND e.STATUS IN (" + deviceFilters + ")"); + "FROM DM_ENROLMENT e " + + "JOIN DM_DEVICE d ON e.DEVICE_ID = d.ID " + + "WHERE e.OWNER = ? AND e.TENANT_ID = ? AND d.DEVICE_TYPE_ID = ? AND e.STATUS IN (" + deviceFilters + ")"); if (deviceOwner != null && !deviceOwner.isEmpty()) { sql.append(" AND e.OWNER LIKE ?"); @@ -624,13 +624,6 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { try (ResultSet rs = stmt.executeQuery()) { while (rs.next()) { - if (ownerDetails.getUserName() == null) { - ownerDetails.setUserName(rs.getString("OWNER")); - } - ownerDetails.setDeviceStatus(rs.getString("DEVICE_STATUS")); - ownerDetails.setDeviceNames(rs.getString("DEVICE_NAME")); - ownerDetails.setDeviceTypes(rs.getString("DEVICE_TYPE")); - ownerDetails.setDeviceIdentifiers(rs.getString("DEVICE_IDENTIFICATION")); deviceIds.add(rs.getInt("DEVICE_ID")); deviceCount++; } @@ -641,11 +634,95 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { log.error(msg, e); throw new DeviceManagementDAOException(msg, e); } + ownerDetails.setUserName(deviceOwner); ownerDetails.setDeviceIds(deviceIds); ownerDetails.setDeviceCount(deviceCount); return ownerDetails; } +// @Override +// public OwnerWithDeviceDTO getOwnersWithDevices(String owner, List allowingDeviceStatuses, int tenantId, +// int deviceTypeId, String deviceOwner, String deviceName, +// String deviceStatus) throws DeviceManagementDAOException { +// Connection conn = null; +// OwnerWithDeviceDTO ownerDetails = new OwnerWithDeviceDTO(); +// List deviceIds = new ArrayList<>(); +// int deviceCount = 0; +// +// StringBuilder deviceFilters = new StringBuilder(); +// for (int i = 0; i < allowingDeviceStatuses.size(); i++) { +// deviceFilters.append("?"); +// if (i < allowingDeviceStatuses.size() - 1) { +// deviceFilters.append(","); +// } +// } +// +// StringBuilder sql = new StringBuilder( +// "SELECT e.DEVICE_ID, " + +// "e.OWNER, " + +// "e.STATUS AS DEVICE_STATUS, " + +// "d.NAME AS DEVICE_NAME, " + +// "e.DEVICE_TYPE AS DEVICE_TYPE, " + +// "e.DEVICE_IDENTIFICATION AS DEVICE_IDENTIFICATION " + +// "FROM DM_ENROLMENT e " + +// "JOIN DM_DEVICE d ON e.DEVICE_ID = d.ID " + +// "WHERE e.OWNER = ? AND e.TENANT_ID = ? AND d.DEVICE_TYPE_ID = ? AND e.STATUS IN (" + deviceFilters + ")"); +// +// if (deviceOwner != null && !deviceOwner.isEmpty()) { +// sql.append(" AND e.OWNER LIKE ?"); +// } +// if (deviceName != null && !deviceName.isEmpty()) { +// sql.append(" AND d.NAME LIKE ?"); +// } +// if (deviceStatus != null && !deviceStatus.isEmpty()) { +// sql.append(" AND e.STATUS = ?"); +// } +// +// try { +// conn = this.getConnection(); +// try (PreparedStatement stmt = conn.prepareStatement(sql.toString())) { +// int index = 1; +// stmt.setString(index++, owner); +// stmt.setInt(index++, tenantId); +// stmt.setInt(index++, deviceTypeId); +// for (String status : allowingDeviceStatuses) { +// stmt.setString(index++, status); +// } +// +// if (deviceOwner != null && !deviceOwner.isEmpty()) { +// stmt.setString(index++, "%" + deviceOwner + "%"); +// } +// if (deviceName != null && !deviceName.isEmpty()) { +// stmt.setString(index++, "%" + deviceName + "%"); +// } +// if (deviceStatus != null && !deviceStatus.isEmpty()) { +// stmt.setString(index++, deviceStatus); +// } +// +// try (ResultSet rs = stmt.executeQuery()) { +// while (rs.next()) { +// if (ownerDetails.getUserName() == null) { +// ownerDetails.setUserName(rs.getString("OWNER")); +// } +// ownerDetails.setDeviceStatus(rs.getString("DEVICE_STATUS")); +// ownerDetails.setDeviceNames(rs.getString("DEVICE_NAME")); +// ownerDetails.setDeviceTypes(rs.getString("DEVICE_TYPE")); +// ownerDetails.setDeviceIdentifiers(rs.getString("DEVICE_IDENTIFICATION")); +// deviceIds.add(rs.getInt("DEVICE_ID")); +// deviceCount++; +// } +// } +// } +// } catch (SQLException e) { +// String msg = "Error occurred while retrieving owners and device IDs for owner: " + owner; +// log.error(msg, e); +// throw new DeviceManagementDAOException(msg, e); +// } +// ownerDetails.setDeviceIds(deviceIds); +// ownerDetails.setDeviceCount(deviceCount); +// return ownerDetails; +// } + @Override public OwnerWithDeviceDTO getOwnerWithDeviceByDeviceId(int deviceId, int tenantId, String deviceOwner, String deviceName, String deviceStatus) throws DeviceManagementDAOException { diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index b07f915e43..3192cefcd0 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -161,6 +161,7 @@ import javax.xml.bind.Marshaller; import java.io.IOException; import java.io.StringWriter; import java.lang.reflect.Type; +import java.sql.Array; import java.sql.SQLException; import java.sql.Timestamp; import java.time.LocalDateTime; @@ -5359,25 +5360,13 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); OwnerWithDeviceDTO ownerWithDeviceDTO; - List allowingDeviceStatuses = new ArrayList<>(); - allowingDeviceStatuses.add(EnrolmentInfo.Status.ACTIVE.toString()); - allowingDeviceStatuses.add(EnrolmentInfo.Status.INACTIVE.toString()); - allowingDeviceStatuses.add(EnrolmentInfo.Status.UNREACHABLE.toString()); + List allowingDeviceStatuses = Arrays.asList(EnrolmentInfo.Status.ACTIVE.toString(), + EnrolmentInfo.Status.INACTIVE.toString(), EnrolmentInfo.Status.UNREACHABLE.toString()); try { DeviceManagementDAOFactory.openConnection(); - ownerWithDeviceDTO = this.enrollmentDAO.getOwnersWithDevices(owner, allowingDeviceStatuses, tenantId, deviceTypeId, deviceOwner, deviceName, deviceStatus); - if (ownerWithDeviceDTO == null) { - String msg = "No data found for owner: " + owner; - log.error(msg); - throw new DeviceManagementDAOException(msg); - } - List deviceIds = ownerWithDeviceDTO.getDeviceIds(); - if (deviceIds != null) { - ownerWithDeviceDTO.setDeviceCount(deviceIds.size()); - } else { - ownerWithDeviceDTO.setDeviceCount(0); - } + ownerWithDeviceDTO = this.enrollmentDAO.getOwnersWithDevices(owner, allowingDeviceStatuses, + tenantId, deviceTypeId, deviceOwner, deviceName, deviceStatus); } catch (DeviceManagementDAOException | SQLException e) { String msg = "Error occurred while retrieving device IDs for owner: " + owner; log.error(msg, e); @@ -5388,6 +5377,41 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return ownerWithDeviceDTO; } +// @Override +// public OwnerWithDeviceDTO getOwnersWithDeviceIds(String owner, int deviceTypeId, String deviceOwner, String deviceName, String deviceStatus) +// throws DeviceManagementDAOException { +// int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); +// OwnerWithDeviceDTO ownerWithDeviceDTO; +// +// List allowingDeviceStatuses = new ArrayList<>(); +// allowingDeviceStatuses.add(EnrolmentInfo.Status.ACTIVE.toString()); +// allowingDeviceStatuses.add(EnrolmentInfo.Status.INACTIVE.toString()); +// allowingDeviceStatuses.add(EnrolmentInfo.Status.UNREACHABLE.toString()); +// +// try { +// DeviceManagementDAOFactory.openConnection(); +// ownerWithDeviceDTO = this.enrollmentDAO.getOwnersWithDevices(owner, allowingDeviceStatuses, tenantId, deviceTypeId, deviceOwner, deviceName, deviceStatus); +// if (ownerWithDeviceDTO == null) { +// String msg = "No data found for owner: " + owner; +// log.error(msg); +// throw new DeviceManagementDAOException(msg); +// } +// List deviceIds = ownerWithDeviceDTO.getDeviceIds(); +// if (deviceIds != null) { +// ownerWithDeviceDTO.setDeviceCount(deviceIds.size()); +// } else { +// ownerWithDeviceDTO.setDeviceCount(0); +// } +// } catch (DeviceManagementDAOException | SQLException e) { +// String msg = "Error occurred while retrieving device IDs for owner: " + owner; +// log.error(msg, e); +// throw new DeviceManagementDAOException(msg, e); +// } finally { +// DeviceManagementDAOFactory.closeConnection(); +// } +// return ownerWithDeviceDTO; +// } + @Override public OwnerWithDeviceDTO getOwnerWithDeviceByDeviceId(int deviceId, String deviceOwner, String deviceName, String deviceStatus)