Fixing minor issues on GroupDAO

revert-70aa11f8
Rasika Perera 8 years ago
parent a2db592d69
commit 54211bc3cc

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

Loading…
Cancel
Save