From 2f0a3388a6fab59c8e161d968cce722fc41b0512 Mon Sep 17 00:00:00 2001 From: charithag Date: Tue, 29 Mar 2016 18:01:21 +0530 Subject: [PATCH] Implemented missing methods in group management. --- .../mgt/core/group/mgt/dao/GroupDAO.java | 13 +++- .../mgt/core/group/mgt/dao/GroupDAOImpl.java | 32 +++++++- .../GroupManagementProviderService.java | 17 +++++ .../GroupManagementProviderServiceImpl.java | 76 +++++++++++++++++-- .../mgt/core/dao/GroupPersistTests.java | 4 +- 5 files changed, 131 insertions(+), 11 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/group/mgt/dao/GroupDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/group/mgt/dao/GroupDAO.java index 00fdd97a44..a353ff25c6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/group/mgt/dao/GroupDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/group/mgt/dao/GroupDAO.java @@ -67,11 +67,22 @@ public interface GroupDAO { /** * Get the list of Device Groups in tenant. * + * @param request for pagination. + * @param tenantId of user's tenant. + * @return List of all Device Groups in tenant. + * @throws GroupManagementDAOException + */ + List getGroups(PaginationRequest request, int tenantId) throws GroupManagementDAOException; + + + /** + * Get count of Device Groups in tenant. + * * @param tenantId of user's tenant. * @return List of all Device Groups in tenant. * @throws GroupManagementDAOException */ - List getGroups(int tenantId) throws GroupManagementDAOException; + int getGroupCount(int tenantId) throws GroupManagementDAOException; /** * Get the list of Groups that matches with the given DeviceGroup name. diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/group/mgt/dao/GroupDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/group/mgt/dao/GroupDAOImpl.java index 0a2004dff2..702e20c44a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/group/mgt/dao/GroupDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/group/mgt/dao/GroupDAOImpl.java @@ -147,16 +147,21 @@ public class GroupDAOImpl implements GroupDAO { } @Override - public List getGroups(int tenantId) throws GroupManagementDAOException { + public List getGroups(PaginationRequest request, int tenantId) + throws GroupManagementDAOException { PreparedStatement stmt = null; ResultSet resultSet = null; List deviceGroupList = null; try { Connection conn = GroupManagementDAOFactory.getConnection(); String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, DATE_OF_CREATE, DATE_OF_LAST_UPDATE, OWNER " - + "FROM DM_GROUP WHERE TENANT_ID = ?"; + + "FROM DM_GROUP WHERE TENANT_ID = ? LIMIT ?, ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); + //noinspection JpaQueryApiInspection + stmt.setInt(2, request.getStartIndex()); + //noinspection JpaQueryApiInspection + stmt.setInt(3, request.getRowCount()); resultSet = stmt.executeQuery(); deviceGroupList = new ArrayList<>(); while (resultSet.next()) { @@ -170,6 +175,28 @@ public class GroupDAOImpl implements GroupDAO { return deviceGroupList; } + @Override + public int getGroupCount(int tenantId) throws GroupManagementDAOException { + PreparedStatement stmt = null; + ResultSet resultSet = null; + try { + Connection conn = GroupManagementDAOFactory.getConnection(); + String sql = "SELECT COUNT(ID) AS DEVICE_COUNT FROM DM_DEVICE_GROUP_MAP WHERE TENANT_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, tenantId); + resultSet = stmt.executeQuery(); + if (resultSet.next()) { + return resultSet.getInt("DEVICE_COUNT"); + } else { + return 0; + } + } catch (SQLException e) { + throw new GroupManagementDAOException("Error occurred while getting group count'", e); + } finally { + GroupManagementDAOUtil.cleanupResources(stmt, resultSet); + } + } + @Override public List findInGroups(String groupName, int tenantId) throws GroupManagementDAOException { @@ -363,6 +390,7 @@ public class GroupDAOImpl implements GroupDAO { stmt.setInt(3, tenantId); //noinspection JpaQueryApiInspection stmt.setInt(4, request.getStartIndex()); + //noinspection JpaQueryApiInspection stmt.setInt(5, request.getRowCount()); rs = stmt.executeQuery(); devices = new ArrayList<>(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java index 7c47929802..b202917b6d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java @@ -15,6 +15,7 @@ * specific language governing permissions and limitations * under the License. */ + package org.wso2.carbon.device.mgt.core.service; import org.wso2.carbon.device.mgt.common.Device; @@ -78,6 +79,22 @@ public interface GroupManagementProviderService { */ List findInGroups(String groupName, String username) throws GroupManagementException; + /** + * Get all device groups in tenant + * + * @return list of groups + * @throws GroupManagementException + */ + List getGroups(PaginationRequest request) throws GroupManagementException; + + /** + * Get all device group count in tenant + * + * @return group count + * @throws GroupManagementException + */ + int getGroupCount() throws GroupManagementException; + /** * Get device groups of user * diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java index 985be91cf0..02788d78ac 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java @@ -15,6 +15,7 @@ * specific language governing permissions and limitations * under the License. */ + package org.wso2.carbon.device.mgt.core.service; import org.apache.commons.logging.Log; @@ -211,6 +212,46 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid return groupsWithData; } + @Override + public List getGroups(PaginationRequest request) throws GroupManagementException { + List deviceGroups = new ArrayList<>(); + try { + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + GroupManagementDAOFactory.openConnection(); + deviceGroups = this.groupDAO.getGroups(request, tenantId); + } catch (GroupManagementDAOException e) { + throw new GroupManagementException("Error occurred while retrieving all groups in tenant", e); + } catch (SQLException e) { + throw new GroupManagementException("Error occurred while opening a connection to the data source.", e); + } finally { + GroupManagementDAOFactory.closeConnection(); + } + List groupsWithData = new ArrayList<>(); + for (DeviceGroupBuilder groupBroker : deviceGroups) { + groupBroker.setUsers(this.getUsers(groupBroker.getId())); + groupBroker.setRoles(this.getRoles(groupBroker.getId())); + groupsWithData.add(groupBroker.getGroup()); + } + return groupsWithData; + } + + @Override + public int getGroupCount() throws GroupManagementException { + try { + int count; + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + GroupManagementDAOFactory.openConnection(); + count = groupDAO.getGroupCount(tenantId); + return count; + } catch (GroupManagementDAOException e) { + throw new GroupManagementException("Error occurred while retrieving all groups in tenant", e); + } catch (SQLException e) { + throw new GroupManagementException("Error occurred while opening a connection to the data source.", e); + } finally { + GroupManagementDAOFactory.closeConnection(); + } + } + /** * {@inheritDoc} */ @@ -415,9 +456,14 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid public List getDevices(int groupId) throws GroupManagementException { try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + GroupManagementDAOFactory.getConnection(); return this.groupDAO.getDevices(groupId, tenantId); } catch (GroupManagementDAOException e) { throw new GroupManagementException("Error occurred while getting devices in group.", e); + } catch (SQLException e) { + throw new GroupManagementException("Error occurred while opening a connection to the data source.", e); + } finally { + GroupManagementDAOFactory.closeConnection(); } } @@ -429,9 +475,14 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid throws GroupManagementException { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); try { + GroupManagementDAOFactory.getConnection(); return this.groupDAO.getDevices(groupId, request, tenantId); } catch (GroupManagementDAOException e) { throw new GroupManagementException("Error occurred while getting devices in group.", e); + } catch (SQLException e) { + throw new GroupManagementException("Error occurred while opening a connection to the data source.", e); + } finally { + GroupManagementDAOFactory.closeConnection(); } } @@ -442,17 +493,14 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid public int getDeviceCount(int groupId) throws GroupManagementException { try { int count; - GroupManagementDAOFactory.beginTransaction(); + GroupManagementDAOFactory.getConnection(); count = groupDAO.getDeviceCount(groupId, CarbonContext.getThreadLocalCarbonContext().getTenantId()); - GroupManagementDAOFactory.commitTransaction(); return count; } catch (GroupManagementDAOException e) { - GroupManagementDAOFactory.rollbackTransaction(); - throw new GroupManagementException("Error occurred while retrieving device count of group " + - "'" + groupId + "'.", e); - } catch (TransactionManagementException e) { - throw new GroupManagementException("Error occurred while initiating transaction.", e); + throw new GroupManagementException("Error occurred while retrieving all groups in tenant", e); + } catch (SQLException e) { + throw new GroupManagementException("Error occurred while opening a connection to the data source.", e); } finally { GroupManagementDAOFactory.closeConnection(); } @@ -473,11 +521,18 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid return false; } int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + GroupManagementDAOFactory.beginTransaction(); this.groupDAO.addDevice(groupId, device.getId(), tenantId); + GroupManagementDAOFactory.commitTransaction(); } catch (DeviceManagementException e) { throw new GroupManagementException("Error occurred while retrieving device.", e); } catch (GroupManagementDAOException e) { + GroupManagementDAOFactory.rollbackTransaction(); throw new GroupManagementException("Error occurred while adding device to group '" + groupId + "'.", e); + } catch (TransactionManagementException e) { + throw new GroupManagementException("Error occurred while initiating transaction.", e); + } finally { + GroupManagementDAOFactory.closeConnection(); } return true; } @@ -497,11 +552,18 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid return false; } int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + GroupManagementDAOFactory.beginTransaction(); this.groupDAO.removeDevice(groupId, device.getId(), tenantId); + GroupManagementDAOFactory.commitTransaction(); } catch (DeviceManagementException e) { throw new GroupManagementException("Error occurred while retrieving device.", e); + } catch (TransactionManagementException e) { + throw new GroupManagementException("Error occurred while initiating transaction.", e); } catch (GroupManagementDAOException e) { + GroupManagementDAOFactory.rollbackTransaction(); throw new GroupManagementException("Error occurred while adding device to group '" + groupId + "'.", e); + } finally { + GroupManagementDAOFactory.closeConnection(); } return true; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/GroupPersistTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/GroupPersistTests.java index 0e7772fa0c..29bfa22a5d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/GroupPersistTests.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/GroupPersistTests.java @@ -24,6 +24,7 @@ import org.testng.Assert; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.TransactionManagementException; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest; @@ -154,7 +155,8 @@ public class GroupPersistTests extends BaseDeviceManagementTest { public void getGroupTest() { try { GroupManagementDAOFactory.openConnection(); - List groups = groupDAO.getGroups(TestDataHolder.SUPER_TENANT_ID); + PaginationRequest paginationRequest = new PaginationRequest(0, 1000); + List groups = groupDAO.getGroups(paginationRequest, TestDataHolder.SUPER_TENANT_ID); Assert.assertNotEquals(groups.size(), 0, "No groups found"); Assert.assertNotNull(groups.get(0), "Group is null"); log.debug("No of Groups found: " + groups.size());