Implemented missing methods in group management.

revert-70aa11f8
charithag 9 years ago
parent 3fd141acdf
commit 2f0a3388a6

@ -67,11 +67,22 @@ public interface GroupDAO {
/** /**
* Get the list of Device Groups in tenant. * 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<DeviceGroupBuilder> getGroups(PaginationRequest request, int tenantId) throws GroupManagementDAOException;
/**
* Get count of Device Groups in tenant.
*
* @param tenantId of user's tenant. * @param tenantId of user's tenant.
* @return List of all Device Groups in tenant. * @return List of all Device Groups in tenant.
* @throws GroupManagementDAOException * @throws GroupManagementDAOException
*/ */
List<DeviceGroupBuilder> getGroups(int tenantId) throws GroupManagementDAOException; int getGroupCount(int tenantId) throws GroupManagementDAOException;
/** /**
* Get the list of Groups that matches with the given DeviceGroup name. * Get the list of Groups that matches with the given DeviceGroup name.

@ -147,16 +147,21 @@ public class GroupDAOImpl implements GroupDAO {
} }
@Override @Override
public List<DeviceGroupBuilder> getGroups(int tenantId) throws GroupManagementDAOException { public List<DeviceGroupBuilder> getGroups(PaginationRequest request, int tenantId)
throws GroupManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null; ResultSet resultSet = null;
List<DeviceGroupBuilder> deviceGroupList = null; List<DeviceGroupBuilder> deviceGroupList = null;
try { try {
Connection conn = GroupManagementDAOFactory.getConnection(); Connection conn = GroupManagementDAOFactory.getConnection();
String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, DATE_OF_CREATE, DATE_OF_LAST_UPDATE, OWNER " 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 = conn.prepareStatement(sql);
stmt.setInt(1, tenantId); stmt.setInt(1, tenantId);
//noinspection JpaQueryApiInspection
stmt.setInt(2, request.getStartIndex());
//noinspection JpaQueryApiInspection
stmt.setInt(3, request.getRowCount());
resultSet = stmt.executeQuery(); resultSet = stmt.executeQuery();
deviceGroupList = new ArrayList<>(); deviceGroupList = new ArrayList<>();
while (resultSet.next()) { while (resultSet.next()) {
@ -170,6 +175,28 @@ public class GroupDAOImpl implements GroupDAO {
return deviceGroupList; 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 @Override
public List<DeviceGroupBuilder> findInGroups(String groupName, int tenantId) public List<DeviceGroupBuilder> findInGroups(String groupName, int tenantId)
throws GroupManagementDAOException { throws GroupManagementDAOException {
@ -363,6 +390,7 @@ public class GroupDAOImpl implements GroupDAO {
stmt.setInt(3, tenantId); stmt.setInt(3, tenantId);
//noinspection JpaQueryApiInspection //noinspection JpaQueryApiInspection
stmt.setInt(4, request.getStartIndex()); stmt.setInt(4, request.getStartIndex());
//noinspection JpaQueryApiInspection
stmt.setInt(5, request.getRowCount()); stmt.setInt(5, request.getRowCount());
rs = stmt.executeQuery(); rs = stmt.executeQuery();
devices = new ArrayList<>(); devices = new ArrayList<>();

@ -15,6 +15,7 @@
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*/ */
package org.wso2.carbon.device.mgt.core.service; package org.wso2.carbon.device.mgt.core.service;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
@ -78,6 +79,22 @@ public interface GroupManagementProviderService {
*/ */
List<DeviceGroup> findInGroups(String groupName, String username) throws GroupManagementException; List<DeviceGroup> findInGroups(String groupName, String username) throws GroupManagementException;
/**
* Get all device groups in tenant
*
* @return list of groups
* @throws GroupManagementException
*/
List<DeviceGroup> 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 * Get device groups of user
* *

@ -15,6 +15,7 @@
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*/ */
package org.wso2.carbon.device.mgt.core.service; package org.wso2.carbon.device.mgt.core.service;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -211,6 +212,46 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
return groupsWithData; return groupsWithData;
} }
@Override
public List<DeviceGroup> getGroups(PaginationRequest request) throws GroupManagementException {
List<DeviceGroupBuilder> 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<DeviceGroup> 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} * {@inheritDoc}
*/ */
@ -415,9 +456,14 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
public List<Device> getDevices(int groupId) throws GroupManagementException { public List<Device> getDevices(int groupId) throws GroupManagementException {
try { try {
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
GroupManagementDAOFactory.getConnection();
return this.groupDAO.getDevices(groupId, tenantId); return this.groupDAO.getDevices(groupId, tenantId);
} catch (GroupManagementDAOException e) { } catch (GroupManagementDAOException e) {
throw new GroupManagementException("Error occurred while getting devices in group.", 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 { throws GroupManagementException {
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
try { try {
GroupManagementDAOFactory.getConnection();
return this.groupDAO.getDevices(groupId, request, tenantId); return this.groupDAO.getDevices(groupId, request, tenantId);
} catch (GroupManagementDAOException e) { } catch (GroupManagementDAOException e) {
throw new GroupManagementException("Error occurred while getting devices in group.", 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 { public int getDeviceCount(int groupId) throws GroupManagementException {
try { try {
int count; int count;
GroupManagementDAOFactory.beginTransaction(); GroupManagementDAOFactory.getConnection();
count = groupDAO.getDeviceCount(groupId, count = groupDAO.getDeviceCount(groupId,
CarbonContext.getThreadLocalCarbonContext().getTenantId()); CarbonContext.getThreadLocalCarbonContext().getTenantId());
GroupManagementDAOFactory.commitTransaction();
return count; return count;
} catch (GroupManagementDAOException e) { } catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.rollbackTransaction(); throw new GroupManagementException("Error occurred while retrieving all groups in tenant", e);
throw new GroupManagementException("Error occurred while retrieving device count of group " + } catch (SQLException e) {
"'" + groupId + "'.", e); throw new GroupManagementException("Error occurred while opening a connection to the data source.", e);
} catch (TransactionManagementException e) {
throw new GroupManagementException("Error occurred while initiating transaction.", e);
} finally { } finally {
GroupManagementDAOFactory.closeConnection(); GroupManagementDAOFactory.closeConnection();
} }
@ -473,11 +521,18 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
return false; return false;
} }
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
GroupManagementDAOFactory.beginTransaction();
this.groupDAO.addDevice(groupId, device.getId(), tenantId); this.groupDAO.addDevice(groupId, device.getId(), tenantId);
GroupManagementDAOFactory.commitTransaction();
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
throw new GroupManagementException("Error occurred while retrieving device.", e); throw new GroupManagementException("Error occurred while retrieving device.", e);
} catch (GroupManagementDAOException e) { } catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.rollbackTransaction();
throw new GroupManagementException("Error occurred while adding device to group '" + groupId + "'.", e); 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; return true;
} }
@ -497,11 +552,18 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
return false; return false;
} }
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
GroupManagementDAOFactory.beginTransaction();
this.groupDAO.removeDevice(groupId, device.getId(), tenantId); this.groupDAO.removeDevice(groupId, device.getId(), tenantId);
GroupManagementDAOFactory.commitTransaction();
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
throw new GroupManagementException("Error occurred while retrieving device.", 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) { } catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.rollbackTransaction();
throw new GroupManagementException("Error occurred while adding device to group '" + groupId + "'.", e); throw new GroupManagementException("Error occurred while adding device to group '" + groupId + "'.", e);
} finally {
GroupManagementDAOFactory.closeConnection();
} }
return true; return true;
} }

@ -24,6 +24,7 @@ import org.testng.Assert;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.wso2.carbon.device.mgt.common.Device; 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.TransactionManagementException;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest; import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest;
@ -154,7 +155,8 @@ public class GroupPersistTests extends BaseDeviceManagementTest {
public void getGroupTest() { public void getGroupTest() {
try { try {
GroupManagementDAOFactory.openConnection(); GroupManagementDAOFactory.openConnection();
List<DeviceGroupBuilder> groups = groupDAO.getGroups(TestDataHolder.SUPER_TENANT_ID); PaginationRequest paginationRequest = new PaginationRequest(0, 1000);
List<DeviceGroupBuilder> groups = groupDAO.getGroups(paginationRequest, TestDataHolder.SUPER_TENANT_ID);
Assert.assertNotEquals(groups.size(), 0, "No groups found"); Assert.assertNotEquals(groups.size(), 0, "No groups found");
Assert.assertNotNull(groups.get(0), "Group is null"); Assert.assertNotNull(groups.get(0), "Group is null");
log.debug("No of Groups found: " + groups.size()); log.debug("No of Groups found: " + groups.size());

Loading…
Cancel
Save