diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java index 4dbd0bf9c81..3242ebec9b8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementServiceProviderImpl.java @@ -596,7 +596,48 @@ public class DeviceManagementServiceProviderImpl implements DeviceManagementServ int deviceCount = this.deviceDAO.getDeviceCount(); return deviceCount; } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException("Error occurred while obtaining devices all devices", e); + log.error("Error occurred while counting devices", e); + throw new DeviceManagementException("Error occurred while counting devices", e); + } + } + + @Override + public List getDevicesByName(String deviceName, int tenantId) throws DeviceManagementException { + List devicesOfUser = new ArrayList(); + List devicesList; + Device convertedDevice; + DeviceIdentifier deviceIdentifier; + DeviceManager dms; + Device dmsDevice; + org.wso2.carbon.device.mgt.core.dto.Device device; + + try { + devicesList = this.getDeviceDAO().getDevicesByName(deviceName, tenantId); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementException("Error occurred while fetching the list of devices that matches to '" + + deviceName + "'", e); + } + + for (int x = 0; x < devicesList.size(); x++) { + device = devicesList.get(x); + try { + device.setDeviceType(deviceTypeDAO.getDeviceType(device.getDeviceTypeId())); + dms = this.getPluginRepository().getDeviceManagementProvider(device.getDeviceType().getName()); + convertedDevice = DeviceManagementDAOUtil.convertDevice(device, device.getDeviceType()); + deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(device.getDeviceIdentificationId()); + deviceIdentifier.setType(device.getDeviceType().getName()); + dmsDevice = dms.getDevice(deviceIdentifier); + if (dmsDevice != null) { + convertedDevice.setProperties(dmsDevice.getProperties()); + convertedDevice.setFeatures(dmsDevice.getFeatures()); + } + devicesOfUser.add(convertedDevice); + } catch (DeviceManagementDAOException e) { + log.error("Error occurred while obtaining the device type of DeviceTypeId '" + + device.getDeviceTypeId() + "'", e); + } } + return devicesOfUser; } } 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 0379cd9ede3..1380194e282 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 @@ -62,8 +62,18 @@ public interface DeviceDAO { /** * Get the count of devices + * * @return device count * @throws DeviceManagementDAOException */ int getDeviceCount() throws DeviceManagementDAOException; + + /** + * Get the list of devices that matches with the given device name. + * + * @param deviceName Name of the device + * @return List of devices that matches with the given device name. + * @throws DeviceManagementDAOException + */ + List getDevicesByName(String deviceName , int tenantId) throws DeviceManagementDAOException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java index 1d97328c4a8..2a38f4acaa8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java @@ -330,4 +330,59 @@ public class DeviceDAOImpl implements DeviceDAO { return deviceCount; } + /** + * Get the list of devices that matches with the given device name. + * + * @param deviceName Name of the device. + * @param tenantId + * @return device list + * @throws DeviceManagementDAOException + */ + @Override + public List getDevicesByName(String deviceName, int tenantId) throws DeviceManagementDAOException { + Connection conn = this.getConnection(); + PreparedStatement stmt = null; + List deviceList = new ArrayList(); + try { + stmt = conn.prepareStatement( + "SELECT DM_DEVICE_TYPE.ID, DM_DEVICE_TYPE.NAME, DM_DEVICE.ID, DM_DEVICE.DESCRIPTION, " + + "DM_DEVICE.NAME, DM_DEVICE.DATE_OF_ENROLLMENT, DM_DEVICE.DATE_OF_LAST_UPDATE, " + + "DM_DEVICE.OWNERSHIP, DM_DEVICE.STATUS, DM_DEVICE.DEVICE_TYPE_ID, " + + "DM_DEVICE.DEVICE_IDENTIFICATION, DM_DEVICE.OWNER, DM_DEVICE.TENANT_ID FROM " + + "DM_DEVICE, DM_DEVICE_TYPE WHERE DM_DEVICE.DEVICE_TYPE_ID = DM_DEVICE_TYPE.ID " + + "AND DM_DEVICE.NAME LIKE %?% AND DM_DEVICE.TENANT_ID =?"); + stmt.setString(1, deviceName); + stmt.setInt(2, tenantId); + ResultSet resultSet = stmt.executeQuery(); + + while (resultSet.next()) { + Device device = new Device(); + DeviceType deviceType = new DeviceType(); + int id = resultSet.getInt(resultSet.getInt(1)); + deviceType.setId(id); + deviceType.setName(resultSet.getString(2)); + device.setId(resultSet.getInt(3)); + device.setDescription(resultSet.getString(4)); + device.setName(resultSet.getString(5)); + device.setDateOfEnrollment(resultSet.getLong(6)); + device.setDateOfLastUpdate(resultSet.getLong(7)); + //TODO:- Ownership is not a enum in DeviceDAO + device.setOwnerShip(resultSet.getString(8)); + device.setStatus(Status.valueOf(resultSet.getString(9))); + device.setDeviceTypeId(resultSet.getInt(10)); + device.setDeviceIdentificationId(resultSet.getString(11)); + device.setOwnerId(resultSet.getString(12)); + device.setTenantId(resultSet.getInt(13)); + deviceList.add(device); + } + } catch (SQLException e) { + String msg = "Error occurred while fetching the list of devices that matches to '" + deviceName + "'"; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } finally { + DeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + } + return deviceList; + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementService.java index 6b9820068ef..27b0d700c90 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementService.java @@ -73,7 +73,18 @@ public interface DeviceManagementService extends DeviceManager, LicenseManager, /** * Method to get the count of all types of devices. * @return device count - * @throws DeviceManagementException + * @throws DeviceManagementException If some unusual behaviour is observed while counting + * the devices */ int getDeviceCount() throws DeviceManagementException; + + /** + * Method to get the list of devices that matches with the given device name. + * + * @param deviceName name of the device + * @return List of devices that matches with the given device name. + * @throws DeviceManagementException If some unusual behaviour is observed while fetching the + * device list + */ + List getDevicesByName(String deviceName, int tenantId) throws DeviceManagementException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java index a76cc2f9ede..3b15a363895 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementServiceImpl.java @@ -211,4 +211,10 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider() .getDeviceCount(); } + + @Override + public List getDevicesByName(String deviceName, int tenantId) throws DeviceManagementException { + return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider() + .getDevicesByName(deviceName, tenantId); + } }