Fixing minor issues on GroupDAO

4.x.x
Rasika Perera 8 years ago
parent a2db592d69
commit 54211bc3cc

@ -60,7 +60,7 @@ public class GroupDAOImpl implements GroupDAO {
return groupId; return groupId;
} catch (SQLException e) { } catch (SQLException e) {
throw new GroupManagementDAOException("Error occurred while adding deviceGroup '" + throw new GroupManagementDAOException("Error occurred while adding deviceGroup '" +
deviceGroup.getName() + "'", e); deviceGroup.getName() + "'", e);
} finally { } finally {
GroupManagementDAOUtil.cleanupResources(stmt, null); GroupManagementDAOUtil.cleanupResources(stmt, null);
} }
@ -72,7 +72,8 @@ public class GroupDAOImpl implements GroupDAO {
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
Connection conn = GroupManagementDAOFactory.getConnection(); Connection conn = GroupManagementDAOFactory.getConnection();
String sql = "UPDATE DM_GROUP SET DESCRIPTION = ?, GROUP_NAME = ?, OWNER = ? WHERE ID = ? AND TENANT_ID = ?"; String sql =
"UPDATE DM_GROUP SET DESCRIPTION = ?, GROUP_NAME = ?, OWNER = ? WHERE ID = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setString(1, deviceGroup.getDescription()); stmt.setString(1, deviceGroup.getDescription());
stmt.setString(2, deviceGroup.getName()); stmt.setString(2, deviceGroup.getName());
@ -82,7 +83,7 @@ public class GroupDAOImpl implements GroupDAO {
stmt.executeUpdate(); stmt.executeUpdate();
} catch (SQLException e) { } catch (SQLException e) {
throw new GroupManagementDAOException("Error occurred while updating deviceGroup '" + throw new GroupManagementDAOException("Error occurred while updating deviceGroup '" +
deviceGroup.getName() + "'", e); deviceGroup.getName() + "'", e);
} finally { } finally {
GroupManagementDAOUtil.cleanupResources(stmt, null); GroupManagementDAOUtil.cleanupResources(stmt, null);
} }
@ -142,7 +143,7 @@ public class GroupDAOImpl implements GroupDAO {
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new GroupManagementDAOException("Error occurred while obtaining information of Device Group '" + throw new GroupManagementDAOException("Error occurred while obtaining information of Device Group '" +
groupId + "'", e); groupId + "'", e);
} finally { } finally {
GroupManagementDAOUtil.cleanupResources(stmt, resultSet); GroupManagementDAOUtil.cleanupResources(stmt, resultSet);
} }
@ -156,14 +157,14 @@ public class GroupDAOImpl implements GroupDAO {
try { try {
Connection conn = GroupManagementDAOFactory.getConnection(); Connection conn = GroupManagementDAOFactory.getConnection();
String sql = "SELECT G.ID, G.GROUP_NAME, G.DESCRIPTION, G.OWNER FROM DM_GROUP G " + String sql = "SELECT G.ID, G.GROUP_NAME, G.DESCRIPTION, G.OWNER FROM DM_GROUP G " +
"INNER JOIN DM_DEVICE_GROUP_MAP GM ON G.ID = GM.GROUP_ID " + "INNER JOIN DM_DEVICE_GROUP_MAP GM ON G.ID = GM.GROUP_ID " +
"WHERE GM.DEVICE_ID = ? AND GM.TENANT_ID = ?"; "WHERE GM.DEVICE_ID = ? AND GM.TENANT_ID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, deviceId); stmt.setInt(1, deviceId);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
resultSet = stmt.executeQuery(); resultSet = stmt.executeQuery();
while (resultSet.next()) { while (resultSet.next()) {
deviceGroupBuilders.add(GroupManagementDAOUtil.loadGroup(resultSet)); deviceGroupBuilders.add(GroupManagementDAOUtil.loadGroup(resultSet));
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new GroupManagementDAOException("Error occurred while obtaining information of Device Groups ", e); throw new GroupManagementDAOException("Error occurred while obtaining information of Device Groups ", e);
@ -205,7 +206,7 @@ public class GroupDAOImpl implements GroupDAO {
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(paramIndex++, tenantId); stmt.setInt(paramIndex++, tenantId);
if (hasGroupName) { if (hasGroupName) {
stmt.setString(paramIndex++, groupName + "%"); stmt.setString(paramIndex++, groupName + "%");
} }
if (hasOwner) { if (hasOwner) {
stmt.setString(paramIndex++, owner + "%"); stmt.setString(paramIndex++, owner + "%");
@ -230,7 +231,8 @@ public class GroupDAOImpl implements GroupDAO {
@Override @Override
public List<DeviceGroup> getGroups(GroupPaginationRequest request, List<Integer> deviceGroupIds, public List<DeviceGroup> getGroups(GroupPaginationRequest request, List<Integer> deviceGroupIds,
int tenantId) throws GroupManagementDAOException { int tenantId) throws GroupManagementDAOException {
if (deviceGroupIds.size() == 0) { int deviceGroupIdsCount = deviceGroupIds.size();
if (deviceGroupIdsCount == 0) {
return new ArrayList<>(); return new ArrayList<>();
} }
PreparedStatement stmt = null; PreparedStatement stmt = null;
@ -255,10 +257,10 @@ public class GroupDAOImpl implements GroupDAO {
hasOwner = true; hasOwner = true;
} }
sql += " AND ID IN ("; sql += " AND ID IN (";
for (int i = 0; i < deviceGroupIds.size() - 1; i++) { for (int i = 0; i < deviceGroupIdsCount; i++) {
sql += "?,"; sql += (deviceGroupIdsCount - 1 != i) ? "?," : "?";
} }
sql += "?)"; sql += ")";
if (hasLimit) { if (hasLimit) {
sql += " LIMIT ?, ?"; sql += " LIMIT ?, ?";
} }
@ -388,7 +390,8 @@ public class GroupDAOImpl implements GroupDAO {
ResultSet resultSet = null; ResultSet resultSet = null;
try { try {
Connection conn = GroupManagementDAOFactory.getConnection(); Connection conn = GroupManagementDAOFactory.getConnection();
String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER FROM DM_GROUP WHERE GROUP_NAME = ? AND TENANT_ID = ?"; String sql =
"SELECT ID, DESCRIPTION, GROUP_NAME, OWNER FROM DM_GROUP WHERE GROUP_NAME = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setString(1, groupName); stmt.setString(1, groupName);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
@ -469,7 +472,8 @@ public class GroupDAOImpl implements GroupDAO {
ResultSet resultSet = null; ResultSet resultSet = null;
try { try {
Connection conn = GroupManagementDAOFactory.getConnection(); Connection conn = GroupManagementDAOFactory.getConnection();
String sql = "SELECT COUNT(ID) AS DEVICE_COUNT FROM DM_DEVICE_GROUP_MAP WHERE GROUP_ID = ? AND TENANT_ID = ?"; String sql =
"SELECT COUNT(ID) AS DEVICE_COUNT FROM DM_DEVICE_GROUP_MAP WHERE GROUP_ID = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, groupId); stmt.setInt(1, groupId);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
@ -496,13 +500,15 @@ public class GroupDAOImpl implements GroupDAO {
try { try {
conn = GroupManagementDAOFactory.getConnection(); conn = GroupManagementDAOFactory.getConnection();
String sql = "SELECT d1.DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " + String sql = "SELECT d1.DEVICE_ID, d1.DESCRIPTION, d1.NAME AS DEVICE_NAME, d1.DEVICE_TYPE, " +
"d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " + "d1.DEVICE_IDENTIFICATION, e.OWNER, e.OWNERSHIP, e.STATUS, e.DATE_OF_LAST_UPDATE, " +
"e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, " + "e.DATE_OF_ENROLMENT, e.ID AS ENROLMENT_ID FROM DM_ENROLMENT e, " +
"(SELECT gd.DEVICE_ID, gd.DESCRIPTION, gd.NAME, gd.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM " + "(SELECT gd.DEVICE_ID, gd.DESCRIPTION, gd.NAME, gd.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE " +
"(SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, d.DEVICE_TYPE_ID FROM DM_DEVICE d, (" + "FROM " +
"SELECT dgm.DEVICE_ID FROM DM_DEVICE_GROUP_MAP dgm WHERE dgm.GROUP_ID = ?) dgm1 " + "(SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME, d.DEVICE_IDENTIFICATION, d.DEVICE_TYPE_ID FROM" +
"WHERE d.ID = dgm1.DEVICE_ID AND d.TENANT_ID = ?) gd, DM_DEVICE_TYPE t " + " DM_DEVICE d, (" +
"WHERE gd.DEVICE_TYPE_ID = t.ID) d1 WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ? , ?"; "SELECT dgm.DEVICE_ID FROM DM_DEVICE_GROUP_MAP dgm WHERE dgm.GROUP_ID = ?) dgm1 " +
"WHERE d.ID = dgm1.DEVICE_ID AND d.TENANT_ID = ?) gd, DM_DEVICE_TYPE t " +
"WHERE gd.DEVICE_TYPE_ID = t.ID) d1 WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ? , ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, groupId); stmt.setInt(1, groupId);
@ -520,7 +526,7 @@ public class GroupDAOImpl implements GroupDAO {
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new GroupManagementDAOException("Error occurred while retrieving information of all " + throw new GroupManagementDAOException("Error occurred while retrieving information of all " +
"registered devices", e); "registered devices", e);
} finally { } finally {
DeviceManagementDAOUtil.cleanupResources(stmt, rs); DeviceManagementDAOUtil.cleanupResources(stmt, rs);
} }
@ -589,7 +595,8 @@ public class GroupDAOImpl implements GroupDAO {
@Override @Override
public List<DeviceGroup> getGroups(String[] roles, int tenantId) throws GroupManagementDAOException { public List<DeviceGroup> getGroups(String[] roles, int tenantId) throws GroupManagementDAOException {
if (roles.length == 0) { int rolesCount = roles.length;
if (rolesCount == 0) {
return new ArrayList<>(); return new ArrayList<>();
} }
PreparedStatement stmt = null; PreparedStatement stmt = null;
@ -598,18 +605,18 @@ public class GroupDAOImpl implements GroupDAO {
try { try {
Connection conn = GroupManagementDAOFactory.getConnection(); Connection conn = GroupManagementDAOFactory.getConnection();
String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER FROM DM_GROUP g, " + String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER FROM DM_GROUP g, " +
"(SELECT GROUP_ID FROM DM_ROLE_GROUP_MAP WHERE ROLE IN ("; "(SELECT GROUP_ID FROM DM_ROLE_GROUP_MAP WHERE ROLE IN (";
int index = 1; int index = 0;
while (index++ <= roles.length) { while (index++ < rolesCount) {
sql += "?"; sql += (rolesCount - 1 != index) ? "?," : "?";
} }
sql += ")) gr WHERE g.ID = gr.GROUP_ID AND TENANT_ID = ? GROUP BY g.ID"; sql += ")) gr WHERE g.ID = gr.GROUP_ID AND TENANT_ID = ? GROUP BY g.ID";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
index = 1; index = 0;
while (index++ <= roles.length) { while (index++ < rolesCount) {
stmt.setString(index, roles[index++]); stmt.setString(index, roles[index - 1]);
} }
stmt.setInt(index, tenantId); stmt.setInt(index, tenantId);
resultSet = stmt.executeQuery(); resultSet = stmt.executeQuery();
@ -637,15 +644,16 @@ public class GroupDAOImpl implements GroupDAO {
Connection conn = GroupManagementDAOFactory.getConnection(); Connection conn = GroupManagementDAOFactory.getConnection();
String sql = "SELECT ID FROM DM_GROUP g, (SELECT GROUP_ID FROM DM_ROLE_GROUP_MAP WHERE ROLE IN ("; String sql = "SELECT ID FROM DM_GROUP g, (SELECT GROUP_ID FROM DM_ROLE_GROUP_MAP WHERE ROLE IN (";
for (int i = 0; i < roles.length - 1; i++) { int rolesCount = roles.length;
sql += "?,"; for (int i = 0; i < rolesCount; i++) {
sql += (rolesCount - 1 != i) ? "?," : "?";
} }
sql += "?)) gr WHERE g.ID = gr.GROUP_ID AND TENANT_ID = ? GROUP BY g.ID"; sql += ")) gr WHERE g.ID = gr.GROUP_ID AND TENANT_ID = ? GROUP BY g.ID";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
int index = 1; int index = 0;
while (index++ <= roles.length) { while (index++ < rolesCount) {
stmt.setString(index, roles[index++]); stmt.setString(index, roles[index - 1]);
} }
stmt.setInt(index, tenantId); stmt.setInt(index, tenantId);
resultSet = stmt.executeQuery(); resultSet = stmt.executeQuery();
@ -663,7 +671,8 @@ public class GroupDAOImpl implements GroupDAO {
@Override @Override
public int getGroupsCount(String[] roles, int tenantId) throws GroupManagementDAOException { public int getGroupsCount(String[] roles, int tenantId) throws GroupManagementDAOException {
if (roles.length == 0) { int rolesCount = roles.length;
if (rolesCount == 0) {
return 0; return 0;
} }
PreparedStatement stmt = null; PreparedStatement stmt = null;
@ -671,18 +680,16 @@ public class GroupDAOImpl implements GroupDAO {
try { try {
Connection conn = GroupManagementDAOFactory.getConnection(); Connection conn = GroupManagementDAOFactory.getConnection();
String sql = "SELECT COUNT(ID) AS GROUP_COUNT FROM DM_GROUP g, " + String sql = "SELECT COUNT(ID) AS GROUP_COUNT FROM DM_GROUP g, " +
"(SELECT GROUP_ID FROM DM_ROLE_GROUP_MAP WHERE ROLE IN ("; "(SELECT GROUP_ID FROM DM_ROLE_GROUP_MAP WHERE ROLE IN (";
for (int i = 0; i < rolesCount; i++) {
int index = 1; sql += (rolesCount - 1 != i) ? "?," : "?";
while (index++ <= roles.length) {
sql += "?";
} }
sql += ")) gr WHERE g.ID = gr.GROUP_ID AND TENANT_ID = ? GROUP BY g.ID"; sql += ")) gr WHERE g.ID = gr.GROUP_ID AND TENANT_ID = ? GROUP BY g.ID";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
index = 1; int index = 0;
while (index++ <= roles.length) { while (index++ < rolesCount) {
stmt.setString(index, roles[index++]); stmt.setString(index, roles[index - 1]);
} }
stmt.setInt(index, tenantId); stmt.setInt(index, tenantId);
resultSet = stmt.executeQuery(); resultSet = stmt.executeQuery();
@ -716,7 +723,7 @@ public class GroupDAOImpl implements GroupDAO {
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new GroupManagementDAOException("Error occurred while getting own groups of user '" throw new GroupManagementDAOException("Error occurred while getting own groups of user '"
+ username + "'", e); + username + "'", e);
} finally { } finally {
GroupManagementDAOUtil.cleanupResources(stmt, resultSet); GroupManagementDAOUtil.cleanupResources(stmt, resultSet);
} }
@ -741,7 +748,7 @@ public class GroupDAOImpl implements GroupDAO {
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new GroupManagementDAOException("Error occurred while getting own groups of user '" throw new GroupManagementDAOException("Error occurred while getting own groups of user '"
+ username + "'", e); + username + "'", e);
} finally { } finally {
GroupManagementDAOUtil.cleanupResources(stmt, resultSet); GroupManagementDAOUtil.cleanupResources(stmt, resultSet);
} }
@ -766,7 +773,7 @@ public class GroupDAOImpl implements GroupDAO {
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new GroupManagementDAOException("Error occurred while getting own groups count of user '" throw new GroupManagementDAOException("Error occurred while getting own groups count of user '"
+ username + "'", e); + username + "'", e);
} finally { } finally {
GroupManagementDAOUtil.cleanupResources(stmt, resultSet); GroupManagementDAOUtil.cleanupResources(stmt, resultSet);
} }

Loading…
Cancel
Save