From 1e6d06027d8a2050318bb4c29e25b67a228cd330 Mon Sep 17 00:00:00 2001 From: manoj Date: Fri, 5 Dec 2014 11:04:16 +0530 Subject: [PATCH] DAO Layer Implementation --- .../mgt/core/dao/impl/DeviceMgtDAOImpl.java | 65 ++++++++++++++++--- .../carbon/device/mgt/core/dto/Device.java | 11 +++- pom.xml | 35 ++++++++-- .../src/repository/dbscripts/cdm/h2.sql | 3 +- .../src/repository/dbscripts/cdm/mysql.sql | 1 + 5 files changed, 101 insertions(+), 14 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceMgtDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceMgtDAOImpl.java index 426266cca6..024c551540 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceMgtDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceMgtDAOImpl.java @@ -20,42 +20,91 @@ 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.common.DeviceManagementConstants; +import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; import org.wso2.carbon.device.mgt.core.dao.DeviceMgtDAO; -import org.wso2.carbon.device.mgt.core.dao.exception.CDMDAOException; -import org.wso2.carbon.device.mgt.core.dao.exception.CDMDatabaseConnectionException; +import org.wso2.carbon.device.mgt.core.dao.exception.DeviceDAOException; +import org.wso2.carbon.device.mgt.core.dao.exception.DeviceDatabaseConnectionException; +import org.wso2.carbon.device.mgt.core.dao.util.DeviceDAOUtil; +import org.wso2.carbon.device.mgt.core.dao.util.ErrorHandler; import org.wso2.carbon.device.mgt.core.dto.Device; import org.wso2.carbon.device.mgt.core.dto.Status; +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.Date; import java.util.List; -public class DeviceMgtDAOImpl implements DeviceMgtDAO { +public class DeviceMgtDAOImpl extends DeviceDAO implements DeviceMgtDAO { private static final Log log = LogFactory.getLog(DeviceMgtDAOImpl.class); + private DataSource dataSource; @Override - public void addDevice(Device device) throws CDMDAOException, CDMDatabaseConnectionException { + public void addDevice(Device device) throws DeviceDAOException, DeviceDatabaseConnectionException { + Connection conn = null; + PreparedStatement addDeviceDBStatement = null; + try { + conn = getDataSourceConnection(); + conn.setAutoCommit(false); + String createDBQuery = + "INSERT INTO DM_DEVICE(DESCRIPTION,NAME,DATE_OF_ENROLLMENT,DATE_OF_LAST_UPDATE,OWNERSHIP," + + "STATUS,DEVICE_TYPE_ID,DEVICE_IDENTIFICATION,OWNER) VALUES (?,?,?,?,?,?,?,?,?)"; + + addDeviceDBStatement = conn.prepareStatement(createDBQuery); + addDeviceDBStatement.setString(1, device.getDescription()); + addDeviceDBStatement.setString(2, device.getName()); + addDeviceDBStatement.setLong(3, new Date().getTime()); + addDeviceDBStatement.setLong(4, new Date().getTime()); + addDeviceDBStatement.setString(5, device.getOwnerShip()); + addDeviceDBStatement.setString(6, device.getStatus().toString()); + addDeviceDBStatement.setLong(7, device.getDeviceType().getId()); + addDeviceDBStatement.setLong(8, device.getDeviceIdentificationId()); + addDeviceDBStatement.setString(9, device.getOwnerId()); + addDeviceDBStatement.executeUpdate(); + conn.commit(); + + } catch (SQLException e) { + DeviceDAOUtil.rollback(conn, DeviceManagementConstants.ADD_DEVICE_ENTRY); + String msg = "Failed to enroll device " + device.getName() + " to CDM"; + ErrorHandler errorHandler = new ErrorHandler(msg, log); + errorHandler.handleDAOException(msg, e); + } finally { + DeviceDAOUtil.cleanupResources(null, addDeviceDBStatement, conn, DeviceManagementConstants.ADD_DEVICE_ENTRY); + } } @Override - public void updateDevice(Device device) throws CDMDAOException, CDMDatabaseConnectionException { + public void updateDevice(Device device) throws DeviceDAOException, DeviceDatabaseConnectionException { } @Override public void updateDeviceStatus(Long deviceId, Status status) - throws CDMDAOException, CDMDatabaseConnectionException { + throws DeviceDAOException, DeviceDatabaseConnectionException { } @Override - public void deleteDevice(Long deviceId) throws CDMDAOException, CDMDatabaseConnectionException { + public void deleteDevice(Long deviceId) throws DeviceDAOException, DeviceDatabaseConnectionException { } @Override public List getDeviceByDeviceId(Long deviceId) - throws CDMDAOException, CDMDatabaseConnectionException { + throws DeviceDAOException, DeviceDatabaseConnectionException { return null; } + + private Connection getDataSourceConnection() throws DeviceDatabaseConnectionException { + try { + return dataSource.getConnection(); + } catch (SQLException e) { + String msg = "Error while acquiring the database connection. Meta Repository Database server may down"; + throw new DeviceDatabaseConnectionException(msg, e); + } + } } 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 36a52a45cc..c988968ce5 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 @@ -20,7 +20,7 @@ package org.wso2.carbon.device.mgt.core.dto; import java.io.Serializable; -public class Device implements Serializable{ +public class Device implements Serializable { private static final long serialVersionUID = -8101106997837486245L; private Long id; @@ -33,6 +33,15 @@ public class Device implements Serializable{ private String ownerId; private String ownerShip; private Long tenantId; + private DeviceType deviceType; + + public DeviceType getDeviceType() { + return deviceType; + } + + public void setDeviceType(DeviceType deviceType) { + this.deviceType = deviceType; + } public Long getId() { return id; diff --git a/pom.xml b/pom.xml index d0b77c184f..188c3ccef1 100644 --- a/pom.xml +++ b/pom.xml @@ -45,8 +45,6 @@ - - 4.3.1 6.8 diff --git a/product/modules/distribution/src/repository/dbscripts/cdm/h2.sql b/product/modules/distribution/src/repository/dbscripts/cdm/h2.sql index 4c1e805f9b..94e01ca15c 100644 --- a/product/modules/distribution/src/repository/dbscripts/cdm/h2.sql +++ b/product/modules/distribution/src/repository/dbscripts/cdm/h2.sql @@ -16,7 +16,8 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE STATUS VARCHAR(15) NULL DEFAULT NULL, DEVICE_TYPE_ID INT(11) NULL DEFAULT NULL, DEVICE_IDENTIFICATION VARCHAR(300) NULL DEFAULT NULL, - OWNER VARCHAR(45) NULL DEFAULT NULL, + OWNER VARCHAR(45) NULL DEFAULT NULL, + TENANT_ID INTEGER DEFAULT 0, PRIMARY KEY (ID), CONSTRAINT fk_DM_DEVICE_DM_DEVICE_TYPE2 FOREIGN KEY (DEVICE_TYPE_ID ) REFERENCES DM_DEVICE_TYPE (ID ) ON DELETE NO ACTION ON UPDATE NO ACTION diff --git a/product/modules/distribution/src/repository/dbscripts/cdm/mysql.sql b/product/modules/distribution/src/repository/dbscripts/cdm/mysql.sql index fc78109c54..bc04de8732 100644 --- a/product/modules/distribution/src/repository/dbscripts/cdm/mysql.sql +++ b/product/modules/distribution/src/repository/dbscripts/cdm/mysql.sql @@ -23,6 +23,7 @@ CREATE TABLE IF NOT EXISTS `DM_DEVICE` ( `DEVICE_TYPE_ID` INT(11) NULL DEFAULT NULL , `DEVICE_IDENTIFICATION` VARCHAR(300) NULL DEFAULT NULL , `OWNER` VARCHAR(45) NULL DEFAULT NULL , + TENANT_ID INTEGER DEFAULT 0, PRIMARY KEY (`ID`) , INDEX `fk_DM_DEVICE_DM_DEVICE_TYPE2_idx` (`DEVICE_TYPE_ID` ASC) , CONSTRAINT `fk_DM_DEVICE_DM_DEVICE_TYPE2`