Add methods to get device count based on status and device type

merge-requests/38/head
Charitha Goonetilleke 6 years ago
parent cbe85378d8
commit 75dd9321af

@ -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.
*

@ -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);
}

@ -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<Integer, Device> getTenantedDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementException;
void sendEnrolmentInvitation(String templateName, EmailMetaInfo metaInfo) throws DeviceManagementException,

@ -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<Device> getDevicesByNameAndType(PaginationRequest request, boolean requireDeviceInfo)
throws DeviceManagementException {

@ -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"})

Loading…
Cancel
Save