Fixing minor issues on GroupDAO

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

@ -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());
@ -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);
@ -498,8 +502,10 @@ public class GroupDAOImpl implements GroupDAO {
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 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 ? , ?";
@ -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;
@ -600,16 +607,16 @@ public class GroupDAOImpl implements GroupDAO {
String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER FROM DM_GROUP g, " +
"(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;
@ -672,17 +681,15 @@ public class GroupDAOImpl implements GroupDAO {
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 += "?";
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();

Loading…
Cancel
Save