diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManager.java index 1f2c4df3763..9dd432c8701 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManager.java @@ -55,4 +55,7 @@ public interface DeviceManager { public OperationManager getOperationManager(String type) throws DeviceManagementException; + public List getDeviceListOfUser(String username) + throws DeviceManagementException, DeviceManagementDAOException; + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagerImpl.java index 64ebbf0e464..f1232fb6581 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagerImpl.java @@ -203,4 +203,21 @@ public class DeviceManagerImpl implements DeviceManager { public DeviceManagementRepository getPluginRepository() { return pluginRepository; } + + @Override + public List getDeviceListOfUser(String username) + throws DeviceManagementException, DeviceManagementDAOException { + List devicesList = this.deviceDAO.getDeviceListOfUser(username); + List devicesOfUser=new ArrayList(); + for(int x=0;x getDevices(Integer type) throws DeviceManagementDAOException; + + /** + * Get the list of devices belongs to a user. + * @param username Requested user. + * @return List of devices of the user. + * @throws DeviceManagementDAOException + */ + List getDeviceListOfUser(String username) 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 54010a85546..f4a08f107f5 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 @@ -24,6 +24,7 @@ import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.core.dto.Device; +import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.Status; import javax.sql.DataSource; @@ -76,6 +77,39 @@ public class DeviceDAOImpl implements DeviceDAO { } } + @Override public List getDeviceListOfUser(String username) + 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, DEVICE_TYPE_ID, DEVICE_IDENTIFICATION FROM DM_DEVICE, DM_DEVICE_TYPE WHERE DM_DEVICE.DEVICE_TYPE_ID =DM_DEVICE_TYPE.ID AND DM_DEVICE.OWNER =?"); + stmt.setString(1, username); + ResultSet results = stmt.executeQuery(); + + while (results.next()) { + Device device = new Device(); + DeviceType deviceType=new DeviceType(); + int id=results.getInt(results.getInt(1)); + deviceType.setId((long) id); + deviceType.setName(results.getString(2)); + device.setId(results.getInt(3)); + device.setDeviceType(deviceType); + device.setDeviceTypeId(results.getInt(4)); + device.setDeviceIdentificationId(results.getString(5)); + deviceList.add(device); + } + } catch (SQLException e) { + String msg = "Error occurred while fetching the list of devices belongs to " + username; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } finally { + DeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + } + return deviceList; + } + @Override public void updateDevice(Device device) throws DeviceManagementDAOException { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/Device.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/Device.java index fae390ac6b0..f98c9fda860 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/Device.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/Device.java @@ -34,6 +34,15 @@ public class Device implements Serializable { private String ownerShip; private int tenantId; private Integer deviceTypeId; + private DeviceType deviceType; + + public DeviceType getDeviceType() { + return deviceType; + } + + public void setDeviceType(DeviceType deviceType) { + this.deviceType = deviceType; + } public Integer getDeviceTypeId() { return deviceTypeId; 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 cb908edd7ea..42459187361 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 @@ -22,6 +22,7 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.OperationManager; import org.wso2.carbon.device.mgt.core.DeviceManager; +import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import java.util.List; @@ -93,4 +94,9 @@ public class DeviceManagementService implements DeviceManager { getOperationManager(type); } + + @Override public List getDeviceListOfUser(String username) throws DeviceManagementException,DeviceManagementDAOException { + return DeviceManagementDataHolder.getInstance().getDeviceManager().getDeviceListOfUser(username); + } + }