Implemented getDeviceByStatus function

revert-70aa11f8
milanperera 10 years ago
parent eeca0d4ded
commit a5a9d02dee

@ -21,7 +21,7 @@ package org.wso2.carbon.device.mgt.common;
public class EnrolmentInfo {
public enum Status {
CREATED, ACTIVE, INACTIVE, UNREACHABLE, UNCLAIMED, SUSPENDED, BLOCKED, REMOVED
CREATED, ACTIVE, INACTIVE, UNREACHABLE, UNCLAIMED, SUSPENDED, BLOCKED, REMOVED, DISENROLLMENT_REQUESTED
}
public enum OwnerShip {

@ -60,5 +60,7 @@ public interface DeviceDAO {
EnrolmentInfo getEnrolment(DeviceIdentifier deviceId, String currentUser,
int tenantId) throws DeviceManagementDAOException;
List<Device> getDevicesByStatus(EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException;
}

@ -442,4 +442,32 @@ public class DeviceDAOImpl implements DeviceDAO {
return enrolmentInfo;
}
public List<Device> getDevicesByStatus(EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
List<Device> devices = new ArrayList<Device>();
try {
conn = this.getConnection();
stmt = conn.prepareStatement("SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME AS DEVICE_NAME, d.DEVICE_TYPE_ID, " +
"d.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
"e.DATE_OF_ENROLMENT FROM (SELECT e.ID, e.DEVICE_ID, e.OWNER, e.OWNERSHIP, e.STATUS, " +
"e.DATE_OF_ENROLMENT, e.DATE_OF_LAST_UPDATE FROM DM_ENROLMENT e WHERE TENANT_ID = ? " +
"AND STATUS = ?) e, DM_DEVICE d WHERE DEVICE_ID = e.DEVICE_ID AND d.TENANT_ID = ?");
stmt.setInt(1, tenantId);
stmt.setString(2, status.toString());
stmt.setInt(3, tenantId);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Device device = this.loadDevice(rs);
devices.add(device);
}
} 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);
}
return devices;
}
}

@ -79,4 +79,13 @@ public interface DeviceManagementProviderService extends DeviceManager, LicenseM
List<Device> getDevicesByName(String deviceName) throws DeviceManagementException;
void updateDeviceEnrolmentInfo(Device device, EnrolmentInfo.Status active) throws DeviceManagementException;
/**
* This method is used to retrieve list of devices based on the device status
*
* @param status Device status
* @return List of devices
* @throws DeviceManagementException
*/
List<Device> getDevicesByStatus(EnrolmentInfo.Status status) throws DeviceManagementException;
}

@ -699,4 +699,40 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
}
}
public List<Device> getDevicesByStatus(EnrolmentInfo.Status status) throws DeviceManagementException {
List<Device> devices = new ArrayList<Device>();
List<Device> allDevices;
try {
DeviceManagementDAOFactory.getConnection();
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
allDevices = deviceDAO.getDevicesByStatus(status, tenantId);
} catch (DeviceManagementDAOException e) {
String errorMsg = "Error occurred while fetching the list of devices that matches to status: '"
+ status + "'";
log.error(errorMsg, e);
throw new DeviceManagementException(errorMsg, e);
} finally {
try {
DeviceManagementDAOFactory.closeConnection();
} catch (DeviceManagementDAOException e) {
log.warn("Error occurred while closing the connection", e);
}
}
for (Device device : allDevices) {
Device dmsDevice =
this.getPluginRepository().getDeviceManagementService(
device.getType()).getDeviceManager().getDevice(
new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()));
if (dmsDevice != null) {
device.setFeatures(dmsDevice.getFeatures());
device.setProperties(dmsDevice.getProperties());
}
devices.add(device);
}
return devices;
}
}

Loading…
Cancel
Save