DAO Layer Implementation

revert-70aa11f8
manoj 10 years ago
parent a3028b0683
commit 1e6d06027d

@ -20,42 +20,91 @@ package org.wso2.carbon.device.mgt.core.dao.impl;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; 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.DeviceMgtDAO;
import org.wso2.carbon.device.mgt.core.dao.exception.CDMDAOException; import org.wso2.carbon.device.mgt.core.dao.exception.DeviceDAOException;
import org.wso2.carbon.device.mgt.core.dao.exception.CDMDatabaseConnectionException; 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.Device;
import org.wso2.carbon.device.mgt.core.dto.Status; 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; 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 static final Log log = LogFactory.getLog(DeviceMgtDAOImpl.class);
private DataSource dataSource;
@Override @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 @Override
public void updateDevice(Device device) throws CDMDAOException, CDMDatabaseConnectionException { public void updateDevice(Device device) throws DeviceDAOException, DeviceDatabaseConnectionException {
} }
@Override @Override
public void updateDeviceStatus(Long deviceId, Status status) public void updateDeviceStatus(Long deviceId, Status status)
throws CDMDAOException, CDMDatabaseConnectionException { throws DeviceDAOException, DeviceDatabaseConnectionException {
} }
@Override @Override
public void deleteDevice(Long deviceId) throws CDMDAOException, CDMDatabaseConnectionException { public void deleteDevice(Long deviceId) throws DeviceDAOException, DeviceDatabaseConnectionException {
} }
@Override @Override
public List<Device> getDeviceByDeviceId(Long deviceId) public List<Device> getDeviceByDeviceId(Long deviceId)
throws CDMDAOException, CDMDatabaseConnectionException { throws DeviceDAOException, DeviceDatabaseConnectionException {
return null; 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);
}
}
} }

@ -33,6 +33,15 @@ public class Device implements Serializable{
private String ownerId; private String ownerId;
private String ownerShip; private String ownerShip;
private Long tenantId; private Long tenantId;
private DeviceType deviceType;
public DeviceType getDeviceType() {
return deviceType;
}
public void setDeviceType(DeviceType deviceType) {
this.deviceType = deviceType;
}
public Long getId() { public Long getId() {
return id; return id;

@ -45,8 +45,6 @@
</modules> </modules>
<dependencyManagement> <dependencyManagement>
<dependencies> <dependencies>
<!--<dependency> <!--<dependency>
@ -90,16 +88,45 @@
<version>${stub.version}</version> <version>${stub.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.user.core</artifactId>
<version>${carbon.kernel.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.felix</groupId>
<artifactId>javax.servlet</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.user.api</artifactId>
<version>${carbon.kernel.version}</version>
</dependency>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.utils</artifactId>
<version>${carbon.kernel.version}</version>
</dependency>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.user.mgt</artifactId>
<version>${carbon.kernel.version}</version>
</dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>
<properties> <properties>
<carbon.kernel.version>4.3.0-SNAPSHOT</carbon.kernel.version> <carbon.kernel.version>4.3.0-SNAPSHOT</carbon.kernel.version>
<carbon.platform.version>4.2.0-SNAPSHOT</carbon.platform.version> <carbon.platform.version>4.3.0-SNAPSHOT</carbon.platform.version>
<carbon.p2.plugin.version>1.5.4</carbon.p2.plugin.version> <carbon.p2.plugin.version>1.5.4</carbon.p2.plugin.version>
<maven-buildnumber-plugin.version>1.3</maven-buildnumber-plugin.version> <maven-buildnumber-plugin.version>1.3</maven-buildnumber-plugin.version>
<axis2.osgi.version.range>[1.6.1.wso2v11, 1.7.0)</axis2.osgi.version.range> <axis2.osgi.version.range>[1.6.1.wso2v11, 1.7.0)</axis2.osgi.version.range>
<jboss-transaction-api.version>1.0.0.Final</jboss-transaction-api.version>
<!--Testing --> <!--Testing -->
<test.framework.version>4.3.1</test.framework.version> <test.framework.version>4.3.1</test.framework.version>
<testng.version>6.8</testng.version> <testng.version>6.8</testng.version>

@ -17,6 +17,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE
DEVICE_TYPE_ID INT(11) NULL DEFAULT NULL, DEVICE_TYPE_ID INT(11) NULL DEFAULT NULL,
DEVICE_IDENTIFICATION VARCHAR(300) 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), PRIMARY KEY (ID),
CONSTRAINT fk_DM_DEVICE_DM_DEVICE_TYPE2 FOREIGN KEY (DEVICE_TYPE_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 REFERENCES DM_DEVICE_TYPE (ID ) ON DELETE NO ACTION ON UPDATE NO ACTION

@ -23,6 +23,7 @@ CREATE TABLE IF NOT EXISTS `DM_DEVICE` (
`DEVICE_TYPE_ID` INT(11) NULL DEFAULT NULL , `DEVICE_TYPE_ID` INT(11) NULL DEFAULT NULL ,
`DEVICE_IDENTIFICATION` VARCHAR(300) 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`) , PRIMARY KEY (`ID`) ,
INDEX `fk_DM_DEVICE_DM_DEVICE_TYPE2_idx` (`DEVICE_TYPE_ID` ASC) , INDEX `fk_DM_DEVICE_DM_DEVICE_TYPE2_idx` (`DEVICE_TYPE_ID` ASC) ,
CONSTRAINT `fk_DM_DEVICE_DM_DEVICE_TYPE2` CONSTRAINT `fk_DM_DEVICE_DM_DEVICE_TYPE2`

Loading…
Cancel
Save