diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java index b1f2ffb043..d2775ae772 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java @@ -76,6 +76,17 @@ public interface DeviceDAO { */ int getDeviceCountByStatus(String status, int tenantId) throws DeviceManagementDAOException; + /** + * This method is used to get the device count by status and type. + * + * @param deviceType device type name. + * @param status enrollment status. + * @param tenantId tenant id. + * @return returns the device count of given status. + * @throws DeviceManagementDAOException + */ + int getDeviceCountByStatus(String deviceType, String status, int tenantId) throws DeviceManagementDAOException; + /** * This method is used to get the device count by ownership. * diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index 2d0d877c03..1067b4a5ce 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -769,8 +769,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { try { conn = this.getConnection(); String sql = "SELECT COUNT(d.ID) AS DEVICE_COUNT FROM (SELECT e.DEVICE_ID FROM DM_ENROLMENT e WHERE " + - "TENANT_ID = ? AND STATUS = ?) e, DM_DEVICE d, " + - "DM_DEVICE_TYPE t WHERE d.ID = e.DEVICE_ID AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?"; + "TENANT_ID = ? AND STATUS = ?) e, DM_DEVICE d, " + + "DM_DEVICE_TYPE t WHERE d.ID = e.DEVICE_ID AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); stmt.setString(2, status); @@ -782,7 +782,36 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches to status " + - "'" + status + "'", e); + "'" + status + "'", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, null); + } + return deviceCount; + } + + @Override + public int getDeviceCountByStatus(String deviceType, String status, int tenantId) throws DeviceManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + int deviceCount = 0; + try { + conn = this.getConnection(); + String sql = "SELECT COUNT(d.ID) AS DEVICE_COUNT FROM (SELECT e.DEVICE_ID FROM DM_ENROLMENT e WHERE " + + "TENANT_ID = ? AND STATUS = ?) e, DM_DEVICE d, " + + "DM_DEVICE_TYPE t WHERE t.NAME = ? AND d.ID = e.DEVICE_ID AND d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, tenantId); + stmt.setString(2, status); + stmt.setString(3, deviceType); + stmt.setInt(4, tenantId); + ResultSet rs = stmt.executeQuery(); + + if (rs.next()) { + deviceCount = rs.getInt("DEVICE_COUNT"); + } + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches to status " + + "'" + status + "'", e); } finally { DeviceManagementDAOUtil.cleanupResources(stmt, null); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java index 9786b8f73d..2280d2bb3c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java @@ -465,6 +465,29 @@ public interface DeviceManagementProviderService { */ int getDeviceCount() throws DeviceManagementException; + /** + * Method to get the count of devices with given status and type. + * + * @param deviceType Device type name + * @param status Device status + * + * @return device count + * @throws DeviceManagementException If some unusual behaviour is observed while counting + * the devices + */ + int getDeviceCount(String deviceType, EnrolmentInfo.Status status) throws DeviceManagementException; + + /** + * Method to get the count of all types of devices with given status. + * + * @param status Device status + * + * @return device count + * @throws DeviceManagementException If some unusual behaviour is observed while counting + * the devices + */ + int getDeviceCount(EnrolmentInfo.Status status) throws DeviceManagementException; + HashMap getTenantedDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementException; void sendEnrolmentInvitation(String templateName, EmailMetaInfo metaInfo) throws DeviceManagementException, diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index a22242796c..d5cf4e769b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -1922,6 +1922,57 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } } + @Override + public int getDeviceCount(String deviceType, EnrolmentInfo.Status status) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Get devices count for type '" + deviceType + "' and status: " + status.toString()); + } + try { + DeviceManagementDAOFactory.openConnection(); + return deviceDAO.getDeviceCountByStatus(deviceType, status.toString(), this.getTenantId()); + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred while retrieving the device count for type '" + deviceType + + "' and status: " + status.toString(); + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred in getDeviceCount for type '" + deviceType + "' and status: " + status.toString(); + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Override + public int getDeviceCount(EnrolmentInfo.Status status) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Get devices count status: " + status.toString()); + } + try { + DeviceManagementDAOFactory.openConnection(); + return deviceDAO.getDeviceCountByStatus(status.toString(), this.getTenantId()); + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred while retrieving the device count"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred in getDeviceCount status: " + status.toString(); + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + @Override public List getDevicesByNameAndType(PaginationRequest request, boolean requireDeviceInfo) throws DeviceManagementException { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceTest.java index 22d7098c74..ffff69c6a5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceTest.java @@ -301,7 +301,7 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes @Test(expectedExceptions = DeviceManagementException.class) public void testGetDeviceCountForNullUser() throws DeviceManagementException { - deviceMgtService.getDeviceCount(null); + deviceMgtService.getDeviceCount((String) null); } @Test(dependsOnMethods = {"testSuccessfulDeviceEnrollment"})