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 1a8888ae1bb..5ea2a87b5b6 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 @@ -185,11 +185,22 @@ public interface DeviceDAO { * Retrieves a list of devices based on a given criteria of properties * @param deviceProps properties by which devices need to be filtered * @param tenantId tenant id - * @return list of devices + * @return list of devices with properties * @throws DeviceManagementDAOException */ List getDeviceBasedOnDeviceProperties(Map deviceProps, int tenantId) throws DeviceManagementDAOException; + + /** + * Retrieves properties of given device identifier + * + * @param deviceId identifier of device that need to be retrieved + * @param tenantId tenant ID + * @return list of devices with properties + * @throws DeviceManagementDAOException + */ + Device getDeviceProps(String deviceId, int tenantId) throws DeviceManagementDAOException; + /** * This method is used to retrieve a device of a given device-identifier and tenant-id which modified * later than the ifModifiedSince param. 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 f6b42189ebb..8e9442f8d5c 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 @@ -39,7 +39,6 @@ package org.wso2.carbon.device.mgt.core.dao.impl; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; -import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.EnrolmentInfo.Status; import org.wso2.carbon.device.mgt.common.PaginationRequest; @@ -290,6 +289,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { return device; } + @Override public List getDeviceBasedOnDeviceProperties(Map deviceProps, int tenantId) throws DeviceManagementDAOException { Connection conn = null; @@ -318,7 +318,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { } List deviceIds = findIntersection(outputLists); for(String deviceId : deviceIds){ - devices.add(getDevice(deviceId, tenantId)); + devices.add(getDeviceProps(deviceId, tenantId)); } } catch (SQLException e) { String msg = "Error occurred while fetching devices against criteria : '" + deviceProps; @@ -330,6 +330,47 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { return devices; } + @Override + public Device getDeviceProps(String deviceId, int tenantId) throws DeviceManagementDAOException { + Connection conn = null; + PreparedStatement stmt = null; + Device device = null; + ResultSet resultSet = null; + String deviceType = null; + try { + conn = this.getConnection(); + stmt = conn.prepareStatement( + "SELECT * FROM DM_DEVICE_PROPERTIES WHERE DEVICE_IDENTIFICATION = ? AND TENANT_ID = ?"); + stmt.setString(1, deviceId); + stmt.setInt(2, tenantId); + resultSet = stmt.executeQuery(); + List properties = new ArrayList<>(); + while (resultSet.next()) { + Device.Property property = new Device.Property(); + property.setName(resultSet.getString(PROPERTY_KEY_COLUMN_NAME)); + property.setValue(resultSet.getString(PROPERTY_VALUE_COLUMN_NAME)); + properties.add(property); + //We are repeatedly assigning device type here. Yes. This was done intentionally, as there would be + //No other efficient/simple/inexpensive way of retrieving device type of a particular device from the database + //Note that device-identification will be unique across device types + deviceType = resultSet.getString(PROPERTY_DEVICE_TYPE_NAME); + } + device = new Device(); + device.setDeviceIdentifier(deviceId); + device.setType(deviceType); + device.setProperties(properties); + + } catch (SQLException e) { + String msg = "Error occurred while fetching properties for device : '" + deviceId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, resultSet); + } + + return device; + } + private List findIntersection(List> collections) { boolean first = true; List intersectedResult = new ArrayList<>();