Fixing DeviceDAOs to associate tenant id to filter out tenant specific resources

revert-70aa11f8
prabathabey 10 years ago
parent 0e18ebcf4e
commit 60ee259b2b

@ -33,7 +33,8 @@ public interface DeviceDAO {
void updateDevice(int typeId, Device device, int tenantId) throws DeviceManagementDAOException; void updateDevice(int typeId, Device device, int tenantId) throws DeviceManagementDAOException;
void updateDeviceStatus(DeviceIdentifier deviceId, Status status, int tenantId) throws DeviceManagementDAOException; void updateDeviceStatus(DeviceIdentifier deviceId, Status status,
int tenantId) throws DeviceManagementDAOException;
void deleteDevice(DeviceIdentifier deviceId, int tenantId) throws DeviceManagementDAOException; void deleteDevice(DeviceIdentifier deviceId, int tenantId) throws DeviceManagementDAOException;
@ -41,9 +42,6 @@ public interface DeviceDAO {
List<Device> getDevices(int tenantId) throws DeviceManagementDAOException; List<Device> getDevices(int tenantId) throws DeviceManagementDAOException;
List<Integer> getDeviceIds(List<DeviceIdentifier> devices,
int tenantId) throws DeviceManagementDAOException;
/** /**
* @param type - The device type id. * @param type - The device type id.
* @return a list of devices based on the type id. * @return a list of devices based on the type id.

@ -75,11 +75,12 @@ public class DeviceDAOImpl implements DeviceDAO {
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
conn = this.getConnection(); conn = this.getConnection();
String sql = "UPDATE DM_DEVICE SET STATUS=?, OWNER=? WHERE DEVICE_IDENTIFICATION = ?"; String sql = "UPDATE DM_DEVICE SET STATUS = ?, OWNER = ? WHERE DEVICE_IDENTIFICATION = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setString(1, device.getEnrolmentInfo().getStatus().toString()); stmt.setString(1, device.getEnrolmentInfo().getStatus().toString());
stmt.setString(2, device.getEnrolmentInfo().getOwner()); stmt.setString(2, device.getEnrolmentInfo().getOwner());
stmt.setString(3, device.getDeviceIdentifier()); stmt.setString(3, device.getDeviceIdentifier());
stmt.setInt(4, tenantId);
stmt.executeUpdate(); stmt.executeUpdate();
} catch (SQLException e) { } catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while enrolling device '" + throw new DeviceManagementDAOException("Error occurred while enrolling device '" +
@ -111,10 +112,11 @@ public class DeviceDAOImpl implements DeviceDAO {
String sql = String sql =
"SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME AS DEVICE_NAME, d.DATE_OF_ENROLLMENT, d.DATE_OF_LAST_UPDATE, d.OWNERSHIP, d.STATUS, " + "SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME AS DEVICE_NAME, d.DATE_OF_ENROLLMENT, d.DATE_OF_LAST_UPDATE, d.OWNERSHIP, d.STATUS, " +
"d.DEVICE_TYPE_ID, d.DEVICE_IDENTIFICATION, d.OWNER, d.TENANT_ID FROM DM_DEVICE d, DM_DEVICE_TYPE dt WHERE " + "d.DEVICE_TYPE_ID, d.DEVICE_IDENTIFICATION, d.OWNER, d.TENANT_ID FROM DM_DEVICE d, DM_DEVICE_TYPE dt WHERE " +
"dt.NAME = ? AND d.DEVICE_IDENTIFICATION = ?"; "dt.NAME = ? AND d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setString(1, deviceId.getType()); stmt.setString(1, deviceId.getType());
stmt.setString(2, deviceId.getId()); stmt.setString(2, deviceId.getId());
stmt.setInt(3, tenantId);
rs = stmt.executeQuery(); rs = stmt.executeQuery();
if (rs.next()) { if (rs.next()) {
device = this.loadDevice(rs); device = this.loadDevice(rs);
@ -139,8 +141,9 @@ public class DeviceDAOImpl implements DeviceDAO {
String sql = "SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME AS DEVICE_NAME, d.DATE_OF_ENROLLMENT, " + String sql = "SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME AS DEVICE_NAME, d.DATE_OF_ENROLLMENT, " +
"d.DATE_OF_LAST_UPDATE, d.OWNERSHIP, d.STATUS, d.DEVICE_TYPE_ID, " + "d.DATE_OF_LAST_UPDATE, d.OWNERSHIP, d.STATUS, d.DEVICE_TYPE_ID, " +
"d.DEVICE_IDENTIFICATION, d.OWNER, d.TENANT_ID, t.NAME AS DEVICE_TYPE_NAME FROM DM_DEVICE d, DEVICE_TYPE t " + "d.DEVICE_IDENTIFICATION, d.OWNER, d.TENANT_ID, t.NAME AS DEVICE_TYPE_NAME FROM DM_DEVICE d, DEVICE_TYPE t " +
"WHERE d.DEVICE_TYPE_ID = t.ID "; "WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ? ";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId);
rs = stmt.executeQuery(); rs = stmt.executeQuery();
devices = new ArrayList<Device>(); devices = new ArrayList<Device>();
while (rs.next()) { while (rs.next()) {
@ -156,30 +159,6 @@ public class DeviceDAOImpl implements DeviceDAO {
return devices; return devices;
} }
@Override
public List<Integer> getDeviceIds(List<DeviceIdentifier> devices,
int tenantId) throws DeviceManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
try {
List<Integer> deviceIds = new ArrayList<Integer>();
Connection conn = this.getConnection();
String sql = "SELECT DISTINCT ID FROM DEVICE WHERE NAME IN (?) AND ID IN (?)";
stmt = conn.prepareStatement(sql);
//stmt.setArray(1, new java.sql.Date[0]);
stmt.setString(2, "");
rs = stmt.executeQuery();
while (rs.next()) {
deviceIds.add(rs.getInt("ID"));
}
return deviceIds;
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while retrieving device ids", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs);
}
}
@Override @Override
public List<Device> getDevices(String type, int tenantId) throws DeviceManagementDAOException { public List<Device> getDevices(String type, int tenantId) throws DeviceManagementDAOException {
Connection conn; Connection conn;
@ -190,9 +169,10 @@ public class DeviceDAOImpl implements DeviceDAO {
conn = this.getConnection(); conn = this.getConnection();
String selectDBQueryForType = "SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME AS DEVICE_NAME, d.DATE_OF_ENROLLMENT, d.DATE_OF_LAST_UPDATE, " + String selectDBQueryForType = "SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME AS DEVICE_NAME, d.DATE_OF_ENROLLMENT, d.DATE_OF_LAST_UPDATE, " +
"d.OWNERSHIP, d.STATUS, d.DEVICE_TYPE_ID, d.DEVICE_IDENTIFICATION, d.OWNER, d.TENANT_ID FROM DM_DEVICE d, DM_DEVICE_TYPE t " + "d.OWNERSHIP, d.STATUS, d.DEVICE_TYPE_ID, d.DEVICE_IDENTIFICATION, d.OWNER, d.TENANT_ID FROM DM_DEVICE d, DM_DEVICE_TYPE t " +
"WHERE d.DM_DEVICE.DEVICE_TYPE_ID = t.ID AND t.NAME = ?"; "WHERE d.DM_DEVICE.DEVICE_TYPE_ID = t.ID AND t.NAME = ? AND d.TENANT_ID = ?";
stmt = conn.prepareStatement(selectDBQueryForType); stmt = conn.prepareStatement(selectDBQueryForType);
stmt.setString(1, type); stmt.setString(1, type);
stmt.setInt(2, tenantId);
rs = stmt.executeQuery(); rs = stmt.executeQuery();
devices = new ArrayList<Device>(); devices = new ArrayList<Device>();
while (rs.next()) { while (rs.next()) {
@ -220,7 +200,7 @@ public class DeviceDAOImpl implements DeviceDAO {
"d.OWNERSHIP, d.STATUS, d.DEVICE_TYPE_ID, " + "d.OWNERSHIP, d.STATUS, d.DEVICE_TYPE_ID, " +
"d.DEVICE_IDENTIFICATION, d.OWNER, d.TENANT_ID FROM " + "d.DEVICE_IDENTIFICATION, d.OWNER, d.TENANT_ID FROM " +
"DM_DEVICE d, DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID " + "DM_DEVICE d, DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID " +
"AND d.OWNER =? AND d.TENANT_ID =?"); "AND d.OWNER =? AND d.TENANT_ID = ?");
stmt.setString(1, username); stmt.setString(1, username);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
ResultSet rs = stmt.executeQuery(); ResultSet rs = stmt.executeQuery();
@ -252,20 +232,21 @@ public class DeviceDAOImpl implements DeviceDAO {
public int getDeviceCount(int tenantId) throws DeviceManagementDAOException { public int getDeviceCount(int tenantId) throws DeviceManagementDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null; ResultSet rs = null;
int deviceCount = 0; int deviceCount = 0;
try { try {
conn = this.getConnection(); conn = this.getConnection();
String selectDBQueryForType = "SELECT COUNT(DM_DEVICE.ID) FROM DM_DEVICE"; String sql = "SELECT COUNT(ID) AS DEVICE_COUNT FROM DM_DEVICE WHERE TENANT_ID = ? ";
stmt = conn.prepareStatement(selectDBQueryForType); stmt = conn.prepareStatement(sql);
resultSet = stmt.executeQuery(); stmt.setInt(1, tenantId);
while (resultSet.next()) { rs = stmt.executeQuery();
deviceCount = resultSet.getInt(1); if (rs.next()) {
deviceCount = rs.getInt("DEVICE_COUNT");
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while getting the device count", e); throw new DeviceManagementDAOException("Error occurred while getting the device count", e);
} finally { } finally {
DeviceManagementDAOUtil.cleanupResources(stmt, resultSet); DeviceManagementDAOUtil.cleanupResources(stmt, rs);
} }
return deviceCount; return deviceCount;
} }
@ -274,7 +255,7 @@ public class DeviceDAOImpl implements DeviceDAO {
* Get the list of devices that matches with the given device name. * Get the list of devices that matches with the given device name.
* *
* @param deviceName Name of the device. * @param deviceName Name of the device.
* @param tenantId * @param tenantId Id of the current tenant
* @return device list * @return device list
* @throws DeviceManagementDAOException * @throws DeviceManagementDAOException
*/ */
@ -282,7 +263,7 @@ public class DeviceDAOImpl implements DeviceDAO {
public List<Device> getDevicesByName(String deviceName, int tenantId) throws DeviceManagementDAOException { public List<Device> getDevicesByName(String deviceName, int tenantId) throws DeviceManagementDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
List<Device> deviceList = new ArrayList<Device>(); List<Device> devices = new ArrayList<Device>();
try { try {
conn = this.getConnection(); conn = this.getConnection();
stmt = conn.prepareStatement( stmt = conn.prepareStatement(
@ -298,7 +279,7 @@ public class DeviceDAOImpl implements DeviceDAO {
while (rs.next()) { while (rs.next()) {
Device device = this.loadDevice(rs); Device device = this.loadDevice(rs);
deviceList.add(device); devices.add(device);
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches " + throw new DeviceManagementDAOException("Error occurred while fetching the list of devices that matches " +
@ -306,7 +287,7 @@ public class DeviceDAOImpl implements DeviceDAO {
} finally { } finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null); DeviceManagementDAOUtil.cleanupResources(stmt, null);
} }
return deviceList; return devices;
} }
private Device loadDevice(ResultSet rs) throws SQLException { private Device loadDevice(ResultSet rs) throws SQLException {

Loading…
Cancel
Save