From 8b6eee0a97f1173d09c31e795002af48fc6bcd3c Mon Sep 17 00:00:00 2001 From: Oshani Silva Date: Wed, 17 Jul 2024 05:42:24 +0000 Subject: [PATCH 1/6] Add fix for group subscription table Co-authored-by: Oshani Silva Co-committed-by: Oshani Silva --- .../common/CategorizedSubscriptionResult.java | 87 +++++++++++++++++++ .../mgt/core/dao/SubscriptionDAO.java | 12 +++ .../GenericSubscriptionDAOImpl.java | 46 +++++++++- .../core/impl/SubscriptionManagerImpl.java | 48 +++++++++- .../core/dao/impl/AbstractGroupDAOImpl.java | 12 ++- 5 files changed, 194 insertions(+), 11 deletions(-) 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/CategorizedSubscriptionResult.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/CategorizedSubscriptionResult.java index 18da5a7736..e685284b77 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/CategorizedSubscriptionResult.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/CategorizedSubscriptionResult.java @@ -23,10 +23,15 @@ import java.util.List; public class CategorizedSubscriptionResult { private List installedDevices; + private int installedDevicesCount; private List pendingDevices; + private int pendingDevicesCount; private List errorDevices; + private int errorDevicesCount; private List newDevices; + private int newDevicesCount; private List subscribedDevices; + private int subscribedDevicesCount; public CategorizedSubscriptionResult(List installedDevices, List pendingDevices, @@ -61,6 +66,48 @@ public class CategorizedSubscriptionResult { this.subscribedDevices = subscribedDevices; } + public CategorizedSubscriptionResult(List installedDevices, + List pendingDevices, + List errorDevices, + List newDevices, + int installedDevicesCount, + int pendingDevicesCount, + int errorDevicesCount, + int newDevicesCount + ) { + this.installedDevices = installedDevices; + this.pendingDevices = pendingDevices; + this.errorDevices = errorDevices; + this.newDevices = newDevices; + this.subscribedDevices = null; + this.installedDevicesCount = installedDevicesCount; + this.pendingDevicesCount = pendingDevicesCount; + this.errorDevicesCount = errorDevicesCount; + this.newDevicesCount = newDevicesCount; + this.subscribedDevicesCount = 0; + } + + public CategorizedSubscriptionResult(List installedDevices, + List pendingDevices, + List errorDevices, + List newDevices, + List subscribedDevices, int installedDevicesCount, + int pendingDevicesCount, + int errorDevicesCount, + int newDevicesCount, + int subscribedDevicesCount) { + this.installedDevices = installedDevices; + this.pendingDevices = pendingDevices; + this.errorDevices = errorDevices; + this.newDevices = newDevices; + this.subscribedDevices = subscribedDevices; + this.installedDevicesCount = installedDevicesCount; + this.pendingDevicesCount = pendingDevicesCount; + this.errorDevicesCount = errorDevicesCount; + this.newDevicesCount = newDevicesCount; + this.subscribedDevicesCount = subscribedDevicesCount; + } + public CategorizedSubscriptionResult(List devices, String tabActionStatus) { switch (tabActionStatus) { case "COMPLETED": @@ -127,4 +174,44 @@ public class CategorizedSubscriptionResult { public void setSubscribedDevices(List subscribedDevices) { this.subscribedDevices = subscribedDevices; } + + public int getInstalledDevicesCount() { + return installedDevicesCount; + } + + public void setInstalledDevicesCount(int installedDevicesCount) { + this.installedDevicesCount = installedDevicesCount; + } + + public int getPendingDevicesCount() { + return pendingDevicesCount; + } + + public void setPendingDevicesCount(int pendingDevicesCount) { + this.pendingDevicesCount = pendingDevicesCount; + } + + public int getErrorDevicesCount() { + return errorDevicesCount; + } + + public void setErrorDevicesCount(int errorDevicesCount) { + this.errorDevicesCount = errorDevicesCount; + } + + public int getNewDevicesCount() { + return newDevicesCount; + } + + public void setNewDevicesCount(int newDevicesCount) { + this.newDevicesCount = newDevicesCount; + } + + public int getSubscribedDevicesCount() { + return subscribedDevicesCount; + } + + public void setSubscribedDevicesCount(int subscribedDevicesCount) { + this.subscribedDevicesCount = subscribedDevicesCount; + } } 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..498221eb68 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 @@ -517,4 +517,16 @@ public interface SubscriptionDAO { * @throws ApplicationManagementDAOException if connection establishment or SQL execution fails. */ int getUserUnsubscriptionCount(int appReleaseId, int tenantId) throws ApplicationManagementDAOException; + + /** + * This method is used to get the counts of devices related to a UUID. + * + * @param appReleaseId the UUID of the application release. + * @param tenantId id of the current tenant. + * @param actionStatus categorized status. + * @param actionTriggeredFrom type of the action. + * @return {@link int} which contains the count of the subscription type + * @throws ApplicationManagementDAOException if connection establishment or SQL execution fails. + */ + int countSubscriptionsByStatus(int appReleaseId, int tenantId, String actionStatus, String actionTriggeredFrom) 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 8ffb95f039..2241a0b989 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 @@ -1655,14 +1655,22 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc "GS.UNSUBSCRIBED_BY, GS.UNSUBSCRIBED_TIMESTAMP, GS.AP_APP_RELEASE_ID " + "FROM AP_GROUP_SUBSCRIPTION GS " + "WHERE GS.AP_APP_RELEASE_ID = ? AND GS.UNSUBSCRIBED = ? AND GS.TENANT_ID = ? " + - "ORDER BY " + subscriptionStatusTime + " DESC " + - "LIMIT ? OFFSET ?"; + "ORDER BY " + subscriptionStatusTime + " DESC "; + + // Append limit and offset only if limit is not -1 + if (limit != -1) { + sql = sql + " LIMIT ? OFFSET ?"; + } try (PreparedStatement ps = conn.prepareStatement(sql)) { ps.setInt(1, appReleaseId); ps.setBoolean(2, unsubscribe); ps.setInt(3, tenantId); - ps.setInt(4, limit); - ps.setInt(5, offset); + + // Set limit and offset parameters only if limit is not -1 + if (limit != -1) { + ps.setInt(4, limit); + ps.setInt(5, offset); + } try (ResultSet rs = ps.executeQuery()) { GroupSubscriptionDTO groupDetail; @@ -2487,4 +2495,34 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc throw new ApplicationManagementDAOException(msg, e); } } + + public int countSubscriptionsByStatus(int appReleaseId, int tenantId, String actionStatus, String actionTriggeredFrom) throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to count device subscriptions by status and actionTriggeredFrom for the given AppReleaseID."); + } + try { + Connection conn = this.getDBConnection(); + String sql = "SELECT COUNT(*) FROM AP_DEVICE_SUBSCRIPTION WHERE AP_APP_RELEASE_ID = ? AND TENANT_ID = ? AND STATUS = ? AND ACTION_TRIGGERED_FROM = ?"; + try (PreparedStatement ps = conn.prepareStatement(sql)) { + ps.setInt(1, appReleaseId); + ps.setInt(2, tenantId); + ps.setString(3, actionStatus); + ps.setString(4, actionTriggeredFrom); + try (ResultSet rs = ps.executeQuery()) { + if (rs.next()) { + return rs.getInt(1); + } + } + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection to count device subscriptions by status and action trigger."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "SQL Error occurred while counting device subscriptions by status and action trigger."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + return 0; + } } 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..b7dc62a0fc 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 @@ -1725,7 +1725,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { List groupDetailsWithDevices = new ArrayList<>(); List groupDetails = - subscriptionDAO.getGroupsSubscriptionDetailsByAppReleaseID(appReleaseId, unsubscribe, tenantId, offset, limit); + subscriptionDAO.getGroupsSubscriptionDetailsByAppReleaseID(appReleaseId, unsubscribe, tenantId, offset, -1); if (groupDetails == null) { throw new ApplicationManagementException("Group details not found for appReleaseId: " + appReleaseId); } @@ -1744,7 +1744,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { GroupDetailsDTO groupDetailWithDevices = groupManagementProviderService.getGroupDetailsWithDevices( groupName, applicationDTO.getDeviceTypeId(), request.getOwner(), - request.getDeviceName(), request.getDeviceStatus(), offset, limit); + request.getDeviceName(), request.getDeviceStatus(), offset, -1); SubscriptionsDTO groupDetailDTO = new SubscriptionsDTO(); groupDetailDTO.setId(groupDetailWithDevices.getGroupId()); @@ -1878,33 +1878,73 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } List requestedDevices = new ArrayList<>(); + int installedCount; + int pendingCount; + int errorCount; + int newCount; + int subscribedCount; + int totalDeviceCount = + groupManagementProviderService.getDeviceCount(groupDetailWithDevices.getGroupId()); + if (StringUtils.isNotBlank(request.getTabActionStatus())) { switch (request.getTabActionStatus()) { case "COMPLETED": requestedDevices = installedDevices; + installedCount = subscriptionDAO.countSubscriptionsByStatus(appReleaseId, tenantId, request.getTabActionStatus(), request.getActionType()); break; case "PENDING": requestedDevices = pendingDevices; + pendingCount = subscriptionDAO.countSubscriptionsByStatus(appReleaseId, tenantId, request.getTabActionStatus(), request.getActionType()); break; case "ERROR": requestedDevices = errorDevices; + errorCount = subscriptionDAO.countSubscriptionsByStatus(appReleaseId, tenantId, request.getTabActionStatus(), request.getActionType()); break; case "NEW": requestedDevices = newDevices; break; case "SUBSCRIBED": requestedDevices = subscribedDevices; + subscribedCount = subscriptionDAO.countSubscriptionsByStatus(appReleaseId, tenantId, request.getTabActionStatus(), request.getActionType()); break; } groupDetailDTO.setDevices(new CategorizedSubscriptionResult(requestedDevices, request.getTabActionStatus())); } else { CategorizedSubscriptionResult categorizedSubscriptionResult; + + installedCount = subscriptionDAO.countSubscriptionsByStatus(appReleaseId, tenantId, "COMPLETED", request.getActionType()); + pendingCount = subscriptionDAO.countSubscriptionsByStatus(appReleaseId, tenantId, "PENDING", request.getActionType()); + errorCount = subscriptionDAO.countSubscriptionsByStatus(appReleaseId, tenantId, "ERROR", request.getActionType()); + subscribedCount = subscriptionDAO.countSubscriptionsByStatus(appReleaseId, tenantId, "SUBSCRIBED", request.getActionType()); + newCount = totalDeviceCount - (installedCount + pendingCount + errorCount + subscribedCount); + + List paginatedInstalledDevices = installedDevices.stream() + .skip(offset) + .limit(limit) + .collect(Collectors.toList()); + List paginatedPendingDevices = pendingDevices.stream() + .skip(offset) + .limit(limit) + .collect(Collectors.toList()); + List paginatedErrorDevices = errorDevices.stream() + .skip(offset) + .limit(limit) + .collect(Collectors.toList()); + List paginatedNewDevices = newDevices.stream() + .skip(offset) + .limit(limit) + .collect(Collectors.toList()); + List paginatedSubscribedDevices = subscribedDevices.stream() + .skip(offset) + .limit(limit) + .collect(Collectors.toList()); + if (subscribedDevices.isEmpty()) { categorizedSubscriptionResult = - new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices); + new CategorizedSubscriptionResult(paginatedInstalledDevices, paginatedPendingDevices, paginatedErrorDevices, paginatedNewDevices, installedCount, pendingCount, errorCount, newCount); } else { categorizedSubscriptionResult = - new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices, subscribedDevices); + new CategorizedSubscriptionResult(paginatedInstalledDevices, paginatedPendingDevices, paginatedErrorDevices, paginatedNewDevices, paginatedSubscribedDevices, installedCount, pendingCount, errorCount, newCount, subscribedCount); } groupDetailDTO.setDevices(categorizedSubscriptionResult); } 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/AbstractGroupDAOImpl.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/AbstractGroupDAOImpl.java index 1eba6c1a7e..cfa706b0e0 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/AbstractGroupDAOImpl.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/AbstractGroupDAOImpl.java @@ -1495,7 +1495,10 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { sql.append(" AND e.STATUS = ?"); } - sql.append(" LIMIT ? OFFSET ?"); + // Append limit and offset only if limit is not -1 + if (limit != -1) { + sql.append(" LIMIT ? OFFSET ?"); + } Connection conn = null; try { @@ -1519,8 +1522,11 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { stmt.setString(index++, deviceStatus); } - stmt.setInt(index++, limit); - stmt.setInt(index++, offset); + // Set limit and offset parameters only if limit is not -1 + if (limit != -1) { + stmt.setInt(index++, limit); + stmt.setInt(index++, offset); + } try (ResultSet rs = stmt.executeQuery()) { while (rs.next()) { From 7be828a55607fd1874b519c301c1b8ac3ffe4860 Mon Sep 17 00:00:00 2001 From: waruni Date: Wed, 17 Jul 2024 11:52:35 +0530 Subject: [PATCH 2/6] Add EVENT REVOKE operation for groupIds to delete --- .../GeoLocationProviderServiceImpl.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) 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/geo/service/GeoLocationProviderServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java index 9b380f14ee..f11914d5ea 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java @@ -1528,7 +1528,11 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic @Override public boolean updateGeofence(GeofenceData geofenceData, int fenceId) throws GeoLocationBasedServiceException, EventConfigurationException { + EventConfigurationProviderService eventConfigService; + eventConfigService = DeviceManagementDataHolder.getInstance().getEventConfigurationService(); int tenantId; + List groupIdsToDelete = new ArrayList<>(); + List groupIdsToAdd = new ArrayList<>(); try { tenantId = DeviceManagementDAOUtil.getTenantId(); } catch (DeviceManagementDAOException e) { @@ -1543,8 +1547,6 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic int updatedRowCount = geofenceDAO.updateGeofence(geofenceData, fenceId); savedGroupIds = geofenceDAO.getGroupIdsOfGeoFence(fenceId); geofenceData.setId(fenceId); - List groupIdsToDelete = new ArrayList<>(); - List groupIdsToAdd = new ArrayList<>(); for (Integer savedGroupId : savedGroupIds) { if (!geofenceData.getGroupIds().contains(savedGroupId)) { groupIdsToDelete.add(savedGroupId); @@ -1558,6 +1560,18 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic geofenceDAO.deleteGeofenceGroupMapping(groupIdsToDelete, fenceId); geofenceDAO.createGeofenceGroupMapping(geofenceData, groupIdsToAdd); EventManagementDAOFactory.commitTransaction(); + try { + if (!groupIdsToDelete.isEmpty()) { + eventConfigService.createEventOperationTask(OperationMgtConstants.OperationCodes.EVENT_REVOKE, + DeviceManagementConstants.EventServices.GEOFENCE, + new GeoFenceEventMeta(geofenceData), tenantId, groupIdsToDelete); + } + } catch (EventConfigurationException e) { + String msg = "Failed while creating EVENT_REVOKE operation creation task entry while updating geo fence " + + geofenceData.getFenceName() + " of the tenant " + tenantId; + log.error(msg, e); + throw new GeoLocationBasedServiceException(msg, e); + } if (updatedRowCount > 0) { GeoCacheManagerImpl.getInstance().updateGeoFenceInCache(geofenceData, fenceId, tenantId); } From 8a8846be5af3a93752920fd9f1dec6f785876a48 Mon Sep 17 00:00:00 2001 From: pramilaniroshan Date: Thu, 18 Jul 2024 12:26:19 +0530 Subject: [PATCH 3/6] Fix devices not loading in web apps --- .../mgt/core/dao/impl/AbstractEnrollmentDAOImpl.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) 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..f826980fae 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 @@ -731,8 +731,11 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { StringBuilder sql = new StringBuilder("SELECT e.DEVICE_ID, e.OWNER, e.STATUS, e.DEVICE_TYPE, e.DEVICE_IDENTIFICATION " + "FROM DM_ENROLMENT e " + "JOIN DM_DEVICE d ON e.DEVICE_ID = d.ID " + - "WHERE e.TENANT_ID = ? AND e.STATUS IN (" + deviceFilters.toString() + ") AND d.DEVICE_TYPE_ID = ?"); + "WHERE e.TENANT_ID = ? AND e.STATUS IN (" + deviceFilters.toString() + ")"); + if (deviceTypeId != 0) { + sql.append(" AND d.DEVICE_TYPE_ID = ?"); + } if (deviceOwner != null && !deviceOwner.isEmpty()) { sql.append(" AND e.OWNER LIKE ?"); } @@ -750,7 +753,9 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { for (String status : allowingDeviceStatuses) { stmt.setString(index++, status); } - stmt.setInt(index++, deviceTypeId); + if (deviceTypeId != 0) { + stmt.setInt(index++, deviceTypeId); + } if (deviceOwner != null && !deviceOwner.isEmpty()) { stmt.setString(index++, "%" + deviceOwner + "%"); From 7b0f12b890078e6265174e8849fdf5bc22cd9c0b Mon Sep 17 00:00:00 2001 From: pramilaniroshan Date: Thu, 18 Jul 2024 13:45:39 +0530 Subject: [PATCH 4/6] Fix devices not loading in web apps --- .../dao/impl/AbstractEnrollmentDAOImpl.java | 18 ++++++++++++++---- .../core/dao/impl/AbstractGroupDAOImpl.java | 9 ++++++--- 2 files changed, 20 insertions(+), 7 deletions(-) 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..bca05c6b18 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 @@ -589,8 +589,11 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { "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 + ")"); + "WHERE e.OWNER = ? AND e.TENANT_ID = ? AND e.STATUS IN (" + deviceFilters + ")"); + if (deviceTypeId != 0) { + sql.append(" AND d.DEVICE_TYPE_ID = ?"); + } if (deviceOwner != null && !deviceOwner.isEmpty()) { sql.append(" AND e.OWNER LIKE ?"); } @@ -607,10 +610,12 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { int index = 1; stmt.setString(index++, owner); stmt.setInt(index++, tenantId); - stmt.setInt(index++, deviceTypeId); for (String status : allowingDeviceStatuses) { stmt.setString(index++, status); } + if (deviceTypeId != 0) { + stmt.setInt(index++, deviceTypeId); + } if (deviceOwner != null && !deviceOwner.isEmpty()) { stmt.setString(index++, "%" + deviceOwner + "%"); @@ -731,8 +736,11 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { StringBuilder sql = new StringBuilder("SELECT e.DEVICE_ID, e.OWNER, e.STATUS, e.DEVICE_TYPE, e.DEVICE_IDENTIFICATION " + "FROM DM_ENROLMENT e " + "JOIN DM_DEVICE d ON e.DEVICE_ID = d.ID " + - "WHERE e.TENANT_ID = ? AND e.STATUS IN (" + deviceFilters.toString() + ") AND d.DEVICE_TYPE_ID = ?"); + "WHERE e.TENANT_ID = ? AND e.STATUS IN (" + deviceFilters.toString() + ")"); + if (deviceTypeId != 0) { + sql.append(" AND d.DEVICE_TYPE_ID = ?"); + } if (deviceOwner != null && !deviceOwner.isEmpty()) { sql.append(" AND e.OWNER LIKE ?"); } @@ -750,7 +758,9 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO { for (String status : allowingDeviceStatuses) { stmt.setString(index++, status); } - stmt.setInt(index++, deviceTypeId); + if (deviceTypeId != 0) { + stmt.setInt(index++, deviceTypeId); + } if (deviceOwner != null && !deviceOwner.isEmpty()) { stmt.setString(index++, "%" + deviceOwner + "%"); 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/AbstractGroupDAOImpl.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/AbstractGroupDAOImpl.java index cfa706b0e0..f747321fc4 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/AbstractGroupDAOImpl.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/AbstractGroupDAOImpl.java @@ -1482,9 +1482,11 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { "WHERE " + " g.GROUP_NAME = ? " + " AND g.TENANT_ID = ? " + - " AND d.DEVICE_TYPE_ID = ? " + " AND e.STATUS IN (" + statusPlaceholders + ")"); + if (deviceTypeId != 0) { + sql.append(" AND d.DEVICE_TYPE_ID = ?"); + } if (deviceOwner != null && !deviceOwner.isEmpty()) { sql.append(" AND e.OWNER LIKE ?"); } @@ -1507,11 +1509,12 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { int index = 1; stmt.setString(index++, groupName); stmt.setInt(index++, tenantId); - stmt.setInt(index++, deviceTypeId); for (String status : allowedStatuses) { stmt.setString(index++, status); } - + if (deviceTypeId != 0) { + stmt.setInt(index++, deviceTypeId); + } if (deviceOwner != null && !deviceOwner.isEmpty()) { stmt.setString(index++, "%" + deviceOwner + "%"); } From 71eae764ce2d0d23a594341a08b0ed8bbfce2d49 Mon Sep 17 00:00:00 2001 From: navodzoysa Date: Mon, 22 Jul 2024 02:07:16 +0530 Subject: [PATCH 5/6] Fix stream corrupted exception for profile type operations --- .../mgt/api/jaxrs/beans/ApplicationUninstallation.java | 5 +++++ .../service/impl/DeviceManagementServiceImpl.java | 10 +++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/ApplicationUninstallation.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/ApplicationUninstallation.java index f78d15966e..eee5f06c49 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/ApplicationUninstallation.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/beans/ApplicationUninstallation.java @@ -18,6 +18,7 @@ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans; +import com.google.gson.Gson; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; @@ -113,4 +114,8 @@ public class ApplicationUninstallation { public void setType(String type) { this.type = type; } + + public String toJson() { + return new Gson().toJson(this); + } } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/DeviceManagementServiceImpl.java index b514277817..88cfe125ca 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -19,7 +19,6 @@ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl; import com.fasterxml.jackson.databind.ObjectMapper; -import com.google.gson.Gson; import io.entgra.device.mgt.core.application.mgt.common.ApplicationInstallResponse; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionType; import io.entgra.device.mgt.core.application.mgt.common.exception.SubscriptionManagementException; @@ -44,6 +43,7 @@ import org.wso2.carbon.context.PrivilegedCarbonContext; import io.entgra.device.mgt.core.device.mgt.common.*; import io.entgra.device.mgt.core.device.mgt.common.app.mgt.Application; import io.entgra.device.mgt.core.device.mgt.common.app.mgt.ApplicationManagementException; +import io.entgra.device.mgt.core.device.mgt.common.app.mgt.MobileAppTypes; import io.entgra.device.mgt.core.device.mgt.common.authorization.DeviceAccessAuthorizationException; import io.entgra.device.mgt.core.device.mgt.common.authorization.DeviceAccessAuthorizationService; import io.entgra.device.mgt.core.device.mgt.common.device.details.DeviceData; @@ -1112,7 +1112,6 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { @QueryParam("version") String version, @QueryParam("user") String user) { List deviceIdentifiers = new ArrayList<>(); - Operation operation = new Operation(); try { RequestValidationUtil.validateDeviceIdentifier(type, id); Device device = DeviceMgtAPIUtils.getDeviceManagementService().getDevice(id, false); @@ -1133,11 +1132,12 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { //if the applications not installed via entgra store } else { if (Constants.ANDROID.equals(type)) { - ApplicationUninstallation applicationUninstallation = new ApplicationUninstallation(packageName, "PUBLIC", name, platform, version, user); - Gson gson = new Gson(); + ApplicationUninstallation applicationUninstallation = new ApplicationUninstallation(packageName, + MobileAppTypes.PUBLIC.toString(), name, platform, version, user); + ProfileOperation operation = new ProfileOperation(); operation.setCode(MDMAppConstants.AndroidConstants.UNMANAGED_APP_UNINSTALL); operation.setType(Operation.Type.PROFILE); - operation.setPayLoad(gson.toJson(applicationUninstallation)); + operation.setPayLoad(applicationUninstallation.toJson()); DeviceManagementProviderService deviceManagementProviderService = HelperUtil .getDeviceManagementProviderService(); Activity activity = deviceManagementProviderService.addOperation( From fa400f8995a7b0d2c803335f77c6bf074b985d8d Mon Sep 17 00:00:00 2001 From: prathabanKavin Date: Mon, 22 Jul 2024 09:56:37 +0530 Subject: [PATCH 6/6] Fix space in getAppCount query --- .../core/dao/impl/application/GenericApplicationDAOImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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/application/GenericApplicationDAOImpl.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/application/GenericApplicationDAOImpl.java index 618149cd72..86b7407378 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/application/GenericApplicationDAOImpl.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/application/GenericApplicationDAOImpl.java @@ -313,7 +313,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic sql += " AND AP_APP_RELEASE.CURRENT_STATE = ?"; } if (deviceTypeId != -1) { - sql += "AND (AP_APP.DEVICE_TYPE_ID = ? "; + sql += " AND (AP_APP.DEVICE_TYPE_ID = ? "; if (filter.isWithWebApps()) { sql += "OR AP_APP.DEVICE_TYPE_ID = 0 "; }