From adebf34ff25224ffd8b188594dbdfbbabe2b89d9 Mon Sep 17 00:00:00 2001 From: Dulitha Wijewantha Date: Mon, 19 Jan 2015 17:54:39 +0530 Subject: [PATCH] Refactored the device id to integer. Created method to convert dto.device to common.device Created a bulk method for conversion Implemented getAllDevices --- .../device/mgt/core/DeviceManagerImpl.java | 28 +++++++++-- .../carbon/device/mgt/core/dao/DeviceDAO.java | 7 +++ .../mgt/core/dao/impl/DeviceDAOImpl.java | 44 ++++++++++++++++- .../dao/util/DeviceManagementDAOUtil.java | 48 +++++++++++++++++++ .../carbon/device/mgt/core/dto/Device.java | 16 +++---- 5 files changed, 130 insertions(+), 13 deletions(-) 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 a2b6496747..d11488d526 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 @@ -29,6 +29,7 @@ import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; +import java.util.ArrayList; import java.util.List; public class DeviceManagerImpl implements DeviceManager { @@ -51,10 +52,9 @@ public class DeviceManagerImpl implements DeviceManager { boolean status = dms.enrollDevice(device); try { - this.getDeviceTypeDAO().getDeviceType(); org.wso2.carbon.device.mgt.core.dto.Device deviceDto = DeviceManagementDAOUtil.convertDevice(device); Integer deviceTypeId = this.getDeviceTypeDAO().getDeviceTypeIdByDeviceTypeName(device.getType()); - deviceDto.setDeviceType(deviceTypeId); + deviceDto.setDeviceTypeId(deviceTypeId); this.getDeviceDAO().addDevice(deviceDto); } catch (DeviceManagementDAOException e) { @@ -105,9 +105,29 @@ public class DeviceManagerImpl implements DeviceManager { @Override public List getAllDevices(String type) throws DeviceManagementException { - DeviceManagerService dms = + DeviceManagerService dms = this.getPluginRepository().getDeviceManagementProvider(type); - return dms.getAllDevices(); + List devicesList = new ArrayList(); + try { + Integer deviceTypeId = this.getDeviceTypeDAO().getDeviceTypeIdByDeviceTypeName(type); + List devices = + this.getDeviceDAO().getDevices(deviceTypeId); + + for (org.wso2.carbon.device.mgt.core.dto.Device device : devices) { + Device convertedDevice = DeviceManagementDAOUtil.convertDevice(device); + DeviceIdentifier deviceIdentifier = DeviceManagementDAOUtil + .createDeviceIdentifier(device, this.deviceTypeDAO + .getDeviceType(device.getDeviceTypeId())); + Device dmsDevice = dms.getDevice(deviceIdentifier); + convertedDevice.setProperties(dmsDevice.getProperties()); + convertedDevice.setFeatures(dmsDevice.getFeatures()); + devicesList.add(convertedDevice); + } + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementException("Error occurred while obtaining the device for type '" + type + "'", + e); + } + return devicesList; } @Override 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 60ab53fd01..d99db291a2 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 @@ -39,4 +39,11 @@ public interface DeviceDAO { Device getDeviceByDeviceId(Long deviceId) throws DeviceManagementDAOException; List getDevices() throws DeviceManagementDAOException; + + /** + * @param type - The device type id. + * @return a list of devices based on the type id. + * @throws DeviceManagementDAOException + */ + List getDevices(Integer type) 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 87ce6ab397..97fa8e25b3 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 @@ -31,6 +31,7 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -62,7 +63,7 @@ public class DeviceDAOImpl implements DeviceDAO { stmt.setLong(4, new Date().getTime()); stmt.setString(5, device.getOwnerShip()); stmt.setString(6, device.getStatus().toString()); - stmt.setInt(7, device.getDeviceType()); + stmt.setInt(7, device.getDeviceTypeId()); stmt.setString(8, device.getDeviceIdentificationId()); stmt.setString(9, device.getOwnerId()); stmt.setInt(10, device.getTenantId()); @@ -102,6 +103,47 @@ public class DeviceDAOImpl implements DeviceDAO { return null; } + @Override public List getDevices(Integer type) throws DeviceManagementDAOException { + Connection conn = null; + PreparedStatement stmt = null; + ResultSet resultSet = null; + List devicesList = null; + try { + conn = this.getConnection(); + String selectDBQueryForType = "SELECT ID, DESCRIPTION, NAME, DATE_OF_ENROLLMENT, " + + "DATE_OF_LAST_UPDATE, OWNERSHIP, STATUS, DEVICE_TYPE_ID, " + + "DEVICE_IDENTIFICATION, OWNER, TENANT_ID FROM DM_DEVICE " + + "WHERE DM_DEVICE.DEVICE_TYPE_ID=?"; + stmt = conn.prepareStatement(selectDBQueryForType); + stmt.setInt(1, type); + resultSet = stmt.executeQuery(); + devicesList = new ArrayList(); + while (resultSet.next()) { + Device device = new Device(); + device.setId(resultSet.getInt(1)); + device.setDescription(resultSet.getString(2)); + device.setName(resultSet.getString(3)); + device.setDateOfEnrollment(resultSet.getLong(4)); + device.setDateOfLastUpdate(resultSet.getLong(5)); + //TODO:- Ownership is not a enum in DeviceDAO + device.setOwnerShip(resultSet.getString(6)); + device.setStatus(Status.valueOf(resultSet.getString(7))); + device.setDeviceTypeId(resultSet.getInt(8)); + device.setDeviceIdentificationId(resultSet.getString(9)); + device.setOwnerId(resultSet.getString(10)); + device.setTenantId(resultSet.getInt(11)); + devicesList.add(device); + } + } catch (SQLException e) { + String msg = "Error occurred while listing devices for type '" + type + "'"; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } finally { + DeviceManagementDAOUtil.cleanupResources(conn, stmt, resultSet); + } + return devicesList; + } + private Connection getConnection() throws DeviceManagementDAOException { try { return dataSource.getConnection(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java index 20fa5e2354..1f536a2411 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/DeviceManagementDAOUtil.java @@ -18,8 +18,10 @@ package org.wso2.carbon.device.mgt.core.dao.util; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.CarbonContext; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; 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 org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.user.api.UserStoreException; @@ -32,7 +34,9 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; import java.util.Hashtable; +import java.util.List; public final class DeviceManagementDAOUtil { @@ -100,6 +104,44 @@ public final class DeviceManagementDAOUtil { } } + /** + * @param device - The DTO device object. + * @return A Business Object. + */ + public static org.wso2.carbon.device.mgt.common.Device convertDevice(Device device){ + org.wso2.carbon.device.mgt.common.Device deviceBO = + new org.wso2.carbon.device.mgt.common.Device(); + deviceBO.setDateOfEnrolment(device.getDateOfEnrollment()); + deviceBO.setDateOfLastUpdate(device.getDateOfLastUpdate()); + deviceBO.setDescription(device.getDescription()); + deviceBO.setDeviceIdentifier(device.getDeviceIdentificationId()); + deviceBO.setDeviceTypeId(device.getDeviceTypeId()); + deviceBO.setName(device.getName()); + deviceBO.setId(device.getId()); + deviceBO.setOwner(device.getOwnerId()); + deviceBO.setOwnership(device.getOwnerShip()); + if (device.getStatus() == Status.ACTIVE) { + deviceBO.setStatus(true); + } else if (device.getStatus() == Status.INACTIVE) { + deviceBO.setStatus(false); + } + return null; + } + + /** + * @param devices - DTO Device Object list. + * @return converted Business Object list. + */ + public static List convertDevices( + List devices) { + List deviceBOList = + new ArrayList(); + for (Device device : devices) { + deviceBOList.add(convertDevice(device)); + } + return deviceBOList; + } + public static Device convertDevice(org.wso2.carbon.device.mgt.common.Device device) throws DeviceManagementDAOException { Device deviceBO = new Device(); @@ -120,4 +162,10 @@ public final class DeviceManagementDAOUtil { return deviceBO; } + public static DeviceIdentifier createDeviceIdentifier(Device device, DeviceType deviceType) { + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setType(deviceType.getName()); + deviceIdentifier.setId(device.getDeviceIdentificationId()); + return deviceIdentifier; + } } 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 21c951cfae..fae390ac6b 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 @@ -23,7 +23,7 @@ import java.io.Serializable; public class Device implements Serializable { private static final long serialVersionUID = -8101106997837486245L; - private String id; + private Integer id; private String description; private String name; private Long dateOfEnrollment; @@ -33,21 +33,21 @@ public class Device implements Serializable { private String ownerId; private String ownerShip; private int tenantId; - private Integer deviceType; + private Integer deviceTypeId; - public Integer getDeviceType() { - return deviceType; + public Integer getDeviceTypeId() { + return deviceTypeId; } - public void setDeviceType(Integer deviceType) { - this.deviceType = deviceType; + public void setDeviceTypeId(Integer deviceTypeId) { + this.deviceTypeId = deviceTypeId; } - public String getId() { + public Integer getId() { return id; } - public void setId(String id) { + public void setId(Integer id) { this.id = id; }