From a3dc5d07e555a117c3c35afda952770bd7eb4824 Mon Sep 17 00:00:00 2001 From: Dulitha Wijewantha Date: Thu, 22 Jan 2015 21:14:35 +0530 Subject: [PATCH 1/3] * Commented cdm app copying * Implemented the getDevice() * Added documentation * Added a method to deviceDAO to get the device by a DeviceIdentifier --- .../device/mgt/core/DeviceManagerImpl.java | 21 ++++++++- .../carbon/device/mgt/core/dao/DeviceDAO.java | 8 ++++ .../mgt/core/dao/impl/DeviceDAOImpl.java | 44 ++++++++++++++++++- .../modules/distribution/src/assembly/bin.xml | 16 ++++--- 4 files changed, 80 insertions(+), 9 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 1af868981a..0f5c75cb25 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 @@ -135,7 +135,26 @@ public class DeviceManagerImpl implements DeviceManager { @Override public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException { DeviceManagerService dms = this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); - return dms.getDevice(deviceId); + Device convertedDevice = null; + try { + Integer deviceTypeId = this.getDeviceTypeDAO().getDeviceTypeIdByDeviceTypeName(deviceId.getType()); + org.wso2.carbon.device.mgt.core.dto.Device device = + this.getDeviceDAO().getDeviceByDeviceIdentifier(deviceTypeId, deviceId.getId()); + if(device!=null){ + convertedDevice = DeviceManagementDAOUtil.convertDevice(device, this.getDeviceTypeDAO().getDeviceType(deviceTypeId)); + Device dmsDevice = dms.getDevice(deviceId); + if (dmsDevice != null) { + convertedDevice.setProperties(dmsDevice.getProperties()); + convertedDevice.setFeatures(dmsDevice.getFeatures()); + } + }else{ + throw new DeviceManagementException("No device found for the id '" + deviceId.getId() + "'"); + } + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementException("Error occurred while obtaining the device for id '" + deviceId.getId() + "'", + e); + } + return convertedDevice; } @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 d99db291a2..faced8c295 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 @@ -38,6 +38,14 @@ public interface DeviceDAO { Device getDeviceByDeviceId(Long deviceId) throws DeviceManagementDAOException; + /** + * @param type - Device type. + * @param identifier - Device identifier. + * @return + * @throws DeviceManagementDAOException + */ + Device getDeviceByDeviceIdentifier(Integer type, String identifier) throws DeviceManagementDAOException; + List getDevices() 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 97fa8e25b3..8ac8e74e6a 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 @@ -21,8 +21,8 @@ package org.wso2.carbon.device.mgt.core.dao.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; -import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; 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.Status; @@ -98,6 +98,48 @@ public class DeviceDAOImpl implements DeviceDAO { return null; } + @Override public Device getDeviceByDeviceIdentifier(Integer type, String identifier) + throws DeviceManagementDAOException { + Connection conn = null; + PreparedStatement stmt = null; + ResultSet resultSet = null; + Device device = 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=? AND DM_DEVICE.DEVICE_IDENTIFICATION=?"; + stmt = conn.prepareStatement(selectDBQueryForType); + stmt.setInt(1, type); + stmt.setString(2, identifier); + resultSet = stmt.executeQuery(); + while (resultSet.next()) { + 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)); + } + } 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 device; + } + + @Override public List getDevices() throws DeviceManagementDAOException { return null; diff --git a/product/modules/distribution/src/assembly/bin.xml b/product/modules/distribution/src/assembly/bin.xml index 8c186c073d..7a79eb6b89 100644 --- a/product/modules/distribution/src/assembly/bin.xml +++ b/product/modules/distribution/src/assembly/bin.xml @@ -223,13 +223,15 @@ ${project.artifactId}-${project.version}/repository/resources - - - src/repository/jaggeryapps - wso2cdm-${project.version}/repository/deployment/server/jaggeryapps - - 755 - + + + + + + + From 315603481da986a6a4054a098712b8b5c1a46bd1 Mon Sep 17 00:00:00 2001 From: Dulitha Wijewantha Date: Thu, 22 Jan 2015 21:51:40 +0530 Subject: [PATCH 2/3] Fixed line spacing issues --- .../device/mgt/core/DeviceManagerImpl.java | 287 ++++++++-------- .../carbon/device/mgt/core/dao/DeviceDAO.java | 38 +-- .../mgt/core/dao/impl/DeviceDAOImpl.java | 305 +++++++++--------- 3 files changed, 304 insertions(+), 326 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 0f5c75cb25..0b7b402d22 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 @@ -35,155 +35,142 @@ import java.util.List; public class DeviceManagerImpl implements DeviceManager { - private DeviceDAO deviceDAO; - private DeviceTypeDAO deviceTypeDAO; - private DeviceManagementConfig config; - private DeviceManagementRepository pluginRepository; - - public DeviceManagerImpl(DeviceManagementConfig config, DeviceManagementRepository pluginRepository) { - this.config = config; - this.pluginRepository = pluginRepository; - this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); - this.deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); - } - - @Override - public boolean enrollDevice(Device device) throws DeviceManagementException { - DeviceManagerService dms = this.getPluginRepository().getDeviceManagementProvider(device.getType()); - boolean status = dms.enrollDevice(device); - - try { - org.wso2.carbon.device.mgt.core.dto.Device deviceDto = DeviceManagementDAOUtil.convertDevice(device); - Integer deviceTypeId = this.getDeviceTypeDAO().getDeviceTypeIdByDeviceTypeName(device.getType()); - deviceDto.setDeviceTypeId(deviceTypeId); - this.getDeviceDAO().addDevice(deviceDto); - - } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException("Error occurred while enrolling the device '" + device.getId() + "'", e); - } - return status; - } - - @Override - public boolean modifyEnrollment(Device device) throws DeviceManagementException { - DeviceManagerService dms = this.getPluginRepository().getDeviceManagementProvider(device.getType()); - boolean status = dms.modifyEnrollment(device); - try { - this.getDeviceDAO().updateDevice(DeviceManagementDAOUtil.convertDevice(device)); - } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException("Error occurred while modifying the device '" + device.getId() + "'", - e); - } - return status; - } - - @Override - public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException { - DeviceManagerService dms = this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); - return dms.disenrollDevice(deviceId); - } - - @Override - public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException { - DeviceManagerService dms = this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); - return dms.isEnrolled(deviceId); - } - - @Override - public boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException { - DeviceManagerService dms = this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); - return dms.isActive(deviceId); - } - - @Override - public boolean setActive(DeviceIdentifier deviceId, boolean status) - throws DeviceManagementException { - DeviceManagerService dms = - this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); - return dms.setActive(deviceId, status); - } - - @Override - public List getAllDevices(String type) throws DeviceManagementException { - DeviceManagerService dms = - this.getPluginRepository().getDeviceManagementProvider(type); - 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) { - DeviceType deviceType = this.deviceTypeDAO.getDeviceType(device.getDeviceTypeId()); - Device convertedDevice = DeviceManagementDAOUtil.convertDevice(device, deviceType); - DeviceIdentifier deviceIdentifier = - DeviceManagementDAOUtil.createDeviceIdentifier(device, deviceType); - Device dmsDevice = dms.getDevice(deviceIdentifier); - if (dmsDevice != null) { - 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 - public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException { - DeviceManagerService dms = this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); - Device convertedDevice = null; - try { - Integer deviceTypeId = this.getDeviceTypeDAO().getDeviceTypeIdByDeviceTypeName(deviceId.getType()); - org.wso2.carbon.device.mgt.core.dto.Device device = - this.getDeviceDAO().getDeviceByDeviceIdentifier(deviceTypeId, deviceId.getId()); - if(device!=null){ - convertedDevice = DeviceManagementDAOUtil.convertDevice(device, this.getDeviceTypeDAO().getDeviceType(deviceTypeId)); - Device dmsDevice = dms.getDevice(deviceId); - if (dmsDevice != null) { - convertedDevice.setProperties(dmsDevice.getProperties()); - convertedDevice.setFeatures(dmsDevice.getFeatures()); - } - }else{ - throw new DeviceManagementException("No device found for the id '" + deviceId.getId() + "'"); - } - } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException("Error occurred while obtaining the device for id '" + deviceId.getId() + "'", - e); - } - return convertedDevice; - } - - @Override - public boolean updateDeviceInfo(Device device) throws DeviceManagementException { - DeviceManagerService dms = this.getPluginRepository().getDeviceManagementProvider(device.getType()); - return dms.updateDeviceInfo(device); - } - - @Override - public boolean setOwnership(DeviceIdentifier deviceId, String ownershipType) throws DeviceManagementException { - DeviceManagerService dms = this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); - return dms.setOwnership(deviceId, ownershipType); - } - - public OperationManager getOperationManager(String type) throws DeviceManagementException { - DeviceManagerService dms = this.getPluginRepository().getDeviceManagementProvider(type); - return dms.getOperationManager(); - } - - public DeviceDAO getDeviceDAO() { - return deviceDAO; - } - - public DeviceTypeDAO getDeviceTypeDAO() { - return deviceTypeDAO; - } - - public DeviceManagementRepository getPluginRepository() { - return pluginRepository; - } + private DeviceDAO deviceDAO; + private DeviceTypeDAO deviceTypeDAO; + private DeviceManagementConfig config; + private DeviceManagementRepository pluginRepository; + + public DeviceManagerImpl(DeviceManagementConfig config, DeviceManagementRepository pluginRepository) { + this.config = config; + this.pluginRepository = pluginRepository; + this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); + this.deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); + } + + @Override public boolean enrollDevice(Device device) throws DeviceManagementException { + DeviceManagerService dms = this.getPluginRepository().getDeviceManagementProvider(device.getType()); + boolean status = dms.enrollDevice(device); + + try { + org.wso2.carbon.device.mgt.core.dto.Device deviceDto = DeviceManagementDAOUtil.convertDevice(device); + Integer deviceTypeId = this.getDeviceTypeDAO().getDeviceTypeIdByDeviceTypeName(device.getType()); + deviceDto.setDeviceTypeId(deviceTypeId); + this.getDeviceDAO().addDevice(deviceDto); + + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementException("Error occurred while enrolling the device '" + device.getId() + "'", + e); + } + return status; + } + + @Override public boolean modifyEnrollment(Device device) throws DeviceManagementException { + DeviceManagerService dms = this.getPluginRepository().getDeviceManagementProvider(device.getType()); + boolean status = dms.modifyEnrollment(device); + try { + this.getDeviceDAO().updateDevice(DeviceManagementDAOUtil.convertDevice(device)); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementException("Error occurred while modifying the device '" + device.getId() + "'", + e); + } + return status; + } + + @Override public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException { + DeviceManagerService dms = this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); + return dms.disenrollDevice(deviceId); + } + + @Override public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException { + DeviceManagerService dms = this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); + return dms.isEnrolled(deviceId); + } + + @Override public boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException { + DeviceManagerService dms = this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); + return dms.isActive(deviceId); + } + + @Override public boolean setActive(DeviceIdentifier deviceId, boolean status) throws DeviceManagementException { + DeviceManagerService dms = this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); + return dms.setActive(deviceId, status); + } + + @Override public List getAllDevices(String type) throws DeviceManagementException { + DeviceManagerService dms = this.getPluginRepository().getDeviceManagementProvider(type); + 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) { + DeviceType deviceType = this.deviceTypeDAO.getDeviceType(device.getDeviceTypeId()); + Device convertedDevice = DeviceManagementDAOUtil.convertDevice(device, deviceType); + DeviceIdentifier deviceIdentifier = DeviceManagementDAOUtil.createDeviceIdentifier(device, deviceType); + Device dmsDevice = dms.getDevice(deviceIdentifier); + if (dmsDevice != null) { + 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 public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException { + DeviceManagerService dms = this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); + Device convertedDevice = null; + try { + Integer deviceTypeId = this.getDeviceTypeDAO().getDeviceTypeIdByDeviceTypeName(deviceId.getType()); + org.wso2.carbon.device.mgt.core.dto.Device device = + this.getDeviceDAO().getDeviceByDeviceIdentifier(deviceTypeId, deviceId.getId()); + if (device != null) { + convertedDevice = DeviceManagementDAOUtil + .convertDevice(device, this.getDeviceTypeDAO().getDeviceType(deviceTypeId)); + Device dmsDevice = dms.getDevice(deviceId); + if (dmsDevice != null) { + convertedDevice.setProperties(dmsDevice.getProperties()); + convertedDevice.setFeatures(dmsDevice.getFeatures()); + } + } else { + throw new DeviceManagementException("No device found for the id '" + deviceId.getId() + "'"); + } + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementException( + "Error occurred while obtaining the device for id '" + deviceId.getId() + "'", e); + } + return convertedDevice; + } + + @Override public boolean updateDeviceInfo(Device device) throws DeviceManagementException { + DeviceManagerService dms = this.getPluginRepository().getDeviceManagementProvider(device.getType()); + return dms.updateDeviceInfo(device); + } + + @Override public boolean setOwnership(DeviceIdentifier deviceId, String ownershipType) + throws DeviceManagementException { + DeviceManagerService dms = this.getPluginRepository().getDeviceManagementProvider(deviceId.getType()); + return dms.setOwnership(deviceId, ownershipType); + } + + public OperationManager getOperationManager(String type) throws DeviceManagementException { + DeviceManagerService dms = this.getPluginRepository().getDeviceManagementProvider(type); + return dms.getOperationManager(); + } + + public DeviceDAO getDeviceDAO() { + return deviceDAO; + } + + public DeviceTypeDAO getDeviceTypeDAO() { + return deviceTypeDAO; + } + + public DeviceManagementRepository getPluginRepository() { + return pluginRepository; + } } 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 faced8c295..f474ba1c6f 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 @@ -28,30 +28,30 @@ import java.util.List; */ public interface DeviceDAO { - void addDevice(Device device) throws DeviceManagementDAOException; + void addDevice(Device device) throws DeviceManagementDAOException; - void updateDevice(Device device) throws DeviceManagementDAOException; + void updateDevice(Device device) throws DeviceManagementDAOException; - void updateDeviceStatus(Long deviceId, Status status) throws DeviceManagementDAOException; + void updateDeviceStatus(Long deviceId, Status status) throws DeviceManagementDAOException; - void deleteDevice(Long deviceId) throws DeviceManagementDAOException; + void deleteDevice(Long deviceId) throws DeviceManagementDAOException; - Device getDeviceByDeviceId(Long deviceId) throws DeviceManagementDAOException; + Device getDeviceByDeviceId(Long deviceId) throws DeviceManagementDAOException; - /** - * @param type - Device type. - * @param identifier - Device identifier. - * @return - * @throws DeviceManagementDAOException - */ - Device getDeviceByDeviceIdentifier(Integer type, String identifier) throws DeviceManagementDAOException; + /** + * @param type - Device type. + * @param identifier - Device identifier. + * @return + * @throws DeviceManagementDAOException + */ + Device getDeviceByDeviceIdentifier(Integer type, String identifier) throws DeviceManagementDAOException; - List getDevices() 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; + /** + * @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 8ac8e74e6a..e47fbcc17e 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 @@ -37,162 +37,153 @@ import java.util.List; public class DeviceDAOImpl implements DeviceDAO { - private DataSource dataSource; - private static final Log log = LogFactory.getLog(DeviceDAOImpl.class); - - - public DeviceDAOImpl(DataSource dataSource) { - this.dataSource = dataSource; - } - - @Override - public void addDevice(Device device) throws DeviceManagementDAOException { - Connection conn = null; - PreparedStatement stmt = null; - try { - conn = this.getConnection(); - String createDBQuery = - "INSERT INTO DM_DEVICE(DESCRIPTION, NAME, DATE_OF_ENROLLMENT, DATE_OF_LAST_UPDATE, OWNERSHIP," + - "STATUS, DEVICE_TYPE_ID, DEVICE_IDENTIFICATION, OWNER, TENANT_ID) VALUES " + - "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; - - stmt = conn.prepareStatement(createDBQuery); - stmt.setString(1, device.getDescription()); - stmt.setString(2, device.getName()); - stmt.setLong(3, new Date().getTime()); - stmt.setLong(4, new Date().getTime()); - stmt.setString(5, device.getOwnerShip()); - stmt.setString(6, device.getStatus().toString()); - stmt.setInt(7, device.getDeviceTypeId()); - stmt.setString(8, device.getDeviceIdentificationId()); - stmt.setString(9, device.getOwnerId()); - stmt.setInt(10, device.getTenantId()); - stmt.executeUpdate(); - } catch (SQLException e) { - String msg = "Error occurred while enrolling device '" + device.getName() + "'"; - log.error(msg, e); - throw new DeviceManagementDAOException(msg, e); - } finally { - DeviceManagementDAOUtil.cleanupResources(conn, stmt, null); - } - } - - @Override - public void updateDevice(Device device) throws DeviceManagementDAOException { - - } - - @Override - public void updateDeviceStatus(Long deviceId, Status status) throws DeviceManagementDAOException { - - } - - @Override - public void deleteDevice(Long deviceId) throws DeviceManagementDAOException { - - } - - @Override - public Device getDeviceByDeviceId(Long deviceId) - throws DeviceManagementDAOException { - return null; - } - - @Override public Device getDeviceByDeviceIdentifier(Integer type, String identifier) - throws DeviceManagementDAOException { - Connection conn = null; - PreparedStatement stmt = null; - ResultSet resultSet = null; - Device device = 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=? AND DM_DEVICE.DEVICE_IDENTIFICATION=?"; - stmt = conn.prepareStatement(selectDBQueryForType); - stmt.setInt(1, type); - stmt.setString(2, identifier); - resultSet = stmt.executeQuery(); - while (resultSet.next()) { - 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)); - } - } 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 device; - } - - - @Override - public List getDevices() throws DeviceManagementDAOException { - 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(); - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while obtaining a connection from the device " + - "management metadata repository datasource", e); - } - } + private DataSource dataSource; + private static final Log log = LogFactory.getLog(DeviceDAOImpl.class); + + public DeviceDAOImpl(DataSource dataSource) { + this.dataSource = dataSource; + } + + @Override public void addDevice(Device device) throws DeviceManagementDAOException { + Connection conn = null; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String createDBQuery = + "INSERT INTO DM_DEVICE(DESCRIPTION, NAME, DATE_OF_ENROLLMENT, DATE_OF_LAST_UPDATE, OWNERSHIP," + + "STATUS, DEVICE_TYPE_ID, DEVICE_IDENTIFICATION, OWNER, TENANT_ID) VALUES " + + "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + + stmt = conn.prepareStatement(createDBQuery); + stmt.setString(1, device.getDescription()); + stmt.setString(2, device.getName()); + stmt.setLong(3, new Date().getTime()); + stmt.setLong(4, new Date().getTime()); + stmt.setString(5, device.getOwnerShip()); + stmt.setString(6, device.getStatus().toString()); + stmt.setInt(7, device.getDeviceTypeId()); + stmt.setString(8, device.getDeviceIdentificationId()); + stmt.setString(9, device.getOwnerId()); + stmt.setInt(10, device.getTenantId()); + stmt.executeUpdate(); + } catch (SQLException e) { + String msg = "Error occurred while enrolling device '" + device.getName() + "'"; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } finally { + DeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + } + } + + @Override public void updateDevice(Device device) throws DeviceManagementDAOException { + + } + + @Override public void updateDeviceStatus(Long deviceId, Status status) throws DeviceManagementDAOException { + + } + + @Override public void deleteDevice(Long deviceId) throws DeviceManagementDAOException { + + } + + @Override public Device getDeviceByDeviceId(Long deviceId) throws DeviceManagementDAOException { + return null; + } + + @Override public Device getDeviceByDeviceIdentifier(Integer type, String identifier) + throws DeviceManagementDAOException { + Connection conn = null; + PreparedStatement stmt = null; + ResultSet resultSet = null; + Device device = 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=? AND DM_DEVICE.DEVICE_IDENTIFICATION=?"; + stmt = conn.prepareStatement(selectDBQueryForType); + stmt.setInt(1, type); + stmt.setString(2, identifier); + resultSet = stmt.executeQuery(); + while (resultSet.next()) { + 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)); + } + } 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 device; + } + + @Override public List getDevices() throws DeviceManagementDAOException { + 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(); + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while obtaining a connection from the device " + + "management metadata repository datasource", e); + } + } } From 5e687356255ef7d7a6f5eb987cd30704bbd1c89f Mon Sep 17 00:00:00 2001 From: Dulitha Wijewantha Date: Thu, 22 Jan 2015 22:06:37 +0530 Subject: [PATCH 3/3] Removed the exception. It has to be handled in a different layer --- .../java/org/wso2/carbon/device/mgt/core/DeviceManagerImpl.java | 2 -- 1 file changed, 2 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 0b7b402d22..726c43c343 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 @@ -135,8 +135,6 @@ public class DeviceManagerImpl implements DeviceManager { convertedDevice.setProperties(dmsDevice.getProperties()); convertedDevice.setFeatures(dmsDevice.getFeatures()); } - } else { - throw new DeviceManagementException("No device found for the id '" + deviceId.getId() + "'"); } } catch (DeviceManagementDAOException e) { throw new DeviceManagementException(