From 4754da70be87f5a756e59ef32714355063bd6bed Mon Sep 17 00:00:00 2001 From: inosh-perera Date: Mon, 16 Mar 2015 12:39:08 +0530 Subject: [PATCH] get all devices when the username is provided --- .../carbon/device/mgt/core/DeviceManager.java | 3 ++ .../device/mgt/core/DeviceManagerImpl.java | 17 ++++++++++ .../carbon/device/mgt/core/dao/DeviceDAO.java | 8 +++++ .../mgt/core/dao/impl/DeviceDAOImpl.java | 34 +++++++++++++++++++ .../carbon/device/mgt/core/dto/Device.java | 9 +++++ .../core/service/DeviceManagementService.java | 6 ++++ 6 files changed, 77 insertions(+) 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 1f2c4df376..9dd432c870 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 64ebbf0e46..f1232fb658 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 54010a8554..f4a08f107f 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 fae390ac6b..f98c9fda86 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 cb908edd7e..4245918736 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); + } + }