|
|
@ -21,8 +21,8 @@ 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.core.dao.DeviceDAO;
|
|
|
|
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.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.Device;
|
|
|
|
import org.wso2.carbon.device.mgt.core.dto.Status;
|
|
|
|
import org.wso2.carbon.device.mgt.core.dto.Status;
|
|
|
|
|
|
|
|
|
|
|
@ -37,120 +37,153 @@ import java.util.List;
|
|
|
|
|
|
|
|
|
|
|
|
public class DeviceDAOImpl implements DeviceDAO {
|
|
|
|
public class DeviceDAOImpl implements DeviceDAO {
|
|
|
|
|
|
|
|
|
|
|
|
private DataSource dataSource;
|
|
|
|
private DataSource dataSource;
|
|
|
|
private static final Log log = LogFactory.getLog(DeviceDAOImpl.class);
|
|
|
|
private static final Log log = LogFactory.getLog(DeviceDAOImpl.class);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public DeviceDAOImpl(DataSource dataSource) {
|
|
|
|
public DeviceDAOImpl(DataSource dataSource) {
|
|
|
|
this.dataSource = dataSource;
|
|
|
|
this.dataSource = dataSource;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override public void addDevice(Device device) throws DeviceManagementDAOException {
|
|
|
|
@Override
|
|
|
|
Connection conn = null;
|
|
|
|
public void addDevice(Device device) throws DeviceManagementDAOException {
|
|
|
|
PreparedStatement stmt = null;
|
|
|
|
Connection conn = null;
|
|
|
|
try {
|
|
|
|
PreparedStatement stmt = null;
|
|
|
|
conn = this.getConnection();
|
|
|
|
try {
|
|
|
|
String createDBQuery =
|
|
|
|
conn = this.getConnection();
|
|
|
|
"INSERT INTO DM_DEVICE(DESCRIPTION, NAME, DATE_OF_ENROLLMENT, DATE_OF_LAST_UPDATE, OWNERSHIP," +
|
|
|
|
String createDBQuery =
|
|
|
|
"STATUS, DEVICE_TYPE_ID, DEVICE_IDENTIFICATION, OWNER, TENANT_ID) VALUES " +
|
|
|
|
"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 = conn.prepareStatement(createDBQuery);
|
|
|
|
stmt.setString(2, device.getName());
|
|
|
|
stmt.setString(1, device.getDescription());
|
|
|
|
stmt.setLong(3, new Date().getTime());
|
|
|
|
stmt.setString(2, device.getName());
|
|
|
|
stmt.setLong(4, new Date().getTime());
|
|
|
|
stmt.setLong(3, new Date().getTime());
|
|
|
|
stmt.setString(5, device.getOwnerShip());
|
|
|
|
stmt.setLong(4, new Date().getTime());
|
|
|
|
stmt.setString(6, device.getStatus().toString());
|
|
|
|
stmt.setString(5, device.getOwnerShip());
|
|
|
|
stmt.setInt(7, device.getDeviceTypeId());
|
|
|
|
stmt.setString(6, device.getStatus().toString());
|
|
|
|
stmt.setString(8, device.getDeviceIdentificationId());
|
|
|
|
stmt.setInt(7, device.getDeviceTypeId());
|
|
|
|
stmt.setString(9, device.getOwnerId());
|
|
|
|
stmt.setString(8, device.getDeviceIdentificationId());
|
|
|
|
stmt.setInt(10, device.getTenantId());
|
|
|
|
stmt.setString(9, device.getOwnerId());
|
|
|
|
stmt.executeUpdate();
|
|
|
|
stmt.setInt(10, device.getTenantId());
|
|
|
|
} catch (SQLException e) {
|
|
|
|
stmt.executeUpdate();
|
|
|
|
String msg = "Error occurred while enrolling device '" + device.getName() + "'";
|
|
|
|
} catch (SQLException e) {
|
|
|
|
log.error(msg, e);
|
|
|
|
String msg = "Error occurred while enrolling device '" + device.getName() + "'";
|
|
|
|
throw new DeviceManagementDAOException(msg, e);
|
|
|
|
log.error(msg, e);
|
|
|
|
} finally {
|
|
|
|
throw new DeviceManagementDAOException(msg, e);
|
|
|
|
DeviceManagementDAOUtil.cleanupResources(conn, stmt, null);
|
|
|
|
} finally {
|
|
|
|
}
|
|
|
|
DeviceManagementDAOUtil.cleanupResources(conn, stmt, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
@Override public void updateDevice(Device device) throws DeviceManagementDAOException {
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
}
|
|
|
|
public void updateDevice(Device device) throws DeviceManagementDAOException {
|
|
|
|
|
|
|
|
|
|
|
|
@Override public void updateDeviceStatus(Long deviceId, Status status) throws DeviceManagementDAOException {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void updateDeviceStatus(Long deviceId, Status status) throws DeviceManagementDAOException {
|
|
|
|
@Override public void deleteDevice(Long deviceId) throws DeviceManagementDAOException {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override public Device getDeviceByDeviceId(Long deviceId) throws DeviceManagementDAOException {
|
|
|
|
public void deleteDevice(Long deviceId) throws DeviceManagementDAOException {
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override public Device getDeviceByDeviceIdentifier(Integer type, String identifier)
|
|
|
|
@Override
|
|
|
|
throws DeviceManagementDAOException {
|
|
|
|
public Device getDeviceByDeviceId(Long deviceId)
|
|
|
|
Connection conn = null;
|
|
|
|
throws DeviceManagementDAOException {
|
|
|
|
PreparedStatement stmt = null;
|
|
|
|
return null;
|
|
|
|
ResultSet resultSet = null;
|
|
|
|
}
|
|
|
|
Device device = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
@Override
|
|
|
|
conn = this.getConnection();
|
|
|
|
public List<Device> getDevices() throws DeviceManagementDAOException {
|
|
|
|
String selectDBQueryForType = "SELECT ID, DESCRIPTION, NAME, DATE_OF_ENROLLMENT, " +
|
|
|
|
return null;
|
|
|
|
"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=?";
|
|
|
|
@Override public List<Device> getDevices(Integer type) throws DeviceManagementDAOException {
|
|
|
|
stmt = conn.prepareStatement(selectDBQueryForType);
|
|
|
|
Connection conn = null;
|
|
|
|
stmt.setInt(1, type);
|
|
|
|
PreparedStatement stmt = null;
|
|
|
|
stmt.setString(2, identifier);
|
|
|
|
ResultSet resultSet = null;
|
|
|
|
resultSet = stmt.executeQuery();
|
|
|
|
List<Device> devicesList = null;
|
|
|
|
while (resultSet.next()) {
|
|
|
|
try {
|
|
|
|
device = new Device();
|
|
|
|
conn = this.getConnection();
|
|
|
|
device.setId(resultSet.getInt(1));
|
|
|
|
String selectDBQueryForType = "SELECT ID, DESCRIPTION, NAME, DATE_OF_ENROLLMENT, " +
|
|
|
|
device.setDescription(resultSet.getString(2));
|
|
|
|
"DATE_OF_LAST_UPDATE, OWNERSHIP, STATUS, DEVICE_TYPE_ID, " +
|
|
|
|
device.setName(resultSet.getString(3));
|
|
|
|
"DEVICE_IDENTIFICATION, OWNER, TENANT_ID FROM DM_DEVICE " +
|
|
|
|
device.setDateOfEnrollment(resultSet.getLong(4));
|
|
|
|
"WHERE DM_DEVICE.DEVICE_TYPE_ID=?";
|
|
|
|
device.setDateOfLastUpdate(resultSet.getLong(5));
|
|
|
|
stmt = conn.prepareStatement(selectDBQueryForType);
|
|
|
|
//TODO:- Ownership is not a enum in DeviceDAO
|
|
|
|
stmt.setInt(1, type);
|
|
|
|
device.setOwnerShip(resultSet.getString(6));
|
|
|
|
resultSet = stmt.executeQuery();
|
|
|
|
device.setStatus(Status.valueOf(resultSet.getString(7)));
|
|
|
|
devicesList = new ArrayList<Device>();
|
|
|
|
device.setDeviceTypeId(resultSet.getInt(8));
|
|
|
|
while (resultSet.next()) {
|
|
|
|
device.setDeviceIdentificationId(resultSet.getString(9));
|
|
|
|
Device device = new Device();
|
|
|
|
device.setOwnerId(resultSet.getString(10));
|
|
|
|
device.setId(resultSet.getInt(1));
|
|
|
|
device.setTenantId(resultSet.getInt(11));
|
|
|
|
device.setDescription(resultSet.getString(2));
|
|
|
|
}
|
|
|
|
device.setName(resultSet.getString(3));
|
|
|
|
} catch (SQLException e) {
|
|
|
|
device.setDateOfEnrollment(resultSet.getLong(4));
|
|
|
|
String msg = "Error occurred while listing devices for type '" + type + "'";
|
|
|
|
device.setDateOfLastUpdate(resultSet.getLong(5));
|
|
|
|
log.error(msg, e);
|
|
|
|
//TODO:- Ownership is not a enum in DeviceDAO
|
|
|
|
throw new DeviceManagementDAOException(msg, e);
|
|
|
|
device.setOwnerShip(resultSet.getString(6));
|
|
|
|
} finally {
|
|
|
|
device.setStatus(Status.valueOf(resultSet.getString(7)));
|
|
|
|
DeviceManagementDAOUtil.cleanupResources(conn, stmt, resultSet);
|
|
|
|
device.setDeviceTypeId(resultSet.getInt(8));
|
|
|
|
}
|
|
|
|
device.setDeviceIdentificationId(resultSet.getString(9));
|
|
|
|
return device;
|
|
|
|
device.setOwnerId(resultSet.getString(10));
|
|
|
|
}
|
|
|
|
device.setTenantId(resultSet.getInt(11));
|
|
|
|
|
|
|
|
devicesList.add(device);
|
|
|
|
@Override public List<Device> getDevices() throws DeviceManagementDAOException {
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
} catch (SQLException e) {
|
|
|
|
}
|
|
|
|
String msg = "Error occurred while listing devices for type '" + type + "'";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
@Override public List<Device> getDevices(Integer type) throws DeviceManagementDAOException {
|
|
|
|
throw new DeviceManagementDAOException(msg, e);
|
|
|
|
Connection conn = null;
|
|
|
|
} finally {
|
|
|
|
PreparedStatement stmt = null;
|
|
|
|
DeviceManagementDAOUtil.cleanupResources(conn, stmt, resultSet);
|
|
|
|
ResultSet resultSet = null;
|
|
|
|
}
|
|
|
|
List<Device> devicesList = null;
|
|
|
|
return devicesList;
|
|
|
|
try {
|
|
|
|
}
|
|
|
|
conn = this.getConnection();
|
|
|
|
|
|
|
|
String selectDBQueryForType = "SELECT ID, DESCRIPTION, NAME, DATE_OF_ENROLLMENT, " +
|
|
|
|
private Connection getConnection() throws DeviceManagementDAOException {
|
|
|
|
"DATE_OF_LAST_UPDATE, OWNERSHIP, STATUS, DEVICE_TYPE_ID, " +
|
|
|
|
try {
|
|
|
|
"DEVICE_IDENTIFICATION, OWNER, TENANT_ID FROM DM_DEVICE " +
|
|
|
|
return dataSource.getConnection();
|
|
|
|
"WHERE DM_DEVICE.DEVICE_TYPE_ID=?";
|
|
|
|
} catch (SQLException e) {
|
|
|
|
stmt = conn.prepareStatement(selectDBQueryForType);
|
|
|
|
throw new DeviceManagementDAOException("Error occurred while obtaining a connection from the device " +
|
|
|
|
stmt.setInt(1, type);
|
|
|
|
"management metadata repository datasource", e);
|
|
|
|
resultSet = stmt.executeQuery();
|
|
|
|
}
|
|
|
|
devicesList = new ArrayList<Device>();
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|